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 #include <UnitTest++.h>
\r
23 #include <enchant.h>
\r
24 #include "../EnchantDictionaryTestFixture.h"
\r
26 struct EnchantDictionaryStoreReplacement_TestFixture : EnchantDictionaryTestFixture
\r
28 static std::string misspelling;
\r
29 static std::string correction;
\r
31 static bool storeReplacementCalled;
\r
34 EnchantDictionaryStoreReplacement_TestFixture():
\r
35 EnchantDictionaryTestFixture(DictionaryStoreReplacement_ProviderConfiguration)
\r
37 storeReplacementCalled = false;
\r
38 misspelling.clear();
\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
47 misspelling = std::string(mis, mis_len);
\r
48 correction = std::string(cor, cor_len);
\r
49 storeReplacementCalled = true;
\r
53 MockProviderRequestStoreReplacementMockDictionary(EnchantProvider * me, const char *tag)
\r
56 EnchantDict* dict = MockProviderRequestEmptyMockDictionary(me, tag);
\r
57 dict->store_replacement = MockDictionaryStoreReplacement;
\r
61 static void DictionaryStoreReplacement_ProviderConfiguration (EnchantProvider * me, const char *)
\r
63 me->request_dict = MockProviderRequestStoreReplacementMockDictionary;
\r
64 me->dispose_dict = MockProviderDisposeDictionary;
\r
67 bool EnchantDictionaryStoreReplacement_TestFixture::storeReplacementCalled;
\r
68 std::string EnchantDictionaryStoreReplacement_TestFixture::misspelling;
\r
69 std::string EnchantDictionaryStoreReplacement_TestFixture::correction;
\r
71 struct EnchantDictionaryLacksStoreReplacement_TestFixture : EnchantDictionaryTestFixture
\r
74 EnchantDictionaryLacksStoreReplacement_TestFixture():
\r
75 EnchantDictionaryTestFixture(EmptyDictionary_ProviderConfiguration)
\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
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
95 /////////////////////////////////////////////////////////////////////////////
\r
96 // Test Normal Operation
\r
97 TEST_FIXTURE(EnchantDictionaryStoreReplacement_TestFixture,
\r
98 EnchantDictStoreReplacment_ExplicitWordLength)
\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
112 TEST_FIXTURE(EnchantDictionaryStoreReplacement_TestFixture,
\r
113 EnchantDictStoreReplacment_ComputedWordLength)
\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
120 correction.c_str(),
\r
122 CHECK(storeReplacementCalled);
\r
123 CHECK_EQUAL(EnchantDictionaryStoreReplacement_TestFixture::misspelling, misspelling);
\r
124 CHECK_EQUAL(EnchantDictionaryStoreReplacement_TestFixture::correction, correction);
\r
127 TEST_FIXTURE(EnchantDictionaryStoreReplacement_TestFixture,
\r
128 EnchantDictStoreReplacment_OnBrokerPwl)
\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
135 correction.c_str(),
\r
139 /////////////////////////////////////////////////////////////////////////////
\r
140 // Test Error Conditions
\r
141 TEST_FIXTURE(EnchantDictionaryStoreReplacement_TestFixture,
\r
142 EnchantDictStoreReplacment_NullDict_DoNothing)
\r
144 enchant_dict_store_replacement(NULL, "helo", -1, "hello", -1);
\r
145 CHECK(!storeReplacementCalled);
\r
148 TEST_FIXTURE(EnchantDictionaryStoreReplacement_TestFixture,
\r
149 EnchantDictStoreReplacment_NullMisspelling_DoNothing)
\r
151 enchant_dict_store_replacement(_dict, NULL, -1, "hello", -1);
\r
152 CHECK(!storeReplacementCalled);
\r
155 TEST_FIXTURE(EnchantDictionaryStoreReplacement_TestFixture,
\r
156 EnchantDictStoreReplacment_NullCorrection_DoNothing)
\r
158 enchant_dict_store_replacement(_dict, "helo", -1, NULL, -1);
\r
159 CHECK(!storeReplacementCalled);
\r
162 TEST_FIXTURE(EnchantDictionaryStoreReplacement_TestFixture,
\r
163 EnchantDictStoreReplacment_EmptyMisspelling_DoNothing)
\r
165 enchant_dict_store_replacement(_dict, "", -1, "hello", -1);
\r
166 CHECK(!storeReplacementCalled);
\r
169 TEST_FIXTURE(EnchantDictionaryStoreReplacement_TestFixture,
\r
170 EnchantDictStoreReplacment_EmptyCorrection_DoNothing)
\r
172 enchant_dict_store_replacement(_dict, "helo", -1, "", -1);
\r
173 CHECK(!storeReplacementCalled);
\r
176 TEST_FIXTURE(EnchantDictionaryStoreReplacement_TestFixture,
\r
177 EnchantDictStoreReplacment_ZeroMisspellingLength_DoNothing)
\r
179 enchant_dict_store_replacement(_dict, "helo", 0, "hello", -1);
\r
180 CHECK(!storeReplacementCalled);
\r
183 TEST_FIXTURE(EnchantDictionaryStoreReplacement_TestFixture,
\r
184 EnchantDictStoreReplacment_ZeroCorrectionLength_DoNothing)
\r
186 enchant_dict_store_replacement(_dict, "helo", -1, "hello", 0);
\r
187 CHECK(!storeReplacementCalled);
\r
190 TEST_FIXTURE(EnchantDictionaryStoreReplacement_TestFixture,
\r
191 EnchantDictStoreReplacement_InvalidUtf8Misspelling_DoNothing)
\r
193 enchant_dict_store_replacement(_dict, "\xa5\xf1\x08", -1, "hello", -1);
\r
194 CHECK(!storeReplacementCalled);
\r
197 TEST_FIXTURE(EnchantDictionaryStoreReplacement_TestFixture,
\r
198 EnchantDictStoreReplacement_InvalidUtf8Correction_DoNothing)
\r
200 enchant_dict_store_replacement(_dict, "helo", -1, "\xa5\xf1\x08", -1);
\r
201 CHECK(!storeReplacementCalled);
\r
204 TEST_FIXTURE(EnchantDictionaryLacksStoreReplacement_TestFixture,
\r
205 EnchantDictStoreReplacment_ProviderLacksStoreReplacement_DoNothing)
\r
208 enchant_dict_store_replacement(_dict, "helo", -1, "hello", -1);
\r
211 TEST_FIXTURE(EnchantDictionaryStoreReplacement_TestFixture,
\r
212 EnchantDictStoreReplacment_ExplicitWordLengthDoesNotCoincideWithNulTerminator)
\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
222 misspelling.resize(misspelling.size()-1);
\r
223 correction.resize(correction.size()-1);
\r
225 CHECK(storeReplacementCalled);
\r
226 CHECK_EQUAL(EnchantDictionaryStoreReplacement_TestFixture::misspelling, misspelling);
\r
227 CHECK_EQUAL(EnchantDictionaryStoreReplacement_TestFixture::correction, correction);
\r