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

$LOAD_PATH.unshift('../../lib')
$LOAD_PATH.unshift('../../compat')
require 'test/unit'
require 'qwik/page'

module Qwik
  class Page
    def test(t)
      # test_first_line
      store("line1");         t.assert_equal("line1", first_line)
      store("l1\nl2\n");      t.assert_equal("l1", first_line)
      store("\nline2\n");     t.assert_equal("", first_line)
      store("# c\nl1\nl2\n"); t.assert_equal("l1", first_line)
    end
  end
end

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

  def test_page
    assert_instance_of(Pathname, @pages.path(""))
    assert_equal(@dir, @pages.path(""))
    assert_equal(@dir+"1", @pages.path("1"))
    assert_equal(false, @pages.exist?("1"))
    assert_equal(nil, @pages['1'])

    page = @pages.create("1")
    assert_instance_of(Qwik::Page, page)
    assert_equal(true, @pages.exist?("1"))
    assert_equal("", page.load)
    page.store('t')
    assert_raises(Qwik::PageExistError){ @pages.create("1") }

    page.store("test1")
    assert_equal("test1", page.load)

    page = @pages["1"]
    assert_instance_of(Qwik::Page, page)
    assert_equal("test1", page.load)
    page.store("test2")
    assert_equal("test2", page.load)

    # test_first_line
    page.test(self)

    # test_url
    assert_equal('1.html', page.url)

    # test_title
    page.store("*t");	assert_equal("t", page.get_title)
    page.store("*t ");	assert_equal("t", page.get_title)
    page.store("* t");	assert_equal("t", page.get_title)
    page.store("* t ");	assert_equal("t", page.get_title)
    page.store("*a b");	assert_equal("a b", page.get_title)
    page.store("* t");	assert_equal("t", page.get_title)
    page.store("* a b"); assert_equal("a b", page.get_title)
    page.store("*- t");	assert_equal("- t", page.get_title)
    page.store("*漢字");	assert_equal("漢字", page.get_title)
    page.store("*表面");	assert_equal("表面", page.get_title)
    page.store("");	assert_equal("1", page.get_title)
    page.append("*t");	assert_equal("t", page.get_title)
    page.store("**t");	assert_equal("1", page.get_title)
    page.store("* *t");	assert_equal("*t", page.get_title)
    page.store("-t");	assert_equal("1", page.get_title)
    page.store("*あ");	assert_equal("あ", page.get_title)
    page.store("* コ");	assert_equal("コ", page.get_title)
    page.store("* コ\n{{mail(test@example.com)\nあ\n\n}}\n")
    assert_equal("コ", page.get_title)

    # test_key
    assert_equal('1', page.key)

    page2 = @pages.create('あ') # test_key_kanji
    assert_equal('あ', page2.key)
    assert_equal(true, @pages.exist?('あ'))
    @pages.delete('あ')
    assert_equal(false, @pages.exist?('あ'))

    page2 = @pages.create('1+2') # test_key_with_plus
    assert_equal('1+2', page2.key)
    assert_equal(true, @pages.exist?('1+2'))

    @pages.each {|page|
      assert_instance_of(Qwik::Page, page)
      assert_instance_of(Qwik::PageKey, page.key)
    }

    # test_mtime
    assert_instance_of(Time, page.mtime)

    # test_append
    page.store("t1")
    page.append("t2")
    assert_equal("t1t2", page.load)

    # test_substitute
    page.store('t')
    str = page.load
    assert_equal('t', str)
    str.sub!(/t/, 's')
    assert_equal('s', str)
    assert_equal('t', page.load)

    # test_delete
    @pages.delete('1')
    assert_equal(false, @pages.exist?('1'))

    # test_store_md5
    page.store("t1")
    assert_raises(Qwik::PageCollisionError){ page.store("t1", '0') }
    md5 = page.load.md5hex
    page.store('t2', md5)
    assert_equal('t2', page.load)
    md5 = page.load.md5hex
    page.append('t3', md5)
    assert_equal('t2t3', page.load)

    # test_super_pages
    assert_equal(true, @pages.exist?('TitleList'))
    page = @pages['TitleList']
    assert_equal('TitleList', page.get_title)
    assert_equal('SearchPage', @pages['SearchPage'].get_title)

    assert_equal(true, @pages.exist?('SideMenu')) # exist
    assert_equal(false, @pages.baseexist?('SideMenu')) # non exist
    page = @pages.create('SideMenu') # can create
    assert_equal(true, @pages.baseexist?('SideMenu')) # not yet
    page.store('t')
    assert_equal(true, @pages.baseexist?('SideMenu')) # become true here
  end

  def test_delete
    page = @pages.create("1")
    page.store("* a")
    assert_instance_of(Time, page.mtime)
    page.delete
    assert_instance_of(Time, page.mtime)
  end

  def test_get_new_id
    page = @pages.create("0");	page.store('t')
    assert_equal('1', @pages.get_new_id())
    page = @pages.create(@pages.get_new_id());	page.store('t')
    assert_equal(true, @pages.exist?(1))
    assert_equal('2', @pages.get_new_id())
    page = @pages.create("t");	page.store('t')
    assert_equal('2', @pages.get_new_id())
    page = @pages.create("03+1234+5678");	page.store('t')
    assert_equal('2', @pages.get_new_id())
    page = @pages.create("4");	page.store('t')
    assert_equal('5', @pages.get_new_id())
  end

  def test_backup
    page = @pages.create("0")
    page.store('t')
    assert_equal('t', str = page.load)
    assert_instance_of(Pathname, @pages.backup_path)
    @pages.backup_path.each_entry {|f|
      next if f.directory?
      pa = @pages.backup_path+f
      str2 = pa.open {|ff| ff.read }
      assert_equal(str, str2)
    }
  end

  def test_list
    @pages.create("t1").store('')
    @pages.create("t2").store('')
    @pages.create("1").store('t3')
    @pages.create("2").store('t4')

    @pages.each {|page|
      assert_instance_of(Qwik::Page, page)
    }

    tlist = @pages.title_list
    assert_equal("1", tlist[0].key)
    assert_equal("2", tlist[1].key)
    assert_equal("t1", tlist[2].key)
    assert_equal("t2", tlist[3].key)
    tlist.each {|page|
      assert_instance_of(String, page.get_title)
    }
  end

  def test_enumerable
    #test_find_title
    @pages.create_new.store('* あ')
    @pages.create_new.store('* い')
    @pages.create_new.store('* う')

    page = @pages.find_title('い')
    assert_equal("2", page.key)
    assert_equal(["1", "2", "3"], @pages.keys)
  end

  def test_recent_list
    # Since this test takes long time, ignore this if under test suite.
    return if $0 != __FILE__
    @pages.create("t1").store('t');	sleep 1
    @pages.create("t2").store('t');	sleep 1
    @pages.create("1").store('t3');	sleep 1
    @pages.create("2").store('t4')
    dlist = @pages.date_list
    assert_equal("t1", dlist[0].key)
    assert_equal("t2", dlist[1].key)
    assert_equal("1",  dlist[2].key)
    assert_equal("2",  dlist[3].key)
    dlist.each {|page|
      assert_instance_of(Time, page.mtime)
    }
  end

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