Class: Macros4Cuke::Formatter::ToGherkin

Inherits:
Object
  • Object
show all
Defined in:
lib/macros4cuke/formatter/to-gherkin.rb

Overview

macro collection into a Gherkin feature file.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(anIO) ⇒ ToGherkin

Returns a new instance of ToGherkin.



19
20
21
22
# File 'lib/macros4cuke/formatter/to-gherkin.rb', line 19

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

Instance Attribute Details

#ioObject (readonly)

The IO where the formatter's output will be written to.



14
15
16
# File 'lib/macros4cuke/formatter/to-gherkin.rb', line 14

def io
  @io
end

#step_countObject (readonly)

The number of macro-step encountered by the formatter.



17
18
19
# File 'lib/macros4cuke/formatter/to-gherkin.rb', line 17

def step_count
  @step_count
end

Instance Method Details

#implementsObject

Tell which notifications this formatter subscribes to.



25
26
27
# File 'lib/macros4cuke/formatter/to-gherkin.rb', line 25

def implements()
  return %i[on_collection on_step on_step_end on_phrase on_source]
end

#on_collection(_, _) ⇒ Object



29
30
31
32
33
34
35
36
37
# File 'lib/macros4cuke/formatter/to-gherkin.rb', line 29

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

#on_phrase(aLevel, aPhraseText, useTable) ⇒ Object



48
49
50
51
52
# File 'lib/macros4cuke/formatter/to-gherkin.rb', line 48

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

#on_source(aLevel, aSourceText) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
# File 'lib/macros4cuke/formatter/to-gherkin.rb', line 54

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

#on_step(aLevel, _) ⇒ Object



39
40
41
42
# File 'lib/macros4cuke/formatter/to-gherkin.rb', line 39

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

#on_step_end(_) ⇒ Object



44
45
46
# File 'lib/macros4cuke/formatter/to-gherkin.rb', line 44

def on_step_end(_)
  io.puts ''
end