diff options
Diffstat (limited to 'spec/ruby/core/matchdata/begin_spec.rb')
| -rw-r--r-- | spec/ruby/core/matchdata/begin_spec.rb | 30 |
1 files changed, 29 insertions, 1 deletions
diff --git a/spec/ruby/core/matchdata/begin_spec.rb b/spec/ruby/core/matchdata/begin_spec.rb index 85c454da56..b4be077ae4 100644 --- a/spec/ruby/core/matchdata/begin_spec.rb +++ b/spec/ruby/core/matchdata/begin_spec.rb @@ -12,7 +12,7 @@ describe "MatchData#begin" do it "returns nil when the nth match isn't found" do match_data = /something is( not)? (right)/.match("something is right") - match_data.begin(1).should be_nil + match_data.begin(1).should == nil end it "returns the character offset for multi-byte strings" do @@ -36,6 +36,18 @@ describe "MatchData#begin" do match_data = /(.)(.)(\d+)(\d)/.match("THX1138.") match_data.begin(obj).should == 2 end + + it "raises IndexError if index is out of bounds" do + match_data = /(?<f>foo)(?<b>bar)/.match("foobar") + + -> { + match_data.begin(-1) + }.should.raise(IndexError, "index -1 out of matches") + + -> { + match_data.begin(3) + }.should.raise(IndexError, "index 3 out of matches") + end end context "when passed a String argument" do @@ -68,6 +80,14 @@ describe "MatchData#begin" do match_data = /(?<æ>.)(.)(?<b>\d+)(\d)/.match("THX1138.") match_data.begin("æ").should == 1 end + + it "raises IndexError if there is no group with the provided name" do + match_data = /(?<f>foo)(?<b>bar)/.match("foobar") + + -> { + match_data.begin("y") + }.should.raise(IndexError, "undefined group name reference: y") + end end context "when passed a Symbol argument" do @@ -100,5 +120,13 @@ describe "MatchData#begin" do match_data = /(?<æ>.)(.)(?<b>\d+)(\d)/.match("THX1138.") match_data.begin(:æ).should == 1 end + + it "raises IndexError if there is no group with the provided name" do + match_data = /(?<f>foo)(?<b>bar)/.match("foobar") + + -> { + match_data.begin(:y) + }.should.raise(IndexError, "undefined group name reference: y") + end end end |
