KickJava   Java API By Example, From Geeks To Geeks.

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


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 /**
37  * Tests the interaction between quotes and backslashes.
38  * @version $Id: SingleQuoteTest.java 3553 2006-02-20 21:22:09Z rcartwright $
39  */

40 public final class SingleQuoteTest extends BraceReductionTestCase
41   implements ReducedModelStates
42 {
43   /**
44    * Tests the relationship between backslash characters and quote characters.
45    * It focuses on the case where the backslash is inserted first before the quote.
46    */

47   public void testInsideQuotePrevious() {
48     model1.insertChar('\'');
49     model1.insertChar('\\');
50     model1.insertChar('\'');
51     model1.move(-2);
52     // "#\"
53
assertEquals("#0.0", "\\'", model1.currentToken().getType());
54     assertEquals("#0.1", INSIDE_SINGLE_QUOTE, stateOfCurrentToken(model1));
55     model1.move(2);
56     model1.insertChar('\'');
57     model1.move(-1);
58     // '\'#'
59
assertEquals("#1.0", "\'", model1.currentToken().getType());
60     assertEquals("#1.1", FREE, stateOfCurrentToken(model1));
61     assertTrue("#1.2", model1.currentToken().isClosed());
62     model1.move(1);
63     model1.insertChar('\'');
64     model1.insertChar('\\');
65     model1.insertChar('\\');
66     model1.move(-2);
67     // '\'''#\\
68
assertEquals("#2.0", "\\\\", model1.currentToken().getType());
69     assertEquals("#2.1", INSIDE_SINGLE_QUOTE, stateOfCurrentToken(model1));
70     model1.move(2);
71     model1.insertChar('\\');
72     model1.move(-1);
73     // '\'''\\#\
74
assertEquals("#3.0", "\\", model1.currentToken().getType());
75     assertEquals("#3.1", INSIDE_SINGLE_QUOTE, stateOfCurrentToken(model1));
76     model1.move(1);
77     model1.insertChar('\'');
78     model1.move(-1);
79     // '\'''\\\#"
80
assertEquals("#4.0", "\\'", model1.currentToken().getType());
81     assertEquals("#4.1", INSIDE_SINGLE_QUOTE, stateOfCurrentToken(model1));
82   }
83
84   /**
85    * Tests the relationship between backslashes and quotes.
86    * Focuses on the case where a backslash is inserted and turns a regular quote
87    * into an escaped quote.
88    */

89   public void testInsideQuoteNext() {
90     model1.insertChar('\'');
91     model1.insertChar('\'');
92     model1.move(-1);
93     model1.insertChar('\\');
94     assertEquals("#0.0", "\\'", model1.currentToken().getType());
95     assertEquals("#0.1", INSIDE_SINGLE_QUOTE, stateOfCurrentToken(model1));
96     assertEquals("#0.2", 1, model1.getBlockOffset());
97     model1.move(1);
98     model1.insertChar('\'');
99     model1.move(-1);
100     assertEquals("#1.0", "\'", model1.currentToken().getType());
101     assertEquals("#1.1", FREE, stateOfCurrentToken(model1));
102     assertTrue("#1.2", model1.currentToken().isClosed());
103     model1.move(1);
104     model1.insertChar('\'');
105     model1.insertChar('\\');
106     model1.move(-1);
107     model1.insertChar('\\');
108     assertEquals("#2.0", "\\\\", model1.currentToken().getType());
109     assertEquals("#2.1", INSIDE_SINGLE_QUOTE, stateOfCurrentToken(model1));
110     assertEquals("#2.2", 6, model1.absOffset());
111     model1.move(-2);
112     model1.insertChar('{');
113     model1.move(-1);
114     assertEquals("#3.0", "{", model1.currentToken().getType());
115     assertEquals("#3.1", FREE, stateOfCurrentToken(model1));
116     model1.move(1);
117     model1.move(3);
118     model1.insertChar('\'');
119     model1.move(-1);
120     assertEquals("#4.0", "\'", model1.currentToken().getType());
121     assertEquals("#4.1", FREE, stateOfCurrentToken(model1));
122     assertTrue("#4.2", model1.currentToken().isClosed());
123     model1.insertChar('\\');
124     assertEquals("#5.0", "\\'", model1.currentToken().getType());
125     assertEquals("#5.1", INSIDE_SINGLE_QUOTE, stateOfCurrentToken(model1));
126     assertEquals("#5.2", 1, model1.getBlockOffset());
127   }
128
129   /**
130    * Tests the case when a backslash is inserted before two backslashes.
131    * The existing double escape is broken and the first two backslashes become
132    * a double escape with the third backslash ending up alone.
133    */

134   public void testBackSlashBeforeDoubleEscape() {
135     model1.insertChar('\\');
136     model1.insertChar('\\');
137     model1.move(-2);
138     model1.insertChar('\\');
139     assertEquals("#0.0", "\\\\", model1.currentToken().getType());
140     assertEquals("#0.1", 2, model1.currentToken().getSize());
141     model1.move(1);
142     assertEquals("#0.2", "\\", model1.currentToken().getType());
143     model2.insertChar('\\');
144     model2.insertChar('\'');
145     model2.move(-2);
146     model2.insertChar('\\');
147     assertEquals("#1.0", "\\\\", model2.currentToken().getType());
148     assertEquals("#1.1", 1, model2.absOffset());
149     model2.move(1);
150     assertEquals("#1.2", "'", model2.currentToken().getType());
151   }
152
153   /**
154    * Tests the case where a backslash breaks up two backslashes together.
155    * The newly inserted backslash and the first backslash form a new double escape
156    * and the second backslash in the previous double escape becomes free.
157    */

158   public void testInsertBetweenDoubleEscape() {
159     model1.insertChar('\\');
160     model1.insertChar('\\');
161     model1.move(-1);
162     model1.insertChar('\\');
163     model1.move(-2);
164     assertEquals("#0.0", "\\\\", model1.currentToken().getType());
165     model1.move(2);
166     assertEquals("#0.1", "\\", model1.currentToken().getType());
167     model2.insertChar('\\');
168     model2.insertChar('\'');
169     model2.move(-1);
170     model2.insertChar('\\');
171     model2.move(-2);
172     assertEquals("#1.0", "\\\\", model2.currentToken().getType());
173     model2.move(2);
174     assertEquals("#1.1", "\'", model2.currentToken().getType());
175     model0.insertChar('\\');
176     model0.insertChar('\\');
177     model0.move(-1);
178     model0.insertChar(')');
179     model0.move(-2);
180     assertEquals("#2.0", "\\", model0.currentToken().getType());
181     model0.move(1);
182     assertEquals("#2.1", ")", model0.currentToken().getType());
183     model0.move(1);
184     assertEquals("#2.2", "\\", model0.currentToken().getType());
185     model0.move(1);
186     model0.delete(-3);
187     model0.insertChar('\\');
188     model0.insertChar('\'');
189     model0.move(-1);
190     model0.insertChar(')');
191     model0.move(-2);
192     assertEquals("#3.0", "\\", model0.currentToken().getType());
193     model0.move(1);
194     assertEquals("#3.1", ")", model0.currentToken().getType());
195     model0.move(1);
196     assertEquals("#3.2", "'", model0.currentToken().getType());
197   }
198
199   /**
200    * Tests the case where deletion combines a backslash and a quote or two backslashes.
201    * The deletion of characters in between the two special characters brings them together
202    * and unites them into a 2-character special token.
203    */

204   public void testDeleteAndCombine() {
205     model0.insertChar('\\');
206     insertGap(model0, 2);
207     model0.insertChar('\'');
208     model0.move(-1);
209     assertEquals("#0.0", "\'", model0.currentToken().getType());
210     model0.delete(-2);
211     assertEquals("#1.0", "\\'", model0.currentToken().getType());
212     assertEquals("#1.1", 1, model0.absOffset());
213     model0.delete(1);
214     insertGap(model0, 2);
215     model0.insertChar('\\');
216     model0.move(-1);
217     assertEquals("#2.0", "\\", model0.currentToken().getType());
218     model0.delete(-2);
219     assertEquals("#3.0", "\\\\", model0.currentToken().getType());
220     assertEquals("#3.1", 2, model0.currentToken().getSize());
221   }
222
223   /**
224    * Tests more of the same sort of cases as found in testDeleteAndCombine().
225    */

226   public void testDeleteAndCombine2() {
227     model0.insertChar('\\');
228     model0.insertChar('\'');
229     model0.move(-1);
230     model0.delete(-1);
231     assertEquals("#0.0", "\'", model0.currentToken().getType());
232     assertEquals("#0.1", FREE, model0.getStateAtCurrent());
233     model1.insertChar('\\');
234     model1.insertChar('\\');
235     model1.delete(-1);
236     model1.move(-1);
237     assertEquals("#1.0", "\\", model1.currentToken().getType());
238     model1.move(1);
239     model1.insertChar('\\');
240     model1.move(-1);
241     model1.delete(-1);
242     assertEquals("#2.0", "\\", model1.currentToken().getType());
243   }
244
245   /**
246    * More of the same sort of cases as found in testDeleteAndCombine().
247    */

248   public void testDeleteAndCombine3() {
249     model0.insertChar('\\');
250     model0.insertChar('\\');
251     insertGap(model0, 3);
252     model0.insertChar('\\');
253     model0.move(-1);
254     model0.delete(-4);
255     assertEquals("#0.0", "\\\\", model0.currentToken().getType());
256     assertEquals("#0.1", 1, model0.absOffset());
257     model1.insertChar('\\');
258     insertGap(model1, 3);
259     model1.insertChar('\\');
260     model1.insertChar('\'');
261     model1.move(-1);
262     model1.delete(-4);
263     assertEquals("#1.0", "\\\'", model1.currentToken().getType());
264     assertEquals("#1.1", 1, model1.absOffset());
265   }
266
267   /**
268    * Tests cases where a long chain of backslashes and quotes can be all altered with a simple
269    * insertion or deletion of a special character.
270    */

271   public void testChainEffect() {
272     model0.insertChar('\'');
273     model0.insertChar('\\');
274     model0.insertChar('\'');
275     model0.insertChar('\'');
276     model0.insertChar('\'');
277     model0.insertChar('\\');
278     model0.insertChar('\'');
279     model0.insertChar('\'');
280     model0.insertChar('\'');
281     model0.insertChar('\\');
282     model0.insertChar('\'');
283     model0.insertChar('\'');
284     // '\'''\'''\''#
285
model0.move(-1);
286     assertEquals("#0.0", "\'", model0.currentToken().getType());
287     assertTrue("#0.1", model0.currentToken().isClosed());
288     model0.move(-2);
289     // '\'''\'''#\''
290
assertEquals("#1.0", "\\'", model0.currentToken().getType());
291     assertEquals("#1.1", INSIDE_SINGLE_QUOTE, stateOfCurrentToken(model0));
292     model0.move(-1);
293     assertEquals("#1.2", "'", model0.currentToken().getType());
294     assertEquals("#1.3", FREE, stateOfCurrentToken(model0));
295     assertTrue("#1.4", model0.currentToken().isOpen());
296     model0.move(1);
297     model0.insertChar('\\');
298     // '\'''\'''\#\''
299
assertEquals("#2.0", "\\\\", model0.currentToken().getType());
300     assertEquals("#2.1", INSIDE_SINGLE_QUOTE, stateOfCurrentToken(model0));
301     assertEquals("#2.2", 10, model0.absOffset());
302     model0.move(-2);
303     assertEquals("#2.3", "'", model0.currentToken().getType());
304     assertEquals("#2.4", FREE, stateOfCurrentToken(model0));
305     assertTrue("#2.5", model0.currentToken().isOpen());
306     model0.move(3);
307     assertEquals("#2.6", "'", model0.currentToken().getType());
308     assertEquals("#2.7", FREE, stateOfCurrentToken(model0));
309     assertTrue("#2.8", model0.currentToken().isClosed());
310     model0.move(-1);
311     model0.insertChar('\'');
312     // '\'''\'''\'#\''
313
assertEquals("#3.0", "\\'", model0.currentToken().getType());
314     assertEquals("#3.1", INSIDE_SINGLE_QUOTE, stateOfCurrentToken(model0));
315     assertEquals("#3.2", 11, model0.absOffset());
316     model0.move(-2);
317     assertEquals("#3.3", "\\'", model0.currentToken().getType());
318     assertEquals("#3.4", INSIDE_SINGLE_QUOTE, stateOfCurrentToken(model0));
319     model0.move(4);
320     assertEquals("#3.5", "'", model0.currentToken().getType());
321     assertEquals("#3.6", FREE, stateOfCurrentToken(model0));
322     assertTrue("#3.7", model0.currentToken().isClosed());
323     model0.move(-12);
324     // '#\'''\'''\'\''
325
model0.delete(1);
326     // '#'''\'''\'\''
327
model0.move(-1);
328     // #''''\'''\'\''
329
assertEquals("#4.0", "'", model0.currentToken().getType());
330     assertTrue("#4.1", model0.currentToken().isOpen());
331     assertEquals("#4.2", FREE, stateOfCurrentToken(model0));
332     model0.move(1);
333     // '#'''\'''\'\''
334
assertEquals("#4.3", "'", model0.currentToken().getType());
335     assertTrue("#4.4", model0.currentToken().isClosed());
336     assertEquals("#4.5", FREE, stateOfCurrentToken(model0));
337     model0.move(1);
338     // ''#''\'''\'\''
339
assertEquals("#5.0", "'", model0.currentToken().getType());
340     assertTrue("#5.1", model0.currentToken().isOpen());
341     assertEquals("#5.2", FREE, stateOfCurrentToken(model0));
342     model0.move(1);
343     // '''#'\'''\'\''
344
assertEquals("#5.3", "'", model0.currentToken().getType());
345     assertTrue("#5.4", model0.currentToken().isClosed());
346     assertEquals("#5.5", FREE, stateOfCurrentToken(model0));
347     model0.move(1);
348     // ''''#\'''\'\''
349
assertEquals("#5.6", "\\'", model0.currentToken().getType());
350     assertEquals("#5.7", FREE, stateOfCurrentToken(model0));
351     model0.move(2);
352     // ''''\'#''\'\''
353
assertEquals("#6.0", "'", model0.currentToken().getType());
354     assertTrue("#6.1", model0.currentToken().isOpen());
355     assertEquals("#6.2", FREE, stateOfCurrentToken(model0));
356     model0.move(1);
357     // ''''\''#'\'\''
358
assertEquals("#6.3", "'", model0.currentToken().getType());
359     assertTrue("#6.4", model0.currentToken().isClosed());
360     assertEquals("#6.5", FREE, stateOfCurrentToken(model0));
361     model0.move(1);
362     // ''''\'''#\'\''
363
assertEquals("#6.6", "\\'", model0.currentToken().getType());
364     assertEquals("#6.7", FREE, stateOfCurrentToken(model0));
365     model0.move(2);
366     // ''''\'''\'#\''
367
assertEquals("#6.0", "\\'", model0.currentToken().getType());
368     assertEquals("#6.1", FREE, stateOfCurrentToken(model0));
369     model0.move(2);
370     // ''''\'''\'\'#'
371
assertEquals("#6.2", "'", model0.currentToken().getType());
372     assertTrue("#6.3", model0.currentToken().isOpen());
373     assertEquals("#6.4", FREE, stateOfCurrentToken(model0));
374   }
375 }
376
Popular Tags