Enchant: tests/enchant_providers/unittest_enchant_providers.h Source File

Enchant Generic spell checking library

unittest_enchant_providers.h
1 /* Copyright (c) 2008 Eric Scott Albright
2 *
3 * Permission is hereby granted, free of charge, to any person obtaining a copy
4 * of this software and associated documentation files (the "Software"), to deal
5 * in the Software without restriction, including without limitation the rights
6 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7 * copies of the Software, and to permit persons to whom the Software is
8 * furnished to do so, subject to the following conditions:
9 *
10 * The above copyright notice and this permission notice shall be included in
11 * all copies or substantial portions of the Software.
12 *
13 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19 * THE SOFTWARE.
20 */
21
22 #include "enchant.h"
23 #include "enchant-provider.h"
24 #include <stdlib.h>
25 #include <glib.h>
26
27 EnchantProvider* GetProviderForTests();
28 char* GetErrorMessage(EnchantProvider* provider);
29
30 struct Provider_TestFixture
31 {
32 EnchantProvider* _provider;
33
34 //Setup
35 Provider_TestFixture()
36 {
37 _provider = GetProviderForTests();
38 }
39
40 std::string Convert(const std::wstring & ws)
41 {
42 gchar* str;
43 switch (sizeof(wchar_t)) {
44 case 2:
45 str = g_utf16_to_utf8((gunichar2*)ws.c_str(), (glong)ws.length(), NULL, NULL, NULL);
46 break;
47 case 4:
48 str = g_ucs4_to_utf8((gunichar*)ws.c_str(), (glong)ws.length(), NULL, NULL, NULL);
49 break;
50 default:
51 abort();
52 }
53 std::string s(str);
54 g_free(str);
55 return s;
56 }
57
58 std::wstring Convert(const std::string & s)
59 {
60 gunichar2* str = g_utf8_to_utf16(s.c_str(), (glong)s.length(), NULL, NULL, NULL);
61 std::wstring ws((wchar_t*)str);
62 g_free(str);
63 return ws;
64 }
65
66 EnchantDict* GetDefaultDictionary()
67 {
68 EnchantDict* dict=NULL;
69
70 // Try getting dictionary for user's default language
71 char *lang = enchant_get_user_language();
72 dict = (*_provider->request_dict) (_provider, lang);
73 g_free (lang);
74
75 // If not available, get the first dictionary listed as being available
76 if (!dict && _provider->list_dicts)
77 {
78 size_t n_dicts;
79
80 char ** dicts = (*_provider->list_dicts) (_provider, &n_dicts);
81 if (n_dicts > 0)
82 dict = (*_provider->request_dict) (_provider, dicts[0]);
83 g_strfreev (dicts);
84 }
85 return dict;
86 }
87
88 EnchantDict* GetDictionary(const char* language)
89 {
90 return (*_provider->request_dict) (_provider, language);
91 }
92
93 virtual void ReleaseDictionary(EnchantDict* dict)
94 {
95 if (dict)
96 _provider->dispose_dict(_provider, dict);
97 }
98 };
Provider_TestFixture
Definition: unittest_enchant_providers.h:31