0001: /*BEGIN_COPYRIGHT_BLOCK
0002: *
0003: * Copyright (c) 2001-2007, JavaPLT group at Rice University (javaplt@rice.edu)
0004: * All rights reserved.
0005: *
0006: * Redistribution and use in source and binary forms, with or without
0007: * modification, are permitted provided that the following conditions are met:
0008: * * Redistributions of source code must retain the above copyright
0009: * notice, this list of conditions and the following disclaimer.
0010: * * Redistributions in binary form must reproduce the above copyright
0011: * notice, this list of conditions and the following disclaimer in the
0012: * documentation and/or other materials provided with the distribution.
0013: * * Neither the names of DrJava, the JavaPLT group, Rice University, nor the
0014: * names of its contributors may be used to endorse or promote products
0015: * derived from this software without specific prior written permission.
0016: *
0017: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
0018: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
0019: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
0020: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
0021: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
0022: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
0023: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
0024: * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
0025: * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
0026: * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
0027: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
0028: *
0029: * This software is Open Source Initiative approved Open Source Software.
0030: * Open Source Initative Approved is a trademark of the Open Source Initiative.
0031: *
0032: * This file is part of DrJava. Download the current version of this project
0033: * from http://www.drjava.org/ or http://sourceforge.net/projects/drjava/
0034: *
0035: * END_COPYRIGHT_BLOCK*/
0036:
0037: package edu.rice.cs.drjava.model.definitions;
0038:
0039: import junit.framework.*;
0040: import javax.swing.text.BadLocationException; //import java.io.File;
0041: //import java.io.FileReader;
0042: //import java.io.BufferedReader;
0043: //import java.io.IOException;
0044:
0045: import edu.rice.cs.drjava.DrJava;
0046: import edu.rice.cs.drjava.DrJavaTestCase;
0047: import edu.rice.cs.drjava.model.DJDocument;
0048: import edu.rice.cs.drjava.model.definitions.reducedmodel.*;
0049: import edu.rice.cs.drjava.config.*;
0050: import edu.rice.cs.drjava.model.definitions.indent.*;
0051: import edu.rice.cs.drjava.model.GlobalEventNotifier; //import edu.rice.cs.util.FileOps;
0052: import edu.rice.cs.util.swing.Utilities;
0053:
0054: /**
0055: * Test the tab/enter/squiggly indenting functionality.
0056: * @version $Id: IndentTest.java 4255 2007-08-28 19:17:37Z mgricken $
0057: */
0058: public final class IndentTest extends DrJavaTestCase {
0059: protected DefinitionsDocument doc;
0060:
0061: static String noBrace = IndentInfo.noBrace;
0062: static String openSquiggly = IndentInfo.openSquiggly;
0063: static String openParen = IndentInfo.openParen;
0064: static String openBracket = IndentInfo.openBracket;
0065: private Integer indentLevel = new Integer(2);
0066: private GlobalEventNotifier _notifier;
0067:
0068: /**
0069: * Tests the indent operation.
0070: * @param name {@inheritDoc}
0071: */
0072: public IndentTest(String name) {
0073: super (name);
0074: }
0075:
0076: /** Sets up the member bindings common to all tests. */
0077: public void setUp() throws Exception {
0078: super .setUp();
0079: DrJava.getConfig().resetToDefaults();
0080: _notifier = new GlobalEventNotifier();
0081: doc = new DefinitionsDocument(_notifier);
0082: DrJava.getConfig().setSetting(OptionConstants.INDENT_LEVEL,
0083: indentLevel);
0084: }
0085:
0086: /** Builds the suite of tests for Indent.class.
0087: * @return the suite.
0088: */
0089: public static Test suite() {
0090: return new TestSuite(IndentTest.class);
0091: }
0092:
0093: /** Regression test for comment portion of indent tree. */
0094: public void testIndentComments() throws BadLocationException {
0095: String text = " foo();\n" + " // foo\n" + "/**\n" + "\n"
0096: + "* Comment\n" + " * More comment\n" + "code;\n"
0097: + "* More comment\n" + "\n" + "*/\n" + "\n";
0098:
0099: String indented = " foo();\n" + // (skip this line)
0100: " // foo\n" + // align to start of statement
0101: " /**\n" + // start of statement
0102: " * \n" + // add a star after first line
0103: " * Comment\n" + // align to star
0104: " * More comment\n" + // align to star
0105: " code;\n" + // align commented code to stars
0106: " * More comment\n" + // align star after commented code
0107: " * \n" + // add a star after line with star
0108: " */\n" + // align star
0109: " \n"; // align close comment to prev statement
0110:
0111: doc.insertString(0, text, null);
0112: _assertContents(text, doc);
0113: doc.indentLines(9, doc.getLength());
0114: _assertContents(indented, doc);
0115: }
0116:
0117: /** Test case for SourceForge bug# 681203. */
0118: public void testMultiLineStarInsertFirstLine()
0119: throws BadLocationException {
0120: String text = "/**\n" + "comments here blah blah\n" + " */";
0121:
0122: String noStarAdded = "/**\n" + " comments here blah blah\n"
0123: + " */";
0124:
0125: String starAdded = "/**\n" + " * comments here blah blah\n"
0126: + " */";
0127:
0128: doc.insertString(0, text, null);
0129: _assertContents(text, doc);
0130: doc.gotoLine(2);
0131: /* First test that indentation caused not by an enter press inserts no star */
0132: doc._indentLine(Indenter.IndentReason.OTHER);
0133: _assertContents(noStarAdded, doc);
0134: /* Now test that indentation caused by an enter press does insert a star */
0135: doc._indentLine(Indenter.IndentReason.ENTER_KEY_PRESS);
0136: _assertContents(starAdded, doc);
0137: }
0138:
0139: /** Test case for SourceForge bug# 681203. */
0140: public void testMultiLineStarInsertLaterLine()
0141: throws BadLocationException {
0142:
0143: String text = "/**\n" + " * other comments\n"
0144: + "comments here blah blah\n" + " */";
0145:
0146: String noStarAdded = "/**\n" + " * other comments\n"
0147: + " comments here blah blah\n" + " */";
0148:
0149: String starAdded = "/**\n" + " * other comments\n"
0150: + " * comments here blah blah\n" + " */";
0151:
0152: doc.insertString(0, text, null);
0153: _assertContents(text, doc);
0154: doc.gotoLine(3);
0155: /* First test that indentation caused not by an enter press inserts no star */
0156: doc._indentLine(Indenter.IndentReason.OTHER);
0157: _assertContents(noStarAdded, doc);
0158: /* Now test that indentation caused by an enter press does insert a star */
0159: doc._indentLine(Indenter.IndentReason.ENTER_KEY_PRESS);
0160: _assertContents(starAdded, doc);
0161: }
0162:
0163: /**
0164: * Regression test for paren phrases.
0165: */
0166: public void testIndentParenPhrases() throws BadLocationException {
0167: String text = "foo(i,\n" + "j.\n" + "bar().\n" + "// foo();\n"
0168: + "baz(),\n" + "cond1 ||\n" + "cond2);\n"
0169: + "i = myArray[x *\n" + "y.\n" + "foo() +\n" + "z\n"
0170: + "];\n";
0171:
0172: String indented = "foo(i,\n" + " j.\n" + // new paren phrase
0173: " bar().\n" + // not new paren phrase
0174: "// foo();\n" + // not new
0175: " baz(),\n" + // not new (after comment)
0176: " cond1 ||\n" + // new
0177: " cond2);\n" + // new (after operator)
0178: "i = myArray[x *\n" + // new statement
0179: " y.\n" + // new phrase
0180: " foo() +\n" + // not new phrase
0181: " z\n" + // new phrase
0182: " ];\n"; // not new phrase (debatable)
0183:
0184: doc.insertString(0, text, null);
0185: _assertContents(text, doc);
0186: doc.indentLines(0, doc.getLength());
0187: _assertContents(indented, doc);
0188: }
0189:
0190: /**
0191: * Regression test for braces.
0192: */
0193: public void testIndentBraces() throws BadLocationException {
0194: String text = "{\n" + "class Foo\n" + "extends F {\n"
0195: + "int i; \n" + "void foo() {\n" + "if (true) {\n"
0196: + "bar();\n" + "}\n" + "}\n" + "/* comment */ }\n"
0197: + "class Bar {\n" + "/* comment\n" + "*/ }\n"
0198: + "int i;\n" + "}\n";
0199:
0200: String indented = "{\n" + " class Foo\n" + // After open brace
0201: " extends F {\n" + // Not new statement
0202: " int i; \n" + // After open brace
0203: " void foo() {\n" + // After statement
0204: " if (true) {\n" + // Nested brace
0205: " bar();\n" + // Nested brace
0206: " }\n" + // Close nested brace
0207: " }\n" + // Close nested brace
0208: " /* comment */ }\n" + // Close brace after comment
0209: " class Bar {\n" + // After close brace
0210: " /* comment\n" + // After open brace
0211: " */ }\n" + // In comment
0212: " int i;\n" + // After close brace
0213: "}\n";
0214:
0215: doc.insertString(0, text, null);
0216: _assertContents(text, doc);
0217: doc.indentLines(0, doc.getLength());
0218: _assertContents(indented, doc);
0219: }
0220:
0221: /**
0222: * Regression test for arrays.
0223: */
0224: public void testIndentArray() throws BadLocationException {
0225: String text = "int[2][] a ={\n" + "{\n" + "1,\n" + "2,\n"
0226: + "3},\n" + "{\n" + "4,\n" + "5}\n" + "};\n";
0227:
0228: String indented = "int[2][] a ={\n" + " {\n" + " 1,\n"
0229: + " 2,\n" + " 3},\n" + " {\n" + " 4,\n"
0230: + " 5}\n" + "};\n";
0231:
0232: doc.insertString(0, text, null);
0233: _assertContents(text, doc);
0234: doc.indentLines(0, doc.getLength());
0235: _assertContents(indented, doc);
0236: }
0237:
0238: /**
0239: * Regression test for common cases.
0240: */
0241: public void testIndentCommonCases() throws BadLocationException {
0242: String text = "int x;\n" + " int y;\n" + " class Foo\n"
0243: + " extends F\n" + " {\n" + " }";
0244:
0245: String indented = "int x;\n" + "int y;\n" + "class Foo\n"
0246: + " extends F\n" + "{\n" + "}";
0247:
0248: doc.insertString(0, text, null);
0249: _assertContents(text, doc);
0250: doc.indentLines(0, doc.getLength());
0251: _assertContents(indented, doc);
0252: }
0253:
0254: /**
0255: * Regression test for switch statements.
0256: */
0257: public void testIndentSwitch() throws BadLocationException {
0258: String text = "switch (x) {\n" + "case 1:\n" + "foo();\n"
0259: + "break;\n" + "case 2: case 3:\n"
0260: + "case 4: case 5:\n" + "bar();\n" + "break;\n" + "}\n";
0261:
0262: String indented = "switch (x) {\n" + " case 1:\n" + // Starting new statement after brace
0263: " foo();\n" + // Not new statement
0264: " break;\n" + // Indent to prev statement
0265: " case 2: case 3:\n" + // Case (indent to stmt of brace)
0266: " case 4: case 5:\n" + // Case (not new stmt)
0267: " bar();\n" + // Not new stmt
0268: " break;\n" + // Indent to prev stmt
0269: "}\n"; // Close brace
0270:
0271: doc.insertString(0, text, null);
0272: _assertContents(text, doc);
0273: doc.indentLines(0, doc.getLength());
0274: _assertContents(indented, doc);
0275: }
0276:
0277: /**
0278: * Regression test for ternary operators.
0279: */
0280: public void testIndentTernary() throws BadLocationException {
0281: String text = "test1 = x ? y : z;\n" + "test2 = x ? y :\n"
0282: + "z;\n" + "foo();\n" + "test3 =\n" + "x ?\n" + "y :\n"
0283: + "z;\n" + "bar();\n" + "test4 = (x ?\n" + "y :\n"
0284: + "z);\n";
0285:
0286: String indented = "test1 = x ? y : z;\n" + // ternary on one line
0287: "test2 = x ? y :\n" + // ? and : on one line
0288: " z;\n" + // unfinished ternary
0289: "foo();\n" + // new stmt
0290: "test3 =\n" + // new stmt
0291: " x ?\n" + // not new stmt
0292: " y :\n" + // : with ? in stmt
0293: " z;\n" + // in ternary op
0294: "bar();\n" + // new stmt
0295: "test4 = (x ?\n" + // ternary in paren
0296: " y :\n" + // : with ? in paren stmt
0297: " z);\n"; // in ternary in paren
0298:
0299: doc.insertString(0, text, null);
0300: _assertContents(text, doc);
0301: doc.indentLines(0, doc.getLength());
0302: _assertContents(indented, doc);
0303: }
0304:
0305: /**
0306: * put your documentation comment here
0307: * @exception BadLocationException
0308: */
0309: public void testIndentInfoSquiggly() throws BadLocationException {
0310: //empty document
0311: BraceReduction _reduced = doc.getReduced();
0312: IndentInfo ii = _reduced.getIndentInformation();
0313: _assertIndentInfo(ii, noBrace, -1, -1, -1);
0314: //single newline
0315: doc.insertString(0, "\n", null);
0316: _assertContents("\n", doc);
0317: ii = _reduced.getIndentInformation();
0318: _assertIndentInfo(ii, noBrace, -1, -1, 0);
0319: //single layer brace
0320: doc.insertString(0, "{\n\n", null);
0321: // {\n\n#\n
0322: _assertContents("{\n\n\n", doc);
0323: ii = _reduced.getIndentInformation();
0324: _assertIndentInfo(ii, openSquiggly, -1, 3, 0);
0325: //another squiggly
0326: doc.insertString(3, "{\n\n", null);
0327: // {\n\n{\n\n#\n
0328: _assertContents("{\n\n{\n\n\n", doc);
0329: ii = _reduced.getIndentInformation();
0330: _assertIndentInfo(ii, openSquiggly, 3, 3, 0);
0331: //brace with whitespace
0332: doc.insertString(6, " {\n\n", null);
0333: // {\n\n{\n\n {\n\n#\n
0334: _assertContents("{\n\n{\n\n {\n\n\n", doc);
0335: ii = _reduced.getIndentInformation();
0336: _assertIndentInfo(ii, openSquiggly, 5, 3, 0);
0337: }
0338:
0339: /**
0340: * put your documentation comment here
0341: * @exception BadLocationException
0342: */
0343: public void testIndentInfoParen() throws BadLocationException {
0344: // just paren
0345: BraceReduction _reduced = doc.getReduced();
0346: doc.insertString(0, "\n(\n", null);
0347: IndentInfo ii = _reduced.getIndentInformation();
0348: _assertIndentInfo(ii, openParen, 2, 2, 0);
0349: // paren with stuff in front
0350: doc.insertString(1, " helo ", null);
0351: doc.move(2);
0352: // \n helo (\n#
0353: _assertContents("\n helo (\n", doc);
0354: ii = _reduced.getIndentInformation();
0355: _assertIndentInfo(ii, openParen, 9, 2, 0);
0356: //single layer brace
0357: doc.move(-1);
0358: doc.insertString(9, " (", null);
0359: doc.move(1);
0360: // \n helo ( (\n#
0361: _assertContents("\n helo ( (\n", doc);
0362: ii = _reduced.getIndentInformation();
0363: _assertIndentInfo(ii, openParen, 11, 2, 0);
0364: }
0365:
0366: /**
0367: * put your documentation comment here
0368: * @exception BadLocationException
0369: */
0370: public void testIndentInfoBracket() throws BadLocationException {
0371: // just bracket
0372: BraceReduction _reduced = doc.getReduced();
0373: doc.insertString(0, "\n[\n", null);
0374: IndentInfo ii = _reduced.getIndentInformation();
0375: _assertIndentInfo(ii, openBracket, 2, 2, 0);
0376: // bracket with stuff in front
0377: doc.insertString(1, " helo ", null);
0378: doc.move(2);
0379: // \n helo (\n#
0380: _assertContents("\n helo [\n", doc);
0381: ii = _reduced.getIndentInformation();
0382: _assertIndentInfo(ii, openBracket, 9, 2, 0);
0383: //single layer brace
0384: doc.move(-1);
0385: doc.insertString(9, " [", null);
0386: doc.move(1);
0387: // \n helo ( (\n#
0388: _assertContents("\n helo [ [\n", doc);
0389: ii = _reduced.getIndentInformation();
0390: _assertIndentInfo(ii, openBracket, 11, 2, 0);
0391: }
0392:
0393: /**
0394: * put your documentation comment here
0395: * @exception BadLocationException
0396: */
0397: public void testIndentInfoPrevNewline() throws BadLocationException {
0398: BraceReduction _reduced = doc.getReduced();
0399: doc.insertString(0, "{\n {\nhello", null);
0400: // {\n {\nhello#
0401: IndentInfo ii = _reduced.getIndentInformation();
0402: _assertIndentInfo(ii, openSquiggly, 9, 7, 5);
0403: }
0404:
0405: /**
0406: * put your documentation comment here
0407: * @exception BadLocationException
0408: */
0409: public void testEndOfBlockComment() throws BadLocationException {
0410: doc.insertString(0, "\n{\n hello;\n /*\n hello\n */", null);
0411: doc.indentLines(doc.getCurrentLocation(), doc
0412: .getCurrentLocation());
0413: _assertContents("\n{\n hello;\n /*\n hello\n */", doc);
0414: }
0415:
0416: /**
0417: * put your documentation comment here
0418: * @exception BadLocationException
0419: */
0420: public void testAfterBlockComment() throws BadLocationException {
0421: doc.insertString(0,
0422: "\n{\n hello;\n /*\n hello\n */\nhello", null);
0423: doc.indentLines(doc.getCurrentLocation(), doc
0424: .getCurrentLocation());
0425: _assertContents("\n{\n hello;\n /*\n hello\n */\n hello",
0426: doc);
0427: }
0428:
0429: /**
0430: * put your documentation comment here
0431: * @exception BadLocationException
0432: */
0433: public void testAfterBlockComment3() throws BadLocationException {
0434: doc.insertString(0,
0435: "\n{\n hello;\n /*\n hello\n grr*/\nhello", null);
0436: doc.indentLines(doc.getCurrentLocation(), doc
0437: .getCurrentLocation());
0438: _assertContents(
0439: "\n{\n hello;\n /*\n hello\n grr*/\n hello", doc);
0440: }
0441:
0442: /**
0443: * put your documentation comment here
0444: * @exception BadLocationException
0445: */
0446: public void testAfterBlockComment4() throws BadLocationException {
0447: doc.insertString(0, "\n{\n hello;\n /*\n hello\n */ hello",
0448: null);
0449: doc.indentLines(doc.getCurrentLocation(), doc
0450: .getCurrentLocation());
0451: _assertContents("\n{\n hello;\n /*\n hello\n */ hello", doc);
0452: }
0453:
0454: /**
0455: * put your documentation comment here
0456: * @exception BadLocationException
0457: */
0458: public void testAfterBlockComment2() throws BadLocationException {
0459: doc.insertString(0,
0460: "\n{\n hello;\n /*\n hello\n */ (\nhello", null);
0461: doc.indentLines(doc.getCurrentLocation(), doc
0462: .getCurrentLocation());
0463: _assertContents(
0464: "\n{\n hello;\n /*\n hello\n */ (\n hello",
0465: doc);
0466: }
0467:
0468: /**
0469: * put your documentation comment here
0470: * @exception BadLocationException
0471: */
0472: public void testIndentInfoBlockComments()
0473: throws BadLocationException {
0474: BraceReduction _reduced = doc.getReduced();
0475: doc.insertString(0, "(\n /*\n*\n", null);
0476: // (\n/*\n*#\n
0477: _reduced.move(-1);
0478: IndentInfo ii = _reduced.getIndentInformation();
0479: _assertIndentInfo(ii, openParen, -1, 7, 1);
0480: }
0481:
0482: /**
0483: * put your documentation comment here
0484: * @exception BadLocationException
0485: */
0486: public void testIndentInfoBlockComments2()
0487: throws BadLocationException {
0488: BraceReduction _reduced = doc.getReduced();
0489: doc.insertString(0, "\n(\n /*\n*\n", null);
0490: // \n(\n/*\n*#\n
0491: _reduced.move(-1);
0492: IndentInfo ii = _reduced.getIndentInformation();
0493: _assertIndentInfo(ii, openParen, 7, 7, 1);
0494: }
0495:
0496: /**
0497: * put your documentation comment here
0498: * @exception BadLocationException
0499: */
0500: public void testIndentInfoBlockComments3()
0501: throws BadLocationException {
0502: BraceReduction _reduced = doc.getReduced();
0503: doc.insertString(0, "{\n /*\n*\n", null);
0504: // (\n/*\n*#\n
0505: _reduced.move(-1);
0506: IndentInfo ii = _reduced.getIndentInformation();
0507: _assertIndentInfo(ii, openSquiggly, -1, 8, 1);
0508: }
0509:
0510: /**
0511: * put your documentation comment here
0512: * @exception BadLocationException
0513: */
0514: public void testIndentInfoBlockComments4()
0515: throws BadLocationException {
0516: BraceReduction _reduced = doc.getReduced();
0517: doc.insertString(0, "\n{\n /*\n*\n", null);
0518: // \n(\n/*\n*#\n
0519: _reduced.move(-1);
0520: IndentInfo ii = _reduced.getIndentInformation();
0521: _assertIndentInfo(ii, openSquiggly, 8, 8, 1);
0522: }
0523:
0524: /**
0525: * put your documentation comment here
0526: * @exception BadLocationException
0527: */
0528: public void testSkippingBraces() throws BadLocationException {
0529: BraceReduction _reduced = doc.getReduced();
0530: doc.insertString(0, "\n{\n { ()}\n}", null);
0531: IndentInfo ii = _reduced.getIndentInformation();
0532: _assertIndentInfo(ii, openSquiggly, 12, 12, 1);
0533: }
0534:
0535: /**
0536: * put your documentation comment here
0537: * @exception BadLocationException
0538: */
0539: public void testSkippingComments() throws BadLocationException {
0540: // just paren
0541: BraceReduction _reduced = doc.getReduced();
0542: doc.insertString(0, "\n{\n //{ ()\n}", null);
0543: IndentInfo ii = _reduced.getIndentInformation();
0544: _assertIndentInfo(ii, openSquiggly, 13, 13, 1);
0545: }
0546:
0547: /**
0548: * put your documentation comment here
0549: * @exception BadLocationException
0550: */
0551: public void testSkippingCommentsBraceAtBeginning()
0552: throws BadLocationException {
0553: // just paren
0554: BraceReduction _reduced = doc.getReduced();
0555: doc.insertString(0, "{\n //{ ()}{", null);
0556: IndentInfo ii = _reduced.getIndentInformation();
0557: _assertIndentInfo(ii, openSquiggly, -1, 13, 11);
0558: }
0559:
0560: /**
0561: * put your documentation comment here
0562: * @exception BadLocationException
0563: */
0564: public void testNothingToIndentOn() throws BadLocationException {
0565: // just paren
0566: BraceReduction _reduced = doc.getReduced();
0567: doc.insertString(0, " //{ ()}{", null);
0568: IndentInfo ii = _reduced.getIndentInformation();
0569: _assertIndentInfo(ii, noBrace, -1, -1, -1);
0570: }
0571:
0572: /**
0573: * put your documentation comment here
0574: * @exception BadLocationException
0575: */
0576: public void testStartSimple() throws BadLocationException {
0577: // just paren
0578: doc.insertString(0, "abcde", null);
0579: doc.indentLines(doc.getCurrentLocation(), doc
0580: .getCurrentLocation());
0581: _assertContents("abcde", doc);
0582: }
0583:
0584: /**
0585: * put your documentation comment here
0586: * @exception BadLocationException
0587: */
0588: public void testStartSpaceIndent() throws BadLocationException {
0589: // just paren
0590: doc.insertString(0, " abcde", null);
0591: doc.indentLines(doc.getCurrentLocation(), doc
0592: .getCurrentLocation());
0593: _assertContents("abcde", doc);
0594: }
0595:
0596: /**
0597: * put your documentation comment here
0598: * @exception BadLocationException
0599: */
0600: public void testStartBrace() throws BadLocationException {
0601: // just paren
0602: doc.insertString(0, "public class temp \n {", null);
0603: doc.indentLines(doc.getCurrentLocation(), doc
0604: .getCurrentLocation());
0605: _assertContents("public class temp \n{", doc);
0606: }
0607:
0608: /**
0609: * put your documentation comment here
0610: * @exception BadLocationException
0611: */
0612: public void testEndBrace() throws BadLocationException {
0613: // just paren
0614: doc.insertString(0, "public class temp \n{ \n }", null);
0615: doc.indentLines(doc.getCurrentLocation(), doc
0616: .getCurrentLocation());
0617: _assertContents("public class temp \n{ \n}", doc);
0618: }
0619:
0620: /**
0621: * put your documentation comment here
0622: * @exception BadLocationException
0623: */
0624: public void testInsideClass() throws BadLocationException {
0625: // just paren
0626: doc.insertString(0, "public class temp \n{ \ntext here", null);
0627: doc.indentLines(doc.getCurrentLocation(), doc
0628: .getCurrentLocation());
0629: _assertContents("public class temp \n{ \n text here", doc);
0630: }
0631:
0632: /**
0633: * put your documentation comment here
0634: * @exception BadLocationException
0635: */
0636: public void testInsideClassWithBraceSets()
0637: throws BadLocationException {
0638: // just paren
0639: doc.insertString(0, "public class temp \n{ ()\ntext here",
0640: null);
0641: doc.indentLines(doc.getCurrentLocation(), doc
0642: .getCurrentLocation());
0643: _assertContents("public class temp \n{ ()\n text here", doc);
0644: }
0645:
0646: /**
0647: * put your documentation comment here
0648: * @exception BadLocationException
0649: */
0650: public void testIgnoreBraceOnSameLine() throws BadLocationException {
0651: // just paren
0652: doc.insertString(0, "public class temp \n{ ()\n{text here",
0653: null);
0654: doc.indentLines(doc.getCurrentLocation(), doc
0655: .getCurrentLocation());
0656: _assertContents("public class temp \n{ ()\n {text here", doc);
0657: }
0658:
0659: /**
0660: * Not supported any more.
0661: *
0662: public void testLargerIndent () throws BadLocationException {
0663: // just paren
0664: BraceReduction rm = doc.getReduced();
0665: doc.insertString(0, "public class temp \n { ()\n { text here", null);
0666: doc.indentLines(doc.getCurrentLocation(), doc.getCurrentLocation());
0667: _assertContents("public class temp \n { ()\n { text here", doc);
0668: }*/
0669:
0670: /**
0671: * put your documentation comment here
0672: * @exception BadLocationException
0673: */
0674: public void testWeird() throws BadLocationException {
0675: // just paren
0676: doc.insertString(0, "hello\n", null);
0677: doc.indentLines(doc.getCurrentLocation(), doc
0678: .getCurrentLocation());
0679: _assertContents("hello\n ", doc);
0680: }
0681:
0682: /**
0683: * put your documentation comment here
0684: * @exception BadLocationException
0685: */
0686: public void testWierd2() throws BadLocationException {
0687: // just paren
0688: doc.insertString(0, "hello", null);
0689: doc.indentLines(doc.getCurrentLocation(), doc
0690: .getCurrentLocation());
0691: _assertContents("hello", doc);
0692: }
0693:
0694: /**
0695: * put your documentation comment here
0696: * @exception BadLocationException
0697: */
0698: public void testMotion() throws BadLocationException {
0699: // just paren
0700: doc.insertString(0, "hes{\n{abcde", null);
0701: doc.insertString(11, "\n{", null);
0702: // hes{\n{abcde\n{#
0703: doc.move(-8);
0704: // hes{\n#{abcde\n{
0705: doc.indentLines(doc.getCurrentLocation(), doc
0706: .getCurrentLocation());
0707: // hes{\n #{abcde\n{
0708: _assertContents("hes{\n {abcde\n{", doc);
0709: }
0710:
0711: /**
0712: * put your documentation comment here
0713: * @exception BadLocationException
0714: */
0715: public void testNextCharIsNewline() throws BadLocationException {
0716: // just paren
0717: doc.insertString(0, "hes{\n{abcde", null);
0718: doc.insertString(11, "\n{", null);
0719: // hes{\n{abcde\n{#
0720: doc.move(-2);
0721: // hes{\n{abcde#\n{
0722: doc.indentLines(doc.getCurrentLocation(), doc
0723: .getCurrentLocation());
0724: // hes{\n {abcde#\n{
0725: _assertContents("hes{\n {abcde\n{", doc);
0726: }
0727:
0728: /**
0729: * put your documentation comment here
0730: * @exception BadLocationException
0731: */
0732: public void testFor() throws BadLocationException {
0733: // just paren
0734: doc.insertString(0, "for(;;)\n", null);
0735: doc.indentLines(doc.getCurrentLocation(), doc
0736: .getCurrentLocation());
0737: _assertContents("for(;;)\n ", doc);
0738: }
0739:
0740: /**
0741: * put your documentation comment here
0742: * @exception BadLocationException
0743: */
0744: public void testFor2() throws BadLocationException {
0745: // just paren
0746: doc.insertString(0, "{\n for(;;)\n", null);
0747: doc.indentLines(doc.getCurrentLocation(), doc
0748: .getCurrentLocation());
0749: _assertContents("{\n for(;;)\n ", doc);
0750: }
0751:
0752: /**
0753: * put your documentation comment here
0754: * @exception BadLocationException
0755: */
0756: public void testOpenParen() throws BadLocationException {
0757: // just paren
0758: doc.insertString(0, "hello(\n", null);
0759: doc.indentLines(doc.getCurrentLocation(), doc
0760: .getCurrentLocation());
0761: _assertContents("hello(\n ", doc);
0762: }
0763:
0764: /**
0765: * put your documentation comment here
0766: * @exception BadLocationException
0767: */
0768: public void testPrintString() throws BadLocationException {
0769: // just paren
0770: doc.insertString(0, "Sys.out(\"hello\"\n", null);
0771: doc.indentLines(doc.getCurrentLocation(), doc
0772: .getCurrentLocation());
0773: _assertContents("Sys.out(\"hello\"\n ", doc);
0774: }
0775:
0776: /**
0777: * put your documentation comment here
0778: * @exception BadLocationException
0779: */
0780: public void testOpenBracket() throws BadLocationException {
0781: // just paren
0782: doc.insertString(0, "hello[\n", null);
0783: doc.indentLines(doc.getCurrentLocation(), doc
0784: .getCurrentLocation());
0785: _assertContents("hello[\n ", doc);
0786: }
0787:
0788: /**
0789: * put your documentation comment here
0790: * @exception BadLocationException
0791: */
0792: public void testSquigglyAlignment() throws BadLocationException {
0793: // just paren
0794: doc.insertString(0, "{\n }", null);
0795: doc.indentLines(doc.getCurrentLocation(), doc
0796: .getCurrentLocation());
0797: _assertContents("{\n}", doc);
0798: }
0799:
0800: /**
0801: * put your documentation comment here
0802: * @exception BadLocationException
0803: */
0804: public void testSpaceBrace() throws BadLocationException {
0805: // just paren
0806: doc.insertString(0, " {\n", null);
0807: doc.indentLines(doc.getCurrentLocation(), doc
0808: .getCurrentLocation());
0809: _assertContents(" {\n ", doc);
0810: }
0811:
0812: /**
0813: * Cascading indent is not used anymore.
0814: *
0815: public void testOpenSquigglyCascade () throws BadLocationException {
0816: // just paren
0817: BraceReduction rm = doc.getReduced();
0818: doc.insertString(0, "if\n if\n if\n{", null);
0819: doc.indentLines(doc.getCurrentLocation(), doc.getCurrentLocation());
0820: _assertContents("if\n if\n if\n {", doc);
0821: }*/
0822:
0823: /**
0824: * Cascading indent is not used anymore.
0825: *
0826: public void testOpenSquigglyCascade2 () throws BadLocationException {
0827: // just paren
0828: BraceReduction rm = doc.getReduced();
0829: doc.insertString(0, "{\n if\n if\n if\n{", null);
0830: doc.indentLines(doc.getCurrentLocation(), doc.getCurrentLocation());
0831: _assertContents("{\n if\n if\n if\n {", doc);
0832: }*/
0833:
0834: /**
0835: * put your documentation comment here
0836: * @exception BadLocationException
0837: */
0838: public void testEnter() throws BadLocationException {
0839: // just paren
0840: doc.insertString(0, "\n\n", null);
0841: doc.indentLines(doc.getCurrentLocation(), doc
0842: .getCurrentLocation());
0843: _assertContents("\n\n", doc);
0844: }
0845:
0846: /**
0847: * put your documentation comment here
0848: * @exception BadLocationException
0849: */
0850: public void testEnter2() throws BadLocationException {
0851: // just paren
0852: doc.insertString(0, "\n", null);
0853: doc.indentLines(doc.getCurrentLocation(), doc
0854: .getCurrentLocation());
0855: _assertContents("\n", doc);
0856: }
0857:
0858: /**
0859: * put your documentation comment here
0860: * @exception BadLocationException
0861: */
0862: public void testNotRecognizeComments() throws BadLocationException {
0863: // just paren
0864: doc.insertString(0, "\nhello //bal;\n", null);
0865: doc.indentLines(doc.getCurrentLocation(), doc
0866: .getCurrentLocation());
0867: _assertContents("\nhello //bal;\n ", doc);
0868: }
0869:
0870: /**
0871: * put your documentation comment here
0872: * @exception BadLocationException
0873: */
0874: public void testNotRecognizeComments2() throws BadLocationException {
0875: // just paren
0876: doc.insertString(0, "\nhello; /*bal*/\n ", null);
0877: doc.indentLines(doc.getCurrentLocation(), doc
0878: .getCurrentLocation());
0879: _assertContents("\nhello; /*bal*/\n", doc);
0880: }
0881:
0882: /**
0883: * put your documentation comment here
0884: * @exception BadLocationException
0885: */
0886: public void testBlockIndent() throws BadLocationException {
0887: // just paren
0888: doc.insertString(0, "hello\n{\n{\n {", null);
0889: doc.indentLines(8, 13);
0890: _assertContents("hello\n{\n {\n {", doc);
0891: }
0892:
0893: /**
0894: * Regression test for bug in drjava-20010802-1020:
0895: * Indent block on a file containing just " x;\n y;\n" would throw an
0896: * exception.
0897: * @exception BadLocationException
0898: */
0899: public void testBlockIndent2() throws BadLocationException {
0900: doc.insertString(0, " x;\n y;\n", null);
0901: doc.indentLines(0, doc.getLength());
0902: _assertContents("x;\ny;\n", doc);
0903: }
0904:
0905: /**
0906: * put your documentation comment here
0907: * @exception BadLocationException
0908: */
0909: public void testIndentInsideCommentBlock()
0910: throws BadLocationException {
0911: doc.insertString(0, "hello\n{\n/*{\n{\n*/\nhehe", null);
0912: doc.indentLines(0, 21);
0913: _assertContents("hello\n{\n /*{\n {\n */\n hehe", doc);
0914: }
0915:
0916: /**
0917: * put your documentation comment here
0918: * @exception BadLocationException
0919: */
0920: public void testSecondLineProblem() throws BadLocationException {
0921: // just paren
0922: doc.insertString(0, "\n", null);
0923: doc.indentLines(doc.getCurrentLocation(), doc
0924: .getCurrentLocation());
0925: _assertContents("\n", doc);
0926: }
0927:
0928: /**
0929: * put your documentation comment here
0930: * @exception BadLocationException
0931: */
0932: public void testSecondLineProblem2() throws BadLocationException {
0933: // just paren
0934: doc.insertString(0, "a\n", null);
0935: doc.indentLines(doc.getCurrentLocation(), doc
0936: .getCurrentLocation());
0937: _assertContents("a\n ", doc);
0938: }
0939:
0940: /**
0941: * put your documentation comment here
0942: * @exception BadLocationException
0943: */
0944: public void testSmallFileProblem() throws BadLocationException {
0945: // just paren
0946: doc.insertString(0, "\n\n", null);
0947: doc.indentLines(doc.getCurrentLocation(), doc
0948: .getCurrentLocation());
0949: _assertContents("\n\n", doc);
0950: }
0951:
0952: /**
0953: * Regression test for arrays.
0954: */
0955: public void testAnonymousInnerClass() throws BadLocationException {
0956: String text = "addWindowListener(new WindowAdapter() {\n"
0957: + "public void windowClosing(WindowEvent e) {\n"
0958: + "dispose();\n" + "}\n" + "void x() {\n" + "\n"
0959: + "}\n" + "\n" + "}\n" + ");\n";
0960: String indented = "addWindowListener(new WindowAdapter() {\n"
0961: + " public void windowClosing(WindowEvent e) {\n"
0962: + " dispose();\n" + " }\n" + " void x() {\n"
0963: + " \n" + " }\n" + " \n" + "}\n" + ");\n";
0964:
0965: doc.insertString(0, text, null);
0966: _assertContents(text, doc);
0967: doc.indentLines(0, doc.getLength());
0968: _assertContents(indented, doc);
0969: }
0970:
0971: // /** Regression test for Bug #627753. Uncomment when it is fixed.
0972: // */
0973: // public void testNestedUnbracedFor() throws BadLocationException {
0974: // String text =
0975: // "for (int a =0; a < 5; a++)\n" +
0976: // "for (int b = 0; b < 5; b++) {\n" +
0977: // "System.out.println(a + b);";
0978: // String indented =
0979: // "for (int a =0; a < 5; a++)\n" +
0980: // " for (int b = 0; b < 5; b++) {\n" +
0981: // " System.out.println(a + b);";
0982: // doc.insertString(0, text, null);
0983: // _assertContents(text, doc);
0984: // doc.indentLines(0, doc.getLength());
0985: // _assertContents(indented, doc);
0986: // doc.remove(0,doc.getLength() - 1);
0987: //
0988: // text =
0989: // "if (true)\n" +
0990: // "if (true)\n" +
0991: // "System.out.println(\"Hello\");";
0992: // indented =
0993: // "if (true)\n" +
0994: // " if (true)\n" +
0995: // " System.out.println(\"Hello\");";
0996: // doc.insertString(0, text, null);
0997: // _assertContents(text, doc);
0998: // doc.indentLines(0, doc.getLength());
0999: // _assertContents(indented, doc);
1000: // doc.remove(0,doc.getLength() - 1);
1001: //
1002: // text =
1003: // "{\n" +
1004: // "while (a < 5)\n" +
1005: // "while (b < 5) {\n" +
1006: // "System.out.println(a + b);";
1007: // indented =
1008: // "{\n" +
1009: // " while (a < 5)\n" +
1010: // " while (b < 5) {\n" +
1011: // " System.out.println(a + b);";
1012: // doc.insertString(0, text, null);
1013: // _assertContents(text, doc);
1014: // doc.indentLines(0, doc.getLength());
1015: // _assertContents(indented, doc);
1016: // doc.remove(0,doc.getLength() - 1);
1017: //
1018: // text =
1019: // "while (a < 5)\n" +
1020: // "while (b < 5);\n" +
1021: // "System.out.println(a + b);";
1022: // indented =
1023: // "while (a < 5)\n" +
1024: // " while (b < 5);\n" +
1025: // "System.out.println(a + b);";
1026: // doc.insertString(0, text, null);
1027: // _assertContents(text, doc);
1028: // doc.indentLines(0, doc.getLength());
1029: // _assertContents(indented, doc);
1030: // doc.remove(0,doc.getLength() - 1);
1031: //
1032: // text =
1033: // "do\n" +
1034: // "do\n" +
1035: // "x=5;\n" +
1036: // "while(false);\n" +
1037: // "while(false);\n";
1038: // indented =
1039: // "do\n" +
1040: // " do\n" +
1041: // " x=5;\n" +
1042: // " while(false);\n" +
1043: // "while(false);\n";
1044: // doc.insertString(0, text, null);
1045: // _assertContents(text, doc);
1046: // doc.indentLines(0, doc.getLength());
1047: // _assertContents(indented, doc);
1048: // doc.remove(0,doc.getLength() - 1);
1049: // }
1050:
1051: public void testLiveUpdateOfIndentLevel()
1052: throws BadLocationException {
1053:
1054: String text = "int[2][] a ={\n" + "{\n" + "1,\n" + "2,\n"
1055: + "3},\n" + "{\n" + "4,\n" + "5}\n" + "};\n";
1056:
1057: String indentedBefore = "int[2][] a ={\n" + " {\n"
1058: + " 1,\n" + " 2,\n" + " 3},\n" + " {\n"
1059: + " 4,\n" + " 5}\n" + "};\n";
1060:
1061: String indentedAfter = "int[2][] a ={\n" + " {\n"
1062: + " 1,\n" + " 2,\n"
1063: + " 3},\n" + " {\n"
1064: + " 4,\n" + " 5}\n"
1065: + "};\n";
1066:
1067: doc.insertString(0, text, null);
1068:
1069: _assertContents(text, doc);
1070: doc.indentLines(0, doc.getLength());
1071: _assertContents(indentedBefore, doc);
1072: DrJava.getConfig().setSetting(OptionConstants.INDENT_LEVEL,
1073: new Integer(8));
1074:
1075: Utilities.clearEventQueue();
1076: doc.indentLines(0, doc.getLength());
1077: _assertContents(indentedAfter, doc);
1078: }
1079:
1080: /**
1081: * tests that an if statment nested in a switch will be indented properly
1082: * @throws BadLocationException
1083: */
1084: public void testNestedIfInSwitch() throws BadLocationException {
1085: String text = "switch(cond) {\n" + "case 1:\n"
1086: + "object.doStuff();\n"
1087: + "if(object.hasDoneStuff()) {\n"
1088: + "thingy.doOtherStuff();\n"
1089: + "lion.roar(\"raaargh\");\n" + "}\n" + "break;\n"
1090: + "}\n";
1091:
1092: String indented = "switch(cond) {\n" + " case 1:\n"
1093: + " object.doStuff();\n"
1094: + " if(object.hasDoneStuff()) {\n"
1095: + " thingy.doOtherStuff();\n"
1096: + " lion.roar(\"raaargh\");\n" + " }\n"
1097: + " break;\n" + "}\n";
1098:
1099: doc.insertString(0, text, null);
1100: _assertContents(text, doc);
1101: doc.indentLines(0, doc.getLength());
1102: _assertContents(indented, doc);
1103: }
1104:
1105: // Commented out because reference files are missing!
1106: // /** Tests a list of files when indented match their correct indentations */
1107: // public void testIndentationFromFile() throws IOException {
1108: // File directory = new File("testFiles");
1109: //
1110: // File[] unindentedFiles = {new File(directory, "IndentSuccesses.indent")
1111: // /*, new File(directory, "IndentProblems.indent")*/};
1112: // File[] correctFiles = {new File(directory, "IndentSuccessesCorrect.indent")
1113: // /*, new File(directory, "IndentProblemsCorrect.indent")*/};
1114: //
1115: // for (int x = 0; x < correctFiles.length; x++) {
1116: // _indentAndCompare(unindentedFiles[x], correctFiles[x]);
1117: // }
1118: //
1119: // //We know the following test file should (currently) fail, so we assert that it will fail to check
1120: // //our _indentAndCompare(...) function
1121: // boolean threwAFE = false;
1122: // try {
1123: // _indentAndCompare(new File(directory, "IndentProblems.indent"),
1124: // new File(directory, "IndentProblemsCorrect.indent"));
1125: // }
1126: // catch(AssertionFailedError afe) {
1127: // threwAFE = true;
1128: // }
1129: // if (!threwAFE) {
1130: // fail("_indentAndCompare should have failed for IndentProblems.indent");
1131: // }
1132: // }
1133:
1134: public void testIndentingCorrectLine() throws BadLocationException {
1135: String test1 = "class A {\n" + " int a = 5;\n" + " }";
1136:
1137: String test1Correct = "class A {\n" + " int a = 5;\n" + "}";
1138:
1139: String test2 = " {\n" + " int a = 5;\n" + " }\n";
1140:
1141: String test2Correct = "{\n" + " int a = 5;\n" + " }\n";
1142:
1143: doc.insertString(0, test1, null);
1144: _assertContents(test1, doc);
1145: doc.setCurrentLocation(20);
1146: doc.indentLines(20, 20);
1147: // System.out.println("test1 = \n" + test1 + "\n length = " + test1.length());
1148: // System.out.println("test1 = \n" + doc.getText() + "\n length = " + doc.getLength());
1149: _assertContents(test1, doc);
1150:
1151: doc = new DefinitionsDocument(_notifier);
1152:
1153: doc.insertString(0, test1, null);
1154: _assertContents(test1, doc);
1155: doc.indentLines(28, 28);
1156: _assertContents(test1Correct, doc);
1157:
1158: doc = new DefinitionsDocument(_notifier);
1159:
1160: doc.insertString(0, test2, null);
1161: _assertContents(test2, doc);
1162: doc.setCurrentLocation(5);
1163: doc.indentLines(5, 5);
1164: _assertContents(test2Correct, doc);
1165: }
1166:
1167: /**
1168: * Tests that annotations do not change the indent level of the lines following.
1169: * @throws BadLocationException
1170: */
1171: public void testAnnotationsAfterOpenCurly()
1172: throws BadLocationException {
1173: String textToIndent = "@Annotation\n"
1174: + "public class TestClass {\n"
1175: + "public TestClass() {}\n" + "\n"
1176: + "@Annotation(WithParens)\n"
1177: + "private int _classField = 42;\n" + "\n"
1178: + "@Override\n" + "public String toString() {\n"
1179: + "@LocalVariableAnnotation\n"
1180: + "String msg = \"hello\";\n" + "return msg;\n" + "}\n"
1181: + "\n" + "public int methodAfterAnnotation() {\n"
1182: + "return 0;\n" + "}\n" + "}\n" + "\n";
1183: String textIndented = "@Annotation\n"
1184: + "public class TestClass {\n"
1185: + " public TestClass() {}\n" + " \n"
1186: + " @Annotation(WithParens)\n"
1187: + " private int _classField = 42;\n" + " \n"
1188: + " @Override\n" + " public String toString() {\n"
1189: + " @LocalVariableAnnotation\n"
1190: + " String msg = \"hello\";\n" + " return msg;\n"
1191: + " }\n" + " \n"
1192: + " public int methodAfterAnnotation() {\n"
1193: + " return 0;\n" + " }\n" + "}\n" + "\n";
1194:
1195: doc.insertString(0, textToIndent, null);
1196: _assertContents(textToIndent, doc);
1197: doc.indentLines(0, doc.getLength());
1198: _assertContents(textIndented, doc);
1199: }
1200:
1201: /**
1202: * Tests that annotations do not change the indent level of the lines following.
1203: * @throws BadLocationException
1204: */
1205: public void testAnnotationsAfterDefinition()
1206: throws BadLocationException {
1207: String textToIndent = "@Annotation\n"
1208: + "public class TestClass {\n"
1209: + "public TestClass() {}\n" + "\n"
1210: + "private int _classField = 0;\n" + "\n"
1211: + "@Annotation(WithParens)\n"
1212: + "private int _classField2 = 42;\n" + "\n"
1213: + "@Override\n" + "public String toString() {\n"
1214: + "@LocalVariableAnnotation\n"
1215: + "String msg = \"hello\";\n" + "return msg;\n" + "}\n"
1216: + "\n" + "public int methodAfterAnnotation() {\n"
1217: + "return 0;\n" + "}\n" + "}\n";
1218: String textIndented = "@Annotation\n"
1219: + "public class TestClass {\n"
1220: + " public TestClass() {}\n" + " \n"
1221: + " private int _classField = 0;\n" + " \n"
1222: + " @Annotation(WithParens)\n"
1223: + " private int _classField2 = 42;\n" + " \n"
1224: + " @Override\n" + " public String toString() {\n"
1225: + " @LocalVariableAnnotation\n"
1226: + " String msg = \"hello\";\n" + " return msg;\n"
1227: + " }\n" + " \n"
1228: + " public int methodAfterAnnotation() {\n"
1229: + " return 0;\n" + " }\n" + "}\n";
1230:
1231: doc.insertString(0, textToIndent, null);
1232: _assertContents(textToIndent, doc);
1233: doc.indentLines(0, doc.getLength());
1234: _assertContents(textIndented, doc);
1235: }
1236:
1237: /**
1238: * tests that an if statment nested in a switch will be indented properly
1239: * this, as opposed to the previous test, does not have any code in that case
1240: * except the if statement
1241: * @throws BadLocationException
1242: */
1243: /* public void testNestedIfInSwitch2() throws BadLocationException {
1244: String text =
1245: "switch(c) {\n" +
1246: "case 2:\n" +
1247: "break;\n" +
1248: "case 3:\n" +
1249: "if(owner.command() == ROLL_OVER) {\n" +
1250: "dog.rollOver();\n" +
1251: "}\n" +
1252: "break;\n" +
1253: "}\n";
1254:
1255: String indented =
1256: "switch(c) {\n" +
1257: " case 2:\n" +
1258: " break;\n" +
1259: " case 3:\n" +
1260: " if(owner.command() == ROLL_OVER) {\n" +
1261: " dog.rollOver();\n" +
1262: " }\n" +
1263: " break;\n" +
1264: "}\n";
1265:
1266: doc.insertString(0, text, null);
1267: _assertContents(text, doc);
1268: doc.indentLines(0, doc.getLength());
1269: _assertContents(indented, doc);
1270: }
1271: */
1272: private void _assertContents(String expected, DJDocument document)
1273: throws BadLocationException {
1274: assertEquals("document contents", expected, document.getText());
1275: }
1276:
1277: private void _assertIndentInfo(IndentInfo ii, String braceType,
1278: int distToNewline, int distToBrace, int distToPrevNewline) {
1279: assertEquals("indent info: brace type", braceType, ii.braceType);
1280: assertEquals("indent info: dist to new line", distToNewline,
1281: ii.distToNewline);
1282: assertEquals("indent info: dist to brace", distToBrace,
1283: ii.distToBrace);
1284: assertEquals("indent info: dist to prev new line",
1285: distToPrevNewline, ii.distToPrevNewline);
1286: }
1287:
1288: // /** Copies fromFile to toFile, assuming both files exist. */
1289: // private void _copyFile(File fromFile, File toFile) throws IOException {
1290: // String text = FileOps.readFileAsString(fromFile);
1291: // FileOps.writeStringToFile(toFile, text);
1292: // String newText = FileOps.readFileAsString(toFile);
1293: // assertEquals("File copy verify", text, newText);
1294: // }
1295:
1296: // /**
1297: // * indents one file, compares it to the other, reindents and recompares
1298: // * to make sure indent(x) = indent(indent(x))
1299: // */
1300: // private void _indentAndCompare(File unindented, File correct)
1301: // throws IOException
1302: // {
1303: // File test = null;
1304: // try {
1305: // test = File.createTempFile("test", ".java");
1306: // _copyFile(unindented, test);
1307: // test.deleteOnExit();
1308: // IndentFiles.main(new String[] {"-silent", test.toString()});
1309: // _fileCompare(test, correct);
1310: // IndentFiles.main(new String[] {"-silent", test.toString()});
1311: // _fileCompare(test, correct);
1312: // }
1313: // finally {
1314: // if (test != null) {
1315: // test.delete();
1316: // }
1317: // }
1318: //
1319: // }
1320:
1321: // /**
1322: // * @throws AssertionFailedError if the files are not identical
1323: // */
1324: // private void _fileCompare(File test, File correct) throws IOException {
1325: // FileReader fr = new FileReader(correct);
1326: // FileReader fr2 = new FileReader(test);
1327: // BufferedReader correctBufferedReader = new BufferedReader(fr);
1328: // BufferedReader testBufferedReader = new BufferedReader(fr2);
1329: //
1330: // String correctString = correctBufferedReader.readLine();
1331: // String testString = testBufferedReader.readLine();
1332: // int lineNo = 1;
1333: // while (correctString != null && testString != null) {
1334: // assertEquals("File: " + correct + " line: " + lineNo, correctString, testString);
1335: // correctString = correctBufferedReader.readLine();
1336: // testString = testBufferedReader.readLine();
1337: // lineNo++;
1338: // }
1339: // assertTrue("Indented file longer than expected", correctString == null);
1340: // assertTrue("Indented file shorter than expected", testString == null);
1341: //
1342: // testBufferedReader.close();
1343: // correctBufferedReader.close();
1344: // fr.close();
1345: // fr2.close();
1346: // }
1347:
1348: /*
1349: public void testNoParameters() throws BadLocationException
1350: {
1351: IndentRuleAction _action = new ActionBracePlus("");
1352:
1353: String _text =
1354: "method(\n"+
1355: ")\n";
1356:
1357: String _aligned =
1358: "method(\n"+
1359: " )\n";
1360:
1361: doc.insertString(0, _text, null);
1362: _action.indentLine(doc, 0); // Does nothing.
1363: assertEquals("START has no brace.", _text.length(), doc.getLength());
1364: doc.indentLines(0, 7); // Does nothing.
1365: assertEquals("START has no brace.", _text.length(), doc.getLength());
1366:
1367: doc.indentLines(0, doc.getLength()); // Aligns second line, a second time.
1368: System.out.println(doc.getText());
1369: _assertContents(_aligned, doc);
1370: assertEquals("Line aligned to open paren.", _aligned.length(), doc.getLength());
1371: }
1372: */
1373: /*
1374: public void testArrayInit() throws BadLocationException
1375: {
1376: IndentRuleAction _action = new ActionBracePlus("");
1377:
1378: String _text =
1379: "int[] ar = new int[] {\n"+
1380: "1,1,1,1,1,1,1,1,1 };";
1381:
1382: String _aligned =
1383: "int[] ar = new int[] {\n"+
1384: " 1,1,1,1,1,1,1,1,1 };";
1385:
1386: doc.insertString(0, _text, null);
1387: _action.indentLine(doc, 0); // Does nothing.
1388: assertEquals("START has no brace.", _text.length(), doc.getLength());
1389: doc.indentLines(0, 7); // Does nothing.
1390: assertEquals("START has no brace.", _text.length(), doc.getLength());
1391:
1392: doc.indentLines(0, doc.getLength()); // Aligns second line, a second time.
1393: System.out.println(doc.getText());
1394: _assertContents(_aligned, doc);
1395: assertEquals("Line aligned to open paren.", _aligned.length(), doc.getLength());
1396: }
1397: */
1398: /*
1399: public void testArrayInitNewline() throws BadLocationException
1400: {
1401: IndentRuleAction _action = new ActionBracePlus("");
1402:
1403: String _text =
1404: "int[] ar = new int[] { 1,1,1,\n"+
1405: "1,1,1,1,1,1 };";
1406:
1407: String _aligned =
1408: "int[] ar = new int[] { 1,1,1,\n"+
1409: " 1,1,1,1,1,1 };";
1410:
1411: doc.insertString(0, _text, null);
1412: _action.indentLine(doc, 0); // Does nothing.
1413: assertEquals("START has no brace.", _text.length(), doc.getLength());
1414: doc.indentLines(0, 7); // Does nothing.
1415: assertEquals("START has no brace.", _text.length(), doc.getLength());
1416:
1417: doc.indentLines(0, doc.getLength()); // Aligns second line, a second time.
1418: System.out.println(doc.getText());
1419: _assertContents(_aligned, doc);
1420: assertEquals("Line aligned to open paren.", _aligned.length(), doc.getLength());
1421: }
1422: */
1423: /*
1424: public void testArrayInitBraceNewline() throws BadLocationException
1425: {
1426: IndentRuleAction _action = new ActionBracePlus("");
1427:
1428: String _text =
1429: "int[] blah = new int[] {1, 2, 3\n"+
1430: "};";
1431:
1432: String _aligned =
1433: "int[] blah = new int[] {1, 2, 3\n"+
1434: " };";
1435:
1436: doc.insertString(0, _text, null);
1437: _action.indentLine(doc, 0); // Does nothing.
1438: assertEquals("START has no brace.", _text.length(), doc.getLength());
1439: doc.indentLines(0, 7); // Does nothing.
1440: assertEquals("START has no brace.", _text.length(), doc.getLength());
1441:
1442: doc.indentLines(0, doc.getLength()); // Aligns second line, a second time.
1443: System.out.println(doc.getText());
1444: _assertContents(_aligned, doc);
1445: assertEquals("Line aligned to open paren.", _aligned.length(), doc.getLength());
1446: }
1447: */
1448: /*
1449: public void testArrayInitAllNewline() throws BadLocationException
1450: {
1451: IndentRuleAction _action = new ActionBracePlus("");
1452:
1453: String _text =
1454: "int[] blah = new int[]\n"+
1455: "{4, 5, 6};";
1456:
1457: String _aligned =
1458: "int[] blah = new int[]\n"+
1459: " {4, 5, 6};";
1460:
1461: doc.insertString(0, _text, null);
1462: _action.indentLine(doc, 0); // Does nothing.
1463: assertEquals("START has no brace.", _text.length(), doc.getLength());
1464: doc.indentLines(0, 7); // Does nothing.
1465: assertEquals("START has no brace.", _text.length(), doc.getLength());
1466:
1467: doc.indentLines(0, doc.getLength()); // Aligns second line, a second time.
1468: System.out.println(doc.getText());
1469: _assertContents(_aligned, doc);
1470: assertEquals("Line aligned to open paren.", _aligned.length(), doc.getLength());
1471: }
1472: */
1473: }
|