Tizen 2.1 base
[external/enchant.git] / unittests / dictionary / enchant_dict_store_replacement_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 struct EnchantDictionaryStoreReplacement_TestFixture : EnchantDictionaryTestFixture\r
27 {\r
28     static std::string misspelling;\r
29     static std::string correction;\r
30 \r
31     static bool storeReplacementCalled;\r
32 \r
33     //Setup\r
34     EnchantDictionaryStoreReplacement_TestFixture():\r
35             EnchantDictionaryTestFixture(DictionaryStoreReplacement_ProviderConfiguration)\r
36     { \r
37         storeReplacementCalled = false;\r
38         misspelling.clear();\r
39         correction.clear();\r
40     }\r
41 \r
42     static void\r
43     MockDictionaryStoreReplacement (EnchantDict *,\r
44                                        const char *const mis, size_t mis_len,\r
45                                        const char *const cor, size_t cor_len)\r
46     {\r
47         misspelling = std::string(mis, mis_len);\r
48         correction = std::string(cor, cor_len);\r
49         storeReplacementCalled = true;\r
50     }\r
51 \r
52     static EnchantDict*\r
53     MockProviderRequestStoreReplacementMockDictionary(EnchantProvider * me, const char *tag)\r
54     {\r
55         \r
56         EnchantDict* dict = MockProviderRequestEmptyMockDictionary(me, tag);\r
57         dict->store_replacement = MockDictionaryStoreReplacement;\r
58         return dict;\r
59     }\r
60 \r
61     static void DictionaryStoreReplacement_ProviderConfiguration (EnchantProvider * me, const char *)\r
62     {\r
63          me->request_dict = MockProviderRequestStoreReplacementMockDictionary;\r
64          me->dispose_dict = MockProviderDisposeDictionary;\r
65     }\r
66 };\r
67 bool EnchantDictionaryStoreReplacement_TestFixture::storeReplacementCalled;\r
68 std::string EnchantDictionaryStoreReplacement_TestFixture::misspelling;\r
69 std::string EnchantDictionaryStoreReplacement_TestFixture::correction;\r
70 \r
71 struct EnchantDictionaryLacksStoreReplacement_TestFixture : EnchantDictionaryTestFixture\r
72 {\r
73     //Setup\r
74     EnchantDictionaryLacksStoreReplacement_TestFixture():\r
75             EnchantDictionaryTestFixture(EmptyDictionary_ProviderConfiguration)\r
76     { }\r
77 };\r
78 \r
79 \r
80 \r
81 \r
82 /**\r
83  * enchant_dict_store_replacement\r
84  * @dict: A non-null #EnchantDict\r
85  * @mis: The non-null word you wish to add a correction for, in UTF-8 encoding\r
86  * @mis_len: The byte length of @mis, or -1 for strlen (@mis)\r
87  * @cor: The non-null correction word, in UTF-8 encoding\r
88  * @cor_len: The byte length of @cor, or -1 for strlen (@cor)\r
89  *\r
90  * Notes that you replaced @mis with @cor, so it's possibly more likely\r
91  * that future occurrences of @mis will be replaced with @cor. So it might\r
92  * bump @cor up in the suggestion list.\r
93  */\r
94 \r
95 /////////////////////////////////////////////////////////////////////////////\r
96 // Test Normal Operation\r
97 TEST_FIXTURE(EnchantDictionaryStoreReplacement_TestFixture,\r
98              EnchantDictStoreReplacment_ExplicitWordLength)\r
99 {\r
100     std::string misspelling("helo");\r
101     std::string correction("hello");\r
102     enchant_dict_store_replacement(_dict, \r
103                                    misspelling.c_str(), \r
104                                    misspelling.size(), \r
105                                    correction.c_str(),\r
106                                    correction.size());\r
107     CHECK(storeReplacementCalled);\r
108     CHECK_EQUAL(EnchantDictionaryStoreReplacement_TestFixture::misspelling, misspelling);\r
109     CHECK_EQUAL(EnchantDictionaryStoreReplacement_TestFixture::correction, correction);\r
110 }\r
111 \r
112 TEST_FIXTURE(EnchantDictionaryStoreReplacement_TestFixture,\r
113              EnchantDictStoreReplacment_ComputedWordLength)\r
114 {\r
115     std::string misspelling("helo");\r
116     std::string correction("hello");\r
117     enchant_dict_store_replacement(_dict, \r
118                                    misspelling.c_str(), \r
119                                    -1, \r
120                                    correction.c_str(),\r
121                                    -1);\r
122     CHECK(storeReplacementCalled);\r
123     CHECK_EQUAL(EnchantDictionaryStoreReplacement_TestFixture::misspelling, misspelling);\r
124     CHECK_EQUAL(EnchantDictionaryStoreReplacement_TestFixture::correction, correction);\r
125 }\r
126 \r
127 TEST_FIXTURE(EnchantDictionaryStoreReplacement_TestFixture,\r
128              EnchantDictStoreReplacment_OnBrokerPwl)\r
129 {\r
130     std::string misspelling("helo");\r
131     std::string correction("hello");\r
132     enchant_dict_store_replacement(_pwl, \r
133                                    misspelling.c_str(), \r
134                                    -1, \r
135                                    correction.c_str(),\r
136                                    -1);\r
137 }\r
138 \r
139 /////////////////////////////////////////////////////////////////////////////\r
140 // Test Error Conditions\r
141 TEST_FIXTURE(EnchantDictionaryStoreReplacement_TestFixture,\r
142              EnchantDictStoreReplacment_NullDict_DoNothing)\r
143 {\r
144     enchant_dict_store_replacement(NULL, "helo", -1, "hello", -1);\r
145     CHECK(!storeReplacementCalled);\r
146 }\r
147 \r
148 TEST_FIXTURE(EnchantDictionaryStoreReplacement_TestFixture,\r
149              EnchantDictStoreReplacment_NullMisspelling_DoNothing)\r
150 {\r
151     enchant_dict_store_replacement(_dict, NULL, -1, "hello", -1);\r
152     CHECK(!storeReplacementCalled);\r
153 }\r
154 \r
155 TEST_FIXTURE(EnchantDictionaryStoreReplacement_TestFixture,\r
156              EnchantDictStoreReplacment_NullCorrection_DoNothing)\r
157 {\r
158     enchant_dict_store_replacement(_dict, "helo", -1, NULL, -1);\r
159     CHECK(!storeReplacementCalled);\r
160 }\r
161 \r
162 TEST_FIXTURE(EnchantDictionaryStoreReplacement_TestFixture,\r
163              EnchantDictStoreReplacment_EmptyMisspelling_DoNothing)\r
164 {\r
165     enchant_dict_store_replacement(_dict, "", -1, "hello", -1);\r
166     CHECK(!storeReplacementCalled);\r
167 }\r
168 \r
169 TEST_FIXTURE(EnchantDictionaryStoreReplacement_TestFixture,\r
170              EnchantDictStoreReplacment_EmptyCorrection_DoNothing)\r
171 {\r
172     enchant_dict_store_replacement(_dict, "helo", -1, "", -1);\r
173     CHECK(!storeReplacementCalled);\r
174 }\r
175 \r
176 TEST_FIXTURE(EnchantDictionaryStoreReplacement_TestFixture,\r
177              EnchantDictStoreReplacment_ZeroMisspellingLength_DoNothing)\r
178 {\r
179     enchant_dict_store_replacement(_dict, "helo", 0, "hello", -1);\r
180     CHECK(!storeReplacementCalled);\r
181 }\r
182 \r
183 TEST_FIXTURE(EnchantDictionaryStoreReplacement_TestFixture,\r
184              EnchantDictStoreReplacment_ZeroCorrectionLength_DoNothing)\r
185 {\r
186     enchant_dict_store_replacement(_dict, "helo", -1, "hello", 0);\r
187     CHECK(!storeReplacementCalled);\r
188 }\r
189 \r
190 TEST_FIXTURE(EnchantDictionaryStoreReplacement_TestFixture,\r
191              EnchantDictStoreReplacement_InvalidUtf8Misspelling_DoNothing)\r
192 {\r
193     enchant_dict_store_replacement(_dict, "\xa5\xf1\x08", -1, "hello", -1);\r
194     CHECK(!storeReplacementCalled);\r
195 }\r
196 \r
197 TEST_FIXTURE(EnchantDictionaryStoreReplacement_TestFixture,\r
198              EnchantDictStoreReplacement_InvalidUtf8Correction_DoNothing)\r
199 {\r
200     enchant_dict_store_replacement(_dict, "helo", -1, "\xa5\xf1\x08", -1);\r
201     CHECK(!storeReplacementCalled);\r
202 }\r
203 \r
204 TEST_FIXTURE(EnchantDictionaryLacksStoreReplacement_TestFixture,\r
205              EnchantDictStoreReplacment_ProviderLacksStoreReplacement_DoNothing)\r
206 {\r
207     testResults_;\r
208     enchant_dict_store_replacement(_dict, "helo", -1, "hello", -1);\r
209 }\r
210 \r
211 TEST_FIXTURE(EnchantDictionaryStoreReplacement_TestFixture,\r
212              EnchantDictStoreReplacment_ExplicitWordLengthDoesNotCoincideWithNulTerminator)\r
213 {\r
214     std::string misspelling("helo1");\r
215     std::string correction("hello1");\r
216     enchant_dict_store_replacement(_dict, \r
217                                    misspelling.c_str(), \r
218                                    misspelling.size()-1, \r
219                                    correction.c_str(),\r
220                                    correction.size()-1);\r
221 \r
222     misspelling.resize(misspelling.size()-1);\r
223     correction.resize(correction.size()-1);\r
224 \r
225     CHECK(storeReplacementCalled);\r
226     CHECK_EQUAL(EnchantDictionaryStoreReplacement_TestFixture::misspelling, misspelling);\r
227     CHECK_EQUAL(EnchantDictionaryStoreReplacement_TestFixture::correction, correction);\r
228 }\r