Tizen 2.1 base
[external/enchant.git] / unittests / dictionary / enchant_dict_add_to_session_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 addToSessionCalled;\r
27 static std::string wordToAdd;\r
28 \r
29 static void\r
30 MockDictionaryAddToSession (EnchantDict * dict, const char *const word, size_t len)\r
31 {\r
32     dict;\r
33     addToSessionCalled = true;\r
34     wordToAdd = std::string(word, len);\r
35 }\r
36 \r
37 static EnchantDict* MockProviderRequestAddToSessionMockDictionary(EnchantProvider * me, const char *tag)\r
38 {\r
39     \r
40     EnchantDict* dict = MockProviderRequestEmptyMockDictionary(me, tag);\r
41     dict->add_to_session = MockDictionaryAddToSession;\r
42     return dict;\r
43 }\r
44 \r
45 static void DictionaryAddToSession_ProviderConfiguration (EnchantProvider * me, const char *)\r
46 {\r
47      me->request_dict = MockProviderRequestAddToSessionMockDictionary;\r
48      me->dispose_dict = MockProviderDisposeDictionary;\r
49 }\r
50 \r
51 struct EnchantDictionaryAddToSession_TestFixture : EnchantDictionaryTestFixture\r
52 {\r
53     //Setup\r
54     EnchantDictionaryAddToSession_TestFixture():\r
55             EnchantDictionaryTestFixture(DictionaryAddToSession_ProviderConfiguration)\r
56     { \r
57         addToSessionCalled = false;\r
58     }\r
59 };\r
60 \r
61 struct EnchantDictionaryAddToSessionNotImplemented_TestFixture : EnchantDictionaryTestFixture\r
62 {\r
63     //Setup\r
64     EnchantDictionaryAddToSessionNotImplemented_TestFixture():\r
65             EnchantDictionaryTestFixture(EmptyDictionary_ProviderConfiguration)\r
66     { \r
67         addToSessionCalled = false;\r
68     }\r
69 };\r
70 /**\r
71  * enchant_dict_add_to_session\r
72  * @dict: A non-null #EnchantDict\r
73  * @word: The non-null word you wish to add to this spell-checking session, in UTF-8 encoding\r
74  * @len: The byte length of @word, or -1 for strlen (@word)\r
75  *\r
76  */\r
77 \r
78 /////////////////////////////////////////////////////////////////////////////\r
79 // Test Normal Operation\r
80 TEST_FIXTURE(EnchantDictionaryAddToSession_TestFixture,\r
81              EnchantDictionaryAddToSession_WordNotAddedToEnchantPwlFile)\r
82 {\r
83     enchant_dict_add_to_session(_dict, "hello", -1);\r
84     CHECK(!PersonalWordListFileHasContents());\r
85 }\r
86 \r
87 TEST_FIXTURE(EnchantDictionaryAddToSession_TestFixture,\r
88              EnchantDictionaryAddToSession_PassedOnToProvider_LenComputed)\r
89 {\r
90     enchant_dict_add_to_session(_dict, "hello", -1);\r
91     CHECK(addToSessionCalled);\r
92     CHECK_EQUAL(std::string("hello"), wordToAdd);\r
93 }\r
94 \r
95 TEST_FIXTURE(EnchantDictionaryAddToSession_TestFixture,\r
96              EnchantDictionaryAddToSession_PassedOnToProvider_LenSpecified)\r
97 {\r
98     enchant_dict_add_to_session(_dict, "hellodisregard me", 5);\r
99     CHECK(addToSessionCalled);\r
100     CHECK_EQUAL(std::string("hello"), wordToAdd);\r
101 }\r
102 \r
103 TEST_FIXTURE(EnchantDictionaryAddToSession_TestFixture,\r
104              EnchantDictionaryAddToSession_WordExistsInSession_StillCallsProvider)\r
105 {\r
106     enchant_dict_add_to_session(_dict, "session", -1);\r
107     addToSessionCalled=false;\r
108     wordToAdd = std::string();\r
109 \r
110     enchant_dict_add_to_session(_dict, "session", -1);\r
111     CHECK(addToSessionCalled);\r
112     CHECK_EQUAL(std::string("session"), wordToAdd);\r
113 }\r
114 \r
115 TEST_FIXTURE(EnchantDictionaryAddToSession_TestFixture,\r
116              EnchantDictionaryAddToSession_WordExistsInPersonal_StillCallsProvider)\r
117 {\r
118     enchant_dict_add(_dict, "personal", -1);\r
119     CHECK(!addToSessionCalled);\r
120 \r
121     enchant_dict_add_to_session(_dict, "personal", -1);\r
122     CHECK(addToSessionCalled);\r
123     CHECK_EQUAL(std::string("personal"), wordToAdd);\r
124 }\r
125 \r
126 TEST_FIXTURE(EnchantDictionaryAddToSession_TestFixture,\r
127              EnchantDictionaryAddToSession_WordExistsInExclude_AddedToSessionNotRemovedFromExcludeFile)\r
128 {\r
129     enchant_dict_remove(_dict, "personal", -1);\r
130 \r
131     enchant_dict_add_to_session(_dict, "personal", -1);\r
132     CHECK(IsWordInDictionary("personal"));\r
133     CHECK(ExcludeFileHasContents());\r
134 }\r
135 \r
136 \r
137 TEST_FIXTURE(EnchantDictionaryAddToSession_TestFixture,\r
138              EnchantDictionaryAddToSession_WordAddedToSession)\r
139 {\r
140     enchant_dict_add_to_session(_dict, "hello", -1);\r
141     CHECK(IsWordInSession("hello"));\r
142 }\r
143 \r
144 TEST_FIXTURE(EnchantDictionaryAddToSession_TestFixture,\r
145              EnchantDictionaryAddToSession_InBrokerSession)\r
146 {\r
147     enchant_dict_add_to_session(_pwl, "personal", -1);\r
148     CHECK(!addToSessionCalled);\r
149     CHECK(!PersonalWordListFileHasContents());\r
150 }\r
151 \r
152 TEST_FIXTURE(EnchantDictionaryAddToSession_TestFixture,\r
153              EnchantDictionaryAddToSession_IsNotPermanent)\r
154 {\r
155     enchant_dict_add_to_session(_dict, "hello", -1);\r
156     CHECK(IsWordInSession("hello"));\r
157 \r
158     ReloadTestDictionary();\r
159 \r
160     CHECK(!IsWordInSession("hello"));\r
161 }\r
162 \r
163 TEST_FIXTURE(EnchantDictionaryAddToSession_TestFixture, \r
164              EnchantDictionaryAddToSession_HasPreviousError_ErrorCleared)\r
165 {\r
166     SetErrorOnMockDictionary("something bad happened");\r
167 \r
168     enchant_dict_add_to_session(_dict, "hello", -1);\r
169     CHECK_EQUAL((void*)NULL, (void*)enchant_dict_get_error(_dict));\r
170 }\r
171 \r
172 /////////////////////////////////////////////////////////////////////////////\r
173 // Test Error Conditions\r
174 TEST_FIXTURE(EnchantDictionaryAddToSession_TestFixture,\r
175              EnchantDictionaryAddToSession_NullDictionary_NotAdded)\r
176 {\r
177     enchant_dict_add_to_session(NULL, "hello", -1);\r
178     CHECK(!addToSessionCalled);\r
179 }\r
180 \r
181 TEST_FIXTURE(EnchantDictionaryAddToSession_TestFixture,\r
182              EnchantDictionaryAddToSession_NullWord_NotAdded)\r
183 {\r
184     enchant_dict_add_to_session(_dict, NULL, -1);\r
185     CHECK(!addToSessionCalled);\r
186 }\r
187 \r
188 TEST_FIXTURE(EnchantDictionaryAddToSession_TestFixture,\r
189              EnchantDictionaryAddToSession_EmptyWord_NotAdded)\r
190 {\r
191     enchant_dict_add_to_session(_dict, "", -1);\r
192     CHECK(!addToSessionCalled);\r
193 }\r
194 \r
195 TEST_FIXTURE(EnchantDictionaryAddToSession_TestFixture,\r
196              EnchantDictionaryAddToSession_WordSize0_NotAdded)\r
197 {\r
198     enchant_dict_add_to_session(_dict, "hello", 0);\r
199     CHECK(!addToSessionCalled);\r
200 }\r
201 \r
202 TEST_FIXTURE(EnchantDictionaryAddToSession_TestFixture,\r
203              EnchantDictionaryAddToSession_InvalidUtf8Word_NotAdded)\r
204 {\r
205     enchant_dict_add_to_session(_dict, "\xa5\xf1\x08", -1);\r
206     CHECK(!addToSessionCalled);\r
207 }\r
208 \r
209 TEST_FIXTURE(EnchantDictionaryAddToSessionNotImplemented_TestFixture,\r
210              EnchantDictionaryAddToSessionNotImplemented_WordAddedToSession)\r
211 {\r
212     enchant_dict_add_to_session(_dict, "hello", -1);\r
213     CHECK(IsWordInSession("hello"));\r
214 }\r
215 \r
216 TEST_FIXTURE(EnchantDictionaryAddToSessionNotImplemented_TestFixture,\r
217              EnchantDictionaryAddToSessionNotImplemented_NotPassedOnToProvider)\r
218 {\r
219     enchant_dict_add_to_session(_dict, "hello", -1);\r
220     CHECK(!addToSessionCalled);\r
221 }\r