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 QuestionHasCharPrecedingOpenBrace.
044: * @version $Id: QuestionHasCharPrecedingOpenBraceTest.java 4255 2007-08-28 19:17:37Z mgricken $
045: */
046: public final class QuestionHasCharPrecedingOpenBraceTest extends
047: IndentRulesTestCase {
048: private String _text;
049:
050: // private IndentRuleQuestion _rule;
051:
052: public void testIsIn1DArray() throws BadLocationException { //01234567890123456789012345
053: _text = "int[2][] a = \n" + /* 0 */
054: "{ \n" + /* 25 */
055: " a, // line comment \n" + /* 50 */
056: " int b, \n" + /* 75 */
057: " /** \n" + /* 100 */
058: " * javadoc comment \n" + /* 125 */
059: " */ \n" + /* 150 */
060: " START \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: char[] chars = { '=' };
074: IndentRuleQuestion rule = new QuestionHasCharPrecedingOpenBrace(
075: chars, null, null);
076:
077: assertTrue("At DOCSTART.", !rule.applyRule(_doc, 0,
078: Indenter.IndentReason.OTHER));
079: assertTrue("At identifier.", !rule.applyRule(_doc, 10,
080: Indenter.IndentReason.OTHER));
081: assertTrue("At start of array.", !rule.applyRule(_doc, 25,
082: Indenter.IndentReason.OTHER));
083: assertTrue("START starts one-line comment.", rule.applyRule(
084: _doc, 54, Indenter.IndentReason.OTHER));
085: assertTrue("START starts one-line comment.", rule.applyRule(
086: _doc, 60, Indenter.IndentReason.OTHER));
087: assertTrue("START starts javadoc comment.", rule.applyRule(
088: _doc, 104, Indenter.IndentReason.OTHER));
089: assertTrue("START starts javadoc comment.", rule.applyRule(
090: _doc, 110, Indenter.IndentReason.OTHER));
091: assertTrue("Line inside javadoc comment.", rule.applyRule(_doc,
092: 130, Indenter.IndentReason.OTHER));
093: assertTrue("Line closes javadoc comment.", rule.applyRule(_doc,
094: 150, Indenter.IndentReason.OTHER));
095: assertTrue("START is stil in first.", rule.applyRule(_doc, 180,
096: Indenter.IndentReason.OTHER));
097: assertTrue("Second pseudo array element.", !rule.applyRule(
098: _doc, 230, Indenter.IndentReason.OTHER));
099: assertTrue("Start of multi-line comment.", !rule.applyRule(
100: _doc, 260, Indenter.IndentReason.OTHER));
101: assertTrue("Line inside multi-line comment.", !rule.applyRule(
102: _doc, 275, Indenter.IndentReason.OTHER));
103: assertTrue("Line inside multi-line comment.", !rule.applyRule(
104: _doc, 300, Indenter.IndentReason.OTHER));
105: assertTrue("Line closes multi-line comment.", !rule.applyRule(
106: _doc, 399, Indenter.IndentReason.OTHER));
107: assertTrue("Last close brace", !rule.applyRule(_doc, 400,
108: Indenter.IndentReason.OTHER));
109: assertTrue("At end of document.", !rule.applyRule(_doc, 401,
110: Indenter.IndentReason.OTHER));
111: }
112:
113: public void testIsIn2DArray() throws BadLocationException { //01234567890123456789012345
114: _text = "int[2][] a = \n" + /* 0 */
115: "{ \n" + /* 25 */
116: " { \n" + /* 50 */
117: " a, // line comment \n" + /* 75 */
118: " int b, \n" + /* 100 */
119: " /** \n" + /* 125 */
120: " */ \n" + /* 150 */
121: " START \n" + /* 175 */
122: " }, \n" + /* 200 */
123: " { \n" + /* 225 */
124: " /* = { multi line \n" + /* 250 */
125: " comment } \n" + /* 275 */
126: " boolean method() \n" + /* 300 */
127: " { \n" + /* 325 */
128: " } \n" + /* 350 */
129: " */} \n" + /* 375 */
130: "}" + /* 400 */
131: "";
132:
133: _setDocText(_text);
134:
135: char[] chars = { '=' };
136: IndentRuleQuestion rule = new QuestionHasCharPrecedingOpenBrace(
137: chars, null, null);
138:
139: assertTrue("At DOCSTART.", !rule.applyRule(_doc, 0,
140: Indenter.IndentReason.OTHER));
141: assertTrue("At identifier.", !rule.applyRule(_doc, 10,
142: Indenter.IndentReason.OTHER));
143: assertTrue("At start of outer array", !rule.applyRule(_doc, 25,
144: Indenter.IndentReason.OTHER));
145:
146: assertTrue("Before start of inner array", rule.applyRule(_doc,
147: 50, Indenter.IndentReason.OTHER));
148:
149: assertTrue("Same line as inner {.", rule.applyRule(_doc, 54,
150: Indenter.IndentReason.OTHER));
151: assertTrue("Line after inner {.", !rule.applyRule(_doc, 75,
152: Indenter.IndentReason.OTHER));
153: assertTrue("START is stil in first.", !rule.applyRule(_doc,
154: 180, Indenter.IndentReason.OTHER));
155:
156: assertTrue("Second pseudo array element.", rule.applyRule(_doc,
157: 230, Indenter.IndentReason.OTHER));
158: assertTrue("In multi-line comment.", !rule.applyRule(_doc, 260,
159: Indenter.IndentReason.OTHER));
160:
161: assertTrue("multi-line comment w/ = {.", !rule.applyRule(_doc,
162: 275, Indenter.IndentReason.OTHER));
163:
164: assertTrue("Line inside multi-line comment.", !rule.applyRule(
165: _doc, 300, Indenter.IndentReason.OTHER));
166: assertTrue("Line closes multi-line comment.", !rule.applyRule(
167: _doc, 399, Indenter.IndentReason.OTHER));
168:
169: assertTrue("Last close brace", rule.applyRule(_doc, 400,
170: Indenter.IndentReason.OTHER));
171: assertTrue("At end of document.", rule.applyRule(_doc, 401,
172: Indenter.IndentReason.OTHER));
173: }
174:
175: public void testNoEquals() throws BadLocationException { //01234567890123456789012345
176: _text = "int[2][] a \n" + /* 0 */
177: "{ \n" + /* 25 */
178: " { \n" + /* 50 */
179: " a, // line comment \n" + /* 75 */
180: " int b, \n" + /* 100 */
181: " /** \n" + /* 125 */
182: " */ \n" + /* 150 */
183: " START \n" + /* 175 */
184: " }, \n" + /* 200 */
185: " { \n" + /* 225 */
186: " /* = { multi line \n" + /* 250 */
187: " comment } \n" + /* 275 */
188: " boolean method() \n" + /* 300 */
189: " { \n" + /* 325 */
190: " } \n" + /* 350 */
191: " */} \n" + /* 375 */
192: "}" + /* 400 */
193: "";
194:
195: _setDocText(_text);
196:
197: char[] chars = { '=' };
198: IndentRuleQuestion rule = new QuestionHasCharPrecedingOpenBrace(
199: chars, null, null);
200:
201: assertTrue("At DOCSTART.", !rule.applyRule(_doc, 0,
202: Indenter.IndentReason.OTHER));
203: assertTrue("At identifier.", !rule.applyRule(_doc, 10,
204: Indenter.IndentReason.OTHER));
205: assertTrue("At start of outer array", !rule.applyRule(_doc, 25,
206: Indenter.IndentReason.OTHER));
207:
208: assertTrue("Before start of inner array", !rule.applyRule(_doc,
209: 50, Indenter.IndentReason.OTHER));
210: assertTrue("Same line as inner {.", !rule.applyRule(_doc, 54,
211: Indenter.IndentReason.OTHER));
212: assertTrue("Line after inner {.", !rule.applyRule(_doc, 75,
213: Indenter.IndentReason.OTHER));
214: assertTrue("START is stil in first.", !rule.applyRule(_doc,
215: 180, Indenter.IndentReason.OTHER));
216:
217: }
218: }
|