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 the action rules for code in the indentation decision tree.
043: * Assumes cursor is within a brace.
044: * @version $Id: ActionStartStmtOfBracePlusTest.java 4255 2007-08-28 19:17:37Z mgricken $
045: */
046: public final class ActionStartStmtOfBracePlusTest extends
047: IndentRulesTestCase {
048:
049: /**
050: * Tests indenting with a single line contract
051: */
052: public void testSingleLineContract() throws BadLocationException {
053: IndentRuleAction rule1 = new ActionStartStmtOfBracePlus("");
054: IndentRuleAction rule2 = new ActionStartStmtOfBracePlus(" "); // 3 spaces
055:
056: String text = "public void foo() {\nbar();";
057: String aligned1 = text;
058: String aligned2 = "public void foo() {\n bar();";
059:
060: _setDocText(text);
061: rule1.indentLine(_doc, 20, Indenter.IndentReason.OTHER);
062: assertEquals("single line contract, no indent, no suffix",
063: aligned1, _doc.getText());
064:
065: _setDocText(text);
066: rule2.indentLine(_doc, 20, Indenter.IndentReason.OTHER);
067: assertEquals("single line contract, no indent, with suffix",
068: aligned2, _doc.getText());
069: }
070:
071: /**
072: * Tests indenting with an indented single line contract
073: */
074: public void testIndentedSingleLineContract()
075: throws BadLocationException {
076: IndentRuleAction rule1 = new ActionStartStmtOfBracePlus("");
077: IndentRuleAction rule2 = new ActionStartStmtOfBracePlus(" "); // 3 spaces
078:
079: String text = " y = new Foo() {\nbar();";
080: String aligned1 = " y = new Foo() {\n bar();";
081: String aligned2 = " y = new Foo() {\n bar();";
082:
083: _setDocText(text);
084: rule1.indentLine(_doc, 20, Indenter.IndentReason.OTHER);
085: assertEquals("single line contract, with indent, no suffix",
086: aligned1, _doc.getText());
087:
088: _setDocText(text);
089: rule2.indentLine(_doc, 20, Indenter.IndentReason.OTHER);
090: assertEquals("single line contract, with indent, with suffix",
091: aligned2, _doc.getText());
092: }
093:
094: /**
095: * Tests indenting with a multiple line contract
096: */
097: public void testMultiLineContract() throws BadLocationException {
098: IndentRuleAction rule1 = new ActionStartStmtOfBracePlus("");
099: IndentRuleAction rule2 = new ActionStartStmtOfBracePlus(" "); // 2 spaces
100:
101: String text = " foobar();\n" + " int foo(int x,\n"
102: + " int y) {\n" + "bar();";
103: String aligned1 = " foobar();\n" + " int foo(int x,\n"
104: + " int y) {\n" + " bar();";
105: String aligned2 = " foobar();\n" + " int foo(int x,\n"
106: + " int y) {\n" + " bar();";
107:
108: _setDocText(text);
109: rule1.indentLine(_doc, 56, Indenter.IndentReason.OTHER);
110: assertEquals("multi line contract, with indent, no suffix",
111: aligned1, _doc.getText());
112:
113: _setDocText(text);
114: rule2.indentLine(_doc, 56, Indenter.IndentReason.OTHER);
115: assertEquals("multi line contract, with indent, with suffix",
116: aligned2, _doc.getText());
117: }
118:
119: /**
120: * Tests indenting a for statement (odd semicolons)
121: */
122: public void testForStatement() throws BadLocationException {
123: IndentRuleAction rule1 = new ActionStartStmtOfBracePlus("");
124: IndentRuleAction rule2 = new ActionStartStmtOfBracePlus(" "); // 3 spaces
125:
126: String text = " for (int i=0; i<j; i++) {\nbar();";
127: String aligned1 = " for (int i=0; i<j; i++) {\n bar();";
128: String aligned2 = " for (int i=0; i<j; i++) {\n bar();";
129:
130: _setDocText(text);
131: rule1.indentLine(_doc, 28, Indenter.IndentReason.OTHER);
132: assertEquals("for statement, with indent, no suffix", aligned1,
133: _doc.getText());
134:
135: _setDocText(text);
136: rule2.indentLine(_doc, 28, Indenter.IndentReason.OTHER);
137: assertEquals("for statement, with indent, with suffix",
138: aligned2, _doc.getText());
139: }
140:
141: /**
142: * Tests indenting a multiple line for statement (odd semicolons)
143: */
144: public void testMultiLineForStatement() throws BadLocationException {
145: IndentRuleAction rule1 = new ActionStartStmtOfBracePlus("");
146: IndentRuleAction rule2 = new ActionStartStmtOfBracePlus(" "); // 2 spaces
147:
148: String text = " for (int i=0;\n" + " i<j;\n"
149: + " i++)\n" + " {\n" + "bar();";
150: String aligned1 = " for (int i=0;\n" + " i<j;\n"
151: + " i++)\n" + " {\n" + " bar();";
152: String aligned2 = " for (int i=0;\n" + " i<j;\n"
153: + " i++)\n" + " {\n" + " bar();";
154:
155: _setDocText(text);
156: rule1.indentLine(_doc, 44, Indenter.IndentReason.OTHER);
157: assertEquals(
158: "multi-line for statement, with indent, no suffix",
159: aligned1, _doc.getText());
160:
161: _setDocText(text);
162: rule2.indentLine(_doc, 44, Indenter.IndentReason.OTHER);
163: assertEquals(
164: "multi-line for statement, with indent, with suffix",
165: aligned2, _doc.getText());
166: }
167:
168: /**
169: * Tests indenting with nested braces
170: * Note: multiple braces on a line are not yet supported. This test
171: * will be useful in a later version.
172: *
173: public void testNestedBraces() throws BadLocationException {
174: IndentRuleAction rule1 = new ActionStartStmtOfBracePlus("");
175: IndentRuleAction rule2 = new ActionStartStmtOfBracePlus(" "); // 2 spaces
176:
177: String text = " { { }\n" +
178: " {\n" +
179: " } {\n" +
180: "foo();\n";
181: String aligned1 = " { { }\n" +
182: " {\n" +
183: " } {\n" +
184: " foo();\n";
185: String aligned2 = " { { }\n" +
186: " {\n" +
187: " } {\n" +
188: " foo();\n";
189:
190: _setDocText(text);
191: rule1.indentLine(_doc, 28);
192: assertEquals("nested braces, no suffix",
193: aligned1, _doc.getText());
194:
195: _setDocText(text);
196: rule2.indentLine(_doc, 28);
197: assertEquals("nested braces, with suffix",
198: aligned2, _doc.getText());
199: }*/
200:
201: /**
202: * Tests indenting with commented delimiters
203: */
204: public void testCommentedBrace() throws BadLocationException {
205: IndentRuleAction rule1 = new ActionStartStmtOfBracePlus("");
206: IndentRuleAction rule2 = new ActionStartStmtOfBracePlus(" "); // 2 spaces
207:
208: String text = " void foo()\n" + " {\n" + " // {\n"
209: + "foo();\n";
210: String aligned1 = " void foo()\n" + " {\n" + " // {\n"
211: + " foo();\n";
212: String aligned2 = " void foo()\n" + " {\n" + " // {\n"
213: + " foo();\n";
214:
215: _setDocText(text);
216: rule1.indentLine(_doc, 30, Indenter.IndentReason.OTHER);
217: assertEquals("commented brace, no suffix", aligned1, _doc
218: .getText());
219:
220: _setDocText(text);
221: rule2.indentLine(_doc, 30, Indenter.IndentReason.OTHER);
222: assertEquals("commented brace, with suffix", aligned2, _doc
223: .getText());
224: }
225:
226: /**
227: * Tests having start of line belong to a different brace
228: * Note: multiple braces on a line are not yet supported. This test
229: * will be useful in a later version.
230: *
231: public void testStartOfLineInDifferentBrace() throws BadLocationException {
232: IndentRuleAction rule1 = new ActionStartStmtOfBracePlus("");
233: IndentRuleAction rule2 = new ActionStartStmtOfBracePlus(" "); // 2 spaces
234:
235: String text = " class Foo {\n" +
236: " void bar() {\n" +
237: " i=0; } void foo() {\n" +
238: "bar();\n";
239: String aligned1 = " class Foo {\n" +
240: " void bar() {\n" +
241: " i=0; } void foo() {\n" +
242: " bar();\n";
243: String aligned2 = " class Foo {\n" +
244: " void bar() {\n" +
245: " i=0; } void foo() {\n" +
246: " bar();\n";
247:
248: _setDocText(text);
249: rule1.indentLine(_doc, 30);
250: assertEquals("start in different brace, no suffix",
251: aligned1, _doc.getText());
252:
253: _setDocText(text);
254: rule2.indentLine(_doc, 30);
255: assertEquals("start in different brace, with suffix",
256: aligned2, _doc.getText());
257: }*/
258:
259: /**
260: * Tests indenting without an enclosing brace
261: */
262: public void testNoBrace() throws BadLocationException {
263: IndentRuleAction rule1 = new ActionStartStmtOfBracePlus("");
264: IndentRuleAction rule2 = new ActionStartStmtOfBracePlus(" "); // 2 spaces
265:
266: String text = "package foo;\n" + "import bar.*;\n";
267: String aligned1 = "package foo;\n" + "import bar.*;\n";
268: String aligned2 = "package foo;\n" + " import bar.*;\n";
269:
270: _setDocText(text);
271: rule1.indentLine(_doc, 13, Indenter.IndentReason.OTHER);
272: assertEquals("no brace, no suffix", aligned1, _doc.getText());
273:
274: _setDocText(text);
275: rule2.indentLine(_doc, 13, Indenter.IndentReason.OTHER);
276: assertEquals("no brace, with suffix", aligned2, _doc.getText());
277: }
278: }
|