KickJava   Java API By Example, From Geeks To Geeks.

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


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 class according to the JUnit protocol. Tests the action
40  * that aligns the indentation of the current line to the character
41  * that opened the most recent block or expression list that contains
42  * the beginning of the current line. Optional additional whitespaces
43  * can be passed through the constructor.
44  * @version $Id: ActionBracePlusTest.java 3901 2006-06-30 05:28:11Z rcartwright $
45  */

46 public final class ActionBracePlusTest extends IndentRulesTestCase {
47   private String JavaDoc _text, _aligned;
48   
49   private IndentRuleAction _action;
50   
51   public void testNoSuffix() throws BadLocationException JavaDoc {
52     _action = new ActionBracePlus("");
53     
54     // (1)
55

56     _text =
57       "method(\n"+
58       ")\n";
59
60     _aligned =
61       "method(\n"+
62       " )\n";
63  
64     _setDocText(_text);
65     _action.indentLine(_doc, 0, Indenter.OTHER); // Does nothing.
66
assertEquals("START has no brace.", _text.length(), _doc.getLength());
67     _action.indentLine(_doc, 7, Indenter.OTHER); // Does nothing.
68
assertEquals("START has no brace.", _text.length(), _doc.getLength());
69     _action.indentLine(_doc, 8, Indenter.OTHER); // Aligns second line.
70
assertEquals("Line aligned to open paren.", _aligned.length(), _doc.getLength());
71     assertEquals("Line aligned to open paren.", _aligned, _doc.getText());
72   }
73   
74
75   public void testSpaceSuffix() throws BadLocationException JavaDoc
76   {
77     _action = new ActionBracePlus(" ");
78     
79     // (2)
80

81     _text =
82      "var = method(arg1,\n"+
83      " arg2, arg3) + 4;";
84
85     _aligned =
86      "var = method(arg1,\n"+
87      " arg2, arg3) + 4;";
88  
89     _setDocText(_text);
90     _action.indentLine(_doc, 0, Indenter.OTHER); // Does nothing.
91
assertEquals("START has no brace.", _text.length(), _doc.getLength());
92     _action.indentLine(_doc, 18, Indenter.OTHER); // Does nothing.
93
assertEquals("START has no brace.", _text.length(), _doc.getLength());
94     _action.indentLine(_doc, 20, Indenter.OTHER); // Aligns second line.
95
assertEquals("Line aligned to open paren.", _aligned.length(), _doc.getLength());
96     assertEquals("Line aligned to open paren.", _aligned, _doc.getText());
97     
98     // (3)
99

100     _text =
101      "boolean method(\n"+
102      "int[] a, String b)\n"+
103      "{}";
104     _aligned =
105      "boolean method(\n"+
106      " int[] a, String b)\n"+
107      "{}";
108
109     _setDocText(_text);
110     _action.indentLine(_doc, 0, Indenter.OTHER); // Does nothing.
111
assertEquals("START has no brace.", _text.length(), _doc.getLength());
112     _action.indentLine(_doc, 15, Indenter.OTHER); // Does nothing.
113
assertEquals("START has no brace.", _text.length(), _doc.getLength());
114     _action.indentLine(_doc, 16, Indenter.OTHER); // Aligns second line.
115
assertEquals("Line aligned to open paren.", _aligned.length(), _doc.getLength());
116     assertEquals("Line aligned to open paren.", _aligned, _doc.getText());
117  
118     // (4)
119

120     _text =
121      "boolean method(\n"+
122      "int[] a,\n"+
123      " String b)\n"+
124      "{}";
125     _aligned =
126      "boolean method(\n"+
127      " int[] a,\n"+
128      " String b)\n"+
129      "{}";
130
131     _setDocText(_text);
132     _action.indentLine(_doc, 0, Indenter.OTHER); // Does nothing.
133
assertEquals("START has no brace.", _text.length(), _doc.getLength());
134     _action.indentLine(_doc, 15, Indenter.OTHER); // Does nothing.
135
assertEquals("START has no brace.", _text.length(), _doc.getLength());
136     _action.indentLine(_doc, 20, Indenter.OTHER); // Aligns second line.
137
assertEquals("Line aligned to open paren.", _aligned, _doc.getText());
138  
139     // (5)
140

141     _text =
142      "array[\n"+
143      " new Listener() {\n"+
144      " method() {\n"+
145      " }\n"+
146      " }]";
147     _aligned =
148      "array[\n"+
149      " new Listener() {\n"+
150      " method() {\n"+
151      " }\n"+
152      " }]";
153
154     _setDocText(_text);
155     _action.indentLine(_doc, 0, Indenter.OTHER); // Does nothing.
156
assertEquals("START has no brace.", _text.length(), _doc.getLength());
157     _action.indentLine(_doc, 6, Indenter.OTHER); // Does nothing.
158
assertEquals("START has no brace.", _text.length(), _doc.getLength());
159     _action.indentLine(_doc, 10, Indenter.OTHER); // Aligns second line.
160
assertEquals("Line aligned to open bracket.", _aligned, _doc.getText());
161
162   }
163   
164   public void testLargeSuffix() throws BadLocationException JavaDoc
165   {
166     _action = new ActionBracePlus(" " + " ");
167     
168     // (6)
169

170     _text =
171      "var = method(foo.\n"+
172      " bar(), arg3) + 4;";
173
174     _aligned =
175      "var = method(foo.\n"+
176      " bar(), arg3) + 4;";
177  
178     _setDocText(_text);
179     _action.indentLine(_doc, 0, Indenter.OTHER); // Does nothing.
180
assertEquals("START has no brace.", _text.length(), _doc.getLength());
181     _action.indentLine(_doc, 17, Indenter.OTHER); // Does nothing.
182
assertEquals("START has no brace.", _text.length(), _doc.getLength());
183     _action.indentLine(_doc, 25, Indenter.OTHER); // Aligns second line.
184
assertEquals("Line aligned to open paren.", _aligned.length(), _doc.getLength());
185     assertEquals("Line aligned to open paren.", _aligned, _doc.getText());
186   }
187   
188   public void testComment() throws BadLocationException JavaDoc
189   {
190     _action = new ActionBracePlus(" " + " ");
191     
192     // (7)
193

194     _text =
195       "foo(i,\n"+
196       " j.\n" +
197       "bar().\n" +
198       "// bar();\n" +
199       "baz(),\n" +
200       " k);";
201
202     _aligned =
203       "foo(i,\n"+
204       " j.\n" +
205       " bar().\n" +
206       " // bar();\n" +
207       " baz(),\n" +
208       " k);";
209  
210     _setDocText(_text);
211     _action.indentLine(_doc, 14, Indenter.OTHER); // line 3
212
_action.indentLine(_doc, 27, Indenter.OTHER); // line 4
213
_action.indentLine(_doc, 43, Indenter.OTHER); // line 5
214
assertEquals("Lines aligned plus one level.",
215                  _aligned, _doc.getText());
216     
217     _action.indentLine(_doc, 54, Indenter.OTHER); // after baz()
218
assertEquals("Cursor after baz().", _aligned, _doc.getText());
219   }
220 }
221
Popular Tags