1 /* Copyright (c) 2007 Eric Scott Albright
\r
3 * Permission is hereby granted, free of charge, to any person obtaining a copy
\r
4 * of this software and associated documentation files (the "Software"), to deal
\r
5 * in the Software without restriction, including without limitation the rights
\r
6 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
\r
7 * copies of the Software, and to permit persons to whom the Software is
\r
8 * furnished to do so, subject to the following conditions:
\r
10 * The above copyright notice and this permission notice shall be included in
\r
11 * all copies or substantial portions of the Software.
\r
13 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
\r
14 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
\r
15 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
\r
16 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
\r
17 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
\r
18 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
\r
22 #ifndef __ENCHANTDICTIONARYTESTFIXTURE
\r
23 #define __ENCHANTDICTIONARYTESTFIXTURE
\r
25 #if defined(_MSC_VER)
\r
27 #pragma warning(push)
\r
28 #pragma warning(disable: 4996) //The POSIX name for this item is deprecated. Instead, use the ISO C++ conformant name.
\r
31 #include "../EnchantBrokerTestFixture.h"
\r
33 #include <sys/types.h>
\r
34 #include <sys/stat.h>
\r
39 MockProviderRequestEmptyMockDictionary(EnchantProvider *, const char *)
\r
41 EnchantDict* dict = g_new0(EnchantDict, 1);
\r
42 dict->user_data = NULL;
\r
44 dict->suggest = NULL;
\r
45 dict->add_to_personal = NULL;
\r
46 dict->add_to_session = NULL;
\r
47 dict->store_replacement = NULL;
\r
52 static void EmptyDictionary_ProviderConfiguration (EnchantProvider * me, const char *)
\r
54 me->request_dict = MockProviderRequestEmptyMockDictionary;
\r
55 me->dispose_dict = MockProviderDisposeDictionary;
\r
60 MockDictionarySuggest (EnchantDict * ,
\r
61 const char *const word,
\r
63 size_t * out_n_suggs)
\r
65 char **sugg_arr = NULL;
\r
69 sugg_arr = g_new0 (char *, *out_n_suggs + 1);
\r
70 for(size_t i=0; i<*out_n_suggs;++i){
\r
72 sugg_arr[i] = g_strdup (word);
\r
75 sugg_arr[i] = g_strndup (word, (gsize)len);
\r
77 sugg_arr[i][0] = 'a' + (char)i;
\r
83 MockProviderRequestBasicMockDictionary(EnchantProvider *me, const char *tag)
\r
86 EnchantDict* dict = MockProviderRequestEmptyMockDictionary(me, tag);
\r
87 dict->suggest = MockDictionarySuggest;
\r
92 static void BasicDictionary_ProviderConfiguration (EnchantProvider * me, const char *)
\r
94 me->request_dict = MockProviderRequestBasicMockDictionary;
\r
95 me->dispose_dict = MockProviderDisposeDictionary;
\r
96 me->free_string_list = MockProviderFreeStringList;
\r
99 struct EnchantDictionaryTestFixture : EnchantBrokerTestFixture
\r
101 EnchantDict* _dict;
\r
103 std::string _pwlFileName;
\r
104 std::string origLangEnv;
\r
106 std::string languageTag;
\r
109 EnchantDictionaryTestFixture(ConfigureHook userConfiguration=BasicDictionary_ProviderConfiguration, const std::string& languageTag="qaa"):
\r
110 EnchantBrokerTestFixture(userConfiguration),
\r
111 languageTag(languageTag)
\r
113 InitializeTestDictionary();
\r
114 _pwl = RequestPersonalDictionary();
\r
115 _pwlFileName = GetLastPersonalDictionaryFileName();
\r
117 bool hasLangEnv = (g_getenv("LANG") != NULL);
\r
120 origLangEnv = std::string(g_getenv("LANG"));
\r
125 ~EnchantDictionaryTestFixture(){
\r
126 FreeTestDictionary();
\r
127 FreeDictionary(_pwl);
\r
131 void SetLocale(const std::string& locale)
\r
133 g_setenv("LANG", locale.c_str(), TRUE);
\r
140 g_setenv("LANG", origLangEnv.c_str(), TRUE);
\r
143 g_unsetenv("LANG");
\r
147 void ReloadTestDictionary()
\r
149 FreeTestDictionary();
\r
150 InitializeTestDictionary();
\r
153 void InitializeTestDictionary()
\r
155 _dict = enchant_broker_request_dict(_broker, languageTag.c_str());
\r
158 void FreeTestDictionary()
\r
160 FreeDictionary(_dict);
\r
163 bool PersonalWordListFileHasContents()
\r
165 return FileHasContents(GetPersonalDictFileName());
\r
168 bool ExcludeFileHasContents()
\r
170 return FileHasContents(GetExcludeDictFileName());
\r
173 bool BrokerPWLFileHasContents()
\r
175 return FileHasContents(_pwlFileName);
\r
178 bool FileHasContents(const std::string & filename)
\r
180 bool hasContents = false;
\r
181 int fd = g_open(filename.c_str(), O_RDONLY, S_IREAD );
\r
187 while(read(fd, &c, 1) == 1 && !hasContents){
\r
195 hasContents = true;
\r
200 return hasContents;
\r
203 std::string GetPersonalDictFileName(){
\r
204 return AddToPath(GetTempUserEnchantDir(), "qaa.dic");
\r
207 std::string GetExcludeDictFileName(){
\r
208 return AddToPath(GetTempUserEnchantDir(), "qaa.exc");
\r
211 void SetErrorOnMockDictionary(const std::string& error)
\r
213 enchant_dict_set_error(_dict, error.c_str());
\r
216 void FreeStringList(char** list)
\r
220 enchant_dict_free_string_list(_dict, list);
\r
224 bool IsWordInSession(const std::string& word){
\r
225 return enchant_dict_is_in_session(_dict, word.c_str(), word.size())!=0;
\r
228 bool IsWordInDictionary(const std::string& word){
\r
229 return enchant_dict_check(_dict, word.c_str(), word.size())==0;
\r
232 void RemoveWordFromDictionary(const std::string& word)
\r
234 enchant_dict_remove(_dict, word.c_str(), word.size());
\r
237 void AddWordToDictionary(const std::string& word)
\r
239 enchant_dict_add(_dict, word.c_str(), word.size());
\r
242 void AddWordsToDictionary(const std::vector<const std::string>& sWords)
\r
244 for(std::vector<const std::string>::const_iterator itWord = sWords.begin();
\r
245 itWord != sWords.end();
\r
247 AddWordToDictionary(*itWord);
\r
251 void ExternalAddWordToDictionary(const std::string& word)
\r
253 ExternalAddWordToFile(word, GetPersonalDictFileName());
\r
256 void ExternalAddWordToExclude(const std::string& word)
\r
258 ExternalAddWordToFile(word, GetExcludeDictFileName());
\r
261 static void ExternalAddWordToFile(const std::string& word, const std::string& filename)
\r
263 Sleep(1000); // FAT systems have a 2 second resolution
\r
264 // NTFS is appreciably faster but no specs on what it is exactly
\r
265 // c runtime library's time_t has a 1 second resolution
\r
266 FILE * f = g_fopen(filename.c_str(), "at");
\r
270 fputs(word.c_str(), f);
\r
275 void ExternalAddNewLineToDictionary()
\r
277 Sleep(1000); // FAT systems have a 2 second resolution
\r
278 // NTFS is appreciably faster but no specs on what it is exactly
\r
279 // c runtime library's time_t has a 1 second resolution
\r
280 FILE * f = g_fopen(GetPersonalDictFileName().c_str(), "at");
\r
288 void ExternalAddWordsToDictionary(const std::vector<const std::string>& sWords)
\r
290 Sleep(1000); // FAT systems have a 2 second resolution
\r
291 // NTFS is appreciably faster but no specs on what it is exactly
\r
292 // c runtime library's time_t has a 1 second resolution
\r
293 FILE * f = g_fopen(GetPersonalDictFileName().c_str(), "at");
\r
296 for(std::vector<const std::string>::const_iterator
\r
297 itWord = sWords.begin();
\r
298 itWord != sWords.end(); ++itWord)
\r
300 if(itWord != sWords.begin()){
\r
303 fputs(itWord->c_str(), f);
\r
309 std::vector<std::string> GetExpectedSuggestions(const std::string& s, size_t begin = 0)
\r
311 size_t cSuggestions;
\r
312 char** expectedSuggestions = MockDictionarySuggest (_dict,
\r
317 std::vector<std::string> result;
\r
318 if(expectedSuggestions != NULL && begin < cSuggestions){
\r
319 result.insert(result.begin(), expectedSuggestions+begin, expectedSuggestions+cSuggestions);
\r
320 FreeStringList(expectedSuggestions);
\r
327 std::vector<const std::string> GetSuggestionsFromWord(const std::string& word)
\r
329 std::vector<const std::string> result;
\r
331 size_t cSuggestions;
\r
332 char** suggestions = enchant_dict_suggest(_dict, word.c_str(), word.size(), &cSuggestions);
\r
334 if(suggestions != NULL){
\r
335 result.insert(result.begin(), suggestions, suggestions+cSuggestions);
\r
338 FreeStringList(suggestions);
\r
343 std::vector<const std::string> GetSuggestions(const std::string& s)
\r
345 return GetSuggestionsFromWord("helo");
\r
348 #if defined(_MSC_VER)
\r
349 #pragma warning(pop)
\r