From e8074a382eb3e07feb4c425498e1605e2e729f7c Mon Sep 17 00:00:00 2001 From: sorah Date: Thu, 18 Feb 2016 05:29:18 +0000 Subject: * re.c: Add MatchData#named_captures [Feature #11999] [ruby-core:72897] * test/ruby/test_regexp.rb(test_match_data_named_captures): Test for above. * NEWS: News about MatchData#named_captures. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@53863 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- test/ruby/test_regexp.rb | 14 ++++++++++++++ 1 file changed, 14 insertions(+) (limited to 'test/ruby/test_regexp.rb') diff --git a/test/ruby/test_regexp.rb b/test/ruby/test_regexp.rb index db83ee3ee7..55e1f3a9c6 100644 --- a/test/ruby/test_regexp.rb +++ b/test/ruby/test_regexp.rb @@ -175,6 +175,20 @@ class TestRegexp < Test::Unit::TestCase assert_raise(IndexError, bug9903) {m[key.dup.force_encoding(Encoding::Shift_JIS)]} end + def test_match_data_named_captures + assert_equal({'a' => '1', 'b' => '2', 'c' => nil}, /^(?.)(?.)(?.)?/.match('12').named_captures) + assert_equal({'a' => '1', 'b' => '2', 'c' => '3'}, /^(?.)(?.)(?.)?/.match('123').named_captures) + assert_equal({'a' => '1', 'b' => '2', 'c' => ''}, /^(?.)(?.)(?.?)/.match('12').named_captures) + + assert_equal({'a' => 'x'}, /(?x)|(?y)/.match('x').named_captures) + assert_equal({'a' => 'y'}, /(?x)|(?y)/.match('y').named_captures) + + assert_equal({'a' => '1', 'b' => '2'}, /^(.)(?.)(?.)/.match('012').named_captures) + assert_equal({'a' => '2'}, /^(?.)(?.)/.match('12').named_captures) + + assert_equal({}, /^(.)/.match('123').named_captures) + end + def test_assign_named_capture assert_equal("a", eval('/(?.)/ =~ "a"; foo')) assert_equal(nil, eval('/(?<@foo>.)/ =~ "a"; defined?(@foo)')) -- cgit v1.2.3