Skip to content

Commit dc54aee

Browse files
committed
Add specs for error handling in MatchData#begin
1 parent 5cd2819 commit dc54aee

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

spec/ruby/core/matchdata/begin_spec.rb

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,18 @@
3636
match_data = /(.)(.)(\d+)(\d)/.match("THX1138.")
3737
match_data.begin(obj).should == 2
3838
end
39+
40+
it "raises IndexError if index is out of matches" do
41+
match_data = /(?<f>foo)(?<b>bar)/.match("foobar")
42+
43+
-> {
44+
match_data.begin(-1)
45+
}.should raise_error(IndexError, "index -1 out of matches")
46+
47+
-> {
48+
match_data.begin(3)
49+
}.should raise_error(IndexError, "index 3 out of matches")
50+
end
3951
end
4052

4153
context "when passed a String argument" do
@@ -68,6 +80,14 @@
6880
match_data = /(?<æ>.)(.)(?<b>\d+)(\d)/.match("THX1138.")
6981
match_data.begin("æ").should == 1
7082
end
83+
84+
it "raises IndexError if there is no group with the provided name" do
85+
match_data = /(?<f>foo)(?<b>bar)/.match("foobar")
86+
87+
-> {
88+
match_data.begin("y")
89+
}.should raise_error(IndexError, "undefined group name reference: y")
90+
end
7191
end
7292

7393
context "when passed a Symbol argument" do
@@ -100,5 +120,13 @@
100120
match_data = /(?<æ>.)(.)(?<b>\d+)(\d)/.match("THX1138.")
101121
match_data.begin().should == 1
102122
end
123+
124+
it "raises IndexError if there is no group with the provided name" do
125+
match_data = /(?<f>foo)(?<b>bar)/.match("foobar")
126+
127+
-> {
128+
match_data.begin(:y)
129+
}.should raise_error(IndexError, "undefined group name reference: y")
130+
end
103131
end
104132
end

spec/ruby/core/matchdata/byteoffset_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ def obj.to_int; 2; end
6060
m.byteoffset(obj).should == [3, 6]
6161
end
6262

63-
it "raises IndexError if there is no group with provided name" do
63+
it "raises IndexError if there is no group with the provided name" do
6464
m = /(?<f>foo)(?<b>bar)/.match("foobar")
6565

6666
-> {

0 commit comments

Comments
 (0)