2 # test/strscan/test_stringscanner.rb
8 class TestStringScanner < Test::Unit::TestCase
10 s = StringScanner.new('test string')
11 assert_instance_of StringScanner, s
12 assert_equal false, s.eos?
13 assert_equal false, s.tainted?
17 s = StringScanner.new(str, false)
18 assert_instance_of StringScanner, s
19 assert_equal false, s.eos?
20 assert_same str, s.string
21 assert_equal true, s.string.tainted?
25 s = StringScanner.new(str)
26 assert_equal true, s.string.tainted?
29 UNINIT_ERROR = ArgumentError
32 s = StringScanner.allocate
33 assert_equal '#<StringScanner (uninitialized)>', s.inspect.sub(/StringScanner_C/, 'StringScanner')
34 assert_raises(UNINIT_ERROR) { s.eos? }
35 assert_raises(UNINIT_ERROR) { s.scan(/a/) }
37 assert_equal '#<StringScanner 0/4 @ "test">', s.inspect.sub(/StringScanner_C/, 'StringScanner')
38 assert_nothing_raised(UNINIT_ERROR) { s.eos? }
39 assert_equal false, s.eos?
43 assert_nothing_raised(NotImplementedError) {
44 StringScanner.must_C_version
49 s = StringScanner.new('test string')
51 assert_equal s.inspect, d.inspect
52 assert_equal s.string, d.string
53 assert_equal s.pos, d.pos
54 assert_equal s.matched?, d.matched?
55 assert_equal s.eos?, d.eos?
57 s = StringScanner.new('test string')
60 assert_equal s.inspect, d.inspect
61 assert_equal s.string, d.string
62 assert_equal s.pos, d.pos
63 assert_equal s.matched?, d.matched?
64 assert_equal s.eos?, d.eos?
66 s = StringScanner.new('test string')
70 assert_equal s.inspect, d.inspect
71 assert_equal s.string, d.string
72 assert_equal s.pos, d.pos
73 assert_equal s.matched?, d.matched?
74 assert_equal s.eos?, d.eos?
76 s = StringScanner.new('test string')
79 assert_equal s.inspect, d.inspect
80 assert_equal s.string, d.string
81 assert_equal s.pos, d.pos
82 assert_equal s.matched?, d.matched?
83 assert_equal s.eos?, d.eos?
86 def test_const_Version
87 assert_instance_of String, StringScanner::Version
88 assert_equal true, StringScanner::Version.frozen?
92 assert_instance_of String, StringScanner::Id
93 assert_equal true, StringScanner::Id.frozen?
99 s = StringScanner.new(str, false)
100 assert_instance_of String, s.inspect
101 assert_equal s.inspect, s.inspect
102 assert_equal '#<StringScanner 0/11 @ "test ...">', s.inspect.sub(/StringScanner_C/, 'StringScanner')
104 assert_equal '#<StringScanner 1/11 "t" @ "est s...">', s.inspect.sub(/StringScanner_C/, 'StringScanner')
105 assert_equal true, s.inspect.tainted?
107 s = StringScanner.new("\n")
108 assert_equal '#<StringScanner 0/1 @ "\n">', s.inspect
112 s = StringScanner.new('test string')
113 assert_equal false, s.eos?
114 assert_equal false, s.eos?
116 assert_equal false, s.eos?
117 assert_equal false, s.eos?
120 assert_equal true, s.eos?
121 assert_equal true, s.eos?
123 assert_equal true, s.eos?
125 s = StringScanner.new('test')
128 assert_equal true, s.eos?
132 s = StringScanner.new("a\nbbb\n\ncccc\nddd\r\neee")
133 assert_equal true, s.bol?
134 assert_equal true, s.bol?
136 assert_equal false, s.bol?
137 assert_equal false, s.bol?
139 assert_equal true, s.bol?
141 assert_equal false, s.bol?
143 assert_equal false, s.bol?
145 assert_equal false, s.bol?
147 assert_equal true, s.bol?
149 assert_equal false, s.bol?
152 assert_equal true, s.bol?
154 assert_equal true, s.bol?
156 assert_equal true, s.bol?
158 assert_equal false, s.bol?
162 s = StringScanner.new('test')
163 assert_equal 'test', s.string
165 assert_equal 'a', s.string
168 assert_equal 0, s.pos
172 s = StringScanner.new('test string')
173 assert_equal 0, s.pos
175 assert_equal 1, s.pos
177 assert_equal 2, s.pos
179 assert_equal 11, s.pos
183 s = StringScanner.new('a')
186 assert_equal false, s.eos?
187 assert_equal 'b', s.scan(/b/)
188 assert_equal true, s.eos?
190 assert_equal false, s.eos?
191 assert_equal 'c', s.scan(/c/)
192 assert_equal true, s.eos?
196 s = StringScanner.new('stra strb strc', true)
198 assert_equal 'stra', tmp
199 assert_equal false, tmp.tainted?
202 assert_equal ' ', tmp
203 assert_equal false, tmp.tainted?
205 assert_equal 'strb', s.scan(/\w+/)
206 assert_equal ' ', s.scan(/\s+/)
209 assert_equal 'strc', tmp
210 assert_equal false, tmp.tainted?
212 assert_nil s.scan(/\w+/)
213 assert_nil s.scan(/\w+/)
216 str = 'stra strb strc'
218 s = StringScanner.new(str, false)
220 assert_equal 'stra', tmp
221 assert_equal true, tmp.tainted?
224 assert_equal ' ', tmp
225 assert_equal true, tmp.tainted?
227 assert_equal 'strb', s.scan(/\w+/)
228 assert_equal ' ', s.scan(/\s+/)
231 assert_equal 'strc', tmp
232 assert_equal true, tmp.tainted?
234 assert_nil s.scan(/\w+/)
235 assert_nil s.scan(/\w+/)
237 s = StringScanner.new('test')
239 # This assumes #string does not duplicate string,
240 # but it is implementation specific issue.
241 # DO NOT RELY ON THIS FEATURE.
243 # unspecified: assert_equal 2, s.pos
244 assert_equal nil, s.scan(/test/)
247 s = StringScanner.new("")
248 assert_equal "", s.scan(//)
249 assert_equal "", s.scan(//)
253 s = StringScanner.new('stra strb strc', true)
254 assert_equal 4, s.skip(/\w+/)
255 assert_equal 1, s.skip(/\s+/)
256 assert_equal 4, s.skip(/\w+/)
257 assert_equal 1, s.skip(/\s+/)
258 assert_equal 4, s.skip(/\w+/)
259 assert_nil s.skip(/\w+/)
260 assert_nil s.skip(/\s+/)
261 assert_equal true, s.eos?
263 s = StringScanner.new('test')
266 assert_equal nil, s.skip(/./)
269 s = StringScanner.new("")
270 assert_equal 0, s.skip(//)
271 assert_equal 0, s.skip(//)
275 s = StringScanner.new('abcde')
276 assert_equal 'a', s.getch
277 assert_equal 'b', s.getch
278 assert_equal 'c', s.getch
279 assert_equal 'd', s.getch
280 assert_equal 'e', s.getch
285 s = StringScanner.new(str)
286 assert_equal true, s.getch.tainted?
287 assert_equal true, s.getch.tainted?
288 assert_equal true, s.getch.tainted?
291 s = StringScanner.new("\244\242".force_encoding("euc-jp"))
292 assert_equal "\244\242".force_encoding("euc-jp"), s.getch
295 s = StringScanner.new('test')
298 assert_equal nil, s.getch
302 s = StringScanner.new('abcde')
303 assert_equal 'a', s.get_byte
304 assert_equal 'b', s.get_byte
305 assert_equal 'c', s.get_byte
306 assert_equal 'd', s.get_byte
307 assert_equal 'e', s.get_byte
308 assert_nil s.get_byte
309 assert_nil s.get_byte
313 s = StringScanner.new(str)
314 assert_equal true, s.get_byte.tainted?
315 assert_equal true, s.get_byte.tainted?
316 assert_equal true, s.get_byte.tainted?
317 assert_nil s.get_byte
319 s = StringScanner.new("\244\242".force_encoding("euc-jp"))
320 assert_equal "\244".force_encoding("euc-jp"), s.get_byte
321 assert_equal "\242".force_encoding("euc-jp"), s.get_byte
322 assert_nil s.get_byte
324 s = StringScanner.new('test')
327 assert_equal nil, s.get_byte
331 s = StringScanner.new('stra strb strc')
333 assert_equal 'stra', s.matched
334 assert_equal false, s.matched.tainted?
336 assert_equal ' ', s.matched
338 assert_equal 'strb', s.matched
340 assert_equal ' ', s.matched
342 assert_equal 'strc', s.matched
348 s = StringScanner.new('stra strb strc')
350 assert_equal 's', s.matched
351 assert_equal false, s.matched.tainted?
353 assert_equal 't', s.matched
354 assert_equal 't', s.matched
355 assert_equal false, s.matched.tainted?
359 s = StringScanner.new(str)
361 assert_equal true, s.matched.tainted?
362 assert_equal true, s.matched.tainted?
366 s = StringScanner.new('stra strb strc')
370 assert_equal 'stra', s[-1]
371 assert_equal 'stra', s[0]
374 assert_equal false, s[-1].tainted?
375 assert_equal false, s[0].tainted?
379 assert_equal ' ', s[-1]
380 assert_equal ' ', s[0]
386 assert_equal 'strb', s[-3]
387 assert_equal 's', s[-2]
388 assert_equal 'r', s[-1]
389 assert_equal 'strb', s[0]
390 assert_equal 's', s[1]
391 assert_equal 'r', s[2]
399 assert_equal 's', s[-1]
400 assert_equal 's', s[0]
405 assert_equal 't', s[-1]
406 assert_equal 't', s[0]
415 s = StringScanner.new("\244\242".force_encoding("euc-jp"))
417 assert_equal "\244\242".force_encoding("euc-jp"), s[0]
421 s = StringScanner.new(str)
422 s.scan(/(t)(e)(s)(t)/)
423 assert_equal true, s[0].tainted?
424 assert_equal true, s[1].tainted?
425 assert_equal true, s[2].tainted?
426 assert_equal true, s[3].tainted?
427 assert_equal true, s[4].tainted?
431 s = StringScanner.new('a b c d e')
433 assert_equal '', s.pre_match
434 assert_equal false, s.pre_match.tainted?
436 assert_equal 'a', s.pre_match
437 assert_equal false, s.pre_match.tainted?
439 assert_equal 'a ', s.pre_match
441 assert_equal 'a b ', s.pre_match
443 assert_equal 'a b c', s.pre_match
445 assert_equal 'a b c ', s.pre_match
447 assert_equal 'a b c d', s.pre_match
448 s.scan(/never match/)
449 assert_nil s.pre_match
453 s = StringScanner.new(str)
455 assert_equal true, s.pre_match.tainted?
457 assert_equal true, s.pre_match.tainted?
459 assert_equal true, s.pre_match.tainted?
463 s = StringScanner.new('a b c d e')
465 assert_equal ' b c d e', s.post_match
467 assert_equal 'b c d e', s.post_match
469 assert_equal ' c d e', s.post_match
471 assert_equal ' d e', s.post_match
473 assert_equal 'd e', s.post_match
475 assert_equal ' e', s.post_match
477 assert_equal 'e', s.post_match
478 s.scan(/never match/)
479 assert_nil s.post_match
481 assert_equal '', s.post_match
483 assert_nil s.post_match
487 s = StringScanner.new(str)
489 assert_equal true, s.post_match.tainted?
491 assert_equal true, s.post_match.tainted?
493 assert_equal true, s.post_match.tainted?
497 s = StringScanner.new('ssss')
500 assert_equal true, s.eos?
502 assert_equal true, s.eos?
506 s = StringScanner.new('ssss')
509 assert_equal 0, s.pos
512 assert_equal 0, s.pos
514 assert_equal 0, s.pos
517 def test_matched_size
518 s = StringScanner.new('test string')
519 assert_nil s.matched_size
521 assert_equal 4, s.matched_size
522 assert_equal 4, s.matched_size
524 assert_equal 0, s.matched_size
526 assert_nil s.matched_size
527 assert_nil s.matched_size
529 assert_nil s.matched_size
532 s = StringScanner.new('test string')
533 assert_nil s.matchedsize
535 assert_equal 4, s.matched_size
537 assert_nil s.matched_size
541 ss = StringScanner.new("\xA1\xA2".force_encoding("euc-jp"))
542 assert_equal(Encoding::EUC_JP, ss.scan(/./e).encoding)
545 def test_generic_regexp
546 ss = StringScanner.new("\xA1\xA2".force_encoding("euc-jp"))
548 assert_equal("\xa1\xa2".force_encoding("euc-jp"), t)
552 s = StringScanner.new("test string")
554 assert_equal("ring", s.rest)
558 s = StringScanner.new("test string")
559 assert_equal(4, s.match?(/\w+/))
560 assert_equal(4, s.match?(/\w+/))
561 assert_equal(nil, s.match?(/\s+/))
565 s = StringScanner.new("Foo Bar Baz")
566 assert_equal("Foo", s.check(/Foo/))
567 assert_equal(0, s.pos)
568 assert_equal("Foo", s.matched)
569 assert_equal(nil, s.check(/Bar/))
570 assert_equal(nil, s.matched)
574 s = StringScanner.new("Foo Bar Baz")
575 assert_equal(4, s.scan_full(/Foo /, false, false))
576 assert_equal(0, s.pos)
577 assert_equal(nil, s.scan_full(/Baz/, false, false))
578 assert_equal("Foo ", s.scan_full(/Foo /, false, true))
579 assert_equal(0, s.pos)
580 assert_equal(nil, s.scan_full(/Baz/, false, false))
581 assert_equal(4, s.scan_full(/Foo /, true, false))
582 assert_equal(4, s.pos)
583 assert_equal(nil, s.scan_full(/Baz /, false, false))
584 assert_equal("Bar ", s.scan_full(/Bar /, true, true))
585 assert_equal(8, s.pos)
586 assert_equal(nil, s.scan_full(/az/, false, false))
590 s = StringScanner.new("test string")
591 assert_equal(3, s.exist?(/s/))
592 assert_equal(0, s.pos)
594 assert_equal(2, s.exist?(/s/))
595 assert_equal(4, s.pos)
596 assert_equal(nil, s.exist?(/e/))
600 s = StringScanner.new("Foo Bar Baz")
601 assert_equal(3, s.skip_until(/Foo/))
602 assert_equal(3, s.pos)
603 assert_equal(4, s.skip_until(/Bar/))
604 assert_equal(7, s.pos)
605 assert_equal(nil, s.skip_until(/Qux/))
609 s = StringScanner.new("Foo Bar Baz")
610 assert_equal("Foo", s.check_until(/Foo/))
611 assert_equal(0, s.pos)
612 assert_equal("Foo Bar", s.check_until(/Bar/))
613 assert_equal(0, s.pos)
614 assert_equal(nil, s.check_until(/Qux/))
618 s = StringScanner.new("Foo Bar Baz")
619 assert_equal(8, s.search_full(/Bar /, false, false))
620 assert_equal(0, s.pos)
621 assert_equal("Foo Bar ", s.search_full(/Bar /, false, true))
622 assert_equal(0, s.pos)
623 assert_equal(8, s.search_full(/Bar /, true, false))
624 assert_equal(8, s.pos)
625 assert_equal("Baz", s.search_full(/az/, true, true))
626 assert_equal(11, s.pos)
630 s = StringScanner.new("test string")
631 assert_equal("test st", s.peek(7))
632 assert_equal("test st", s.peek(7))
634 assert_equal(" stri", s.peek(5))
635 assert_equal(" string", s.peek(10))
637 assert_equal("", s.peek(10))
641 s = StringScanner.new('test string')
642 assert_equal("test", s.scan(/\w+/))
644 assert_equal("te", s.scan(/../))
645 assert_equal(nil, s.scan(/\d/))
646 assert_raise(ScanError) { s.unscan }
650 s = StringScanner.new('test string')
651 assert_equal("test string", s.rest)
653 assert_equal(" string", s.rest)
655 assert_equal("", s.rest)
660 s = StringScanner.new('test string')
661 assert_equal(11, s.rest_size)
663 assert_equal(7, s.rest_size)
665 assert_equal(0, s.rest_size)
670 s = StringScanner.new('test string test')
672 assert_equal('#<StringScanner 10/16 "...strin" @ "g tes...">', s.inspect)