KickJava   Java API By Example, From Geeks To Geeks.

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


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

43 public final class QuestionPrevLineStartsWithTest extends IndentRulesTestCase {
44
45   /** Tests not having the prefix in the text. */
46   public void testNoPrefix() throws BadLocationException JavaDoc {
47     IndentRuleQuestion rule = new QuestionPrevLineStartsWith("{", null, null);
48     
49     // Open brace
50
_setDocText("}\nfoo();\n}\n");
51     assertTrue("line after close brace (no open brace)", !rule.applyRule(_doc, 2, Indenter.OTHER));
52     assertTrue("line after text (no open brace)", !rule.applyRule(_doc, 9, Indenter.OTHER));
53     assertTrue("line after text (no open brace)", !rule.applyRule(_doc, 10, Indenter.OTHER));
54     
55     // Star
56
rule = new QuestionPrevLineStartsWith("*", null, null);
57     _setDocText("{\nfoo();");
58     assertTrue("no star", !rule.applyRule(_doc, 6, Indenter.OTHER));
59     
60   }
61   
62   /** Tests hitting start of document. */
63   public void testStartOfDocument() throws BadLocationException JavaDoc {
64     IndentRuleQuestion rule = new QuestionPrevLineStartsWith("{", null, null);
65     
66     // Hits docstart
67
_setDocText("\nfoo();");
68     assertTrue("first line", !rule.applyRule(_doc, 0, Indenter.OTHER));
69     assertTrue("second line", !rule.applyRule(_doc, 2, Indenter.OTHER));
70   }
71   
72   /** Tests prefix on current line. */
73   public void testPrefixOnCurrLine() throws BadLocationException JavaDoc {
74     IndentRuleQuestion rule = new QuestionPrevLineStartsWith("}", null, null);
75     
76     // Prefix at start of current line
77
_setDocText("} foo();");
78     assertTrue("before brace", !rule.applyRule(_doc, 0, Indenter.OTHER));
79     assertTrue("after brace", !rule.applyRule(_doc, 2, Indenter.OTHER));
80     
81     // Prefix in middle of current line
82
_setDocText("foo();\n bar(); } foo();");
83     assertTrue("before brace", !rule.applyRule(_doc, 7, Indenter.OTHER));
84     assertTrue("after brace", !rule.applyRule(_doc, 18, Indenter.OTHER));
85   }
86   
87   /** Tests having prev line start with prefix, with text following */
88   public void testStartsWithPrefixWithText() throws BadLocationException JavaDoc {
89     IndentRuleQuestion rule = new QuestionPrevLineStartsWith("}", null, null);
90         
91     // Prefix plus text (no space)
92
_setDocText("}bar();\nfoo();\nbar();");
93     assertTrue("line of brace (no space)", !rule.applyRule(_doc, 2, Indenter.OTHER));
94     assertTrue("line after brace (no space)", rule.applyRule(_doc, 8, Indenter.OTHER));
95     assertTrue("two lines after brace (no space)", !rule.applyRule(_doc, 16, Indenter.OTHER));
96     
97     // Prefix plus text (with space)
98
rule = new QuestionPrevLineStartsWith("*", null, null);
99     _setDocText("foo\n * comment\nbar");
100     assertTrue("just before star (with space)", !rule.applyRule(_doc, 4, Indenter.OTHER));
101     assertTrue("just after star (with space)", !rule.applyRule(_doc, 6, Indenter.OTHER));
102     assertTrue("line after star (with space)", rule.applyRule(_doc, 16, Indenter.OTHER));
103   }
104   
105   /** Tests having prev line start with prefix, with no text following */
106   public void testStartsWithPrefixNoText() throws BadLocationException JavaDoc {
107     IndentRuleQuestion rule = new QuestionPrevLineStartsWith("*", null, null);
108     
109     // Prefix plus no text (no space)
110
_setDocText("foo();\n*\nbar();\n}");
111     assertTrue("line of star (no space)", !rule.applyRule(_doc, 8, Indenter.OTHER));
112     assertTrue("line after star (no space)", rule.applyRule(_doc, 10, Indenter.OTHER));
113     assertTrue("two lines after star (no space)", !rule.applyRule(_doc, 16, Indenter.OTHER));
114     
115     // Prefix plus no text (with space)
116
_setDocText("foo();\n * \nbar();\n{");
117     assertTrue("line of star (with space)", !rule.applyRule(_doc, 7, Indenter.OTHER));
118     assertTrue("just after star (with space)", !rule.applyRule(_doc, 11, Indenter.OTHER));
119     assertTrue("line after star (with space)", rule.applyRule(_doc, 13, Indenter.OTHER));
120   }
121   
122   /** Tests having a multiple character prefix. */
123   public void testMultipleCharPrefix() throws BadLocationException JavaDoc {
124     IndentRuleQuestion rule = new QuestionPrevLineStartsWith("* ", null, null);
125     
126     // Multi-char prefix
127
_setDocText("*\n *\n * \n * foo\nbar");
128     assertTrue("star", !rule.applyRule(_doc, 2, Indenter.OTHER));
129     assertTrue("space star", !rule.applyRule(_doc, 5, Indenter.OTHER));
130     assertTrue("space star space", rule.applyRule(_doc, 11, Indenter.OTHER));
131     assertTrue("space star space text", rule.applyRule(_doc, 16, Indenter.OTHER));
132   }
133   
134   /** Tests having a commented prefix. */
135   public void testCommentedPrefix() throws BadLocationException JavaDoc {
136     IndentRuleQuestion rule = new QuestionPrevLineStartsWith("*", null, null);
137     
138     // Star in comment
139
_setDocText("/**\n* \ncomment\n*/");
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     assertTrue("line after star", !rule.applyRule(_doc, 15, Indenter.OTHER));
144   }
145   
146   /** Tests a prefix that begins a comment. */
147   public void testCommentPrefix() throws BadLocationException JavaDoc {
148     IndentRuleQuestion rule = new QuestionPrevLineStartsWith("/**", null, null);
149     
150     // Star in comment
151
_setDocText("/**\n* \ncomment\n*/");
152     assertTrue("just before star", rule.applyRule(_doc, 4, Indenter.OTHER));
153     assertTrue("just after star", rule.applyRule(_doc, 6, Indenter.OTHER));
154     assertTrue("line after star", !rule.applyRule(_doc, 7, Indenter.OTHER));
155     assertTrue("line after star", !rule.applyRule(_doc, 15, Indenter.OTHER));
156   }
157   
158   /** Tests having text on a line before the prefix. */
159   public void testDoesNotStartWithPrefix() throws BadLocationException JavaDoc {
160     IndentRuleQuestion rule = new QuestionPrevLineStartsWith("*", null, null);
161     
162     // Star in text, not starting line
163
_setDocText("foo(); *\nbar();\n");
164     assertTrue("line after star", !rule.applyRule(_doc, 10, Indenter.OTHER));
165   }
166
167
168 }
169
Popular Tags