summaryrefslogtreecommitdiff
path: root/spec/ruby/core/regexp/last_match_spec.rb
diff options
context:
space:
mode:
Diffstat (limited to 'spec/ruby/core/regexp/last_match_spec.rb')
-rw-r--r--spec/ruby/core/regexp/last_match_spec.rb56
1 files changed, 56 insertions, 0 deletions
diff --git a/spec/ruby/core/regexp/last_match_spec.rb b/spec/ruby/core/regexp/last_match_spec.rb
new file mode 100644
index 0000000000..6c256cc1cf
--- /dev/null
+++ b/spec/ruby/core/regexp/last_match_spec.rb
@@ -0,0 +1,56 @@
+require_relative '../../spec_helper'
+
+describe "Regexp.last_match" do
+ it "returns MatchData instance when not passed arguments" do
+ /c(.)t/ =~ 'cat'
+
+ Regexp.last_match.should.is_a?(MatchData)
+ end
+
+ it "returns the nth field in this MatchData when passed an Integer" do
+ /c(.)t/ =~ 'cat'
+ Regexp.last_match(1).should == 'a'
+ end
+
+ it "returns nil when there is no match" do
+ /foo/ =~ "TEST123"
+ Regexp.last_match(:test).should == nil
+ Regexp.last_match(1).should == nil
+ Regexp.last_match(Object.new).should == nil
+ Regexp.last_match("test").should == nil
+ end
+
+ describe "when given a Symbol" do
+ it "returns a named capture" do
+ /(?<test>[A-Z]+.*)/ =~ "TEST123"
+ Regexp.last_match(:test).should == "TEST123"
+ end
+
+ it "raises an IndexError when given a missing name" do
+ /(?<test>[A-Z]+.*)/ =~ "TEST123"
+ -> { Regexp.last_match(:missing) }.should.raise(IndexError)
+ end
+ end
+
+ describe "when given a String" do
+ it "returns a named capture" do
+ /(?<test>[A-Z]+.*)/ =~ "TEST123"
+ Regexp.last_match("test").should == "TEST123"
+ end
+ end
+
+ describe "when given an Object" do
+ it "coerces argument to an index using #to_int" do
+ obj = mock("converted to int")
+ obj.should_receive(:to_int).and_return(1)
+ /(?<test>[A-Z]+.*)/ =~ "TEST123"
+ Regexp.last_match(obj).should == "TEST123"
+ end
+
+ it "raises a TypeError when unable to coerce" do
+ obj = Object.new
+ /(?<test>[A-Z]+.*)/ =~ "TEST123"
+ -> { Regexp.last_match(obj) }.should.raise(TypeError)
+ end
+ end
+end