class GherkinGenerator

A simple tool for generating Gherkin Feature Files programmatically.

Example

gg = GherkinGenerator.new("My Feature")
gg.add_scenario("Something: When I do something Then something should have happened")
gg.add_scenario("Something Else: When I do something else Then something else should have happened")
puts gg.to_gherkin

Attributes

description[RW]

A multiline feature description

Example

GherkinGenerator.new("My Feature").description = <<EOS
All sorts of information about my feature
This info is not parsed, it's just a description of the feature
EOS

Public Class Methods

new(feature_name) click to toggle source

Generate a new feature

Example

GherkinGenerator.new("Feature name goes here")
# File lib/gherkin_generator.rb, line 28
def initialize(feature_name)
  @feature_name = feature_name
  @scenarios = []
  @tag = ''
end

Public Instance Methods

add_scenario(scenario) click to toggle source

Add a scenario to the feature. The format is a single line string with capital letters for the GWTA’s

Example

myFeature.add_scenario("My Scenario: Given I ... When I ... And I ... Then I ...")
# File lib/gherkin_generator.rb, line 43
def add_scenario(scenario)
  examples_keyword = 'Examples '
  scenario_parts = scenario.split examples_keyword
  if scenario_parts[1] && !@ex_yield.nil?
    examples = @ex_yield.call(scenario_parts[1])
  end
  result = Scenario.new(scenario_parts[0], examples)
  @scenarios << result
  result
end
example_handler(&block) click to toggle source

Examples in Gherkin don’t suite the one-line input well. This

Example

myFeature.exampleHandler do |reference|
  puts "looking up examples with ref: #{reference}"
  <<EOS
| something | another |
| abc       | def     |
| def       | ghi     |
EOS
end
myFeature.add_scenario("My Scenario With" +
  " Examples: Given I ... Then I ... Examples http://example.org/example1")
# File lib/gherkin_generator.rb, line 70
def example_handler(&block)
  @ex_yield = block
end
to_gherkin() click to toggle source

Output the Gherkin formatted string, this can be saved in a Feature file.

Example

gg = GherkinGenerator.new("My Feature")
gg.add_scenario("Something: When I do something Then something should have happened")
gg.add_scenario("Something Else: When I do something else Then something else should have happened")
puts gg.to_gherkin
# File lib/gherkin_generator.rb, line 84
def to_gherkin
  feature = 'Feature: ' + @feature_name
  desc = @description.nil? ? '' : "\n\t" + @description.sub("\n", "\n\t")
  scenarios = ''

  @scenarios.each do |item|
    scenarios << item.format_string
  end

  feature + desc + scenarios + "\n"
end