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 the action rules for code in the indentation decision tree.
043: * The implementation relies heavily on functions which are fully
044: * tested in IndentHelpersTest.
045: *
046: * @version $Id: ActionStartPrevStmtPlusTest.java 4255 2007-08-28 19:17:37Z mgricken $
047: */
048: public final class ActionStartPrevStmtPlusTest extends
049: IndentRulesTestCase {
050:
051: public void testNoPrevStmt() throws BadLocationException {
052: IndentRuleAction rule1 = new ActionStartPrevStmtPlus("", true);
053: IndentRuleAction rule2 = new ActionStartPrevStmtPlus(" ", true);
054:
055: _setDocText("foo();\n");
056: rule1.indentLine(_doc, 2, Indenter.IndentReason.OTHER);
057: assertEquals("no prev stmt, no suffix", "foo();\n", _doc
058: .getText());
059:
060: _setDocText("foo();\n");
061: rule2.indentLine(_doc, 2, Indenter.IndentReason.OTHER);
062: assertEquals("no prev stmt, suffix two spaces", " foo();\n",
063: _doc.getText());
064: }
065:
066: public void testPrevStmtPrevLine() throws BadLocationException {
067: IndentRuleAction rule1 = new ActionStartPrevStmtPlus("", true);
068: IndentRuleAction rule2 = new ActionStartPrevStmtPlus(" ", true);
069:
070: _setDocText(" foo().\n//boo();\n/*y=x+1;\nfoo(){}*/\nbar();\nbiz();\n");
071: rule1.indentLine(_doc, 44, Indenter.IndentReason.OTHER);
072: assertEquals(
073: "prev stmt on prev line, no suffix",
074: " foo().\n//boo();\n/*y=x+1;\nfoo(){}*/\nbar();\n biz();\n",
075: _doc.getText());
076:
077: _setDocText(" foo().\n//boo();\n/*y=x+1;\nfoo(){}*/\nbar();\nbiz();\n");
078: rule2.indentLine(_doc, 44, Indenter.IndentReason.OTHER);
079: assertEquals(
080: "prev stmt on prev line, suffix two spaces",
081: " foo().\n//boo();\n/*y=x+1;\nfoo(){}*/\nbar();\n biz();\n",
082: _doc.getText());
083: }
084:
085: public void testPrevStmtSeveralLinesBeforeCurrLocation()
086: throws BadLocationException {
087: IndentRuleAction rule1 = new ActionStartPrevStmtPlus("", true);
088: IndentRuleAction rule2 = new ActionStartPrevStmtPlus(" ", true);
089:
090: _setDocText(" foo();\n//y=x+1;\n/*void blah {\n}*/\n ';' + blah.\n//foo\nx;\n");
091: rule1.indentLine(_doc, 56, Indenter.IndentReason.OTHER);
092: assertEquals(
093: "prev stmt serveral lines before, no suffix",
094: " foo();\n//y=x+1;\n/*void blah {\n}*/\n ';' + blah.\n//foo\n x;\n",
095: _doc.getText());
096:
097: _setDocText(" foo();\n//y=x+1;\n/*void blah {\n}*/\n ';' + blah.\n//foo\nx;\n");
098: rule2.indentLine(_doc, 56, Indenter.IndentReason.OTHER);
099: assertEquals(
100: "prev stmt serveral lines before, suffix two spaces",
101: " foo();\n//y=x+1;\n/*void blah {\n}*/\n ';' + blah.\n//foo\n x;\n",
102: _doc.getText());
103: }
104:
105: public void testColonNotDelim() throws BadLocationException {
106: IndentRuleAction rule = new ActionStartPrevStmtPlus("", false);
107:
108: _setDocText("test2 = x ? y :\n" + // ? and : on one line
109: " z;\n" + // unfinished ternary
110: "foo();\n"); // new stmt
111: rule.indentLine(_doc, 21, Indenter.IndentReason.OTHER);
112: assertEquals("Colon is not a delimiter", "test2 = x ? y :\n" + // ? and : on one line
113: " z;\n" + // unfinished ternary
114: "foo();\n", _doc.getText());
115: }
116:
117: public void testAfterArrayAssign() throws BadLocationException {
118: IndentRuleAction rule = new ActionStartPrevStmtPlus("", false);
119:
120: _setDocText("a = {\n" + " b,c,d\n" + "};\n" + " a;"); // new stmt
121: //rule.indentLine(_doc, 8);
122: rule.indentLine(_doc, 17, Indenter.IndentReason.OTHER);
123: assertEquals("After array assignment", "a = {\n" + " b,c,d\n"
124: + "};\n" + "a;", _doc.getText());
125: }
126:
127: public void testAfterArrayAssignMultiSemi()
128: throws BadLocationException {
129: IndentRuleAction rule = new ActionStartPrevStmtPlus("", false);
130:
131: _setDocText("a = {\n" + " b,c,d\n" + "};;;\n" + " a;"); // new stmt
132: //rule.indentLine(_doc, 8);
133: rule.indentLine(_doc, 19, Indenter.IndentReason.OTHER);
134: assertEquals("After array assignment multi semi colons",
135: "a = {\n" + " b,c,d\n" + "};;;\n" + "a;", _doc
136: .getText());
137: }
138:
139: /**
140: * not currently supported
141: * currently assuming single stmt per line
142: */
143: /*
144: public void testAfterArrayAssignMultiSemiAndStmt() throws BadLocationException {
145: IndentRuleAction rule = new ActionStartPrevStmtPlus("", false);
146:
147: _setDocText("a = {\n" +
148: " b,c,d\n" +
149: "};b;;\n" +
150: " a;"); // new stmt
151: //rule.indentLine(_doc, 8);
152: rule.indentLine(_doc, 20);
153: assertEquals("After array assignment multi semi colons and embedded stmt",
154: "a = {\n" +
155: " b,c,d\n" +
156: "};b;;\n" +
157: "a;",
158: _doc.getText());
159: }
160: */
161: }
|