File: test_context.py

package info (click to toggle)
tomoe 0.6.0-1.3
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 51,396 kB
  • sloc: xml: 1,387,526; ansic: 11,515; sh: 9,072; ruby: 1,344; python: 762; makefile: 450
file content (89 lines) | stat: -rw-r--r-- 2,698 bytes parent folder | download | duplicates (2)
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
# encoding: utf-8
import os
import sys
import shutil
import glob
import unittest
import test_common
import tomoe

class TomoeContextTest(unittest.TestCase):

    def setUp(self):
        test_dict_name = os.path.join(test_common.test_data_dir, 'test-dict.xml')
        self.dict_name = "tomoe-test-xmldict.xml"
        shutil.copy(test_dict_name, self.dict_name)
 
        self.config_filename = "test-config"
        config_file = open(self.config_filename, "w")
        contents = """
[config]
use-system-dictionaries = false
user-dictionary = test
languages = ja;zh_CN

[test-dictionary]
type = xml
file = %s
         """ % (self.dict_name)
        config_file.write(contents)
        config_file.close()
        
        self.context = tomoe.Context()
        self.context.load_config(self.config_filename)

    def tearDown(self):
        if os.access(self.config_filename, os.F_OK):
            os.unlink(self.config_filename)

        if os.access(self.dict_name, os.F_OK):
            os.unlink(self.dict_name)

    def testSearchByStrokes(self):
        char_code = '池'
        stroke_file = os.path.join(test_common.test_data_dir, 'ike.data')

        results, writing = test_common.parseStrokeData(stroke_file)

        tomoe_query = tomoe.Query()
        tomoe_query.set_writing(writing)

        candidates = self.context.search(tomoe_query)
        self.assertEqual(char_code, candidates[0].get_char().get_utf8())

    def testSearchByQuery(self):
        char_code = '池'
        reading = 'チ'
        tomoe_reading = tomoe.Reading(tomoe.READING_UNKNOWN, reading)
        tomoe_query = tomoe.Query()
        tomoe_query.add_reading(tomoe_reading)

        candidates = self.context.search(tomoe_query)
        self.assertEqual(char_code, candidates[0].get_char().get_utf8())

    def testGetChar(self):
        char_code = '池'
        tomoe_char = self.context.get_char(char_code)
        self.assertEqual(char_code, tomoe_char.get_utf8())

    def testRegister(self):
        char_code = '地'
        tomoe_char = self.context.get_char(char_code)
        self.assertEqual(None, tomoe_char)

        tomoe_char = tomoe.Char(char_code)
        self.assert_(self.context.register(tomoe_char))

        tomoe_char = self.context.get_char(char_code)
        self.assertNotEqual(None, tomoe_char)
        self.assertEqual(char_code, tomoe_char.get_utf8())

    def testUnregister(self):
        char_code = '池'
        tomoe_char = self.context.get_char(char_code)
        self.assertEqual(char_code, tomoe_char.get_utf8())

        self.assert_(self.context.unregister(char_code))
        self.assertEqual(None, self.context.get_char(char_code))

# vi:ts=4:nowrap:ai:expandtab