Class: Asciidoctor::DocTest::Generator

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

Instance Method Summary collapse

Constructor Details

#initialize(input_suite, output_suite, converter, io = $stdout) ⇒ Generator


24
25
26
27
28
29
# File 'lib/asciidoctor/doctest/generator.rb', line 24

def initialize(input_suite, output_suite, converter, io = $stdout)
  @input_suite = input_suite
  @output_suite = output_suite
  @converter = converter
  @io = io
end

Instance Method Details

#generate!(pattern: '*:*', rewrite: false) ⇒ void

Generates missing, or rewrite existing output examples from the input examples converted using the converter.


41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/asciidoctor/doctest/generator.rb', line 41

def generate!(pattern: '*:*', rewrite: false)
  updated = []

  @input_suite.pair_with(@output_suite).each do |input, output|
    next unless input.name_match? pattern

    log = ->(msg, color = :default) do
      @io << " --> #{(msg % input.name).color(color)}\n" if @io
    end

    if input.empty?
      log["Unknown %s, doesn't exist in input examples!"]
    else
      actual, expected = @converter.convert_examples(input, output)
      generated = output.dup.tap { |ex| ex.content = actual }

      if output.empty?
        log['Generating %s', :magenta]
        updated << generated
      elsif actual == expected
        log['Unchanged %s', :green]
      elsif rewrite
        log['Rewriting %s', :red]
        updated << generated
      else
        log['Skipping %s', :yellow]
      end
    end
  end

  @output_suite.update_examples updated unless updated.empty?
end