diff options
author | Kenichi Kamiya <kachick1@gmail.com> | 2019-10-17 17:44:46 +0900 |
---|---|---|
committer | Kazuhiro NISHIYAMA <znz@users.noreply.github.com> | 2019-10-17 17:44:46 +0900 |
commit | 2a22a6b2d8465934e75520a7fdcf522d50890caf (patch) | |
tree | 49135af238d55ebe13195dc1fd36c3604749adb3 /spec | |
parent | 41457dcbe050a698c357b516d0a012b1b584643a (diff) |
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
Diffstat (limited to 'spec')
-rw-r--r-- | spec/ruby/core/regexp/match_spec.rb | 40 |
1 files changed, 28 insertions, 12 deletions
diff --git a/spec/ruby/core/regexp/match_spec.rb b/spec/ruby/core/regexp/match_spec.rb index a68448b39c..cac2405c94 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 @@ -87,13 +87,21 @@ describe "Regexp#match" do end end - it "resets $~ if passed nil" do - # set $~ - /./.match("a") - $~.should be_kind_of(MatchData) + 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 - /1/.match(nil) - $~.should be_nil + ruby_version_is "2.7" do + it "raises TypeError when the given argument is nil" do + -> { /foo/.match(nil) }.should raise_error(TypeError) + end end 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 end - it "returns false when given nil" do - /./.match?(nil).should be_false + 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 end end |