[Viplugin-devel] Re: JUnit Test writer for Eclipse based
Brought to you by:
midramas
|
From: Michael B. <zed...@sa...> - 2003-12-08 12:49:49
|
----- Forwarded message from "Brian M. Kelley" <bk...@sb...> -----
Date: Sun, 07 Dec 2003 23:35:55 -0800
From: "Brian M. Kelley" <bk...@sb...>
To: Michael Bartl <zed...@sa...>
Subject: Re: JUnit Test writer for Eclipse based
Hey Michael. I've spent a little time working on the test suite, and
I've got a few patches for you to review. The included patch file is in
eclipse unified patch format. I've described the changes I made to the
files, below.
I think the changes are a step in the right direction for the test
suite, given that it looks like TextModificator is likely to be
refactored (it's always good to have some type of test infrastructure in
place to do regression testing with). Anyway, all tests are being ran
and are passing now. Let me know if there are any problems with the
changes in your environment. Thanks!
TextModificator.java:
setEmptyInstance() is configurable in support of the movement of
singleton management inside the TextModificator class (rather in
SharedTextResources).
getNextWord() checks for null and empty strings, and handles text with
no word-endings like gvim does (moves to end of line).
AllTests.java:
Added test suite for TextModificatorTest class.
DeleteCommandsTest.java:
Integrate configuration of TextModificator singleton into test, for
synchronization purposes.
MotionCommandsTest.java:
Integrate configuration of TextModificator singleton into test, for
synchronization purposes. Slight tweak of testMoveUp() test.
SharedTestResources.java:
Added opinion comment on why tests were doomed prior to changes :)
Made changes to support configuration and management of TextModificator
singleton in the TextModificator class.
TextModificatorTest.java:
Integrate configuration of TextModificator singleton into test, for
synchronization purposes. Added unit-test for
TextModificator.getNextWord() method. Added prototypes for unit tests to
be implemented next.
Brian
Michael Bartl wrote:
>On Tue, Dec 02, 2003 at 11:02:17AM -0800, Brian M. Kelley wrote:
>
>>Hello. Just wanted to drop a line to your team and thank you
>>for starting up this project - not having vi functionality
>>in eclipse, up until viPlugin was started, was my main
>>reason for not using the entire IDE.
>>
>>I'm also interested in taking on the job of unit test
>>developer. I'm a professional developer by day, with 5 years
>>of Java programming experience (I've been programming for
>>17+ years). I work directly with the JUnit test framework
>>every day for our products. If you need help in that space,
>>please let me know - I'd be glad to put some time in for the
>>project.
>
>
>Hello!
>
>This sounds great! I hope you can help out with the JUnit test, because
>I really want the viPlugin to be as stable and bugfree as possible. We
>already have a nice userbase and I don't want to disappoint them.
>
>Just checkout CVS Head, start hacking around on the testsuite and send
>me a patch. After 3 patches I usualy grant CVS access ;)
>
>Have fun,
> Michael
>
Index: src/viPlugin/TextModificator.java
===================================================================
retrieving revision 1.39
diff -u -r1.39 TextModificator.java
--- src/viPlugin/TextModificator.java 6 Dec 2003 11:42:57 -0000 1.39
+++ src/viPlugin/TextModificator.java 8 Dec 2003 07:20:32 -0000
@@ -28,9 +28,9 @@
* @version $Revision: 1.39 $ $Date: 2003/12/06 11:42:57 $
*/
public class TextModificator {
-
- private static TextModificator _currTextModificator;
-
+
+ private static TextModificator _currTextModificator;
+
private ITextEditor _textEditor;
private ITextViewer _textViewer;
private IDocument _document;
@@ -55,29 +55,30 @@
* @param textEditor text editor
* @param textViewer text viewer
*/
- TextModificator(ITextEditor textEditor, ITextViewer textViewer) {
+ public TextModificator(ITextEditor textEditor, ITextViewer textViewer) {
_textEditor = textEditor;
_textViewer = textViewer;
_yankBuffer = new YankBuffer("<default>");
}
+
+ public static void activateInstance(TextModificator textModificator) {
+ _currTextModificator = textModificator;
+ }
- public static void activateInstance(TextModificator textModificator) {
- _currTextModificator = textModificator;
- }
-
- public static TextModificator getInstance() {
- return _currTextModificator;
- }
+ public static TextModificator getInstance() {
+ return _currTextModificator;
+ }
- /**
- * This method servers <b>only</b> for testing purposes. Should be used
- * nowhere else.
- */
- public static void setEmptyInstance() {
+ /**
+ * This method servers <b>only</b> for testing purposes. Should be used
+ * nowhere else.
+ */
+ public static void setEmptyInstance(IDocument doc) {
activateInstance(new TextModificator(null, null));
- _currTextModificator.setSelection(new TextSelection(0, 0));
- }
-
+ _currTextModificator.setDocument(doc);
+ _currTextModificator.setSelection(new TextSelection(doc, 0, 0));
+ }
+
public ITextEditor getTextEditor() {
return _textEditor;
}
@@ -223,8 +224,8 @@
public int getCarretPosition() {
// needed for JUnit Testsuite
-// if (_visualAnchor != -1 || editor == null) {
-// return _cursorPos;
+// if (_visualAnchor != -1 || _textEditor == null) {
+// return getSelection().getOffset();
// }
if (_isInVisualMode
&& (_visualAnchor == getSelection().getOffset())) {
@@ -364,6 +365,12 @@
int endIndex = text.length();
int newStartIndex = startIndex;
+ if (text == null)
+ return 0;
+
+ if (text.length() == 0)
+ return 0;
+
// current character is already an end character
// now jump until we find the last end character!! (vim behaviour)
if (isWordEnding(text.charAt(startIndex), false)) {
@@ -395,14 +402,20 @@
}
// next word starts after all spaces
- if (text.charAt(endIndex) == ' ') {
- for (int i = endIndex; i < text.length(); ++i) {
- char c = text.charAt(i);
- if (isWordEnding(c, false) || Character.isLetter(c)) {
- return i;
- }
- }
- }
+ if (endIndex < text.length())
+ {
+ if (text.charAt(endIndex) == ' ') {
+ for (int i = endIndex; i < text.length(); ++i) {
+ char c = text.charAt(i);
+ if (isWordEnding(c, false) || Character.isLetter(c)) {
+ return i;
+ }
+ }
+ }
+ }
+ else
+ --endIndex;
return endIndex;
}
@@ -876,7 +889,7 @@
/**
* Tells the TextModificator that the visual mode was activated.
*/
- void activateVisualMode() {
+ public void activateVisualMode() {
_visualAnchor = getCarretPosition();
_isInVisualMode = true;
}
@@ -888,7 +901,8 @@
_isInVisualMode = false;
}
- void refreshVisualSelection(int newPosition) {
+ void refreshVisualSelection(int newPosition)
+ {
newVisualSelection(
_visualAnchor < newPosition ? _visualAnchor : newPosition,
Math.abs(newPosition - _visualAnchor));
@@ -922,4 +936,4 @@
IRegion line = getLine();
return getText(line.getOffset(), line.getLength());
}
-}
+}
\ No newline at end of file
Index: src/viPlugin/test/AllTests.java
===================================================================
retrieving revision 1.3
diff -u -r1.3 AllTests.java
--- src/viPlugin/test/AllTests.java 26 Nov 2003 18:20:54 -0000 1.3
+++ src/viPlugin/test/AllTests.java 8 Dec 2003 07:20:35 -0000
@@ -11,16 +11,17 @@
*/
public class AllTests {
- public static void main(String[] args) {
- junit.textui.TestRunner.run(AllTests.class);
- }
+ public static void main(String[] args) {
+ junit.textui.TestRunner.run(AllTests.class);
+ }
- public static Test suite() {
- TestSuite suite = new TestSuite("Test for viPlugin.test");
- //$JUnit-BEGIN$
- suite.addTestSuite(MotionCommandsTest.class);
- suite.addTestSuite(DeleteCommandsTest.class);
- //$JUnit-END$
- return suite;
- }
+ public static Test suite() {
+ TestSuite suite = new TestSuite("Test for viPlugin.test");
+ //$JUnit-BEGIN$
+ suite.addTestSuite(TextModificatorTest.class);
+ suite.addTestSuite(DeleteCommandsTest.class);
+ suite.addTestSuite(MotionCommandsTest.class);
+ //$JUnit-END$
+ return suite;
+ }
}
Index: src/viPlugin/test/DeleteCommandsTest.java
===================================================================
retrieving revision 1.3
diff -u -r1.3 DeleteCommandsTest.java
--- src/viPlugin/test/DeleteCommandsTest.java 4 Dec 2003 09:12:58 -0000 1.3
+++ src/viPlugin/test/DeleteCommandsTest.java 8 Dec 2003 07:20:35 -0000
@@ -14,9 +14,13 @@
*/
public class DeleteCommandsTest extends TestCase {
- private final int _numberOfLines;
- private final TextModificator _tm;
-
+ private TextModificator t;
+ private final String _documentContent =
+ "1. first second, third, ..., last\r\n"
+ + "2. ffirst, ssecond, tthird, ..., llast\r\n"
+ + "3. ffirst, ssecond, tthird, ..., llast\r\n"
+ + "4. ffirst, ssecond, tthird, ..., llast";
+
/**
* Constructor.
*
@@ -24,24 +28,24 @@
*/
public DeleteCommandsTest(String name) {
super(name);
- _tm = SharedTestResources.getTextModificator();
- _numberOfLines = SharedTestResources.getNumberOfLines();
}
+ protected void setUp() throws Exception
+ {
+ SharedTestResources.setConfiguredTextModificator(_documentContent);
+ t = TextModificator.getInstance();
+ }
+
public void testDeleteWordForward() {
Command goToFirstLine = new GoToFirstLine();
goToFirstLine.execute();
Command dw = new DeleteWordForward(1);
dw.execute();
- assertEquals(". first second, third, ..., last", _tm.getLineText());
- dw.undo();
- assertEquals("1. first second, third, ..., last", _tm.getLineText());
-
+ assertEquals(". first second, third, ..., last", t.getLineText());
dw.undo();
+ assertEquals("1. first second, third, ..., last", t.getLineText());
Command dw3 = new DeleteWordForward(3);
dw3.execute();
- assertEquals("second, third, ..., last", _tm.getLineText());
- dw3.undo();
- assertEquals("1. first second, third, ..., last", _tm.getLineText());
- }
+ assertEquals("second, third, ..., last", t.getLineText());
dw3.undo();
+ assertEquals("1. first second, third, ..., last", t.getLineText());
}
}
Index: src/viPlugin/test/MotionCommandsTest.java
===================================================================
retrieving revision 1.3
diff -u -r1.3 MotionCommandsTest.java
--- src/viPlugin/test/MotionCommandsTest.java 26 Nov 2003 19:01:57 -0000 1.3
+++ src/viPlugin/test/MotionCommandsTest.java 8 Dec 2003 07:20:35 -0000
@@ -15,10 +15,13 @@
* @version $Revision: 1.3 $ $Date: 2003/11/26 19:01:57 $
*/
public class MotionCommandsTest extends TestCase {
-
- private final int _numberOfLines;
- private final TextModificator _tm;
-
+ private TextModificator t;
+ private final String _documentContent =
+ "1. first second, third, ..., last\r\n"
+ + "2. ffirst, ssecond, tthird, ..., llast\r\n"
+ + "3. ffirst, ssecond, tthird, ..., llast\r\n"
+ + "4. ffirst, ssecond, tthird, ..., llast";
+
/**
* Constructor.
*
@@ -26,60 +29,63 @@
*/
public MotionCommandsTest(String name) {
super(name);
- _tm = SharedTestResources.getTextModificator();
- _numberOfLines = SharedTestResources.getNumberOfLines();
}
-
+
+ protected void setUp() throws Exception
+ {
+ SharedTestResources.setConfiguredTextModificator(_documentContent);
+ t = TextModificator.getInstance();
+ }
+
public void testMoveDown() {
// move to the first line
Command goToFirstLine = new GoToFirstLine();
goToFirstLine.execute();
- int pos = _tm.getLinePos();
+ int pos = t.getLinePos();
assertEquals(pos, 0);
// move down two lines
Command moveDown = new MoveDown(2);
moveDown.execute();
pos += 2;
- assertEquals(pos, _tm.getLinePos());
+ assertEquals(pos, t.getLinePos());
// paranoiac check
Command moveDown1 = new MoveDown(1);
boolean isLastLine =
- (_tm.getLinePos() + 1) == _tm.getDocument().getNumberOfLines();
+ (t.getLinePos() + 1) == t.getDocument().getNumberOfLines();
moveDown1.execute();
if (isLastLine) {
- assertEquals(pos, _tm.getLinePos());
+ assertEquals(pos, t.getLinePos());
} else {
- assertEquals(++pos, _tm.getLinePos());
+ assertEquals(++pos, t.getLinePos());
}
// move another 100000 lines down
Command moveDown100k = new MoveDown(100000);
moveDown100k.execute();
- assertEquals(_numberOfLines - 1, _tm.getLinePos());
-
- assertEquals("4. ffirst, ssecond, tthird, ..., llast", _tm.getLineText());
+ assertEquals(t.getDocument().getNumberOfLines() - 1, t.getLinePos());
+ assertEquals("4. ffirst, ssecond, tthird, ..., llast",
t.getLineText());
}
public void testMoveUp() {
// move to the last line
Command goToLastLine = new GoToLastLine();
goToLastLine.execute();
- int pos = _tm.getLinePos();
- assertEquals(pos, _numberOfLines - 1);
-
- // move 3 lines up
- Command moveUp3 = new MoveUp(3);
- moveUp3.execute();
- pos -= 3;
- assertEquals(pos, _tm.getLinePos());
+ int pos = t.getLinePos();
+ assertEquals(pos, t.getDocument().getNumberOfLines() - 1);
+ assertEquals("4. ffirst, ssecond, tthird, ..., llast",
t.getLineText());
+
+ // move 2 lines up
+ Command moveUp2 = new MoveUp(2);
+ moveUp2.execute();
+ pos -= 2;
+ assertEquals(pos, t.getLinePos());
// move another 100000 lines up
Command moveUp100k = new MoveUp(100000);
moveUp100k.execute();
- assertEquals(0, _tm.getLinePos());
-
- assertEquals("1. first second, third, ..., last", _tm.getLineText());
+ assertEquals(0, t.getLinePos());
+ assertEquals("1. first second, third, ..., last", t.getLineText());
}
}
Index: src/viPlugin/test/SharedTestResources.java
===================================================================
retrieving revision 1.1
diff -u -r1.1 SharedTestResources.java
--- src/viPlugin/test/SharedTestResources.java 26 Nov 2003 19:01:40 -0000 1.1
+++ src/viPlugin/test/SharedTestResources.java 8 Dec 2003 07:20:35 -0000
@@ -10,36 +10,35 @@
* design of TextModificator class this class is needed if we want to run all
* tests at once (AllTests class). Try it to know why. If you found out a safer
* style than a Singleton, refactore tests.
+ *
+ * BMK: Strangely enough, the problem with letting this class manage a
+ * TextModificator singleton is that the singleton's consituent fields
* become out of sync with the TextModificator singleton that is utilized
* by other, non-test classes (e.g. Command class derivatives). Is this a
* strange JUnit quirk? I wasn't aware of any parallelism in the Eclipse/JUnit
* combo that would result in this kind of behavior...
+ *
+ * Anyway, the solution is to let the singleton be managed in the actual
+ * TextModificator class as usual, and let the tests configure and acquire
+ * the TextModifcator singleton in the JUnit setUp() methods.
*
* @author <a href="mailto:em...@se...">Martin Krauskopf</a>
* @version $Revision: 1.1 $ $Date: 2003/11/26 19:01:40 $
*/
class SharedTestResources {
-
- private static final String _documentContent =
- "1. first second, third, ..., last\r\n"
- + "2. ffirst, ssecond, tthird, ..., llast\r\n"
- + "3. ffirst, ssecond, tthird, ..., llast\r\n"
- + "4. ffirst, ssecond, tthird, ..., llast";
- private static IDocument doc = new Document(_documentContent);
- private static final int _numberOfLines = 4;
- private static TextModificator _tm;
-
- /**
- * Returns singleton.
- *
- * @return TextModificator
- */
- public static synchronized TextModificator getTextModificator() {
- if (_tm == null) {
- TextModificator.setEmptyInstance();
- _tm = TextModificator.getInstance();
- _tm.setDocument(doc);
- }
- return _tm;
- }
+ private static IDocument doc;
+
+ /**
+ * Return a configured TextModificator.
+ * @param content the string that the returned TextModificator will
+ * represent.
+ * @param numLines the number of logical lines in the text represented
+ * by the returned TextModificator.
+ * @return a configured TextModificator singleton.
+ */
+ public static void setConfiguredTextModificator(String content)
+ {
+ doc = new Document(content);
+ TextModificator.setEmptyInstance(doc);
+ }
public static int getNumberOfLines() {
- return _numberOfLines;
+ return doc.getNumberOfLines();
}
}
Index: src/viPlugin/test/TextModificatorTest.java
===================================================================
retrieving revision 1.15
diff -u -r1.15 TextModificatorTest.java
--- src/viPlugin/test/TextModificatorTest.java 26 Nov 2003 18:07:38 -0000 1.15
+++ src/viPlugin/test/TextModificatorTest.java 8 Dec 2003 07:20:36 -0000
@@ -4,48 +4,45 @@
* are made available under the terms of the Common Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/cpl-v10.html
- *
+ *
* Created on 26.05.2003
*******************************************************************************/
package viPlugin.test;
import junit.framework.TestCase;
-
-import org.eclipse.jface.text.Document;
-
import viPlugin.CommandParser;
import viPlugin.TextModificator;
+import org.eclipse.jface.text.IRegion;
/**
* @author Michael Bartl
*/
public class TextModificatorTest extends TestCase {
-
+ private TextModificator t;
private final String simpleString =
"This is a simple.three() line>string ( ) become\r\n"
+ "To test the functionality of\r\n"
+ "the TextModification class";
- private TextModificator tm;
- private Document doc;
-
/**
* Constructor for TextModificatorTest.
* @param arg0
*/
public TextModificatorTest(String arg0) {
super(arg0);
-// tm = TextModificator.getEmptyInstance();
- doc = new Document(simpleString);
- tm.setDocument(doc);
- tm.newVisualSelection(0, 0);
- }
+ }
public static void main(String[] args) {
junit.swingui.TestRunner.run(TextModificatorTest.class);
}
+ protected void setUp() throws Exception
+ {
+ SharedTestResources.setConfiguredTextModificator(simpleString);
+ t = TextModificator.getInstance();
+ }
+
final public void testCommandParser() {
CommandParser parser = new CommandParser();
parser.parse("cc");
@@ -83,17 +80,18 @@
}
final public void testCursorLeft() {
- tm.cursorToLineEnd();
- int cursorPos = tm.cursorLeft(3);
+ t.cursorToLineEnd();
+ int cursorPos = t.cursorLeft(3);
assertEquals(simpleString.indexOf("ome"), cursorPos);
- cursorPos = tm.cursorLeft(100);
+ cursorPos = t.cursorLeft(100);
+ IRegion line = t.getLine();
assertEquals(0, cursorPos);
}
final public void testCursorRight() {
- int cursorPos = tm.cursorRight(3);
+ int cursorPos = t.cursorRight(3);
assertEquals(simpleString.indexOf("s is a"), cursorPos);
- cursorPos = tm.cursorRight(100);
+ cursorPos = t.cursorRight(100);
assertEquals(simpleString.indexOf("\r\nTo test"), cursorPos);
}
@@ -113,15 +111,15 @@
// }
final public void testCursorToLine() {
- int cursorPos = tm.cursorToLine(0, true);
+ int cursorPos = t.cursorToLine(0, true);
assertEquals(0, cursorPos);
- cursorPos = tm.cursorToLine(1, true);
+ cursorPos = t.cursorToLine(1, true);
assertEquals(simpleString.indexOf("To test"), cursorPos);
- cursorPos = tm.cursorToLine(3, true);
+ cursorPos = t.cursorToLine(3, true);
assertEquals(
simpleString.indexOf("the TextModification class"),
cursorPos);
- cursorPos = tm.cursorToLine(100, true);
+ cursorPos = t.cursorToLine(100, true);
assertEquals(
simpleString.indexOf("the TextModification class"),
cursorPos);
@@ -165,25 +163,25 @@
}
final public void testCursorWordEnd() {
- int cursorPos = tm.cursorWordEnd(2);
+ int cursorPos = t.cursorWordEnd(2);
assertEquals(simpleString.indexOf("s a simple"), cursorPos);
- cursorPos = tm.cursorWordEnd(1);
+ cursorPos = t.cursorWordEnd(1);
assertEquals(simpleString.indexOf("a simple.three()"), cursorPos);
- cursorPos = tm.cursorWordEnd(1);
+ cursorPos = t.cursorWordEnd(1);
assertEquals(simpleString.indexOf("e.three()"), cursorPos);
- cursorPos = tm.cursorWordEnd(1);
+ cursorPos = t.cursorWordEnd(1);
assertEquals(simpleString.indexOf(".three()"), cursorPos);
- cursorPos = tm.cursorWordEnd(1);
+ cursorPos = t.cursorWordEnd(1);
assertEquals(simpleString.indexOf("e() line"), cursorPos);
- cursorPos = tm.cursorWordEnd(1);
+ cursorPos = t.cursorWordEnd(1);
assertEquals(simpleString.indexOf(") line"), cursorPos);
- cursorPos = tm.cursorWordEnd(1);
+ cursorPos = t.cursorWordEnd(1);
assertEquals(simpleString.indexOf("e>string"), cursorPos);
- cursorPos = tm.cursorWordEnd(2);
+ cursorPos = t.cursorWordEnd(2);
assertEquals(simpleString.indexOf("g ( ) become"), cursorPos);
- cursorPos = tm.cursorWordEnd(1);
+ cursorPos = t.cursorWordEnd(1);
assertEquals(simpleString.indexOf("( ) become"), cursorPos);
- cursorPos = tm.cursorWordEnd(1);
+ cursorPos = t.cursorWordEnd(1);
assertEquals(simpleString.indexOf(") become"), cursorPos);
}
@@ -256,4 +254,60 @@
//TODO Implement searchCurrentWordBackwards().
}
+ /**
+ * Tests the functionality of the getNextWord() method.
+ * @throws Exception if there was a problem running the test.
+ */
+ final public void testGetNextWord() throws Exception
+ {
+ // Test single word endings
+ String s0 = "Everyone thinks of changing the world, but no " +
+ "one thinks of changing himself.";
+ int wordSum = 0;
+ int wordIdx = 0;
+ do
+ {
+ wordIdx = t.getNextWord(s0, wordIdx);
+ ++wordSum;
+ } while (wordIdx < s0.length() - 1);
+ assertEquals(14, wordSum);
+
+ // Test multiple series of word endings
+ String s1 = "f(x) = mx + b";
+ wordSum = 0;
+ wordIdx = 0;
+ do
+ {
+ wordIdx = t.getNextWord(s1, wordIdx);
+ ++wordSum;
+ } while (wordIdx < s1.length() - 1);
+ assertEquals(7, wordSum);
+
+ // Test no word endings
+ String s2 = "Supercalifragalisticexpialadocious";
+ assertEquals(s2.length() - 1, t.getNextWord(s2, 0));
+ // Test empty string behavior
+ String s3 = "";
+ assertEquals(0, t.getNextWord(s3, 0));
+ }
+
+
+ /**
+ * Tests the functionality of the getPreviousWord() method.
+ * @throws Exception if there was a problem running the test.
+ */
+ final public void testGetPreviousWord() throws Exception
+ {
+ // TODO: Implement test.
+ }
+
+
+ /**
+ * Tests the functionality of the cursorToLineEnd() method.
+ * @throws Exception if there was a problem running the test.
+ */
+ final public void testCursorToLineEnd() throws Exception
+ {
+ // TODO: Implement test.
+ }
}
----- End forwarded message -----
|