Sometimes even the simple stuff can have some interesting unit tests and corner cases. I had a class that's basically a "Thing" with an X,Y position and an OnMoved event. In C# :
public class ThingMovedArgs { public ThingMovedArgs(Thing t, Point ptOld, Point ptNew); } class Thing { int X; // just get/set X int Y; // just get/set Y Point XY; // get/set X and Y together as a Point public event EventHandler<ThingMovedArgs> OnMoved; }
The OnMoved handler provides the old location and new location. Assume this is all single-threaded.
It looks so simple. Yet there are some pretty interesting corner cases that would actually be reasonable for a well-intentioned developer to get wrong.
Here are the tests I wrote and some of the "specification holes" they reveal :
Point XY // get/set X and Y together as a Point { set // BUG!!! Fires OnMoved 2 times instead of just once { X = value.X; // <-- fires OnMoved once for changing X Y = value.Y; // <-- fires OnMoved a 2nd time for changing Y } }
Anybody see others?
The lesson learned:There's an infinite number of possible bugs in a sufficiently dumb implementation. I think a key here is that many of these failures:- could occur with a bad corner-case from a reasonable attempt to implement the class. If you got 100 developers to implement the class, you'd probably hit all of these problems. Or you could probably hit these cases with just 5 implementations if you just asked the developers to try and optimize their code. :)- indicate spec holes.