test/re2_test.rb in re2-0.2.0 vs test/re2_test.rb in re2-0.3.0
- old
+ new
@@ -119,15 +119,48 @@
assert_equal 3, RE2('(a)(b)(c)').number_of_capturing_groups
assert_equal 0, RE2('abc').number_of_capturing_groups
assert_equal 2, RE2('a((b)c)').number_of_capturing_groups
end
+ def test_named_capturing_groups
+ assert_equal(1, RE2('(?P<bob>a)').named_capturing_groups["bob"])
+ assert_equal(1, RE2('(?P<bob>a)(o)(?P<rob>e)').named_capturing_groups["bob"])
+ assert_equal(3, RE2('(?P<bob>a)(o)(?P<rob>e)').named_capturing_groups["rob"])
+ end
+
def test_matching_all_subpatterns
assert_equal ["woo", "o", "o"], RE2('w(o)(o)').match('woo').to_a
assert_equal ["ab", nil, "a", "b"], RE2('(\d?)(a)(b)').match('ab').to_a
end
+ def test_fetching_matchdata_out_of_range
+ matchdata = RE2('(\d+)').match('bob 123')
+ assert_nil matchdata[2]
+ assert_nil matchdata[3]
+ end
+
+ def test_accessing_matches_by_name
+ matchdata = RE2('(?P<numbers>\d+)').match("bob 123")
+ assert_equal "123", matchdata["numbers"]
+ assert_equal "123", matchdata[:numbers]
+ end
+
+ def test_accessing_matches_by_name_with_multiple_groups
+ matchdata = RE2('(?P<name>\w+)(\s*)(?P<numbers>\d+)').match("bob 123")
+ assert_equal "bob", matchdata["name"]
+ assert_equal "bob", matchdata[:name]
+ assert_equal " ", matchdata[2]
+ assert_equal "123", matchdata["numbers"]
+ assert_equal "123", matchdata[:numbers]
+ end
+
+ def test_accessing_matches_by_incorrect_names
+ matchdata = RE2('(?P<numbers>\d+)').match("bob 123")
+ assert_nil matchdata["missing"]
+ assert_nil matchdata[:missing]
+ end
+
def test_matchdata
r = RE2('(\d+)')
text = "bob 123"
m = r.match(text)
assert_kind_of RE2::MatchData, m
@@ -144,9 +177,10 @@
assert_not_equal m.string.object_id, text.object_id
assert_equal '#<RE2::MatchData "123" 1:"123">', m.inspect
assert_equal "123", m.to_s
assert_equal "123", m[0]
assert_equal "123", m[1]
+ assert_nil m[4]
assert_equal ["123"], m[0, 1]
assert_equal ["123", "123"], m[0, 2]
assert_equal ["123"], m[0...1]
assert_equal ["123", "123"], m[0..1]
m1, m2 = *r.match(text)