Sha256: ba1c4dbee23490d2645e348574d10fad01391773b87a3b5c9b969548428a987f

Contents?: true

Size: 1.8 KB

Versions: 12

Compression:

Stored size: 1.8 KB

Contents

$:.unshift(File.dirname(__FILE__)) unless
  $:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))

require 'statement'
require 'result'

module RubyDocTest
  # A +CodeBlock+ is a group of one or more ruby statements, followed by an optional result.
  # For example:
  #  >> a = 1 + 1
  #  >> a - 3
  #  => -1
  class CodeBlock
    attr_reader :statements, :result, :passed
    
    def initialize(statements = [], result = nil)
      @statements = statements
      @result = result
    end
    
    # === Tests
    # doctest: Single statement with result should pass
    # >> ss = [RubyDocTest::Statement.new([">> a = 1"])]
    # >> r = RubyDocTest::Result.new(["=> 1"])
    # >> cb = RubyDocTest::CodeBlock.new(ss, r)
    # >> cb.pass?
    # => true
    #
    # doctest: Single statement without result should pass by default
    # >> ss = [RubyDocTest::Statement.new([">> a = 1"])]
    # >> cb = RubyDocTest::CodeBlock.new(ss)
    # >> cb.pass?
    # => true
    #
    # doctest: Multi-line statement with result should pass
    # >> ss = [RubyDocTest::Statement.new([">> a = 1"]),
    #          RubyDocTest::Statement.new([">> 'a' + a.to_s"])]
    # >> r = RubyDocTest::Result.new(["=> 'a1'"])
    # >> cb = RubyDocTest::CodeBlock.new(ss, r)
    # >> cb.pass?
    # => true
    def pass?
      if @computed
        @passed
      else
        @computed = true
        @passed =
          begin
            actual_results = @statements.map{ |s| s.evaluate }
            @result ? @result.matches?(actual_results.last) : true
          end
      end
    end
    
    def actual_result
      @statements.last.actual_result
    end
    
    def expected_result
      @result.expected_result
    end
    
    def lines
      @statements.map{ |s| s.lines }.flatten +
      @result.lines
    end
  end
end

Version data entries

12 entries across 12 versions & 6 rubygems

Version Path
rubydoctest-1.1.5 lib/code_block.rb
rubydoctest-1.1.4 lib/code_block.rb
bryanlarsen-rubydoctest-1.0.2 lib/code_block.rb
bryanlarsen-rubydoctest-1.1.0 lib/code_block.rb
bryanlarsen-rubydoctest-1.1.1 lib/code_block.rb
canadaduane-rubydoctest-1.0.2 lib/code_block.rb
matthewrudy-rubydoctest-1.0.1 lib/code_block.rb
matthewrudy-rubydoctest-1.0.2 lib/code_block.rb
tablatom-rubydoctest-1.0.0 lib/code_block.rb
rubydoctest-1.1.3 lib/code_block.rb
rdp-rubydoctest-1.0.1 lib/code_block.rb
rubydoctest-1.0.0 lib/code_block.rb