[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

[RFC] DocTest module

Nicholas Wieland

5/31/2005 4:43:00 PM

Hi all,
this is the first time I write code in Ruby that's not Rails-centric, so
I'd like to have feedback and comments on this real newbish attempt to
port Python doctest module to Ruby (http://rubyurl...).
I'm still in the process of learning Ruby - I mean, I've understood the
concepts but lack the experience to call myself a Ruby programmer - so
I'm glad to receive every suggestion or critique to increase my
knowledge of the language.

SCRIPT_LINES__ = {}

module DocTest

class DocTestCase

attr_reader :doctest

def initialize( mod )
@doctest = {}
self.fetch( mod )
end

def run
@doctest.keys.each do |code|
if eval( code ).to_s == @doctest[ code ].to_s
return "OK"
else
return "NO"
end
end
end

def fetch( mod )
body, result = '', ''
require "#{ mod }"
SCRIPT_LINES__.each_value do |source|
source.each do |line|
line = line.strip
case line
when /^# *>>/
body << line.gsub( /^# *>>/, '' ).strip << '; '
when /^# *=>/
result << line.gsub( /^# *=>/, '' ).strip
@doctest[ body ] = result
body, result = '', ''
end
end
end
end

end

Is it a good choice to use eval for this kind of task ?
I wasn't able to find any documentation on IRB, but my first idea was to
use the real IRB for doctest comparing an emulated IRB session (a
doctest) with a real IRB session, and I still think it's the best way,
but reading all IRB source is far out of my possibilities :)

A very simple testcase:

$:.unshift( File.join( File.dirname( __FILE__ ), '../lib' ) )

require 'test/unit'
require 'doctest'

class TestDocTestCase < Test::Unit::TestCase

def test_simple
# >> 1 + 2
# => 3

dt = DocTest::DocTestCase.new( __FILE__ )
assert_equal( "OK", dt.run )
end

def test_complex
# >> a = 1
# >> b = 12.3
# >> c = a + b
# >> c
# => 13.3

dt = DocTest::DocTestCase.new( __FILE__ )
assert_equal( "OK", dt.run )
end

end

Sorry for the long post.

TIA,
ngw

--
checking for life_signs in -lKenny... no
Oh my god, make (1) killed Kenny ! You, bastards !

nicholas_wieland-at-yahoo-dot-it





___________________________________
Yahoo! Mail: gratis 1GB per i messaggi e allegati da 10MB
http://mai...


2 Answers

gabriele renzi

5/31/2005 9:05:00 PM

0

Nicholas Wieland ha scritto:
> Hi all,

hi,

> this is the first time I write code in Ruby that's not Rails-centric, so
> I'd like to have feedback and comments on this real newbish attempt to
> port Python doctest module to Ruby (http://rubyurl...).

I have little time and can't check your code.. but have you looked at
what has been already done in this field ?
http://www.ruby-talk.org/cgi-bin/scat.rb/ruby/ruby-t...

Nicholas Wieland

6/1/2005 12:01:00 PM

0

- gabriele renzi :
> Nicholas Wieland ha scritto:
> >Hi all,
>
> hi,
>
> >this is the first time I write code in Ruby that's not Rails-centric, so
> >I'd like to have feedback and comments on this real newbish attempt to
> >port Python doctest module to Ruby (http://rubyurl...).
>
> I have little time and can't check your code.. but have you looked at
> what has been already done in this field ?
> http://www.ruby-talk.org/cgi-bin/scat.rb/ruby/ruby-t...

Yes, I found extract.rb and looked at it, but it doen't seem to do the
same thing of doctest.
As a long time Python programmer I'm accustomed to trying my code in an
interactive Python shell while programming, doctest is useful because I
can cut and paste what I've done inside the Python shell and use it as a
test and as real documentation on the usage of what I wrote.
If I understand it correctly Florian's code seems to extract normal tests
from comments, so it's a different use case.

TIA,
ngw

--
checking for life_signs in -lKenny... no
Oh my god, make (1) killed Kenny ! You, bastards !

nicholas_wieland-at-yahoo-dot-it





___________________________________
Yahoo! Mail: gratis 1GB per i messaggi e allegati da 10MB
http://mai...