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.
043: * Tests the question determining whether or not the closest non-whitespace character
044: * previous to the start of the current line (excluding any characters
045: * inside comments or strings) is an open brace.
046: *
047: * @version $Id: QuestionStartAfterOpenBraceTest.java 4255 2007-08-28 19:17:37Z mgricken $
048: */
049: public final class QuestionStartAfterOpenBraceTest extends
050: IndentRulesTestCase {
051: private String _text;
052:
053: private IndentRuleQuestion _rule = new QuestionStartAfterOpenBrace(
054: null, null);
055:
056: public void testNoBrace() throws BadLocationException {
057: _text = "method(\nint[] a, String b) {}";
058: _setDocText(_text);
059: assertTrue("START has no preceding brace.", !_rule.applyRule(
060: _doc, 0, Indenter.IndentReason.OTHER));
061: assertTrue(
062: "START immediately follows an open paren, not a brace.",
063: !_rule.applyRule(_doc, 8, Indenter.IndentReason.OTHER));
064: assertTrue(
065: "START immediately follows an open paren, not a brace.",
066: !_rule.applyRule(_doc, _text.length() - 1,
067: Indenter.IndentReason.OTHER));
068: }
069:
070: public void testRightAfterBrace() throws BadLocationException {
071:
072: _text = "boolean method() {\n" + "}";
073:
074: _setDocText(_text);
075: assertTrue("START immediately follows an open brace.", _rule
076: .applyRule(_doc, 19, Indenter.IndentReason.OTHER));
077:
078: _text = "boolean method(\n" + " int[] a, String b)\n"
079: + "{\n" + "}";
080:
081: _setDocText(_text);
082: assertTrue("START immediately follows an open paren.", !_rule
083: .applyRule(_doc, 40, Indenter.IndentReason.OTHER));
084: assertTrue("START immediately follows an open brace.", _rule
085: .applyRule(_doc, 41, Indenter.IndentReason.OTHER));
086:
087: }
088:
089: public void testWSAfterBrace() throws BadLocationException {
090:
091: _text = "if (<cond>) {\n" + "\n" + " if (\n"
092: + " <cond>) { ... }}";
093:
094: _setDocText(_text);
095:
096: assertTrue("START immediatly follows an open brace.", _rule
097: .applyRule(_doc, 14, Indenter.IndentReason.OTHER));
098: assertTrue("Only WS between open brace and START.", _rule
099: .applyRule(_doc, 15, Indenter.IndentReason.OTHER));
100: assertTrue("Only WS between open brace and START.", _rule
101: .applyRule(_doc, 23, Indenter.IndentReason.OTHER));
102: assertTrue("START immediatly follows an open paren.", !_rule
103: .applyRule(_doc, 25, Indenter.IndentReason.OTHER));
104:
105: }
106:
107: public void testCommentsAfterBrace() throws BadLocationException {
108:
109: _text = "class Foo { \n" + " \n"
110: + " /* \n" + " * \n"
111: + " */ \n" + " int field; \n" + "}";
112:
113: _setDocText(_text);
114:
115: assertTrue("START = DOCSTART.", !_rule.applyRule(_doc, 0,
116: Indenter.IndentReason.OTHER));
117: assertTrue("START = DOCSTART.", !_rule.applyRule(_doc, 14,
118: Indenter.IndentReason.OTHER));
119: assertTrue("Only WS between START and open brace.", _rule
120: .applyRule(_doc, 15, Indenter.IndentReason.OTHER));
121: assertTrue("Only WS between START and open brace.", _rule
122: .applyRule(_doc, 30, Indenter.IndentReason.OTHER));
123: assertTrue("Only WS between START and open brace.", _rule
124: .applyRule(_doc, 44, Indenter.IndentReason.OTHER));
125: assertTrue("Only comment and WS between START and open brace.",
126: _rule.applyRule(_doc, 45, Indenter.IndentReason.OTHER));
127: assertTrue("Only comment and WS between START and open brace.",
128: _rule.applyRule(_doc, 60, Indenter.IndentReason.OTHER));
129: assertTrue("Only comment and WS between START and open brace.",
130: _rule.applyRule(_doc, 77, Indenter.IndentReason.OTHER));
131: }
132:
133: public void testBraceLastCharOnLine() throws BadLocationException {
134: _setDocText("{\n");
135: assertTrue("Brace only char on line.", _rule.applyRule(_doc, 2,
136: Indenter.IndentReason.OTHER));
137:
138: _setDocText("void foo() {\n");
139: assertTrue("Brace last char on line.", _rule.applyRule(_doc,
140: 13, Indenter.IndentReason.OTHER));
141: }
142:
143: public void testTextAfterBrace() throws BadLocationException {
144: _setDocText("{ hello\n foo();");
145: assertTrue("Text on line after brace.", _rule.applyRule(_doc,
146: 8, Indenter.IndentReason.OTHER));
147: }
148: }
|