KickJava   Java API By Example, From Geeks To Geeks.

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


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
40  * starts with a particular string.
41  * @version $Id: QuestionCurrLineStartsWithTest.java 3901 2006-06-30 05:28:11Z rcartwright $
42  */

43 public final class QuestionCurrLineStartsWithTest extends IndentRulesTestCase {
44
45   /** Tests not having the prefix in the text. */
46   public void testNoPrefix() throws BadLocationException JavaDoc {
47     IndentRuleQuestion rule = new QuestionCurrLineStartsWith("{", null, null);
48     
49     // Open brace
50
_setDocText("foo();\n}\n");
51     assertTrue("no open brace", !rule.applyRule(_doc, 0, Indenter.OTHER));
52     assertTrue("line of close brace (no open brace)", !rule.applyRule(_doc, 7, Indenter.OTHER));
53     assertTrue("line after close brace (no open brace)", !rule.applyRule(_doc, 8, Indenter.OTHER));
54     
55     // Close brace
56
rule = new QuestionCurrLineStartsWith("}", null, null);
57     _setDocText("{\nfoo();");
58     assertTrue("no close brace", !rule.applyRule(_doc, 0, Indenter.OTHER));
59   }
60   
61   /**
62    * Tests having a line start with prefix, with text following
63    */

64   public void testStartsWithPrefixWithText() throws BadLocationException JavaDoc {
65     IndentRuleQuestion rule = new QuestionCurrLineStartsWith("}", null, null);
66         
67     // Prefix plus text (no space)
68
_setDocText("foo();\n}bar();\n");
69     assertTrue("line before brace (no space)", !rule.applyRule(_doc, 0, Indenter.OTHER));
70     assertTrue("just before brace (no space)", rule.applyRule(_doc, 7, Indenter.OTHER));
71     assertTrue("just after brace (no space)", rule.applyRule(_doc, 9, Indenter.OTHER));
72     assertTrue("line after brace (no space)", !rule.applyRule(_doc, 15, Indenter.OTHER));
73     
74     // Prefix plus text (with space)
75
rule = new QuestionCurrLineStartsWith("*", null, null);
76     _setDocText("foo\n * comment\nbar");
77     assertTrue("line before star (with space)", !rule.applyRule(_doc, 0, Indenter.OTHER));
78     assertTrue("just before star (with space)", rule.applyRule(_doc, 4, Indenter.OTHER));
79     assertTrue("just after star (with space)", rule.applyRule(_doc, 6, Indenter.OTHER));
80     assertTrue("line after star (with space)", !rule.applyRule(_doc, 15, Indenter.OTHER));
81   }
82   
83   /**
84    * Tests having a line start with prefix, with no text following
85    */

86   public void testStartsWithPrefixNoText() throws BadLocationException JavaDoc {
87     IndentRuleQuestion rule = new QuestionCurrLineStartsWith("{", null, null);
88     
89     // Prefix plus no text (no space)
90
_setDocText("foo();\n{\nbar();\n");
91     assertTrue("line before brace (no space)", !rule.applyRule(_doc, 0, Indenter.OTHER));
92     assertTrue("just before brace (no space)", rule.applyRule(_doc, 7, Indenter.OTHER));
93     assertTrue("just after brace (no space)", rule.applyRule(_doc, 8, Indenter.OTHER));
94     assertTrue("line after brace (no space)", !rule.applyRule(_doc, 10, Indenter.OTHER));
95     
96     // Prefix plus no text (with space)
97
_setDocText("foo();\n {\nbar();\n");
98     assertTrue("line before brace (with space)", !rule.applyRule(_doc, 0, Indenter.OTHER));
99     assertTrue("just before brace (with space)", rule.applyRule(_doc, 7, Indenter.OTHER));
100     assertTrue("just after brace (with space)", rule.applyRule(_doc, 11, Indenter.OTHER));
101     assertTrue("line after brace (with space)", !rule.applyRule(_doc, 14, Indenter.OTHER));
102   }
103   
104   /**
105    * Tests having a multiple character prefix.
106    */

107   public void testMultipleCharPrefix() throws BadLocationException JavaDoc {
108     IndentRuleQuestion rule = new QuestionCurrLineStartsWith(".*.", null, null);
109     
110     // Multi-char prefix
111
_setDocText("*\n.*\n.*.\n.*.foo");
112     assertTrue("star", !rule.applyRule(_doc, 0, Indenter.OTHER));
113     assertTrue("dot star", !rule.applyRule(_doc, 2, Indenter.OTHER));
114     assertTrue("dot star dot", rule.applyRule(_doc, 7, Indenter.OTHER));
115     assertTrue("dot star dot text", rule.applyRule(_doc, 9, Indenter.OTHER));
116   }
117     
118   /**
119    * Tests having a commented prefix without searching in comments.
120    */

121   public void testCommentedPrefixDontSearchComment() throws BadLocationException JavaDoc {
122     IndentRuleQuestion rule = new QuestionCurrLineStartsWith("{", null, null);
123     
124     // Open brace in comment
125
_setDocText("foo();\n// {\nbar();\n");
126     assertTrue("just before brace", !rule.applyRule(_doc, 7, Indenter.OTHER));
127     assertTrue("just after brace", !rule.applyRule(_doc, 11, Indenter.OTHER));
128     assertTrue("line after brace", !rule.applyRule(_doc, 12, Indenter.OTHER));
129   }
130
131   /**
132    * Tests having a commented prefix with searching in comments.
133    */

134   public void testCommentedPrefixSearchComment() throws BadLocationException JavaDoc {
135     IndentRuleQuestion rule = new QuestionCurrLineStartsWith("*", null, null);
136     
137     // Star in comment
138
_setDocText("/**\n* \ncomment\n");
139     assertTrue("line before star", !rule.applyRule(_doc, 0, Indenter.OTHER));
140     assertTrue("just before star", rule.applyRule(_doc, 4, Indenter.OTHER));
141     assertTrue("just after star", rule.applyRule(_doc, 6, Indenter.OTHER));
142     assertTrue("line after star", !rule.applyRule(_doc, 7, Indenter.OTHER));
143   }
144   
145   /**
146    * Tests having text on a line before the prefix.
147    */

148   public void testDoesNotStartWithPrefix() throws BadLocationException JavaDoc {
149     IndentRuleQuestion rule = new QuestionCurrLineStartsWith("}", null, null);
150     
151     // Close brace in text, not starting line
152
_setDocText("foo(); }\nbar();\n");
153     assertTrue("before brace", !rule.applyRule(_doc, 0, Indenter.OTHER));
154     assertTrue("just before brace", !rule.applyRule(_doc, 7, Indenter.OTHER));
155     assertTrue("just after brace", !rule.applyRule(_doc, 8, Indenter.OTHER));
156     assertTrue("line after brace", !rule.applyRule(_doc, 10, Indenter.OTHER));
157   }
158
159   /**
160    * Prefix appears at the end of a document.
161    */

162   public void testPrefixAtEnd() throws BadLocationException JavaDoc {
163     IndentRuleQuestion rule = new QuestionCurrLineStartsWith("}", null, null);
164     
165     _setDocText("void foo() {\n}");
166     assertTrue("first line", !rule.applyRule(_doc, 3, Indenter.OTHER));
167     assertTrue("end of first line", !rule.applyRule(_doc, 12, Indenter.OTHER));
168     assertTrue("beginning of second line", rule.applyRule(_doc, 13, Indenter.OTHER));
169     assertTrue("end of second line", rule.applyRule(_doc, 14, Indenter.OTHER));
170   }
171   
172   /**
173    * Tests multiple-character prefix.
174    */

175   public void testMultCharPrefix() throws BadLocationException JavaDoc {
176     IndentRuleQuestion rule = new QuestionCurrLineStartsWith("abcdefg", null, null);
177     
178     _setDocText(" abcdefghij\n abcde");
179     assertTrue("first line, beginning", rule.applyRule(_doc, 0, Indenter.OTHER));
180     assertTrue("first line, mid", rule.applyRule(_doc, 6, Indenter.OTHER));
181     assertTrue("first line, end", rule.applyRule(_doc, 13, Indenter.OTHER));
182     assertTrue("second line, beginning", !rule.applyRule(_doc, 14, Indenter.OTHER));
183     assertTrue("second line, mid", !rule.applyRule(_doc, 18, Indenter.OTHER));
184     assertTrue("second line, end", !rule.applyRule(_doc, 21, Indenter.OTHER));
185   }
186 }
Popular Tags