Class: Asciidoctor::DocTest::Tester

Inherits:
Object
  • Object
show all
Defined in:
lib/asciidoctor/doctest/tester.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(input_suite, output_suite, converter, reporter = nil) ⇒ Tester

Returns a new instance of Tester

Parameters:

  • input_suite (IO::Base)

    an instance of IO::Base subclass to read the reference input examples from.

  • output_suite (IO::Base)

    an instance of IO::Base subclass to read the output examples from (i.e. an expected output).

  • converter (#call)

    a callable that accepts a string content of an input example and a hash with options for the converter, and returns the converted content.

  • reporter (Minitest::Reporter, nil) (defaults to: nil)

    an instance of minitest's Reporter to report test results. When omitted or nil, then Asciidoctor::DocTest::TestReporter is used.


33
34
35
36
37
38
# File 'lib/asciidoctor/doctest/tester.rb', line 33

def initialize(input_suite, output_suite, converter, reporter = nil)
  @input_suite = input_suite
  @output_suite = output_suite
  @converter = converter
  @reporter = reporter || TestReporter.new
end

Instance Attribute Details

#reporterMinitest::Reporter (readonly)

Returns an instance of minitest's Reporter to report test results.

Returns:

  • (Minitest::Reporter)

    an instance of minitest's Reporter to report test results.


16
17
18
# File 'lib/asciidoctor/doctest/tester.rb', line 16

def reporter
  @reporter
end

Instance Method Details

#run_tests(pattern: '*:*') ⇒ void

Runs tests for all the input/output examples which name matches the pattern. When some output example is missing, it's reported as a skipped test.

Parameters:


48
49
50
51
52
53
54
55
56
57
58
# File 'lib/asciidoctor/doctest/tester.rb', line 48

def run_tests(pattern: '*:*')
  @reporter.start

  @input_suite.pair_with(@output_suite).each do |input, output|
    next if input.empty? || !input.name_match?(pattern)
    test_example input, output
  end

  @reporter.report
  @reporter.passed?
end

#test_example(input_exmpl, output_exmpl) ⇒ void

Tests if the given reference input is matching the expected output after conversion through the tested backend.

Parameters:

  • input_exmpl (Example)

    the reference input example.

  • output_exmpl (Example)

    the expected output example.


67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/asciidoctor/doctest/tester.rb', line 67

def test_example(input_exmpl, output_exmpl)
  test_with_minitest input_exmpl.name do |test|
    if output_exmpl.empty?
      test.skip 'No expected output found'
    else
      actual, expected = @converter.convert_examples(input_exmpl, output_exmpl)
      msg = output_exmpl.desc.presence || input_exmpl.desc

      test.assert_equal expected, actual, msg
    end
  end
end