#!/usr/bin/env ruby
# Copyright (C) 2003-2005 Kouichirou Eto, All rights reserved.

$LOAD_PATH << "../../lib" unless $LOAD_PATH.include?("../../lib")
require "qwik/test-module-quickml"
require "qwik/quickml"

module QuickML
  class QuickML
    def members_file_read
      open(@members_file){|f| f.read }
    end
  end

  class Logger
    undef puts_log
    def puts_log(msg) # for test
      return if msg =~ /New ML/
      time = Time.now.strftime("%Y-%m-%dT%H:%M:%S")
      open("testlog.txt", "ab"){|f|
	f.puts "#{time}: #{msg}"
      }
    end
  end
end

class TC_QuickML < Test::Unit::TestCase
  include TestQuickMLModule

  def test_quickml_private_methods
    qml = QuickML::QuickML.new(@ml_config, "test@example.com")

    # Make private methods to public.
    methods = %w(
obfuscate_address
qwik_url
generate_footer
last_article_time
save_member_file
)
    assert_equal(true, qml.private_methods.include?("obfuscate_address"))
    assert_equal(false, qml.public_methods.include?("obfuscate_address"))
    methods.each {|m|
      t_make_public(QuickML::QuickML, m)
    }
    assert_equal(false, qml.private_methods.include?("obfuscate_address"))
    assert_equal(true, qml.public_methods.include?("obfuscate_address"))

    # Make instance variables to readable.
    vars = %w(
members_file
count_file
)
    vars.each {|v|
      t_make_readable(QuickML::QuickML, v)
    }

    # test private method
    assert_equal("test@e...", qml.obfuscate_address("test@example.com"))
    assert_equal("http://qwik.jp/test/", qml.qwik_url)
    assert_equal("
-- 
archive-> http://qwik.jp/test/ 
ML-> test@example.com
", qml.generate_footer)
    assert_instance_of(Time, qml.last_article_time)

    # test member file
    assert_equal("./test/,members", qml.members_file)
    assert_equal("./test/,count", qml.count_file)
    assert_equal(false, File.exist?(qml.members_file))

    qml.save_member_file
    assert_equal(true, File.exist?(qml.members_file))
    assert_equal("", qml.members_file_read)

    qml.add_member("user@e.com")
    str = qml.members_file_read
    assert_equal("user@e.com\n", str)

    qml.add_error_member("user@e.com")
    str = qml.members_file_read
    assert_match(/\Auser@e.com\n; user@e.com 1 /, str)

    assert_equal(true, FileTest.exist?(qml.members_file))
    qml.remove_member("user@e.com")
    #assert_equal(false, FileTest.exist?(qml.members_file)) # why?
  end
end
