Tizen 2.1 base
[external/enchant.git] / unittests / dictionary / enchant_dict_check_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 dictCheckCalled;\r
27 \r
28 static int\r
29 MockDictionaryCheck (EnchantDict * dict, const char *const word, size_t len)\r
30 {\r
31     dict;\r
32     dictCheckCalled = true;\r
33     if(strncmp("hello", word, len)==0)\r
34     {\r
35         return 0; //good word\r
36     }\r
37     return 1; // bad word\r
38 }\r
39 \r
40 static EnchantDict* MockProviderRequestCheckMockDictionary(EnchantProvider * me, const char *tag)\r
41 {\r
42     \r
43     EnchantDict* dict = MockProviderRequestEmptyMockDictionary(me, tag);\r
44     dict->check = MockDictionaryCheck;\r
45     return dict;\r
46 }\r
47 \r
48 \r
49 static void DictionaryCheck_ProviderConfiguration (EnchantProvider * me, const char *)\r
50 {\r
51      me->request_dict = MockProviderRequestCheckMockDictionary;\r
52      me->dispose_dict = MockProviderDisposeDictionary;\r
53 }\r
54 \r
55 \r
56 struct EnchantDictionaryCheck_TestFixture : EnchantDictionaryTestFixture\r
57 {\r
58     //Setup\r
59     EnchantDictionaryCheck_TestFixture():\r
60             EnchantDictionaryTestFixture(DictionaryCheck_ProviderConfiguration)\r
61     { \r
62         dictCheckCalled = false;\r
63     }\r
64 };\r
65 \r
66 struct EnchantDictionaryCheckNotImplemented_TestFixture : EnchantDictionaryTestFixture\r
67 {\r
68     //Setup\r
69     EnchantDictionaryCheckNotImplemented_TestFixture():\r
70             EnchantDictionaryTestFixture(EmptyDictionary_ProviderConfiguration)\r
71     { \r
72         dictCheckCalled = false;\r
73     }\r
74 };\r
75 \r
76 \r
77 /**\r
78  * enchant_dict_check\r
79  * @dict: A non-null #EnchantDict\r
80  * @word: The non-null word you wish to correct, in UTF-8 encoding\r
81  * @len: The byte length of @word, or -1 for strlen (@word)\r
82  *\r
83  * Will return an "incorrect" value if any of those pre-conditions\r
84  * are not met.\r
85  *\r
86  * Returns: 0 if the word is correctly spelled, positive if not, negative if error\r
87  */\r
88 \r
89 /////////////////////////////////////////////////////////////////////////////\r
90 // Test Normal Operation\r
91 TEST_FIXTURE(EnchantDictionaryCheck_TestFixture,\r
92              EnchantDictionaryCheck_WordExists_LenComputed_0)\r
93 {\r
94     CHECK_EQUAL(0, enchant_dict_check(_dict, "hello", -1));\r
95     CHECK(dictCheckCalled);\r
96 }\r
97 \r
98 TEST_FIXTURE(EnchantDictionaryCheck_TestFixture,\r
99              EnchantDictionaryCheck_WordExists_LenSpecified_0)\r
100 {\r
101     CHECK_EQUAL(0, enchant_dict_check(_dict, "hellodisregard me", 5));\r
102     CHECK(dictCheckCalled);\r
103 }\r
104 \r
105 TEST_FIXTURE(EnchantDictionaryCheck_TestFixture,\r
106              EnchantDictionaryCheck_WordDoesNotExist_LenComputed_1)\r
107 {\r
108     CHECK_EQUAL(1, enchant_dict_check(_dict, "helo", -1));\r
109     CHECK(dictCheckCalled);\r
110 }\r
111 \r
112 TEST_FIXTURE(EnchantDictionaryCheck_TestFixture,\r
113              EnchantDictionaryCheck_WordDoesNotExist_LenSpecified_1)\r
114 {\r
115     CHECK_EQUAL(1, enchant_dict_check(_dict, "helodisregard me", 4));\r
116     CHECK(dictCheckCalled);\r
117 }\r
118 \r
119 TEST_FIXTURE(EnchantDictionaryCheck_TestFixture,\r
120              EnchantDictionaryCheck_WordExistsInSession_0_DoesNotCallProvider)\r
121 {\r
122     enchant_dict_add_to_session(_dict, "session", -1);\r
123     CHECK_EQUAL(0, enchant_dict_check(_dict, "session", -1));\r
124     CHECK(!dictCheckCalled);\r
125 }\r
126 \r
127 TEST_FIXTURE(EnchantDictionaryCheck_TestFixture,\r
128              EnchantDictionaryCheck_WordExistsInPersonal_0_DoesNotCallProvider)\r
129 {\r
130     enchant_dict_add(_dict, "personal", -1);\r
131 \r
132     CHECK_EQUAL(0, enchant_dict_check(_dict, "personal", -1));\r
133     CHECK(!dictCheckCalled);\r
134 }\r
135 \r
136 TEST_FIXTURE(EnchantDictionaryCheck_TestFixture,\r
137              EnchantDictionaryCheck_WordDoesExists_InBrokerPwlSession_0)\r
138 {\r
139     enchant_dict_add_to_session(_pwl, "session", -1);\r
140     CHECK_EQUAL(0, enchant_dict_check(_pwl, "session", -1));\r
141 }\r
142 \r
143 TEST_FIXTURE(EnchantDictionaryCheck_TestFixture,\r
144              EnchantDictionaryCheck_WordDoesExists_InBrokerPwl_0)\r
145 {\r
146     enchant_dict_add(_pwl, "personal", -1);\r
147     CHECK_EQUAL(0, enchant_dict_check(_pwl, "personal", -1));\r
148 }\r
149 \r
150 TEST_FIXTURE(EnchantDictionaryCheck_TestFixture,\r
151              EnchantDictionaryCheck_WordDoesNotExist_InBrokerPwl_1)\r
152 {\r
153     CHECK_EQUAL(1, enchant_dict_check(_pwl, "session", -1));\r
154 }\r
155 \r
156 TEST_FIXTURE(EnchantDictionaryCheck_TestFixture, \r
157              EnchantDictionaryCheck_HasPreviousError_ErrorCleared)\r
158 {\r
159     SetErrorOnMockDictionary("something bad happened");\r
160 \r
161     enchant_dict_check(_dict, "hello", -1);\r
162     CHECK_EQUAL((void*)NULL, (void*)enchant_dict_get_error(_dict));\r
163 }\r
164 \r
165 /////////////////////////////////////////////////////////////////////////////\r
166 // Test Error Conditions\r
167 TEST_FIXTURE(EnchantDictionaryCheck_TestFixture,\r
168              EnchantDictionaryCheck_NullDictionary_Negative1)\r
169 {\r
170     CHECK_EQUAL(-1, enchant_dict_check(NULL, "helo", -1));\r
171     CHECK(!dictCheckCalled);\r
172 }\r
173 \r
174 TEST_FIXTURE(EnchantDictionaryCheck_TestFixture,\r
175              EnchantDictionaryCheck_NullWord_Negative1)\r
176 {\r
177     CHECK_EQUAL(-1, enchant_dict_check(_dict, NULL, -1));\r
178     CHECK(!dictCheckCalled);\r
179 }\r
180 \r
181 TEST_FIXTURE(EnchantDictionaryCheck_TestFixture,\r
182              EnchantDictionaryCheck_EmptyWord_Negative1)\r
183 {\r
184     CHECK_EQUAL(-1, enchant_dict_check(_dict, "", -1));\r
185     CHECK(!dictCheckCalled);\r
186 }\r
187 \r
188 TEST_FIXTURE(EnchantDictionaryCheck_TestFixture,\r
189              EnchantDictionaryCheck_WordSize0_Negative1)\r
190 {\r
191     CHECK_EQUAL(-1, enchant_dict_check(_dict, "helo", 0));\r
192     CHECK(!dictCheckCalled);\r
193 }\r
194 \r
195 TEST_FIXTURE(EnchantDictionaryCheck_TestFixture,\r
196              EnchantDictionaryCheck_InvalidUtf8Word_Negative1)\r
197 {\r
198     CHECK_EQUAL(-1, enchant_dict_check(_dict, "\xa5\xf1\x08", -1));\r
199     CHECK(!dictCheckCalled);\r
200 }\r
201 \r
202 TEST_FIXTURE(EnchantDictionaryCheckNotImplemented_TestFixture,\r
203              EnchantDictionaryCheckNotImplemented_Negative1)\r
204 {\r
205     CHECK_EQUAL(-1, enchant_dict_check(_dict, "hello", -1));\r
206     CHECK(!dictCheckCalled);\r
207 }\r
208 \r
209 TEST_FIXTURE(EnchantDictionaryCheckNotImplemented_TestFixture,\r
210              EnchantDictionaryCheckNotImplemented_InBrokerPwl_1)\r
211 {\r
212     CHECK_EQUAL(1, enchant_dict_check(_pwl, "hello", -1));\r
213     CHECK(!dictCheckCalled);\r
214 }\r
215 \r
216 TEST_FIXTURE(EnchantDictionaryCheck_TestFixture,\r
217              EnchantDictionaryCheck_WordInDictionaryAndExclude_1)\r
218 {\r
219     ExternalAddWordToExclude("hello");\r
220     ExternalAddWordToDictionary("hello");\r
221 \r
222         ReloadTestDictionary();\r
223         CHECK_EQUAL(1, enchant_dict_check(_dict, "hello", -1));\r
224 }\r