Tizen 2.1 base
[external/enchant.git] / unittests / dictionary / enchant_dict_add_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 #include <UnitTest++.h>\r
23 #include <enchant.h>\r
24 #include "../EnchantDictionaryTestFixture.h"\r
25 \r
26 static bool addToPersonalCalled;\r
27 static std::string wordToAdd;\r
28 \r
29 static void\r
30 MockDictionaryAddToPersonal (EnchantDict * dict, const char *const word, size_t len)\r
31 {\r
32     dict;\r
33     addToPersonalCalled = true;\r
34     wordToAdd = std::string(word, len);\r
35 }\r
36 \r
37 static EnchantDict* MockProviderRequestAddToPersonalMockDictionary(EnchantProvider * me, const char *tag)\r
38 {\r
39     \r
40     EnchantDict* dict = MockProviderRequestEmptyMockDictionary(me, tag);\r
41     dict->add_to_personal = MockDictionaryAddToPersonal;\r
42     return dict;\r
43 }\r
44 \r
45 static void DictionaryAddToPersonal_ProviderConfiguration (EnchantProvider * me, const char *)\r
46 {\r
47      me->request_dict = MockProviderRequestAddToPersonalMockDictionary;\r
48      me->dispose_dict = MockProviderDisposeDictionary;\r
49 }\r
50 \r
51 struct EnchantDictionaryAdd_TestFixture : EnchantDictionaryTestFixture\r
52 {\r
53     //Setup\r
54     EnchantDictionaryAdd_TestFixture():\r
55             EnchantDictionaryTestFixture(DictionaryAddToPersonal_ProviderConfiguration)\r
56     { \r
57         addToPersonalCalled = false;\r
58     }\r
59 };\r
60 \r
61 struct EnchantDictionaryAddToPersonalNotImplemented_TestFixture : EnchantDictionaryTestFixture\r
62 {\r
63     //Setup\r
64     EnchantDictionaryAddToPersonalNotImplemented_TestFixture():\r
65             EnchantDictionaryTestFixture(EmptyDictionary_ProviderConfiguration)\r
66     { \r
67         addToPersonalCalled = false;\r
68     }\r
69 };\r
70 \r
71 /**\r
72  * enchant_dict_add\r
73  * @dict: A non-null #EnchantDict\r
74  * @word: The non-null word you wish to add to your personal dictionary, in UTF-8 encoding\r
75  * @len: The byte length of @word, or -1 for strlen (@word)\r
76  *\r
77  * Remarks: if the word exists in the exclude dictionary, it will be removed from the \r
78  *          exclude dictionary\r
79  */\r
80 \r
81 /////////////////////////////////////////////////////////////////////////////\r
82 // Test Normal Operation\r
83 TEST_FIXTURE(EnchantDictionaryAdd_TestFixture,\r
84              EnchantDictionaryAdd_WordExistsInDictionary)\r
85 {\r
86     enchant_dict_add(_dict, "hello", -1);\r
87     CHECK(IsWordInDictionary("hello"));\r
88 }\r
89 \r
90 TEST_FIXTURE(EnchantDictionaryAdd_TestFixture,\r
91              EnchantDictionaryAdd_WordExistsInSession)\r
92 {\r
93     enchant_dict_add(_dict, "hello", -1);\r
94     CHECK(IsWordInSession("hello"));\r
95 }\r
96 \r
97 TEST_FIXTURE(EnchantDictionaryAdd_TestFixture,\r
98              EnchantDictionaryAdd_WordAddedToEnchantPwlFile)\r
99 {\r
100     CHECK(!PersonalWordListFileHasContents());\r
101     enchant_dict_add(_dict, "hello", -1);\r
102     CHECK(PersonalWordListFileHasContents());\r
103 }\r
104 \r
105 TEST_FIXTURE(EnchantDictionaryAdd_TestFixture,\r
106              EnchantDictionaryAdd_PassedOnToProvider_LenComputed)\r
107 {\r
108     enchant_dict_add(_dict, "hello", -1);\r
109     CHECK(addToPersonalCalled);\r
110     CHECK_EQUAL(std::string("hello"), wordToAdd);\r
111 }\r
112 \r
113 TEST_FIXTURE(EnchantDictionaryAdd_TestFixture,\r
114              EnchantDictionaryAdd_PassedOnToProvider_LenSpecified)\r
115 {\r
116     enchant_dict_add(_dict, "hellodisregard me", 5);\r
117     CHECK(addToPersonalCalled);\r
118     CHECK_EQUAL(std::string("hello"), wordToAdd);\r
119 }\r
120 \r
121 \r
122 TEST_FIXTURE(EnchantDictionaryAdd_TestFixture,\r
123              EnchantDictionaryAdd_WordExistsInSession_StillCallsProvider)\r
124 {\r
125     enchant_dict_add_to_session(_dict, "session", -1);\r
126     CHECK(!addToPersonalCalled);\r
127 \r
128     enchant_dict_add(_dict, "session", -1);\r
129     CHECK(addToPersonalCalled);\r
130     CHECK_EQUAL(std::string("session"), wordToAdd);\r
131 }\r
132 \r
133 TEST_FIXTURE(EnchantDictionaryAdd_TestFixture,\r
134              EnchantDictionaryAdd_WordExistsInPersonal_StillCallsProvider)\r
135 {\r
136     enchant_dict_add(_dict, "personal", -1);\r
137     addToPersonalCalled=false;\r
138     wordToAdd = std::string();\r
139 \r
140     enchant_dict_add(_dict, "personal", -1);\r
141     CHECK(addToPersonalCalled);\r
142     CHECK_EQUAL(std::string("personal"), wordToAdd);\r
143 }\r
144 \r
145 TEST_FIXTURE(EnchantDictionaryAdd_TestFixture,\r
146              EnchantDictionaryAdd_WordExistsInExclude_RemovedFromExcludeAddedToPersonal)\r
147 {\r
148     enchant_dict_remove(_dict, "personal", -1);\r
149     CHECK(ExcludeFileHasContents());\r
150     enchant_dict_add(_dict, "personal", -1);\r
151     CHECK(!ExcludeFileHasContents());\r
152     CHECK(PersonalWordListFileHasContents());\r
153 }\r
154 \r
155 TEST_FIXTURE(EnchantDictionaryAdd_TestFixture,\r
156              EnchantDictionaryAdd_InBrokerPwl)\r
157 {\r
158     enchant_dict_add(_pwl, "personal", -1);\r
159     CHECK(!addToPersonalCalled);\r
160     CHECK(!PersonalWordListFileHasContents());\r
161 }\r
162 \r
163 TEST_FIXTURE(EnchantDictionaryAdd_TestFixture,\r
164              EnchantDictionaryAdd_IsPermanent)\r
165 {\r
166     enchant_dict_add(_dict, "hello", -1);\r
167     CHECK(IsWordInDictionary("hello"));\r
168 \r
169     ReloadTestDictionary();\r
170 \r
171     CHECK(IsWordInDictionary("hello"));\r
172 }\r
173 \r
174 \r
175 TEST_FIXTURE(EnchantDictionaryAdd_TestFixture, \r
176              EnchantDictionaryAdd_HasPreviousError_ErrorCleared)\r
177 {\r
178     SetErrorOnMockDictionary("something bad happened");\r
179 \r
180     enchant_dict_add(_dict, "hello", -1);\r
181     CHECK_EQUAL((void*)NULL, (void*)enchant_dict_get_error(_dict));\r
182 }\r
183 \r
184 /////////////////////////////////////////////////////////////////////////////\r
185 // Test Error Conditions\r
186 TEST_FIXTURE(EnchantDictionaryAdd_TestFixture,\r
187              EnchantDictionaryAdd_NullDictionary_NotAdded)\r
188 {\r
189     enchant_dict_add(NULL, "hello", -1);\r
190     CHECK(!addToPersonalCalled);\r
191 }\r
192 \r
193 TEST_FIXTURE(EnchantDictionaryAdd_TestFixture,\r
194              EnchantDictionaryAdd_NullWord_NotAdded)\r
195 {\r
196     enchant_dict_add(_dict, NULL, -1);\r
197     CHECK(!addToPersonalCalled);\r
198 }\r
199 \r
200 TEST_FIXTURE(EnchantDictionaryAdd_TestFixture,\r
201              EnchantDictionaryAdd_EmptyWord_NotAdded)\r
202 {\r
203     enchant_dict_add(_dict, "", -1);\r
204     CHECK(!addToPersonalCalled);\r
205 }\r
206 \r
207 TEST_FIXTURE(EnchantDictionaryAdd_TestFixture,\r
208              EnchantDictionaryAdd_WordSize0_NotAdded)\r
209 {\r
210     enchant_dict_add(_dict, "hello", 0);\r
211     CHECK(!addToPersonalCalled);\r
212 }\r
213 \r
214 TEST_FIXTURE(EnchantDictionaryAdd_TestFixture,\r
215              EnchantDictionaryAdd_InvalidUtf8Word_NotAdded)\r
216 {\r
217     enchant_dict_add(_dict, "\xa5\xf1\x08", -1);\r
218     CHECK(!addToPersonalCalled);\r
219 }\r
220 \r
221 TEST_FIXTURE(EnchantDictionaryAddToPersonalNotImplemented_TestFixture,\r
222              EnchantDictionaryAddToPersonalNotImplemented_WordAddedToEnchantPwlFile)\r
223 {\r
224     CHECK(!PersonalWordListFileHasContents());\r
225     enchant_dict_add(_dict, "hello", -1);\r
226     CHECK(PersonalWordListFileHasContents());\r
227 }\r
228 \r
229 TEST_FIXTURE(EnchantDictionaryAddToPersonalNotImplemented_TestFixture,\r
230              EnchantDictionaryAddToPersonalNotImplemented_NotPassedOnToProvider)\r
231 {\r
232     enchant_dict_add(_dict, "hello", -1);\r
233     CHECK(!addToPersonalCalled);\r
234 }\r