#!/usr/bin/env ruby
# $Id: tc_ext_interwiki.rb,v 1.1 2004/05/09 09:24:53 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.

require 'test_common'

class TC_InterWiki < Test::Unit::TestCase
  include TestSite

  def test_interwiki
    page = @site.create('InterWikiName')
    page.store(',Test,http://example.com/?q=,sjis')
    iw = @site.interwiki
    assert_equal("http://example.com/?q=t", iw.href("Test", "t"))
    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'>Test:t</a>", "{{interwiki(Test:t)}}".w.html_p(@site))
    assert_equal("<a href='http://example.com/?q=t' class='interwiki'>content</a>", "{{interwiki(Test:t)\ncontent\n}}".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("http://example.com/?q=%8E%9A", iw.href('SJIS', "字"))
    assert_equal("http://example.com/?q=%BB%FA", iw.href('EUC', "字"))
    assert_equal("http://example.com/?q=%E5%AD%97", iw.href('UTF8', "字"))
    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=%E5%AD%97' class='interwiki'>UTF8:字</a>", '[[UTF8:字]]'.w.html_p(@site))

    # test_interwiki_realuse
    page.store(<<"EOM")
,google,http://www.google.com/search?num=50&lr=lang_ja&q=,utf8
,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
,hiki,http://www.namaraii.com/hiki/hiki.cgi?,euc
,yukiwiki,http://www.hyuki.com/yukiwiki/wiki.cgi?,euc
EOM
    assert_equal("http://www.google.com/search?num=50&lr=lang_ja&q=%E5%AD%97", iw.href("google", '字'))
    assert_equal("http://www.google.com/search?num=50&lr=lang_ja&q=%3C", 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=%E5%AD%97&mode=blended", iw.href("amazon", '字'))
  end
end
