diff options
author | nobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2017-11-29 07:57:48 +0000 |
---|---|---|
committer | nobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2017-11-29 07:57:48 +0000 |
commit | 256c88861beb452d790d74c7677a7e4217bc170b (patch) | |
tree | 8b992570061fc56456a48a6a8208e2f42414ea8f /test | |
parent | 0d82c354c205ffcb9749141b36027f6ed9e5333d (diff) | |
download | ruby-256c88861beb452d790d74c7677a7e4217bc170b.tar.gz |
strscan.c: add MatchData-like methods
* ext/strscan/strscan.c: added `size`, `captures` and `values_at`
to StringScanner, shorthands of accessing the matched data.
based on the patch by apeiros (Stefan Rusterholz) at
[ruby-core:20412]. [Feature #836]
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@60929 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'test')
-rw-r--r-- | test/strscan/test_stringscanner.rb | 22 |
1 files changed, 22 insertions, 0 deletions
diff --git a/test/strscan/test_stringscanner.rb b/test/strscan/test_stringscanner.rb index 9fe6ad7125..3423f9cfed 100644 --- a/test/strscan/test_stringscanner.rb +++ b/test/strscan/test_stringscanner.rb @@ -728,4 +728,26 @@ class TestStringScanner < Test::Unit::TestCase assert_nil(s[:c]) assert_nil(s["c"]) end + + def test_size + s = StringScanner.new("Fri Dec 12 1975 14:39") + s.scan(/(\w+) (\w+) (\d+) /) + assert_equal(4, s.size) + end + + def test_captures + s = StringScanner.new("Fri Dec 12 1975 14:39") + s.scan(/(\w+) (\w+) (\d+) /) + assert_equal(["Fri", "Dec", "12"], s.captures) + s.scan(/(\w+) (\w+) (\d+) /) + assert_nil(s.captures) + end + + def test_values_at + s = StringScanner.new("Fri Dec 12 1975 14:39") + s.scan(/(\w+) (\w+) (\d+) /) + assert_equal(["Fri Dec 12 ", "12", nil, "Dec"], s.values_at(0, -1, 5, 2)) + s.scan(/(\w+) (\w+) (\d+) /) + assert_nil(s.values_at(0, -1, 5, 2)) + end end |