[RFC] DocTest module

Discussion in 'Ruby' started by Nicholas Wieland, May 31, 2005.

  1. 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
     
    Nicholas Wieland, May 31, 2005
    #1
    1. Advertising

  2. 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/scat.rb/ruby/ruby-talk/116284
     
    gabriele renzi, May 31, 2005
    #2
    1. Advertising

  3. - 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/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
     
    Nicholas Wieland, Jun 1, 2005
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Marco Gazerro

    RFC: New module Business::GestPayCrypt

    Marco Gazerro, May 14, 2004, in forum: Perl
    Replies:
    0
    Views:
    443
    Marco Gazerro
    May 14, 2004
  2. Replies:
    0
    Views:
    324
  3. Gary Jefferson

    logging module and doctest

    Gary Jefferson, Jan 25, 2007, in forum: Python
    Replies:
    3
    Views:
    313
    Gary Jefferson
    Jan 25, 2007
  4. Replies:
    1
    Views:
    332
    John J. Lee
    Nov 22, 2007
  5. Ivan Shmakov
    Replies:
    3
    Views:
    1,221
    Kari Hurtta
    Feb 13, 2012
Loading...

Share This Page