#!/usr/bin/env ruby
# $Id: tc_formatter.rb,v 1.30 2004/04/22 02:37:45 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/diff'
require 'qwik/page'
require 'qwik/wikitext'

class TC_HTMLFormatter < Test::Unit::TestCase
  def setup()
    @config = Qwik::Config.new(true)
    @config.data_dir = './'
    @dir = './test/'.path
    @dir.rmtree if @dir.directory?
    @dir.rmdir  if @dir.directory?
    @dir.mkdir if !@dir.directory?
    @site = Qwik::Site.new(@config, 'test')
  end

  def test_formatter
    assert_equal("<h2>t</h2>", '*t'.w.html) # test_header
    assert_equal("<h3>t</h3>", '**t'.w.html)
    assert_equal("<h2>t</h2>", '* t'.w.html) # test_ignore_space
    assert_equal("<h2>t</h2>", '*t '.w.html)
    assert_equal("<h2>t</h2>", '* t '.w.html)
    assert_equal("<h3>t</h3>", '** t'.w.html)
    assert_equal("<h3>t</h3>", '**t '.w.html)
    assert_equal("<h3>t</h3>", '** t '.w.html)
    assert_equal("<h2>'</h2>", "*'".w.html) # test_escape
    assert_equal("<h2>&lt;</h2>", "*<".w.html) # test_escape

    assert_equal("<dl><dt>t</dt>\n<dd>s</dd>\n</dl>", ':t:s'.w.html) # test_block
    assert_equal("<dl><dt>t</dt>\n<dd />\n</dl>", ':t:'.w.html)
    assert_equal("<dl><dt>t</dt>\n</dl>", ':t'.w.html)
    assert_equal("<blockquote><p> t</p>\n</blockquote>", '> t'.w.html)
    assert_equal("<blockquote><p>t</p>\n</blockquote>", '>t'.w.html)
    assert_equal("<div class='table'><table><tr><td>t</td>\n<td>s</td>\n</tr>\n</table></div>", '|t|s|'.w.html)

    # test_pre
    assert_equal("<pre>t\n</pre>", ' t'.w.html)
    assert_equal("<pre>s\nt\n</pre>", " s\n t".w.html)

    # test_name()
    #assert_equal("<h2><span class='date'/><span class='title'><a name='l0'> </a> t</span></h2>", '* t'.w.html(@site))

    # test_lines
    assert_equal("<ul><li> t1</li>\n<li> t2</li>\n</ul>", "- t1\n- t2".w.html)
    assert_equal("<p>t1</p>\n<ul><li> t2</li>\n</ul>", "t1\n- t2".w.html)

    # test_inline
    assert_equal("<em>t</em>", "''t''".w.html_p)
    assert_equal("<strong>t</strong>", "'''t'''".w.html_p)

    # test_link
    assert_equal("<p>t</p>", "t".w.html)
