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.indent;
038:
039: import javax.swing.text.BadLocationException;
040:
041: /**
042: * Tests the indention rule which detects whether the current line
043: * starts with a particular string.
044: * @version $Id: QuestionCurrLineStartsWithTest.java 4255 2007-08-28 19:17:37Z mgricken $
045: */
046: public final class QuestionCurrLineStartsWithTest extends
047: IndentRulesTestCase {
048:
049: /** Tests not having the prefix in the text. */
050: public void testNoPrefix() throws BadLocationException {
051: IndentRuleQuestion rule = new QuestionCurrLineStartsWith("{",
052: null, null);
053:
054: // Open brace
055: _setDocText("foo();\n}\n");
056: assertTrue("no open brace", !rule.applyRule(_doc, 0,
057: Indenter.IndentReason.OTHER));
058: assertTrue("line of close brace (no open brace)", !rule
059: .applyRule(_doc, 7, Indenter.IndentReason.OTHER));
060: assertTrue("line after close brace (no open brace)", !rule
061: .applyRule(_doc, 8, Indenter.IndentReason.OTHER));
062:
063: // Close brace
064: rule = new QuestionCurrLineStartsWith("}", null, null);
065: _setDocText("{\nfoo();");
066: assertTrue("no close brace", !rule.applyRule(_doc, 0,
067: Indenter.IndentReason.OTHER));
068: }
069:
070: /**
071: * Tests having a line start with prefix, with text following
072: */
073: public void testStartsWithPrefixWithText()
074: throws BadLocationException {
075: IndentRuleQuestion rule = new QuestionCurrLineStartsWith("}",
076: null, null);
077:
078: // Prefix plus text (no space)
079: _setDocText("foo();\n}bar();\n");
080: assertTrue("line before brace (no space)", !rule.applyRule(
081: _doc, 0, Indenter.IndentReason.OTHER));
082: assertTrue("just before brace (no space)", rule.applyRule(_doc,
083: 7, Indenter.IndentReason.OTHER));
084: assertTrue("just after brace (no space)", rule.applyRule(_doc,
085: 9, Indenter.IndentReason.OTHER));
086: assertTrue("line after brace (no space)", !rule.applyRule(_doc,
087: 15, Indenter.IndentReason.OTHER));
088:
089: // Prefix plus text (with space)
090: rule = new QuestionCurrLineStartsWith("*", null, null);
091: _setDocText("foo\n * comment\nbar");
092: assertTrue("line before star (with space)", !rule.applyRule(
093: _doc, 0, Indenter.IndentReason.OTHER));
094: assertTrue("just before star (with space)", rule.applyRule(
095: _doc, 4, Indenter.IndentReason.OTHER));
096: assertTrue("just after star (with space)", rule.applyRule(_doc,
097: 6, Indenter.IndentReason.OTHER));
098: assertTrue("line after star (with space)", !rule.applyRule(
099: _doc, 15, Indenter.IndentReason.OTHER));
100: }
101:
102: /**
103: * Tests having a line start with prefix, with no text following
104: */
105: public void testStartsWithPrefixNoText()
106: throws BadLocationException {
107: IndentRuleQuestion rule = new QuestionCurrLineStartsWith("{",
108: null, null);
109:
110: // Prefix plus no text (no space)
111: _setDocText("foo();\n{\nbar();\n");
112: assertTrue("line before brace (no space)", !rule.applyRule(
113: _doc, 0, Indenter.IndentReason.OTHER));
114: assertTrue("just before brace (no space)", rule.applyRule(_doc,
115: 7, Indenter.IndentReason.OTHER));
116: assertTrue("just after brace (no space)", rule.applyRule(_doc,
117: 8, Indenter.IndentReason.OTHER));
118: assertTrue("line after brace (no space)", !rule.applyRule(_doc,
119: 10, Indenter.IndentReason.OTHER));
120:
121: // Prefix plus no text (with space)
122: _setDocText("foo();\n {\nbar();\n");
123: assertTrue("line before brace (with space)", !rule.applyRule(
124: _doc, 0, Indenter.IndentReason.OTHER));
125: assertTrue("just before brace (with space)", rule.applyRule(
126: _doc, 7, Indenter.IndentReason.OTHER));
127: assertTrue("just after brace (with space)", rule.applyRule(
128: _doc, 11, Indenter.IndentReason.OTHER));
129: assertTrue("line after brace (with space)", !rule.applyRule(
130: _doc, 14, Indenter.IndentReason.OTHER));
131: }
132:
133: /**
134: * Tests having a multiple character prefix.
135: */
136: public void testMultipleCharPrefix() throws BadLocationException {
137: IndentRuleQuestion rule = new QuestionCurrLineStartsWith(".*.",
138: null, null);
139:
140: // Multi-char prefix
141: _setDocText("*\n.*\n.*.\n.*.foo");
142: assertTrue("star", !rule.applyRule(_doc, 0,
143: Indenter.IndentReason.OTHER));
144: assertTrue("dot star", !rule.applyRule(_doc, 2,
145: Indenter.IndentReason.OTHER));
146: assertTrue("dot star dot", rule.applyRule(_doc, 7,
147: Indenter.IndentReason.OTHER));
148: assertTrue("dot star dot text", rule.applyRule(_doc, 9,
149: Indenter.IndentReason.OTHER));
150: }
151:
152: /**
153: * Tests having a commented prefix without searching in comments.
154: */
155: public void testCommentedPrefixDontSearchComment()
156: throws BadLocationException {
157: IndentRuleQuestion rule = new QuestionCurrLineStartsWith("{",
158: null, null);
159:
160: // Open brace in comment
161: _setDocText("foo();\n// {\nbar();\n");
162: assertTrue("just before brace", !rule.applyRule(_doc, 7,
163: Indenter.IndentReason.OTHER));
164: assertTrue("just after brace", !rule.applyRule(_doc, 11,
165: Indenter.IndentReason.OTHER));
166: assertTrue("line after brace", !rule.applyRule(_doc, 12,
167: Indenter.IndentReason.OTHER));
168: }
169:
170: /**
171: * Tests having a commented prefix with searching in comments.
172: */
173: public void testCommentedPrefixSearchComment()
174: throws BadLocationException {
175: IndentRuleQuestion rule = new QuestionCurrLineStartsWith("*",
176: null, null);
177:
178: // Star in comment
179: _setDocText("/**\n* \ncomment\n");
180: assertTrue("line before star", !rule.applyRule(_doc, 0,
181: Indenter.IndentReason.OTHER));
182: assertTrue("just before star", rule.applyRule(_doc, 4,
183: Indenter.IndentReason.OTHER));
184: assertTrue("just after star", rule.applyRule(_doc, 6,
185: Indenter.IndentReason.OTHER));
186: assertTrue("line after star", !rule.applyRule(_doc, 7,
187: Indenter.IndentReason.OTHER));
188: }
189:
190: /**
191: * Tests having text on a line before the prefix.
192: */
193: public void testDoesNotStartWithPrefix()
194: throws BadLocationException {
195: IndentRuleQuestion rule = new QuestionCurrLineStartsWith("}",
196: null, null);
197:
198: // Close brace in text, not starting line
199: _setDocText("foo(); }\nbar();\n");
200: assertTrue("before brace", !rule.applyRule(_doc, 0,
201: Indenter.IndentReason.OTHER));
202: assertTrue("just before brace", !rule.applyRule(_doc, 7,
203: Indenter.IndentReason.OTHER));
204: assertTrue("just after brace", !rule.applyRule(_doc, 8,
205: Indenter.IndentReason.OTHER));
206: assertTrue("line after brace", !rule.applyRule(_doc, 10,
207: Indenter.IndentReason.OTHER));
208: }
209:
210: /**
211: * Prefix appears at the end of a document.
212: */
213: public void testPrefixAtEnd() throws BadLocationException {
214: IndentRuleQuestion rule = new QuestionCurrLineStartsWith("}",
215: null, null);
216:
217: _setDocText("void foo() {\n}");
218: assertTrue("first line", !rule.applyRule(_doc, 3,
219: Indenter.IndentReason.OTHER));
220: assertTrue("end of first line", !rule.applyRule(_doc, 12,
221: Indenter.IndentReason.OTHER));
222: assertTrue("beginning of second line", rule.applyRule(_doc, 13,
223: Indenter.IndentReason.OTHER));
224: assertTrue("end of second line", rule.applyRule(_doc, 14,
225: Indenter.IndentReason.OTHER));
226: }
227:
228: /**
229: * Tests multiple-character prefix.
230: */
231: public void testMultCharPrefix() throws BadLocationException {
232: IndentRuleQuestion rule = new QuestionCurrLineStartsWith(
233: "abcdefg", null, null);
234:
235: _setDocText(" abcdefghij\n abcde");
236: assertTrue("first line, beginning", rule.applyRule(_doc, 0,
237: Indenter.IndentReason.OTHER));
238: assertTrue("first line, mid", rule.applyRule(_doc, 6,
239: Indenter.IndentReason.OTHER));
240: assertTrue("first line, end", rule.applyRule(_doc, 13,
241: Indenter.IndentReason.OTHER));
242: assertTrue("second line, beginning", !rule.applyRule(_doc, 14,
243: Indenter.IndentReason.OTHER));
244: assertTrue("second line, mid", !rule.applyRule(_doc, 18,
245: Indenter.IndentReason.OTHER));
246: assertTrue("second line, end", !rule.applyRule(_doc, 21,
247: Indenter.IndentReason.OTHER));
248: }
249: }
|