class TestMatcher(unittest.TestCase): def match_one(self, match): self.functionCalled = True def setUp(self): self.newMatcher = Matcher() self.functionCalled = False def test_register_and_registry_count_does_increase(self): self.newMatcher.register("method1", self.match_one) self.assertEquals( 1, self.newMatcher.registry_length() ) def test_register_and_can_find_via_matching_method(self): self.newMatcher.register("method1", self.match_one) self.assertEquals( True, self.newMatcher.has_matching_method("method1") ) def test_register_and_returns_false_when_not_found_via_matching_method(self): self.newMatcher.register("method1", self.match_one) self.assertEquals( False, self.newMatcher.has_matching_method("method2") ) def test_register_and_calls(self): self.newMatcher.register("method1", self.match_one) self.assertEquals( True, self.newMatcher.has_matching_method("method1") ) self.newMatcher.execute_method_for("method1") self.assertEquals( True, self.functionCalled) def test_calls_bad_name(self): self.assertRaises(NameError, self.newMatcher.execute_method_for, "blowUp" )
from method_matcher import Matcher def i_am_given_n_fish(match): return "I have been given %d fish" % int(match.group(1)) mat = Matcher() mat.register("When I am given (\d+) fish", i_am_given_n_fish) print mat.execute_method_for("When I am given 12 fish") print "You should have been given 12 fish..."