Class: YARD::Doctest::Example

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#assertsArray<Hash>

Returns assertions to be done.

Returns:

  • (Array<Hash>)

    assertions to be done



12
13
14
# File 'lib/yard/doctest/example.rb', line 12

def asserts
  @asserts
end

#definitionString

Returns namespace path of example (e.g. ‘Foo#bar`).

Returns:

  • (String)

    namespace path of example (e.g. ‘Foo#bar`)



6
7
8
# File 'lib/yard/doctest/example.rb', line 6

def definition
  @definition
end

#filepathString

Returns filepath to definition (e.g. ‘app/app.rb:10`).

Returns:

  • (String)

    filepath to definition (e.g. ‘app/app.rb:10`)



9
10
11
# File 'lib/yard/doctest/example.rb', line 9

def filepath
  @filepath
end

Instance Method Details

#generateObject

Generates a spec and registers it to Minitest runner.



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

def generate
  this = self

  Class.new(this.class).class_eval do
    require 'minitest/autorun'

    %w[. support spec].each do |dir|
      require "#{dir}/doctest_helper" if File.exist?("#{dir}/doctest_helper.rb")
    end

    return if YARD::Doctest.skips.any? { |skip| this.definition.include?(skip) }

    describe this.definition do
      # Append this.name to this.definition if YARD's @example tag is followed by
      # descriptive text, to support hooks for multiple examples per code object.
      example_name = if this.name.empty?
                       this.definition
                     else
                       "#{this.definition}@#{this.name}"
                     end

      register_hooks(example_name, YARD::Doctest.hooks, this)

      it this.name do
        begin
          object_name = this.definition.split(/#|\./).first
          scope = Object.const_get(object_name) if self.class.const_defined?(object_name)
        rescue NameError
        end

        global_constants = Object.constants
        scope_constants = scope.constants if scope && scope.respond_to?(:constants)
        this.asserts.each do |assert|
          expected, actual = assert[:expected], assert[:actual]
          if expected.empty?
            evaluate_example(this, actual, scope)
          else
            assert_example(this, expected, actual, scope)
          end
        end
        clear_extra_constants(Object, global_constants)
        clear_extra_constants(scope, scope_constants) if scope && scope.respond_to?(:constants)
      end
    end
  end
end