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 static bool dictCheckCalled;
\r
29 MockDictionaryCheck (EnchantDict * dict, const char *const word, size_t len)
\r
32 dictCheckCalled = true;
\r
33 if(strncmp("hello", word, len)==0)
\r
35 return 0; //good word
\r
37 return 1; // bad word
\r
40 static EnchantDict* MockProviderRequestCheckMockDictionary(EnchantProvider * me, const char *tag)
\r
43 EnchantDict* dict = MockProviderRequestEmptyMockDictionary(me, tag);
\r
44 dict->check = MockDictionaryCheck;
\r
49 static void DictionaryCheck_ProviderConfiguration (EnchantProvider * me, const char *)
\r
51 me->request_dict = MockProviderRequestCheckMockDictionary;
\r
52 me->dispose_dict = MockProviderDisposeDictionary;
\r
56 struct EnchantDictionaryCheck_TestFixture : EnchantDictionaryTestFixture
\r
59 EnchantDictionaryCheck_TestFixture():
\r
60 EnchantDictionaryTestFixture(DictionaryCheck_ProviderConfiguration)
\r
62 dictCheckCalled = false;
\r
66 struct EnchantDictionaryCheckNotImplemented_TestFixture : EnchantDictionaryTestFixture
\r
69 EnchantDictionaryCheckNotImplemented_TestFixture():
\r
70 EnchantDictionaryTestFixture(EmptyDictionary_ProviderConfiguration)
\r
72 dictCheckCalled = false;
\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
83 * Will return an "incorrect" value if any of those pre-conditions
\r
86 * Returns: 0 if the word is correctly spelled, positive if not, negative if error
\r
89 /////////////////////////////////////////////////////////////////////////////
\r
90 // Test Normal Operation
\r
91 TEST_FIXTURE(EnchantDictionaryCheck_TestFixture,
\r
92 EnchantDictionaryCheck_WordExists_LenComputed_0)
\r
94 CHECK_EQUAL(0, enchant_dict_check(_dict, "hello", -1));
\r
95 CHECK(dictCheckCalled);
\r
98 TEST_FIXTURE(EnchantDictionaryCheck_TestFixture,
\r
99 EnchantDictionaryCheck_WordExists_LenSpecified_0)
\r
101 CHECK_EQUAL(0, enchant_dict_check(_dict, "hellodisregard me", 5));
\r
102 CHECK(dictCheckCalled);
\r
105 TEST_FIXTURE(EnchantDictionaryCheck_TestFixture,
\r
106 EnchantDictionaryCheck_WordDoesNotExist_LenComputed_1)
\r
108 CHECK_EQUAL(1, enchant_dict_check(_dict, "helo", -1));
\r
109 CHECK(dictCheckCalled);
\r
112 TEST_FIXTURE(EnchantDictionaryCheck_TestFixture,
\r
113 EnchantDictionaryCheck_WordDoesNotExist_LenSpecified_1)
\r
115 CHECK_EQUAL(1, enchant_dict_check(_dict, "helodisregard me", 4));
\r
116 CHECK(dictCheckCalled);
\r
119 TEST_FIXTURE(EnchantDictionaryCheck_TestFixture,
\r
120 EnchantDictionaryCheck_WordExistsInSession_0_DoesNotCallProvider)
\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
127 TEST_FIXTURE(EnchantDictionaryCheck_TestFixture,
\r
128 EnchantDictionaryCheck_WordExistsInPersonal_0_DoesNotCallProvider)
\r
130 enchant_dict_add(_dict, "personal", -1);
\r
132 CHECK_EQUAL(0, enchant_dict_check(_dict, "personal", -1));
\r
133 CHECK(!dictCheckCalled);
\r
136 TEST_FIXTURE(EnchantDictionaryCheck_TestFixture,
\r
137 EnchantDictionaryCheck_WordDoesExists_InBrokerPwlSession_0)
\r
139 enchant_dict_add_to_session(_pwl, "session", -1);
\r
140 CHECK_EQUAL(0, enchant_dict_check(_pwl, "session", -1));
\r
143 TEST_FIXTURE(EnchantDictionaryCheck_TestFixture,
\r
144 EnchantDictionaryCheck_WordDoesExists_InBrokerPwl_0)
\r
146 enchant_dict_add(_pwl, "personal", -1);
\r
147 CHECK_EQUAL(0, enchant_dict_check(_pwl, "personal", -1));
\r
150 TEST_FIXTURE(EnchantDictionaryCheck_TestFixture,
\r
151 EnchantDictionaryCheck_WordDoesNotExist_InBrokerPwl_1)
\r
153 CHECK_EQUAL(1, enchant_dict_check(_pwl, "session", -1));
\r
156 TEST_FIXTURE(EnchantDictionaryCheck_TestFixture,
\r
157 EnchantDictionaryCheck_HasPreviousError_ErrorCleared)
\r
159 SetErrorOnMockDictionary("something bad happened");
\r
161 enchant_dict_check(_dict, "hello", -1);
\r
162 CHECK_EQUAL((void*)NULL, (void*)enchant_dict_get_error(_dict));
\r
165 /////////////////////////////////////////////////////////////////////////////
\r
166 // Test Error Conditions
\r
167 TEST_FIXTURE(EnchantDictionaryCheck_TestFixture,
\r
168 EnchantDictionaryCheck_NullDictionary_Negative1)
\r
170 CHECK_EQUAL(-1, enchant_dict_check(NULL, "helo", -1));
\r
171 CHECK(!dictCheckCalled);
\r
174 TEST_FIXTURE(EnchantDictionaryCheck_TestFixture,
\r
175 EnchantDictionaryCheck_NullWord_Negative1)
\r
177 CHECK_EQUAL(-1, enchant_dict_check(_dict, NULL, -1));
\r
178 CHECK(!dictCheckCalled);
\r
181 TEST_FIXTURE(EnchantDictionaryCheck_TestFixture,
\r
182 EnchantDictionaryCheck_EmptyWord_Negative1)
\r
184 CHECK_EQUAL(-1, enchant_dict_check(_dict, "", -1));
\r
185 CHECK(!dictCheckCalled);
\r
188 TEST_FIXTURE(EnchantDictionaryCheck_TestFixture,
\r
189 EnchantDictionaryCheck_WordSize0_Negative1)
\r
191 CHECK_EQUAL(-1, enchant_dict_check(_dict, "helo", 0));
\r
192 CHECK(!dictCheckCalled);
\r
195 TEST_FIXTURE(EnchantDictionaryCheck_TestFixture,
\r
196 EnchantDictionaryCheck_InvalidUtf8Word_Negative1)
\r
198 CHECK_EQUAL(-1, enchant_dict_check(_dict, "\xa5\xf1\x08", -1));
\r
199 CHECK(!dictCheckCalled);
\r
202 TEST_FIXTURE(EnchantDictionaryCheckNotImplemented_TestFixture,
\r
203 EnchantDictionaryCheckNotImplemented_Negative1)
\r
205 CHECK_EQUAL(-1, enchant_dict_check(_dict, "hello", -1));
\r
206 CHECK(!dictCheckCalled);
\r
209 TEST_FIXTURE(EnchantDictionaryCheckNotImplemented_TestFixture,
\r
210 EnchantDictionaryCheckNotImplemented_InBrokerPwl_1)
\r
212 CHECK_EQUAL(1, enchant_dict_check(_pwl, "hello", -1));
\r
213 CHECK(!dictCheckCalled);
\r
216 TEST_FIXTURE(EnchantDictionaryCheck_TestFixture,
\r
217 EnchantDictionaryCheck_WordInDictionaryAndExclude_1)
\r
219 ExternalAddWordToExclude("hello");
\r
220 ExternalAddWordToDictionary("hello");
\r
222 ReloadTestDictionary();
\r
223 CHECK_EQUAL(1, enchant_dict_check(_dict, "hello", -1));
\r