#!/usr/bin/env ruby
# $Id: tc_qtree.rb,v 1.3 2004/05/11 13:08:23 eto Exp $
# Copyright (C) 2003-2004 Kouichirou Eto, All rights reserved.
# This is free software with ABSOLUTELY NO WARRANTY.
# You can redistribute it and/or modify it under the terms of the GNU GPL2.

$LOAD_PATH.unshift("../../lib")
$LOAD_PATH.unshift("../../compat")
require "test/unit"
require "qwik/qtree"
require "qwik/qp"

module HTree
  class Template
    def test(t)
      str = "<p id='t'>s</p>"
      t.assert_equal("<p _text=\"data['t']\">s</p>", parse_str(str))
      str = "<p id='s_t'>s</p>"
      t.assert_equal("<p _text=\"data['s_t']\">s</p>", parse_str(str))

      sdata = {}
      sdata[:t] = "s"
      data = symbol_to_str(sdata)
      t.assert_equal({"t"=>"s"}, data)
    end
  end
end

class TC_QTree < Test::Unit::TestCase
  def test_generator
    g = HTree::Generator.new
    assert_equal("<a/>", g.a.r)
    assert_equal("<img src='u'/>", g.img(:src=>"u").r)
    assert_equal("<b>t</b>", g.b{"t"}.r)
    assert_equal("<p><b>b</b></p>", g.p{g.b{"b"}}.r)
    assert_equal("<p>t<p>b</p>t</p>", g.p{["t", g.p{"b"}, "t"]}.r)
    assert_equal("<a href='u'>t</a>", g.a(:href=>"u"){"t"}.r)
    assert_equal("<font size='7'>t</font>", g.font(:size => 7){'t'}.r)

    # test_namespace
    assert_equal("<b>t</b>", g.make("b"){"t"}.r) 
    assert_equal("<n:b>t</n:b>", g.make("n:b"){"t"}.r)
    assert_equal("<a href='u'>t</a>", g.make("a", :href=>"u"){"t"}.r)

    html = g.html {[
	g.head {[
	    g.title {"タイトル"},
	  ]},
	g.body {[
	    g.pre {[g.b {"world"},  "hello"]},
	    g.pre {["This is ", g.b{"bold"}, " text."]},
	    g.pre {["This is ", g.i{"italic"}, " text."]},
	    g.p {["This is ", g.a(:href=>"hoge"){"anchor"}, " text."]},
	  ]}
      ]}
    assert_equal("<html><head><title>タイトル</title></head><body><pre><b>world</b>hello</pre><pre>This is <b>bold</b> text.</pre><pre>This is <i>italic</i> text.</pre><p>This is <a href='hoge'>anchor</a> text.</p></body></html>", html.r)
  end

  def assert_xhtml(expected, template, message=nil)
    prefix = '<?xml version="1.0" encoding="US-ASCII"?>' +
             "<html xmlns=\"http://www.w3.org/1999/xhtml\"\n>"
    suffix = "</html\n>"
    result = HTree.expand_template(''){"<html>#{template}</html>"}
    assert_match(/\A#{Regexp.quote prefix}/, result)
    assert_match(/#{Regexp.quote suffix}\z/, result)
    result = result[prefix.length..(-suffix.length-1)]
    assert_equal(expected, result, message)
  end

  def test_htree
    assert_xhtml("<b\n>t</b\n>", "<b>t</b>")
    assert_xhtml("<e\n>1</e\n>", '<e _text=1>d</e>')
#    str = "<html><e _text=1>d</e></html>"
#    doc = HTree{str}
#    pp doc
#    doc = HTree(str)
#    pp doc

    str = <<"EOS"
<html>
  <body _text="body"></body>
</html>
EOS
    body = "this is body text."
    result = HTree.expand_template(''){str}
    assert_equal("<?xml version=\"1.0\" encoding=\"US-ASCII\"?><html xmlns=\"http://www.w3.org/1999/xhtml\"\n><body\n>this is body text.</body\n></html\n>", result)
  end

  def test_template
    str = "<p id='t'>s</p>"
    template = HTree::Template.new(str)
    template.test(self)

    xml = template.apply()
    assert_equal("<p/>", xml.r)

    xml = template.apply({:t => 'dddd'})
    assert_equal("<p>dddd</p>", xml.r)

    xml = template.apply({:t => 'あ'})
    assert_equal("<p>あ</p>", xml.r)
    return

    str = "<p id='t'>s</p>"
    xml = HTree::Template.new(str)
    assert_equal("<p id='t'>s</p>", xml.to_s)

    xml.apply({:t => 'あ'})
    assert_equal("<p id='t'>あ</p>", xml.to_s)
  end

end
