Class: Cucumber::CucumberExpressions::CucumberExpression

Inherits:
Object
  • Object
show all
Defined in:
lib/cucumber/cucumber_expressions/cucumber_expression.rb

Constant Summary collapse

PARAMETER_REGEXP =
/{([^}]+)}/
OPTIONAL_REGEXP =
/\(([^)]+)\)/
ALTERNATIVE_WORD_REGEXP =
/([[:alpha:]]+)((\/[[:alpha:]]+)+)/

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(expression, parameter_type_registry) ⇒ CucumberExpression

Returns a new instance of CucumberExpression.



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/cucumber/cucumber_expressions/cucumber_expression.rb', line 14

def initialize(expression, parameter_type_registry)
  @source = expression
  @parameter_types = []
  regexp = '^'
  match_offset = 0

  # Escape Does not include (){} because they have special meaning
  expression = expression.gsub(/([\\\^\[$.|?*+\]])/, '\\\\\1')

  # Create non-capturing, optional capture groups from parenthesis
  expression = expression.gsub(OPTIONAL_REGEXP, '(?:\1)?')

  expression = expression.gsub(ALTERNATIVE_WORD_REGEXP) do |_|
    "(?:#{$1}#{$2.tr('/', '|')})"
  end

  loop do
    match = PARAMETER_REGEXP.match(expression, match_offset)
    break if match.nil?

    type_name = match[1]

    parameter_type = parameter_type_registry.lookup_by_type_name(type_name)
    raise UndefinedParameterTypeError.new(type_name) if parameter_type.nil?
    @parameter_types.push(parameter_type)

    text = expression.slice(match_offset...match.offset(0)[0])
    capture_regexp = build_capture_regexp(parameter_type.regexps)
    match_offset = match.offset(0)[1]
    regexp += text
    regexp += capture_regexp
  end
  regexp += expression.slice(match_offset..-1)
  regexp += '$'
  @tree_regexp = TreeRegexp.new(regexp)
end

Instance Attribute Details

#sourceObject (readonly)

Returns the value of attribute source.



12
13
14
# File 'lib/cucumber/cucumber_expressions/cucumber_expression.rb', line 12

def source
  @source
end

Instance Method Details

#match(text) ⇒ Object



51
52
53
# File 'lib/cucumber/cucumber_expressions/cucumber_expression.rb', line 51

def match(text)
  Argument.build(@tree_regexp, text, @parameter_types)
end

#regexpObject



55
56
57
# File 'lib/cucumber/cucumber_expressions/cucumber_expression.rb', line 55

def regexp
  @tree_regexp.regexp
end

#to_sObject



59
60
61
# File 'lib/cucumber/cucumber_expressions/cucumber_expression.rb', line 59

def to_s
  @source.inspect
end