Unit Testing

2 views
Skip to first unread message

Kris Kowal

unread,
Jun 10, 2008, 1:31:50 AM6/10/08
to Chiron Javascript Library Discussion
So, test driven development is cool. I'm a believer because, no
matter how much I think about it, there's still funny stuff going on
in the edge cases. By funny, I mean annoying when you actually run
into it. My goal: no funny stuff.

Of course, any API is a leaky abstraction. Funny stuff from
JavaScript tends to leak through our API's no matter how we try. So
I've been writing unit tests about stuff I figure already works, and
about 10% of the time I find a surprise.

Just for example, I pose an arithmetic axiom:

a + (b - a) == b
for all a and b of the same type and addition and subtraction are meaningful

So, where do we get leaks? I want this to work for dates. Naturally,
JavaScript lets me do the math without complaint. (b - a) works
great, returning effectively:

b.getTime() - a.getTime()

Then the addition operation runs. JavaScript (in Safari at least)
doesn't seem to understand how to add a lapse back into a date, so it
casts (a) and (b - a) to Strings and concatenates them. Lame,
especially since JavaScript doesn't have any problem doing:

new Date(a.getTime() + (b - a))

which returns the value I would expect.

So, I've modified my wrappers for these operators. You can trust this axiom:

eq(add(a, sub(b, a)), b)

Meanwhile, I found some bugs. For example:

bool({}) and bool([])

should be false since they're empty. Everything else was going well
but these two cases. I fixed them a while back.

Kris

Reply all
Reply to author
Forward
0 new messages