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 the character '{'.
046: * This questions corresponds to rule 15 in our decision tree.
047: * @version $Id: QuestionBraceIsCurlyTest.java 4255 2007-08-28 19:17:37Z mgricken $
048: */
049: public final class QuestionBraceIsCurlyTest extends IndentRulesTestCase {
050: // PRE: we are not inside a multiline comment
051: // PRE: the current block or expression list was *not*
052: // opened by '[' or '('.
053:
054: private String _text;
055:
056: private final IndentRuleQuestion _rule = new QuestionBraceIsCurly(
057: null, null);
058:
059: public void testWithParen() throws BadLocationException {
060: int i;
061:
062: /* (1) */
063:
064: _text = "boolean method(int[] a, String b) {}";
065: _setDocText(_text);
066:
067: for (i = 0; i < _text.length(); i++)
068: assertTrue("START has no brace.", !_rule.applyRule(_doc, i,
069: Indenter.IndentReason.OTHER));
070:
071: /* (2) */
072:
073: _text = "boolean method() {\n" + "}";
074:
075: _setDocText(_text);
076:
077: assertTrue("START has no brace.", !_rule.applyRule(_doc, 18,
078: Indenter.IndentReason.OTHER));
079: assertTrue("START's brace is curly brace.", _rule.applyRule(
080: _doc, 19, Indenter.IndentReason.OTHER));
081:
082: /* (3) */
083:
084: _text = "boolean method(\n" + " int[] a, String b)\n" + "{}";
085:
086: _setDocText(_text);
087:
088: assertTrue("START is open curly brace.", !_rule.applyRule(_doc,
089: _text.length() - 2, Indenter.IndentReason.OTHER));
090: assertTrue("START is open curly brace.", !_rule.applyRule(_doc,
091: _text.length() - 1, Indenter.IndentReason.OTHER));
092:
093: /* (4) */
094:
095: _text = "if (<cond>) {\n" + " if (\n"
096: + " <cond>) { ... }}";
097:
098: _setDocText(_text);
099:
100: assertTrue("START's brace is open curly brace.", _rule
101: .applyRule(_doc, 14, Indenter.IndentReason.OTHER));
102: assertTrue("START's brace is open curly brace.", _rule
103: .applyRule(_doc, 22, Indenter.IndentReason.OTHER));
104: assertTrue("START's brace is an open paren.", !_rule.applyRule(
105: _doc, 23, Indenter.IndentReason.OTHER));
106:
107: /* (5) */
108:
109: _text = "array[\n" + " new Listener() {\n"
110: + " method() {\n" + " }\n" + " }]";
111:
112: _setDocText(_text);
113:
114: assertTrue("START has no brace.", !_rule.applyRule(_doc, 0,
115: Indenter.IndentReason.OTHER));
116: assertTrue("START's brace is open bracket.", !_rule.applyRule(
117: _doc, 7, Indenter.IndentReason.OTHER));
118: assertTrue("START's brace is an open curly brace.", _rule
119: .applyRule(_doc, 28, Indenter.IndentReason.OTHER));
120: assertTrue("START's brace is an open curly brace.", _rule
121: .applyRule(_doc, 30, Indenter.IndentReason.OTHER));
122: assertTrue("START's brace is an open curly brace.", _rule
123: .applyRule(_doc, _text.length() - 1,
124: Indenter.IndentReason.OTHER));
125: }
126:
127: public void testOnlyCurly() throws BadLocationException {
128: /* (1) */
129:
130: _text = "{ /* block1 */ }\n" + "{ /* block2 */ }\n"
131: + "{ /* block3 */ }";
132:
133: _setDocText(_text);
134:
135: assertTrue("START has no brace.", !_rule.applyRule(_doc, 0,
136: Indenter.IndentReason.OTHER));
137: assertTrue("START has no brace.", !_rule.applyRule(_doc, 7,
138: Indenter.IndentReason.OTHER));
139: assertTrue("START has no brace.", !_rule.applyRule(_doc, 28,
140: Indenter.IndentReason.OTHER));
141: assertTrue("START has no brace.", !_rule.applyRule(_doc, 30,
142: Indenter.IndentReason.OTHER));
143: assertTrue("START has no brace.", !_rule.applyRule(_doc, _text
144: .length() - 1, Indenter.IndentReason.OTHER));
145:
146: /* (2) */
147:
148: _text = "{\n" + " {\n" + " {}\n" + " }\n" + "}";
149:
150: _setDocText(_text);
151:
152: assertTrue("START has no brace.", !_rule.applyRule(_doc, 0,
153: Indenter.IndentReason.OTHER));
154: assertTrue("START's brace is an open curly brace.", _rule
155: .applyRule(_doc, 7, Indenter.IndentReason.OTHER));
156: assertTrue("START's brace is an open curly brace.", _rule
157: .applyRule(_doc, 18, Indenter.IndentReason.OTHER));
158: assertTrue("START's brace is an open curly brace.", _rule
159: .applyRule(_doc, 19, Indenter.IndentReason.OTHER));
160: assertTrue("START's brace is an open curly brace.", _rule
161: .applyRule(_doc, _text.length() - 1,
162: Indenter.IndentReason.OTHER));
163:
164: /* (3) */
165:
166: _text = "class Foo {\n" + "}";
167: _setDocText(_text);
168:
169: assertTrue("Close brace immediately after open brace.", _rule
170: .applyRule(_doc, 12, Indenter.IndentReason.OTHER));
171:
172: /* (4) */
173:
174: _text = "class Foo {\n" + " method m()\n" + " {\n" + " }\n"
175: + "}";
176: _setDocText(_text);
177:
178: assertTrue("Close brace immediately after open brace.", _rule
179: .applyRule(_doc, 29, Indenter.IndentReason.OTHER));
180: }
181:
182: public void testEmbeddedBraceForms() throws BadLocationException {
183: /* (1) */
184:
185: _text = "Foo f1 = x,\n" + " f2 = new int[]{1, 2, 3},\n"
186: + " f3 = y;";
187:
188: _setDocText(_text);
189:
190: assertTrue("START has no brace.", !_rule.applyRule(_doc, 0,
191: Indenter.IndentReason.OTHER));
192: assertTrue("START has no brace.", !_rule.applyRule(_doc, 12,
193: Indenter.IndentReason.OTHER));
194: assertTrue("START has no brace.", !_rule.applyRule(_doc, 22,
195: Indenter.IndentReason.OTHER));
196: // assertTrue("START has brace.", _rule.applyRule(_doc, 32, Indenter.IndentReason.OTHER));
197: // assertTrue("START has brace.", _rule.applyRule(_doc, 40, Indenter.IndentReason.OTHER));
198: assertTrue("START has no brace.", !_rule.applyRule(_doc, _text
199: .length() - 1, Indenter.IndentReason.OTHER));
200:
201: /* (2) */
202:
203: }
204: }
|