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

Returns a new instance of Generator.

Parameters:

  • input_suite (IO::Base)

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

  • output_suite (IO::Base)

    an instance of IO::Base subclass to read and generate the output examples.

  • 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.

  • io (#<<) (defaults to: $stdout)

    output stream where to write log messages.


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.

Parameters:

  • pattern (String) (defaults to: '*:*')

    glob-like pattern to select examples to (re)generate (see Example#name_match?).

  • rewrite (Boolean) (defaults to: false)

    whether to rewrite an already existing example.


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