summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNARUSE, Yui <naruse@airemix.jp>2019-12-04 01:26:29 +0900
committerNARUSE, Yui <naruse@airemix.jp>2019-12-04 06:40:54 +0900
commit8852fa876039ed177fd5e867f36177d8a9ff411c (patch)
tree56dd2c368bfe7cc694bf333c3ef987e2fee1b727
parent08074eb71229b4c9f669f7bfb215bbb43525bfc0 (diff)
Revert "Regexp#match{?} with nil raises TypeError as String, Symbol (#1506)"
This reverts commit 2a22a6b2d8465934e75520a7fdcf522d50890caf. Revert [Feature #13083]
-rw-r--r--re.c2
-rw-r--r--spec/ruby/core/regexp/match_spec.rb40
-rw-r--r--test/ruby/test_regexp.rb4
-rw-r--r--test/ruby/test_string.rb5
-rw-r--r--test/ruby/test_symbol.rb5
5 files changed, 15 insertions, 41 deletions
diff --git a/re.c b/re.c
index 6dfb9f273f..b15c3a07a8 100644
--- a/re.c
+++ b/re.c
@@ -3299,7 +3299,6 @@ rb_reg_match_m(int argc, VALUE *argv, VALUE re)
pos = 0;
}
- str = SYMBOL_P(str) ? rb_sym2str(str) : StringValue(str);
pos = reg_match_pos(re, &str, pos);
if (pos < 0) {
rb_backref_set(Qnil);
@@ -3345,6 +3344,7 @@ rb_reg_match_p(VALUE re, VALUE str, long pos)
const UChar *start, *end;
int tmpreg;
+ if (NIL_P(str)) return Qfalse;
str = SYMBOL_P(str) ? rb_sym2str(str) : StringValue(str);
if (pos) {
if (pos < 0) {
diff --git a/spec/ruby/core/regexp/match_spec.rb b/spec/ruby/core/regexp/match_spec.rb
index 89cb3be4f6..80dbfb4c10 100644
--- a/spec/ruby/core/regexp/match_spec.rb
+++ b/spec/ruby/core/regexp/match_spec.rb
@@ -5,15 +5,15 @@ describe :regexp_match, shared: true do
it "returns nil if there is no match" do
/xyz/.send(@method,"abxyc").should be_nil
end
+
+ it "returns nil if the object is nil" do
+ /\w+/.send(@method, nil).should be_nil
+ end
end
describe "Regexp#=~" do
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
(/(.)(.)(.)/ =~ "abc").should == 0
end
@@ -91,21 +91,13 @@ describe "Regexp#match" do
end
end
- ruby_version_is ""..."2.7" do
- it "resets $~ if passed nil" do
- # set $~
- /./.match("a")
- $~.should be_kind_of(MatchData)
-
- /1/.match(nil)
- $~.should be_nil
- end
- end
+ it "resets $~ if passed nil" do
+ # set $~
+ /./.match("a")
+ $~.should be_kind_of(MatchData)
- ruby_version_is "2.7" do
- it "raises TypeError when the given argument is nil" do
- -> { /foo/.match(nil) }.should raise_error(TypeError)
- end
+ /1/.match(nil)
+ $~.should be_nil
end
it "raises TypeError when the given argument cannot be coerced to String" do
@@ -141,16 +133,8 @@ describe "Regexp#match?" do
/str/i.match?('string', 1).should be_false
end
- ruby_version_is ""..."2.7" do
- 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
+ it "returns false when given nil" do
+ /./.match?(nil).should be_false
end
end
diff --git a/test/ruby/test_regexp.rb b/test/ruby/test_regexp.rb
index d48eef5366..a1d49c595a 100644
--- a/test/ruby/test_regexp.rb
+++ b/test/ruby/test_regexp.rb
@@ -539,7 +539,7 @@ class TestRegexp < Test::Unit::TestCase
end
def test_match
- assert_raise(TypeError) { //.match(nil) }
+ assert_nil(//.match(nil))
assert_equal("abc", /.../.match(:abc)[0])
assert_raise(TypeError) { /.../.match(Object.new)[0] }
assert_equal("bc", /../.match('abc', 1)[0])
@@ -561,10 +561,10 @@ class TestRegexp < Test::Unit::TestCase
/backref/ =~ 'backref'
# must match here, but not in a separate method, e.g., assert_send,
# to check if $~ is affected or not.
+ assert_equal(false, //.match?(nil))
assert_equal(true, //.match?(""))
assert_equal(true, /.../.match?(:abc))
assert_raise(TypeError) { /.../.match?(Object.new) }
- assert_raise(TypeError) { //.match?(nil) }
assert_equal(true, /b/.match?('abc'))
assert_equal(true, /b/.match?('abc', 1))
assert_equal(true, /../.match?('abc', 1))
diff --git a/test/ruby/test_string.rb b/test/ruby/test_string.rb
index a86e26c774..0276d6a14d 100644
--- a/test/ruby/test_string.rb
+++ b/test/ruby/test_string.rb
@@ -2469,7 +2469,6 @@ CODE
def test_match_method
assert_equal("bar", "foobarbaz".match(/bar/).to_s)
- assert_raise(TypeError) { "".match(nil) }
o = Regexp.new('foo')
def o.match(x, y, z); x + y + z; end
@@ -2525,10 +2524,6 @@ CODE
assert_equal('backref', $&)
end
- def test_match_p_nil
- assert_raise(TypeError) { ''.match?(nil) }
- end
-
def test_clear
s = "foo" * 100
s.clear
diff --git a/test/ruby/test_symbol.rb b/test/ruby/test_symbol.rb
index 75335f180d..660f2e1574 100644
--- a/test/ruby/test_symbol.rb
+++ b/test/ruby/test_symbol.rb
@@ -367,7 +367,6 @@ class TestSymbol < Test::Unit::TestCase
def test_match_method
assert_equal("bar", :"foobarbaz".match(/bar/).to_s)
- assert_raise(TypeError) { :"".match(nil) }
o = Regexp.new('foo')
def o.match(x, y, z); x + y + z; end
@@ -421,10 +420,6 @@ class TestSymbol < Test::Unit::TestCase
assert_equal('backref', $&)
end
- def test_match_p_nil
- assert_raise(TypeError) { :''.match?(nil) }
- end
-
def test_symbol_popped
assert_nothing_raised { eval('a = 1; :"#{ a }"; 1') }
end