Sha256: 65b5ac87582a7c49f9c1f5553eb0a8c91560eba9b0b953a6d8920edb313b4fff

Contents?: true

Size: 1.85 KB

Versions: 1

Compression:

Stored size: 1.85 KB

Contents

# frozen_string_literal: true

# File: to-gherkin.rb

require_relative '../constants'

module Macros4Cuke # Module used as a namespace
# Namespace for all formatters of MacroCollection and MacroStep objects
module Formatter
# A macro-step formatter that outputs to the given IO the macro-steps from a
# macro collection into a Gherkin feature file.
class ToGherkin
  # The IO where the formatter's output will be written to.
  attr_reader(:io)

  # The number of macro-step encountered by the formatter.
  attr_reader(:step_count)

  def initialize(anIO)
    @io = anIO
    @step_count = 0
  end

  # Tell which notifications this formatter subscribes to.
  def implements()
    return %i[on_collection on_step on_step_end on_phrase on_source]
  end

  def on_collection(_, _)
    io.print "# Feature file generated by Macro4Cuke #{Macros4Cuke::Version}"
    io.puts " on #{Time.now.strftime('%d/%m/%Y %H:%M:%S')}"
    io.puts ''
    io.puts 'Feature: the set of macro-step definitions'
    io.puts "#{indentation(1)}As a feature file writer"
    io.puts "#{indentation(1)}So that I write higher-level steps"
    io.puts ''
  end

  def on_step(aLevel, _)
    @step_count += 1
    io.puts "#{indentation(aLevel)}Scenario: Macro #{step_count}"
  end

  def on_step_end(_)
    io.puts ''
  end

  def on_phrase(aLevel, aPhraseText, useTable)
    suffix = useTable ? ':' : ''
    io.print "#{indentation(aLevel)}Given I define the step "
    io.puts %("* I [#{aPhraseText}]#{suffix}" to mean:)
  end

  def on_source(aLevel, aSourceText)
    ljust = indentation(aLevel)
    triple_quotes = %(#{ljust}""")
    io.puts triple_quotes

    # Indent source text
    indented_text = aSourceText.gsub(/^/m, ljust.to_s)

    io.puts indented_text
    io.puts triple_quotes
  end

  private

  def indentation(aLevel)
    return '  ' * aLevel
  end
end # class
end # module
end # module

# End of file

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
macros4cuke-0.5.17 lib/macros4cuke/formatter/to-gherkin.rb