Javaв„ў EE 5 Tutorial, The (3rd Edition)

All right, the plan is to add a test to TestTextModel, at least if it doesn t look too hard. Take another look at that class earlier in the Test Text Model section. Check out that ControlTwo test right there at the bottom. We need a test like that, except that it tests ControlS. Let s write it; it doesn t look too hard.

[Test] public void ControlS() { model.Lines = new ArrayList(new String[0]); model.ControlS(); AssertEquals("<sect1><title></title>", model.Lines[0]); AssertEquals("</sect1>", model.Lines[1]); }

Lesson  

Are you wondering why, with a perfectly good customer test, I m going to write a programmer test? I try to do it as a matter of discipline and good practice. On a real Extreme Programming project, the Customer Acceptance Tests would belong to the customer, and they might not come along in quite as timely a way as this one did. And as a programmer, my main responsibility is the Programmer Unit Tests. In addition, it s not common that customer tests are quite so well- targeted as this one. Usually when a customer test fails, there s some debugging to do. Adding all this up, when I find myself thinking that maybe I don t need a test, I try always to go ahead and write one anyway. It pays off more often than not.

Compiling, I find that TextModel has no ControlS method. No surprise. Let s write that; it ll be a lot like the Enter method or InsertParagraghTag, as I think it s called. Those methods look like this:

public void Enter() { InsertParagraphTag(); } public void InsertParagraphTag() { if ( lines.Count == 0 ) { lines.Add( "<P></P>" ); selectionStart = 3; return; } lines.InsertRange(LineContainingCursor()+1, NewParagraph()); // set cursor location selectionStart = NewSelectionStart(LineContainingCursor() + 2); }

I m going to begin in sb fake it before you make it mode and just add the lines directly, as we do in the first part of the method above. I expect this to be enough to make this test run, and I expect it to lead to some discoveries in the code. Here s my first cut at the new methods:

public void ControlS() { InsertSectionTags(); } public void InsertSectionTags() { if ( lines.Count == 0 ) { lines.Add( "<sect1><title></title>" ); lines.Add( "</sect1>"); selectionStart = 14; return; }

The code compiles, and both tests run, the new programmer test and the first customer test. It s time for a break! I ll see you after lunch !

Категории