aboutsummaryrefslogtreecommitdiffstats
path: root/test/strscan
diff options
context:
space:
mode:
authormame <mame@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2008-06-05 14:33:01 +0000
committermame <mame@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2008-06-05 14:33:01 +0000
commit437af4f46fdc54cd348779f65bc12d2c962f1b18 (patch)
tree54f3afcf82d82d426dd2c5b00fcee6e94916fbe8 /test/strscan
parentcbbc06dcf78ab65c60261805504d5a1adda9462c (diff)
downloadruby-437af4f46fdc54cd348779f65bc12d2c962f1b18.tar.gz
* test/stringio/test_stringio.rb: add tests to achieve over 95% test
coverage of stringio. * test/strscan/test_stringscanner.rb: ditto for strscan. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@16847 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'test/strscan')
-rw-r--r--test/strscan/test_stringscanner.rb123
1 files changed, 123 insertions, 0 deletions
diff --git a/test/strscan/test_stringscanner.rb b/test/strscan/test_stringscanner.rb
index fafda2c3a0..ff1eb7a298 100644
--- a/test/strscan/test_stringscanner.rb
+++ b/test/strscan/test_stringscanner.rb
@@ -548,4 +548,127 @@ class TestStringScanner < Test::Unit::TestCase
assert_equal("\xa1\xa2".force_encoding("euc-jp"), t)
end
+ def test_set_pos
+ s = StringScanner.new("test string")
+ s.pos = 7
+ assert_equal("ring", s.rest)
+ end
+
+ def test_match_p
+ s = StringScanner.new("test string")
+ assert_equal(4, s.match?(/\w+/))
+ assert_equal(4, s.match?(/\w+/))
+ assert_equal(nil, s.match?(/\s+/))
+ end
+
+ def test_check
+ s = StringScanner.new("Foo Bar Baz")
+ assert_equal("Foo", s.check(/Foo/))
+ assert_equal(0, s.pos)
+ assert_equal("Foo", s.matched)
+ assert_equal(nil, s.check(/Bar/))
+ assert_equal(nil, s.matched)
+ end
+
+ def test_scan_full
+ s = StringScanner.new("Foo Bar Baz")
+ assert_equal(4, s.scan_full(/Foo /, false, false))
+ assert_equal(0, s.pos)
+ assert_equal(nil, s.scan_full(/Baz/, false, false))
+ assert_equal("Foo ", s.scan_full(/Foo /, false, true))
+ assert_equal(0, s.pos)
+ assert_equal(nil, s.scan_full(/Baz/, false, false))
+ assert_equal(4, s.scan_full(/Foo /, true, false))
+ assert_equal(4, s.pos)
+ assert_equal(nil, s.scan_full(/Baz /, false, false))
+ assert_equal("Bar ", s.scan_full(/Bar /, true, true))
+ assert_equal(8, s.pos)
+ assert_equal(nil, s.scan_full(/az/, false, false))
+ end
+
+ def test_exist_p
+ s = StringScanner.new("test string")
+ assert_equal(3, s.exist?(/s/))
+ assert_equal(0, s.pos)
+ s.scan(/test/)
+ assert_equal(2, s.exist?(/s/))
+ assert_equal(4, s.pos)
+ assert_equal(nil, s.exist?(/e/))
+ end
+
+ def test_skip_until
+ s = StringScanner.new("Foo Bar Baz")
+ assert_equal(3, s.skip_until(/Foo/))
+ assert_equal(3, s.pos)
+ assert_equal(4, s.skip_until(/Bar/))
+ assert_equal(7, s.pos)
+ assert_equal(nil, s.skip_until(/Qux/))
+ end
+
+ def test_check_until
+ s = StringScanner.new("Foo Bar Baz")
+ assert_equal("Foo", s.check_until(/Foo/))
+ assert_equal(0, s.pos)
+ assert_equal("Foo Bar", s.check_until(/Bar/))
+ assert_equal(0, s.pos)
+ assert_equal(nil, s.check_until(/Qux/))
+ end
+
+ def test_search_full
+ s = StringScanner.new("Foo Bar Baz")
+ assert_equal(8, s.search_full(/Bar /, false, false))
+ assert_equal(0, s.pos)
+ assert_equal("Foo Bar ", s.search_full(/Bar /, false, true))
+ assert_equal(0, s.pos)
+ assert_equal(8, s.search_full(/Bar /, true, false))
+ assert_equal(8, s.pos)
+ assert_equal("Baz", s.search_full(/az/, true, true))
+ assert_equal(11, s.pos)
+ end
+
+ def test_peek
+ s = StringScanner.new("test string")
+ assert_equal("test st", s.peek(7))
+ assert_equal("test st", s.peek(7))
+ s.scan(/test/)
+ assert_equal(" stri", s.peek(5))
+ assert_equal(" string", s.peek(10))
+ s.scan(/ string/)
+ assert_equal("", s.peek(10))
+ end
+
+ def test_unscan
+ s = StringScanner.new('test string')
+ assert_equal("test", s.scan(/\w+/))
+ s.unscan
+ assert_equal("te", s.scan(/../))
+ assert_equal(nil, s.scan(/\d/))
+ assert_raise(ScanError) { s.unscan }
+ end
+
+ def test_rest
+ s = StringScanner.new('test string')
+ assert_equal("test string", s.rest)
+ s.scan(/test/)
+ assert_equal(" string", s.rest)
+ s.scan(/ string/)
+ assert_equal("", s.rest)
+ s.scan(/ string/)
+ end
+
+ def test_rest_size
+ s = StringScanner.new('test string')
+ assert_equal(11, s.rest_size)
+ s.scan(/test/)
+ assert_equal(7, s.rest_size)
+ s.scan(/ string/)
+ assert_equal(0, s.rest_size)
+ s.scan(/ string/)
+ end
+
+ def test_inspect
+ s = StringScanner.new('test string test')
+ s.scan(/test strin/)
+ assert_equal('#<StringScanner 10/16 "...strin" @ "g tes...">', s.inspect)
+ end
end