require 'test/unit' require 'langscan/php' class TestScan < Test::Unit::TestCase def assert_yield(recv, meth, *args) yielded = false recv.__send__(meth, *args) {|*block_args| yielded = true yield(*block_args) } assert(yielded, "block not yielded") end def assert_not_yield(recv, meth, *args) yielded = false recv.__send__(meth, *args) {|*block_args| assert(false, "block yielded") } end def assert_yield_any(recv, meth, *args) success = false recv.__send__(meth, *args) {|*block_args| if yield(*block_args) assert(true) success = true break end } assert(false, "no expected yields") unless success end def assert_yield_all(recv, meth, *args) recv.__send__(meth, *args) {|*block_args| if !yield(*block_args) assert(false, "unexpected yields") return end } assert(true) end def test_classdef assert_yield_any(LangScan::PHP, :scan, "") {|f| f.type == :classdef && f.text == 'foo' } end def test_fundef assert_yield_any(LangScan::PHP, :scan, "") {|f| f.type == :fundef && f.text == 'foo1' } end def test_funcall assert_yield_any(LangScan::PHP, :scan, "") {|f| f.type == :funcall && f.text == 'hoge' } assert_yield_all(LangScan::PHP, :scan, "") {|f| f.type != :funcall } end def test_string [ "'hoge'", '"hoge"', '"hoge\nfuga"' ].each do |src| assert_yield_any(LangScan::PHP, :scan, "") {|f| f.type == :string } end end def test_comment [ '# comment', "# comment\n", "fuga(); # comment\nhoga();", "fuga(); # comment", ].each do |src| assert_yield_any(LangScan::PHP, :scan, "") {|f| f.type == :comment } assert_yield_any(LangScan::PHP, :scan, "") {|f| f.type == :ident and f.text == "?>" } end end def test_space [ '3 + 2', "hoge( fuga)" ].each do |src| assert_yield_any(LangScan::PHP, :scan, "") {|f| f.type == :space } end end def test_heredoc [ "echo <<" } end end def test_scriptregion [ '', '<%= "hoge" %>', '<% "hoge" %>', '', '', '', ].each do |src| assert_yield_any(LangScan::PHP, :scan, src) {|f| f.type == :string and f.text == '"hoge"' } end end end