KickJava   Java API By Example, From Geeks To Geeks.

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


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  * Tests the question rule which determines if the current line
40  * is starting a new statement.
41  *
42  * @version $Id: QuestionStartingNewStmtTest.java 3901 2006-06-30 05:28:11Z rcartwright $
43  */

44 public final class QuestionStartingNewStmtTest extends IndentRulesTestCase {
45
46   /**
47    * Ensures that the current line is the first line of a statement.
48    * This is done by testing if the previous character is one of
49    * the following: docstart, ';', '{', '}'
50    * These characters are here-on refered to as 'end-characters'.
51    */

52   public void testStartOfStmtCheckForEndCharacters() throws BadLocationException JavaDoc {
53     IndentRuleQuestion rule = new QuestionStartingNewStmt(null, null);
54
55     // Starting new stmt, prev char docstart
56
_setDocText("import java.util.Vector;\n");
57     _doc.setCurrentLocation(4);
58     assertTrue("starting new stmt, prev char docstart",
59         rule.applyRule(_doc, Indenter.OTHER));
60
61     // Starting new stmt, prev char ';'
62
_setDocText("foo();\nbar();\n");
63     _doc.setCurrentLocation(7);
64     assertTrue("starting new stmt, prev char ';'",
65         rule.applyRule(_doc, Indenter.OTHER));
66     
67     // Starting new stmt, prev char '{'
68
_setDocText("public void foo() {\nfoo()\n");
69     _doc.setCurrentLocation(20);
70     assertTrue("starting new stmt, prev char '{'",
71         rule.applyRule(_doc, Indenter.OTHER));
72
73     // Starting new stmt, prev char '}'
74
_setDocText("x();\n}\nfoo()\n");
75     _doc.setCurrentLocation(7);
76     assertTrue("starting new stmt, prev char '}'",
77         rule.applyRule(_doc, Indenter.OTHER));
78   }
79
80   /**
81    * Ensures that the current line is the first line of a statement.
82    * Tests that whitespace, single-line and multi-line comments
83    * are ignored when searching for the end-characters.
84    */

85   public void testStartOfStmtIgnoreWhiteSpaceAndCommentsInBetween() throws BadLocationException JavaDoc {
86     IndentRuleQuestion rule = new QuestionStartingNewStmt(null, null);
87   
88     // Starting new stmt, ignore whitespace in between
89
_setDocText("bar();\n\t \n foo();");
90     _doc.setCurrentLocation(12);
91     assertTrue("starting new stmt, ignore whitespace in between",
92         rule.applyRule(_doc, Indenter.OTHER));
93
94     // Starting new stmt, ignore single line comments
95
_setDocText("} // note:\n//please ignore me\nfoo();\n");
96     _doc.setCurrentLocation(30);
97     assertTrue("starting new stmt, ignore single line comments",
98         rule.applyRule(_doc, Indenter.OTHER));
99
100     // Starting new stmt, ignore multi-line comments
101
_setDocText("{ /* in a comment\nstill in a comment\ndone */\nfoo();");
102     _doc.setCurrentLocation(45);
103     assertTrue("starting new stmt, ignore multi-line comments",
104         rule.applyRule(_doc, Indenter.OTHER));
105
106     _setDocText("bar();\n/* blah */ foo();\n");
107     _doc.setCurrentLocation(18);
108     assertTrue("starting new stmt, ignore multi-line comment on same " +
109         "line as new stmt",
110         rule.applyRule(_doc, Indenter.OTHER));
111
112     _setDocText("method foo() {\n" +
113   "}\n" +
114   " ");
115     _doc.setCurrentLocation(17);
116     assertTrue("Blank line with no non-WS after",
117         rule.applyRule(_doc, Indenter.OTHER));
118
119     _setDocText("method foo() {\n" +
120   "}\n" +
121   " \n" +
122   "// comment");
123     _doc.setCurrentLocation(17);
124     assertTrue("Blank line with comments after, but no non-WS",
125         rule.applyRule(_doc, Indenter.OTHER));
126   }
127
128   /**
129    * Ensures that the current line is the first line of a statement.
130    * Tests that end characters in single-line comments, multi-line
131    * comments or quotes are ignored.
132    */

133   public void testNotStartOfStmtDueToEndCharactersInCommentsOrQuotes() throws BadLocationException JavaDoc {
134     IndentRuleQuestion rule = new QuestionStartingNewStmt(null, null);
135
136     // Not starting new stmt, ignore end chars in quotes
137
_setDocText("x = bar + \";\" + \"}\" + \"{\"\n+ foo;\n");
138     _doc.setCurrentLocation(26);
139     assertTrue("not starting new stmt, ignore end chars in quotes",
140         !rule.applyRule(_doc, Indenter.OTHER));
141
142     // Not starting new stmt, ignore end chars in single-line comments
143
_setDocText("x = bar.//;{}\nfoo();\n");
144     _doc.setCurrentLocation(14);
145     assertTrue("not starting new stmt, ignore end chars in single-line comments",
146         !rule.applyRule(_doc, Indenter.OTHER));
147
148     // Not starting new stmt, ignore end chars in multi-line comments
149
_setDocText("x = bar./*;\n{\n}\n*/\nfoo();\n");
150     _doc.setCurrentLocation(19);
151     assertTrue("not starting new stmt, ignore end chars in multi-line comments",
152         !rule.applyRule(_doc, Indenter.OTHER));
153   }
154 }
155
Popular Tags