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

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

if $0 == __FILE__
  $KCODE = "s"
end

module Qwik
  module WabisabiGeneratorModule
    def method_missing(symbol, *args, &block)
      make(symbol, *args, &block)
    end

    def p(*args, &block)
      make(:p, *args, &block)
    end

    def make(symbol, *args, &block)
      symbol = symbol.intern if symbol.is_a?(String)
      ar = make_ar(symbol, *args, &block)
      ar
    end

    def make_ar(symbol, *args, &block)
      ar = []

      ar << symbol

      if 0 < args.length
	ar += args.flatten
      end

      if block
	y = block.call
	es = add_elems(y)
	if 0 < es.length
	  if es[0].is_a? Symbol
	    ar << es
	  else
	    ar += es
	  end
	end
      end

      ar
    end

    def add_elems(args)
      return [] if args.nil?
      return [args] unless args.kind_of? Array
      ar = []
      args.each {|a|
	a = "" if a.nil?
	ar << a
      }
      ar
    end
  end

  module WabisabiHtmlGeneratorModule
    include WabisabiGeneratorModule
    include HtmlGeneratorUnit
  end

  class WabisabiGenerator; include WabisabiGeneratorModule; end
  class WabisabiHtmlGenerator; include WabisabiHtmlGeneratorModule; end

  class Generator < WabisabiGenerator; end
  class HtmlGenerator < WabisabiHtmlGenerator; end
end

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

if $debug
  class TC_WabisabiGenerator < Test::Unit::TestCase
    def assert(e, s)
      assert_equal(e, s)
    end

    def test_all
      @g = Qwik::WabisabiGenerator.new

      # test_htree_generator
      assert([:p], @g.p)
      assert([:a], @g.a)
      assert([:b], @g.b{})
      assert([:b, ""], @g.b{""})
      assert([:img, {:src=>"u"}], @g.img(:src=>"u"))
      assert([:b, "t"], @g.b{"t"})
      assert([:p, [:b, "b"]], @g.p{@g.b{"b"}})
      assert([:p, "t", [:p, "b"], "t"], @g.p{["t", @g.p{"b"}, "t"]})
      assert([:a, {:href=>"u"}, "t"], @g.a(:href=>"u"){"t"})
      assert([:font, {:size=>"7"}, "t"], @g.font(:size=>"7"){'t'})

      # test_namespace
      assert([:b, "t"], @g.make("b"){"t"}) 
      assert([:"n:b", "t"], @g.make("n:b"){"t"})
      assert([:a, {:href=>"u"}, "t"], @g.make("a", :href=>"u"){"t"})
      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([:html,
 [:head, [:title, "タイトル"]],
 [:body,
  [:pre, [:b, "world"], "hello"],
  [:pre, "this is ", [:b, "bold"], " text."],
  [:pre, "this is ", [:i, "italic"], " text."],
  [:p, "this is ", [:a, {:href=>"hoge"}, "anchor"], " text."]]],
 html)

      # test_with_underbar
      assert([:ab], @g.ab)
      assert([:"a-b"], @g.make("a-b"))

      # test_ordered_hash
      assert([:a, {"b"=>"c"}], @g.a({"b"=>"c"}))
      assert([:a, {"b"=>"c"}], @g.a([{"b"=>"c"}]))
      assert([:a, {"b"=>"c"}, {"d"=>"e"}], @g.a([{"b"=>"c"}, {"d"=>"e"}]))
      assert([:a, {"b"=>"c"}, {"d"=>"e"}], @g.a({"b"=>"c"}, {"d"=>"e"}))
    end
  end

  class TC_WabisabiHtmlGenerator < Test::Unit::TestCase
    def assert(e, s)
      assert_equal(e, s)
    end

    def test_htree_html_generator
      @g = Qwik::WabisabiHtmlGenerator.new
      assert([:p], @g.p)

      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",
		 :type=>"text/css", :rel=>"stylesheet"}],
	     @g.stylesheet("style1.css"))

      # test_link
      assert([:a, {:href=>"t"}, "s"], @g.a("t"){"s"})
      assert([:a, {:href=>"t?a"}, "s"], @g.a("t?a"){"s"})
      assert([:a, {:href=>"t?a&b"}, "s"], @g.a("t?a&b"){"s"})
      assert([:a, {:href=>"t"}, "<"], @g.a("t"){"<"})

      # test_form
      assert([:input, {:name=>"n"}], @g.text("n"))
      assert([:input, {:value=>"t", :type=>"submit", :name=>"n"}],
	     @g.submit("t", "n"))
      assert([:textarea, {:name=>""}, "t"],
	     @g.textarea{"t"})
      assert([:select,
{:name=>"n"},
[:option, {:name=>"t1"}, "t1"],
[:option, {:name=>"t2"}, "t2"]], @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, {:id=>"i", :value=>"v", :name=>"n"}],
	     @g.hidden({:name=>"n", :value=>"v", :id=>"i"}))
      assert([:input, {:id=>"i", :value=>"v", :name=>"n"}],
	     @g.hidden(:name=>"n", :value=>"v", :id=>"i"))

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

      # 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, "タイトル"],
		 [:link,
		   {:href=>"style.css", :rel=>"stylesheet", :type=>"text/css"}]],
	       [:body,
		 [:pre, [:b, "world"], "hello"],
		 [:pre, "This is ", [:b, "bold"], " text."],
		 [:pre, "This is ", [:i, "italic"], " text."],
		 [:p, "This is ", [:a, {:href=>"hoge"}, "anchor"], " text."],
		 [:p, "This is ", [:a, {:href=>"hoge"}, "anchor"], " text."],
		 [:img, {:alt=>"new", :src=>"new.gif"}]]], html)
    end
  end

  class TC_WabisabiXmlGeneratorModule < Test::Unit::TestCase
    include Qwik::WabisabiGeneratorModule

    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_WabisabiHtmlGeneratorModule < Test::Unit::TestCase
    include Qwik::WabisabiHtmlGeneratorModule

    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("<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)
    end
  end
end
