Enter the List Item


It s time to get the Enter key to put a ListItem in for us. I was thinking for a moment that we might want that to happen in other places and that perhaps we should just have whatever tag we re inside be replicated. But that s not right: when we re inside a <title> tag, we don t want a new title, we want a paragraph. So I ll curb my DOGBITE instincts for a while, until the code tells me what we need. Here s the code that creates the paragraph tags on Enter, in TextModel:

 public void Enter() { 
InsertParagraphTag();
}
public void InsertParagraphTag() {
InsertTags(newParagraph, paragraphSkip);
}

Not much to that. Do you remember, earlier on, when we were worrying about whether the TextModel should understand Enter or whether the Form should know to do the translation from Enter to InsertParagraphTag? We decided to let the form keep doing Enter. That makes us happy now, because we can do everything we need to do right here in the TextModel. Waiting paid off that time. It usually does, but this time we happened to notice. I ll program the new feature by intention :

 public void Enter() { 
if (InListItem())
InsertListItemTag();
else
InsertParagraphTag();
}

That seems clear enough. I would do more, but I just realized that I don t have a broken test. Bad programmer, bad. I ll comment out the above change and then enhance the existing test:

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

That gives us the failure we need: we get a P line instead of an LI line. The test needs a cursor count as well. I m putting off adding that, since I always get them wrong. Remind me about that in a little while. Here s what I write next :

 private Boolean InListItem() { 
return ((string) lines[LineContainingCursor()]).StartsWith("<LI>");
}
public void InsertListItemTag() {
InsertTags(newListItem, listItemSkip);
}

Two things about that. Look at that horrible string cast and access to the lines array. It turns out that we use that construction in several places. As soon as this works, I m going to improve that. But first, I have to write those new tag variables :

 private static string[] newListItem = { "<LI></LI>" }; 
private static string[] listItemSkip = { "<LI>" };

Excellent! The test runs! We might want to write another customer test, or extend this one, but basically we have completed our first story. It s time to reflect and to do some cleanup.




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