require 'runit/testcase' require 'amrita/parser' require 'amrita/node_expand' require 'amrita/format' class TestHtmlParser < RUNIT::TestCase include Amrita Tag = HtmlScanner::TagInline def scan(text) begin ret = [] HtmlScanner.scan_text(text) do | state, token| ret << token end end ret end def parse(text) HtmlParser.parse_text(text) end def test_scanner assert_equal(Tag.new("html"), Tag.new("html")) assert_equal([Tag.new("em"), "xxx", Tag.new("/em")], scan("xxx")) assert_equal([Tag.new("img", [["src", "xxx.gif"]])], scan("")) assert_equal([Tag.new("a",[["href", "http://www.brain-tokyo.co.jp/"]])], scan('')) h = < [2:3] mailtoのテスト
1名前: 中島 投稿日: Fri Jan 26 19:00:25 JST 2001

2名前: ななしさん 投稿日: Fri Jan 26 19:00:33 JST 2001

3名前: ななしさん 投稿日: Fri Jan 26 19:00:44 JST 2001
されたみたい
END assert_equal( [ Tag.new("table", [["border", "1"], ["align", "center"], ["width", "95%"], ["bgcolor", "#CCFFCC"]]), "\n ", Tag.new("a", [["name", "28077"]]) ], scan(h).indexes(0,3,6)) end def parse_and_print(t) n = parse(t) puts "" puts n.to_ruby puts "\n------------------------------" print M { n } puts "\n------------------------------" end def test_parser assert_equal('e(:p) { "abc" }',parse("

abc

").to_ruby) assert_equal('e(:head) { e(:title) { "abc" } }', parse("abc").to_ruby) assert_equal('e(:html) { [ e(:head) { e(:title) { "abc" } }, e(:body) { e(:p) { "abc" } } ] }', parse("abc

abc

").to_ruby) assert_equal('e(:x,a(:a, "1"),a(:b)) { " " }', parse(" ").to_ruby) h = < [2:3] mailtoのテスト
3名前: ななしさん 投稿日: Fri Jan 26 19:00:44 JST 2001されたみたい
3名前: ななしさん 投稿日: Fri Jan 26 19:00:44 JST 2001されたみたい
END #parse_and_print(h) assert_equal('special_tag("%=", " Time.new ") ', parse('<%= Time.new %>').to_ruby) assert_equal('<%= Time.new %>', parse('<%= Time.new %>').to_s) assert_equal('', parse('').to_s) assert_equal('', parse('').to_s) assert_equal('', parse('').to_s) # assert_equal('', # parse('').to_s) assert_equal('', parse('').to_s) assert_equal('', parse('').to_s) end def test_parser2 assert_equal('e(:ul) { e(:li) { "abc" } }',parse("").to_ruby) assert_equal('e(:ul) { [ e(:li) { "abc" }, e(:li) { "efg" }, e(:li) { "xyz" } ] }', parse("").to_ruby) assert_equal('e(:ul) { [ e(:li) { "abc" }, e(:li) { "efg" }, e(:li) { "xyz" } ] }', parse("").to_ruby) assert_equal('e(:ul) { [ e(:li) { "abc" }, e(:li) { "efg" }, e(:li) { "xyz" } ] }', parse("").to_ruby) assert_equal('e(:ul) { [ e(:li) { "abc" }, e(:li) { "efg" }, e(:li) { "xyz" } ] }', parse("").to_ruby) assert_equal('[ e(:p) { "a" }, e(:p) { "b" } ]', parse("

a

b").to_ruby) assert_equal('e(:dl) { [ e(:dt) { "a" }, e(:dd) { "b" }, e(:dd) { "c" } ] }', parse("

a
b
c").to_ruby) #assert_equal('[ e(:p) { [ "a", e(:em) { "b" }, "c" ] }, e(:br) , e(:hr) , e(:p) { "x" } ]', assert_equal('[ e(:p) { [ "a", e(:em) { "b" }, "c", e(:br) ] }, e(:hr) , e(:p) { "x" } ]', parse("

abc


x").to_ruby) assert_equal('e(:table) { e(:tr) { e(:td) { e(:p) { "a" } } } }', parse("

a

").to_ruby) assert_equal('e(:table) { e(:tbody) { e(:tr) { e(:td) { e(:p) { "a" } } } } }', parse("

a

").to_ruby) template_text = < title

title

END assert_equal('[ ' + 'special_tag("!--", " start of template -----------") , "\n", ' + 'e(:html) { [ "\n", e(:head) { [ "\n ", e(:title,a(:id, "title")) { "title" }, "\n" ] }, "\n", ' + 'e(:body) { [ "\n", e(:h1,a(:id, "title")) { "title" }, "\n ", e(:ul) { [ "\n ", e(:li,a(:id, "list")) { " " }, "\n " ] }, "\n" ] }, "\n" ] }, "\n" ]', parse(template_text).to_ruby) end def test_parser_error assert_exception(HtmlParseError) { parse('') } assert_exception(HtmlParseError) { parse('') } assert_exception(HtmlParseError) { parse('
      a
')} assert_exception(HtmlParseError) { parse('
  • ')} assert_exception(HtmlParseError) { parse('

    aaa

    ') } end def test_expander1 template = "

    xx

    " d = { :title => "expand title" } assert_equal('

    expand title

    ', format_inline(d) { e(:h1, a(:id, "title") { "xx" }) }) assert_equal('

    expand title

    ', format_inline(d) { parse(template) }) s1 = Struct.new("SS", :title) d = s1.new("struct title") d.extend ExpandByMember assert_equal('

    struct title

    ', format_inline(d) { parse(template) }) end class S1 < Struct.new("S1", :name, :email) include Amrita::ExpandByMember end def test_expander2 template = "

    xx

    zz

    " d = { :title1 => "expand title1" , :title2 => "expand title2"} assert_equal('

    expand title1

    expand title2

    ', format_inline(d) { parse(template) }) d = { :title1 => (e(:em) { "expand title1" } ), :title2 => "expand title2"} assert_equal('

    expand title1

    expand title2

    ', format_inline(d) { parse(template) }) template = "
    • name
    " d = { :list => [ { :name => 'aaa', :email => 'aaa@xxx' }, { :name => 'bbb', :email => 'bbb@xxx' }, { :name => 'ccc', :email => 'ccc@xxx' }, ] } assert_equal( '
      ' + '
    • aaa aaa@xxx
    • ' + '
    • bbb bbb@xxx
    • ' + '
    • ccc ccc@xxx
    • ' + '
    ', format_inline(d) { parse(template) }) # puts gen_html_multi(d) { parse(template) } # puts format_inline(d) { parse(template) } d = { :list => [ S1.new("aaa","aaa@xxx"), S1.new("bbb","bbb@xxx"), S1.new("ccc","ccc@xxx") ] } assert_equal( '
      ' + '
    • aaa aaa@xxx
    • ' + '
    • bbb bbb@xxx
    • ' + '
    • ccc ccc@xxx
    • ' + '
    ', format_inline(d) { parse(template) }) end def test_expander3 template = "
    • name email
    " template += "
    1. name email
    " #puts parse(template).to_ruby d = { :list1 => [ S1.new("aaa","aaa@xxx"), S1.new("bbb","bbb@xxx"), ], :list2 => [ S1.new("xxx","xxx@zzz"), S1.new("yyy","yyy@zzz"), ] } assert_equal( '
      ' + '
    • aaa aaa@xxx
    • ' + '
    • bbb bbb@xxx
    • ' + '
    ' + '
      ' + '
    1. xxx xxx@zzz
    2. ' + '
    3. yyy yyy@zzz
    4. ' + '
    ', format_inline(d) { parse(template) }) end def test_prettyprint s = "" f = PrettyPrintFormatter.new(s) f.format(parse('
    • aaa
    ')) assert_equal("\n
      \n
    • aaa
    • \n
    \n", s) s = "" f = PrettyPrintFormatter.new(s) f.format(parse("\n
      \n
    • aaa bbb
    • \n
    \n")) assert_equal(" \n
      \n
    • aaa bbb
    • \n
    \n", s) html = < walrusのサンプル END s = "" f = PrettyPrintFormatter.new(s) f.format(parse(html)) result = < walrusのサンプル END assert_equal(result, s) end def test_proc template = HtmlParser.parse_inline <<-END
  • END colomun_data = [[ "aa", "bb"], ["cc", "dd"], ["ee", "ff"], ["gg", "hh"]] data = { :table1 => proc do |table, context| h = table.find { |e| e.hid == "header" } l1 = table.find { |e| e.hid == "line1" } l2 = table.find { |e| e.hid == "line2" } l1.delete_attr!(:id) l2.delete_attr!(:id) ret = [ h ] colomun_data.each_with_index do |d, i| if i%2 == 0 ret << l1.expand({ :col11 => d[0], :col12 => d[1] }, context) else ret << l2.expand({ :col21 => d[0], :col22 => d[1] }, context) end end ret end } result = < col1 col2 aa bb cc dd ee ff gg hh END s = "" f = PrettyPrintFormatter.new(s) f.format(template.expand(data)) assert_equal(result, s) end def test_dt template = HtmlParser.parse_inline <<-END
    1
    2
    3
    4
    5
    6
    END assert_equal("
    1
    2
    3
    4
    5
    6
    ", template.to_s) end def test_pre pre_text = <<-END
    1
     2
      3
     4
    5
        
    END template = HtmlParser.parse_text(pre_text) assert_equal(pre_text.strip, template.to_s.strip) d = { :xxx=> CompactSpace.new(false) { "\nabc\nefg\n" } } assert_equal('
    abc
    efg
    
    ', template.expand(d).to_s) assert_equal(pre_text.strip, template.to_s.strip) end def test_noescape template = HtmlParser.parse_inline <<-END
    END d = { :list => [ "aaaa", e(:span) { [ "bbbb", e(:br), "cccc" ] }, noescape { "dddd
    eeee" } ] } assert_equal("
    • aaaa
    • bbbb
      cccc
    • dddd
      eeee
    ", template.expand(d).to_s.gsub(/\s/, "")) end def test_attrarray template = HtmlParser.parse_inline <<-END
    END d = { :body=>a(:bgcolor,"blue", :text, "red") { { :table=>a(:border, 1) { { :list=> [ { :xxx=>"x1", :yyy=>"y1" }, { :xxx=>"x2", :yyy=>"y2" }, ] } } } } } assert_equal((<
    x1 y1
    x2 y2
    END end def test_filter tmpl = HtmlParser.parse_text('

    ') do |e| if e[:walrus] e[:id] = e[:walrus] e.delete_attr!(:walrus) end e end assert_equal('

    ', tmpl.to_s) end def test_escaped_char assert_equal('<>&"',parse("<>&"").to_s) end end #--- main program ---- if __FILE__ == $0 require 'runit/cui/testrunner' if ARGV.size == 0 RUNIT::CUI::TestRunner.run(TestHtmlParser.suite) else ARGV.each do |method| RUNIT::CUI::TestRunner.run(TestHtmlParser.new(method)) end end end