KickJava   Java API By Example, From Geeks To Geeks.

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


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 question
40  * that determines whether or not the last block or expression list
41  * opened previous to the start of the current line was opened
42  * by one of the characters '(' or '['.
43  * This questions corresponds to rule 11 in our decision tree.
44  * @version $Id: QuestionBraceIsParenOrBracketTest.java 3901 2006-06-30 05:28:11Z rcartwright $
45  */

46 public final class QuestionBraceIsParenOrBracketTest extends IndentRulesTestCase {
47   // PRE: We are not inside a multiline comment.
48

49   private String JavaDoc _text;
50   
51   private final IndentRuleQuestion _rule = new QuestionBraceIsParenOrBracket(null, null);
52   
53   public void testParen() throws BadLocationException JavaDoc {
54     int i;
55     
56     /* (1) */
57     _text = "boolean method(int[] a, String b)";
58     _setDocText(_text);
59     
60     for (i = 0; i < _text.length(); i++)
61       assertTrue("START has no brace.", !_rule.applyRule(_doc, i, Indenter.OTHER));
62     
63     /* (2) */
64     _text =
65       "boolean method\n" +
66       " (int[] a, String b)";
67     
68     _setDocText(_text);
69     
70     for (i = 0; i < _text.length(); i++)
71       assertTrue("START has no brace.", !_rule.applyRule(_doc, i, Indenter.OTHER));
72     
73     /* (3) */
74     _text =
75       "boolean method(\n" +
76       " int[] a, String b)";
77     
78     _setDocText(_text);
79     
80     for (i = 0; i < 16; i++)
81       assertTrue("START has no brace.", !_rule.applyRule(_doc, i, Indenter.OTHER));
82     
83     // For any position past the '\n' character, the rule applies:
84

85     for (i = 16; i < _text.length(); i++)
86       assertTrue("START's brace is an open paren.", _rule.applyRule(_doc, i, Indenter.OTHER));
87     
88     /* (4) */
89     
90     _text =
91       "if (<cond>) {\n" +
92       " if (\n" +
93       " <cond>) { ... }}";
94     
95     _setDocText(_text);
96     
97     for (i = 0; i < 23; i++)
98       assertTrue("START has no brace.", !_rule.applyRule(_doc, i, Indenter.OTHER));
99     
100     // For any position past the second '\n' character, the rule applies:
101

102     for (i = 23; i < _text.length(); i++)
103       assertTrue("START's brace is an open paren.", _rule.applyRule(_doc, i, Indenter.OTHER));
104     
105     /* (5) */
106     
107     _text =
108       "method(\n" +
109       " array1, foo(array1[x]))\n" +
110       " <other stuff>";
111     
112     _setDocText(_text);
113     
114     assertTrue("START has no brace.", !_rule.applyRule(_doc, 0, Indenter.OTHER));
115     assertTrue("START has no brace", !_rule.applyRule(_doc, 7, Indenter.OTHER));
116     assertTrue("START's brace is an open paren.", _rule.applyRule(_doc, 8, Indenter.OTHER));
117     assertTrue("START's brace is an open paren.", _rule.applyRule(_doc, 30, Indenter.OTHER));
118     assertTrue("START has no brace.", !_rule.applyRule(_doc, _text.length() - 1, Indenter.OTHER));
119   }
120   
121   public void testBracket() throws BadLocationException JavaDoc
122   {
123     int i;
124     
125     /* (1) */
126     
127     _text =
128       "boolean method(int[\n" +
129       " ELTS]\n" +
130       " a, String b)";
131     
132     _setDocText(_text);
133     
134     for (i = 0; i < 20; i++)
135       assertTrue("START has no brace.", !_rule.applyRule(_doc, i, Indenter.OTHER));
136     
137     // For any position past the first '\n' character, the rule applies:
138

139     for (i = 20; i < 29; i++)
140       assertTrue("START's brace is an open bracket.", _rule.applyRule(_doc, i, Indenter.OTHER));
141     
142     for (i = 29; i < _text.length(); i++)
143       assertTrue("START's brace is an open paren.", _rule.applyRule(_doc, i, Indenter.OTHER));
144     
145     /* (2) */
146     
147     _text = "array1[i]\n" +
148       " [j]";
149     
150     _setDocText(_text);
151     
152     for (i = 0; i < _text.length(); i++)
153       assertTrue("START has no brace.", !_rule.applyRule(_doc, i, Indenter.OTHER));
154     
155     /* (3) */
156     
157     _text =
158       "array1[\n" +
159       " i][\n" +
160       " j]";
161     
162     _setDocText(_text);
163     
164     assertTrue("START's paren is an open bracket.", _rule.applyRule(_doc, 8, Indenter.OTHER));
165     assertTrue("START's paren is an open bracket.", _rule.applyRule(_doc, 22, Indenter.OTHER));
166     assertTrue("START's paren is an open bracket.", _rule.applyRule(_doc, 23, Indenter.OTHER));
167   }
168   
169   public void testCurly() throws BadLocationException JavaDoc
170   {
171     /* (1) */
172     
173     _text =
174       "class X extends Base\n" +
175       "{\n" +
176       "}";
177     
178     _setDocText(_text);
179     
180     assertTrue("START has no brace.", !_rule.applyRule(_doc, 0, Indenter.OTHER));
181     assertTrue("START has no brace.", !_rule.applyRule(_doc, 20, Indenter.OTHER));
182     assertTrue("START is curly brace.", !_rule.applyRule(_doc, 21, Indenter.OTHER));
183     assertTrue("START is close brace.", !_rule.applyRule(_doc, 23, Indenter.OTHER));
184     
185     /* (2) */
186     
187     _text =
188       "class X extends Base\n" +
189       "{\n" +
190       " int bla() { return 44; }\n" +
191       "}";
192     
193     _setDocText(_text);
194     
195     assertTrue("START has no brace.", !_rule.applyRule(_doc, 0, Indenter.OTHER));
196     assertTrue("START has no brace.", !_rule.applyRule(_doc, 20, Indenter.OTHER));
197     assertTrue("START is curly brace.", !_rule.applyRule(_doc, 21, Indenter.OTHER));
198     assertTrue("START's brace is curly brace.", !_rule.applyRule(_doc, 23, Indenter.OTHER));
199     assertTrue("START is close curly brace.", !_rule.applyRule(_doc, _text.length() - 1, Indenter.OTHER));
200     
201     /* (3) */
202     
203     _text =
204       "class X extends Base\n" +
205       "{}\n" +
206       "class Y extends Base\n" +
207       "{}";
208     
209     _setDocText(_text);
210     
211     assertTrue("START has no brace.", !_rule.applyRule(_doc, 0, Indenter.OTHER));
212     assertTrue("START has no brace.", !_rule.applyRule(_doc, 20, Indenter.OTHER));
213     assertTrue("START is open curly brace.", !_rule.applyRule(_doc, 21, Indenter.OTHER));
214     assertTrue("START has no brace.", !_rule.applyRule(_doc, 24, Indenter.OTHER));
215     assertTrue("START's brace is open curly brace.", !_rule.applyRule(_doc, _text.length() - 1, Indenter.OTHER));
216   }
217   
218   public void testBracketWithArrayComprehension() throws BadLocationException JavaDoc {
219     int i;
220     
221     /* (1) */
222     
223     _text =
224       "f(int[\n" +
225       " ] {1, 2, 3},\n" +
226       " a, String b)";
227     
228     _setDocText(_text);
229     
230     for (i = 0; i < 7; i++)
231       assertTrue("START has no brace.", !_rule.applyRule(_doc, i, Indenter.OTHER));
232     
233     // For any position past the first '\n' character, the rule applies:
234

235     for (i = 7; i < 27; i++)
236       assertTrue("START's brace is an open bracket.", _rule.applyRule(_doc, i, Indenter.OTHER));
237     
238     for (i = 27; i < _text.length(); i++)
239       assertTrue("START's brace is an open paren.", _rule.applyRule(_doc, i, Indenter.OTHER));
240   }
241 }
242
243
244
245
246
247
248
Popular Tags