mirror of
https://github.com/ruby/ruby.git
synced 2025-08-24 21:44:30 +02:00
* 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
This commit is contained in:
parent
e4809b056c
commit
e8074a382e
4 changed files with 94 additions and 0 deletions
|
@ -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}, /^(?<a>.)(?<b>.)(?<c>.)?/.match('12').named_captures)
|
||||
assert_equal({'a' => '1', 'b' => '2', 'c' => '3'}, /^(?<a>.)(?<b>.)(?<c>.)?/.match('123').named_captures)
|
||||
assert_equal({'a' => '1', 'b' => '2', 'c' => ''}, /^(?<a>.)(?<b>.)(?<c>.?)/.match('12').named_captures)
|
||||
|
||||
assert_equal({'a' => 'x'}, /(?<a>x)|(?<a>y)/.match('x').named_captures)
|
||||
assert_equal({'a' => 'y'}, /(?<a>x)|(?<a>y)/.match('y').named_captures)
|
||||
|
||||
assert_equal({'a' => '1', 'b' => '2'}, /^(.)(?<a>.)(?<b>.)/.match('012').named_captures)
|
||||
assert_equal({'a' => '2'}, /^(?<a>.)(?<a>.)/.match('12').named_captures)
|
||||
|
||||
assert_equal({}, /^(.)/.match('123').named_captures)
|
||||
end
|
||||
|
||||
def test_assign_named_capture
|
||||
assert_equal("a", eval('/(?<foo>.)/ =~ "a"; foo'))
|
||||
assert_equal(nil, eval('/(?<@foo>.)/ =~ "a"; defined?(@foo)'))
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue