2018-03-04 10:09:32 -05:00
|
|
|
require_relative '../../spec_helper'
|
|
|
|
require_relative '../fixtures/classes'
|
2017-05-07 08:04:49 -04:00
|
|
|
|
2019-02-07 11:35:33 -05:00
|
|
|
describe "Regexps with modifiers" do
|
2017-05-07 08:04:49 -04:00
|
|
|
it "supports /i (case-insensitive)" do
|
|
|
|
/foo/i.match("FOO").to_a.should == ["FOO"]
|
|
|
|
end
|
|
|
|
|
|
|
|
it "supports /m (multiline)" do
|
|
|
|
/foo.bar/m.match("foo\nbar").to_a.should == ["foo\nbar"]
|
|
|
|
/foo.bar/.match("foo\nbar").should be_nil
|
|
|
|
end
|
|
|
|
|
|
|
|
it "supports /x (extended syntax)" do
|
|
|
|
/\d +/x.match("abc123").to_a.should == ["123"] # Quantifiers can be separated from the expression they apply to
|
|
|
|
end
|
|
|
|
|
|
|
|
it "supports /o (once)" do
|
|
|
|
2.times do |i|
|
|
|
|
/#{i}/o.should == /0/
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it "invokes substitutions for /o only once" do
|
|
|
|
ScratchPad.record []
|
|
|
|
o = Object.new
|
|
|
|
def o.to_s
|
|
|
|
ScratchPad << :to_s
|
|
|
|
"class_with_to_s"
|
|
|
|
end
|
|
|
|
eval "2.times { /#{o}/o }"
|
|
|
|
ScratchPad.recorded.should == [:to_s]
|
|
|
|
end
|
|
|
|
|
|
|
|
it "supports modifier combinations" do
|
|
|
|
/foo/imox.match("foo").to_a.should == ["foo"]
|
|
|
|
/foo/imoximox.match("foo").to_a.should == ["foo"]
|
|
|
|
|
2019-07-27 06:40:09 -04:00
|
|
|
-> { eval('/foo/a') }.should raise_error(SyntaxError)
|
2017-05-07 08:04:49 -04:00
|
|
|
end
|
|
|
|
|
2019-04-27 12:53:23 -04:00
|
|
|
it "supports (?~) (absent operator)" do
|
|
|
|
Regexp.new("(?~foo)").match("hello").to_a.should == ["hello"]
|
|
|
|
"foo".scan(Regexp.new("(?~foo)")).should == ["fo","o",""]
|
2017-10-28 11:15:48 -04:00
|
|
|
end
|
|
|
|
|
2017-05-07 08:04:49 -04:00
|
|
|
it "supports (?imx-imx) (inline modifiers)" do
|
|
|
|
/(?i)foo/.match("FOO").to_a.should == ["FOO"]
|
|
|
|
/foo(?i)/.match("FOO").should be_nil
|
|
|
|
# Interaction with /i
|
|
|
|
/(?-i)foo/i.match("FOO").should be_nil
|
|
|
|
/foo(?-i)/i.match("FOO").to_a.should == ["FOO"]
|
|
|
|
# Multiple uses
|
|
|
|
/foo (?i)bar (?-i)baz/.match("foo BAR baz").to_a.should == ["foo BAR baz"]
|
|
|
|
/foo (?i)bar (?-i)baz/.match("foo BAR BAZ").should be_nil
|
|
|
|
|
|
|
|
/(?m)./.match("\n").to_a.should == ["\n"]
|
|
|
|
/.(?m)/.match("\n").should be_nil
|
|
|
|
# Interaction with /m
|
|
|
|
/(?-m)./m.match("\n").should be_nil
|
|
|
|
/.(?-m)/m.match("\n").to_a.should == ["\n"]
|
|
|
|
# Multiple uses
|
|
|
|
/. (?m). (?-m)./.match(". \n .").to_a.should == [". \n ."]
|
|
|
|
/. (?m). (?-m)./.match(". \n \n").should be_nil
|
|
|
|
|
|
|
|
/(?x) foo /.match("foo").to_a.should == ["foo"]
|
|
|
|
/ foo (?x)/.match("foo").should be_nil
|
|
|
|
# Interaction with /x
|
|
|
|
/(?-x) foo /x.match("foo").should be_nil
|
|
|
|
/ foo (?-x)/x.match("foo").to_a.should == ["foo"]
|
|
|
|
# Multiple uses
|
|
|
|
/( foo )(?x)( bar )(?-x)( baz )/.match(" foo bar baz ").to_a.should == [" foo bar baz ", " foo ", "bar", " baz "]
|
|
|
|
/( foo )(?x)( bar )(?-x)( baz )/.match(" foo barbaz").should be_nil
|
|
|
|
|
|
|
|
# Parsing
|
|
|
|
/(?i-i)foo/.match("FOO").should be_nil
|
|
|
|
/(?ii)foo/.match("FOO").to_a.should == ["FOO"]
|
|
|
|
/(?-)foo/.match("foo").to_a.should == ["foo"]
|
2019-07-27 06:40:09 -04:00
|
|
|
-> { eval('/(?o)/') }.should raise_error(SyntaxError)
|
2017-05-07 08:04:49 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "supports (?imx-imx:expr) (scoped inline modifiers)" do
|
|
|
|
/foo (?i:bar) baz/.match("foo BAR baz").to_a.should == ["foo BAR baz"]
|
|
|
|
/foo (?i:bar) baz/.match("foo BAR BAZ").should be_nil
|
|
|
|
/foo (?-i:bar) baz/i.match("foo BAR BAZ").should be_nil
|
|
|
|
|
|
|
|
/. (?m:.) ./.match(". \n .").to_a.should == [". \n ."]
|
|
|
|
/. (?m:.) ./.match(". \n \n").should be_nil
|
|
|
|
/. (?-m:.) ./m.match("\n \n \n").should be_nil
|
|
|
|
|
|
|
|
/( foo )(?x: bar )( baz )/.match(" foo bar baz ").to_a.should == [" foo bar baz ", " foo ", " baz "]
|
|
|
|
/( foo )(?x: bar )( baz )/.match(" foo barbaz").should be_nil
|
|
|
|
/( foo )(?-x: bar )( baz )/x.match("foo bar baz").to_a.should == ["foo bar baz", "foo", "baz"]
|
|
|
|
|
|
|
|
# Parsing
|
|
|
|
/(?i-i:foo)/.match("FOO").should be_nil
|
|
|
|
/(?ii:foo)/.match("FOO").to_a.should == ["FOO"]
|
|
|
|
/(?-:)foo/.match("foo").to_a.should == ["foo"]
|
2019-07-27 06:40:09 -04:00
|
|
|
-> { eval('/(?o:)/') }.should raise_error(SyntaxError)
|
2017-05-07 08:04:49 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "supports . with /m" do
|
|
|
|
# Basic matching
|
|
|
|
/./m.match("\n").to_a.should == ["\n"]
|
|
|
|
end
|
|
|
|
|
|
|
|
it "supports ASII/Unicode modifiers" do
|
|
|
|
eval('/(?a)[[:alpha:]]+/').match("a\u3042").to_a.should == ["a"]
|
|
|
|
eval('/(?d)[[:alpha:]]+/').match("a\u3042").to_a.should == ["a\u3042"]
|
|
|
|
eval('/(?u)[[:alpha:]]+/').match("a\u3042").to_a.should == ["a\u3042"]
|
|
|
|
eval('/(?a)\w+/').match("a\u3042").to_a.should == ["a"]
|
|
|
|
eval('/(?d)\w+/').match("a\u3042").to_a.should == ["a"]
|
|
|
|
eval('/(?u)\w+/').match("a\u3042").to_a.should == ["a\u3042"]
|
|
|
|
end
|
|
|
|
end
|