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 starts
043: * a new parenthesized phrase. (ie. Previous line ends in comma, semicolon,
044: * open paren, or open bracket.)
045: * @version $Id: QuestionNewParenPhraseTest.java 4255 2007-08-28 19:17:37Z mgricken $
046: */
047: public final class QuestionNewParenPhraseTest extends
048: IndentRulesTestCase {
049:
050: /**
051: * Tests hitting start of document.
052: */
053: public void testStartOfDocument() throws BadLocationException {
054: IndentRuleQuestion rule = new QuestionNewParenPhrase(null, null);
055:
056: // Hits docstart
057: _setDocText("\nfoo();");
058: assertTrue("first line", !rule.applyRule(_doc, 0,
059: Indenter.IndentReason.OTHER));
060: assertTrue("second line", !rule.applyRule(_doc, 2,
061: Indenter.IndentReason.OTHER));
062: }
063:
064: /**
065: * Tests having no paren phrase delimiters on prev line.
066: */
067: public void testNoParenDelims() throws BadLocationException {
068: IndentRuleQuestion rule = new QuestionNewParenPhrase(null, null);
069:
070: // No paren delimiters
071: _setDocText("foo\nbar.\ny");
072: assertTrue("second line", !rule.applyRule(_doc, 4,
073: Indenter.IndentReason.OTHER));
074: assertTrue("third line", !rule.applyRule(_doc, 9,
075: Indenter.IndentReason.OTHER));
076: }
077:
078: /**
079: * Tests having delimiter on prev line, with text preceding
080: */
081: public void testParenDelimsWithText() throws BadLocationException {
082: IndentRuleQuestion rule = new QuestionNewParenPhrase(null, null);
083:
084: // Lines ending in delimiter, each with preceding text
085: _setDocText("new Foo(\nx,\ny;\na[\nbar])\n{");
086: assertTrue("line after paren", rule.applyRule(_doc, 9,
087: Indenter.IndentReason.OTHER));
088: assertTrue("line after comma", rule.applyRule(_doc, 12,
089: Indenter.IndentReason.OTHER));
090: assertTrue("line after semicolon", rule.applyRule(_doc, 15,
091: Indenter.IndentReason.OTHER));
092: assertTrue("line after bracket", rule.applyRule(_doc, 18,
093: Indenter.IndentReason.OTHER));
094: assertTrue("line after close paren", !rule.applyRule(_doc, 24,
095: Indenter.IndentReason.OTHER));
096: }
097:
098: /**
099: * Tests having delimiter on prev line, with no text preceding
100: */
101: public void testParenDelimsNoText() throws BadLocationException {
102: IndentRuleQuestion rule = new QuestionNewParenPhrase(null, null);
103:
104: // Paren delims with no leading text
105: _setDocText("(\n,\n;\n[\nfoo\nbar");
106: assertTrue("line after paren", rule.applyRule(_doc, 2,
107: Indenter.IndentReason.OTHER));
108: assertTrue("line after comma", rule.applyRule(_doc, 4,
109: Indenter.IndentReason.OTHER));
110: assertTrue("line after semicolon", rule.applyRule(_doc, 6,
111: Indenter.IndentReason.OTHER));
112: assertTrue("line after bracket", rule.applyRule(_doc, 8,
113: Indenter.IndentReason.OTHER));
114: assertTrue("line after text", !rule.applyRule(_doc, 12,
115: Indenter.IndentReason.OTHER));
116: }
117:
118: /**
119: * Tests having a comment after the delimiter
120: */
121: public void testParenDelimsWithComment()
122: throws BadLocationException {
123: IndentRuleQuestion rule = new QuestionNewParenPhrase(null, null);
124:
125: // Delim in text, with comment before
126: _setDocText("for (int i; // comment\ni < 2; /** comment */\ni++) {");
127: assertTrue("// comment", rule.applyRule(_doc, 23,
128: Indenter.IndentReason.OTHER));
129: assertTrue("/* */ comment", rule.applyRule(_doc, 45,
130: Indenter.IndentReason.OTHER));
131: }
132:
133: /**
134: * Tests having a paren delimiter several lines back, with only
135: * whitespace inbetween.
136: */
137: public void testMultipleBlankLinesBack()
138: throws BadLocationException {
139: IndentRuleQuestion rule = new QuestionNewParenPhrase(null, null);
140:
141: // Blank lines between
142: _setDocText("for(\n\nint i;\n\n\ni > 0;;\n)");
143: assertTrue("line after open paren", rule.applyRule(_doc, 5,
144: Indenter.IndentReason.OTHER));
145: assertTrue("two lines after open paren", rule.applyRule(_doc,
146: 6, Indenter.IndentReason.OTHER));
147: assertTrue("line after semicolon", rule.applyRule(_doc, 13,
148: Indenter.IndentReason.OTHER));
149: assertTrue("two lines after semicolon", rule.applyRule(_doc,
150: 16, Indenter.IndentReason.OTHER));
151: }
152:
153: /**
154: * Tests having a paren delimiter several lines back, with only
155: * blank space and comments inbetween.
156: */
157: public void testMultipleCommentLinesBack()
158: throws BadLocationException {
159: IndentRuleQuestion rule = new QuestionNewParenPhrase(null, null);
160:
161: // Comments between
162: _setDocText("for(\n//\n/** foo * /int i;\n\n// bar\ni > 0;;\n)");
163: assertTrue("line after open paren", rule.applyRule(_doc, 7,
164: Indenter.IndentReason.OTHER));
165: assertTrue("two lines after open paren", rule.applyRule(_doc,
166: 18, Indenter.IndentReason.OTHER));
167: assertTrue("line after semicolon", rule.applyRule(_doc, 25,
168: Indenter.IndentReason.OTHER));
169: assertTrue("two lines after semicolon", rule.applyRule(_doc,
170: 28, Indenter.IndentReason.OTHER));
171: }
172:
173: /**
174: * Tests having text on a line after the delimiter.
175: */
176: public void testDoesNotEndWithParenDelim()
177: throws BadLocationException {
178: IndentRuleQuestion rule = new QuestionNewParenPhrase(null, null);
179:
180: // Delim in text, not ending line
181: _setDocText("foo(bar.\nx,y\n)");
182: assertTrue("line after paren", !rule.applyRule(_doc, 9,
183: Indenter.IndentReason.OTHER));
184: assertTrue("line after comma", !rule.applyRule(_doc, 13,
185: Indenter.IndentReason.OTHER));
186: }
187:
188: /**
189: * Tests having an operator as a delimiter.
190: */
191: public void testOperatorDelim() throws BadLocationException {
192: IndentRuleQuestion rule = new QuestionNewParenPhrase(null, null);
193:
194: // Delim in text, not ending line
195: _setDocText("foo(x +\ny\n)");
196: assertTrue("line after operator", rule.applyRule(_doc, 8,
197: Indenter.IndentReason.OTHER));
198: assertTrue("line after comma", !rule.applyRule(_doc, 10,
199: Indenter.IndentReason.OTHER));
200: }
201:
202: /**
203: * Tests ignoring delims on line.
204: */
205: public void testIgnoreDelimsOnLine() throws BadLocationException {
206: IndentRuleQuestion rule = new QuestionNewParenPhrase(null, null);
207:
208: // Delim in text, not ending line
209: _setDocText("foo(x.\ny()\n)");
210: assertTrue("after paren, but not new phrase", !rule.applyRule(
211: _doc, 10, Indenter.IndentReason.OTHER));
212: }
213:
214: }
|