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: * Tests the question rule which determines if the given findChar
043: * is found between the start of the statement and the endChar,
044: * which must exist on the current line.
045: * <p>
046: * This is done in the context of determining if a colon that
047: * was found on the current line is part of a ternary operator.
048: * Hence, we use endChar=':' and findChar='?'.
049: *
050: * @version $Id: QuestionExistsCharInStmtTest.java 4255 2007-08-28 19:17:37Z mgricken $
051: */
052: public final class QuestionExistsCharInStmtTest extends
053: IndentRulesTestCase {
054:
055: /**
056: * Ensures that a colon that is part of a ternary operator is detected.
057: * Tests that this rule works for one line statements.
058: */
059: public void testColonInTernaryOpOneLineStmts()
060: throws BadLocationException {
061: IndentRuleQuestion rule = new QuestionExistsCharInStmt('?',
062: ':', null, null);
063:
064: // Colon not in ternary op, one line stmt, no '?'
065: _setDocText("case 1: foo()\ncase default: break;\n");
066: _doc.setCurrentLocation(0);
067: assertTrue("colon not in ternary op, one line stmt, no '?'",
068: !rule.applyRule(_doc, Indenter.IndentReason.OTHER));
069: _doc.setCurrentLocation(16);
070: assertTrue(
071: "after newline (colon not in ternary op, one line stmt, no '?')",
072: !rule.applyRule(_doc, Indenter.IndentReason.OTHER));
073:
074: // Colon in ternary op, one line stmt
075: _setDocText("foo();\nreturn (test ? x : y;)\n");
076: _doc.setCurrentLocation(10);
077: assertTrue("colon in ternary op, same line", rule.applyRule(
078: _doc, Indenter.IndentReason.OTHER));
079: }
080:
081: /**
082: * Ensures that a colon that is part of a ternary operator is detected.
083: * Tests that this rule works when there are two statements on the same line.
084: * Essentially, that it uses the first colon that it finds on the line
085: * as the endChar.
086: */
087: public void testColonInTernaryOpTwoStmtsOnOneLine()
088: throws BadLocationException {
089: IndentRuleQuestion rule = new QuestionExistsCharInStmt('?',
090: ':', null, null);
091:
092: // Colon in ternary op, two stmts on one line
093: _setDocText("foo();\nreturn (test ? x : y); case default: break;\n");
094: _doc.setCurrentLocation(7);
095: assertTrue("colon in ternary op, two stmts on one line", rule
096: .applyRule(_doc, Indenter.IndentReason.OTHER));
097:
098: _setDocText("foo();\ncase default: break; return test ? x : y;\n");
099: // Colon not in ternary op, two stmts on one line
100: _doc.setCurrentLocation(7);
101: assertTrue("colon not in ternary op, two stmts on one line",
102: !rule.applyRule(_doc, Indenter.IndentReason.OTHER));
103: }
104:
105: /**
106: * Ensures that a colon that is part of a ternary operator is detected.
107: * Tests that a colon in a multi-line ternary op statement is detected.
108: */
109: public void testColonInTernaryOpMultiLineStmts()
110: throws BadLocationException {
111: IndentRuleQuestion rule = new QuestionExistsCharInStmt('?',
112: ':', null, null);
113:
114: // Colon in ternary op, multi-line stmt
115: _setDocText("foo();\nreturn test ?\nx : y;\n");
116: _doc.setCurrentLocation(22);
117: assertTrue("colon in ternary op, multi-line stmt", rule
118: .applyRule(_doc, Indenter.IndentReason.OTHER));
119: }
120:
121: /**
122: * Ensures that a colon that is part of a ternary operator is detected.
123: * Tests that whitespace, single-line comments and multi-line comments
124: * in between the ':' character and the '?' character are ignored.
125: */
126: public void testColonInTernaryOpIgnoreWhitespaceAndComments()
127: throws BadLocationException {
128: IndentRuleQuestion rule = new QuestionExistsCharInStmt('?',
129: ':', null, null);
130:
131: // Colon in ternary op, ignores whitespace
132: _setDocText("foo;\nreturn test ?\n \n \t \nx : y;\n");
133: _doc.setCurrentLocation(28);
134: assertTrue(
135: "colon in ternary op, multi-line stmt, ignores whitespace",
136: rule.applyRule(_doc, Indenter.IndentReason.OTHER));
137:
138: // Colon in ternary op, ignores single line comments
139: _setDocText("foo();\nreturn test ? //{\n//case 1: bar();\nx() : y();\n");
140: _doc.setCurrentLocation(42);
141: assertTrue("colon in ternary op, ignores single line comments",
142: rule.applyRule(_doc, Indenter.IndentReason.OTHER));
143:
144: // Colon in ternary op, ignores multi-line comments
145: _setDocText("foo();\nreturn test ? /* {\ncase 1 : bar();*/\nx() : y();\n");
146: _doc.setCurrentLocation(44);
147: assertTrue("colon in ternary op, ignores multi-line comments",
148: rule.applyRule(_doc, Indenter.IndentReason.OTHER));
149: }
150:
151: /**
152: * Ensures that a colon that is part of a ternary operator is detected.
153: * Tests that a '?' in quotes or single-line comments or multi-line
154: * comments is not detected - and hence that a colon is not party of
155: * a ternary op.
156: */
157: public void testColonNotInTernaryOpDueToQuestionMarkInCommentsOrQuotes()
158: throws BadLocationException {
159: IndentRuleQuestion rule = new QuestionExistsCharInStmt('?',
160: ':', null, null);
161:
162: // Colon not in ternary op, ignores '?' in single-line comments
163: _setDocText("foo();\nreturn test; //?\ncase default: break;\n");
164: _doc.setCurrentLocation(38);
165: assertTrue(
166: "colon not in ternary op, ignores '?' in single-line comments",
167: !rule.applyRule(_doc, Indenter.IndentReason.OTHER));
168:
169: // Colon not in ternary op, ignores '?' in multi-line comments
170: _setDocText("foo();\nreturn test; /* huh? okay */\ncase default: break;\n");
171: _doc.setCurrentLocation(36);
172: assertTrue(
173: "colon not in ternary op, ignores '?' in multi-line comments",
174: !rule.applyRule(_doc, Indenter.IndentReason.OTHER));
175:
176: // Colon not in quotes, ignores '?' in quotes
177: _setDocText("foo();\nreturn str + \"?\";\ncase default: break;\n");
178: _doc.setCurrentLocation(25);
179: assertTrue("colon not in ternary op, ignores '?' in quotes",
180: !rule.applyRule(_doc, Indenter.IndentReason.OTHER));
181:
182: }
183:
184: /**
185: * Ensures that a colon that is part of a ternary operator is detected.
186: * Tests that a colon that is part of a multi-line statement is
187: * not falsely identified as belonging to a ternary op.
188: */
189: public void testColonNotInTernaryOpMultiLineStmts()
190: throws BadLocationException {
191: IndentRuleQuestion rule = new QuestionExistsCharInStmt('?',
192: ':', null, null);
193:
194: // Colon not in ternary op, multi-line stmt
195: _setDocText("return test ? x : y;\ncase 1\n: foo();\n");
196: _doc.setCurrentLocation(28);
197: assertTrue("colon not in ternary op, multi-line stmt", !rule
198: .applyRule(_doc, Indenter.IndentReason.OTHER));
199:
200: // Colon not in ternary op, multi-line stmt,
201: // same line as end of ternary op
202: _setDocText("foo()\nreturn test ? x :\ny; case default: break;\n");
203: _doc.setCurrentLocation(24);
204: assertTrue(
205: "colon not in ternary op, multi-line stmt, same line as end of ternary op",
206: !rule.applyRule(_doc, Indenter.IndentReason.OTHER));
207: }
208: }
|