KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > edu > rice > cs > drjava > model > definitions > reducedmodel > BackSlashTest


1 /*BEGIN_COPYRIGHT_BLOCK
2  *
3  * This file is part of DrJava. Download the current version of this project from http://www.drjava.org/
4  * or http://sourceforge.net/projects/drjava/
5  *
6  * DrJava Open Source License
7  *
8  * Copyright (C) 2001-2005 JavaPLT group at Rice University (javaplt@rice.edu). All rights reserved.
9  *
10  * Developed by: Java Programming Languages Team, Rice University, http://www.cs.rice.edu/~javaplt/
11  *
12  * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
13  * documentation files (the "Software"), to deal with the Software without restriction, including without limitation
14  * the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and
15  * to permit persons to whom the Software is furnished to do so, subject to the following conditions:
16  *
17  * - Redistributions of source code must retain the above copyright notice, this list of conditions and the
18  * following disclaimers.
19  * - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
20  * following disclaimers in the documentation and/or other materials provided with the distribution.
21  * - Neither the names of DrJava, the JavaPLT, Rice University, nor the names of its contributors may be used to
22  * endorse or promote products derived from this Software without specific prior written permission.
23  * - Products derived from this software may not be called "DrJava" nor use the term "DrJava" as part of their
24  * names without prior written permission from the JavaPLT group. For permission, write to javaplt@rice.edu.
25  *
26  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
27  * THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
28  * CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
29  * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
30  * WITH THE SOFTWARE.
31  *
32  *END_COPYRIGHT_BLOCK*/

33
34 package edu.rice.cs.drjava.model.definitions.reducedmodel;
35
36 import junit.framework.*;
37
38 /**
39  * Test the interactions between double quotes and backslashes.
40  * @version $Id: BackSlashTest.java 3553 2006-02-20 21:22:09Z rcartwright $
41  */

