module CukeModeler::Parsing

A module providing source text parsing functionality.

Attributes

dialect[W]

The dialect that will be used to parse snippets of Gherkin text

Public Class Methods

dialect() click to toggle source

The dialect that will be used to parse snippets of Gherkin text

@example

Parsing.dialect

@return [String] The current dialect. Defaults to ‘en’.

# File lib/cuke_modeler/parsing.rb, line 36
def dialect
  @dialect || 'en'
end
dialects() click to toggle source

The dialects currently known by the cucumber-gherkin gem. See Gherkin::DIALECTS.

@example

Parsing.dialects

@return [Hash] The dialect data

# File lib/cuke_modeler/parsing.rb, line 46
def dialects
  Gherkin::DIALECTS
end
parse_text(source_text, filename = 'cuke_modeler_fake_file.feature') click to toggle source

Parses the Cucumber feature given in source_text and returns a Hash representation of its logical structure. This is a standardized AST that should remain consistent across different versions of ‘cucumber-gherkin`

@example

Parsing.parse_text('Feature: Some feature')
Parsing.parse_text('Feature: Some feature', 'my.feature')

@param source_text [String] The Gherkin text to parse @param filename [String] The file name associated with the parsed text. Used for error messages. @raise [ArgumentError] If source_text is not a String @raise [ArgumentError] If source_text does not parse cleanly @return [Hash] An AST of the text

# File lib/cuke_modeler/parsing.rb, line 63
def parse_text(source_text, filename = 'cuke_modeler_fake_file.feature')
  unless source_text.is_a?(String)
    raise(ArgumentError, "Text to parse must be a String but got #{source_text.class}")
  end

  begin
    parsed_result = parsing_method(source_text.encode('UTF-8'), filename)
  rescue => e
    raise(ArgumentError, "Error encountered while parsing '#{filename}'\n#{e.class} - #{e.message}")
  end

  adapter_class.new.adapt(parsed_result)
end

Private Class Methods

parsing_method(source_text, filename) click to toggle source

The method to use for parsing Gherkin text

# File lib/cuke_modeler/parsing.rb, line 91
def parsing_method(source_text, filename)
  messages = Gherkin.from_source(filename,
                                 source_text,
                                 { include_gherkin_document: true })
                    .to_a

  error_message = messages.find(&:parse_error)
  gherkin_ast_message = messages.find(&:gherkin_document)

  raise error_message.parse_error.message if error_message

  gherkin_ast_message.gherkin_document
end

Private Instance Methods

dialect_feature_keyword() click to toggle source
# File lib/cuke_modeler/parsing.rb, line 182
def dialect_feature_keyword
  get_word(Parsing.dialects[Parsing.dialect]['feature'])
end
dialect_outline_keyword() click to toggle source
# File lib/cuke_modeler/parsing.rb, line 190
def dialect_outline_keyword
  get_word(Parsing.dialects[Parsing.dialect]['scenarioOutline'] ||
             Parsing.dialects[Parsing.dialect]['scenario_outline'])
end
dialect_scenario_keyword() click to toggle source
# File lib/cuke_modeler/parsing.rb, line 186
def dialect_scenario_keyword
  get_word(Parsing.dialects[Parsing.dialect]['scenario'])
end
dialect_step_keyword() click to toggle source
# File lib/cuke_modeler/parsing.rb, line 195
def dialect_step_keyword
  get_word(Parsing.dialects[Parsing.dialect]['given'])
end
get_word(word_set) click to toggle source
# File lib/cuke_modeler/parsing.rb, line 199
def get_word(word_set)
  word_set = word_set.split('|') unless word_set.is_a?(Array)

  word_set.first
end