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: /**
040: * Test class for an indent rule with a really long name. :-)
041: * Inherits from ActionStartPrevLinePlusTest, since this rule's functionality
042: * should be a strict extension of ActionStartPrevLinePlus.
043: * @version $Id: ActionStartPrevLinePlusBackupTest.java 4255 2007-08-28 19:17:37Z mgricken $
044: */
045: public class ActionStartPrevLinePlusBackupTest extends
046: ActionStartPrevLinePlusTest {
047:
048: /**
049: * Factory to enable reuse of methods from ActionStartPrevLinePlusTest.
050: * This creates an action that should behave identically to an instance of
051: * ActionStartPrevLinePlus.
052: * @param suffix the text to be added by this rule after indent padding
053: * @see ActionStartPrevLinePlus#ActionStartPrevLinePlus(String)
054: */
055: private IndentRuleAction makeAction(String suffix) {
056: return new ActionStartPrevLinePlusBackup(suffix, suffix
057: .length());
058: }
059:
060: /**
061: * Factory to enable reuse of methods from ActionStartPrevLinePlusBackupTest.
062: * This works similarly to {@link #makeAction(String)}.
063: * @param suffix the text to be added by this rule after indent padding
064: * @param position the character within the suffix string before which to
065: * place the cursor
066: * @see ActionStartPrevLinePlusBackup#ActionStartPrevLinePlusBackup(String, int)
067: */
068: private IndentRuleAction makeBackupAction(String suffix,
069: int position) {
070: return new ActionStartPrevLinePlusBackup(suffix, position);
071: }
072:
073: private String _noIndent = "foo\nbar";
074: private String _evenIndent = " foo\n bar";
075: private String _unevenIndent = " foo\nbar";
076: private String _noIndentRes = "foo\nabc bar";
077: private String _evenIndentRes = " foo\n abc bar";
078: private String _unevenIndentRes = " foo\n abc bar";
079:
080: /**
081: * Attempts to move current location to the start of the suffix.
082: */
083: public void testMoveToStart()
084: throws javax.swing.text.BadLocationException {
085: moveTestHelper(_noIndent, _noIndentRes, 0, 7, 0, 4);
086: moveTestHelper(_evenIndent, _evenIndentRes, 0, 11, 0, 8);
087: moveTestHelper(_unevenIndent, _unevenIndentRes, 2, 9, 0, 8);
088: }
089:
090: /**
091: * Attempts to move current location to the end of the suffix.
092: */
093: public void testMoveToEnd()
094: throws javax.swing.text.BadLocationException {
095: moveTestHelper(_noIndent, _noIndentRes, 0, 4, 4, 8);
096: moveTestHelper(_evenIndent, _evenIndentRes, 0, 6, 4, 12);
097: moveTestHelper(_unevenIndent, _unevenIndentRes, 2, 6, 4, 12);
098: }
099:
100: /**
101: * Attempts to move current location to the middle of the suffix.
102: */
103: public void testMoveToMiddle()
104: throws javax.swing.text.BadLocationException {
105: moveTestHelper(_noIndent, _noIndentRes, 0, 4, 2, 6);
106: moveTestHelper(_evenIndent, _evenIndentRes, 0, 6, 2, 10);
107: moveTestHelper(_unevenIndent, _unevenIndentRes, 2, 6, 2, 10);
108: }
109:
110: /**
111: * Helper method for "MoveTo" tests.
112: * @param text the test text
113: * @param result the result text
114: * @param deltaLen the change in text length
115: * @param before location to set before indenting
116: * @param position param to pass to makeBackupAction
117: * @param after location to expect after indenting
118: */
119: private void moveTestHelper(String text, String result,
120: int deltaLen, int before, int position, int after)
121: throws javax.swing.text.BadLocationException {
122: _setDocText(text);
123: _doc.setCurrentLocation(before); // end of bar line
124:
125: String suffix = "abc ";
126: makeBackupAction(suffix, position).indentLine(_doc,
127: Indenter.IndentReason.OTHER);
128: assertEquals("text length", text.length() + deltaLen
129: + suffix.length(), _doc.getLength());
130: assertEquals("text contents", result, _doc.getText());
131: assertEquals("location", after, _doc.getCurrentLocation());
132: }
133: }
|