42 public final class BackSlashTest extends BraceReductionTestCase
43   implements ReducedModelStates
44 {
45
46   protected ReducedModelControl model0;
47   protected ReducedModelControl model1;
48   protected ReducedModelControl model2;
49
50   /**
51    * Initializes the reduced models used in the tests.
52    */

53   protected void setUp() throws Exception JavaDoc {
54     super.setUp();
55     model0 = new ReducedModelControl();
56     model1 = new ReducedModelControl();
57     model2 = new ReducedModelControl();
58   }
59
60   /**
61    * Creates a test suite for JUnit to use.
62    * @return a test suite for JUnit
63    */

64   public static Test suite() {
65     return new TestSuite(BackSlashTest.class);
66   }
67
68   /**
69    * Convenience function to insert a number of non-special characters into a reduced model.
70    * @param model the model being modified
71    * @param size the number of characters being inserted
72    */

73   protected void insertGap(BraceReduction model, int size) {
74     for (int i = 0; i < size; i++) {
75       model.insertChar(' ');
76     }
77   }
78
79   /**
80    * Tests the relationship between backslash characters and quote characters.
81    * It focuses on the case where the backslash is inserted first before the quote.
82    */

83   public void testInsideQuotePrevious() {
84     model1.insertChar('\"');
85     model1.insertChar('\\');
86     model1.insertChar('\"');
87     model1.move(-2);
88     // "#\"
89
assertEquals("#0.0", "\\\"", model1.currentToken().getType());
90     assertEquals("#0.1", INSIDE_DOUBLE_QUOTE, stateOfCurrentToken(model1));
91     model1.move(2);
92     model1.insertChar('\"');
93     model1.move(-1);
94     // "\"#"
95
assertEquals("#1.0", "\"", model1.currentToken().getType());
96     assertEquals("#1.1", FREE, stateOfCurrentToken(model1));
97     assertTrue("#1.2", model1.currentToken().isClosed());
98     model1.move(1);
99     model1.insertChar('\"');
100     model1.insertChar('\\');
101     model1.insertChar('\\');
102     model1.move(-2);
103     // "\"""#\\
104
assertEquals("#2.0", "\\\\", model1.currentToken().getType());
105     assertEquals("#2.1", INSIDE_DOUBLE_QUOTE, stateOfCurrentToken(model1));
106     model1.move(2);
107     model1.insertChar('\\');
108     model1.move(-1);
109     // "\"""\\#\
110
assertEquals("#3.0", "\\", model1.currentToken().getType());
111     assertEquals("#3.1", INSIDE_DOUBLE_QUOTE, stateOfCurrentToken(model1));
112     model1.move(1);
113     model1.insertChar('\"');
114     model1.move(-1);
115     // "\"""\\\#"
116
assertEquals("#4.0", "\\\"", model1.currentToken().getType());
117     assertEquals("#4.1", INSIDE_DOUBLE_QUOTE, stateOfCurrentToken(model1));
118   }
119
120   /**
121    * Tests the relationship between backslashes and quotes.
122    * Focuses on the case where a backslash is inserted and turns a regular quote
123    * into an escaped quote.
124    */

125   public void testInsideQuoteNext() {
126     model1.insertChar('\"');
127     model1.insertChar('\"');
128     model1.move(-1);
129     model1.insertChar('\\');
130     assertEquals("#0.0", "\\\"", model1.currentToken().getType());
131     assertEquals("#0.1", INSIDE_DOUBLE_QUOTE, stateOfCurrentToken(model1));
132     assertEquals("#0.2", 1, model1.getBlockOffset());
133     model1.move(1);
134     model1.insertChar('\"');
135     model1.move(-1);
136     assertEquals("#1.0", "\"", model1.currentToken().getType());
137     assertEquals("#1.1", FREE, stateOfCurrentToken(model1));
138     assertTrue("#1.2", model1.currentToken().isClosed());
139     model1.move(1);
140     model1.insertChar('\"');
141     model1.insertChar('\\');
142     model1.move(-1);
143     model1.insertChar('\\');
144     assertEquals("#2.0", "\\\\", model1.currentToken().getType());
145     assertEquals("#2.1", INSIDE_DOUBLE_QUOTE, stateOfCurrentToken(model1));
146     assertEquals("#2.2", 6, model1.absOffset());
147     model1.move(-2);
148     model1.insertChar('{');
149     model1.move(-1);
150     assertEquals("#3.0", "{", model1.currentToken().getType());
151     assertEquals("#3.1", FREE, stateOfCurrentToken(model1));
152     model1.move(1);
153     model1.move(3);
154     model1.insertChar('\"');
155     model1.move(-1);
156     assertEquals("#4.0", "\"", model1.currentToken().getType());
157     assertEquals("#4.1", FREE, stateOfCurrentToken(model1));
158     assertTrue("#4.2", model1.currentToken().isClosed());
159     model1.insertChar('\\');
160     assertEquals("#5.0", "\\\"", model1.currentToken().getType());
161     assertEquals("#5.1", INSIDE_DOUBLE_QUOTE, stateOfCurrentToken(model1));
162     assertEquals("#5.2", 1, model1.getBlockOffset());
163   }
164
165   /**
166    * Tests the case when a backslash is inserted before two backslashes.
167    * The existing double escape is broken and the first two backslashes become
168    * a double escape with the third backslash ending up alone.
169    */

170   public void testBackSlashBeforeDoubleEscape() {
171     model1.insertChar('\\');
172     model1.insertChar('\\');
173     model1.move(-2);
174     model1.insertChar('\\');
175     assertEquals("#0.0", "\\\\", model1.currentToken().getType());
176     assertEquals("#0.1", 2, model1.currentToken().getSize());
177     model1.move(1);
178     assertEquals("#0.2", "\\", model1.currentToken().getType());
179     model2.insertChar('\\');
180     model2.insertChar('\"');
181     model2.move(-2);
182     model2.insertChar('\\');
183     assertEquals("#1.0", "\\\\", model2.currentToken().getType());
184     assertEquals("#1.1", 1, model2.absOffset());
185     model2.move(1);
186     assertEquals("#1.2", "\"", model2.currentToken().getType());
187   }
188
189   /**
190    * Tests the case where a backslash breaks up two backslashes together.
191    * The newly inserted backslash and the first backslash form a new double escape
192    * and the second backslash in the previous double escape becomes free.
193    */

194   public void testInsertBetweenDoubleEscape() {
195     model1.insertChar('\\');
196     model1.insertChar('\\');
197     model1.move(-1);
198     model1.insertChar('\\');
199     model1.move(-2);
200     assertEquals("#0.0", "\\\\", model1.currentToken().getType());
201     model1.move(2);
202     assertEquals("#0.1", "\\", model1.currentToken().getType());
203     model2.insertChar('\\');
204     model2.insertChar('\"');
205     model2.move(-1);
206     model2.insertChar('\\');
207     model2.move(-2);
208     assertEquals("#1.0", "\\\\", model2.currentToken().getType());
209     model2.move(2);
210     assertEquals("#1.1", "\"", model2.currentToken().getType());
211     model0.insertChar('\\');
212     model0.insertChar('\\');
213     model0.move(-1);
214     model0.insertChar(')');
215     model0.move(-2);
216     assertEquals("#2.0", "\\", model0.currentToken().getType());
217     model0.move(1);
218     assertEquals("#2.1", ")", model0.currentToken().getType());
219     model0.move(1);
220     assertEquals("#2.2", "\\", model0.currentToken().getType());
221     model0.move(1);
222     model0.delete(-3);
223     model0.insertChar('\\');
224     model0.insertChar('\"');
225     model0.move(-1);
226     model0.insertChar(')');
227     model0.move(-2);
228     assertEquals("#3.0", "\\", model0.currentToken().getType());
229     model0.move(1);
230     assertEquals("#3.1", ")", model0.currentToken().getType());
231     model0.move(1);
232     assertEquals("#3.2", "\"", model0.currentToken().getType());
233   }
234
235   /**
236    * Tests the case where deletion combines a backslash and a quote or two backslashes.
237    * The deletion of characters in between the two special characters brings them together
238    * and unites them into a 2-character special token.
239    */

240   public void testDeleteAndCombine() {
241     model0.insertChar('\\');
242     insertGap(model0, 2);
243     model0.insertChar('\"');
244     model0.move(-1);
245     assertEquals("#0.0", "\"", model0.currentToken().getType());
246     model0.delete(-2);
247     assertEquals("#1.0", "\\\"", model0.currentToken().getType());
248     assertEquals("#1.1", 1, model0.absOffset());
249     model0.delete(1);
250     insertGap(model0, 2);
251     model0.insertChar('\\');
252     model0.move(-1);
253     assertEquals("#2.0", "\\", model0.currentToken().getType());
254     model0.delete(-2);
255     assertEquals("#3.0", "\\\\", model0.currentToken().getType());
256     assertEquals("#3.1", 2, model0.currentToken().getSize());
257   }
258
259   /**
260    * Tests more of the same sort of cases as found in testDeleteAndCombine().
261    */

262   public void testDeleteAndCombine2() {
263     model0.insertChar('\\');
264     model0.insertChar('\"');
265     model0.move(-1);
266     model0.delete(-1);
267     assertEquals("#0.0", "\"", model0.currentToken().getType());
268     assertEquals("#0.1", FREE, model0.getStateAtCurrent());
269     model1.insertChar('\\');
270     model1.insertChar('\\');
271     model1.delete(-1);
272     model1.move(-1);
273     assertEquals("#1.0", "\\", model1.currentToken().getType());
274     model1.move(1);
275     model1.insertChar('\\');
276     model1.move(-1);
277     model1.delete(-1);
278     assertEquals("#2.0", "\\", model1.currentToken().getType());
279   }
280
281   /**
282    * More of the same sort of cases as found in testDeleteAndCombine().
283    */

284   public void testDeleteAndCombine3() {
285     model0.insertChar('\\');
286     model0.insertChar('\\');
287     insertGap(model0, 3);
288     model0.insertChar('\\');
289     model0.move(-1);
290     model0.delete(-4);
291     assertEquals("#0.0", "\\\\", model0.currentToken().getType());
292     assertEquals("#0.1", 1, model0.absOffset());
293     model1.insertChar('\\');
294     insertGap(model1, 3);
295     model1.insertChar('\\');
296     model1.insertChar('\"');
297     model1.move(-1);
298     model1.delete(-4);
299     assertEquals("#1.0", "\\\"", model1.currentToken().getType());
300     assertEquals("#1.1", 1, model1.absOffset());
301   }
302
303   /**
304    * Tests cases where a long chain of backslashes and quotes can be all altered with a simple
305    * insertion or deletion of a special character.
306    */

307   public void testChainEffect() {
308     model0.insertChar('\"');
309     model0.insertChar('\\');
310     model0.insertChar('\"');
311     model0.insertChar('\"');
312     model0.insertChar('\"');
313     model0.insertChar('\\');
314     model0.insertChar('\"');
315     model0.insertChar('\"');
316     model0.insertChar('\"');
317     model0.insertChar('\\');
318     model0.insertChar('\"');
319     model0.insertChar('\"');
320     // "\"""\"""\""#
321
model0.move(-1);
322     assertEquals("#0.0", "\"", model0.currentToken().getType());
323     assertTrue("#0.1", model0.currentToken().isClosed());
324     model0.move(-2);
325     // "\"""\"""#\""
326
assertEquals("#1.0", "\\\"", model0.currentToken().getType());
327     assertEquals("#1.1", INSIDE_DOUBLE_QUOTE, stateOfCurrentToken(model0));
328     model0.move(-1);
329     assertEquals("#1.2", "\"", model0.currentToken().getType());
330     assertEquals("#1.3", FREE, stateOfCurrentToken(model0));
331     assertTrue("#1.4", model0.currentToken().isOpen());
332     model0.move(1);
333     model0.insertChar('\\');
334     // "\"""\"""\#\""
335
assertEquals("#2.0", "\\\\", model0.currentToken().getType());
336     assertEquals("#2.1", INSIDE_DOUBLE_QUOTE, stateOfCurrentToken(model0));
337     assertEquals("#2.2", 10, model0.absOffset());
338     model0.move(-2);
339     assertEquals("#2.3", "\"", model0.currentToken().getType());
340     assertEquals("#2.4", FREE, stateOfCurrentToken(model0));
341     assertTrue("#2.5", model0.currentToken().isOpen());
342     model0.move(3);
343     assertEquals("#2.6", "\"", model0.currentToken().getType());
344     assertEquals("#2.7", FREE, stateOfCurrentToken(model0));
345     assertTrue("#2.8", model0.currentToken().isClosed());
346     model0.move(-1);
347     model0.insertChar('\"');
348     // "\"""\"""\"#\""
349
assertEquals("#3.0", "\\\"", model0.currentToken().getType());
350     assertEquals("#3.1", INSIDE_DOUBLE_QUOTE, stateOfCurrentToken(model0));
351     assertEquals("#3.2", 11, model0.absOffset());
352     model0.move(-2);
353     assertEquals("#3.3", "\\\"", model0.currentToken().getType());
354     assertEquals("#3.4", INSIDE_DOUBLE_QUOTE, stateOfCurrentToken(model0));
355     model0.move(4);
356     assertEquals("#3.5", "\"", model0.currentToken().getType());
357     assertEquals("#3.6", FREE, stateOfCurrentToken(model0));
358     assertTrue("#3.7", model0.currentToken().isClosed());
359     model0.move(-12);
360     // "#\"""\"""\"\""
361
model0.delete(1);
362     // "#"""\"""\"\""
363
model0.move(-1);
364     // #""""\"""\"\""
365
assertEquals("#4.0", "\"", model0.currentToken().getType());
366     assertTrue("#4.1", model0.currentToken().isOpen());
367     assertEquals("#4.2", FREE, stateOfCurrentToken(model0));
368     model0.move(1);
369     // "#"""\"""\"\""
370
assertEquals("#4.3", "\"", model0.currentToken().getType());
371     assertTrue("#4.4", model0.currentToken().isClosed());
372     assertEquals("#4.5", FREE, stateOfCurrentToken(model0));
373     model0.move(1);
374     // ""#""\"""\"\""
375
assertEquals("#5.0", "\"", model0.currentToken().getType());
376     assertTrue("#5.1", model0.currentToken().isOpen());
377     assertEquals("#5.2", FREE, stateOfCurrentToken(model0));
378     model0.move(1);
379     // """#"\"""\"\""
380
assertEquals("#5.3", "\"", model0.currentToken().getType());
381     assertTrue("#5.4", model0.currentToken().isClosed());
382     assertEquals("#5.5", FREE, stateOfCurrentToken(model0));
383     model0.move(1);
384     // """"#\"""\"\""
385
assertEquals("#5.6", "\\\"", model0.currentToken().getType());
386     assertEquals("#5.7", FREE, stateOfCurrentToken(model0));
387     model0.move(2);
388     // """"\"#""\"\""
389
assertEquals("#6.0", "\"", model0.currentToken().getType());
390     assertTrue("#6.1", model0.currentToken().isOpen());
391     assertEquals("#6.2", FREE, stateOfCurrentToken(model0));
392     model0.move(1);
393     // """"\""#"\"\""
394
assertEquals("#6.3", "\"", model0.currentToken().getType());
395     assertTrue("#6.4", model0.currentToken().isClosed());
396     assertEquals("#6.5", FREE, stateOfCurrentToken(model0));
397     model0.move(1);
398     // """"\"""#\"\""
399
assertEquals("#6.6", "\\\"", model0.currentToken().getType());
400     assertEquals("#6.7", FREE, stateOfCurrentToken(model0));
401     model0.move(2);
402     // """"\"""\"#\""
403
assertEquals("#6.0", "\\\"", model0.currentToken().getType());
404     assertEquals("#6.1", FREE, stateOfCurrentToken(model0));
405     model0.move(2);
406     // """"\"""\"\"#"
407
assertEquals("#6.2", "\"", model0.currentToken().getType());
408     assertTrue("#6.3", model0.currentToken().isOpen());
409     assertEquals("#6.4", FREE, stateOfCurrentToken(model0));
410   }
411 }
412
413
414
415
Popular Tags