Enchant: tests/EnchantBrokerTestFixture.h Source File

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