001: /*BEGIN_COPYRIGHT_BLOCK
002: *
003: * Copyright (c) 2001-2007, JavaPLT group at Rice University (javaplt@rice.edu)
004: * All rights reserved.
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions are met:
008: * * Redistributions of source code must retain the above copyright
009: * notice, this list of conditions and the following disclaimer.
010: * * Redistributions in binary form must reproduce the above copyright
011: * notice, this list of conditions and the following disclaimer in the
012: * documentation and/or other materials provided with the distribution.
013: * * Neither the names of DrJava, the JavaPLT group, Rice University, nor the
014: * names of its contributors may be used to endorse or promote products
015: * derived from this software without specific prior written permission.
016: *
017: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
018: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
019: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
020: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
021: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
022: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
023: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
024: * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
025: * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
026: * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
027: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
028: *
029: * This software is Open Source Initiative approved Open Source Software.
030: * Open Source Initative Approved is a trademark of the Open Source Initiative.
031: *
032: * This file is part of DrJava. Download the current version of this project
033: * from http://www.drjava.org/ or http://sourceforge.net/projects/drjava/
034: *
035: * END_COPYRIGHT_BLOCK*/
036:
037: package edu.rice.cs.drjava.model.definitions.indent;
038:
039: import javax.swing.text.BadLocationException;
040:
041: /**
042: * Test class according to the JUnit protocol. Tests the proper functionality
043: * of the class QuestionCurrLineStartsWithSkipComments.
044: * @version $Id: QuestionCurrLineStartsWithSkipCommentsTest.java 4255 2007-08-28 19:17:37Z mgricken $
045: */
046: public final class QuestionCurrLineStartsWithSkipCommentsTest extends
047: IndentRulesTestCase {
048: private String _text;
049:
050: private IndentRuleQuestion _rule;
051:
052: public void testNoPrefix() throws BadLocationException {
053: _text = "class A \n" + /* 0 */
054: "{ \n" + /* 25 */
055: " // one line comment \n" + /* 50 */
056: " int method1 \n" + /* 75 */
057: " /** \n" + /* 100 */
058: " * javadoc comment \n" + /* 125 */
059: " */ \n" + /* 150 */
060: " int method() \n" + /* 175 */
061: " { \n" + /* 200 */
062: " } \n" + /* 225 */
063: " /* multi line \n" + /* 250 */
064: " comment \n" + /* 275 */
065: " boolean method() \n" + /* 300 */
066: " { \n" + /* 325 */
067: " } \n" + /* 350 */
068: " */ \n" + /* 375 */
069: "}"; /* 400 */
070:
071: _setDocText(_text);
072:
073: IndentRuleQuestion rule = new QuestionCurrLineStartsWithSkipComments(
074: "", null, null);
075:
076: // This rule should always apply, unless the entire line is inside a comment.
077:
078: assertTrue("At DOCSTART.", rule.applyRule(_doc, 0,
079: Indenter.IndentReason.OTHER));
080: assertTrue("At start of block.", rule.applyRule(_doc, 25,
081: Indenter.IndentReason.OTHER));
082: assertTrue("START starts one-line comment.", rule.applyRule(
083: _doc, 54, Indenter.IndentReason.OTHER));
084: assertTrue("START starts one-line comment.", rule.applyRule(
085: _doc, 60, Indenter.IndentReason.OTHER));
086: assertTrue("START starts javadoc comment.", rule.applyRule(
087: _doc, 104, Indenter.IndentReason.OTHER));
088: assertTrue("START starts javadoc comment.", rule.applyRule(
089: _doc, 110, Indenter.IndentReason.OTHER));
090: assertTrue("Line inside javadoc comment.", !rule.applyRule(
091: _doc, 130, Indenter.IndentReason.OTHER));
092: assertTrue("Line closes javadoc comment.", rule.applyRule(_doc,
093: 150, Indenter.IndentReason.OTHER));
094: assertTrue("START is free.", rule.applyRule(_doc, 180,
095: Indenter.IndentReason.OTHER));
096: assertTrue("START is free.", rule.applyRule(_doc, 230,
097: Indenter.IndentReason.OTHER));
098: assertTrue("START starts multi-line comment.", rule.applyRule(
099: _doc, 260, Indenter.IndentReason.OTHER));
100: assertTrue("Line inside multi-line comment.", !rule.applyRule(
101: _doc, 275, Indenter.IndentReason.OTHER));
102: assertTrue("Line inside multi-line comment.", !rule.applyRule(
103: _doc, 300, Indenter.IndentReason.OTHER));
104: assertTrue("Line closes multi-line comment.", rule.applyRule(
105: _doc, 399, Indenter.IndentReason.OTHER));
106: assertTrue("START is free.", rule.applyRule(_doc, 400,
107: Indenter.IndentReason.OTHER));
108: assertTrue("At end of document.", rule.applyRule(_doc, 401,
109: Indenter.IndentReason.OTHER));
110: }
111:
112: public void testOpenBracePrefix() throws BadLocationException {
113: _text = "class A extends \n" + /* 0 */
114: "B { \n" + /* 25 */
115: " // { } \n" + /* 50 */
116: " int field; \n" + /* 75 */
117: " /** \n" + /* 100 */
118: " * { } \n" + /* 125 */
119: " */ \n" + /* 150 */
120: " int method() /* \n" + /* 175 */
121: " */ { \n" + /* 200 */
122: " } \n" + /* 225 */
123: " /* multi line \n" + /* 250 */
124: " comment \n" + /* 275 */
125: " boolean method() \n" + /* 300 */
126: "/**stuff*/ { // stuff\n" + /* 325 */
127: " } \n" + /* 350 */
128: " \n" + /* 375 */
129: "}"; /* 400 */
130:
131: _setDocText(_text);
132:
133: _rule = new QuestionCurrLineStartsWithSkipComments("{", null,
134: null);
135:
136: assertTrue(
137: "At DOCSTART - line doesn't start with an open brace.",
138: !_rule.applyRule(_doc, 0, Indenter.IndentReason.OTHER));
139: assertTrue(
140: "Line starts a block, but not the start of the line.",
141: !_rule.applyRule(_doc, 25, Indenter.IndentReason.OTHER));
142: assertTrue(
143: "Inside block - line starts with an alphanumeric character.",
144: !_rule.applyRule(_doc, 30, Indenter.IndentReason.OTHER));
145: assertTrue("Line starts a one-line comment.", !_rule.applyRule(
146: _doc, 54, Indenter.IndentReason.OTHER));
147: assertTrue("Line starts a one-line comment.", !_rule.applyRule(
148: _doc, 60, Indenter.IndentReason.OTHER));
149: assertTrue("Line starts with alphanumeric character.", !_rule
150: .applyRule(_doc, 80, Indenter.IndentReason.OTHER));
151: assertTrue("Line starts a javadoc comment.", !_rule.applyRule(
152: _doc, 104, Indenter.IndentReason.OTHER));
153: assertTrue("Line starts a javadoc comment.", !_rule.applyRule(
154: _doc, 110, Indenter.IndentReason.OTHER));
155: assertTrue("Line inside javadoc comment.", !_rule.applyRule(
156: _doc, 130, Indenter.IndentReason.OTHER));
157: assertTrue("Line starts with alphanumeric character.", !_rule
158: .applyRule(_doc, 180, Indenter.IndentReason.OTHER));
159: assertTrue("Line closes comment. It follows an open brace.",
160: _rule.applyRule(_doc, 201, Indenter.IndentReason.OTHER));
161: assertTrue("Line closes comment. It follows an open brace.",
162: _rule.applyRule(_doc, 221, Indenter.IndentReason.OTHER));
163: assertTrue("At end of block - line starts with a close brace.",
164: !_rule
165: .applyRule(_doc, 225,
166: Indenter.IndentReason.OTHER));
167: assertTrue("Line starts a multi-line comment.", !_rule
168: .applyRule(_doc, 260, Indenter.IndentReason.OTHER));
169: assertTrue("Line inside multi-line comment.", !_rule.applyRule(
170: _doc, 275, Indenter.IndentReason.OTHER));
171: assertTrue("Line inside multi-line comment.", !_rule.applyRule(
172: _doc, 300, Indenter.IndentReason.OTHER));
173: assertTrue("Line closes comment. It follows an open brace.",
174: _rule.applyRule(_doc, 325, Indenter.IndentReason.OTHER));
175: assertTrue("Line starts with a close brace.", !_rule.applyRule(
176: _doc, 355, Indenter.IndentReason.OTHER));
177: assertTrue("Empty line.", !_rule.applyRule(_doc, 390,
178: Indenter.IndentReason.OTHER));
179: assertTrue(
180: "At last character - line starts with a close brace.",
181: !_rule
182: .applyRule(_doc, 400,
183: Indenter.IndentReason.OTHER));
184: assertTrue(
185: "At end of document - line starts with a close brace.",
186: !_rule
187: .applyRule(_doc, 401,
188: Indenter.IndentReason.OTHER));
189: }
190:
191: public void testCloseBracePrefix() throws BadLocationException {
192: _text = "class A \n" + /* 0 */
193: "{ \n" + /* 25 */
194: " // } } \n" + /* 50 */
195: " int field; \n" + /* 75 */
196: " /** \n" + /* 100 */
197: " * javadoc comment \n" + /* 125 */
198: " */ } \n" + /* 150 */
199: " int method() \n" + /* 175 */
200: "/**/} \n" + /* 200 */
201: "/ * } \n" + /* 225 */
202: " /* multi line \n" + /* 250 */
203: " comment \n" + /* 275 */
204: " boolean method() \n" + /* 300 */
205: " { \n" + /* 325 */
206: "*/ / } \n" + /* 350 */
207: " * } \n" + /* 375 */
208: "}"; /* 400 */
209:
210: _setDocText(_text);
211:
212: _rule = new QuestionCurrLineStartsWithSkipComments("}", null,
213: null);
214:
215: assertTrue(
216: "At DOCSTART - line doesn't start with a close brace.",
217: !_rule.applyRule(_doc, 0, Indenter.IndentReason.OTHER));
218: assertTrue(
219: "At start of block - line starts with an open brace.",
220: !_rule.applyRule(_doc, 25, Indenter.IndentReason.OTHER));
221: assertTrue("Inside block - line starts with an open brace.",
222: !_rule.applyRule(_doc, 30, Indenter.IndentReason.OTHER));
223: assertTrue("Line starts a one-line comment.", !_rule.applyRule(
224: _doc, 54, Indenter.IndentReason.OTHER));
225: assertTrue("Line starts a one-line comment.", !_rule.applyRule(
226: _doc, 60, Indenter.IndentReason.OTHER));
227: assertTrue("Line starts with alphanumeric character.", !_rule
228: .applyRule(_doc, 80, Indenter.IndentReason.OTHER));
229: assertTrue("Line starts a javadoc comment.", !_rule.applyRule(
230: _doc, 104, Indenter.IndentReason.OTHER));
231: assertTrue("Line starts a javadoc comment.", !_rule.applyRule(
232: _doc, 110, Indenter.IndentReason.OTHER));
233: assertTrue("Line inside javadoc comment.", !_rule.applyRule(
234: _doc, 130, Indenter.IndentReason.OTHER));
235: assertTrue(
236: "Line closes multi-line comment, it follows a close brace.",
237: _rule.applyRule(_doc, 150, Indenter.IndentReason.OTHER));
238: assertTrue("Line starts with alphanumeric character.", !_rule
239: .applyRule(_doc, 180, Indenter.IndentReason.OTHER));
240: assertTrue(
241: "Line starts with a comment, it follows a close brace.",
242: _rule.applyRule(_doc, 221, Indenter.IndentReason.OTHER));
243: assertTrue("At end of block - line starts with a slash.",
244: !_rule
245: .applyRule(_doc, 225,
246: Indenter.IndentReason.OTHER));
247: assertTrue("Line starts a multi-line comment.", !_rule
248: .applyRule(_doc, 260, Indenter.IndentReason.OTHER));
249: assertTrue("Line inside multi-line comment.", !_rule.applyRule(
250: _doc, 275, Indenter.IndentReason.OTHER));
251: assertTrue("Line inside multi-line comment.", !_rule.applyRule(
252: _doc, 300, Indenter.IndentReason.OTHER));
253: assertTrue("Line inside multi-line comment.", !_rule.applyRule(
254: _doc, 325, Indenter.IndentReason.OTHER));
255: assertTrue(
256: "Line closes multi-line comment, it follows a slash.",
257: !_rule
258: .applyRule(_doc, 355,
259: Indenter.IndentReason.OTHER));
260: assertTrue("Line starts with a star.", !_rule.applyRule(_doc,
261: 376, Indenter.IndentReason.OTHER));
262: assertTrue(
263: "At last character - line starts with a close brace.",
264: _rule.applyRule(_doc, 400, Indenter.IndentReason.OTHER));
265: assertTrue(
266: "At end of document - line starts with a close brace.",
267: _rule.applyRule(_doc, 401, Indenter.IndentReason.OTHER));
268: }
269: }
|