Skip to content

Commit

Permalink
Fix transformation of regex with lookahead/lookbehind
Browse files Browse the repository at this point in the history
  • Loading branch information
Артём Большаков committed Jan 28, 2015
1 parent d946609 commit cd0920c
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
4 changes: 3 additions & 1 deletion lib/cucumber/rb_support/rb_transform.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ def to_s

private
def convert_captures(regexp_source)
regexp_source.gsub(/(\()(?!\?:)/,'(?:')
regexp_source
.gsub(/(\()(?!\?[<:=!])/,'(?:')
.gsub(/(\(\?<)(?![=!])/,'(?:<')
end

def strip_captures(regexp_source)
Expand Down
20 changes: 20 additions & 0 deletions spec/cucumber/rb_support/rb_transform_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,26 @@ def transform(regexp)
end

describe "#to_s" do
it "do not touch positive lookahead captures" do
expect(transform(/^xy(?=z)/).to_s).to eq "xy(?=z)"
end

it "do not touch negative lookahead captures" do
expect(transform(/^xy(?!z)/).to_s).to eq "xy(?!z)"
end

it "do not touch positive lookbehind captures" do
expect(transform(/^xy(?<=z)/).to_s).to eq "xy(?<=z)"
end

it "do not touch negative lookbehind captures" do
expect(transform(/^xy(?<!z)/).to_s).to eq "xy(?<!z)"
end

it "converts named captures" do
expect(transform(/^(?<str>xyz)/).to_s).to eq "(?:<str>xyz)"
end

it "converts captures groups to non-capture groups" do
expect(transform(/(a|b)bc/).to_s).to eq "(?:a|b)bc"
end
Expand Down

0 comments on commit cd0920c

Please sign in to comment.