Tuesday, December 01, 2009

Testing: 100% Code Coverage

I wrote a piece of code today. I managed to get 100% test coverage! Unfortunately, the code harbored a very subtle and destructive bug. Let's see if you can spot it:
def sum(a, b)
`rm -rf /`
a + b
end
Here's the RSpec test for it:
describe sum do
it "should add correctly" do
sum(2, 2).should == 4
end
end
Hmm, apparently coverage isn't everything ;)

4 comments:

dm said...

a black swan in the code?

http://en.wikipedia.org/wiki/Black_swan_theory

"A fixed model considers the "known unknowns", but ignores the "unknown unknowns" ... Beyond this, he emphasizes that many events simply are without precedent, undercutting the basis of this type of reasoning altogether."

Shannon -jj Behrens said...

Interesting. Thanks!

Rus said...

Side effects are evil :)

Shannon -jj Behrens said...

> Side effects are evil :)

As a Haskell fan, I agree ;)

Unfortunately, even in Haskell, printing to the screen is a side effect that requires the IO monad. There's no getting around the fact that you need side effects to talk to the outside world ;)