Finding a Simple Pattern


Our mission in the grand scheme of things is to find XML tags in text. So our first test will just look for a paragraph tag, <p>, in a string.

 [Test] public void SimplePattern() { 
Regex r = new Regex("<p>");
Match m = r.Match("contains <p> here");
Assert(m.Success);
m = r.Match("contains no para");
Assert(!m.Success);
}

So. There s the [Test] attribute, telling NUnit this is a test method. We create a Regex that searches for <p>, then test it against two strings. We expect the first to succeed and the second to fail. (As far as I can tell, the Match class doesn t have a Failure property, only a Success property.)

If you re not familiar with C#, notice that the code says m.Success, not m.Success(). C# has methods , which take parenthesized lists of arguments (sometimes null), and properties, which act like instance variables, with setters and getters. They can be implemented with member variables or any other way you might like. They aren t member variables , they just act that way.

OK, we get a Green Bar, and we have a running Regex test now. The reality was almost that easy, even though the only documentation I had was the C# Help, which is comprehensive but not easy to dig through.

Lesson  

Now, to some people, that test may seem too simple. You ll want to find your own balance, but I always make my first test really simple. Sometimes I even write a test that just says Assert(true) to get things going. I do that for a few reasons. First of all, it takes a little discipline to do testing, even when I do them first. So I like to make it easy to get started. Second, for me, the first tests usually include a lot of learning about something new, and rather than spend time debugging, I m perfectly happy to start with something trivial. It takes only a moment to do, and it starts me off on the right foot . Third, sometimes even the simplest test I can think of doesn t work. The simpler it is, the sooner I ll figure out what it is I m missing about whatever I m testing. I d suggest that you try tests ranging from very simple to more difficult and see what works best for you. But please do try trivial tests ” they re surprisingly useful.

Lesson  

It occurs to me as I write this that in the course of this book, you ll probably encounter examples of doing things in ways that seem almost obtusely simple. I assure you that I m not doing it to make the book suitable for third-graders, nor because I myself am also simple. I work that way because in the half- dozen years I ve been doing Extreme Programming, I ve been working in simpler and simpler ways, and my work seems to be getting better and better. So, please, when you see something here that looks odd, give it a try. Often I think you ll find it interesting.




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