Module: CukeModeler::Parsing

Included in:
Background, Cell, Comment, DocString, Example, Outline, Row, Rule, Scenario, Step, Table, Tag
Defined in:
lib/cuke_modeler/parsing.rb

Overview

A module providing source text parsing functionality.

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.dialect ⇒ String

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

Examples:

Parsing.dialect

Returns:

  • (String) —

    The current dialect. Defaults to 'en'.



36
37
38
# File 'lib/cuke_modeler/parsing.rb', line 36

def dialect
  @dialect || 'en'
end

Class Method Details

.dialects ⇒ Hash

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

Examples:

Parsing.dialects

Returns:

  • (Hash) —

    The dialect data



46
47
48
# File 'lib/cuke_modeler/parsing.rb', line 46

def dialects
  Gherkin::DIALECTS
end

.parse_text(source_text, filename = 'cuke_modeler_fake_file.feature') ⇒ Hash

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

Examples:

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

Parameters:

  • source_text (String) —

    The Gherkin text to parse

  • filename (String) (defaults to: 'cuke_modeler_fake_file.feature') —

    The file name associated with the parsed text. Used for error messages.

Returns:

  • (Hash) —

    An AST of the text

Raises:

  • (ArgumentError) —

    If source_text is not a String

  • (ArgumentError) —

    If source_text does not parse cleanly



63
64
65
66
67
68
69
70
71
72
73
74
75
# 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