Sha256: d62f4c0a3f09e3c83ccfde5a68af416a2b102246cafa38c0cc881ed0e4618a45

Contents?: true

Size: 1.88 KB

Versions: 2

Compression:

Stored size: 1.88 KB

Contents

# 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

  public

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

  def on_collection(aLevel, aMacroCollection)
    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, aMacroStep)
    @step_count += 1
    io.puts "#{indentation(aLevel)}Scenario: Macro #{step_count}"
  end

  def on_step_end(aLevel)
    io.puts ''
  end


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

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

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

    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

2 entries across 2 versions & 1 rubygems

Version Path
macros4cuke-0.5.03 lib/macros4cuke/formatter/to-gherkin.rb
macros4cuke-0.4.09 lib/macros4cuke/formatter/to-gherkin.rb