#!/usr/bin/env ruby
# $Id: tc_util.rb,v 1.19 2004/04/28 13:17:58 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/config'
require 'qwik/util'
require 'qwik/mail'
require 'qwik/diff'
require 'qwik/qp'

class TC_Config < Test::Unit::TestCase
  def test_config
    @config = Qwik::Config.new
    base = @config.base_dir
    assert_equal(base+"/bin", @config.bin_dir)
    assert_equal(false, @config.debug)
    @config = Qwik::Config.new(true)
    assert_equal(true, @config.debug)
  end
end

class TC_Util < Test::Unit::TestCase
  def test_pathname
    dir = "./test".path
    dir.rmtree if dir.exist?
    dir.rmdir  if dir.exist?
    dir.mkdir
    assert_equal(true, dir.exist?)
    file = dir+"t"
    file.open('wb'){|f|
      f.print "test"
    }
    assert_equal(true, file.exist?)
    dir.rmtree
    assert_equal(false, dir.exist?)
  end

  def test_encode
    assert_equal("a", "a")
    assert_equal("字", "字")
    assert_equal("字", "字".u8tosjis.sjistou8)
    assert_equal("字", "字".u8toeuc.euctou8)
    assert_equal("字", "字".u8tojis.jistou8)
    assert_equal("漢字", "\264\301\273\372".euctou8)
    assert_equal("\e$B4A;z\e(B", "漢字".u8tojis)
  end

  def test_escape
    assert_equal("A", "A".escape)
    assert_equal("+", " ".escape)
    assert_equal("%2B", "+".escape)
    assert_equal("%21", "!".escape)
    assert_equal("A", "%41".unescape)
    assert_equal(" ", "+".unescape)
    assert_equal("!", "%21".unescape)
    assert_equal("&lt;", "<".escapeHTML)
    assert_equal("<", "&lt;".unescapeHTML)
    assert_equal("&gt;", ">".escapeHTML)
    assert_equal(">", "&gt;".unescapeHTML)
    assert_equal("&amp;", "&".escapeHTML)
    assert_equal("&", "&amp;".unescapeHTML)
    assert_equal("ABC%82%A0%82%A2%82%A4+%2B%23", 'ABCあいう +#'.u8tosjis.escape)
    assert_equal('ABCあいう +#', "ABC%82%A0%82%A2%82%A4+%2B%23".unescape.sjistou8)
    assert_equal("&lt;a href=&quot;http://www.example.com/&quot;&gt;www.example.com&lt;/a&gt;",
		 '<a href="http://www.example.com/">www.example.com</a>'.escapeHTML)
    assert_equal('<a href="http://www.example.com/">www.example.com</a>',
		 "&lt;a href=&quot;http://www.example.com/&quot;&gt;www.example.com&lt;/a&gt;".unescapeHTML)
  end

  def test_hash
    assert_instance_of(String, "t".md5)
    assert_equal(16, "t".md5.length)
    assert_equal("\343X\357\244\211\365\200b\361\r\3271ked\236", "t".md5)

    assert_instance_of(String, "t".md5hex)
    assert_equal(32, "t".md5hex.length)
    assert_equal("e358efa489f58062f10dd7316b65649e", "t".md5hex)
  end

  def test_path
    assert_instance_of(Pathname, "t".path)
    assert_equal("t", "t".path.to_s)
  end

  def test_extname
    assert_equal("", "t".path.extname)
    assert_equal(".txt", "t.txt".path.extname)
    assert_equal(".gz", "t.tar.gz".path.extname)
  end

  def test_chdir
    pwd = Dir.pwd
    Dir.chdir('/') {
      assert_not_equal(pwd, Dir.pwd)
    }
    assert_equal(pwd, Dir.pwd)
  end

  def test_time
    t = Time.gm(2000, 1, 1, 12, 34, 56)
    assert_equal('2000-01-01', t.ymd)
    assert_equal('2000-01-01 12:34:56', t.ymdx)
    assert_equal('2000-01-01(Sat) 12:34:56', t.ymdax)
    assert_equal('2000-01-01 (土) 12:34:56', t.format_date)
    assert_equal('Sat, 01 Jan 2000 12:34:56 GMT', t.rfc1123_date)
  end
end

class TC_MailAddress < Test::Unit::TestCase
  def setup
    @m = Qwik::MailAddress.new('test@example.com')
  end
  def test_mail
    assert_equal('test@example.com', @m.to_s)
    assert_equal(true, @m.valid?)
    assert_equal(false, Qwik::MailAddress.new(nil).valid?)
    assert_equal(false, Qwik::MailAddress.new('invalid!@example.com').valid?)
    assert_equal(false, Qwik::MailAddress.new('invalid@example').valid?)
    assert_equal(false, Qwik::MailAddress.new('invalid').valid?)
    assert_equal(false, Qwik::MailAddress.new('').valid?)
    assert_equal('test@e...', @m.obfuscate)
    assert_equal('test@example.com', @m.normalize)
    assert_equal('test@example.com', Qwik::MailAddress.new('test@Example.com').normalize)
    m = Qwik::MailAddress.new('"test"@example.com')
    assert_equal('test@example.com', m.normalize)
    assert_equal('"test"@example.com', m.to_s)
    m.normalize!
    assert_equal('test@example.com', m.to_s)
  end
end

class TC_SendMail < Test::Unit::TestCase
  def setup
    @config = Qwik::Config.new(true)
    @config.test = true
    @sendmail = Qwik::Sendmail.new(@config)
#    @sendmail.localmode = true
  end

  def test_sendmail
    content = <<"EOM"
From: from@example.com
To: to@example.com
Subject: testsubject

test
EOM
    assert_equal(content, @sendmail.send('from@example.com', 'to@example.com', 'testsubject', 'test'))
  end

  def test_sendmail_encode
    assert_equal('t', @sendmail.encode_field('t'))
    assert_equal("=?iso-2022-jp?B?GyRCJCIbKEI=?=", @sendmail.encode_field('あ'))
    assert_equal("=?iso-2022-jp?B?GyRCJCIkJBsoQg==?=", @sendmail.encode_field('あい'))
    assert_equal("=?iso-2022-jp?B?GyRCJCIkJCQmJCgkKiQrJC0kLyQxJDMbKEI=?=\n =?iso-2022-jp?B?GyRCJDUkNyQ5JDskPRsoQg==?=", @sendmail.encode_field('あいうえおかきくけこさしすせそ'))
  end
end

class TC_QP < Test::Unit::TestCase
  def test_qp()
    assert_match(/testcase.rb:\d\d:_*send_*/, QP.caller_msg(caller(1)))
    assert_equal('[1, "a"]', QP.ar_inspect([1, 'a']))
    assert_equal('0.0', QP.runtime())
    assert_match(/0.\d:testcase.rb:\d\d:_*send_*	\[1, "a"\]/, QP.qt(caller(1), 1, 'a'))
  end
end

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