| 1 | require 'strscan' |
|---|
| 2 | require "test/unit" |
|---|
| 3 | |
|---|
| 4 | class TestStringScanner < Test::Unit::TestCase |
|---|
| 5 | def test_lookahead |
|---|
| 6 | match = [] |
|---|
| 7 | ss = StringScanner.new($str) |
|---|
| 8 | |
|---|
| 9 | while ss.skip_until(/class(?![^ \n])|def(?![^ \n])|end(?![^ \n])|if(?![^ \n])|then(?![^ \n])|else(?![^ \n])/) |
|---|
| 10 | # while ss.skip_until(/(class|def|end|if|then|else)(?![^ \n])/) |
|---|
| 11 | a = ss.pos - ss.matched_size |
|---|
| 12 | b = ss.matched_size |
|---|
| 13 | |
|---|
| 14 | warn "[#{a}, #{b}] = \"#{ss.matched}\"" |
|---|
| 15 | x = [a,b,ss.matched] |
|---|
| 16 | match << x |
|---|
| 17 | end |
|---|
| 18 | |
|---|
| 19 | assert_equal(match[0], [0, 5, "class"]) |
|---|
| 20 | assert_equal(match[1], [120, 3, "def"]) |
|---|
| 21 | end |
|---|
| 22 | |
|---|
| 23 | end |
|---|
| 24 | |
|---|
| 25 | $str = <<EOT |
|---|
| 26 | class PrintProxy |
|---|
| 27 | # instance_methods.each { |
|---|
| 28 | # |m| undef_method m unless m =~ /(^__|^send$|^object_id$)/ |
|---|
| 29 | # } |
|---|
| 30 | |
|---|
| 31 | def initialize(obj, &block) |
|---|
| 32 | @obj = obj |
|---|
| 33 | end |
|---|
| 34 | |
|---|
| 35 | protected |
|---|
| 36 | |
|---|
| 37 | def method_missing(symbol, *args, &block) |
|---|
| 38 | print "foo" |
|---|
| 39 | if @block then print "foo\n" else print "\n" end |
|---|
| 40 | |
|---|
| 41 | @obj.send(symbol, *args, &block) |
|---|
| 42 | end |
|---|
| 43 | end |
|---|
| 44 | EOT |
|---|