[RFC] DocTest module

N

Nicholas Wieland

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.com/tE0er).
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://mail.yahoo.it
 
N

Nicholas Wieland

- gabriele renzi :
Nicholas Wieland ha scritto:

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-talk/116284

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://mail.yahoo.it
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,989
Messages
2,570,207
Members
46,782
Latest member
ThomasGex

Latest Threads

Top