Getting a Little Organized
I’m mostly a self-taught pogrammer, so I tend to cite books as influences. One book that has influenced me a lot is Test-Driven Development By Example, by Kent Beck. Beck is a big proponent of XP, and in this book he walks his readers through the process of writing tests first, and then coding just enough to make the tests pass. The book convinced me to trust test-first enough to give it a fair and thorough try-out. It was hard to write that first test that really only tested for the existence of a class and method. But once I started working this way, I found myself not only testing before I coded, but thinking before I coded too. “This method I’m proposing should exist… is this the right signature? Could it be simpler and more flexible?”
So I’m going to do the same thing with this Pattern Spy. I’m going to add a method to my test script that uses some application code, an object of class PatternSpy.
require 'test/unit'
...
def test_spy
pspy = PatternSpy.new(Regexp::new("", Regexp::MULTILINE),
"www.michaelharrison.ws/index.html")
assert_not_nil pspy.spy
end
This test obviously fails because there’s no PatternSpy yet. In crafting the test, though, I’ve expressed my desire to be able to instantiate a PatternSpy object with a Regexp object and a string for the URL to examine. I’ll need to handle that in the initialize method. I also want a ‘spy’ method, and I want it to return something if the Regexp matches the URL, or nil if it doesn’t.
Coming up: First Code