I released GreenRocket library on October 2012. It is a dead simple implementation of Observer design pattern, which I use in almost all of my projects. I thought, there was nothing to improve. But my recent project heavily uses the library. And I get tired to write tests that checks signals. This is how them look like:
from nose import tools # I use Nose for testing my code from myproject import MySignal, some_func # ``MySignal`` inherits ``greenrocket.Signal`` # ``some_func`` must fire ``MySignal`` as its side-effect def test_some_func(): log = [] # Create log for fired signals @MySignal.subscribe # Subscribe a dummy handler def handler(signal): log.append(signal) # Put fired signal to the log some_func() # Call the function to test # Test fited signal from the log tools.eq_(len(log), 1) tools.eq_(log[0].x, 1) tools.eq_(log[0].y, 2) # ...and so on
There are four lines of utility code. And it is boring. So I added helper class Watchman to the library to make it testing friendly. This is how it works:
from greenrocket import Watchman from myproject import MySignal, some_func def test_some_code_that_fires_signal(): watchman = Watchman(MySignal) # Create a watchman for MySignal some_func() watchman.assert_fired_with(x=1, y=2) # Test fired signal
Just one line of utility code and one line for actual test! I have already rewritten all of my tests. So if you are using the library, it’s time to upgrade. If you don’t, then try it out.