diff options
Diffstat (limited to 'spec/ruby/core/regexp/match_spec.rb')
| -rw-r--r-- | spec/ruby/core/regexp/match_spec.rb | 100 |
1 files changed, 49 insertions, 51 deletions
diff --git a/spec/ruby/core/regexp/match_spec.rb b/spec/ruby/core/regexp/match_spec.rb index 872c3ff59e..276cecc8e4 100644 --- a/spec/ruby/core/regexp/match_spec.rb +++ b/spec/ruby/core/regexp/match_spec.rb @@ -1,18 +1,18 @@ # -*- encoding: utf-8 -*- -require File.expand_path('../../../spec_helper', __FILE__) +require_relative '../../spec_helper' describe :regexp_match, shared: true do it "returns nil if there is no match" do - /xyz/.send(@method,"abxyc").should be_nil + /xyz/.send(@method,"abxyc").should == nil end it "returns nil if the object is nil" do - /\w+/.send(@method, nil).should be_nil + /\w+/.send(@method, nil).should == nil end end describe "Regexp#=~" do - it_behaves_like(:regexp_match, :=~) + it_behaves_like :regexp_match, :=~ it "returns the index of the first character of the matching region" do (/(.)(.)(.)/ =~ "abc").should == 0 @@ -24,18 +24,22 @@ describe "Regexp#=~" do end describe "Regexp#match" do - it_behaves_like(:regexp_match, :match) + it_behaves_like :regexp_match, :match it "returns a MatchData object" do - /(.)(.)(.)/.match("abc").should be_kind_of(MatchData) + /(.)(.)(.)/.match("abc").should.is_a?(MatchData) end it "returns a MatchData object, when argument is a Symbol" do - /(.)(.)(.)/.match(:abc).should be_kind_of(MatchData) + /(.)(.)(.)/.match(:abc).should.is_a?(MatchData) end it "raises a TypeError on an uninitialized Regexp" do - lambda { Regexp.allocate.match('foo') }.should raise_error(TypeError) + -> { Regexp.allocate.match('foo') }.should.raise(TypeError) + end + + it "raises TypeError on an uninitialized Regexp" do + -> { Regexp.allocate.match('foo'.encode("UTF-16LE")) }.should.raise(TypeError) end describe "with [string, position]" do @@ -44,15 +48,13 @@ describe "Regexp#match" do /(.).(.)/.match("01234", 1).captures.should == ["1", "3"] end - with_feature :encoding do - it "uses the start as a character offset" do - /(.).(.)/.match("零一二三四", 1).captures.should == ["一", "三"] - end + it "uses the start as a character offset" do + /(.).(.)/.match("零一二三四", 1).captures.should == ["一", "三"] + end - it "raises an ArgumentError for an invalid encoding" do - x96 = ([150].pack('C')).force_encoding('utf-8') - lambda { /(.).(.)/.match("Hello, #{x96} world!", 1) }.should raise_error(ArgumentError) - end + it "raises an ArgumentError for an invalid encoding" do + x96 = ([150].pack('C')).force_encoding('utf-8') + -> { /(.).(.)/.match("Hello, #{x96} world!", 1) }.should.raise(ArgumentError) end end @@ -61,22 +63,20 @@ describe "Regexp#match" do /(.).(.)/.match("01234", -4).captures.should == ["1", "3"] end - with_feature :encoding do - it "uses the start as a character offset" do - /(.).(.)/.match("零一二三四", -4).captures.should == ["一", "三"] - end + it "uses the start as a character offset" do + /(.).(.)/.match("零一二三四", -4).captures.should == ["一", "三"] + end - it "raises an ArgumentError for an invalid encoding" do - x96 = ([150].pack('C')).force_encoding('utf-8') - lambda { /(.).(.)/.match("Hello, #{x96} world!", -1) }.should raise_error(ArgumentError) - end + it "raises an ArgumentError for an invalid encoding" do + x96 = ([150].pack('C')).force_encoding('utf-8') + -> { /(.).(.)/.match("Hello, #{x96} world!", -1) }.should.raise(ArgumentError) end end describe "when passed a block" do it "yields the MatchData" do /./.match("abc") {|m| ScratchPad.record m } - ScratchPad.recorded.should be_kind_of(MatchData) + ScratchPad.recorded.should.is_a?(MatchData) end it "returns the block result" do @@ -94,49 +94,47 @@ describe "Regexp#match" do it "resets $~ if passed nil" do # set $~ /./.match("a") - $~.should be_kind_of(MatchData) + $~.should.is_a?(MatchData) /1/.match(nil) - $~.should be_nil + $~.should == nil end - it "raises TypeError when the given argument cannot be coarce to String" do + it "raises TypeError when the given argument cannot be coerced to String" do f = 1 - lambda { /foo/.match(f)[0] }.should raise_error(TypeError) + -> { /foo/.match(f)[0] }.should.raise(TypeError) end it "raises TypeError when the given argument is an Exception" do f = Exception.new("foo") - lambda { /foo/.match(f)[0] }.should raise_error(TypeError) + -> { /foo/.match(f)[0] }.should.raise(TypeError) end end -ruby_version_is "2.4" do - describe "Regexp#match?" do - before :each do - # Resetting Regexp.last_match - /DONTMATCH/.match '' - end +describe "Regexp#match?" do + before :each do + # Resetting Regexp.last_match + /DONTMATCH/.match '' + end - context "when matches the given value" do - it "returns true but does not set Regexp.last_match" do - /string/i.match?('string').should be_true - Regexp.last_match.should be_nil - end + context "when matches the given value" do + it "returns true but does not set Regexp.last_match" do + /string/i.match?('string').should == true + Regexp.last_match.should == nil end + end - it "returns false when does not match the given value" do - /STRING/.match?('string').should be_false - end + it "returns false when does not match the given value" do + /STRING/.match?('string').should == false + end - it "takes matching position as the 2nd argument" do - /str/i.match?('string', 0).should be_true - /str/i.match?('string', 1).should be_false - end + it "takes matching position as the 2nd argument" do + /str/i.match?('string', 0).should == true + /str/i.match?('string', 1).should == false + end - it "returns false when given nil" do - /./.match?(nil).should be_false - end + it "returns false when given nil" do + /./.match?(nil).should == false end end |
