Begin with a Test


I ve got Microsoft Visual Studio loaded up on the XML Notepad app, and I ve run the tests. They are green; we re good to go. I m envisioning a Programmer Unit Test for TextModel along these lines: Set the TextModel to some contents. Take a snapshot. Set the model to some other contents. Verify that they re there. Undo the snapshot, show that things are back as they were. Simple enough, and a step along the way. In TestTextModel, I find a test that looks like this:

 [Test] public void CursorPosition() { 
model.SetLines (new String[] { "<P></P>", "<pre></pre>" });
model.SelectionStart = 14; // after <pre>
AssertEquals(5, model.PositionOfCursorInLine());
AssertEquals("<pre>", model.FrontOfCursorLine());
AssertEquals("</pre>", model.BackOfCursorLine());
}

I ll use that concept to create a new test. I ll skip the use of legal tags, because they are hard to type in and don t really improve the quality of the test in this case:

 [Test] public void Snapshot() { 
model.SetLines(new String[] { "before", "snapshot" });
model.SelectionStart = 8; // after before ;->
AssertEquals("before", model.Lines[0]);
model.Snapshot();
model.SetLines(new String[] {"a", "new", "contents", "list"} );
model.SelectionStart = 4; // after n in new
AssertEquals("contents", model.Lines[2]);
model.Restore();
AssertEquals("before", model.Lines[0]);
AssertEquals(8, model.SelectionStart);
}
Lesson  

We ll see later on that this test is weak, in that it s not a good example of anything that s likely to happen in a real editing situation. It amounts to Copy All/Paste Something Entirely Different, which is not a common activity. The test will support us fairly well, but not as well as one that would have been more like real editing.

There is a bit of a lesson lurking here. All through this part of the book, we re going to be feeling a little pressure because it s difficult to set up the programmer tests. We have to count the location of the caret manually, and so on. We might have done a little better had we paid a bit more attention to making testing a bit easier.

Model doesn t understand Snapshot() and Restore(), as referred to in our new test, so first we implement them trivially to get the red bar:

 public void Snapshot() { 
}
public void Restore() {
}

No surprises ”we get the red bar, because the methods don t save or restore anything. But building the methods that way has given me an idea for an easy implementation. How about if we just save the text and selection start in a couple of new member variables :

  private ArrayList savedLines;  
private int savedSelectionStart;

public void Snapshot() {
savedLines = new ArrayList(lines);
savedSelectionStart = SelectionStart;
}
public void Restore() {
lines = savedLines;
SelectionStart = savedSelectionStart;
}

This actually makes the test run! Whee, we have implemented Undo. Not a very good Undo yet, but Undo nonetheless.

start sidebar
Lesson: Let s Reflect

This is a frightening story, so let s reflect early and often. What do we need to keep in mind? Well, first of all, I m a little worried about not retaining the selection length. In our current implementation we have never used the selection length for anything, but it seems possible that Undo should be saving and restoring it. I m not very worried, however, because the extension is pretty clear ”we ll just save it and restore it.

Second, we can foresee that as we build a stack of Undo operations, we will need a stack that contains the lines and one for the selections. These stacks will be in parallel. This calls for an object that combines the text lines and the selection information. Should we create such an object?

As soon as I thought this thought, I realized that there already is such an object! TextModel itself is an object that contains exactly those variables, and up until now, no other ones. Here s the head of TextModel class as it stands now, just as a reminder:

end sidebar
 
 class TextModel { 
private ArrayList lines;
private int selectionStart;
private ArrayList savedLines;
private int savedSelectionStart;
public TextModel() {
lines = new ArrayList();
}
...
}

What about just using the TextModel as our saved object? Wouldn t that be too big an object somehow? Well, no. It is true that the TextModel has a lot of methods that we might not use in the stacked items, but they won t take up any extra space. The TextModel instances have just exactly the storage we want: one ArrayList of lines, and the selection information. Let s give TextModel a new constructor and use it in our Snapshot() and Restore() methods.




Extreme Programming Adventures in C#
Javaв„ў EE 5 Tutorial, The (3rd Edition)
ISBN: 735619492
EAN: 2147483647
Year: 2006
Pages: 291

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net