class Turnip::Node::ScenarioOutline

@note ScenarioOutline metadata generated by Gherkin

{
  type: :Scenario,
  tags: [], # Array of Tag
  location: { line: 10, column: 3 },
  keyword: "Scenario Outline",
  name: "Scenario Outline Description",
  steps: []
}

Public Instance Methods

examples() click to toggle source
# File lib/turnip/node/scenario_outline.rb, line 22
def examples
  @examples ||= @raw[:examples].map do |raw|
    Example.new(raw)
  end
end
to_scenarios() click to toggle source

Return array of Scenario

@note

example:

  Scenario Outline: Test
    Then I go to <where>

    Examples:
      | where   |
      | bank    |
      | airport |

  to

  Scenario: Test
    Then I go to bank

  Scenario: Test
    Then I go to airport

@return [Array]

# File lib/turnip/node/scenario_outline.rb, line 53
def to_scenarios
  return [] if examples.nil?

  @scenarios ||= examples.map do |example|
    header = example.header

    example.rows.map do |row|
      metadata = convert_metadata_to_scenario(header, row)

      #
      # Replace <placeholder> using Example values
      #
      metadata[:steps].each do |step|
        step[:text] = substitute(step[:text], header, row)

        case
        when step[:doc_string]
          step[:doc_string][:content] = substitute(step[:doc_string][:content], header, row)
        when step[:data_table]
          step[:data_table][:rows].map do |table_row|
            table_row[:cells].map do |cell|
              cell[:value] = substitute(cell[:value], header, row)
            end
          end
        end
      end

      Scenario.new(metadata)
    end
  end.flatten.compact
end

Private Instance Methods

convert_metadata_to_scenario(header, row) click to toggle source

Convert ScenariOutline metadata for Scenario

@example:

{
  "type": "ScenarioOutline",
  "tags": ['tag'],
  "location": {'loc'},
  "keyword": "Scenario Outline",
  "name": "...",
  "steps": [],
  "examples": []
}

to

{
  "type": "Scenario",
  "tags": ['tag'],
  "location": {'loc'},
  "keyword": "Scenario",
  "name": "...",
  "steps": []
}

@note At this method, placeholder of step text is not replaced yet

@todo :keyword is not considered a language (en only) @return [Hash]

# File lib/turnip/node/scenario_outline.rb, line 118
def convert_metadata_to_scenario(header, row)
  # deep copy
  Marshal.load(Marshal.dump(raw)).tap do |new_raw|
    new_raw.delete(:examples)
    new_raw[:name] = substitute(new_raw[:name], header, row)
    new_raw[:type] = :Scenario
    new_raw[:keyword] = 'Scenario'
  end
end
substitute(text, header, row) click to toggle source

Replace placeholder `<..>`

@example:

text = 'There is a <monster> that has <hp> hitpoints.'
header = ['monster', 'hp']
row = ['slime', '10']

substitute(text, header, row)
# => 'There is a slime that has 10 hitpoints.'

@param text [String] @param header [Array] @param row [Array] @return [String]

# File lib/turnip/node/scenario_outline.rb, line 145
def substitute(text, header, row)
  text.gsub(/<([^>]*)>/) { |_| Hash[header.zip(row)][$1] }
end