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: * Test class according to the JUnit protocol. Tests the question
043: * that determines whether or not the last block or expression list
044: * opened previous to the start of the current line was opened
045: * by one of the characters '(' or '['.
046: * This questions corresponds to rule 11 in our decision tree.
047: * @version $Id: QuestionBraceIsParenOrBracketTest.java 4255 2007-08-28 19:17:37Z mgricken $
048: */
049: public final class QuestionBraceIsParenOrBracketTest extends
050: IndentRulesTestCase {
051: // PRE: We are not inside a multiline comment.
052:
053: private String _text;
054:
055: private final IndentRuleQuestion _rule = new QuestionBraceIsParenOrBracket(
056: null, null);
057:
058: public void testParen() throws BadLocationException {
059: int i;
060:
061: /* (1) */
062: _text = "boolean method(int[] a, String b)";
063: _setDocText(_text);
064:
065: for (i = 0; i < _text.length(); i++)
066: assertTrue("START has no brace.", !_rule.applyRule(_doc, i,
067: Indenter.IndentReason.OTHER));
068:
069: /* (2) */
070: _text = "boolean method\n" + " (int[] a, String b)";
071:
072: _setDocText(_text);
073:
074: for (i = 0; i < _text.length(); i++)
075: assertTrue("START has no brace.", !_rule.applyRule(_doc, i,
076: Indenter.IndentReason.OTHER));
077:
078: /* (3) */
079: _text = "boolean method(\n" + " int[] a, String b)";
080:
081: _setDocText(_text);
082:
083: for (i = 0; i < 16; i++)
084: assertTrue("START has no brace.", !_rule.applyRule(_doc, i,
085: Indenter.IndentReason.OTHER));
086:
087: // For any position past the '\n' character, the rule applies:
088:
089: for (i = 16; i < _text.length(); i++)
090: assertTrue("START's brace is an open paren.", _rule
091: .applyRule(_doc, i, Indenter.IndentReason.OTHER));
092:
093: /* (4) */
094:
095: _text = "if (<cond>) {\n" + " if (\n"
096: + " <cond>) { ... }}";
097:
098: _setDocText(_text);
099:
100: for (i = 0; i < 23; i++)
101: assertTrue("START has no brace.", !_rule.applyRule(_doc, i,
102: Indenter.IndentReason.OTHER));
103:
104: // For any position past the second '\n' character, the rule applies:
105:
106: for (i = 23; i < _text.length(); i++)
107: assertTrue("START's brace is an open paren.", _rule
108: .applyRule(_doc, i, Indenter.IndentReason.OTHER));
109:
110: /* (5) */
111:
112: _text = "method(\n" + " array1, foo(array1[x]))\n"
113: + " <other stuff>";
114:
115: _setDocText(_text);
116:
117: assertTrue("START has no brace.", !_rule.applyRule(_doc, 0,
118: Indenter.IndentReason.OTHER));
119: assertTrue("START has no brace", !_rule.applyRule(_doc, 7,
120: Indenter.IndentReason.OTHER));
121: assertTrue("START's brace is an open paren.", _rule.applyRule(
122: _doc, 8, Indenter.IndentReason.OTHER));
123: assertTrue("START's brace is an open paren.", _rule.applyRule(
124: _doc, 30, Indenter.IndentReason.OTHER));
125: assertTrue("START has no brace.", !_rule.applyRule(_doc, _text
126: .length() - 1, Indenter.IndentReason.OTHER));
127: }
128:
129: public void testBracket() throws BadLocationException {
130: int i;
131:
132: /* (1) */
133:
134: _text = "boolean method(int[\n" + " ELTS]\n"
135: + " a, String b)";
136:
137: _setDocText(_text);
138:
139: for (i = 0; i < 20; i++)
140: assertTrue("START has no brace.", !_rule.applyRule(_doc, i,
141: Indenter.IndentReason.OTHER));
142:
143: // For any position past the first '\n' character, the rule applies:
144:
145: for (i = 20; i < 29; i++)
146: assertTrue("START's brace is an open bracket.", _rule
147: .applyRule(_doc, i, Indenter.IndentReason.OTHER));
148:
149: for (i = 29; i < _text.length(); i++)
150: assertTrue("START's brace is an open paren.", _rule
151: .applyRule(_doc, i, Indenter.IndentReason.OTHER));
152:
153: /* (2) */
154:
155: _text = "array1[i]\n" + " [j]";
156:
157: _setDocText(_text);
158:
159: for (i = 0; i < _text.length(); i++)
160: assertTrue("START has no brace.", !_rule.applyRule(_doc, i,
161: Indenter.IndentReason.OTHER));
162:
163: /* (3) */
164:
165: _text = "array1[\n" + " i][\n" + " j]";
166:
167: _setDocText(_text);
168:
169: assertTrue("START's paren is an open bracket.", _rule
170: .applyRule(_doc, 8, Indenter.IndentReason.OTHER));
171: assertTrue("START's paren is an open bracket.", _rule
172: .applyRule(_doc, 22, Indenter.IndentReason.OTHER));
173: assertTrue("START's paren is an open bracket.", _rule
174: .applyRule(_doc, 23, Indenter.IndentReason.OTHER));
175: }
176:
177: public void testCurly() throws BadLocationException {
178: /* (1) */
179:
180: _text = "class X extends Base\n" + "{\n" + "}";
181:
182: _setDocText(_text);
183:
184: assertTrue("START has no brace.", !_rule.applyRule(_doc, 0,
185: Indenter.IndentReason.OTHER));
186: assertTrue("START has no brace.", !_rule.applyRule(_doc, 20,
187: Indenter.IndentReason.OTHER));
188: assertTrue("START is curly brace.", !_rule.applyRule(_doc, 21,
189: Indenter.IndentReason.OTHER));
190: assertTrue("START is close brace.", !_rule.applyRule(_doc, 23,
191: Indenter.IndentReason.OTHER));
192:
193: /* (2) */
194:
195: _text = "class X extends Base\n" + "{\n"
196: + " int bla() { return 44; }\n" + "}";
197:
198: _setDocText(_text);
199:
200: assertTrue("START has no brace.", !_rule.applyRule(_doc, 0,
201: Indenter.IndentReason.OTHER));
202: assertTrue("START has no brace.", !_rule.applyRule(_doc, 20,
203: Indenter.IndentReason.OTHER));
204: assertTrue("START is curly brace.", !_rule.applyRule(_doc, 21,
205: Indenter.IndentReason.OTHER));
206: assertTrue("START's brace is curly brace.", !_rule.applyRule(
207: _doc, 23, Indenter.IndentReason.OTHER));
208: assertTrue("START is close curly brace.", !_rule.applyRule(
209: _doc, _text.length() - 1, Indenter.IndentReason.OTHER));
210:
211: /* (3) */
212:
213: _text = "class X extends Base\n" + "{}\n"
214: + "class Y extends Base\n" + "{}";
215:
216: _setDocText(_text);
217:
218: assertTrue("START has no brace.", !_rule.applyRule(_doc, 0,
219: Indenter.IndentReason.OTHER));
220: assertTrue("START has no brace.", !_rule.applyRule(_doc, 20,
221: Indenter.IndentReason.OTHER));
222: assertTrue("START is open curly brace.", !_rule.applyRule(_doc,
223: 21, Indenter.IndentReason.OTHER));
224: assertTrue("START has no brace.", !_rule.applyRule(_doc, 24,
225: Indenter.IndentReason.OTHER));
226: assertTrue("START's brace is open curly brace.", !_rule
227: .applyRule(_doc, _text.length() - 1,
228: Indenter.IndentReason.OTHER));
229: }
230:
231: public void testBracketWithArrayComprehension()
232: throws BadLocationException {
233: int i;
234:
235: /* (1) */
236:
237: _text = "f(int[\n" + " ] {1, 2, 3},\n"
238: + " a, String b)";
239:
240: _setDocText(_text);
241:
242: for (i = 0; i < 7; i++)
243: assertTrue("START has no brace.", !_rule.applyRule(_doc, i,
244: Indenter.IndentReason.OTHER));
245:
246: // For any position past the first '\n' character, the rule applies:
247:
248: for (i = 7; i < 27; i++)
249: assertTrue("START's brace is an open bracket.", _rule
250: .applyRule(_doc, i, Indenter.IndentReason.OTHER));
251:
252: for (i = 27; i < _text.length(); i++)
253: assertTrue("START's brace is an open paren.", _rule
254: .applyRule(_doc, i, Indenter.IndentReason.OTHER));
255: }
256: }
|