spec/re2/match_data_spec.rb in re2-0.6.1 vs spec/re2/match_data_spec.rb in re2-0.7.0
- old
+ new
@@ -1,5 +1,7 @@
+# encoding: utf-8
+
require "spec_helper"
describe RE2::MatchData do
describe "#to_a" do
@@ -67,10 +69,15 @@
md = RE2::Regexp.new('(\d+)').match("bob 123")
md["missing"].must_be_nil
md[:missing].must_be_nil
end
+ it "raises an error if given an inappropriate index" do
+ md = RE2::Regexp.new('(\d+)').match("bob 123")
+ lambda { md[nil] }.must_raise(TypeError)
+ end
+
if String.method_defined?(:encoding)
it "returns UTF-8 encoded strings by default" do
md = RE2::Regexp.new('(?P<name>\S+)').match("bob")
md[0].encoding.name.must_equal("UTF-8")
md["name"].encoding.name.must_equal("UTF-8")
@@ -150,8 +157,52 @@
md = RE2::Regexp.new('(\d+) (\d+)').match("1234 56")
m1, m2, m3 = *md
m1.must_equal("1234 56")
m2.must_equal("1234")
m3.must_equal("56")
+ end
+ end
+
+ describe "#begin" do
+ it "returns the offset of the start of a match by index" do
+ md = RE2::Regexp.new('(wo{2})').match('a woohoo')
+ md.string[md.begin(0)..-1].must_equal('woohoo')
+ end
+
+ it "returns the offset of the start of a match by string name" do
+ md = RE2::Regexp.new('(?P<foo>fo{2})').match('a foobar')
+ md.string[md.begin('foo')..-1].must_equal('foobar')
+ end
+
+ it "returns the offset of the start of a match by symbol name" do
+ md = RE2::Regexp.new('(?P<foo>fo{2})').match('a foobar')
+ md.string[md.begin(:foo)..-1].must_equal('foobar')
+ end
+
+ it "returns the offset despite multibyte characters" do
+ md = RE2::Regexp.new('(Ruby)').match('I ♥ Ruby')
+ md.string[md.begin(0)..-1].must_equal('Ruby')
+ end
+ end
+
+ describe "#end" do
+ it "returns the offset of the character following the end of a match" do
+ md = RE2::Regexp.new('(wo{2})').match('a woohoo')
+ md.string[0...md.end(0)].must_equal('a woo')
+ end
+
+ it "returns the offset of a match by string name" do
+ md = RE2::Regexp.new('(?P<foo>fo{2})').match('a foobar')
+ md.string[0...md.end('foo')].must_equal('a foo')
+ end
+
+ it "returns the offset of a match by symbol name" do
+ md = RE2::Regexp.new('(?P<foo>fo{2})').match('a foobar')
+ md.string[0...md.end(:foo)].must_equal('a foo')
+ end
+
+ it "returns the offset despite multibyte characters" do
+ md = RE2::Regexp.new('(Ruby)').match('I ♥ Ruby')
+ md.string[0...md.end(0)].must_equal('I ♥ Ruby')
end
end
end