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 #define NOMINMAX //don't want windows to collide with std::min
\r
24 #include <UnitTest++.h>
\r
25 #include <enchant.h>
\r
26 #include "../EnchantDictionaryTestFixture.h"
\r
28 static bool addToExcludeCalled;
\r
29 static std::string wordToAdd;
\r
32 MockDictionaryAddToExclude (EnchantDict * dict, const char *const word, size_t len)
\r
35 addToExcludeCalled = true;
\r
36 wordToAdd = std::string(word, len);
\r
39 static bool dictCheckCalled;
\r
42 MockDictionaryCheck (EnchantDict * dict, const char *const word, size_t len)
\r
45 dictCheckCalled = true;
\r
46 if(strncmp("hello", word, len)==0)
\r
48 return 0; //good word
\r
50 return 1; // bad word
\r
54 static EnchantDict* MockProviderRequestAddToExcludeMockDictionary(EnchantProvider * me, const char *tag)
\r
57 EnchantDict* dict = MockProviderRequestBasicMockDictionary(me, tag);
\r
58 dict->add_to_exclude = MockDictionaryAddToExclude;
\r
59 dict->check = MockDictionaryCheck;
\r
63 static void DictionaryAddToExclude_ProviderConfiguration (EnchantProvider * me, const char *)
\r
65 me->request_dict = MockProviderRequestAddToExcludeMockDictionary;
\r
66 me->dispose_dict = MockProviderDisposeDictionary;
\r
69 struct EnchantDictionaryRemove_TestFixture : EnchantDictionaryTestFixture
\r
72 EnchantDictionaryRemove_TestFixture():
\r
73 EnchantDictionaryTestFixture(DictionaryAddToExclude_ProviderConfiguration)
\r
75 addToExcludeCalled = false;
\r
76 dictCheckCalled = false;
\r
80 static EnchantDict* MockProviderRequestCheckMockDictionary(EnchantProvider * me, const char *tag)
\r
83 EnchantDict* dict = MockProviderRequestEmptyMockDictionary(me, tag);
\r
84 dict->check = MockDictionaryCheck;
\r
88 static void DictionaryCheck_ProviderConfiguration (EnchantProvider * me, const char *)
\r
90 me->request_dict = MockProviderRequestCheckMockDictionary;
\r
91 me->dispose_dict = MockProviderDisposeDictionary;
\r
94 struct EnchantDictionaryRemoveNotImplemented_TestFixture : EnchantDictionaryTestFixture
\r
97 EnchantDictionaryRemoveNotImplemented_TestFixture():
\r
98 EnchantDictionaryTestFixture(DictionaryCheck_ProviderConfiguration)
\r
100 addToExcludeCalled = false;
\r
101 dictCheckCalled = false;
\r
105 * enchant_dict_remove
\r
106 * @dict: A non-null #EnchantDict
\r
107 * @word: The non-null word you wish to add to your exclude dictionary and
\r
108 * remove from the personal dictionary, in UTF-8 encoding
\r
109 * @len: The byte length of @word, or -1 for strlen (@word)
\r
114 /////////////////////////////////////////////////////////////////////////////
\r
115 // Test Normal Operation
\r
116 TEST_FIXTURE(EnchantDictionaryRemove_TestFixture,
\r
117 EnchantDictionaryRemove_WordNoLongerExistsInDictionary)
\r
119 CHECK(IsWordInDictionary("hello"));
\r
120 enchant_dict_remove(_dict, "hello", -1);
\r
121 CHECK(!IsWordInDictionary("hello"));
\r
124 TEST_FIXTURE(EnchantDictionaryRemove_TestFixture,
\r
125 EnchantDictionaryRemove_WordNoLongerExistsInSession)
\r
127 enchant_dict_add(_dict, "hello", -1);
\r
128 CHECK(IsWordInSession("hello"));
\r
129 enchant_dict_remove(_dict, "hello", -1);
\r
130 CHECK(!IsWordInSession("hello"));
\r
133 TEST_FIXTURE(EnchantDictionaryRemove_TestFixture,
\r
134 EnchantDictionaryRemove_WordAddedToEnchantExcludeFile)
\r
136 CHECK(!ExcludeFileHasContents());
\r
137 enchant_dict_remove(_dict, "hello", -1);
\r
138 CHECK(ExcludeFileHasContents());
\r
141 TEST_FIXTURE(EnchantDictionaryRemove_TestFixture,
\r
142 EnchantDictionaryRemove_WordRemovedFromEnchantPwlFile)
\r
144 enchant_dict_add(_dict, "hello", -1);
\r
145 CHECK(PersonalWordListFileHasContents());
\r
146 enchant_dict_remove(_dict, "hello", -1);
\r
147 CHECK(!PersonalWordListFileHasContents());
\r
150 TEST_FIXTURE(EnchantDictionaryRemove_TestFixture,
\r
151 EnchantDictionaryRemove_PassedOnToProvider_LenComputed)
\r
153 enchant_dict_remove(_dict, "hello", -1);
\r
154 CHECK(addToExcludeCalled);
\r
155 CHECK_EQUAL(std::string("hello"), wordToAdd);
\r
158 TEST_FIXTURE(EnchantDictionaryRemove_TestFixture,
\r
159 EnchantDictionaryRemove_PassedOnToProvider_LenSpecified)
\r
161 enchant_dict_remove(_dict, "hellodisregard me", 5);
\r
162 CHECK(addToExcludeCalled);
\r
163 CHECK_EQUAL(std::string("hello"), wordToAdd);
\r
167 TEST_FIXTURE(EnchantDictionaryRemove_TestFixture,
\r
168 EnchantDictionaryRemove_WordRemovedFromSession_StillCallsProvider)
\r
170 enchant_dict_remove_from_session(_dict, "hello", -1);
\r
171 CHECK(!addToExcludeCalled);
\r
173 enchant_dict_remove(_dict, "hello", -1);
\r
174 CHECK(addToExcludeCalled);
\r
175 CHECK_EQUAL(std::string("hello"), wordToAdd);
\r
178 TEST_FIXTURE(EnchantDictionaryRemove_TestFixture,
\r
179 EnchantDictionaryRemove_WordExistsInExclude_StillCallsProvider)
\r
181 enchant_dict_remove(_dict, "hello", -1);
\r
182 addToExcludeCalled=false;
\r
183 wordToAdd = std::string();
\r
185 enchant_dict_remove(_dict, "hello", -1);
\r
186 CHECK(addToExcludeCalled);
\r
187 CHECK_EQUAL(std::string("hello"), wordToAdd);
\r
190 TEST_FIXTURE(EnchantDictionaryRemove_TestFixture,
\r
191 EnchantDictionaryRemove_ProviderNotAskedIfWordExistsInDictionary)
\r
193 enchant_dict_remove(_dict, "hello", -1);
\r
194 CHECK(!IsWordInDictionary("hello"));
\r
195 CHECK(!dictCheckCalled);
\r
198 TEST_FIXTURE(EnchantDictionaryRemove_TestFixture,
\r
199 EnchantDictionaryRemove_NotGivenAsSuggestion)
\r
201 enchant_dict_remove(_dict, "aelo", -1);
\r
203 std::vector<const std::string> suggestions = GetSuggestions("helo");
\r
204 CHECK_EQUAL(3, suggestions.size());
\r
205 CHECK_ARRAY_EQUAL(GetExpectedSuggestions("helo",1), suggestions, std::min(3u,suggestions.size()));
\r
208 TEST_FIXTURE(EnchantDictionaryRemove_TestFixture,
\r
209 EnchantDictionaryRemove_ThenAddedToSession_GivenAsSuggestion)
\r
211 enchant_dict_remove(_dict, "aelo", -1);
\r
212 enchant_dict_add_to_session(_dict, "aelo", -1);
\r
214 std::vector<const std::string> suggestions = GetSuggestions("helo");
\r
215 CHECK_EQUAL(4, suggestions.size());
\r
216 CHECK_ARRAY_EQUAL(GetExpectedSuggestions("helo"), suggestions, suggestions.size());
\r
219 TEST_FIXTURE(EnchantDictionaryRemove_TestFixture,
\r
220 EnchantDictionaryRemove_ThenAddedToSession_WhenSessionOverStillExcluded)
\r
222 enchant_dict_remove(_dict, "aelo", -1);
\r
223 enchant_dict_add_to_session(_dict, "aelo", -1);
\r
225 ReloadTestDictionary(); // to see what actually persisted
\r
227 std::vector<const std::string> suggestions = GetSuggestions("helo");
\r
228 CHECK_EQUAL(3, suggestions.size());
\r
229 CHECK_ARRAY_EQUAL(GetExpectedSuggestions("helo",1), suggestions, std::min(3u,suggestions.size()));
\r
232 /* since the broker pwl file is a read/write file (there is no readonly dictionary in addition)
\r
233 * there is no need for complementary exclude file to add a word to. The word just needs to be
\r
234 * removed from the broker pwl file
\r
236 TEST_FIXTURE(EnchantDictionaryRemove_TestFixture,
\r
237 EnchantDictionaryRemove_OnBrokerPwl_RemovedFromBrokerPwlFile)
\r
239 CHECK(!BrokerPWLFileHasContents());
\r
240 enchant_dict_add(_pwl, "personal", -1);
\r
241 CHECK(BrokerPWLFileHasContents());
\r
243 enchant_dict_remove(_pwl, "personal", -1);
\r
244 CHECK(!BrokerPWLFileHasContents());
\r
247 TEST_FIXTURE(EnchantDictionaryRemove_TestFixture,
\r
248 EnchantDictionaryRemove_OnBrokerPwl_NotAddedToOtherPwlFile)
\r
250 CHECK(!BrokerPWLFileHasContents());
\r
252 enchant_dict_remove(_pwl, "personal", -1);
\r
253 CHECK(!addToExcludeCalled);
\r
254 CHECK(!ExcludeFileHasContents());
\r
257 TEST_FIXTURE(EnchantDictionaryRemove_TestFixture,
\r
258 EnchantDictionaryRemove_IsPermanent)
\r
260 enchant_dict_remove(_dict, "hello", -1);
\r
261 CHECK(!IsWordInDictionary("hello"));
\r
263 ReloadTestDictionary();
\r
265 CHECK(!IsWordInDictionary("hello"));
\r
269 TEST_FIXTURE(EnchantDictionaryRemove_TestFixture,
\r
270 EnchantDictionaryRemove_HasPreviousError_ErrorCleared)
\r
272 SetErrorOnMockDictionary("something bad happened");
\r
274 enchant_dict_remove(_dict, "hello", -1);
\r
275 CHECK_EQUAL((void*)NULL, (void*)enchant_dict_get_error(_dict));
\r
278 /////////////////////////////////////////////////////////////////////////////
\r
279 // Test Error Conditions
\r
280 TEST_FIXTURE(EnchantDictionaryRemove_TestFixture,
\r
281 EnchantDictionaryRemove_NullDictionary_NotRemoved)
\r
283 enchant_dict_remove(NULL, "hello", -1);
\r
284 CHECK(!addToExcludeCalled);
\r
287 TEST_FIXTURE(EnchantDictionaryRemove_TestFixture,
\r
288 EnchantDictionaryRemove_NullWord_NotRemoved)
\r
290 enchant_dict_remove(_dict, NULL, -1);
\r
291 CHECK(!addToExcludeCalled);
\r
294 TEST_FIXTURE(EnchantDictionaryRemove_TestFixture,
\r
295 EnchantDictionaryRemove_EmptyWord_NotRemoved)
\r
297 enchant_dict_remove(_dict, "", -1);
\r
298 CHECK(!addToExcludeCalled);
\r
301 TEST_FIXTURE(EnchantDictionaryRemove_TestFixture,
\r
302 EnchantDictionaryRemove_WordSize0_NotRemoved)
\r
304 enchant_dict_remove(_dict, "hello", 0);
\r
305 CHECK(!addToExcludeCalled);
\r
308 TEST_FIXTURE(EnchantDictionaryRemove_TestFixture,
\r
309 EnchantDictionaryRemove_InvalidUtf8Word_NotRemoved)
\r
311 enchant_dict_remove(_dict, "\xa5\xf1\x08", -1);
\r
312 CHECK(!addToExcludeCalled);
\r
315 TEST_FIXTURE(EnchantDictionaryRemoveNotImplemented_TestFixture,
\r
316 EnchantDictionaryRemoveNotImplemented_WordAddedToExcludeFile)
\r
318 CHECK(!ExcludeFileHasContents());
\r
319 enchant_dict_remove(_dict, "hello", -1);
\r
320 CHECK(ExcludeFileHasContents());
\r
323 TEST_FIXTURE(EnchantDictionaryRemoveNotImplemented_TestFixture,
\r
324 EnchantDictionaryRemoveNotImplemented_NotPassedOnToProvider)
\r
326 enchant_dict_remove(_dict, "hello", -1);
\r
327 CHECK(!addToExcludeCalled);
\r
330 TEST_FIXTURE(EnchantDictionaryRemoveNotImplemented_TestFixture,
\r
331 EnchantDictionaryRemoveNotImplemented_StillExcluded)
\r
333 CHECK(IsWordInDictionary("hello"));
\r
334 enchant_dict_remove(_dict, "hello", -1);
\r
335 CHECK(!IsWordInDictionary("hello"));
\r