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.reducedmodel;
038:
039: import edu.rice.cs.drjava.DrJavaTestCase;
040: import edu.rice.cs.drjava.model.AbstractDJDocument;
041: import edu.rice.cs.drjava.model.DJDocument;
042: import edu.rice.cs.drjava.model.definitions.indent.Indenter;
043:
044: import javax.swing.text.AbstractDocument;
045: import javax.swing.text.BadLocationException;
046:
047: //import edu.rice.cs.drjava.model.definitions.DefinitionsDocument;
048:
049: /**
050: *
051: * @version $Id: IndentInfoTest.java 4255 2007-08-28 19:17:37Z mgricken $
052: */
053: public final class IndentInfoTest extends DrJavaTestCase {
054: private String _text;
055: //private DefinitionsDocument _document;
056: //private BraceReduction _reduced;
057: private IndentInfo _info;
058: //private GlobalEventNotifier _notifier;
059: private DJDocument _document;
060:
061: public void setUp() throws Exception {
062: super .setUp();
063: //_notifier = new GlobalEventNotifier();
064: // _document = new DefinitionsDocument(_notifier);
065: _document = new AbstractDJDocument() {
066: protected int startCompoundEdit() {
067: //Do nothing
068: return 0;
069: }
070:
071: protected void endCompoundEdit(int key) { /* Do nothing. */
072: }
073:
074: protected void endLastCompoundEdit() { /* Do nothing. */
075: }
076:
077: protected void addUndoRedo(
078: AbstractDocument.DefaultDocumentEvent chng,
079: Runnable undoCommand, Runnable doCommand) {
080: /* Do nothing */
081: }
082:
083: protected void _styleChanged() { /* Do nothing. */
084: }
085:
086: protected Indenter makeNewIndenter(int indentLevel) {
087: return new Indenter(indentLevel);
088: }
089: };
090: }
091:
092: private void _infoTestHelper(int location, String message,
093: int expDistToPrevNewline, int expDistToBrace,
094: int expDistToNewline, int expDistToBraceCurrent,
095: int expDistToNewlineCurrent) {
096: _document.setCurrentLocation(location);
097: //_reduced = _document.getReduced();
098: _info = _document.getIndentInformation();
099:
100: assertEquals(message + " -- distToPrevNewline",
101: expDistToPrevNewline, _info.distToPrevNewline);
102: assertEquals(message + " -- distToBrace", expDistToBrace,
103: _info.distToBrace);
104: assertEquals(message + " -- distToNewline", expDistToNewline,
105: _info.distToNewline);
106: assertEquals(message + " -- distToBraceCurrent",
107: expDistToBraceCurrent, _info.distToBraceCurrent);
108: assertEquals(message + " -- distToNewlineCurrent",
109: expDistToNewlineCurrent, _info.distToNewlineCurrent);
110: }
111:
112: public void testFieldsForCurrentLocation()
113: throws BadLocationException {
114:
115: _text = "foo {\nvoid m1(int a,\nint b) {\n}\n}";
116: // . . .. . .. . . . . ... .
117: // | | | |
118: // 0 10 20 30
119:
120: _document.clear();
121: _document.insertString(0, _text, null);
122:
123: _infoTestHelper(0, "DOCSTART -- no brace or newline", -1, -1,
124: -1, -1, -1);
125: _infoTestHelper(4, "Location has no brace or newline", -1, -1,
126: -1, -1, -1);
127: _infoTestHelper(5, "Location has a brace but no newline", -1,
128: -1, -1, 1, -1);
129: _infoTestHelper(6, "Location has a brace and a newline", 0, 2,
130: -1, 2, -1);
131: _infoTestHelper(10, "Location has a brace and a newline", 4, 6,
132: -1, 6, -1);
133: _infoTestHelper(13, "Location has a brace and a newline", 7, 9,
134: -1, 9, -1);
135: _infoTestHelper(14, "Location has a brace and a newline", 8,
136: 10, -1, 1, 8);
137: _infoTestHelper(20, "At \\n within parens", 14, 16, -1, 7, 14);
138: _infoTestHelper(21, "Second line within parens", 0, 8, 15, 8,
139: 15);
140: _infoTestHelper(26, "On close paren", 5, 13, 20, 13, 20);
141: _infoTestHelper(28, "On second open brace", 7, 15, 22, 24, -1);
142: _infoTestHelper(29, "On \\n in second set of braces", 8, 16,
143: 23, 1, 8);
144: _infoTestHelper(30, "Close brace of method declaration", 0, 2,
145: 9, 2, 9);
146: _infoTestHelper(31, "Last \\n", 1, 3, 10, 27, -1);
147: _infoTestHelper(32, "Final close brace", 0, 28, -1, 28, -1);
148: }
149: }
|