ShiftEnter Test


Shift+Enter Test

We ve already got a test for the <pre> tag:

 [Test] public void InsertPre() { 
model.SetLines (new String[1] {"<P></P>"});
model.SelectionStart = 7;
model.InsertPreTag();
AssertEquals("<pre></pre>", model.Lines[1]);
AssertEquals(14, model.SelectionStart);
}

My first inclination is just to enhance this test by doing the shift enter action. This raises a question for the customer: should Shift+Enter work all the time or just inside the <pre> tags? The answer is that it s harmless elsewhere but might be nice for making the textbox more readable, so we ll let it work anywhere . With that definition, I might have to write more tests to be sure that it works, but I don t think so. Here s our enhanced test including Shift+Enter:

 [Test] public void InsertPre() { 
model.SetLines (new String[1] {"<P></P>"});
model.SelectionStart = 7;
model.InsertPreTag();
AssertEquals("<pre></pre>", model.Lines[1]);
AssertEquals(14, model.SelectionStart);
model.InsertReturn();
AssertEquals(" < pre > ", model.Lines[1]);
AssertEquals("", model.Lines[2]);
AssertEquals(" < /pre > ", model.Lines[3]); AssertEquals(16, model.SelectionStart);
}

I m assuming here that the model action will be called InsertReturn and that the GUI action will be the typing of the Shift+Enter character. The naming is a bit awkward here because the user types the Enter key, but I m thinking about inserting a carriage return character into the lines. Also notice that we ve not been entirely orderly about which way we name commands: some of the model methods have had names that sound like keystrokes. We ll take a look and see if any of that needs cleaning up; it s certainly best if the model thinks in terms of what it does, not how what it does is invoked. For this case, this way seems best to me.

We build, to get the message that InsertReturn isn t implemented on TextModel. We knew that, of course. You might prefer to fix it first; I prefer to let the computer guide me.

Lesson  

There may actually be a lesson here. Recall that Watts Humphrey recommends serious desk-checking before compiling or fixing a test, and although we wanted to try his recommendation, Chet and I haven t been able to bring ourselves to do it. The issue might be that we are really combining our thoughts with the thoughts of the computer in a substantially different way from what Watts suggests. I believe that by letting the computer deal with some of the more trivial matters, we may be freeing up our thoughts for bigger issues.

Of course, it s possible that I m kidding myself , but I m sure that things go more smoothly for me than ever before when I use this simple test/code/refactor cycle. It s working for me!

False Start Number One

To implement InsertReturn, we review other insert methods in TextModel. Here s one and its helper methods and data:

 private static string[] newSection = {"<sect1><title></title>","</sect1>" }; 
private static string sectionSkip = "<sect1><title>";

public void InsertSectionTags() {
InsertTags(newSection, sectionSkip);
}
private void InsertTags(string[] tagsToInsert, string tagsPrecedingCursor) {
int cursorLine = LineContainingCursor();
lines.InsertRange(cursorLine+1, tagsToInsert);
selectionStart = NewSelectionStart(cursorLine + 1, tagsPrecedingCursor);
}

This seems custom-made for our purposes, except of course that in our case we aren t going to insert much of anything. Still, it ought to work, and if it doesn t, we should make it work, to avoid duplicating any logic.

The InsertTags method is simple enough: it inserts a new line containing the tagsToInsert input array and then sets the selection based on skipping the tagsPrecedingCursor string. It seems like we should be able to do what we want with a couple of empty strings. Let s try it. Here s my attempted code:

 private static string[] emptyLine = { "" }; 
private static string emptyLineSkip = "";

public void InsertReturn() {
InsertTags(emptyLine, emptyLineSkip);
}

Oops! That doesn t work. The test fails, with this message: expected ˜<pre> but was ˜<pre></pre> . Silly me. It isn t enough to insert a new line after the current line: the Shift+Enter has to insert a new line right in the middle of the current line, wherever the cursor is. So this is actually going to amount to replacing a line in our terms, from

 1: <pre></pre> 

to this:

 1: <pre> 
2:
3: </pre>

Our InsertTags feature may come in there, but there s going to be more to it than that. I m going to program this one by intention , because it feels a little tricky to me and I m working without a pair.

False Start Number Two

I coded the following, which isn t exactly by intention. In fact, it isn t much of anything:

 public void InsertReturn() { 
int cursorLine = LineContainingCursor();
string cursorString = (string) lines[cursorLine];
int lineCursorPosition = LineCursorPosition();
string front = cursorString.Substring(0, lineCursorPosition);
string back = cursorString.Substring(lineCursorPosition+1);
lines[cursorLine] = front;
string[] linesToInsert = { "", back };
InsertTags(linesToInsert, "");
}

Frankly, this is pretty awful code. Worse yet, it s based on an incorrect plan, and even if it worked, and even if it was clear, it would still be wrong. We don t want to add a blank line, as shown in that 1, 2, 3 thing in the last section. We just want to insert a return, but no blank line, to get this:

 1: <pre> 
2: </pre>
Lesson  

What happened here? Two attempts and they both fail. The only good thing we can say is that because I m working in such small steps, there s not much harm done before I discover I m all wet. The lesson: if you re prone to failure, be sure to fail early and often. Another lesson might be that when you set out to program by intention, don t write ugly procedural code instead!

If I had had a pair, maybe the bad plan would have been recognized, or maybe my pair would have seen how awful that code is and stopped me. Neither happened, but I did get lucky. I got a phone call, interrupting my train of thought, and then I decided to change locations from one restaurant to another. That let me think a little more clearly about what has to happen.




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