Enchant Generic spell checking library

EnchantBrokerTestFixture.h
1 /* Copyright (c) 2007 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 #ifndef __ENCHANTBROKERTESTFIXTURE
23 #define __ENCHANTBROKERTESTFIXTURE
24
25 #include "EnchantTestFixture.h"
26 #include "mock_provider.h"
27 #include <stack>
28 #include <stdio.h>
29 #include <gmodule.h>
30 #include <assert.h>
31
33 // Mock provider functions
34 static void
35 MockProviderDispose(EnchantProvider *me)
36 {
37 g_free(me);
38 }
39
40 static int
41 MockEnGbAndQaaProviderDictionaryExists (EnchantProvider *,
42 const char *const tag)
43 {
44 return (strcmp(tag, "en_GB")==0 || strcmp(tag, "qaa") ==0);
45 }
46
47
48 static EnchantDict*
49 MockEnGbAndQaaProviderRequestDictionary(EnchantProvider * me, const char *tag)
50 {
51 EnchantDict *dict = NULL;
52
53 if(MockEnGbAndQaaProviderDictionaryExists(me, tag)){
54 dict = g_new0 (EnchantDict, 1);
55 }
56 return dict;
57 }
58
59 static void
60 MockProviderDisposeDictionary (EnchantProvider *, EnchantDict * dict)
61 {
62 g_free(dict);
63 }
64
65 static const char *
66 MockProviderIdentify (EnchantProvider *)
67 {
68 return "mock";
69 }
70
71 static const char *
72 MockProviderDescribe (EnchantProvider *)
73 {
74 return "Mock Provider";
75 }
76
77 static char **
78 MockEnGbAndQaaProviderListDictionaries (EnchantProvider *,
79 size_t * out_n_dicts)
80 {
81 *out_n_dicts = 2;
82 char** out_list = g_new0 (char *, *out_n_dicts + 1);
83 out_list[0] = g_strdup ("en_GB");
84 out_list[1] = g_strdup ("qaa");
85
86 return out_list;
87 }
88
89 static char **
90 MockEnGbProviderListDictionaries (EnchantProvider *,
91 size_t * out_n_dicts)
92 {
93 *out_n_dicts = 1;
94 char** out_list = g_new0 (char *, *out_n_dicts + 1);
95 out_list[0] = g_strdup ("en_GB");
96
97 return out_list;
98 }
99
100 typedef void (*SET_CONFIGURE)(ConfigureHook);
101
102 struct EnchantBrokerTestFixture : EnchantTestFixture
103 {
104 EnchantBroker* _broker;
105 GModule *hModule, *hModule2;
106
107 //Setup
108 EnchantBrokerTestFixture(ConfigureHook userConfiguration=NULL,
109 ConfigureHook user2Configuration=NULL,
110 bool includeNullProviders = false)
111 {
112 userMockProviderConfiguration = userConfiguration;
113 userMockProvider2Configuration = user2Configuration;
114
115 CopyProvider("enchant_mock_provider", "enchant_mock_provider");
116 hModule = g_module_open(LIBDIR_SUBDIR "/enchant-" ENCHANT_MAJOR_VERSION "/enchant_mock_provider", (GModuleFlags) 0);
117 if(hModule!=NULL){
118 SET_CONFIGURE sc;
119 assert(g_module_symbol(hModule, "set_configure", (gpointer *)&sc));
120 (sc)(ConfigureMockProvider);
121 }
122
123 hModule2 = NULL;
124 if(user2Configuration != NULL){
125 CopyProvider("enchant_mock_provider2", "enchant_mock_provider2");
126 hModule2 = g_module_open(LIBDIR_SUBDIR "/enchant-" ENCHANT_MAJOR_VERSION "/enchant_mock_provider2", (GModuleFlags) 0);
127 if(hModule2!=NULL){
128 SET_CONFIGURE sc;
129 assert(g_module_symbol(hModule2, "set_configure", (gpointer *)&sc));
130 (sc)(ConfigureMockProvider2);
131 }
132 }
133
134 if(includeNullProviders){
135 CopyProvider("enchant_null_provider", "null_provider");
136 CopyProvider("enchant_null_identify", "null_identify");
137 CopyProvider("enchant_null_describe", "null_describe");
138 CopyProvider("enchant-" ENCHANT_MAJOR_VERSION, "enchant-" ENCHANT_MAJOR_VERSION); //not a provider
139 }
140
141 InitializeBroker();
142 }
143
144 //Teardown
145 ~EnchantBrokerTestFixture(){
146 if(hModule!=NULL){
147 g_module_close(hModule);
148 }
149 if(hModule2!=NULL){
150 g_module_close(hModule2);
151 }
152
153 if(_broker){
154 enchant_broker_free (_broker);
155 }
156 while(!pwlFilenames.empty())
157 {
158 DeleteFile(pwlFilenames.top());
159 pwlFilenames.pop();
160 }
161 }
162
163 void InitializeBroker()
164 {
165 _broker = enchant_broker_init ();
166 }
167
168 void CopyProvider(const std::string& sourceProviderName, const std::string& destinationProviderName)
169 {
170 std::string prefix =
171 // FIXME: Get this information from libtool
172 #if defined(__MSYS__)
173 "msys-"
174 #elif defined(__CYGWIN__)
175 "cyg"
176 #else
177 "lib"
178 #endif
179 ;
180 std::string sourceName = prefix + sourceProviderName + "." + G_MODULE_SUFFIX;
181 std::string destinationName = destinationProviderName + "." + G_MODULE_SUFFIX;
182
183 std::string destinationDir = AddToPath(LIBDIR_SUBDIR, "enchant-" ENCHANT_MAJOR_VERSION);
184
185 CreateDirectory(destinationDir);
186
187 std::string destinationPath = AddToPath(destinationDir, destinationName);
188
189 gchar * contents;
190 gsize length;
191 if(g_file_get_contents(sourceName.c_str(), &contents, &length, NULL)){
192 g_file_set_contents(destinationPath.c_str(), contents, length, NULL);
193 g_free(contents);
194 }
195 }
196
197
198 EnchantProvider* GetMockProvider(){
199 return mock_provider;
200 }
201
202 void SetErrorOnMockProvider(const std::string& error)
203 {
204 EnchantProvider* provider = GetMockProvider();
205 if(provider != NULL)
206 {
207 enchant_provider_set_error(provider, error.c_str());
208 }
209 }
210
211 EnchantDict* RequestDictionary(const std::string& tag){
212 return enchant_broker_request_dict(_broker, tag.c_str());
213 }
214
215 EnchantDict* RequestPersonalDictionary()
216 {
217 std::string pwlFileName = GetTemporaryFilename("epwl");
218 CreateFile(pwlFileName);
219 pwlFilenames.push(pwlFileName);
220 return enchant_broker_request_pwl_dict(_broker, pwlFileName.c_str());
221 }
222
223 std::string GetLastPersonalDictionaryFileName()
224 {
225 return pwlFilenames.top();
226 }
227
228
229 void FreeDictionary(EnchantDict* dictionary){
230 if(dictionary != NULL){
231 enchant_broker_free_dict(_broker, dictionary);
232 }
233 }
234
235 private:
236 std::stack<std::string> pwlFilenames;
237 static EnchantProvider * mock_provider;
238 static ConfigureHook userMockProviderConfiguration;
239 static ConfigureHook userMockProvider2Configuration;
240 static void ConfigureMockProvider (EnchantProvider * me, const char * dir_name)
241 {
242 mock_provider = me;
243 if(userMockProviderConfiguration){
244 userMockProviderConfiguration(me, dir_name);
245 }
246 }
247
248 static void ConfigureMockProvider2 (EnchantProvider * me, const char * dir_name)
249 {
250 mock_provider = me;
251 if(userMockProvider2Configuration){
252 userMockProvider2Configuration(me, dir_name);
253 }
254 }
255 };
256
257 struct DictionaryDescription
258 {
259 std::string LanguageTag;
260 std::string Name;
261 std::string Description;
262 std::string DllFile;
263 DictionaryDescription()
264 {}
265 DictionaryDescription(const char* const language_tag,
266 const char * const provider_name,
267 const char * const provider_desc,
268 const char * const provider_file):
269 LanguageTag(language_tag),
270 Name(provider_name),
271 Description(provider_desc),
272 DllFile(provider_file)
273 {}
274
275 DictionaryDescription(const DictionaryDescription& d):
276 LanguageTag(d.LanguageTag),
277 Name(d.Name),
278 Description(d.Description),
279 DllFile(d.DllFile)
280 {}
281
282 bool DataIsComplete()
283 {
284 return (LanguageTag.length() &&
285 Name.length() &&
286 Description.length() &&
287 DllFile.length());
288 }
289 };
290
291 #endif
DictionaryDescription
Definition: EnchantBrokerTestFixture.h:258
EnchantBrokerTestFixture
Definition: EnchantBrokerTestFixture.h:103
EnchantTestFixture
Definition: EnchantTestFixture.h:44
str_enchant_broker
Definition: lib.c:55
str_enchant_dict
Definition: enchant-provider.h:125
str_enchant_provider
Definition: enchant-provider.h:150