#!/usr/bin/env ruby
# $Id: tc_qxml.rb,v 1.17 2004/04/22 14:47:49 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/qxml'

class TC_Qxml < Test::Unit::TestCase
  def test_qxml
    g = Qxml.new
    assert_equal("<b>t</b>", g.b{'t'}.to_s)
    assert_equal("<p><b>b</b></p>", g.p{g.b{'b'}}.to_s)
    assert_equal("<p>t<p>b</p>t</p>", g.p{['t', g.p{'b'}, 't']}.to_s)
    assert_equal("<a href='u'>t</a>", g.a(:href=>'u'){"t"}.to_s)

    x = g.p # test_rawtext
    x.rawtext = '<b>b</b>'
    assert_equal("<p><b>b</b></p>", x.to_s)
    x.rawtext = '<b>b' # ilegal but accept
    assert_equal("<p><b>b</p>", x.to_s)
    x.rawtext = '<' # ilegal but accept
    assert_equal("<p><</p>", x.to_s)

    assert_equal("<b>&lt;&gt;'&quot;</b>", g.b{%q|<>'"|}.to_s) # '
    e = g.b{' t'};		assert_equal("<b> t</b>", e.to_s)
    e.get_text.delete_spaces;	assert_equal("<b>t</b>", e.to_s)
    e.text = " t ";		assert_equal("<b> t </b>", e.to_s)
    e.get_text.delete_spaces;	assert_equal("<b>t</b>", e.to_s)
    e.rawtext = "t ";		assert_equal("<b>t </b>", e.to_s)
    e.get_text.delete_spaces;	assert_equal("<b>t</b>", e.to_s)

    assert_equal("abc&amp;def", REXML::Text.new("abc&def").to_s)
    assert_equal("&quot;'&amp;", REXML::Text.new('"\'&').to_s)
    assert_equal("&quot;'&lt;&amp;&gt;", REXML::Text.new('"\'<&>').to_s)

    assert_equal("<font size='7'>t</font>", g.font(:size => 7){'t'}.to_s)
    assert_equal(<<"EOT".chomp, g.p{g.b{'b'}}.to_s(0))
<p>
  <b>b</b>
</p>
EOT

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

    xml = REXML::QDocument.new("<a><b><c/><d/><c/></b></a>") # test_delete_tag
    assert_equal("<a><b><c/><d/><c/></b></a>", xml.to_s)
    xml.delete_tag('c')
    assert_equal("<a><b><d/></b></a>", xml.to_s)
    xml.delete_tag('b')
    assert_equal("<a/>", xml.to_s)
  
    xml = REXML::QDocument.new("<a><b><c/><d/><c/></b></a>") # test_tag_each
    assert_equal("<a><b><c/><d/><c/></b></a>", xml.to_s)
    xml.tag('c'){|e| e.text = 'test1' }
    assert_equal("<a><b><c>test1</c><d/><c>test1</c></b></a>", xml.to_s)
    xml.tag('b'){|e| e.text = 'test2' }
    assert_equal("<a><b><c>test1</c><d/><c>test1</c>test2</b></a>", xml.to_s)

    xml = REXML::QDocument.new("<a id='b'/>") # test_delete_id
    assert_equal("<a id='b'/>", xml.to_s)
    xml.delete_id
    assert_equal("<a/>", xml.to_s)
  end

  def test_qxhtml
    g = Qxhtml.new
    assert_equal("<meta content='text/html' http-equiv='Content-Type'/>", g.contenttype("text/html").to_s)
    assert_equal("<meta content='0; url=t' http-equiv='Refresh'/>", g.refresh(0, 't').to_s)
    assert_equal("<link href='style1.css' rel='stylesheet' type='text/css'/>",
		 g.stylesheet("style1.css").to_s)

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

    assert_equal("<input name='n' type='submit' value='t'/>", g.submit('t', 'n').to_s) # test_form
    assert_equal("<textarea name=''>t</textarea>", g.textarea(){'t'}.to_s)
    assert_equal("<select name='n'><option name='t1'>t1</option><option name='t2'>t2</option></select>", g.select('n', 't1', 't2').to_s)
    assert_equal("<input name='n' type='hidden' value='v'/>", g.hidden('n', 'v').to_s)

    # 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_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.to_s)
    assert_equal(<<"EOT".chomp, html.to_s(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

  def test_qxml_template
    str = "<p id='t'>s</p>"
    xml = REXML::QDocument.new(str)
    assert_equal("<p id='t'>s</p>", xml.to_s)

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

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

    attr = REXML::Attr.new(:class => 'testclass')
    xml.apply({:t => attr})
    assert_equal("<p class='testclass' id='t'>test</p>", xml.to_s)

    str = "<div><p id='t1'>s1</p><p id='t2'>s2</p></div>"
    xml = REXML::QDocument.new(str)
    assert_equal("<div><p id='t1'>s1</p><p id='t2'>s2</p></div>", xml.to_s)

    xml.apply({:t1 => 'test1'})
    assert_equal("<div><p id='t1'>test1</p><p id='t2'>s2</p></div>", xml.to_s)

    xml.apply({:t1 => 'test1', :t2 => 'test2'})
    assert_equal("<div><p id='t1'>test1</p><p id='t2'>test2</p></div>", xml.to_s)
  
    str = "<div>あ<p id='t'>い</p>う</div>" # test_template_japanese
    xml = REXML::QDocument.new(str)
    assert_equal("<div>あ<p id='t'>い</p>う</div>", xml.to_s)

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

    str = "<p id='t'>あ</p>" # test_template_exception
    xml = REXML::QDocument.new(str)
    assert_equal("<p id='t'>あ</p>", xml.to_s)

    xml.apply({:t => '〜'})
    #assert_equal("<p id='t'>&#xff5e;</p>", xml.to_s)
    assert_equal("<p id='t'>〜</p>", xml.to_s)

    str = "<p id='t'>〜</p>" # test_template_wave_dash
    xml = REXML::QDocument.new(str)
    assert_equal("<p id='t'>〜</p>", xml.to_s)
    text = xml.root.text
    assert_equal("〜", text)
  end

  def test_quickpath
    str = "<p id='t'>s</p>"
    str = "<div><p class='t1'>s<p class='t3'>s</p></p><p class='t2'>s</p></div>"
    xml = REXML::Document.new(str)
    e = REXML::QuickPath.axe(xml, 'child', 'p')
#    p e.to_s
#    e = REXML::QuickPath.axe(xml, 'descendant-or-self', 'p')
    e = REXML::QuickPath.axe(xml, 'descendant', 'p')
#    p e.to_s
#    e = REXML::QuickPath.axe(xml, 'descendant', "[@class]")
#    p e.to_s

#    e = REXML::QuickPath.axe(xml, 'attribute', 'class')
#    REXML::QuickPath.each(xml, "//[@class]"){|e|
#      p e.to_s
#    }
  end

  def test_apos
    assert_equal("'", REXML::Text.normalize("'"))
    assert_equal("&lt;&amp;", REXML::Text.new("<&").to_s)
    assert_equal("'", REXML::Text.new("'").to_s)
    g = Qxhtml.new
    xml = g.p(:title => "What's New"){"t"}
    assert_equal("<p title='What&apos;s New'>t</p>", xml.to_s)
    xml = g.p{"What's New"}
    assert_equal("<p>What's New</p>", xml.to_s)

    str = "<a>'</a>"
    xml = REXML::Document.new(str)
    assert_equal("<a>'</a>", xml.to_s)
    xml.root.text = "a"
    assert_equal("<a>a</a>", xml.to_s)
    xml.root.text = "'"
    assert_equal("<a>'</a>", xml.to_s)
  end
end
