KickJava   Java API By Example, From Geeks To Geeks.

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


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 indention rule which detects whether the current line starts
40  * a new parenthesized phrase. (ie. Previous line ends in comma, semicolon,
41  * open paren, or open bracket.)
42  * @version $Id: QuestionNewParenPhraseTest.java 3901 2006-06-30 05:28:11Z rcartwright $
43  */

44 public final class QuestionNewParenPhraseTest extends IndentRulesTestCase {
45
46   /**
47    * Tests hitting start of document.
48    */

49   public void testStartOfDocument() throws BadLocationException JavaDoc {
50     IndentRuleQuestion rule = new QuestionNewParenPhrase(null, null);
51     
52     // Hits docstart
53
_setDocText("\nfoo();");
54     assertTrue("first line", !rule.applyRule(_doc, 0, Indenter.OTHER));
55     assertTrue("second line", !rule.applyRule(_doc, 2, Indenter.OTHER));
56   }
57   
58   /**
59    * Tests having no paren phrase delimiters on prev line.
60    */

61   public void testNoParenDelims() throws BadLocationException JavaDoc {
62     IndentRuleQuestion rule = new QuestionNewParenPhrase(null, null);
63     
64     // No paren delimiters
65
_setDocText("foo\nbar.\ny");
66     assertTrue("second line", !rule.applyRule(_doc, 4, Indenter.OTHER));
67     assertTrue("third line", !rule.applyRule(_doc, 9, Indenter.OTHER));
68   }
69   
70   /**
71    * Tests having delimiter on prev line, with text preceding
72    */

73   public void testParenDelimsWithText() throws BadLocationException JavaDoc {
74     IndentRuleQuestion rule = new QuestionNewParenPhrase(null, null);
75         
76     // Lines ending in delimiter, each with preceding text
77
_setDocText("new Foo(\nx,\ny;\na[\nbar])\n{");
78     assertTrue("line after paren", rule.applyRule(_doc, 9, Indenter.OTHER));
79     assertTrue("line after comma", rule.applyRule(_doc, 12, Indenter.OTHER));
80     assertTrue("line after semicolon", rule.applyRule(_doc, 15, Indenter.OTHER));
81     assertTrue("line after bracket", rule.applyRule(_doc, 18, Indenter.OTHER));
82     assertTrue("line after close paren", !rule.applyRule(_doc, 24, Indenter.OTHER));
83   }
84   
85   /**
86    * Tests having delimiter on prev line, with no text preceding
87    */

88   public void testParenDelimsNoText() throws BadLocationException JavaDoc {
89     IndentRuleQuestion rule = new QuestionNewParenPhrase(null, null);
90     
91     // Paren delims with no leading text
92
_setDocText("(\n,\n;\n[\nfoo\nbar");
93     assertTrue("line after paren", rule.applyRule(_doc, 2, Indenter.OTHER));
94     assertTrue("line after comma", rule.applyRule(_doc, 4, Indenter.OTHER));
95     assertTrue("line after semicolon", rule.applyRule(_doc, 6, Indenter.OTHER));
96     assertTrue("line after bracket", rule.applyRule(_doc, 8, Indenter.OTHER));
97     assertTrue("line after text", !rule.applyRule(_doc, 12, Indenter.OTHER));
98   }
99   
100   /**
101    * Tests having a comment after the delimiter
102    */

103   public void testParenDelimsWithComment() throws BadLocationException JavaDoc {
104     IndentRuleQuestion rule = new QuestionNewParenPhrase(null, null);
105     
106     // Delim in text, with comment before
107
_setDocText("for (int i; // comment\ni < 2; /** comment */\ni++) {");
108     assertTrue("// comment", rule.applyRule(_doc, 23, Indenter.OTHER));
109     assertTrue("/* */ comment", rule.applyRule(_doc, 45, Indenter.OTHER));
110   }
111   
112   /**
113    * Tests having a paren delimiter several lines back, with only
114    * whitespace inbetween.
115    */

116   public void testMultipleBlankLinesBack() throws BadLocationException JavaDoc {
117     IndentRuleQuestion rule = new QuestionNewParenPhrase(null, null);
118     
119     // Blank lines between
120
_setDocText("for(\n\nint i;\n\n\ni > 0;;\n)");
121     assertTrue("line after open paren", rule.applyRule(_doc, 5, Indenter.OTHER));
122     assertTrue("two lines after open paren", rule.applyRule(_doc, 6, Indenter.OTHER));
123     assertTrue("line after semicolon", rule.applyRule(_doc, 13, Indenter.OTHER));
124     assertTrue("two lines after semicolon", rule.applyRule(_doc, 16, Indenter.OTHER));
125   }
126   
127   /**
128    * Tests having a paren delimiter several lines back, with only
129    * blank space and comments inbetween.
130    */

131   public void testMultipleCommentLinesBack() throws BadLocationException JavaDoc {
132     IndentRuleQuestion rule = new QuestionNewParenPhrase(null, null);
133     
134     // Comments between
135
_setDocText("for(\n//\n/** foo * /int i;\n\n// bar\ni > 0;;\n)");
136     assertTrue("line after open paren", rule.applyRule(_doc, 7, Indenter.OTHER));
137     assertTrue("two lines after open paren", rule.applyRule(_doc, 18, Indenter.OTHER));
138     assertTrue("line after semicolon", rule.applyRule(_doc, 25, Indenter.OTHER));
139     assertTrue("two lines after semicolon", rule.applyRule(_doc, 28, Indenter.OTHER));
140   }
141   
142   /**
143    * Tests having text on a line after the delimiter.
144    */

145   public void testDoesNotEndWithParenDelim() throws BadLocationException JavaDoc {
146     IndentRuleQuestion rule = new QuestionNewParenPhrase(null, null);
147     
148     // Delim in text, not ending line
149
_setDocText("foo(bar.\nx,y\n)");
150     assertTrue("line after paren", !rule.applyRule(_doc, 9, Indenter.OTHER));
151     assertTrue("line after comma", !rule.applyRule(_doc, 13, Indenter.OTHER));
152   }
153   
154   /**
155    * Tests having an operator as a delimiter.
156    */

157   public void testOperatorDelim() throws BadLocationException JavaDoc {
158     IndentRuleQuestion rule = new QuestionNewParenPhrase(null, null);
159     
160     // Delim in text, not ending line
161
_setDocText("foo(x +\ny\n)");
162     assertTrue("line after operator", rule.applyRule(_doc, 8, Indenter.OTHER));
163     assertTrue("line after comma", !rule.applyRule(_doc, 10, Indenter.OTHER));
164   }
165   
166   /**
167    * Tests ignoring delims on line.
168    */

169   public void testIgnoreDelimsOnLine() throws BadLocationException JavaDoc {
170     IndentRuleQuestion rule = new QuestionNewParenPhrase(null, null);
171     
172     // Delim in text, not ending line
173
_setDocText("foo(x.\ny()\n)");
174     assertTrue("after paren, but not new phrase", !rule.applyRule(_doc, 10, Indenter.OTHER));
175   }
176
177 }
178
Popular Tags