Class: YARD::Doctest::Example

Inherits:
Object
  • Object
show all
Defined in:
lib/yard/doctest/example.rb

Instance Method Summary collapse

Constructor Details

#initialize(path, file, name, asserts, hooks) ⇒ Example

There are a bunch of hacks happening here:

1. Everything is done within constructor.
2. Context (binding) is shared between helper, example and hooks.
3. Since hooks are blocks, they can't be passed to `#eval`,
   so we translate them into string of Ruby code.
4. Intercept exception backtrace and add example object definition path.


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
# File 'lib/yard/doctest/example.rb', line 17

def initialize(path, file, name, asserts, hooks)
  Object.instance_eval do
    context = binding

    require 'minitest/autorun'
    context.eval "require 'yard-doctest_helper'"

    describe path do
      before { context.eval(hooks[:before].to_source(strip_enclosure: true)) } if hooks[:before]
      after { context.eval(hooks[:after].to_source(strip_enclosure: true)) } if hooks[:after]

      it name do
        asserts.each do |assert|
          expected, actual = assert[:expected], assert[:actual]
          actual = context.eval(actual)

          unless expected.empty?
            begin
              assert_equal context.eval(expected), actual
            rescue Minitest::Assertion => e
              backtrace = e.backtrace
              example = backtrace.find { |trace| trace =~ %r(lib/yard/doctest/example) }
              example = backtrace.index(example)
              backtrace = backtrace.insert(example, file)
              e.set_backtrace backtrace
              raise e
            end
          end
        end
      end
    end
  end
end