KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > edu > rice > cs > drjava > model > definitions > indent > ActionStartStmtOfBracePlusTest


1 /*BEGIN_COPYRIGHT_BLOCK
2  *
3  * This file is part of DrJava. Download the current version of this project from http://www.drjava.org/
4  * or http://sourceforge.net/projects/drjava/
5  *
6  * DrJava Open Source License
7  *
8  * Copyright (C) 2001-2006 JavaPLT group at Rice University (javaplt@rice.edu). All rights reserved.
9  *
10  * Developed by: Java Programming Languages Team, Rice University, http://www.cs.rice.edu/~javaplt/
11  *
12  * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
13  * documentation files (the "Software"), to deal with the Software without restriction, including without limitation
14  * the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and
15  * to permit persons to whom the Software is furnished to do so, subject to the following conditions:
16  *
17  * - Redistributions of source code must retain the above copyright notice, this list of conditions and the
18  * following disclaimers.
19  * - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
20  * following disclaimers in the documentation and/or other materials provided with the distribution.
21  * - Neither the names of DrJava, the JavaPLT, Rice University, nor the names of its contributors may be used to
22  * endorse or promote products derived from this Software without specific prior written permission.
23  * - Products derived from this software may not be called "DrJava" nor use the term "DrJava" as part of their
24  * names without prior written permission from the JavaPLT group. For permission, write to javaplt@rice.edu.
25  *
26  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
27  * THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
28  * CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
29  * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
30  * WITH THE SOFTWARE.
31  *
32  *END_COPYRIGHT_BLOCK*/

33
34 package edu.rice.cs.drjava.model.definitions.indent;
35
36 import javax.swing.text.BadLocationException JavaDoc;
37
38 /**
39  * Test the action rules for code in the indentation decision tree.
40  * Assumes cursor is within a brace.
41  * @version $Id: ActionStartStmtOfBracePlusTest.java 3901 2006-06-30 05:28:11Z rcartwright $
42  */

