KickJava   Java API By Example, From Geeks To Geeks.

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


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.
40  * Tests the question determining whether or not the closest non-whitespace character
41  * previous to the start of the current line (excluding any characters
42  * inside comments or strings) is an open brace.
43  *
44  * @version $Id: QuestionStartAfterOpenBraceTest.java 3901 2006-06-30 05:28:11Z rcartwright $
45  */

46 public final class QuestionStartAfterOpenBraceTest extends IndentRulesTestCase
47 {
48   private String JavaDoc _text;
49   
50   private IndentRuleQuestion _rule = new QuestionStartAfterOpenBrace(null, null);
51   
52   public void testNoBrace() throws BadLocationException JavaDoc
53   {
54     _text = "method(\nint[] a, String b) {}";
55     _setDocText(_text);
56     assertTrue("START has no preceding brace.", !_rule.applyRule(_doc, 0, Indenter.OTHER));
57     assertTrue("START immediately follows an open paren, not a brace.", !_rule.applyRule(_doc, 8, Indenter.OTHER));
58     assertTrue("START immediately follows an open paren, not a brace.", !_rule.applyRule(_doc, _text.length()-1, Indenter.OTHER));
59   }
60   
61   public void testRightAfterBrace() throws BadLocationException JavaDoc
62   {
63     
64     _text =
65       "boolean method() {\n" +
66       "}";
67     
68     _setDocText(_text);
69     assertTrue("START immediately follows an open brace.", _rule.applyRule(_doc, 19, Indenter.OTHER));
70     
71     
72     _text =
73       "boolean method(\n" +
74       " int[] a, String b)\n" +
75       "{\n" +
76       "}";
77     
78     _setDocText(_text);
79     assertTrue("START immediately follows an open paren.", !_rule.applyRule(_doc, 40, Indenter.OTHER));
80     assertTrue("START immediately follows an open brace.", _rule.applyRule(_doc, 41, Indenter.OTHER));
81     
82   }
83   
84   public void testWSAfterBrace() throws BadLocationException JavaDoc
85   {
86     
87     _text =
88       "if (<cond>) {\n" +
89       "\n" +
90       " if (\n" +
91       " <cond>) { ... }}";
92     
93     _setDocText(_text);
94     
95     assertTrue("START immediatly follows an open brace.", _rule.applyRule(_doc, 14, Indenter.OTHER));
96     assertTrue("Only WS between open brace and START.", _rule.applyRule(_doc, 15, Indenter.OTHER));
97     assertTrue("Only WS between open brace and START.", _rule.applyRule(_doc, 23, Indenter.OTHER));
98     assertTrue("START immediatly follows an open paren.", !_rule.applyRule(_doc, 25, Indenter.OTHER));
99     
100   }
101   
102   public void testCommentsAfterBrace() throws BadLocationException JavaDoc
103   {
104     
105     _text =
106       "class Foo { \n" +
107       " \n" +
108       " /* \n" +
109       " * \n" +
110       " */ \n" +
111       " int field; \n" +
112       "}";
113     
114     _setDocText(_text);
115     
116     assertTrue("START = DOCSTART.", !_rule.applyRule(_doc, 0, Indenter.OTHER));
117     assertTrue("START = DOCSTART.", !_rule.applyRule(_doc, 14, Indenter.OTHER));
118     assertTrue("Only WS between START and open brace.", _rule.applyRule(_doc, 15, Indenter.OTHER));
119     assertTrue("Only WS between START and open brace.", _rule.applyRule(_doc, 30, Indenter.OTHER));
120     assertTrue("Only WS between START and open brace.", _rule.applyRule(_doc, 44, Indenter.OTHER));
121     assertTrue("Only comment and WS between START and open brace.", _rule.applyRule(_doc, 45, Indenter.OTHER));
122     assertTrue("Only comment and WS between START and open brace.", _rule.applyRule(_doc, 60, Indenter.OTHER));
123     assertTrue("Only comment and WS between START and open brace.", _rule.applyRule(_doc, 77, Indenter.OTHER));
124   }
125   
126   public void testBraceLastCharOnLine() throws BadLocationException JavaDoc {
127     _setDocText("{\n");
128     assertTrue("Brace only char on line.", _rule.applyRule(_doc, 2, Indenter.OTHER));
129     
130     _setDocText("void foo() {\n");
131     assertTrue("Brace last char on line.", _rule.applyRule(_doc, 13, Indenter.OTHER));
132   }
133   
134   public void testTextAfterBrace() throws BadLocationException JavaDoc {
135     _setDocText("{ hello\n foo();");
136     assertTrue("Text on line after brace.", _rule.applyRule(_doc, 8, Indenter.OTHER));
137   }
138 }
139
140
Popular Tags