aboutsummaryrefslogtreecommitdiff
path: root/test/test_tools/test_himo.py
blob: cb58ca5b24506d97d311af11b19129176f64b876 (plain)
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
import sys
import os
import unittest
path = os.path.realpath(os.path.join(os.path.dirname(__file__), '..', '..'))
sys.path.append(path)
from dodai.tools.himo import String2Himo
from dodai.tools.himo import Himo
from dodai.tools.himo import HimoAsciiError
from decimal import Decimal as D

class TestString2Himo(unittest.TestCase):

    def setUp(self):
        self.string_to_himo_one = String2Himo()
        self.string_to_himo_two = String2Himo('unicode_escape')

    def test_regular_string_one(self):
        obj = self.string_to_himo_one(str('abcd'))
        self.assertTrue(isinstance(obj, Himo))

    def test_regular_string_two(self):
        test = str('abcd')
        obj = self.string_to_himo_one(test)
        self.assertTrue(obj == test)

    def test_regular_string_three(self):
        obj = self.string_to_himo_two(str('abcd'))
        self.assertTrue(isinstance(obj, Himo))

    def test_regular_string_four(self):
        test = str('abcd')
        obj = self.string_to_himo_two(test)
        self.assertTrue(obj == test)

    def test_regular_string_five(self):
        test = 1
        obj = self.string_to_himo_two(test)
        self.assertEqual(int(obj), test)

    def test_decode_html(self):
        test = u'\u4e5d\xf2\xe5\xe9'
        obj = self.string_to_himo_one(u'九òåé')
        self.assertEqual(test, obj)

    def test_decode_chardet(self):
        # Korean Text in the EUC-KR character set
        kor = '\xb4\xe7\xbd\xc5\xc0\xcc \xc3\xa3\xb4\xc2 \xb8\xf0\xb5\xe7 '\
               '\xbd\xba\xc5\xb8\xc0\xcf, \xbf\xc1\xbc\xc7'
        test = '\ub2f9\uc2e0\uc774 \ucc3e\ub294 \ubaa8\ub4e0 '\
               '\uc2a4\ud0c0\uc77c, \uc625\uc158'.decode('unicode_escape')
        obj = self.string_to_himo_one(kor)
        self.assertEqual(obj, test)

    def test_html_decode_one(self):
        # Korean Text
        kor = '당신이 찾는 모든 '\
              '스타일, 옥션& foo'
        test = u'\ub2f9\uc2e0\uc774 \ucc3e\ub294 \ubaa8\ub4e0 '\
               u'\uc2a4\ud0c0\uc77c, \uc625\uc158& foo'
        obj = self.string_to_himo_one(kor)
        self.assertEqual(obj, test)

    def test_html_decode_two(self):
        kor = '&ampppe;'
        obj = self.string_to_himo_one(kor)
        self.assertEqual(obj, kor)


class TestHimo(unittest.TestCase):

    def setUp(self):
        # "Elephants are our brothers" in Japanese.  This line comes from
        # an episode of a old Japanese TV show called "Koko Ga Hen Da Yo,
        # Nihonjin" http://en.wikipedia.org/wiki/Koko_ga_hen_da_yo,_nihonjin
        # This line came from an African native who spoke Japanese fluently
        self.jp = u'\u8c61\u306f\u79c1\u305f\u3061\u306e\u5144\u5f1f'\
                  u'\u3067\u3059'
        self.jp_html = u'象は私たちの'\
                       u'兄弟です'

    def test_html(self):
        obj = Himo(self.jp)
        test = obj.html()
        self.assertEqual(test, self.jp_html)

    def test_decimal(self):
        obj = Himo('23.55')
        obj = obj.decimal()
        self.assertTrue(isinstance(obj, D))
        self.assertEqual(obj, D('23.55'))

    def test_ascii_one(self):
        # "Elephants live in Africa" in spanish
        obj = Himo(u'Los elefantes viven en \xc1frica')
        test = u'Los elefantes viven en Africa'
        self.assertEqual(obj.ascii(), test)

    def test_ascii_two(self):
        # "Elephants are our brothers" in French
        obj = Himo(u'Les \xe9l\xe9phants sont nos fr\xe8res \xa9')
        test = u'Les elephants sont nos freres (C)'
        self.assertEqual(obj.ascii(), test)

    def test_ascii_three(self):

        obj = Himo(self.jp)
        self.failUnlessRaises(HimoAsciiError, obj.ascii)