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 action
043: * that aligns the indentation of the current line to the character
044: * that opened the most recent block or expression list that contains
045: * the beginning of the current line. Optional additional whitespaces
046: * can be passed through the constructor.
047: * @version $Id: ActionBracePlusTest.java 4255 2007-08-28 19:17:37Z mgricken $
048: */
049: public final class ActionBracePlusTest extends IndentRulesTestCase {
050: private String _text, _aligned;
051:
052: private IndentRuleAction _action;
053:
054: public void testNoSuffix() throws BadLocationException {
055: _action = new ActionBracePlus("");
056:
057: // (1)
058:
059: _text = "method(\n" + ")\n";
060:
061: _aligned = "method(\n" + " )\n";
062:
063: _setDocText(_text);
064: _action.indentLine(_doc, 0, Indenter.IndentReason.OTHER); // Does nothing.
065: assertEquals("START has no brace.", _text.length(), _doc
066: .getLength());
067: _action.indentLine(_doc, 7, Indenter.IndentReason.OTHER); // Does nothing.
068: assertEquals("START has no brace.", _text.length(), _doc
069: .getLength());
070: _action.indentLine(_doc, 8, Indenter.IndentReason.OTHER); // Aligns second line.
071: assertEquals("Line aligned to open paren.", _aligned.length(),
072: _doc.getLength());
073: assertEquals("Line aligned to open paren.", _aligned, _doc
074: .getText());
075: }
076:
077: public void testSpaceSuffix() throws BadLocationException {
078: _action = new ActionBracePlus(" ");
079:
080: // (2)
081:
082: _text = "var = method(arg1,\n" + " arg2, arg3) + 4;";
083:
084: _aligned = "var = method(arg1,\n"
085: + " arg2, arg3) + 4;";
086:
087: _setDocText(_text);
088: _action.indentLine(_doc, 0, Indenter.IndentReason.OTHER); // Does nothing.
089: assertEquals("START has no brace.", _text.length(), _doc
090: .getLength());
091: _action.indentLine(_doc, 18, Indenter.IndentReason.OTHER); // Does nothing.
092: assertEquals("START has no brace.", _text.length(), _doc
093: .getLength());
094: _action.indentLine(_doc, 20, Indenter.IndentReason.OTHER); // Aligns second line.
095: assertEquals("Line aligned to open paren.", _aligned.length(),
096: _doc.getLength());
097: assertEquals("Line aligned to open paren.", _aligned, _doc
098: .getText());
099:
100: // (3)
101:
102: _text = "boolean method(\n" + "int[] a, String b)\n" + "{}";
103: _aligned = "boolean method(\n"
104: + " int[] a, String b)\n" + "{}";
105:
106: _setDocText(_text);
107: _action.indentLine(_doc, 0, Indenter.IndentReason.OTHER); // Does nothing.
108: assertEquals("START has no brace.", _text.length(), _doc
109: .getLength());
110: _action.indentLine(_doc, 15, Indenter.IndentReason.OTHER); // Does nothing.
111: assertEquals("START has no brace.", _text.length(), _doc
112: .getLength());
113: _action.indentLine(_doc, 16, Indenter.IndentReason.OTHER); // Aligns second line.
114: assertEquals("Line aligned to open paren.", _aligned.length(),
115: _doc.getLength());
116: assertEquals("Line aligned to open paren.", _aligned, _doc
117: .getText());
118:
119: // (4)
120:
121: _text = "boolean method(\n" + "int[] a,\n"
122: + " String b)\n" + "{}";
123: _aligned = "boolean method(\n" + " int[] a,\n"
124: + " String b)\n" + "{}";
125:
126: _setDocText(_text);
127: _action.indentLine(_doc, 0, Indenter.IndentReason.OTHER); // Does nothing.
128: assertEquals("START has no brace.", _text.length(), _doc
129: .getLength());
130: _action.indentLine(_doc, 15, Indenter.IndentReason.OTHER); // Does nothing.
131: assertEquals("START has no brace.", _text.length(), _doc
132: .getLength());
133: _action.indentLine(_doc, 20, Indenter.IndentReason.OTHER); // Aligns second line.
134: assertEquals("Line aligned to open paren.", _aligned, _doc
135: .getText());
136:
137: // (5)
138:
139: _text = "array[\n" + " new Listener() {\n"
140: + " method() {\n" + " }\n"
141: + " }]";
142: _aligned = "array[\n" + " new Listener() {\n"
143: + " method() {\n" + " }\n"
144: + " }]";
145:
146: _setDocText(_text);
147: _action.indentLine(_doc, 0, Indenter.IndentReason.OTHER); // Does nothing.
148: assertEquals("START has no brace.", _text.length(), _doc
149: .getLength());
150: _action.indentLine(_doc, 6, Indenter.IndentReason.OTHER); // Does nothing.
151: assertEquals("START has no brace.", _text.length(), _doc
152: .getLength());
153: _action.indentLine(_doc, 10, Indenter.IndentReason.OTHER); // Aligns second line.
154: assertEquals("Line aligned to open bracket.", _aligned, _doc
155: .getText());
156:
157: }
158:
159: public void testLargeSuffix() throws BadLocationException {
160: _action = new ActionBracePlus(" " + " ");
161:
162: // (6)
163:
164: _text = "var = method(foo.\n" + " bar(), arg3) + 4;";
165:
166: _aligned = "var = method(foo.\n"
167: + " bar(), arg3) + 4;";
168:
169: _setDocText(_text);
170: _action.indentLine(_doc, 0, Indenter.IndentReason.OTHER); // Does nothing.
171: assertEquals("START has no brace.", _text.length(), _doc
172: .getLength());
173: _action.indentLine(_doc, 17, Indenter.IndentReason.OTHER); // Does nothing.
174: assertEquals("START has no brace.", _text.length(), _doc
175: .getLength());
176: _action.indentLine(_doc, 25, Indenter.IndentReason.OTHER); // Aligns second line.
177: assertEquals("Line aligned to open paren.", _aligned.length(),
178: _doc.getLength());
179: assertEquals("Line aligned to open paren.", _aligned, _doc
180: .getText());
181: }
182:
183: public void testComment() throws BadLocationException {
184: _action = new ActionBracePlus(" " + " ");
185:
186: // (7)
187:
188: _text = "foo(i,\n" + " j.\n" + "bar().\n" + "// bar();\n"
189: + "baz(),\n" + " k);";
190:
191: _aligned = "foo(i,\n" + " j.\n" + " bar().\n"
192: + " // bar();\n" + " baz(),\n" + " k);";
193:
194: _setDocText(_text);
195: _action.indentLine(_doc, 14, Indenter.IndentReason.OTHER); // line 3
196: _action.indentLine(_doc, 27, Indenter.IndentReason.OTHER); // line 4
197: _action.indentLine(_doc, 43, Indenter.IndentReason.OTHER); // line 5
198: assertEquals("Lines aligned plus one level.", _aligned, _doc
199: .getText());
200:
201: _action.indentLine(_doc, 54, Indenter.IndentReason.OTHER); // after baz()
202: assertEquals("Cursor after baz().", _aligned, _doc.getText());
203: }
204: }
|