It Works - Now Let s Test It


It Works ”Now Let s Test It

We may be evil enough to figure out how to make something work ”and in fact actually make it work ”without writing the customer test, but we re certainly not going to skip the customer test altogether. We ve gotten in trouble like that before, and this kind of situation where we drive the GUI is exactly the trouble spot. So we looked for a test to copy or improve, and we found the InsertPre.test file:

 *input 
<P>This is the first paragraph.</P>
<P>This is the second paragraph.</P>
*end
*altP
*output
<P>This is the first paragraph.</P>
<pre></pre>
<P>This is the second paragraph.</P>

Perfect! We ll just add a second bit to this test, to test the Shift+Enter:

 *input 
<P>This is the first paragraph.</P>
<P>This is the second paragraph.</P>
*end
*altP
*output
<P>This is the first paragraph.</P>
<pre></pre>
<P>This is the second paragraph.</P> *shiftEnter *output < P > This is the first paragraph. < /P > < pre > < /pre > < P > This is the second paragraph. < /P >

We expect this test to fail, sort of, and it does. This is all well and good, except that the test failed in a really odd way. It seemed to be failing on the first *output section, comparing a perfectly good result against an example that also includes *shiftEnter, and a number of other lines. Chet immediately saw the problem: The *output command must read all the way to the end. I was sure that it didn t, that it stopped at any line that begins with * . We discussed it for only a moment or two before looking. Here s the code and the answer:

 private String ExpectedOutput(StringReader reader) { 
return ReadToEnd(reader);
}
private String ReadToEnd(StringReader reader) {
String result = "";
String line = reader.ReadLine();
while (line != null && line != "*end") {
result += line;
result += System.Environment.NewLine;
line = reader.ReadLine();
}
return result;
}

Ah. Both wrong. It s reading to "*end". Chet tried vainly to convince me that was what he meant , but I knew better. Anyway, we changed the test. Note the *end:

 *input 
<P>This is the first paragraph.</P>
<P>This is the second paragraph.</P>
*end
*altP
*output
<P>This is the first paragraph.</P>
<pre></pre>
<P>This is the second paragraph.</P>
*end
*shiftEnter
*output
<P>This is the first paragraph.</P>
<pre>
</pre>
<P>This is the second paragraph.</P>
Lesson  

There s a small but important lesson here. Speculate about the code only a little and then read the code. The code knows what it does; we can only guess.

Also, you might be wondering, after we split the Shift+Enter test out in the Programmer Unit Tests, why we put these two tests together in the Customer Acceptance Tests. In the programmer tests, the lack of modularity had left us confused . We generally think of the customer tests as providing assurance to the customer that things are going well, but we don t think of them as providing debugging information to the programmers. Frankly, we could be wrong about this: it could have been sheer laziness that kept us from producing a separate customer test file.

The test fails, as we expect. Why? Because the *shiftEnter line isn t recognized. The CustomerTest command interpreter just ignores commands that it doesn t know. (This could be a problem, but so far it hasn t been.) So now we have to implement the *shiftEnter in the customer test code:

 private void InterpretCommands(String commands, String message) { 
StringReader reader = new StringReader(commands);
String line = reader.ReadLine();
CreateModel(); while ( line != null) {
if ( line == "*enter")
form.XMLKeyDownHandler((object) this, new KeyEventArgs(Keys.Enter));
if ( line == "*shiftEnter")
form.XMLKeyDownHandler((object) this,
new KeyEventArgs(Keys.Enter Keys.Shift));
if ( line == "*altS")
ExecuteMenu("&S");
if ( line == "*altP")
ExecuteMenu("&P");
if (line == "*display")
Console.WriteLine("display\r\n{0}\r\nend", model.TestText);
if (line == "*output")
CompareOutput(reader, message);
if (line == "*input")
SetInput(reader);
line = reader.ReadLine();
}
}

Note the new item there, with the "*shiftEnter". We took a moment to figure out how to create the right keystroke but then just called the handler like we did the *enter command. Now the test runs.




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