Class: Cucumber::CucumberExpressions::CucumberExpression
- Inherits:
-
Object
- Object
- Cucumber::CucumberExpressions::CucumberExpression
- Defined in:
- lib/cucumber/cucumber_expressions/cucumber_expression.rb
Constant Summary collapse
- ESCAPE_REGEXP =
Does not include (){} characters because they have special meaning
/([\\^\[$.|?*+\]])/- PARAMETER_REGEXP =
/{([^}]+)}/- OPTIONAL_REGEXP =
/\(([^)]+)\)/- ALTERNATIVE_NON_WHITESPACE_TEXT_REGEXP =
/([^\s^\/]+)((\/[^\s^\/]+)+)/
Instance Attribute Summary collapse
-
#source ⇒ Object
readonly
Returns the value of attribute source.
Instance Method Summary collapse
-
#initialize(expression, parameter_type_registry) ⇒ CucumberExpression
constructor
A new instance of CucumberExpression.
- #match(text) ⇒ Object
- #regexp ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(expression, parameter_type_registry) ⇒ CucumberExpression
Returns a new instance of CucumberExpression.
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 50 |
# File 'lib/cucumber/cucumber_expressions/cucumber_expression.rb', line 16 def initialize(expression, parameter_type_registry) @source = expression @parameter_types = [] regexp = '^' match_offset = 0 expression = expression.gsub(ESCAPE_REGEXP, '\\\\\1') # Create non-capturing, optional capture groups from parenthesis expression = expression.gsub(OPTIONAL_REGEXP, '(?:\1)?') expression = expression.gsub(ALTERNATIVE_NON_WHITESPACE_TEXT_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
#source ⇒ Object (readonly)
Returns the value of attribute source.
14 15 16 |
# File 'lib/cucumber/cucumber_expressions/cucumber_expression.rb', line 14 def source @source end |
Instance Method Details
#match(text) ⇒ Object
52 53 54 |
# File 'lib/cucumber/cucumber_expressions/cucumber_expression.rb', line 52 def match(text) Argument.build(@tree_regexp, text, @parameter_types) end |
#regexp ⇒ Object
56 57 58 |
# File 'lib/cucumber/cucumber_expressions/cucumber_expression.rb', line 56 def regexp @tree_regexp.regexp end |
#to_s ⇒ Object
60 61 62 |
# File 'lib/cucumber/cucumber_expressions/cucumber_expression.rb', line 60 def to_s @source.inspect end |