Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > [RFC] DocTest module

Reply
Thread Tools

[RFC] DocTest module

 
 
Nicholas Wieland
Guest
Posts: n/a
 
      05-31-2005
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


 
Reply With Quote
 
 
 
 
gabriele renzi
Guest
Posts: n/a
 
      05-31-2005
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.com/tE0er).


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/sca...by-talk/116284
 
Reply With Quote
 
 
 
 
Nicholas Wieland
Guest
Posts: n/a
 
      06-01-2005
- 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.com/tE0er).

>
> 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/sca...by-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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
trace module and doctest skip@pobox.com Python 1 11-22-2007 09:31 PM
logging module and doctest Gary Jefferson Python 3 01-25-2007 09:22 PM
nose, doctest, and module names in subpackages davidlmontgomery@gmail.com Python 0 09-06-2006 01:27 AM
Re: module docstring, documentation,anything? please note is the module type/object NOT some module Maric Michaud Python 0 06-24-2006 12:42 PM
Re: Simulte user input using doctest Steven Taschuk Python 0 06-27-2003 01:57 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57