mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
Regexp#match{?} with nil raises TypeError as String, Symbol (#1506)
* {String|Symbol}#match{?} with nil returns falsy To improve consistency with Regexp#match{?} * String#match(nil) returns `nil` instead of TypeError * String#match?(nil) returns `false` instead of TypeError * Symbol#match(nil) returns `nil` instead of TypeError * Symbol#match?(nil) returns `false` instead of TypeError * Prefer exception * Follow empty ENV * Drop outdated specs * Write ruby/spec for above https://github.com/ruby/ruby/pull/1506/files#r183242981 * Fix merge miss
This commit is contained in:
parent
41457dcbe0
commit
2a22a6b2d8
5 changed files with 41 additions and 15 deletions
2
re.c
2
re.c
|
@ -3323,6 +3323,7 @@ rb_reg_match_m(int argc, VALUE *argv, VALUE re)
|
||||||
pos = 0;
|
pos = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
str = SYMBOL_P(str) ? rb_sym2str(str) : StringValue(str);
|
||||||
pos = reg_match_pos(re, &str, pos);
|
pos = reg_match_pos(re, &str, pos);
|
||||||
if (pos < 0) {
|
if (pos < 0) {
|
||||||
rb_backref_set(Qnil);
|
rb_backref_set(Qnil);
|
||||||
|
@ -3368,7 +3369,6 @@ rb_reg_match_p(VALUE re, VALUE str, long pos)
|
||||||
const UChar *start, *end;
|
const UChar *start, *end;
|
||||||
int tmpreg;
|
int tmpreg;
|
||||||
|
|
||||||
if (NIL_P(str)) return Qfalse;
|
|
||||||
str = SYMBOL_P(str) ? rb_sym2str(str) : StringValue(str);
|
str = SYMBOL_P(str) ? rb_sym2str(str) : StringValue(str);
|
||||||
if (pos) {
|
if (pos) {
|
||||||
if (pos < 0) {
|
if (pos < 0) {
|
||||||
|
|
|
@ -5,15 +5,15 @@ describe :regexp_match, shared: true do
|
||||||
it "returns nil if there is no match" do
|
it "returns nil if there is no match" do
|
||||||
/xyz/.send(@method,"abxyc").should be_nil
|
/xyz/.send(@method,"abxyc").should be_nil
|
||||||
end
|
end
|
||||||
|
|
||||||
it "returns nil if the object is nil" do
|
|
||||||
/\w+/.send(@method, nil).should be_nil
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
describe "Regexp#=~" do
|
describe "Regexp#=~" do
|
||||||
it_behaves_like :regexp_match, :=~
|
it_behaves_like :regexp_match, :=~
|
||||||
|
|
||||||
|
it "returns nil if the object is nil" do
|
||||||
|
(/\w+/ =~ nil).should be_nil
|
||||||
|
end
|
||||||
|
|
||||||
it "returns the index of the first character of the matching region" do
|
it "returns the index of the first character of the matching region" do
|
||||||
(/(.)(.)(.)/ =~ "abc").should == 0
|
(/(.)(.)(.)/ =~ "abc").should == 0
|
||||||
end
|
end
|
||||||
|
@ -87,13 +87,21 @@ describe "Regexp#match" do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
it "resets $~ if passed nil" do
|
ruby_version_is ""..."2.7" do
|
||||||
# set $~
|
it "resets $~ if passed nil" do
|
||||||
/./.match("a")
|
# set $~
|
||||||
$~.should be_kind_of(MatchData)
|
/./.match("a")
|
||||||
|
$~.should be_kind_of(MatchData)
|
||||||
|
|
||||||
|
/1/.match(nil)
|
||||||
|
$~.should be_nil
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
/1/.match(nil)
|
ruby_version_is "2.7" do
|
||||||
$~.should be_nil
|
it "raises TypeError when the given argument is nil" do
|
||||||
|
-> { /foo/.match(nil) }.should raise_error(TypeError)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
it "raises TypeError when the given argument cannot be coerced to String" do
|
it "raises TypeError when the given argument cannot be coerced to String" do
|
||||||
|
@ -129,8 +137,16 @@ describe "Regexp#match?" do
|
||||||
/str/i.match?('string', 1).should be_false
|
/str/i.match?('string', 1).should be_false
|
||||||
end
|
end
|
||||||
|
|
||||||
it "returns false when given nil" do
|
ruby_version_is ""..."2.7" do
|
||||||
/./.match?(nil).should be_false
|
it "returns false when given nil" do
|
||||||
|
/./.match?(nil).should be_false
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
ruby_version_is "2.7" do
|
||||||
|
it "raises TypeError when given nil" do
|
||||||
|
-> { /./.match?(nil) }.should raise_error(TypeError)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -539,7 +539,7 @@ class TestRegexp < Test::Unit::TestCase
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_match
|
def test_match
|
||||||
assert_nil(//.match(nil))
|
assert_raise(TypeError) { //.match(nil) }
|
||||||
assert_equal("abc", /.../.match(:abc)[0])
|
assert_equal("abc", /.../.match(:abc)[0])
|
||||||
assert_raise(TypeError) { /.../.match(Object.new)[0] }
|
assert_raise(TypeError) { /.../.match(Object.new)[0] }
|
||||||
assert_equal("bc", /../.match('abc', 1)[0])
|
assert_equal("bc", /../.match('abc', 1)[0])
|
||||||
|
@ -561,10 +561,10 @@ class TestRegexp < Test::Unit::TestCase
|
||||||
/backref/ =~ 'backref'
|
/backref/ =~ 'backref'
|
||||||
# must match here, but not in a separate method, e.g., assert_send,
|
# must match here, but not in a separate method, e.g., assert_send,
|
||||||
# to check if $~ is affected or not.
|
# to check if $~ is affected or not.
|
||||||
assert_equal(false, //.match?(nil))
|
|
||||||
assert_equal(true, //.match?(""))
|
assert_equal(true, //.match?(""))
|
||||||
assert_equal(true, /.../.match?(:abc))
|
assert_equal(true, /.../.match?(:abc))
|
||||||
assert_raise(TypeError) { /.../.match?(Object.new) }
|
assert_raise(TypeError) { /.../.match?(Object.new) }
|
||||||
|
assert_raise(TypeError) { //.match?(nil) }
|
||||||
assert_equal(true, /b/.match?('abc'))
|
assert_equal(true, /b/.match?('abc'))
|
||||||
assert_equal(true, /b/.match?('abc', 1))
|
assert_equal(true, /b/.match?('abc', 1))
|
||||||
assert_equal(true, /../.match?('abc', 1))
|
assert_equal(true, /../.match?('abc', 1))
|
||||||
|
|
|
@ -2512,6 +2512,7 @@ CODE
|
||||||
|
|
||||||
def test_match_method
|
def test_match_method
|
||||||
assert_equal("bar", "foobarbaz".match(/bar/).to_s)
|
assert_equal("bar", "foobarbaz".match(/bar/).to_s)
|
||||||
|
assert_raise(TypeError) { "".match(nil) }
|
||||||
|
|
||||||
o = Regexp.new('foo')
|
o = Regexp.new('foo')
|
||||||
def o.match(x, y, z); x + y + z; end
|
def o.match(x, y, z); x + y + z; end
|
||||||
|
@ -2567,6 +2568,10 @@ CODE
|
||||||
assert_equal('backref', $&)
|
assert_equal('backref', $&)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_match_p_nil
|
||||||
|
assert_raise(TypeError) { ''.match?(nil) }
|
||||||
|
end
|
||||||
|
|
||||||
def test_clear
|
def test_clear
|
||||||
s = "foo" * 100
|
s = "foo" * 100
|
||||||
s.clear
|
s.clear
|
||||||
|
|
|
@ -367,6 +367,7 @@ class TestSymbol < Test::Unit::TestCase
|
||||||
|
|
||||||
def test_match_method
|
def test_match_method
|
||||||
assert_equal("bar", :"foobarbaz".match(/bar/).to_s)
|
assert_equal("bar", :"foobarbaz".match(/bar/).to_s)
|
||||||
|
assert_raise(TypeError) { :"".match(nil) }
|
||||||
|
|
||||||
o = Regexp.new('foo')
|
o = Regexp.new('foo')
|
||||||
def o.match(x, y, z); x + y + z; end
|
def o.match(x, y, z); x + y + z; end
|
||||||
|
@ -420,6 +421,10 @@ class TestSymbol < Test::Unit::TestCase
|
||||||
assert_equal('backref', $&)
|
assert_equal('backref', $&)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_match_p_nil
|
||||||
|
assert_raise(TypeError) { :''.match?(nil) }
|
||||||
|
end
|
||||||
|
|
||||||
def test_symbol_popped
|
def test_symbol_popped
|
||||||
assert_nothing_raised { eval('a = 1; :"#{ a }"; 1') }
|
assert_nothing_raised { eval('a = 1; :"#{ a }"; 1') }
|
||||||
end
|
end
|
||||||
|
|
Loading…
Add table
Reference in a new issue