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;
038:
039: import javax.swing.text.BadLocationException;
040: import edu.rice.cs.drjava.model.definitions.indent.IndentRulesTestCase;
041:
042: /**
043: * Tests for the helper methods in DefinitionsDocument
044: * @version $Id: IndentHelperTest.java 4255 2007-08-28 19:17:37Z mgricken $
045: */
046: public final class IndentHelperTest extends IndentRulesTestCase {
047:
048: /** Tests findPrevDelimiter() */
049: public void testFindPrevDelimiter() throws BadLocationException {
050: char[] delimiters1 = { ';', ':', '?' };
051:
052: // Used to test for delimiters not in test string
053: char[] delimiters2 = { '%' };
054:
055: // Used to test if delimiter right after DOCSTART is found
056: char[] delimiters3 = { 'f' };
057:
058: // Used to test finding delimiters that can be confused with comments
059: char[] delimiters4 = { '/', '*' };
060:
061: _setDocText("/*bar;\nfoo();\nx;*/\nreturn foo;\n");
062: assertEquals("Check that delimiters in multi-line "
063: + "comments are ignored",
064: DefinitionsDocument.ERROR_INDEX, _doc
065: .findPrevDelimiter(23, delimiters1));
066:
067: _setDocText("foo();\n//bar();\nbiz();\n");
068: assertEquals("Check that delimiters in single-line "
069: + "comments are ignored", 5, _doc.findPrevDelimiter(16,
070: delimiters1));
071:
072: _setDocText("x=';'\n");
073: assertEquals("Check that delimiters in single-quotes "
074: + "are ignored", DefinitionsDocument.ERROR_INDEX, _doc
075: .findPrevDelimiter(5, delimiters1));
076:
077: _setDocText("x=\";\"\n");
078: assertEquals("Check that delimiters in double-quotes "
079: + "are ignored", DefinitionsDocument.ERROR_INDEX, _doc
080: .findPrevDelimiter(5, delimiters1));
081:
082: _setDocText("foo();\nfor(;;)\n");
083: assertEquals("Check that delimiters in paren phrases "
084: + "are usually ignored", 5, _doc.findPrevDelimiter(14,
085: delimiters1));
086:
087: _setDocText("foo();\nfor(;;)\n");
088: assertEquals("Check that delimiters in paren phrases "
089: + "can be detected", 12, _doc.findPrevDelimiter(14,
090: delimiters1, false));
091:
092: _setDocText("foo();\n test ? x : y;\n\t return blah();\n");
093: assertEquals(
094: "Check that ERROR_INDEX is returned if no matching character is found",
095: DefinitionsDocument.ERROR_INDEX, _doc
096: .findPrevDelimiter(20, delimiters2));
097: assertEquals(
098: "Check that delimiter is found if it is right after DOCSTART",
099: 0, _doc.findPrevDelimiter(20, delimiters3));
100: assertEquals("Check that delimiter is not found if "
101: + "it is at cursor's position",
102: DefinitionsDocument.ERROR_INDEX, _doc
103: .findPrevDelimiter(5, delimiters1));
104: assertEquals(
105: "Check that the first delimiter in the list is found",
106: 17, _doc.findPrevDelimiter(19, delimiters1));
107: assertEquals(
108: "Check that the second delimiter in the list is found",
109: 13, _doc.findPrevDelimiter(17, delimiters1));
110: assertEquals(
111: "Check that the last delimiter in the list is found",
112: 5, _doc.findPrevDelimiter(13, delimiters1));
113:
114: _setDocText("foo *\n" + "// comment\n" + "bar\n");
115: assertEquals(
116: "Check that findprevDelimiter ignores comments even"
117: + "when delimiters include * and / (1)", 4,
118: _doc.findPrevDelimiter(17, delimiters4));
119: _setDocText("foo /\n" + "/* comment */\n" + "bar\n");
120: assertEquals(
121: "Check that findprevDelimiter ignores comments even"
122: + "when delimiters include * and / (2)", 4,
123: _doc.findPrevDelimiter(17, delimiters4));
124:
125: _setDocText("abcdefghijk");
126: _doc.setCurrentLocation(3);
127: int reducedModelPos = _doc.getReduced().absOffset();
128: _doc.findPrevDelimiter(8, delimiters2);
129: assertEquals(
130: "Check that position in reduced model is unaffected "
131: + "after call to findPrevDelimiter",
132: reducedModelPos, _doc.getReduced().absOffset());
133:
134: }
135:
136: public void testPosInParenPhrase() throws BadLocationException {
137:
138: _setDocText("(;)");
139: assertEquals("';' in parent phrase", true, _doc
140: .posInParenPhrase(1));
141:
142: _setDocText("abcdefghijk");
143: _doc.setCurrentLocation(3);
144: int reducedModelPos = _doc.getReduced().absOffset();
145: _doc.posInParenPhrase(8);
146: assertEquals(
147: "Check that position in reduced model is unaffected "
148: + "after call to posInParenPhrase",
149: reducedModelPos, _doc.getReduced().absOffset());
150: }
151:
152: public void testGetIndentOfCurrStmtDelimiters()
153: throws BadLocationException {
154:
155: _setDocText("foo();\n");
156: assertEquals("prev delimiter DOCSTART, no indent", "", _doc
157: .getIndentOfCurrStmt(3));
158: _setDocText(" foo();\n");
159: assertEquals("prev delimiter DOCSTART, indent two spaces",
160: " ", _doc.getIndentOfCurrStmt(7));
161:
162: _setDocText("bar();\nfoo();\n");
163: assertEquals("prev delimiter ';', no indent", "", _doc
164: .getIndentOfCurrStmt(7));
165: _setDocText(" bar();\n foo();\n");
166: assertEquals("prev delimiter ';', indent two spaces", " ",
167: _doc.getIndentOfCurrStmt(9));
168:
169: _setDocText("void bar()\n{\nfoo();\n");
170: assertEquals("prev delimiter '{', no indent", "", _doc
171: .getIndentOfCurrStmt(13));
172: _setDocText("void bar()\n{\n foo();\n");
173: assertEquals("prev delimiter '{', indent two spaces", " ",
174: _doc.getIndentOfCurrStmt(13));
175:
176: _setDocText("}\nfoo();\n");
177: assertEquals("prev delimiter '}', no indent", "", _doc
178: .getIndentOfCurrStmt(2));
179: _setDocText("}\n foo();\n");
180: assertEquals("prev delimiter '}', indent two spaces", " ",
181: _doc.getIndentOfCurrStmt(2));
182: }
183:
184: public void testGetIndentOfCurrStmtDelimiterSameLine()
185: throws BadLocationException {
186:
187: _setDocText("bar(); foo();\n");
188: assertEquals("prev delimiter on same line, no indent", "", _doc
189: .getIndentOfCurrStmt(6));
190:
191: _setDocText(" bar(); foo();\n");
192: assertEquals("prev delimiter on same line, indent two spaces",
193: " ", _doc.getIndentOfCurrStmt(8));
194: }
195:
196: public void testGetIndentOfCurrStmtMultipleLines()
197: throws BadLocationException {
198:
199: String text = " oogabooga();\n" + " bar().\n"
200: + " bump().\n" + " //comment\n"
201: + " /*commment\n" + " *again;{}\n" + " */\n"
202: + " foo();\n";
203:
204: _setDocText(text);
205: assertEquals("start stmt on previous line, indent two spaces",
206: " ", _doc.getIndentOfCurrStmt(24));
207: assertEquals("start stmt before previous line, "
208: + "cursor inside single-line comment "
209: + "indent two spaces", " ", _doc
210: .getIndentOfCurrStmt(42));
211: assertEquals("start stmt before single-line comment, "
212: + "cursor inside multi-line comment "
213: + "indent two spaces", " ", _doc
214: .getIndentOfCurrStmt(56));
215: assertEquals("start stmt before multi-line comment, "
216: + "indent two spaces", " ", _doc
217: .getIndentOfCurrStmt(88));
218: }
219:
220: public void testGetIndentOfCurrStmtIgnoreDelimsInParenPhrase()
221: throws BadLocationException {
222:
223: String text = " bar.\n (;)\nfoo();";
224:
225: _setDocText(text);
226: assertEquals("ignores delimiter in paren phrase", " ", _doc
227: .getIndentOfCurrStmt(12));
228: }
229:
230: public void testGetIndentOfCurrStmtEndOfDoc()
231: throws BadLocationException {
232:
233: _setDocText("foo.\n");
234: assertEquals("cursor at end of document, no indent", "", _doc
235: .getIndentOfCurrStmt(5));
236:
237: }
238:
239: public void testGetLineStartPos() throws BadLocationException {
240: _setDocText("foo\nbar\nblah");
241: assertEquals("Returns position after the previous newline", 4,
242: _doc.getLineStartPos(6));
243: assertEquals(
244: "Returns position after previous newline when cursor "
245: + "is at the position after the previous newline",
246: 4, _doc.getLineStartPos(4));
247: assertEquals(
248: "Returns DOCSTART when there's no previous newline", 0,
249: _doc.getLineStartPos(2));
250: assertEquals("Returns DOCSTART when the cursor is at DOCSTART",
251: 0, _doc.getLineStartPos(0));
252:
253: _setDocText("abcdefghijk");
254: _doc.setCurrentLocation(3);
255: int reducedModelPos = _doc.getReduced().absOffset();
256: _doc.getLineStartPos(5);
257: assertEquals(
258: "Check that position in reduced model is unaffected "
259: + "after call to getLineStartPos",
260: reducedModelPos, _doc.getReduced().absOffset());
261: }
262:
263: public void testGetLineEndPos() throws BadLocationException {
264: _setDocText("foo\nbar\nblah");
265: assertEquals("Returns position before the next newline", 7,
266: _doc.getLineEndPos(5));
267: assertEquals(
268: "Returns position before the next newline when cursor "
269: + "is at the position before the next newline",
270: 7, _doc.getLineEndPos(7));
271: assertEquals(
272: "Returns the end of the document when there's no next newline",
273: 12, _doc.getLineEndPos(9));
274:
275: _setDocText("abcdefghijk");
276: _doc.setCurrentLocation(3);
277: int reducedModelPos = _doc.getReduced().absOffset();
278: _doc.getLineEndPos(5);
279: assertEquals(
280: "Check that position in reduced model is unaffected "
281: + "after call to getLineEndPos",
282: reducedModelPos, _doc.getReduced().absOffset());
283: }
284:
285: public void testGetLineFirstCharPos() throws BadLocationException {
286: _setDocText(" ");
287: assertEquals("Returns the end of the document if the line "
288: + "is the last line in the document and has no "
289: + "non-whitespace characters", 3, _doc
290: .getLineFirstCharPos(1));
291:
292: _setDocText(" \nfoo();\n");
293: assertEquals("Returns the next newline if there are "
294: + "no non-whitespace characters on the line", 3, _doc
295: .getLineFirstCharPos(1));
296:
297: _setDocText("foo();\n \t bar();\nbiz()\n");
298: assertEquals(
299: "Returns first non-whitespace character on the line "
300: + "when position is at the start of the line",
301: 13, _doc.getLineFirstCharPos(7));
302: assertEquals(
303: "Returns first non-whitespace character on the line "
304: + "when the position is after the first non-ws character "
305: + "on the line", 13, _doc
306: .getLineFirstCharPos(16));
307: assertEquals(
308: "Returns first non-whitespace character on the line "
309: + "when the position is at the newline", 13,
310: _doc.getLineFirstCharPos(19));
311:
312: _setDocText("abcdefghijk");
313: _doc.setCurrentLocation(3);
314: int reducedModelPos = _doc.getReduced().absOffset();
315: _doc.getLineFirstCharPos(5);
316: assertEquals(
317: "Check that position in reduced model is unaffected "
318: + "after call to getLineFirstCharPos",
319: reducedModelPos, _doc.getReduced().absOffset());
320: }
321:
322: public void testGetFirstNonWSCharPos() throws BadLocationException {
323:
324: _setDocText("foo();\nbar()\tx(); y();\n \t \n\nz();\n ");
325: assertEquals("Current position is non-whitespace", 0, _doc
326: .getFirstNonWSCharPos(0));
327: assertEquals("Current position is non-whitespace, end of line",
328: 5, _doc.getFirstNonWSCharPos(5));
329: assertEquals("Next non-whitespace is 1 '\\n' ahead.", 7, _doc
330: .getFirstNonWSCharPos(6));
331: assertEquals("Next non-whitespace is 2 '\\t' ahead.", 13, _doc
332: .getFirstNonWSCharPos(12));
333: assertEquals("Next non-whitespace is 3 spaces ahead.", 22, _doc
334: .getFirstNonWSCharPos(20));
335: assertEquals(
336: "Next non-whitespace is multiple whitespaces ('\\n', '\\t', ' ') ahead.",
337: 34, _doc.getFirstNonWSCharPos(27));
338: assertEquals("Next non-whitespace is end of document", -1, _doc
339: .getFirstNonWSCharPos(39));
340:
341: _setDocText("foo();\n// comment\nbar();\n");
342: assertEquals("Ignore single-line comments", 18, _doc
343: .getFirstNonWSCharPos(6));
344:
345: _setDocText("foo();\n /* bar\nblah */ boo\n");
346: assertEquals("Ignore multiline comments", 23, _doc
347: .getFirstNonWSCharPos(6));
348: _setDocText("foo /");
349: assertEquals("Slash at end of document", 6, _doc
350: .getFirstNonWSCharPos(4));
351: _setDocText("foo //");
352: assertEquals("// at end", -1, _doc.getFirstNonWSCharPos(4));
353: _setDocText("foo /*");
354: assertEquals("/* at end", -1, _doc.getFirstNonWSCharPos(4));
355:
356: _setDocText("abcdefghijk");
357: _doc.setCurrentLocation(3);
358: int reducedModelPos = _doc.getReduced().absOffset();
359: _doc.getLineFirstCharPos(5);
360: assertEquals(
361: "Check that position in reduced model is unaffected "
362: + "after call to getLineFirstCharPos",
363: reducedModelPos, _doc.getReduced().absOffset());
364: }
365:
366: /**
367: * Tests that the "intelligent" beginning of line can be found, given
368: * a position on the line. Very similar to getFirstNonWSCharPos, except
369: * that comments are treated as non-whitespace, and less parsing needs
370: * to be done.
371: */
372: public void testGetIntelligentBeginLinePos()
373: throws BadLocationException {
374: _setDocText(" foo();");
375: assertEquals("simple text, in WS", 0, _doc
376: .getIntelligentBeginLinePos(1));
377: assertEquals("simple text, end of WS", 0, _doc
378: .getIntelligentBeginLinePos(3));
379: assertEquals("simple text, in text", 3, _doc
380: .getIntelligentBeginLinePos(4));
381: assertEquals("simple text, at end", 3, _doc
382: .getIntelligentBeginLinePos(9));
383:
384: _setDocText(" // foo");
385: assertEquals("comment, in WS", 0, _doc
386: .getIntelligentBeginLinePos(0));
387: assertEquals("comment, end of WS", 0, _doc
388: .getIntelligentBeginLinePos(3));
389: assertEquals("comment, in text", 3, _doc
390: .getIntelligentBeginLinePos(4));
391: assertEquals("comment, at end", 3, _doc
392: .getIntelligentBeginLinePos(9));
393:
394: _setDocText(" foo();\n bar();\n");
395: assertEquals("multiple lines, at start", 10, _doc
396: .getIntelligentBeginLinePos(10));
397: assertEquals("multiple lines, end of WS", 10, _doc
398: .getIntelligentBeginLinePos(11));
399: assertEquals("multiple lines, in text", 11, _doc
400: .getIntelligentBeginLinePos(13));
401: assertEquals("multiple lines, at end", 11, _doc
402: .getIntelligentBeginLinePos(17));
403:
404: _setDocText("abc def");
405: assertEquals("no leading WS, in middle", 0, _doc
406: .getIntelligentBeginLinePos(5));
407: }
408: }
|