Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cucumber-expressions: Allow parentheses to be explicitly escaped #334

Merged
merged 2 commits into from
Feb 10, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions cucumber-expressions/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,16 @@ It would also match this text:

I have 42 cucumbers in my belly

If you ever need to match those parentheses literally, you can escape the
first opening parenthesis with a backslash:

I have {int} cucumber(s) in my belly \(amazing!)

This expression would match the following examples:

I have 1 cucumber in my belly (amazing!)
I have 42 cucumbers in my belly (amazing!)

## Alternative text

Sometimes you want to relax your language, to make it flow better. For example:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ class CucumberExpression
# Does not include (){} characters because they have special meaning
ESCAPE_REGEXP = /([\\^\[$.|?*+\]])/
PARAMETER_REGEXP = /{([^}]+)}/
OPTIONAL_REGEXP = /\(([^)]+)\)/
# Parentheses will be double-escaped due to ESCAPE_REGEXP
OPTIONAL_REGEXP = /([\\][\\])?\(([^)]+)\)/
ALTERNATIVE_NON_WHITESPACE_TEXT_REGEXP = /([^\s^\/]+)((\/[^\s^\/]+)+)/

attr_reader :source
Expand All @@ -19,10 +20,18 @@ def initialize(expression, parameter_type_registry)
regexp = '^'
match_offset = 0

# This will cause explicitly-escaped parentheses to be double-escaped
expression = expression.gsub(ESCAPE_REGEXP, '\\\\\1')

# Create non-capturing, optional capture groups from parenthesis
expression = expression.gsub(OPTIONAL_REGEXP, '(?:\1)?')
expression = expression.gsub(OPTIONAL_REGEXP) do |match|
# look for double-escaped parentheses
if $1 == '\\\\'
"\\(#{$2}\\)"
else
"(?:#{$2})?"
end
end

expression = expression.gsub(ALTERNATIVE_NON_WHITESPACE_TEXT_REGEXP) do |_|
"(?:#{$1}#{$2.tr('/', '|')})"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ module CucumberExpressions
it('matches multiple double quoted strings') do
expect(match('three {string} and {string} mice', 'three "blind" and "crippled" mice')).to eq(['blind', 'crippled'])
end

it('matches single quoted string') do
expect(match('three {string} mice', "three 'blind' mice")).to eq(['blind'])
end
Expand Down Expand Up @@ -55,6 +55,10 @@ module CucumberExpressions
expect(match('three {string} mice', "three 'bl\\'nd' mice")).to eq(["bl'nd"])
end

it 'matches escaped parentheses' do
expect(match('three \\(exceptionally) {string} mice', 'three (exceptionally) "blind" mice')).to eq(['blind'])
end

it "matches int" do
expect(match("{int}", "22")).to eq([22])
end
Expand Down