#    page = @site['0']
    page = @site['FrontPage']
    page.store('*title')
    assert_equal("<a href='FrontPage.html'>FrontPage</a>", "[[FrontPage]]".w.html_p)
    assert_equal("<a href='FrontPage.html'>FrontPage</a>", "[[FrontPage]]".w.html_p(@site))
    page = @site.create('t')
    page.store('t2')
    assert_equal("<a href='t.html'>t</a>", "[[t]]".w.html_p)
    assert_equal("<a href='FrontPage.html'>t</a>", "[[t|FrontPage]]".w.html_p)
    assert_equal("<a href='t.html'>s</a>", '[[s|t]]'.w.html_p)
    page = @site.create('1')
    page.store('*あ')
    assert_equal("<a href='1.html'>1</a>", "[[1]]".w.html_p)
    assert_equal("<a href='1.html'>あ</a>", "[[1]]".w.html_p(@site))

    # test_find_title
    assert_equal("<a href='1.html'>あ</a>", "[[あ]]".w.html_p(@site))

    # test_newpage()
    assert_equal("<span class='new'>test</span><a href='_new.html?t=test' class='new'>?</a>", '[[test]]'.w.html_p(@site))
    assert_equal("<span class='new'>い</span><a href='_new.html?t=%82%A2' class='new'>?</a>", '[[い]]'.w.html_p(@site))

    # test_external_link
    assert_equal("<a href='http://example.com/'>http://example.com/</a>", '[[http://example.com/]]'.w.html_p)
    assert_equal("<a href='http://example.com/'>http://example.com/</a>", 'http://example.com/'.w.html_p)
    assert_equal("<a href='http://example.com/'>t</a>", '[[t|http://example.com/]]'.w.html_p)

    # test_qwik_text
    assert_equal("<hr />", '===='.w.html)
    assert_equal("<ul><li> t</li>\n</ul>", '- t'.w.html)
    assert_equal("<ul><li> s</li>\n<ul><li> t</li>\n</ul>\n</ul>", "- s\n-- t".w.html)
    assert_equal("<ol><li> t</li>\n</ol>", '+ t'.w.html)

    # test_sjis
    assert_equal("<h2>あ</h2>", '* あ'.w.html)
    assert_equal("<h3>あ</h3>", '** あ'.w.html)
    assert_equal("<h3>あ</h3>", "** あ".w.html)
    assert_equal("<h3>漢字</h3>", "** 漢字".w.html)
    assert_equal("<h3>待</h3>", "** 待".w.html)

    # test_interwiki
    page = @site.create('InterWikiName')
    page.store('- [[Test|http://example.com/?q=]] sjis')
    assert_equal("<a href='http://example.com/?q=t' class='interwiki'>Test:t</a>", '[[Test:t]]'.w.html_p(@site))
    assert_equal("<a href='http://example.com/?q=t' class='interwiki'>text</a>", '[[text|Test:t]]'.w.html_p(@site))
    assert_equal("<span class='interwiki'>NosuchWiki:t</span>", '[[NosuchWiki:t]]'.w.html_p(@site))

    # test_interwiki_kanji
    page.store(<<"EOM")
- [[SJIS|http://example.com/?q=]] sjis
- [[EUC|http://example.com/?q=]] euc
- [[UTF8|http://example.com/?q=]] utf8
EOM
    assert_equal("<a href='http://example.com/?q=%8E%9A' class='interwiki'>SJIS:字</a>", '[[SJIS:字]]'.w.html_p(@site))
    assert_equal("<a href='http://example.com/?q=%BB%FA' class='interwiki'>EUC:字</a>", '[[EUC:字]]'.w.html_p(@site))
    assert_equal("<a href='http://example.com/?q=字' class='interwiki'>UTF8:字</a>", '[[UTF8:字]]'.w.html_p(@site))

    # test_interwiki_realuse
    page.store(<<"EOM")
- [[Google|http://www.google.co.jp/search?num=50&lr=lang_ja&q=]] sjis
- [[ISBN|http://www.amazon.co.jp/exec/obidos/ASIN/$1/ref=nosim]] raw
- [[Amazon|http://www.amazon.co.jp/exec/obidos/external-search?keyword=$1&mode=blended]] utf8 Amazon search
- [[Hiki|http://www.namaraii.com/hiki/hiki.cgi?]] euc
- [[YukiWiki|http://www.hyuki.com/yukiwiki/wiki.cgi?]] euc
EOM
    iw = Qwik::InterWiki.new(@site)
    assert_equal("http://www.google.co.jp/search?num=50&lr=lang_ja&q=%8E%9A", iw.href("Google", '字'))
    assert_equal("http://www.amazon.co.jp/exec/obidos/ASIN/412101278X/ref=nosim", iw.href("ISBN", '412101278X'))
    assert_equal("http://www.amazon.co.jp/exec/obidos/external-search?keyword=字&mode=blended", iw.href("Amazon", '字'))
    assert_equal("http://www.google.co.jp/search?num=50&lr=lang_ja&q=%3C", iw.href("Google", '<'))

    # test_apos
    assert_equal("'", "'".w.html_p(@site))

    assert_equal("a", "a".w.html_p)
    assert_equal("&lt;", "<".w.html_p)
  end

  def teardown()
    @dir.rmtree if @dir.directory?
    @dir.rmdir  if @dir.directory?
  end
end

class TC_Diff < Test::Unit::TestCase
  def test_diff
    d = Diff.diff("t", "tt")
    assert_equal('[[:+, 1, "t"]]', d.inspect)
    d = Diff.diff("tt", "t")
    assert_equal('[[:-, 1, "t"]]', d.inspect)
  end
end