43 public final class ActionStartStmtOfBracePlusTest extends IndentRulesTestCase {
44
45
46   /**
47    * Tests indenting with a single line contract
48    */

49   public void testSingleLineContract() throws BadLocationException JavaDoc {
50     IndentRuleAction rule1 = new ActionStartStmtOfBracePlus("");
51     IndentRuleAction rule2 = new ActionStartStmtOfBracePlus(" "); // 3 spaces
52

53     String JavaDoc text = "public void foo() {\nbar();";
54     String JavaDoc aligned1 = text;
55     String JavaDoc aligned2 = "public void foo() {\n bar();";
56     
57     _setDocText(text);
58     rule1.indentLine(_doc, 20, Indenter.OTHER);
59     assertEquals("single line contract, no indent, no suffix",
60                  aligned1, _doc.getText());
61     
62     _setDocText(text);
63     rule2.indentLine(_doc, 20, Indenter.OTHER);
64     assertEquals("single line contract, no indent, with suffix",
65                  aligned2, _doc.getText());
66   }
67   
68   /**
69    * Tests indenting with an indented single line contract
70    */

71   public void testIndentedSingleLineContract() throws BadLocationException JavaDoc {
72     IndentRuleAction rule1 = new ActionStartStmtOfBracePlus("");
73     IndentRuleAction rule2 = new ActionStartStmtOfBracePlus(" "); // 3 spaces
74

75     String JavaDoc text = " y = new Foo() {\nbar();";
76     String JavaDoc aligned1 = " y = new Foo() {\n bar();";
77     String JavaDoc aligned2 = " y = new Foo() {\n bar();";
78     
79     _setDocText(text);
80     rule1.indentLine(_doc, 20, Indenter.OTHER);
81     assertEquals("single line contract, with indent, no suffix",
82                  aligned1, _doc.getText());
83     
84     _setDocText(text);
85     rule2.indentLine(_doc, 20, Indenter.OTHER);
86     assertEquals("single line contract, with indent, with suffix",
87                  aligned2, _doc.getText());
88   }
89   
90   /**
91    * Tests indenting with a multiple line contract
92    */

93   public void testMultiLineContract() throws BadLocationException JavaDoc {
94     IndentRuleAction rule1 = new ActionStartStmtOfBracePlus("");
95     IndentRuleAction rule2 = new ActionStartStmtOfBracePlus(" "); // 2 spaces
96

97     String JavaDoc text = " foobar();\n" +
98                   " int foo(int x,\n" +
99                   " int y) {\n" +
100                   "bar();";
101     String JavaDoc aligned1 = " foobar();\n" +
102                       " int foo(int x,\n" +
103                       " int y) {\n" +
104                       " bar();";
105     String JavaDoc aligned2 = " foobar();\n" +
106                       " int foo(int x,\n" +
107                       " int y) {\n" +
108                       " bar();";
109     
110     _setDocText(text);
111     rule1.indentLine(_doc, 56, Indenter.OTHER);
112     assertEquals("multi line contract, with indent, no suffix",
113                  aligned1, _doc.getText());
114     
115     _setDocText(text);
116     rule2.indentLine(_doc, 56, Indenter.OTHER);
117     assertEquals("multi line contract, with indent, with suffix",
118                  aligned2, _doc.getText());
119   }
120   
121   /**
122    * Tests indenting a for statement (odd semicolons)
123    */

124   public void testForStatement() throws BadLocationException JavaDoc {
125     IndentRuleAction rule1 = new ActionStartStmtOfBracePlus("");
126     IndentRuleAction rule2 = new ActionStartStmtOfBracePlus(" "); // 3 spaces
127

128     String JavaDoc text = " for (int i=0; i<j; i++) {\nbar();";
129     String JavaDoc aligned1 = " for (int i=0; i<j; i++) {\n bar();";
130     String JavaDoc aligned2 = " for (int i=0; i<j; i++) {\n bar();";
131     
132     _setDocText(text);
133     rule1.indentLine(_doc, 28, Indenter.OTHER);
134     assertEquals("for statement, with indent, no suffix",
135                  aligned1, _doc.getText());
136     
137     _setDocText(text);
138     rule2.indentLine(_doc, 28, Indenter.OTHER);
139     assertEquals("for statement, with indent, with suffix",
140                  aligned2, _doc.getText());
141   }
142   
143   /**
144    * Tests indenting a multiple line for statement (odd semicolons)
145    */

146   public void testMultiLineForStatement() throws BadLocationException JavaDoc {
147     IndentRuleAction rule1 = new ActionStartStmtOfBracePlus("");
148     IndentRuleAction rule2 = new ActionStartStmtOfBracePlus(" "); // 2 spaces
149

150     String JavaDoc text = " for (int i=0;\n" +
151                   " i<j;\n" +
152                   " i++)\n" +
153                   " {\n" +
154                   "bar();";
155     String JavaDoc aligned1 = " for (int i=0;\n" +
156                       " i<j;\n" +
157                       " i++)\n" +
158                       " {\n" +
159                       " bar();";
160     String JavaDoc aligned2 = " for (int i=0;\n" +
161                       " i<j;\n" +
162                       " i++)\n" +
163                       " {\n" +
164                       " bar();";
165     
166     _setDocText(text);
167     rule1.indentLine(_doc, 44, Indenter.OTHER);
168     assertEquals("multi-line for statement, with indent, no suffix",
169                  aligned1, _doc.getText());
170     
171     _setDocText(text);
172     rule2.indentLine(_doc, 44, Indenter.OTHER);
173     assertEquals("multi-line for statement, with indent, with suffix",
174                  aligned2, _doc.getText());
175   }
176   
177   /**
178    * Tests indenting with nested braces
179    * Note: multiple braces on a line are not yet supported. This test
180    * will be useful in a later version.
181    *
182   public void testNestedBraces() throws BadLocationException {
183     IndentRuleAction rule1 = new ActionStartStmtOfBracePlus("");
184     IndentRuleAction rule2 = new ActionStartStmtOfBracePlus(" "); // 2 spaces
185     
186     String text = " { { }\n" +
187                   " {\n" +
188                   " } {\n" +
189                   "foo();\n";
190     String aligned1 = " { { }\n" +
191                       " {\n" +
192                       " } {\n" +
193                       " foo();\n";
194     String aligned2 = " { { }\n" +
195                       " {\n" +
196                       " } {\n" +
197                       " foo();\n";
198     
199     _setDocText(text);
200     rule1.indentLine(_doc, 28);
201     assertEquals("nested braces, no suffix",
202                  aligned1, _doc.getText());
203     
204     _setDocText(text);
205     rule2.indentLine(_doc, 28);
206     assertEquals("nested braces, with suffix",
207                  aligned2, _doc.getText());
208    }*/

209   
210   /**
211    * Tests indenting with commented delimiters
212    */

213   public void testCommentedBrace() throws BadLocationException JavaDoc {
214     IndentRuleAction rule1 = new ActionStartStmtOfBracePlus("");
215     IndentRuleAction rule2 = new ActionStartStmtOfBracePlus(" "); // 2 spaces
216

217     String JavaDoc text = " void foo()\n" +
218                   " {\n" +
219                   " // {\n" +
220                   "foo();\n";
221     String JavaDoc aligned1 = " void foo()\n" +
222                       " {\n" +
223                       " // {\n" +
224                       " foo();\n";
225     String JavaDoc aligned2 = " void foo()\n" +
226                       " {\n" +
227                       " // {\n" +
228                       " foo();\n";
229     
230     _setDocText(text);
231     rule1.indentLine(_doc, 30, Indenter.OTHER);
232     assertEquals("commented brace, no suffix",
233                  aligned1, _doc.getText());
234     
235     _setDocText(text);
236     rule2.indentLine(_doc, 30, Indenter.OTHER);
237     assertEquals("commented brace, with suffix",
238                  aligned2, _doc.getText());
239   }
240   
241   /**
242    * Tests having start of line belong to a different brace
243    * Note: multiple braces on a line are not yet supported. This test
244    * will be useful in a later version.
245    *
246   public void testStartOfLineInDifferentBrace() throws BadLocationException {
247     IndentRuleAction rule1 = new ActionStartStmtOfBracePlus("");
248     IndentRuleAction rule2 = new ActionStartStmtOfBracePlus(" "); // 2 spaces
249     
250     String text = " class Foo {\n" +
251                   " void bar() {\n" +
252                   " i=0; } void foo() {\n" +
253                   "bar();\n";
254     String aligned1 = " class Foo {\n" +
255                       " void bar() {\n" +
256                       " i=0; } void foo() {\n" +
257                       " bar();\n";
258     String aligned2 = " class Foo {\n" +
259                       " void bar() {\n" +
260                       " i=0; } void foo() {\n" +
261                       " bar();\n";
262     
263     _setDocText(text);
264     rule1.indentLine(_doc, 30);
265     assertEquals("start in different brace, no suffix",
266                  aligned1, _doc.getText());
267     
268     _setDocText(text);
269     rule2.indentLine(_doc, 30);
270     assertEquals("start in different brace, with suffix",
271                  aligned2, _doc.getText());
272    }*/

273   
274   /**
275    * Tests indenting without an enclosing brace
276    */

277   public void testNoBrace() throws BadLocationException JavaDoc {
278     IndentRuleAction rule1 = new ActionStartStmtOfBracePlus("");
279     IndentRuleAction rule2 = new ActionStartStmtOfBracePlus(" "); // 2 spaces
280

281     String JavaDoc text = "package foo;\n" +
282                   "import bar.*;\n";
283     String JavaDoc aligned1 = "package foo;\n" +
284                       "import bar.*;\n";
285     String JavaDoc aligned2 = "package foo;\n" +
286                       " import bar.*;\n";
287     
288     _setDocText(text);
289     rule1.indentLine(_doc, 13, Indenter.OTHER);
290     assertEquals("no brace, no suffix",
291                  aligned1, _doc.getText());
292     
293     _setDocText(text);
294     rule2.indentLine(_doc, 13, Indenter.OTHER);
295     assertEquals("no brace, with suffix",
296                  aligned2, _doc.getText());
297   }
298 }
299
300
301
302
Popular Tags