1 2 3 4 5 6 7 8 9 10 11 12 13 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 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
|
require 'cucumber/cucumber_expressions/regular_expression'
require 'cucumber/cucumber_expressions/parameter_type_registry'
module Cucumber
module CucumberExpressions
describe RegularExpression do
it "documents match arguments" do
parameter_type_registry = ParameterTypeRegistry.new
### [capture-match-arguments]
expr = /I have (\d+) cukes? in my (\w*) now/
expression = RegularExpression.new(expr, parameter_type_registry)
args = expression.match("I have 7 cukes in my belly now")
expect( args[0].value(nil) ).to eq(7)
expect( args[1].value(nil) ).to eq("belly")
### [capture-match-arguments]
end
it "does no transform by default" do
expect( match(/(\d\d)/, "22") ).to eq(["22"])
end
it "does not transform anonymous" do
expect( match(/(.*)/, "22") ).to eq(["22"])
end
it "transforms negative int" do
expect( match(/(-?\d+)/, "-22") ).to eq([-22])
end
it "transforms positive int" do
expect( match(/(\d+)/, "22") ).to eq([22])
end
it "returns nil when there is no match" do
expect( match(/hello/, "world") ).to be_nil
end
it "matches nested capture group without match" do
expect( match(/^a user( named "([^"]*)")?$/, 'a user') ).to eq([nil])
end
it "matches nested capture group with match" do
expect( match(/^a user( named "([^"]*)")?$/, 'a user named "Charlie"') ).to eq(['Charlie'])
end
it "ignores non capturing groups" do
expect( match(
/(\S+) ?(can|cannot) (?:delete|cancel) the (\d+)(?:st|nd|rd|th) (attachment|slide) ?(?:upload)?/,
"I can cancel the 1st slide upload")
).to eq(["I", "can", 1, "slide"])
end
it "matches capture group nested in optional one" do
regexp = /^a (pre-commercial transaction |pre buyer fee model )?purchase(?: for \$(\d+))?$/
expect( match(regexp, 'a purchase') ).to eq([nil, nil])
expect( match(regexp, 'a purchase for $33') ).to eq([nil, 33])
expect( match(regexp, 'a pre buyer fee model purchase') ).to eq(['pre buyer fee model ', nil])
end
it "works with escaped parenthesis" do
expect( match(/Across the line\(s\)/, 'Across the line(s)') ).to eq([])
end
it "exposes source and regexp" do
regexp = /I have (\d+) cukes? in my (\+) now/
expression = RegularExpression.new(regexp, ParameterTypeRegistry.new)
expect(expression.regexp).to eq(regexp)
expect(expression.source).to eq(regexp.source)
end
def match(expression, text)
regular_expression = RegularExpression.new(expression, ParameterTypeRegistry.new)
arguments = regular_expression.match(text)
return nil if arguments.nil?
arguments.map { |arg| arg.value(nil) }
end
end
end
end
|