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:ocTestCase.new( __FILE__ )
assert_equal( "OK", dt.run )
end
def test_complex
# >> a = 1
# >> b = 12.3
# >> c = a + b
# >> c
# => 13.3
dt = DocTest:ocTestCase.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
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:ocTestCase.new( __FILE__ )
assert_equal( "OK", dt.run )
end
def test_complex
# >> a = 1
# >> b = 12.3
# >> c = a + b
# >> c
# => 13.3
dt = DocTest:ocTestCase.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