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 current line
043: * in the document contains the given character.
044: * <p>
045: * All tests check for the ':' character on the current line.
046: *
047: * @version $Id: QuestionLineContainsTest.java 4255 2007-08-28 19:17:37Z mgricken $
048: */
049: public final class QuestionLineContainsTest extends IndentRulesTestCase {
050:
051: /**
052: * Ensures that a line containing a colon is detected.
053: * Tests that a line of text containing a colon is detected.
054: */
055: public void testLineContainsColon() throws BadLocationException {
056: IndentRuleQuestion rule = new QuestionLineContains(':', null,
057: null);
058:
059: // Colon in text
060: _setDocText("return test ? x : y;\n}\n");
061: _doc.setCurrentLocation(0);
062: assertTrue("colon in text (after startdoc)", rule.applyRule(
063: _doc, Indenter.IndentReason.OTHER));
064: _setDocText("foo();\nreturn test ? x : y;\n}\n");
065: _doc.setCurrentLocation(10);
066: assertTrue("colon in text (after newline)", rule.applyRule(
067: _doc, Indenter.IndentReason.OTHER));
068: _doc.setCurrentLocation(25);
069: assertTrue("colon in text (after colon on line)", rule
070: .applyRule(_doc, Indenter.IndentReason.OTHER));
071: }
072:
073: /**
074: * Ensures that a line containing a colon is detected.
075: * Tests that a line does not contain a colon.
076: */
077: public void testLineDoesNotContainColon()
078: throws BadLocationException {
079: IndentRuleQuestion rule = new QuestionLineContains(':', null,
080: null);
081:
082: // No colon in text
083: _setDocText("foo();\nreturn test ? x : y;\n}\n");
084: _doc.setCurrentLocation(6);
085: assertTrue("no colon", !rule.applyRule(_doc,
086: Indenter.IndentReason.OTHER));
087: _doc.setCurrentLocation(28);
088: assertTrue("line of close brace (no colon in text)", !rule
089: .applyRule(_doc, Indenter.IndentReason.OTHER));
090: }
091:
092: /**
093: * Ensures that a line containing a colon is detected.
094: * Tests that a line containing a commented out colon is identified as a
095: * line that does not contain a colon.
096: */
097: public void testLineDoesNotContainColonDueToComments()
098: throws BadLocationException {
099: IndentRuleQuestion rule = new QuestionLineContains(':', null,
100: null);
101:
102: // No colon, single line comment
103: _setDocText("//case 1:\nreturn test; //? x : y\n}\n");
104: _doc.setCurrentLocation(0);
105: assertTrue(
106: "entire line with colon in comment (no colon, single line comment)",
107: !rule.applyRule(_doc, Indenter.IndentReason.OTHER));
108: _doc.setCurrentLocation(10);
109: assertTrue(
110: "part of line with colon in comment (no colon, single line comment)",
111: !rule.applyRule(_doc, Indenter.IndentReason.OTHER));
112:
113: // No colon, multi-line comment
114: _setDocText("foo();\nreturn test; /*? x : y*/\n}\n");
115: _doc.setCurrentLocation(7);
116: assertTrue("no colon, colon in multi-line comment", !rule
117: .applyRule(_doc, Indenter.IndentReason.OTHER));
118: }
119:
120: /**
121: * Ensures that a line containing a colon is detected.
122: * Tests that a line containing a colon in quotes is identified as a
123: * line that does not contain a colon.
124: */
125: public void testLineDoesNotContainColonDueToQuotes()
126: throws BadLocationException {
127: IndentRuleQuestion rule = new QuestionLineContains(':', null,
128: null);
129:
130: // No colon, quotes
131: _setDocText("foo();\nreturn \"total: \" + sum\n}\n");
132: _doc.setCurrentLocation(7);
133: assertTrue("no colon, colon in quotes", !rule.applyRule(_doc,
134: Indenter.IndentReason.OTHER));
135: }
136: }
|