# Copyright (C) 2003-2005 Kouichirou Eto, All rights reserved.

$LOAD_PATH.unshift("../../compat") unless $LOAD_PATH.include?("../../compat")
require "htree"
$LOAD_PATH << "../../lib" unless $LOAD_PATH.include?("../../lib")
require "qwik/html-generator"
require "qwik/htree-generator"

module HTree
  module HtmlGeneratorModule
    include GeneratorModule
    include HtmlGeneratorUnit
  end

  class HtmlGenerator; include HtmlGeneratorModule; end
end

if $0 == __FILE__
  require "test/unit"
  require "qwik/htree-format-xml"
  $debug = true
end

if $debug
  class TC_HTreeHtmlGeneratorModule < Test::Unit::TestCase
    include HTree::HtmlGeneratorModule

    def test_html_generator_module
      html = html {[
	  head {[
	      contenttype("text/html; charset=SHIFT_JIS"),
	      title {"タイトル"},
	      stylesheet("style.css")
	    ]},
	  body {[
	      pre {[b {"world"},  "hello"]},
	      pre {["This is ", b{"bold"}, " text."]},
	      pre {["This is ", i{"italic"}, " text."]},
	      p {["This is ", a("hoge"){"anchor"}, " text."]},
	      p {["This is ", a(:href=>"hoge"){"anchor"}, " text."]},
	      img("new.gif", "new")
	    ]}
        ]}
      assert_equal("<html><head><meta content=\"text/html; charset=SHIFT_JIS\" http-equiv=\"Content-Type\"/><title>タイトル</title><link href=\"style.css\" rel=\"stylesheet\" type=\"text/css\"/></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><p>This is <a href=\"hoge\">anchor</a> text.</p><img src=\"new.gif\" alt=\"new\"/></body></html>", html.format_xml)
    end
  end

  class TC_HTreeXmlGeneratorModule < Test::Unit::TestCase
    include HTree::GeneratorModule

    def test_generator_module
      assert("<b/>", b)
      assert("<b/>", b{})
      assert("<b>t</b>", b{"t"})
      assert("<div><b>t</b></div>", div{b{"t"}})
      assert("<p><b>b</b></p>", p{b{"b"}})
      assert("<p>t<p>b</p>t</p>", p{["t", p{"b"}, "t"]})
      assert("<a href=\"u\">t</a>", a(:href=>"u"){"t"})
    end
  end

  class TC_HTreeHtmlGenerator < Test::Unit::TestCase
    def test_htree_html_generator
      @g = HTree::HtmlGenerator.new

      assert("<p/>", @g.p)
      doc = HTree::Doc.new(@g.p)
      assert("<p/>", doc)
      str = ""
      doc.display_xml(str)
      #assert_equal("<p xmlns=\"http://www.w3.org/1999/xhtml\"\n/>", str)

      assert("<meta content=\"text/html\" http-equiv=\"Content-Type\"/>",
	     @g.contenttype("text/html"))
      assert("<meta content=\"0; url=t\" http-equiv=\"Refresh\"/>",
	     @g.refresh(0, "t"))
      assert("<link href=\"style1.css\" rel=\"stylesheet\" type=\"text/css\"/>",
	     @g.stylesheet("style1.css"))

      assert("<a href=\"t\">s</a>", @g.a("t"){"s"}) # test_link
      assert("<a href=\"t?a\">s</a>", @g.a("t?a"){"s"})
      assert("<a href=\"t?a&amp;b\">s</a>", @g.a("t?a&b"){"s"})
      assert("<a href=\"t\">&lt;</a>", @g.a("t"){"<"})

      assert("<input name=\"n\" type=\"submit\" value=\"t\"/>",
	     @g.submit("t", "n")) # test_form
      assert("<textarea name=\"\">t</textarea>", @g.textarea{"t"})
      assert("<select name=\"n\"><option name=\"t1\">t1</option><option name=\"t2\">t2</option></select>", @g.select("n", "t1", "t2"))

      assert("<input type=\"hidden\" name=\"n\"/>", @g.hidden("n"))
      assert("<input type=\"hidden\" name=\"n\" value=\"v\"/>",
	     @g.hidden("n", "v"))
      assert("<input name=\"n\" id=\"i\" value=\"v\"/>",
	     @g.hidden({:name=>"n", :value=>"v", :id=>"i"}))
      assert("<input name=\"n\" id=\"i\" value=\"v\"/>",
	     @g.hidden(:name=>"n", :value=>"v", :id=>"i"))

      assert("<form>t</form>", @g.form{"t"})
      assert("<form method=\"POST\">t</form>", @g.form("POST"){"t"})
      assert("<form method=\"POST\" action=\"u\">t</form>",
	     @g.form("POST", "u"){"t"})
      assert("<form enctype=\"multipart/form-data\" method=\"POST\">t</form>",
	     @g.form(:method=>"POST", :enctype=>"multipart/form-data"){"t"})

      # test_apos
      assert("<&", HTree::Text.new("<&"))
      assert("'", HTree::Text.new("'"))
      xml = @g.p(:title => "What's New"){"t"}
      assert("<p title=\"What's New\">t</p>", xml)
      xml = @g.p{"What's New"}
      assert("<p>What's New</p>", xml)

      # test_html
      html = @g.html {[
	  @g.head {[
	      @g.contenttype("text/html; charset=SHIFT_JIS"),
	      @g.title {"タイトル"},
	      @g.stylesheet("style.css")
	    ]},
	  @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("hoge"){"anchor"}, " text."]},
	      @g.p {["This is ", @g.a(:href=>"hoge"){"anchor"}, " text."]},
	      @g.img("new.gif", "new")
	    ]}
	]}
      assert("<html><head><meta content=\"text/html; charset=SHIFT_JIS\" http-equiv=\"Content-Type\"/><title>タイトル</title><link href=\"style.css\" rel=\"stylesheet\" type=\"text/css\"/></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><p>This is <a href=\"hoge\">anchor</a> text.</p><img src=\"new.gif\" alt=\"new\"/></body></html>", html)

      assert_equal(<<"EOT".chomp, html.format_xml(0))
<html
><head
><meta content="text/html; charset=SHIFT_JIS" http-equiv="Content-Type"
/><title
>タイトル</title
><link href="style.css" rel="stylesheet" type="text/css"
/></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
><p
>This is <a href="hoge"
>anchor</a
> text.</p
><img src="new.gif" alt="new"
/></body
></html
>
EOT
    end
  end
end
