#!/usr/bin/env ruby
# $Id: tc_ext_charset.rb,v 1.3 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.

$use_charset = true
$LOAD_PATH.unshift('../../lib')
$LOAD_PATH.unshift('../../compat')
require 'test/unit'
require 'qwik/util'
require 'qwik/site'
require 'qwik/formatter'
require 'qwik/ext_charset'
require 'test_session'

class TC_CharsetPlugin < 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')
    options = Hash.new('')
    options['page'] = '19'
    options['site']   = @site
    options['session'] = Test::Session.new
    @plugin = Qwik::Plugin.new(@config, options)
  end

  def test_charset
    pages = @site.pages
    n = '_chartest'
    p = pages.exist?(n) ? pages[n] : pages.create(n)
    p.store("test")
    assert_equal(true, pages.exist?(n))
    assert_equal(%Q!<p>test</p>!, 'test'.w.html(@site,@plugin))
    assert_equal(%Q!test!, p.load)
    assert_equal(%Q!test!, pages[n].load)
    assert_equal(%Q!<p>test</p>\n!, p.load.w.html(@site,@plugin))

    p.store(<<"EOT")
{{define_char
:my:1
:ids:#0木森
}}

{{define_char
:my:2
:ids:#0森森
}}
EOT
    assert_equal('', p.load.w.html(@site,@plugin))
    assert_equal('#0木森', "&my-1;".de_er.char.ids)
    assert_equal('#0森森', "&my-2;".de_er.char.ids)

    url = @plugin.my_char_kage_url()
    p.store("{{my_char(1)}}")
    assert_equal(%Q!<p><img src="#{url}u2ff0u6728u68ee.gothic.png" style="width:1em;"/></p>\n!, p.load.w.html(@site,@plugin))
    p.store("&my-1;")
    assert_equal(%Q!<p><img src="#{url}u2ff0u6728u68ee.gothic.png" style="width:1em;"/></p>\n!, p.load.w.html(@site,@plugin))
    p.store("{{my_char(2)}}")
    assert_equal(%Q!<p><img src="#{url}u2ff0u68eeu68ee.gothic.png" style="width:1em;"/></p>\n!, p.load.w.html(@site,@plugin))

    p.store("{{my_char(a)}}")
    assert_equal(%Q!<p></p>\n!, p.load.w.html(@site,@plugin))

#    p.store("&my-3;") #そんなの存在していない
#    assert_equal(%Q!<p></p>\n!, p.load.w.html(@site,@plugin)) #やっぱり中は空と。

    p.store("{{my_char_ids(1)}}")
    assert_equal(%Q!<p>#0木森</p>\n!, p.load.w.html(@site,@plugin))
    p.store("{{my_char_ids(2)}}")
    assert_equal(%Q!<p>#0森森</p>\n!, p.load.w.html(@site,@plugin))

    pages.delete(n)
    assert_equal(false, pages.exist?(n))
  end

  def test_charset2
    pages = @site.pages
    n = '_chartest'
    p = pages.exist?(n) ? pages[n] : pages.create(n)
    p.store(<<"EOT")
{{define_char
:my:1
:ids:#0木森
}}

{{define_char
:my:2
:ids:#0森森
}}

{{define_char
:my:3
:ids:#1&U-6728;森
}}
EOT
    assert_equal('', p.load.w.html(@site,@plugin)) # eval the _chartest here.
    assert_equal('#0木森', "&my-1;".de_er.char.ids)
    assert_equal('#0森森', "&my-2;".de_er.char.ids)
    assert_equal('木', "&U-6728;".de_er)
#    assert_equal('#1木森', "&my-3;".de_er.char.ids)
#    assert_equal('OV行重', "&my-2;".de_er.char.ids)
    p.store("&my-1;")
    url = @plugin.my_char_kage_url()
    assert_equal(%Q!<p><img src="#{url}u2ff0u6728u68ee.gothic.png" style="width:1em;"/></p>\n!, p.load.w.html(@site,@plugin))

    pages.delete(n)
    assert_equal(false, pages.exist?(n))
  end

  def teardown()
  end
end

class TC_ChiseBasic < 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')
    options = Hash.new('')
    options['page'] = '19'
    options['site']   = @site
    options['session']    = Test::Session.new
    @plugin = Qwik::Plugin.new(@config, options)

    @char = "字".su.char
  end

  def test_utf8
    assert_equal("\273\372", "字".se)
    assert_equal("\345\255\227", "字".su)
    assert_equal("\273\372", "字".su.ue)
    assert_equal("\216\232", "字")
    assert_equal("\273\372", "字".se)
    assert_equal("\345\255\227", "字".su)
    assert_equal("\216\232", "字".su.us)
  end

  def test_er
    assert_equal(23383, "字".su.ucs)
    assert_equal(@char, Character.get("&J90-3B7A;"))
    assert_equal(@char, Character.get("&MCS-00005B57;"))
    assert_equal(@char, Character.get("&M-06942;"))
    assert_equal("字", "&J90-3B7A;".de_er.us)
    assert_equal("字", "&U5B57;".de_er.us)
    assert_equal("字", "&U-5B57;".de_er.us)
    assert_equal("字", "&U+5B57;".de_er.us)
    assert_equal("字", "&#x5B57;".de_er.us)
    assert_equal("字", "&#23383;".de_er.us)
  end

  def test_my
    # private use area: 0xe000〜0xf8ff
    k = Character.get(0xe001)
    assert_equal(0xe001, k.ucs)
    assert_equal("<\356\200\201,\#xe001>", k.inspect)

    k = "&#xe001;".de_er.char
    assert_equal(0xe001, k.ucs)

    k = "&my-0001;".de_er.char
    assert_equal(0xe001, k.ucs)
    k.ids = IDC_LR+"木森"
    assert_equal(IDC_LR+"木森", k.ids)
    assert_equal(IDC_LR+"木森", "&my-0001;".de_er.ids)
    u = "http://home.fonts.jp:5100/"
    k.kage_url = u
    assert_equal(u, "&my-0001;".de_er.kage_url)

    k = "&my-0002;".de_er.char
    assert_equal(0xe002, k.ucs)
    k.ids = IDC_LR+"森森"
    assert_equal(IDC_LR+"森森", k.ids)
    assert_equal(IDC_LR+"森森", "&my-0002;".de_er.ids)

    "字".eu.mydepth = 1
    assert_equal(1, "字".eu.mydepth)
  end

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