KickJava   Java API By Example, From Geeks To Geeks.

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


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 given findChar
40  * is found between the start of the statement and the endChar,
41  * which must exist on the current line.
42  * <p>
43  * This is done in the context of determining if a colon that
44  * was found on the current line is part of a ternary operator.
45  * Hence, we use endChar=':' and findChar='?'.
46  *
47  * @version $Id: QuestionExistsCharInStmtTest.java 3901 2006-06-30 05:28:11Z rcartwright $
48  */

49 public final class QuestionExistsCharInStmtTest extends IndentRulesTestCase {
50   
51   /**
52    * Ensures that a colon that is part of a ternary operator is detected.
53    * Tests that this rule works for one line statements.
54    */

55   public void testColonInTernaryOpOneLineStmts() throws BadLocationException JavaDoc {
56     IndentRuleQuestion rule = new QuestionExistsCharInStmt('?', ':', null, null);
57    
58     // Colon not in ternary op, one line stmt, no '?'
59
_setDocText("case 1: foo()\ncase default: break;\n");
60     _doc.setCurrentLocation(0);
61     assertTrue("colon not in ternary op, one line stmt, no '?'",
62         !rule.applyRule(_doc, Indenter.OTHER));
63     _doc.setCurrentLocation(16);
64     assertTrue("after newline (colon not in ternary op, one line stmt, no '?')",
65         !rule.applyRule(_doc, Indenter.OTHER));
66
67     // Colon in ternary op, one line stmt
68
_setDocText("foo();\nreturn (test ? x : y;)\n");
69     _doc.setCurrentLocation(10);
70     assertTrue("colon in ternary op, same line",
71         rule.applyRule(_doc, Indenter.OTHER));
72   }
73
74   /**
75    * Ensures that a colon that is part of a ternary operator is detected.
76    * Tests that this rule works when there are two statements on the same line.
77    * Essentially, that it uses the first colon that it finds on the line
78    * as the endChar.
79    */

80   public void testColonInTernaryOpTwoStmtsOnOneLine() throws BadLocationException JavaDoc {
81     IndentRuleQuestion rule = new QuestionExistsCharInStmt('?', ':', null, null);
82
83     // Colon in ternary op, two stmts on one line
84
_setDocText("foo();\nreturn (test ? x : y); case default: break;\n");
85     _doc.setCurrentLocation(7);
86     assertTrue("colon in ternary op, two stmts on one line",
87         rule.applyRule(_doc, Indenter.OTHER));
88     
89     _setDocText("foo();\ncase default: break; return test ? x : y;\n");
90     // Colon not in ternary op, two stmts on one line
91
_doc.setCurrentLocation(7);
92     assertTrue("colon not in ternary op, two stmts on one line",
93         !rule.applyRule(_doc, Indenter.OTHER));
94   }
95
96   /**
97    * Ensures that a colon that is part of a ternary operator is detected.
98    * Tests that a colon in a multi-line ternary op statement is detected.
99    */

100   public void testColonInTernaryOpMultiLineStmts() throws BadLocationException JavaDoc {
101     IndentRuleQuestion rule = new QuestionExistsCharInStmt('?', ':', null, null);
102
103     // Colon in ternary op, multi-line stmt
104
_setDocText("foo();\nreturn test ?\nx : y;\n");
105     _doc.setCurrentLocation(22);
106     assertTrue("colon in ternary op, multi-line stmt",
107         rule.applyRule(_doc, Indenter.OTHER));
108   }
109
110   /**
111    * Ensures that a colon that is part of a ternary operator is detected.
112    * Tests that whitespace, single-line comments and multi-line comments
113    * in between the ':' character and the '?' character are ignored.
114    */

115   public void testColonInTernaryOpIgnoreWhitespaceAndComments() throws BadLocationException JavaDoc {
116     IndentRuleQuestion rule = new QuestionExistsCharInStmt('?', ':', null, null);
117
118     // Colon in ternary op, ignores whitespace
119
_setDocText("foo;\nreturn test ?\n \n \t \nx : y;\n");
120     _doc.setCurrentLocation(28);
121     assertTrue("colon in ternary op, multi-line stmt, ignores whitespace",
122         rule.applyRule(_doc, Indenter.OTHER));
123
124     // Colon in ternary op, ignores single line comments
125
_setDocText("foo();\nreturn test ? //{\n//case 1: bar();\nx() : y();\n");
126     _doc.setCurrentLocation(42);
127     assertTrue("colon in ternary op, ignores single line comments",
128         rule.applyRule(_doc, Indenter.OTHER));
129
130     // Colon in ternary op, ignores multi-line comments
131
_setDocText("foo();\nreturn test ? /* {\ncase 1 : bar();*/\nx() : y();\n");
132     _doc.setCurrentLocation(44);
133     assertTrue("colon in ternary op, ignores multi-line comments",
134         rule.applyRule(_doc, Indenter.OTHER));
135   }
136
137   /**
138    * Ensures that a colon that is part of a ternary operator is detected.
139    * Tests that a '?' in quotes or single-line comments or multi-line
140    * comments is not detected - and hence that a colon is not party of
141    * a ternary op.
142    */

143   public void testColonNotInTernaryOpDueToQuestionMarkInCommentsOrQuotes() throws BadLocationException JavaDoc {
144     IndentRuleQuestion rule = new QuestionExistsCharInStmt('?', ':', null, null);
145
146     // Colon not in ternary op, ignores '?' in single-line comments
147
_setDocText("foo();\nreturn test; //?\ncase default: break;\n");
148     _doc.setCurrentLocation(38);
149     assertTrue("colon not in ternary op, ignores '?' in single-line comments",
150         !rule.applyRule(_doc, Indenter.OTHER));
151
152     // Colon not in ternary op, ignores '?' in multi-line comments
153
_setDocText("foo();\nreturn test; /* huh? okay */\ncase default: break;\n");
154     _doc.setCurrentLocation(36);
155     assertTrue("colon not in ternary op, ignores '?' in multi-line comments",
156         !rule.applyRule(_doc, Indenter.OTHER));
157
158     // Colon not in quotes, ignores '?' in quotes
159
_setDocText("foo();\nreturn str + \"?\";\ncase default: break;\n");
160     _doc.setCurrentLocation(25);
161     assertTrue("colon not in ternary op, ignores '?' in quotes",
162         !rule.applyRule(_doc, Indenter.OTHER));
163
164   }
165
166   /**
167    * Ensures that a colon that is part of a ternary operator is detected.
168    * Tests that a colon that is part of a multi-line statement is
169    * not falsely identified as belonging to a ternary op.
170    */

171   public void testColonNotInTernaryOpMultiLineStmts() throws BadLocationException JavaDoc {
172     IndentRuleQuestion rule = new QuestionExistsCharInStmt('?', ':', null, null);
173
174     // Colon not in ternary op, multi-line stmt
175
_setDocText("return test ? x : y;\ncase 1\n: foo();\n");
176     _doc.setCurrentLocation(28);
177     assertTrue("colon not in ternary op, multi-line stmt",
178         !rule.applyRule(_doc, Indenter.OTHER));
179
180     // Colon not in ternary op, multi-line stmt,
181
// same line as end of ternary op
182
_setDocText("foo()\nreturn test ? x :\ny; case default: break;\n");
183     _doc.setCurrentLocation(24);
184     assertTrue("colon not in ternary op, multi-line stmt, same line as end of ternary op",
185         !rule.applyRule(_doc, Indenter.OTHER));
186   }
187 }
188
189
190
Popular Tags