Code coverage in Ruby?

Has anybody worked on a method for determining
code coverage in a Ruby program?

This is where defadvice might really come in
handy, I think.

Even if it was only at the method level, it
would be better than nothing. I’d prefer the
statement level.

I frequently find that a program “seems” to be
running fine, then I suddenly hit a runtime
error.

On a side note: My most common one is that a
method is not implemented for nil. This, I guess,
is the equivalent of the most common runtime
error in the “pointerless” Java language, which
is a null pointer exception.

My second most common runtime error is that a
constant or variable is not defined.

Of course, XP fans might say that if my tests
were adequate, I wouldn’t be worrying about it.

I’m not much of an XP person yet, though. I’ve
never grasped how to write unit tests for GUI
apps, for instance.

Hal

Has anybody worked on a method for determining
code coverage in a Ruby program?
[…]
Of course, XP fans might say that if my tests
were adequate, I wouldn’t be worrying about it.

You read my mind … Test Driven Design — Its a way of life :slight_smile:

> I'm not much of an XP person yet, though. I've 
> never grasped how to write unit tests for GUI 
> apps, for instance.

I’ve only done one or two GUI programs since I started using Test
Driven Design (TDD) and they were fairly successful. The key is to
make the GUI as thin and dumb as possible. I always felt I did a good
job of separating the business/application logic from display logic,
but using TDD really pushed me farther in that direction than I had
gone before.

If I get time, I’ll see if I can publish an example (don’t hold your
breath, there are several TODO items ahead of it).

···


– Jim Weirich jweirich@one.net http://w3.one.net/~jweirich

“Beware of bugs in the above code; I have only proved it correct,
not tried it.” – Donald Knuth (in a memo to Peter van Emde Boas)

Won’t -w catch these?

···

On Wed, Sep 18, 2002 at 12:12:51PM +0900, Hal E. Fulton wrote:

My second most common runtime error is that a
constant or variable is not defined.


Jim Freeze

Programming Ruby
def initialize; fun; end
A language with class

You know, I never use -w. :) I should form that habit.

On that issue, I sometimes see warnings that I
wish I could get rid of. For example: ‘if x = y’
or better yet ‘while x=meth’ will give me a
warning. Sometimes I’m grateful, but sometimes
I wanted to do that. Maybe that’s my C showing.

Almost makes me wish for something like Ada’s
pragma or Pascal’s {$…} to tell the interpreter
“Ignore the suspicious-looking code on this line.
Pay no attention to that man behind the curtain.”

But that would probably make Ruby more complex
and ugly. That’s “(more complex) and (ugly)”, not
“more (complex and ugly)”. :slight_smile:

Boy, I’m getting tired.

Hal

···

----- Original Message -----
From: “Jim Freeze” jim@freeze.org
To: “ruby-talk ML” ruby-talk@ruby-lang.org
Sent: Tuesday, September 17, 2002 11:27 PM
Subject: Re: Code coverage in Ruby?

On Wed, Sep 18, 2002 at 12:12:51PM +0900, Hal E. Fulton wrote:

My second most common runtime error is that a
constant or variable is not defined.

Won’t -w catch these?

Hi,

“Hal E. Fulton” hal9000@hypermetrics.com writes:

You know, I never use -w. :) I should form that habit.

On that issue, I sometimes see warnings that I
wish I could get rid of. For example: ‘if x = y’
or better yet ‘while x=meth’ will give me a
warning. Sometimes I’m grateful, but sometimes
I wanted to do that. Maybe that’s my C showing.

Ruby will give you a warning only when the right-hand side
is a constant. If it is a constant, the if condition
must be unnecessary.

% ruby -we ‘if foo = 1 then end’
-e:1: warning: found = in conditional, should be ==
% ruby -we ‘bar = 1; if foo = bar then end’
%

···


eban

Thank you. I had not remembered that correctly.

Hal

···

----- Original Message -----
From: “WATANABE Hirofumi” eban@os.rim.or.jp
To: “ruby-talk ML” ruby-talk@ruby-lang.org
Sent: Wednesday, September 18, 2002 12:12 AM
Subject: Re: Code coverage in Ruby?

Ruby will give you a warning only when the right-hand side
is a constant. If it is a constant, the if condition
must be unnecessary.

% ruby -we ‘if foo = 1 then end’
-e:1: warning: found = in conditional, should be ==
% ruby -we ‘bar = 1; if foo = bar then end’