Class: RubyDocTest::Result

Inherits:
Lines
  • Object
show all
Defined in:
lib/result.rb

Instance Method Summary collapse

Methods inherited from Lines

#initialize, #inspect, #line_number, #lines, #range

Constructor Details

This class inherits a constructor from RubyDocTest::Lines

Instance Method Details

#expected_resultObject



13
14
15
16
17
18
19
# File 'lib/result.rb', line 13

def expected_result
  @expected_result ||=
    begin
      lines.first =~ /^#{Regexp.escape(indentation)}=>\s(.*)$/
      ([$1] + (lines[1..-1] || [])).join("\n")
    end
end

#matches?(actual_result) ⇒ Boolean

Tests

doctest: Strings should match >> r = RubyDocTest::Result.new([“=> ‘hi’”]) >> r.matches? ‘hi’

> true

>> r = RubyDocTest::Result.new([“=> "hi"”]) >> r.matches? “hi”

> true

doctest: Regexps should match >> r = RubyDocTest::Result.new([“=> /^reg…/”]) >> r.matches? /^reg…/

> true

>> r = RubyDocTest::Result.new([“=> /^reg…/”]) >> r.matches? /^regexp/

> false

doctest: Arrays should match >> r = RubyDocTest::Result.new([“=> [1, 2, 3]”]) >> r.matches? [1, 2, 3]

> true

doctest: Arrays of arrays should match >> r = RubyDocTest::Result.new([“=> [[1, 2], [3, 4]]”]) >> r.matches? [[1, 2], [3, 4]]

> true

doctest: Hashes should match >> r = RubyDocTest::Result.new([“=> => 1, :two => 2”]) >> r.matches?(=> 2, :one => 1)

> true

Returns:

  • (Boolean)


54
55
56
57
58
59
60
61
# File 'lib/result.rb', line 54

def matches?(actual_result)
  normalize_result(actual_result.inspect) ==
  normalize_result(expected_result) \
    or
  actual_result == eval(expected_result, TOPLEVEL_BINDING)
rescue Exception
  false
end

#normalize_result(s) ⇒ Object



9
10
11
# File 'lib/result.rb', line 9

def normalize_result(s)
  s.gsub(/:0x[a-f0-9]{8}>/, ':0xXXXXXXXX>').strip
end