22 #ifndef __ENCHANTDICTIONARYTESTFIXTURE
23 #define __ENCHANTDICTIONARYTESTFIXTURE
25 #include "EnchantBrokerTestFixture.h"
27 #include <sys/types.h>
38 dict->user_data = NULL;
41 dict->add_to_session = NULL;
46 static void EmptyDictionary_ProviderConfiguration (
EnchantProvider * me,
const char *)
48 me->request_dict = MockProviderRequestEmptyMockDictionary;
49 me->dispose_dict = MockProviderDisposeDictionary;
55 const char *
const word,
61 char **sugg_arr = g_new0 (
char *, *out_n_suggs + 1);
62 for(
size_t i=0; i<*out_n_suggs;++i){
64 sugg_arr[i] = g_strdup (word);
67 sugg_arr[i] = g_strndup (word, (gsize)len);
69 sugg_arr[i][0] =
'a' + (char)i;
75 MockProviderRequestBasicMockDictionary(
EnchantProvider *me,
const char *tag)
78 EnchantDict* dict = MockProviderRequestEmptyMockDictionary(me, tag);
79 dict->suggest = MockDictionarySuggest;
84 static void BasicDictionary_ProviderConfiguration (
EnchantProvider * me,
const char *)
86 me->request_dict = MockProviderRequestBasicMockDictionary;
87 me->dispose_dict = MockProviderDisposeDictionary;
94 std::string _pwlFileName;
95 std::string origLangEnv;
97 std::string languageTag;
100 EnchantDictionaryTestFixture(ConfigureHook userConfiguration=BasicDictionary_ProviderConfiguration,
const std::string& languageTag=
"qaa"):
102 languageTag(languageTag)
104 InitializeTestDictionary();
105 _pwl = RequestPersonalDictionary();
106 _pwlFileName = GetLastPersonalDictionaryFileName();
108 hasLangEnv = (g_getenv(
"LANG") != NULL);
111 origLangEnv = std::string(g_getenv(
"LANG"));
117 FreeTestDictionary();
118 FreeDictionary(_pwl);
122 void SetLocale(
const std::string& locale)
124 g_setenv(
"LANG", locale.c_str(), TRUE);
131 g_setenv(
"LANG", origLangEnv.c_str(), TRUE);
138 void ReloadTestDictionary()
140 FreeTestDictionary();
141 InitializeTestDictionary();
144 void InitializeTestDictionary()
146 _dict = enchant_broker_request_dict(_broker, languageTag.c_str());
149 void FreeTestDictionary()
151 FreeDictionary(_dict);
154 bool PersonalWordListFileHasContents()
156 return FileHasContents(GetPersonalDictFileName());
159 bool ExcludeFileHasContents()
161 return FileHasContents(GetExcludeDictFileName());
164 bool BrokerPWLFileHasContents()
166 return FileHasContents(_pwlFileName);
169 bool FileHasContents(
const std::string & filename)
171 bool hasContents =
false;
172 int fd = g_open(filename.c_str(), O_RDONLY, S_IREAD );
178 while(read(fd, &c, 1) == 1 && !hasContents){
194 std::string GetPersonalDictFileName(){
195 return AddToPath(GetTempUserEnchantDir(),
"qaa.dic");
198 std::string GetExcludeDictFileName(){
199 return AddToPath(GetTempUserEnchantDir(),
"qaa.exc");
202 void SetErrorOnMockDictionary(
const std::string& error)
204 enchant_dict_set_error(_dict, error.c_str());
207 void FreeStringList(
char** list)
211 enchant_dict_free_string_list(_dict, list);
215 void FreePwlStringList(
char** list)
219 enchant_dict_free_string_list(_pwl, list);
223 bool IsWordInSession(
const std::string& word){
224 return enchant_dict_is_added(_dict, word.c_str(), word.size())!=0;
227 bool IsWordInDictionary(
const std::string& word){
228 return enchant_dict_check(_dict, word.c_str(), word.size())==0;
231 void RemoveWordFromDictionary(
const std::string& word)
233 enchant_dict_remove(_dict, word.c_str(), word.size());
236 void AddWordToDictionary(
const std::string& word)
238 enchant_dict_add(_dict, word.c_str(), word.size());
241 void AddWordsToDictionary(
const std::vector<std::string>& sWords)
243 for(std::vector<std::string>::const_iterator itWord = sWords.begin();
244 itWord != sWords.end();
246 AddWordToDictionary(*itWord);
250 void ExternalAddWordToDictionary(
const std::string& word)
252 ExternalAddWordToFile(word, GetPersonalDictFileName());
255 void ExternalAddWordToExclude(
const std::string& word)
257 ExternalAddWordToFile(word, GetExcludeDictFileName());
260 static void ExternalAddWordToFile(
const std::string& word,
const std::string& filename)
265 FILE * f = g_fopen(filename.c_str(),
"a");
269 fputs(word.c_str(), f);
274 void ExternalAddNewLineToDictionary()
279 FILE * f = g_fopen(GetPersonalDictFileName().c_str(),
"a");
287 void ExternalAddWordsToDictionary(
const std::vector<std::string>& sWords)
292 FILE * f = g_fopen(GetPersonalDictFileName().c_str(),
"a");
295 for (std::vector<std::string>::const_iterator itWord = sWords.begin();
296 itWord != sWords.end(); ++itWord)
298 if (itWord != sWords.begin()) {
301 fputs(itWord->c_str(), f);
307 std::vector<std::string> GetExpectedSuggestions(
const std::string& s,
size_t begin = 0)
310 char** expectedSuggestions = MockDictionarySuggest (_dict,
315 std::vector<std::string> result;
316 if(expectedSuggestions != NULL && begin < cSuggestions){
317 result.insert(result.begin(), expectedSuggestions+begin, expectedSuggestions+cSuggestions);
318 g_strfreev(expectedSuggestions);
325 std::vector<std::string> GetSuggestionsFromWord(
const std::string& word)
327 std::vector<std::string> result;
330 char** suggestions = enchant_dict_suggest(_dict, word.c_str(), word.size(), &cSuggestions);
332 if(suggestions != NULL){
333 result.insert(result.begin(), suggestions, suggestions+cSuggestions);
336 FreeStringList(suggestions);
341 std::vector<std::string> GetSuggestions(
const std::string& s)
343 return GetSuggestionsFromWord(
"helo");
Definition: EnchantBrokerTestFixture.h:103
Definition: EnchantDictionaryTestFixture.h:91
Definition: enchant-provider.h:125
Definition: enchant-provider.h:150