Tizen 2.1 base
[external/enchant.git] / unittests / dictionary / enchant_dict_remove_tests.cpp
1 /* Copyright (c) 2007 Eric Scott Albright\r
2  * \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
9  * \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
12  * \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
19  * THE SOFTWARE.\r
20  */\r
21 \r
22 #define NOMINMAX //don't want windows to collide with std::min\r
23 \r
24 #include <UnitTest++.h>\r
25 #include <enchant.h>\r
26 #include "../EnchantDictionaryTestFixture.h"\r
27 \r
28 static bool addToExcludeCalled;\r
29 static std::string wordToAdd;\r
30 \r
31 static void\r
32 MockDictionaryAddToExclude (EnchantDict * dict, const char *const word, size_t len)\r
33 {\r
34     dict;\r
35     addToExcludeCalled = true;\r
36     wordToAdd = std::string(word, len);\r
37 }\r
38 \r
39 static bool dictCheckCalled;\r
40 \r
41 static int\r
42 MockDictionaryCheck (EnchantDict * dict, const char *const word, size_t len)\r
43 {\r
44     dict;\r
45     dictCheckCalled = true;\r
46     if(strncmp("hello", word, len)==0)\r
47     {\r
48         return 0; //good word\r
49     }\r
50     return 1; // bad word\r
51 }\r
52 \r
53 \r
54 static EnchantDict* MockProviderRequestAddToExcludeMockDictionary(EnchantProvider * me, const char *tag)\r
55 {\r
56     \r
57     EnchantDict* dict = MockProviderRequestBasicMockDictionary(me, tag);\r
58     dict->add_to_exclude = MockDictionaryAddToExclude;\r
59     dict->check = MockDictionaryCheck;\r
60     return dict;\r
61 }\r
62 \r
63 static void DictionaryAddToExclude_ProviderConfiguration (EnchantProvider * me, const char *)\r
64 {\r
65      me->request_dict = MockProviderRequestAddToExcludeMockDictionary;\r
66      me->dispose_dict = MockProviderDisposeDictionary;\r
67 }\r
68 \r
69 struct EnchantDictionaryRemove_TestFixture : EnchantDictionaryTestFixture\r
70 {\r
71     //Setup\r
72     EnchantDictionaryRemove_TestFixture():\r
73             EnchantDictionaryTestFixture(DictionaryAddToExclude_ProviderConfiguration)\r
74     { \r
75         addToExcludeCalled = false;\r
76         dictCheckCalled = false;\r
77     }\r
78 };\r
79 \r
80 static EnchantDict* MockProviderRequestCheckMockDictionary(EnchantProvider * me, const char *tag)\r
81 {\r
82     \r
83     EnchantDict* dict = MockProviderRequestEmptyMockDictionary(me, tag);\r
84     dict->check = MockDictionaryCheck;\r
85     return dict;\r
86 }\r
87 \r
88 static void DictionaryCheck_ProviderConfiguration (EnchantProvider * me, const char *)\r
89 {\r
90      me->request_dict = MockProviderRequestCheckMockDictionary;\r
91      me->dispose_dict = MockProviderDisposeDictionary;\r
92 }\r
93 \r
94 struct EnchantDictionaryRemoveNotImplemented_TestFixture : EnchantDictionaryTestFixture\r
95 {\r
96     //Setup\r
97     EnchantDictionaryRemoveNotImplemented_TestFixture():\r
98             EnchantDictionaryTestFixture(DictionaryCheck_ProviderConfiguration)\r
99     { \r
100         addToExcludeCalled = false;\r
101         dictCheckCalled = false;\r
102     }\r
103 };\r
104 /**\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
110  *\r
111  */\r
112 \r
113 \r
114 /////////////////////////////////////////////////////////////////////////////\r
115 // Test Normal Operation\r
116 TEST_FIXTURE(EnchantDictionaryRemove_TestFixture,\r
117              EnchantDictionaryRemove_WordNoLongerExistsInDictionary)\r
118 {\r
119     CHECK(IsWordInDictionary("hello"));\r
120     enchant_dict_remove(_dict, "hello", -1);\r
121     CHECK(!IsWordInDictionary("hello"));\r
122 }\r
123 \r
124 TEST_FIXTURE(EnchantDictionaryRemove_TestFixture,\r
125              EnchantDictionaryRemove_WordNoLongerExistsInSession)\r
126 {\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
131 }\r
132 \r
133 TEST_FIXTURE(EnchantDictionaryRemove_TestFixture,\r
134              EnchantDictionaryRemove_WordAddedToEnchantExcludeFile)\r
135 {\r
136     CHECK(!ExcludeFileHasContents());\r
137     enchant_dict_remove(_dict, "hello", -1);\r
138     CHECK(ExcludeFileHasContents());\r
139 }\r
140 \r
141 TEST_FIXTURE(EnchantDictionaryRemove_TestFixture,\r
142              EnchantDictionaryRemove_WordRemovedFromEnchantPwlFile)\r
143 {\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
148 }\r
149 \r
150 TEST_FIXTURE(EnchantDictionaryRemove_TestFixture,\r
151              EnchantDictionaryRemove_PassedOnToProvider_LenComputed)\r
152 {\r
153     enchant_dict_remove(_dict, "hello", -1);\r
154     CHECK(addToExcludeCalled);\r
155     CHECK_EQUAL(std::string("hello"), wordToAdd);\r
156 }\r
157 \r
158 TEST_FIXTURE(EnchantDictionaryRemove_TestFixture,\r
159              EnchantDictionaryRemove_PassedOnToProvider_LenSpecified)\r
160 {\r
161     enchant_dict_remove(_dict, "hellodisregard me", 5);\r
162     CHECK(addToExcludeCalled);\r
163     CHECK_EQUAL(std::string("hello"), wordToAdd);\r
164 }\r
165 \r
166 \r
167 TEST_FIXTURE(EnchantDictionaryRemove_TestFixture,\r
168              EnchantDictionaryRemove_WordRemovedFromSession_StillCallsProvider)\r
169 {\r
170     enchant_dict_remove_from_session(_dict, "hello", -1);\r
171     CHECK(!addToExcludeCalled);\r
172 \r
173     enchant_dict_remove(_dict, "hello", -1);\r
174     CHECK(addToExcludeCalled);\r
175     CHECK_EQUAL(std::string("hello"), wordToAdd);\r
176 }\r
177 \r
178 TEST_FIXTURE(EnchantDictionaryRemove_TestFixture,\r
179              EnchantDictionaryRemove_WordExistsInExclude_StillCallsProvider)\r
180 {\r
181     enchant_dict_remove(_dict, "hello", -1);\r
182     addToExcludeCalled=false;\r
183     wordToAdd = std::string();\r
184 \r
185     enchant_dict_remove(_dict, "hello", -1);\r
186     CHECK(addToExcludeCalled);\r
187     CHECK_EQUAL(std::string("hello"), wordToAdd);\r
188 }\r
189 \r
190 TEST_FIXTURE(EnchantDictionaryRemove_TestFixture,\r
191              EnchantDictionaryRemove_ProviderNotAskedIfWordExistsInDictionary)\r
192 {\r
193     enchant_dict_remove(_dict, "hello", -1);\r
194     CHECK(!IsWordInDictionary("hello"));\r
195     CHECK(!dictCheckCalled);\r
196 }\r
197 \r
198 TEST_FIXTURE(EnchantDictionaryRemove_TestFixture,\r
199              EnchantDictionaryRemove_NotGivenAsSuggestion)\r
200 {\r
201     enchant_dict_remove(_dict, "aelo", -1);\r
202 \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
206 }\r
207 \r
208 TEST_FIXTURE(EnchantDictionaryRemove_TestFixture,\r
209              EnchantDictionaryRemove_ThenAddedToSession_GivenAsSuggestion)\r
210 {\r
211     enchant_dict_remove(_dict, "aelo", -1);\r
212     enchant_dict_add_to_session(_dict, "aelo", -1);\r
213 \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
217 }\r
218 \r
219 TEST_FIXTURE(EnchantDictionaryRemove_TestFixture,\r
220              EnchantDictionaryRemove_ThenAddedToSession_WhenSessionOverStillExcluded)\r
221 {\r
222     enchant_dict_remove(_dict, "aelo", -1);\r
223     enchant_dict_add_to_session(_dict, "aelo", -1);\r
224 \r
225     ReloadTestDictionary(); // to see what actually persisted\r
226 \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
230 }\r
231 \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
235  */\r
236 TEST_FIXTURE(EnchantDictionaryRemove_TestFixture,\r
237              EnchantDictionaryRemove_OnBrokerPwl_RemovedFromBrokerPwlFile)\r
238 {\r
239     CHECK(!BrokerPWLFileHasContents());\r
240     enchant_dict_add(_pwl, "personal", -1);\r
241     CHECK(BrokerPWLFileHasContents());\r
242 \r
243     enchant_dict_remove(_pwl, "personal", -1);\r
244     CHECK(!BrokerPWLFileHasContents());\r
245 }\r
246 \r
247 TEST_FIXTURE(EnchantDictionaryRemove_TestFixture,\r
248              EnchantDictionaryRemove_OnBrokerPwl_NotAddedToOtherPwlFile)\r
249 {\r
250     CHECK(!BrokerPWLFileHasContents());\r
251 \r
252     enchant_dict_remove(_pwl, "personal", -1);\r
253     CHECK(!addToExcludeCalled);\r
254     CHECK(!ExcludeFileHasContents());\r
255 }\r
256 \r
257 TEST_FIXTURE(EnchantDictionaryRemove_TestFixture,\r
258              EnchantDictionaryRemove_IsPermanent)\r
259 {\r
260     enchant_dict_remove(_dict, "hello", -1);\r
261     CHECK(!IsWordInDictionary("hello"));\r
262 \r
263     ReloadTestDictionary();\r
264 \r
265     CHECK(!IsWordInDictionary("hello"));\r
266 }\r
267 \r
268 \r
269 TEST_FIXTURE(EnchantDictionaryRemove_TestFixture, \r
270              EnchantDictionaryRemove_HasPreviousError_ErrorCleared)\r
271 {\r
272     SetErrorOnMockDictionary("something bad happened");\r
273 \r
274     enchant_dict_remove(_dict, "hello", -1);\r
275     CHECK_EQUAL((void*)NULL, (void*)enchant_dict_get_error(_dict));\r
276 }\r
277 \r
278 /////////////////////////////////////////////////////////////////////////////\r
279 // Test Error Conditions\r
280 TEST_FIXTURE(EnchantDictionaryRemove_TestFixture,\r
281              EnchantDictionaryRemove_NullDictionary_NotRemoved)\r
282 {\r
283     enchant_dict_remove(NULL, "hello", -1);\r
284     CHECK(!addToExcludeCalled);\r
285 }\r
286 \r
287 TEST_FIXTURE(EnchantDictionaryRemove_TestFixture,\r
288              EnchantDictionaryRemove_NullWord_NotRemoved)\r
289 {\r
290     enchant_dict_remove(_dict, NULL, -1);\r
291     CHECK(!addToExcludeCalled);\r
292 }\r
293 \r
294 TEST_FIXTURE(EnchantDictionaryRemove_TestFixture,\r
295              EnchantDictionaryRemove_EmptyWord_NotRemoved)\r
296 {\r
297     enchant_dict_remove(_dict, "", -1);\r
298     CHECK(!addToExcludeCalled);\r
299 }\r
300 \r
301 TEST_FIXTURE(EnchantDictionaryRemove_TestFixture,\r
302              EnchantDictionaryRemove_WordSize0_NotRemoved)\r
303 {\r
304     enchant_dict_remove(_dict, "hello", 0);\r
305     CHECK(!addToExcludeCalled);\r
306 }\r
307 \r
308 TEST_FIXTURE(EnchantDictionaryRemove_TestFixture,\r
309              EnchantDictionaryRemove_InvalidUtf8Word_NotRemoved)\r
310 {\r
311     enchant_dict_remove(_dict, "\xa5\xf1\x08", -1);\r
312     CHECK(!addToExcludeCalled);\r
313 }\r
314 \r
315 TEST_FIXTURE(EnchantDictionaryRemoveNotImplemented_TestFixture,\r
316              EnchantDictionaryRemoveNotImplemented_WordAddedToExcludeFile)\r
317 {\r
318     CHECK(!ExcludeFileHasContents());\r
319     enchant_dict_remove(_dict, "hello", -1);\r
320     CHECK(ExcludeFileHasContents());\r
321 }\r
322 \r
323 TEST_FIXTURE(EnchantDictionaryRemoveNotImplemented_TestFixture,\r
324              EnchantDictionaryRemoveNotImplemented_NotPassedOnToProvider)\r
325 {\r
326     enchant_dict_remove(_dict, "hello", -1);\r
327     CHECK(!addToExcludeCalled);\r
328 }\r
329 \r
330 TEST_FIXTURE(EnchantDictionaryRemoveNotImplemented_TestFixture,\r
331              EnchantDictionaryRemoveNotImplemented_StillExcluded)\r
332 {\r
333     CHECK(IsWordInDictionary("hello"));\r
334     enchant_dict_remove(_dict, "hello", -1);\r
335     CHECK(!IsWordInDictionary("hello"));\r
336 }\r