File: DictionaryInfoTest.py

package info (click to toggle)
libvoikko 4.3.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,812 kB
  • sloc: cpp: 12,641; sh: 5,047; python: 2,075; java: 1,256; cs: 1,172; lisp: 489; makefile: 327; ansic: 184; xml: 106
file content (114 lines) | stat: -rw-r--r-- 4,407 bytes parent folder | download | duplicates (6)
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
# -*- coding: utf-8 -*-

# Copyright 2010 Harri Pitkänen (hatapitk@iki.fi)
# Test suite for functions that return information about available dictionaries.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

import unittest
import libvoikko
from TestUtils import MorphologyInfo, TestDataDir, getVoikkoCLibrary
from ctypes import c_int, c_char_p, byref

VARIANT_NAME = u"dictionaryinfotest"

class DictionaryInfoTest(unittest.TestCase):
	def setUp(self):
		info = MorphologyInfo()
		info.variant = VARIANT_NAME
		info.morphology = u"null"
		self.dataDir = TestDataDir()
		self.dataDir.createMorphology(VARIANT_NAME, info)
	
	def tearDown(self):
		self.dataDir.tearDown()
	
	def testListSupportedSpellingLanguagesReturnsFinnish(self):
		languages = libvoikko.Voikko.listSupportedSpellingLanguages(self.dataDir.getDirectory())
		self.assertTrue(u"fi" in languages)
	
	def __tryOpenWithOldApi(self, variant):
		lib = getVoikkoCLibrary()
		handle = c_int(-1)
		error = lib.voikko_init_with_path(byref(handle), variant,
		        0, self.dataDir.getDirectory())
		isOk = not bool(error)
		lib.voikko_terminate(handle)
		return isOk
	
	def __tryOpenWithNewApi(self, variant):
		lib = getVoikkoCLibrary()
		error = c_char_p()
		handle = lib.voikkoInit(byref(error), variant,
		         self.dataDir.getDirectory())
		if error.value == None:
			lib.voikkoTerminate(handle)
			return True
		else:
			return False
	
	def testVariantCanBeOpenedWithOldApiUsingFullNameAsIs(self):
		self.assertTrue(self.__tryOpenWithOldApi(VARIANT_NAME))
	
	def testDefaultVariantCanBeOpenedWithOldApiUsingValuesSpecifiedInApi(self):
		self.assertTrue(self.__tryOpenWithOldApi(u""))
		self.assertTrue(self.__tryOpenWithOldApi(u"default"))
		self.assertTrue(self.__tryOpenWithOldApi(u"fi_FI"))
	
	def testVariantCannotBeOpenedWithOldApiUsingOtherValues(self):
		self.assertFalse(self.__tryOpenWithOldApi(u"ksajdlaksjdkasl"))
	
	def testVariantCanBeOpenedWithNewApiUsingBCP47LanguageTag(self):
		self.assertTrue(self.__tryOpenWithNewApi(u"fi-x-dictiona-ryinfote-st"))
		self.assertTrue(self.__tryOpenWithNewApi(u"fi-x-Dictiona-ryinfote-st"))
		self.assertTrue(self.__tryOpenWithNewApi(u"Fi-x-dictiona-ryinfote-st"))
		self.assertTrue(self.__tryOpenWithNewApi(u"fi-x-dictio-nary-info-test"))
		self.assertTrue(self.__tryOpenWithNewApi(u"fi-FI-x-dictiona-ryinfote-st"))
		self.assertTrue(self.__tryOpenWithNewApi(u"fi"))
		self.assertTrue(self.__tryOpenWithNewApi(u"fi-FI"))
	
	def testVariantCannotBeOpenedWithNewApiUsingOtherValues(self):
		self.assertFalse(self.__tryOpenWithNewApi(VARIANT_NAME))
		self.assertFalse(self.__tryOpenWithNewApi(u"ksajdlaksjdkasl"))
		self.assertFalse(self.__tryOpenWithNewApi(u"sv"))
		self.assertFalse(self.__tryOpenWithNewApi(u"fi-x-askldjaslkdj"))
		self.assertFalse(self.__tryOpenWithNewApi(u""))
		self.assertFalse(self.__tryOpenWithNewApi(None))
	
	def testOtherLanguagesCanBeUsed(self):
		info2 = MorphologyInfo()
		info2.language = u"dk"
		info2.morphology = u"null"
		info2.description = "Testdescription lkrj"
		self.dataDir.createMorphology(u"test1", info2)
		
		self.assertTrue(self.__tryOpenWithNewApi(u"dk"))
		
		languages = libvoikko.Voikko.listSupportedSpellingLanguages(self.dataDir.getDirectory())
		self.assertTrue(u"fi" in languages)
		self.assertTrue(u"dk" in languages)
		
		dicts = libvoikko.Voikko.listDicts(self.dataDir.getDirectory())
		for dictionary in dicts:
			if dictionary.language == u"dk":
				self.assertEquals(info2.description, dictionary.description)
				self.assertEquals(u"standard", dictionary.variant)
				return
		self.fail(u"Should have found dk dictionary")


if __name__ == "__main__":
	suite = unittest.TestLoader().loadTestsFromTestCase(DictionaryInfoTest)
	unittest.TextTestRunner(verbosity=1).run(suite)