KickJava   Java API By Example, From Geeks To Geeks.

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


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 the character '{'.
43  * This questions corresponds to rule 15 in our decision tree.
44  * @version $Id: QuestionBraceIsCurlyTest.java 3901 2006-06-30 05:28:11Z rcartwright $
45  */

46 public final class QuestionBraceIsCurlyTest extends IndentRulesTestCase {
47   // PRE: we are not inside a multiline comment
48
// PRE: the current block or expression list was *not*
49
// opened by '[' or '('.
50

51   private String JavaDoc _text;
52   
53   private final IndentRuleQuestion _rule = new QuestionBraceIsCurly(null, null);
54   
55   public void testWithParen() throws BadLocationException JavaDoc {
56     int i;
57     
58     /* (1) */
59     
60     _text = "boolean method(int[] a, String b) {}";
61     _setDocText(_text);
62     
63     for (i = 0; i < _text.length(); i++)
64       assertTrue("START has no brace.", !_rule.applyRule(_doc, i, Indenter.OTHER));
65       
66     /* (2) */
67     
68     _text =
69       "boolean method() {\n" +
70       "}";
71     
72     _setDocText(_text);
73     
74     assertTrue("START has no brace.", !_rule.applyRule(_doc, 18, Indenter.OTHER));
75     assertTrue("START's brace is curly brace.", _rule.applyRule(_doc, 19, Indenter.OTHER));
76     
77     /* (3) */
78     
79     _text =
80       "boolean method(\n" +
81       " int[] a, String b)\n" +
82       "{}";
83     
84     _setDocText(_text);
85     
86     assertTrue("START is open curly brace.", !_rule.applyRule(_doc, _text.length() - 2, Indenter.OTHER));
87     assertTrue("START is open curly brace.", !_rule.applyRule(_doc, _text.length() - 1, Indenter.OTHER));
88     
89     /* (4) */
90     
91     _text =
92       "if (<cond>) {\n" +
93       " if (\n" +
94       " <cond>) { ... }}";
95     
96     _setDocText(_text);
97     
98     assertTrue("START's brace is open curly brace.", _rule.applyRule(_doc, 14, Indenter.OTHER));
99     assertTrue("START's brace is open curly brace.", _rule.applyRule(_doc, 22, Indenter.OTHER));
100     assertTrue("START's brace is an open paren.", !_rule.applyRule(_doc, 23, Indenter.OTHER));
101     
102     /* (5) */
103     
104     _text =
105       "array[\n" +
106       " new Listener() {\n" +
107       " method() {\n" +
108       " }\n" +
109       " }]";
110     
111     _setDocText(_text);
112     
113     assertTrue("START has no brace.", !_rule.applyRule(_doc, 0, Indenter.OTHER));
114     assertTrue("START's brace is open bracket.", !_rule.applyRule(_doc, 7, Indenter.OTHER));
115     assertTrue("START's brace is an open curly brace.", _rule.applyRule(_doc, 28, Indenter.OTHER));
116     assertTrue("START's brace is an open curly brace.", _rule.applyRule(_doc, 30, Indenter.OTHER));
117     assertTrue("START's brace is an open curly brace.", _rule.applyRule(_doc, _text.length() - 1, Indenter.OTHER));
118   }
119   
120   public void testOnlyCurly() throws BadLocationException JavaDoc {
121     /* (1) */
122     
123     _text =
124       "{ /* block1 */ }\n" +
125       "{ /* block2 */ }\n" +
126       "{ /* block3 */ }";
127     
128     _setDocText(_text);
129     
130     assertTrue("START has no brace.", !_rule.applyRule(_doc, 0, Indenter.OTHER));
131     assertTrue("START has no brace.", !_rule.applyRule(_doc, 7, Indenter.OTHER));
132     assertTrue("START has no brace.", !_rule.applyRule(_doc, 28, Indenter.OTHER));
133     assertTrue("START has no brace.", !_rule.applyRule(_doc, 30, Indenter.OTHER));
134     assertTrue("START has no brace.", !_rule.applyRule(_doc, _text.length() - 1, Indenter.OTHER));
135     
136     /* (2) */
137     
138     _text =
139       "{\n" +
140       " {\n" +
141       " {}\n" +
142       " }\n" +
143       "}";
144     
145     _setDocText(_text);
146     
147     assertTrue("START has no brace.", !_rule.applyRule(_doc, 0, Indenter.OTHER));
148     assertTrue("START's brace is an open curly brace.", _rule.applyRule(_doc, 7, Indenter.OTHER));
149     assertTrue("START's brace is an open curly brace.", _rule.applyRule(_doc, 18, Indenter.OTHER));
150     assertTrue("START's brace is an open curly brace.", _rule.applyRule(_doc, 19, Indenter.OTHER));
151     assertTrue("START's brace is an open curly brace.", _rule.applyRule(_doc, _text.length() - 1, Indenter.OTHER));
152     
153     /* (3) */
154     
155     _text =
156       "class Foo {\n" +
157       "}";
158     _setDocText(_text);
159     
160     assertTrue("Close brace immediately after open brace.", _rule.applyRule(_doc, 12, Indenter.OTHER));
161     
162     /* (4) */
163     
164     _text =
165       "class Foo {\n" +
166       " method m()\n" +
167       " {\n" +
168       " }\n" +
169       "}";
170     _setDocText(_text);
171     
172     assertTrue("Close brace immediately after open brace.", _rule.applyRule(_doc, 29, Indenter.OTHER));
173   }
174   
175    public void testEmbeddedBraceForms() throws BadLocationException JavaDoc {
176     /* (1) */
177     
178     _text =
179       "Foo f1 = x,\n" +
180       " f2 = new int[]{1, 2, 3},\n" +
181       " f3 = y;";
182      
183     _setDocText(_text);
184     
185     assertTrue("START has no brace.", !_rule.applyRule(_doc, 0, Indenter.OTHER));
186     assertTrue("START has no brace.", !_rule.applyRule(_doc, 12, Indenter.OTHER));
187     assertTrue("START has no brace.", !_rule.applyRule(_doc, 22, Indenter.OTHER));
188 // assertTrue("START has brace.", _rule.applyRule(_doc, 32, Indenter.OTHER));
189
// assertTrue("START has brace.", _rule.applyRule(_doc, 40, Indenter.OTHER));
190
assertTrue("START has no brace.", !_rule.applyRule(_doc, _text.length() - 1, Indenter.OTHER));
191     
192     /* (2) */
193     
194   }
195 }
196
197
Popular Tags