001: /*BEGIN_COPYRIGHT_BLOCK
002: *
003: * Copyright (c) 2001-2007, JavaPLT group at Rice University (javaplt@rice.edu)
004: * All rights reserved.
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions are met:
008: * * Redistributions of source code must retain the above copyright
009: * notice, this list of conditions and the following disclaimer.
010: * * Redistributions in binary form must reproduce the above copyright
011: * notice, this list of conditions and the following disclaimer in the
012: * documentation and/or other materials provided with the distribution.
013: * * Neither the names of DrJava, the JavaPLT group, Rice University, nor the
014: * names of its contributors may be used to endorse or promote products
015: * derived from this software without specific prior written permission.
016: *
017: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
018: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
019: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
020: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
021: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
022: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
023: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
024: * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
025: * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
026: * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
027: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
028: *
029: * This software is Open Source Initiative approved Open Source Software.
030: * Open Source Initative Approved is a trademark of the Open Source Initiative.
031: *
032: * This file is part of DrJava. Download the current version of this project
033: * from http://www.drjava.org/ or http://sourceforge.net/projects/drjava/
034: *
035: * END_COPYRIGHT_BLOCK*/
036:
037: package edu.rice.cs.drjava.model.definitions.reducedmodel;
038:
039: /**
040: * Test the delete functionality of the reduced model.
041: * @version $Id: ReducedModelDeleteTest.java 4255 2007-08-28 19:17:37Z mgricken $
042: */
043: public final class ReducedModelDeleteTest extends
044: BraceReductionTestCase implements ReducedModelStates {
045: /**
046: * Tests that line comments after code are indeted correctly.
047: */
048: public void testHalfLineComment() {
049: model1.insertChar('/');
050: model1.insertChar('/');
051: model1.move(-2);
052: assertEquals("#0.0", "//", model1.currentToken().getType());
053: assertEquals("#0.1", 0, model1.absOffset());
054: model1.delete(1);
055: assertEquals("#1.0", "/", model1.currentToken().getType());
056: assertEquals("#1.1", 0, model1.absOffset());
057: model1.insertChar('/');
058: model1.delete(1); //This time delete the second slash
059: assertEquals("#2.0", 1, model1.absOffset());
060: model1.move(-1);
061: assertEquals("#2.1", 0, model1.absOffset());
062: assertEquals("#2.2", "/", model1.currentToken().getType());
063: model1.delete(1);
064: assertEquals("#3.0", 0, model1.absOffset());
065: }
066:
067: /**
068: * Tests that inner gaps are deleted correctcly.
069: */
070: public void testInnerGapDelete() {
071: insertGap(model1, 8);
072: assertEquals("#0.0", 8, model1.absOffset());
073: model1.move(-6);
074: assertEquals("#0.0", 2, model1.absOffset());
075: model1.delete(3);
076: assertEquals("#1.0", 2, model1.absOffset());
077: assertEquals("#1.1", 5, model1.currentToken().getSize());
078: model1.move(3);
079: assertEquals("#2.0", 5, model1.absOffset());
080: }
081:
082: /**
083: * Tests reduced model behavioir for deleting text then merging two gaps.
084: */
085: public void testDeleteAndMergeTwoGaps() {
086: insertGap(model1, 5);
087: model1.insertChar('/');
088: assertEquals("#1.0", 6, model1.absOffset());
089: model1.insertChar('*');
090: assertEquals("#2.0", 7, model1.absOffset());
091: insertGap(model1, 6);
092: assertEquals("#3.0", 13, model1.absOffset());
093: model1.move(-9);
094: assertEquals("#4.0", 4, model1.absOffset());
095: assertTrue("#4.1", model1.currentToken().isGap());
096: assertEquals("#4.2", 5, model1.currentToken().getSize());
097: model1.move(2);
098: assertEquals("#5.0", 6, model1.absOffset());
099: assertEquals("#5.2", "/*", model1.currentToken().getType());
100: model1.move(-2);
101: model1.delete(5);
102: assertEquals("#6.0", 4, model1.absOffset());
103: assertTrue("#6.1", model1.currentToken().isGap());
104: assertEquals("#6.2", 8, model1.currentToken().getSize());
105: }
106:
107: public void testDeleteBlockCommentMakesLineComment() {
108: model1.insertChar('/');
109: assertEquals("#0.0", 1, model1.absOffset());
110: assertEquals("#0.1", FREE, model1.getStateAtCurrent());
111: model1.insertChar('*');
112: assertEquals("#1.0", 2, model1.absOffset());
113: assertEquals("#1.1", INSIDE_BLOCK_COMMENT, model1
114: .getStateAtCurrent());
115: insertGap(model1, 2);
116: assertEquals("#2.0", 4, model1.absOffset());
117: assertEquals("#2.1", INSIDE_BLOCK_COMMENT, model1
118: .getStateAtCurrent());
119: model1.insertChar('*');
120: assertEquals("#3.0", 5, model1.absOffset());
121: assertEquals("#3.1", INSIDE_BLOCK_COMMENT, model1
122: .getStateAtCurrent());
123: model1.insertChar('/');
124: assertEquals("#4.0", 6, model1.absOffset());
125: assertEquals("#4.1", FREE, model1.getStateAtCurrent());
126: insertGap(model1, 1);
127: assertEquals("#5.0", 7, model1.absOffset());
128: assertEquals("#5.1", FREE, model1.getStateAtCurrent());
129: // /*__*/_#
130: model1.move(-6);
131: assertEquals("#6.0", 1, model1.absOffset());
132: model1.delete(4);
133: assertEquals("#7.0", 1, model1.absOffset());
134: assertEquals("#7.1", "//", model1.currentToken().getType());
135: assertEquals("#7.3", FREE, model1.getStateAtCurrent());
136: model1.move(1);
137: assertEquals("#7.0", 2, model1.absOffset());
138: assertTrue("#7.1", model1.currentToken().isGap());
139: assertEquals("#7.2", 1, model1.currentToken().getSize());
140: assertEquals("#7.3", INSIDE_LINE_COMMENT, model1
141: .getStateAtCurrent());
142: }
143:
144: public void testLineCommentStealsBlockCommentSlash() {
145: model1.insertChar('/');
146: assertEquals("#0.0", 1, model1.absOffset());
147: insertGap(model1, 2);
148: assertEquals("#1.0", 3, model1.absOffset());
149: model1.insertChar('/');
150: assertEquals("#2.0", 4, model1.absOffset());
151: model1.insertChar('*');
152: assertEquals("#3.0", 5, model1.absOffset());
153: assertEquals("#3.1", INSIDE_BLOCK_COMMENT, model1
154: .getStateAtCurrent());
155: model1.move(-4);
156: model1.delete(2);
157: assertEquals("#4.0", 1, model1.absOffset());
158: assertEquals("#4.1", "//", model1.currentToken().getType());
159: assertEquals("#4.2", FREE, model1.getStateAtCurrent());
160: model1.move(1);
161: assertEquals("#5.0", 2, model1.absOffset());
162: assertEquals("#5.1", "*", model1.currentToken().getType());
163: assertEquals("#5.2", INSIDE_LINE_COMMENT, model1
164: .getStateAtCurrent());
165: assertEquals("#5.3", INSIDE_LINE_COMMENT, model1.currentToken()
166: .getState());
167: }
168:
169: public void testLineCommentStealsLineCommentSlash() {
170: model1.insertChar('/');
171: assertEquals("#0.0", 1, model1.absOffset());
172: insertGap(model1, 2);
173: assertEquals("#1.0", 3, model1.absOffset());
174: model1.insertChar('/');
175: assertEquals("#2.0", 4, model1.absOffset());
176: model1.insertChar('/');
177: assertEquals("#3.0", 5, model1.absOffset());
178: assertEquals("#3.1", INSIDE_LINE_COMMENT, model1
179: .getStateAtCurrent());
180: model1.move(-4);
181: model1.delete(2);
182: assertEquals("#4.0", 1, model1.absOffset());
183: assertEquals("#4.1", "//", model1.currentToken().getType());
184: assertEquals("#4.2", FREE, model1.getStateAtCurrent());
185: model1.move(1);
186: assertEquals("#5.0", 2, model1.absOffset());
187: assertEquals("#5.1", "/", model1.currentToken().getType());
188: assertEquals("#5.2", INSIDE_LINE_COMMENT, model1
189: .getStateAtCurrent());
190: assertEquals("#5.3", INSIDE_LINE_COMMENT, model1.currentToken()
191: .getState());
192: }
193:
194: public void testDeleteNewlineAndShadowBlockCommentStart() {
195: model1.insertChar('/');
196: assertEquals("#0.0", 1, model1.absOffset());
197: assertEquals("#0.1", FREE, model1.getStateAtCurrent());
198: model1.insertChar('/');
199: assertEquals("#1.0", 2, model1.absOffset());
200: assertEquals("#1.1", INSIDE_LINE_COMMENT, model1
201: .getStateAtCurrent());
202: model1.insertChar('*');
203: assertEquals("#2.0", 3, model1.absOffset());
204: assertEquals("#2.1", INSIDE_LINE_COMMENT, model1
205: .getStateAtCurrent());
206: model1.insertChar('\n');
207: assertEquals("#3.0", 4, model1.absOffset());
208: assertEquals("#3.1", FREE, model1.getStateAtCurrent());
209: model1.insertChar('/');
210: assertEquals("#4.0", 5, model1.absOffset());
211: assertEquals("#4.1", FREE, model1.getStateAtCurrent());
212: model1.insertChar('*');
213: assertEquals("#5.0", 6, model1.absOffset());
214: assertEquals("#5.1", INSIDE_BLOCK_COMMENT, model1
215: .getStateAtCurrent());
216: model1.move(-3);
217: assertEquals("#6.0", 3, model1.absOffset());
218: model1.delete(1);
219: assertEquals("#7.0", 3, model1.absOffset());
220: assertEquals("#7.1", "/", model1.currentToken().getType());
221: assertEquals("#7.2", INSIDE_LINE_COMMENT, model1.currentToken()
222: .getState());
223: model1.move(-1);
224: assertEquals("#8.0", 2, model1.absOffset());
225: assertEquals("#8.1", "*", model1.currentToken().getType());
226: assertEquals("#8.2", INSIDE_LINE_COMMENT, model1.currentToken()
227: .getState());
228: model1.move(2);
229: assertEquals("#9.0", 4, model1.absOffset());
230: assertEquals("#9.1", "*", model1.currentToken().getType());
231: assertEquals("#9.2", INSIDE_LINE_COMMENT, model1.currentToken()
232: .getState());
233: }
234:
235: public void testBlockCommentStartEatsEnd() {
236: model1.insertChar('/');
237: assertEquals("#0.0", 1, model1.absOffset());
238: assertEquals("#0.1", FREE, model1.getStateAtCurrent());
239: model1.insertChar('*');
240: assertEquals("#1.0", 2, model1.absOffset());
241: assertEquals("#1.1", INSIDE_BLOCK_COMMENT, model1
242: .getStateAtCurrent());
243: insertGap(model1, 2);
244: assertEquals("#2.0", 4, model1.absOffset());
245: assertEquals("#2.1", INSIDE_BLOCK_COMMENT, model1
246: .getStateAtCurrent());
247: model1.insertChar('*');
248: assertEquals("#3.0", 5, model1.absOffset());
249: assertEquals("#3.1", INSIDE_BLOCK_COMMENT, model1
250: .getStateAtCurrent());
251: model1.insertChar('/');
252: assertEquals("#4.0", 6, model1.absOffset());
253: assertEquals("#4.1", FREE, model1.getStateAtCurrent());
254: model1.move(-5);
255: assertEquals("#4.0", 1, model1.absOffset());
256: assertEquals("#4.1", "/*", model1.currentToken().getType());
257: assertEquals("#4.1", FREE, model1.getStateAtCurrent());
258: model1.delete(3);
259: assertEquals("#5.0", 1, model1.absOffset());
260: assertEquals("#5.1", "/*", model1.currentToken().getType());
261: model1.move(1);
262: assertEquals("#6.0", 2, model1.absOffset());
263: assertEquals("#6.1", "/", model1.currentToken().getType());
264: assertEquals("#6.2", INSIDE_BLOCK_COMMENT, model1
265: .getStateAtCurrent());
266: assertEquals("#6.3", INSIDE_BLOCK_COMMENT, model1
267: .currentToken().getState());
268: model1.move(1);
269: assertEquals("#7.0", 3, model1.absOffset());
270: assertEquals("#7.1", INSIDE_BLOCK_COMMENT, model1
271: .getStateAtCurrent());
272: }
273:
274: public void deleteLineCommentSlashOpensBlockComment() {
275: model1.insertChar('/');
276: model1.insertChar('/');
277: model1.insertChar('*');
278: model1.insertChar('\n');
279: insertGap(model1, 2);
280: model1.insertChar('(');
281: model1.insertChar('*');
282: model1.insertChar('/');
283: assertEquals("#0.0", 9, model1.absOffset());
284: assertEquals("#0.1", FREE, model1.getStateAtCurrent());
285: model1.move(-1);
286: assertEquals("#1.0", 8, model1.absOffset());
287: assertEquals("#1.1", FREE, model1.getStateAtCurrent());
288: assertEquals("#1.2", "/", model1.currentToken().getType());
289: model1.move(-1);
290: assertEquals("#2.0", 7, model1.absOffset());
291: assertEquals("#2.1", FREE, model1.getStateAtCurrent());
292: assertEquals("#2.2", "*", model1.currentToken().getType());
293: model1.move(-1);
294: assertEquals("#3.0", 6, model1.absOffset());
295: assertEquals("#3.1", FREE, model1.getStateAtCurrent());
296: assertEquals("#3.2", "(", model1.currentToken().getType());
297: model1.move(-2);
298: assertEquals("#4.0", 4, model1.absOffset());
299: assertEquals("#4.1", FREE, model1.getStateAtCurrent());
300: assertTrue("#4.2", model1.currentToken().isGap());
301: assertEquals("#4.3", 2, model1.currentToken().getSize());
302: model1.move(-1);
303: assertEquals("#5.0", 3, model1.absOffset());
304: assertEquals("#5.1", INSIDE_LINE_COMMENT, model1
305: .getStateAtCurrent());
306: assertEquals("#5.2", "\n", model1.currentToken().getType());
307: model1.move(-1);
308: assertEquals("#6.0", 2, model1.absOffset());
309: assertEquals("#6.1", INSIDE_LINE_COMMENT, model1
310: .getStateAtCurrent());
311: assertEquals("#6.2", "*", model1.currentToken().getType());
312: model1.move(-1);
313: assertEquals("#7.0", 1, model1.absOffset());
314: assertEquals("#7.1", FREE, model1.getStateAtCurrent());
315: assertEquals("#7.2", "//", model1.currentToken().getType());
316: model1.delete(-1);
317: assertEquals("#8.0", 0, model1.absOffset());
318: assertEquals("#8.1", FREE, model1.getStateAtCurrent());
319: assertEquals("#8.2", "/*", model1.currentToken().getType());
320: model1.move(7);
321: assertEquals("#8.0", 7, model1.absOffset());
322: assertEquals("#8.1", INSIDE_BLOCK_COMMENT, model1
323: .getStateAtCurrent());
324: assertEquals("#8.2", "*/", model1.currentToken().getType());
325: }
326:
327: public void testStartDeleteGap() {
328: model1.insertChar('/');
329: model1.insertChar('*');
330: insertGap(model1, 2);
331: model1.insertChar('*');
332: model1.insertChar('/');
333: model1.move(-4);
334: model1.delete(2);
335: assertEquals("#0.0", 2, model1.absOffset());
336: assertEquals("#0.1", "*/", model1.currentToken().getType());
337: assertEquals("#0.2", INSIDE_BLOCK_COMMENT, model1
338: .getStateAtCurrent());
339: model1.move(-2);
340: assertEquals("#1.0", 0, model1.absOffset());
341: assertEquals("#1.1", "/*", model1.currentToken().getType());
342: assertEquals("#1.2", FREE, model1.getStateAtCurrent());
343: }
344:
345: public void testDeleteFreesBlockCommentEnd() {
346: model1.insertChar('/');
347: model1.insertChar('*');
348: assertEquals("#0.0", 2, model1.absOffset());
349:
350: insertGap(model1, 2);
351: assertEquals("#0.1", 4, model1.absOffset());
352:
353: model1.insertChar('*');
354: model1.insertChar('/');
355: assertEquals("#0.2", 6, model1.absOffset());
356:
357: model1.move(-6);
358: assertEquals("#0.3", 0, model1.absOffset());
359:
360: model1.delete(4);
361: assertEquals("#0.4", 0, model1.absOffset());
362: assertEquals("#0.5", "*", model1.currentToken().getType());
363: assertEquals("#0.6", FREE, model1.currentToken().getState());
364: assertEquals("#0.7", FREE, model1.getStateAtCurrent());
365:
366: model1.move(1);
367: assertEquals("#1.0", 1, model1.absOffset());
368: assertEquals("#1.1", "/", model1.currentToken().getType());
369: assertEquals("#1.2", FREE, model1.currentToken().getState());
370: assertEquals("#1.3", FREE, model1.getStateAtCurrent());
371: }
372:
373: public void testUnmaskBlockCommentedLineComment() {
374: model1.insertChar('/');
375: model1.insertChar('*');
376: insertGap(model1, 2);
377: model1.insertChar('/');
378: model1.insertChar('/');
379: model1.move(-1);
380: assertEquals("#0.0", 5, model1.absOffset());
381: assertEquals("#0.1", "/", model1.currentToken().getType());
382: assertEquals("#0.2", INSIDE_BLOCK_COMMENT, model1
383: .currentToken().getState());
384: assertEquals("#0.3", INSIDE_BLOCK_COMMENT, model1
385: .getStateAtCurrent());
386: model1.move(-1);
387: assertEquals("#0.0", 4, model1.absOffset());
388: assertEquals("#0.1", "/", model1.currentToken().getType());
389: assertEquals("#0.2", INSIDE_BLOCK_COMMENT, model1
390: .currentToken().getState());
391: assertEquals("#0.3", INSIDE_BLOCK_COMMENT, model1
392: .getStateAtCurrent());
393: model1.move(-4);
394: model1.delete(4);
395: assertEquals("#2.0", 0, model1.absOffset());
396: assertEquals("#2.1", "//", model1.currentToken().getType());
397: assertEquals("#2.2", FREE, model1.currentToken().getState());
398: assertEquals("#2.3", FREE, model1.getStateAtCurrent());
399: model1.move(2);
400: assertEquals("#3.0", 2, model1.absOffset());
401: assertEquals("#3.1", INSIDE_LINE_COMMENT, model1
402: .getStateAtCurrent());
403: }
404:
405: public void testCrazyDelete() {
406: model1.insertChar('/');
407: model1.insertChar('/');
408: model1.insertChar('*');
409: insertGap(model1, 2);
410: model1.insertChar('\n');
411: model1.insertChar('/');
412: model1.insertChar('/');
413: assertEquals("#0.0", 8, model1.absOffset());
414: assertEquals("#0.1", INSIDE_LINE_COMMENT, model1
415: .getStateAtCurrent());
416: model1.move(-2);
417: assertEquals("#1.0", 6, model1.absOffset());
418: assertEquals("#1.1", FREE, model1.getStateAtCurrent());
419: assertEquals("#1.2", "//", model1.currentToken().getType());
420: assertEquals("#1.3", FREE, model1.currentToken().getState());
421: model1.move(-4);
422: model1.delete(4);
423: assertEquals("#2.0", 2, model1.absOffset());
424: assertEquals("#2.1", "/", model1.currentToken().getType());
425: assertEquals("#2.2", INSIDE_LINE_COMMENT, model1.currentToken()
426: .getState());
427: model1.move(1);
428: assertEquals("#3.0", 3, model1.absOffset());
429: assertEquals("#3.1", "/", model1.currentToken().getType());
430: assertEquals("#3.2", INSIDE_LINE_COMMENT, model1.currentToken()
431: .getState());
432: }
433: }
|