File: DictionaryInfoTest.py

package info (click to toggle)
libvoikko 3.0-1
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 2,904 kB
  • ctags: 2,181
  • sloc: cpp: 11,324; sh: 10,695; python: 1,811; xml: 840; makefile: 268; ansic: 175
file content (94 lines) | stat: -rw-r--r-- 3,695 bytes parent folder | download
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
# -*- 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.assertEqual(1, len(languages))
		self.assertEqual(u"fi", languages[0])
	
	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))


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