1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
|
require 'test/minirunit'
require 'strscan'
##### [] #####
s = StringScanner.new("Fri Dec 12 1975 14:39")
test_equal("Fri Dec 12 ", s.scan(/(\w+) (\w+) (\d+) /))
test_equal("Fri Dec 12 ", s[0])
test_equal("Fri", s[1])
test_equal("Dec", s[2])
test_equal("12", s[3])
test_equal(nil, s[4])
test_equal("1975 14:39", s.post_match)
test_equal("", s.pre_match)
##### check #####
s = StringScanner.new("Fri Dec 12 1975 14:39")
test_equal("Fri", s.check(/Fri/))
test_equal(0, s.pos)
test_equal("Fri", s.matched)
test_equal("", s.pre_match)
test_equal(" Dec 12 1975 14:39", s.post_match)
test_equal(nil, s.check(/12/))
test_equal(nil, s.matched)
##### getch #####
s = StringScanner.new('abc')
test_equal("a", s.getch)
test_equal("a", s.matched)
test_equal("b", s.getch)
test_equal("a", s.pre_match)
test_equal("c", s.getch)
test_equal("ab", s.pre_match)
test_equal(nil, s.getch)
##### pos=, rest #####
s = StringScanner.new('test string')
test_equal(7, s.pos = 7)
test_equal("ring", s.rest)
test_exception(RangeError) { s.pos = 20 }
##### scan ######
s = StringScanner.new('test string')
test_equal("test", s.scan(/\w+/))
test_equal(nil, s.scan(/\w+/))
test_equal(" ", s.scan(/\s+/))
test_equal("string", s.scan(/\w+/))
test_equal(nil, s.scan(/./))
##### scan_until, pre_match #####
s = StringScanner.new("Fri Dec 12 1975 14:39")
test_equal("Fri Dec 1", s.scan_until(/1/))
test_equal("Fri Dec ", s.pre_match)
test_equal("2 1975 14:39", s.post_match)
test_equal("1", s.matched)
test_equal(nil, s.scan_until(/XYZ/))
test_equal(nil, s.pre_match)
test_equal(nil, s.matched)
test_equal(nil, s.post_match)
##### skip #####
s = StringScanner.new('test string')
test_equal(4, s.skip(/\w+/))
test_equal("test", s.matched)
test_equal(nil, s.skip(/\w+/))
test_equal(1, s.skip(/\s+/))
test_equal(6, s.skip(/\w+/))
test_equal(nil, s.skip(/./))
##### scan_full, search_full ######
s = StringScanner.new('test string')
test_equal(4, s.scan_full(/\w+/, true, false))
test_equal(4, s.pos)
test_equal("test", s.matched)
s.reset
test_equal("test", s.scan_full(/\w+/, true, true))
test_equal(4, s.pos)
s.reset
test_equal("test", s.scan_full(/\w+/, false, true))
test_equal(0, s.pos)
s.reset
test_equal("test str", s.search_full(/r/, true, true))
test_equal(8, s.pos)
s.reset
test_equal(8, s.search_full(/r/, true, false))
test_equal(8, s.pos)
test_equal("r", s.matched)
s.reset
test_equal("test str", s.search_full(/r/, false, true))
test_equal(0, s.pos)
##### JRUBY-214: arg 0 should have to_str called if not String ######
class Foo
attr :to_str_called
def to_str
@to_str_called = true
"bar"
end
end
f = Foo.new
test_no_exception { StringScanner.new(f) }
test_ok(f.to_str_called)
test_exception(TypeError) { StringScanner.new(Object.new) }
### JRUBY-685: after moving to JRegex, find(int) doesn't work the same way as before
strscan = StringScanner.new('AB')
strscan.getch
test_equal 1, strscan.skip_until(/B/)
strscan = StringScanner.new('AA')
strscan.getch
test_equal 1, strscan.skip_until(/A/)
### JRUBY-1176: still problems with setPosition, moving to setOffset instead
s = StringScanner.new " a"
s.skip(/\s/)
test_equal 'a', s.scan_until(/\Aa/)
# JRUBY-2523
s = StringScanner.new "abc"
test_equal 'a', s.getbyte
|