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 immediately previous line
043: * starts with a particular string.
044: * @version $Id: QuestionPrevLineStartsWithTest.java 4255 2007-08-28 19:17:37Z mgricken $
045: */
046: public final class QuestionPrevLineStartsWithTest extends
047: IndentRulesTestCase {
048:
049: /** Tests not having the prefix in the text. */
050: public void testNoPrefix() throws BadLocationException {
051: IndentRuleQuestion rule = new QuestionPrevLineStartsWith("{",
052: null, null);
053:
054: // Open brace
055: _setDocText("}\nfoo();\n}\n");
056: assertTrue("line after close brace (no open brace)", !rule
057: .applyRule(_doc, 2, Indenter.IndentReason.OTHER));
058: assertTrue("line after text (no open brace)", !rule.applyRule(
059: _doc, 9, Indenter.IndentReason.OTHER));
060: assertTrue("line after text (no open brace)", !rule.applyRule(
061: _doc, 10, Indenter.IndentReason.OTHER));
062:
063: // Star
064: rule = new QuestionPrevLineStartsWith("*", null, null);
065: _setDocText("{\nfoo();");
066: assertTrue("no star", !rule.applyRule(_doc, 6,
067: Indenter.IndentReason.OTHER));
068:
069: }
070:
071: /** Tests hitting start of document. */
072: public void testStartOfDocument() throws BadLocationException {
073: IndentRuleQuestion rule = new QuestionPrevLineStartsWith("{",
074: null, null);
075:
076: // Hits docstart
077: _setDocText("\nfoo();");
078: assertTrue("first line", !rule.applyRule(_doc, 0,
079: Indenter.IndentReason.OTHER));
080: assertTrue("second line", !rule.applyRule(_doc, 2,
081: Indenter.IndentReason.OTHER));
082: }
083:
084: /** Tests prefix on current line. */
085: public void testPrefixOnCurrLine() throws BadLocationException {
086: IndentRuleQuestion rule = new QuestionPrevLineStartsWith("}",
087: null, null);
088:
089: // Prefix at start of current line
090: _setDocText("} foo();");
091: assertTrue("before brace", !rule.applyRule(_doc, 0,
092: Indenter.IndentReason.OTHER));
093: assertTrue("after brace", !rule.applyRule(_doc, 2,
094: Indenter.IndentReason.OTHER));
095:
096: // Prefix in middle of current line
097: _setDocText("foo();\n bar(); } foo();");
098: assertTrue("before brace", !rule.applyRule(_doc, 7,
099: Indenter.IndentReason.OTHER));
100: assertTrue("after brace", !rule.applyRule(_doc, 18,
101: Indenter.IndentReason.OTHER));
102: }
103:
104: /** Tests having prev line start with prefix, with text following */
105: public void testStartsWithPrefixWithText()
106: throws BadLocationException {
107: IndentRuleQuestion rule = new QuestionPrevLineStartsWith("}",
108: null, null);
109:
110: // Prefix plus text (no space)
111: _setDocText("}bar();\nfoo();\nbar();");
112: assertTrue("line of brace (no space)", !rule.applyRule(_doc, 2,
113: Indenter.IndentReason.OTHER));
114: assertTrue("line after brace (no space)", rule.applyRule(_doc,
115: 8, Indenter.IndentReason.OTHER));
116: assertTrue("two lines after brace (no space)", !rule.applyRule(
117: _doc, 16, Indenter.IndentReason.OTHER));
118:
119: // Prefix plus text (with space)
120: rule = new QuestionPrevLineStartsWith("*", null, null);
121: _setDocText("foo\n * comment\nbar");
122: assertTrue("just before star (with space)", !rule.applyRule(
123: _doc, 4, Indenter.IndentReason.OTHER));
124: assertTrue("just after star (with space)", !rule.applyRule(
125: _doc, 6, Indenter.IndentReason.OTHER));
126: assertTrue("line after star (with space)", rule.applyRule(_doc,
127: 16, Indenter.IndentReason.OTHER));
128: }
129:
130: /** Tests having prev line start with prefix, with no text following */
131: public void testStartsWithPrefixNoText()
132: throws BadLocationException {
133: IndentRuleQuestion rule = new QuestionPrevLineStartsWith("*",
134: null, null);
135:
136: // Prefix plus no text (no space)
137: _setDocText("foo();\n*\nbar();\n}");
138: assertTrue("line of star (no space)", !rule.applyRule(_doc, 8,
139: Indenter.IndentReason.OTHER));
140: assertTrue("line after star (no space)", rule.applyRule(_doc,
141: 10, Indenter.IndentReason.OTHER));
142: assertTrue("two lines after star (no space)", !rule.applyRule(
143: _doc, 16, Indenter.IndentReason.OTHER));
144:
145: // Prefix plus no text (with space)
146: _setDocText("foo();\n * \nbar();\n{");
147: assertTrue("line of star (with space)", !rule.applyRule(_doc,
148: 7, Indenter.IndentReason.OTHER));
149: assertTrue("just after star (with space)", !rule.applyRule(
150: _doc, 11, Indenter.IndentReason.OTHER));
151: assertTrue("line after star (with space)", rule.applyRule(_doc,
152: 13, Indenter.IndentReason.OTHER));
153: }
154:
155: /** Tests having a multiple character prefix. */
156: public void testMultipleCharPrefix() throws BadLocationException {
157: IndentRuleQuestion rule = new QuestionPrevLineStartsWith("* ",
158: null, null);
159:
160: // Multi-char prefix
161: _setDocText("*\n *\n * \n * foo\nbar");
162: assertTrue("star", !rule.applyRule(_doc, 2,
163: Indenter.IndentReason.OTHER));
164: assertTrue("space star", !rule.applyRule(_doc, 5,
165: Indenter.IndentReason.OTHER));
166: assertTrue("space star space", rule.applyRule(_doc, 11,
167: Indenter.IndentReason.OTHER));
168: assertTrue("space star space text", rule.applyRule(_doc, 16,
169: Indenter.IndentReason.OTHER));
170: }
171:
172: /** Tests having a commented prefix. */
173: public void testCommentedPrefix() throws BadLocationException {
174: IndentRuleQuestion rule = new QuestionPrevLineStartsWith("*",
175: null, null);
176:
177: // Star in comment
178: _setDocText("/**\n* \ncomment\n*/");
179: assertTrue("just before star", !rule.applyRule(_doc, 4,
180: Indenter.IndentReason.OTHER));
181: assertTrue("just after star", !rule.applyRule(_doc, 6,
182: Indenter.IndentReason.OTHER));
183: assertTrue("line after star", rule.applyRule(_doc, 7,
184: Indenter.IndentReason.OTHER));
185: assertTrue("line after star", !rule.applyRule(_doc, 15,
186: Indenter.IndentReason.OTHER));
187: }
188:
189: /** Tests a prefix that begins a comment. */
190: public void testCommentPrefix() throws BadLocationException {
191: IndentRuleQuestion rule = new QuestionPrevLineStartsWith("/**",
192: null, null);
193:
194: // Star in comment
195: _setDocText("/**\n* \ncomment\n*/");
196: assertTrue("just before star", rule.applyRule(_doc, 4,
197: Indenter.IndentReason.OTHER));
198: assertTrue("just after star", rule.applyRule(_doc, 6,
199: Indenter.IndentReason.OTHER));
200: assertTrue("line after star", !rule.applyRule(_doc, 7,
201: Indenter.IndentReason.OTHER));
202: assertTrue("line after star", !rule.applyRule(_doc, 15,
203: Indenter.IndentReason.OTHER));
204: }
205:
206: /** Tests having text on a line before the prefix. */
207: public void testDoesNotStartWithPrefix()
208: throws BadLocationException {
209: IndentRuleQuestion rule = new QuestionPrevLineStartsWith("*",
210: null, null);
211:
212: // Star in text, not starting line
213: _setDocText("foo(); *\nbar();\n");
214: assertTrue("line after star", !rule.applyRule(_doc, 10,
215: Indenter.IndentReason.OTHER));
216: }
217:
218: }
|