File: test_util.rb

package info (click to toggle)
hiki 1.0.0-2.1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye
  • size: 1,812 kB
  • sloc: ruby: 14,572; lisp: 926; sh: 19; makefile: 16
file content (128 lines) | stat: -rw-r--r-- 5,233 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# -*- coding: utf-8 -*-
# $Id: test_util.rb,v 1.4 2006-05-29 13:39:10 fdiary Exp $

$:.unshift(File.join(File.dirname(__FILE__), '../hiki'))

require 'test/unit'
require 'hiki/util'

class TestUtil < Test::Unit::TestCase
  include Hiki::Util

  def setup
    @t1 = "123\n456\n"
    @t2 = "123\nabc\n456\n"
    @t3 = "123\n456\ndef\n"
    @t4 = "こんにちは、私の名前はわたなべです。\n私はJust Another Ruby Porterです。"
    @t5 = "こんばんは、私の名前はまつもとです。\nRubyを作ったのは私です。私はRuby Hackerです。"
    @conf = Object.new
  end

  def test_word_diff_html
    assert_equal( "123\n<ins class=\"added\">abc</ins>\n456\n", word_diff( @t1, @t2 ) )
    assert_equal( "<del class=\"deleted\">こんにちは</del><ins class=\"added\">こんばんは</ins>、私の<del class=\"deleted\">名前はわたなべです</del><ins class=\"added\">名前はまつもとです</ins>。\n<ins class=\"added\">Rubyを作ったのは私です。</ins>私は<del class=\"deleted\">Just Another </del>Ruby <del class=\"deleted\">Porter</del><ins class=\"added\">Hacker</ins>です。", word_diff( @t4, @t5) )
  end

  def test_word_diff_text
    assert_equal( "123\n{+abc+}\n456\n", word_diff_text( @t1, @t2 ) )
    assert_equal( "[-こんにちは-]{+こんばんは+}、私の[-名前はわたなべです-]{+名前はまつもとです+}。\n{+Rubyを作ったのは私です。+}私は[-Just Another -]Ruby [-Porter-]{+Hacker+}です。", word_diff_text( @t4, @t5 ) )
  end

  def test_unified_diff
    assert_equal( "@@ -1,2 +1,3 @@\n 123\n+abc\n 456\n", unified_diff( @t1, @t2 ) )
    assert_equal( "@@ -1,3 +1,2 @@\n 123\n-abc\n 456\n", unified_diff( @t2, @t1 ) )
  end

  def test_euc_to_utf8
    omit("do not use this method with Ruby1.9") if Object.const_defined?(:Encoding)
    hoge_euc = "\xA4\xDB\xA4\xB2"
    hoge_utf8 = "ほげ"
    fullwidth_wave_euc = "\xA1\xC1"
    fullwidth_wave_utf8 = "〜"
    assert_equal(hoge_utf8, euc_to_utf8(hoge_euc))
    assert_equal(fullwidth_wave_utf8, euc_to_utf8(fullwidth_wave_euc))
  end

  def test_utf8_to_euc
    omit("do not use this method with Ruby1.9") if Object.const_defined?(:Encoding)
    hoge_euc = "\xA4\xDB\xA4\xB2"
    hoge_utf8 = "ほげ"
    fullwidth_wave_euc = "\xA1\xC1"
    fullwidth_wave_utf8 = "〜"
    assert_equal(hoge_euc, utf8_to_euc(hoge_utf8))
    assert_equal(fullwidth_wave_euc, utf8_to_euc(fullwidth_wave_utf8))
  end

  def test_plugin_error
    error = Object.new
    mock(error).class.returns("Hiki::PluginError")
    mock(error).message.returns("Plugin Error")
    mock(@conf).plugin_debug.returns(false)
    assert_equal("<strong>Hiki::PluginError (Plugin Error): do_something</strong><br>",
                 plugin_error("do_something", error))
  end

  def test_plugin_error_with_debug
    error = Object.new
    mock(error).class.returns("Hiki::PluginError")
    mock(error).message.returns("Plugin Error")
    mock(error).backtrace.returns(["backtrace1", "backtrace2", "backtrace3"])
    mock(@conf).plugin_debug.returns(true)
    assert_equal(<<STR.chomp, plugin_error("do_something", error))
<strong>Hiki::PluginError (Plugin Error): do_something</strong><br><strong>backtrace1<br>
backtrace2<br>
backtrace3</strong>
STR
  end

  def test_cmdstr
    assert_equal("?c=hoge;fuga", cmdstr("hoge", "fuga"))
  end

  def test_title
    mock(@conf).site_name.returns("<TestSite>")
    assert_equal("&lt;TestSite&gt; - FrontPage", title("FrontPage"))
  end

  def test_view_title
    mock(@conf).cgi_name.returns("hiki.cgi")
    assert_equal(%Q!<a href="hiki.cgi?c=search;key=FrontPage">FrontPage</a>!, view_title("FrontPage"))
  end

  def test_format_date
    mock(@conf).msg_time_format.returns("%Y-%m-%d #DAY# %H:%M:%S")
    mock(@conf).msg_day.returns(%w(日 月 火 水 木 金 土))
    assert_equal("2011-01-01 (土) 01:02:03", format_date(Time.mktime(2011, 1, 1, 1, 2, 3)))
  end

  def test_escape
    if Object.const_defined?(:Encoding)
      expected = [
                  "%E3%81%82%E3%81%84%E3%81%86%E3%81%88%E3%81%8A",
                  "%E3%83%95%E3%83%AD%E3%83%B3%E3%83%88%E3%83%9A%E3%83%BC%E3%82%B8",
                  "%A4%A2%A4%A4%A4%A6%A4%A8%A4%AA",
                  "%A5%D5%A5%ED%A5%F3%A5%C8%A5%DA%A1%BC%A5%B8",
                  "%82%A0%82%A2%82%A4%82%A6%82%A8",
                  "%83%74%83%8D%83%93%83%67%83%79%81%5B%83%57",
                 ]
    else
      expected = [
                  "%E3%81%82%E3%81%84%E3%81%86%E3%81%88%E3%81%8A",
                  "%E3%83%95%E3%83%AD%E3%83%B3%E3%83%88%E3%83%9A%E3%83%BC%E3%82%B8",
                  "%A4%A2%A4%A4%A4%A6%A4%A8%A4%AA",
                  "%A5%D5%A5%ED%A5%F3%A5%C8%A5%DA%A1%BC%A5%B8",
                  "%82%A0%82%A2%82%A4%82%A6%82%A8",
                  "%83t%83%8D%83%93%83g%83y%81%5B%83W",
                 ]
    end
    actual = [
              "あいうえお",
              "フロントページ",
              NKF.nkf("-m0 -We", "あいうえお"),
              NKF.nkf("-m0 -We", "フロントページ"),
              NKF.nkf("-m0 -Ws", "あいうえお"),
              NKF.nkf("-m0 -Ws", "フロントページ"),
             ]
    assert_equal(expected, actual.map{|v| escape(v) })
  end
end