The Actual Work

Enough warmup, let s code. We ll do...flip a coin...the UL tag, for Unordered List. The first challenge, of course, is whether to write a test. I find it easy to take the simple copy and paste approach. I feel resistance to writing a test, especially when, like this time, I know just what I need to do to make things work. At least I think I know. I want to code. But my readers are watching, so even though I don t have a pair to prod me this morning, let s at least look at some related tests in TestTextModel.cs to check whether we can borrow anything. We find two tests for the InsertSection code:

 [Test] public void AltS() { 
model.Lines = new ArrayList(new String[0]);
AssertEquals("<sect1><title></title>", model.Lines[0]);
AssertEquals("</sect1>", model.Lines[1]);
AssertEquals(14, model.SelectionStart);
[Test] public void AltSWithText() {
model.SetLines (new String[1] {"<P></P>"});
model.SelectionStart = 7;
AssertEquals("<sect1><title></title>", model.Lines[1]);
AssertEquals("</sect1>", model.Lines[2]);
AssertEquals(23, model.SelectionStart);

The first one is pretty dull, but the second one is a bit more challenging. Let s be good and write a test for the LI tag. Here goes:

 [Test] public void UnorderedList() { 
model.SetLines (new String[1] {"<P></P>"});
model.SelectionStart = 3;
AssertEquals("<UL>", model.Lines[1]);
AssertEquals("<LI></LI>", model.Lines[2]);
AssertEquals("</UL>", model.Lines[3]);
AssertEquals(19, model.SelectionStart);

I m not sure about the 19. I always have this problem counting out where the cursor will have to go. This may be calling for one of those tools I was talking about. One possibility is that instead of writing this test, I should work with a customer test instead. I ll have to write one anyway, pretty soon now. Or maybe there should be some blend of the customer test and programmer test technology. For now, our mission is to implement this feature. We ll make a note to do these other things. We ll compile the test, get the error on InsertUnorderedList, and proceed from there. My first attempt looked like this. Bear with me on the ugly string for a moment, please :

 class TextModel { 
private static string[] newParagraph = { "<P></P>" };
private static string paragraphSkip = "<P>";
private static string[] newSection = {"<sect1><title></title>","</sect1>" };
private static string sectionSkip = "<sect1><title>";
private static string[] newUnorderedList = {" < UL > "," < LI >< /LI > "," < /UL > "};
private static string unorderedListSkip = @"<UL><LI>>";
private static string[] newPre = { "<pre></pre>" };
private static string preSkip = "<pre>";
public void InsertUnorderedList() {
InsertTags(newUnorderedList, unorderedListSkip);

It took me a moment to remember that the newUnorderedList should contain a string for each line to be inserted. The unorderedListSkip is a string used to calculate where the cursor goes. That code looks like this:

  private void InsertTags(string[] tagsToInsert, string tagsPrecedingCursor) {  
int cursorLine = LineContainingCursor();
lines.InsertRange(cursorLine+1, tagsToInsert);
selectionStart = NewSelectionStart(cursorLine + 1, tagsPrecedingCursor);
private int NewSelectionStart(int cursorLine, string tags) {
return FirstPositionOfLine(cursorLine) + tags.Length;

Note that the tagsPrecedingCursor variable is just checked for length, so I had to put the return in the tags. I could have used \r\n, but I have been trying to avoid that because I m worried about systems where the return isn t set to Microsoft Windows style. I suspect that has been a waste of my time ”a perfect example of YAGNI perhaps.

The test doesn t quite run. It returns 20, not 19. Let me recount the characters again: I m still getting 19. I ll have to run the program. This is bad; the tests aren t helping me enough. That has to be fixed, but first let s get green. My first instinct is to run the program, but since we haven t implemented the new menu yet, we can t do that. One way to go is just to do the menu. Is there a quicker way? Well, how about a look at the code? Hah, got it? See the extra > in unorderedListSkip? That s the problem. I ll remove it and we should go green...and we do.


I m going to take a short break in celebration , but that ugly string has some explaining to do. I m sure it is implicated in my mistake. This part of the process wasn t as quick as it should have been, so it s time to improve my tools a bit, when I get back.

Extreme Programming Adventures in C#
Javaв„ў EE 5 Tutorial, The (3rd Edition)
ISBN: 735619492
EAN: 2147483647
Year: 2006
Pages: 291 © 2008-2017.
If you may any questions please contact us: