Tizen 2.1 base
[external/enchant.git] / unittests / dictionary / enchant_dict_remove_from_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 #define NOMINMAX //don't want windows to collide with std::min\r
23 \r
24 #include <UnitTest++.h>\r
25 #include <enchant.h>\r
26 #include "../EnchantDictionaryTestFixture.h"\r
27 \r
28 static bool dictCheckCalled;\r
29 \r
30 static int\r
31 MockDictionaryCheck (EnchantDict * dict, const char *const word, size_t len)\r
32 {\r
33     dict;\r
34     dictCheckCalled = true;\r
35     if(strncmp("hello", word, len)==0)\r
36     {\r
37         return 0; //good word\r
38     }\r
39     return 1; // bad word\r
40 }\r
41 \r
42 \r
43 static EnchantDict* MockProviderRequestCheckMockDictionary(EnchantProvider * me, const char *tag)\r
44 {\r
45     \r
46     EnchantDict* dict = MockProviderRequestBasicMockDictionary(me, tag);\r
47     dict->check = MockDictionaryCheck;\r
48     return dict;\r
49 }\r
50 \r
51 static void DictionaryCheck_ProviderConfiguration (EnchantProvider * me, const char *)\r
52 {\r
53      me->request_dict = MockProviderRequestCheckMockDictionary;\r
54      me->dispose_dict = MockProviderDisposeDictionary;\r
55 }\r
56 \r
57 struct EnchantDictionaryRemoveFromSession_TestFixture : EnchantDictionaryTestFixture\r
58 {\r
59     //Setup\r
60     EnchantDictionaryRemoveFromSession_TestFixture():\r
61             EnchantDictionaryTestFixture(DictionaryCheck_ProviderConfiguration)\r
62     { \r
63         dictCheckCalled = false;\r
64     }\r
65 };\r
66 \r
67 /**\r
68  * enchant_dict_remove_from_session\r
69  * @dict: A non-null #EnchantDict\r
70  * @word: The non-null word you wish to exclude from this spell-checking session, in UTF-8 encoding\r
71  * @len: The byte length of @word, or -1 for strlen (@word)\r
72  *\r
73  */\r
74 \r
75 /////////////////////////////////////////////////////////////////////////////\r
76 // Test Normal Operation\r
77 TEST_FIXTURE(EnchantDictionaryRemoveFromSession_TestFixture,\r
78              EnchantDictionaryRemoveFromSession_WordNotAddedToEnchantExcludeFile)\r
79 {\r
80     enchant_dict_remove_from_session(_dict, "hello", -1);\r
81     CHECK(!ExcludeFileHasContents());\r
82 }\r
83 \r
84 TEST_FIXTURE(EnchantDictionaryRemoveFromSession_TestFixture,\r
85              EnchantDictionaryRemoveFromSession_WordRemovedFromSession)\r
86 {\r
87     enchant_dict_add_to_session(_dict, "hello", -1);\r
88     CHECK(IsWordInSession("hello"));\r
89     enchant_dict_remove_from_session(_dict, "hello", -1);\r
90     CHECK(!IsWordInSession("hello"));\r
91 }\r
92 \r
93 TEST_FIXTURE(EnchantDictionaryRemoveFromSession_TestFixture,\r
94              EnchantDictionaryRemoveFromSession_CalledTwice)\r
95 {\r
96     enchant_dict_add_to_session(_dict, "hello", -1);\r
97     enchant_dict_remove_from_session(_dict, "hello", -1);\r
98     enchant_dict_remove_from_session(_dict, "hello", -1);\r
99     CHECK(!IsWordInSession("hello"));\r
100 }\r
101 \r
102 TEST_FIXTURE(EnchantDictionaryRemoveFromSession_TestFixture,\r
103              EnchantDictionaryRemoveFromSession_WordExcludedFromDictionary)\r
104 {\r
105     enchant_dict_remove_from_session(_dict, "hello", -1);\r
106     CHECK(!IsWordInDictionary("hello"));\r
107 }\r
108 \r
109 TEST_FIXTURE(EnchantDictionaryRemoveFromSession_TestFixture,\r
110              EnchantDictionaryRemoveFromSession_InBrokerSession_WordExcludedFromBrokerSession)\r
111 {\r
112     enchant_dict_add_to_session(_pwl, "hello", -1);\r
113     CHECK(enchant_dict_is_in_session(_pwl, "hello", -1));\r
114     enchant_dict_remove_from_session(_pwl, "hello", -1);\r
115     CHECK(!enchant_dict_is_in_session(_pwl, "hello", -1));\r
116 }\r
117 \r
118 TEST_FIXTURE(EnchantDictionaryRemoveFromSession_TestFixture,\r
119              EnchantDictionaryRemoveFromSession_IsNotPermanent)\r
120 {\r
121     enchant_dict_remove_from_session(_dict, "hello", -1);\r
122     CHECK(!IsWordInDictionary("hello"));\r
123 \r
124     ReloadTestDictionary();\r
125 \r
126     CHECK(IsWordInDictionary("hello"));\r
127 }\r
128 \r
129 TEST_FIXTURE(EnchantDictionaryRemoveFromSession_TestFixture,\r
130              EnchantDictionaryRemoveFromSession_NotGivenAsSuggestion)\r
131 {\r
132     enchant_dict_remove_from_session(_dict, "aelo", -1);\r
133 \r
134     std::vector<const std::string> suggestions = GetSuggestions("helo");\r
135     CHECK_EQUAL(3, suggestions.size());\r
136     CHECK_ARRAY_EQUAL(GetExpectedSuggestions("helo",1), suggestions, std::min(3u,suggestions.size()) );\r
137 }\r
138 \r
139 TEST_FIXTURE(EnchantDictionaryRemoveFromSession_TestFixture,\r
140              EnchantDictionaryRemoveFromSession_ThenAddedToSession_GivenAsSuggestion)\r
141 {\r
142     enchant_dict_remove_from_session(_dict, "aelo", -1);\r
143     enchant_dict_add_to_session(_dict, "aelo", -1);\r
144 \r
145     std::vector<const std::string> suggestions = GetSuggestions("helo");\r
146     CHECK_EQUAL(4, suggestions.size());\r
147     CHECK_ARRAY_EQUAL(GetExpectedSuggestions("helo"), suggestions, suggestions.size());\r
148 }\r
149 \r
150 TEST_FIXTURE(EnchantDictionaryRemoveFromSession_TestFixture, \r
151              EnchantDictionaryRemoveFromSession_HasPreviousError_ErrorCleared)\r
152 {\r
153     SetErrorOnMockDictionary("something bad happened");\r
154 \r
155     enchant_dict_remove_from_session(_dict, "hello", -1);\r
156     CHECK_EQUAL((void*)NULL, (void*)enchant_dict_get_error(_dict));\r
157 }\r
158 \r
159 /////////////////////////////////////////////////////////////////////////////\r
160 // Test Error Conditions\r
161 TEST_FIXTURE(EnchantDictionaryRemoveFromSession_TestFixture,\r
162              EnchantDictionaryRemoveFromSession_NullDictionary_NotRemoved)\r
163 {\r
164     enchant_dict_remove_from_session(NULL, "hello", -1);\r
165     CHECK(IsWordInDictionary("hello"));\r
166 }\r
167 \r
168 TEST_FIXTURE(EnchantDictionaryRemoveFromSession_TestFixture,\r
169              EnchantDictionaryRemoveFromSession_NullWord_NotRemoved)\r
170 {\r
171     enchant_dict_remove_from_session(_dict, NULL, -1);\r
172     CHECK(IsWordInDictionary("hello"));\r
173 }\r
174 \r
175 TEST_FIXTURE(EnchantDictionaryRemoveFromSession_TestFixture,\r
176              EnchantDictionaryRemoveFromSession_EmptyWord_NotRemoved)\r
177 {\r
178     enchant_dict_remove_from_session(_dict, "", -1);\r
179     CHECK(IsWordInDictionary("hello"));\r
180 }\r
181 \r
182 TEST_FIXTURE(EnchantDictionaryRemoveFromSession_TestFixture,\r
183              EnchantDictionaryRemoveFromSession_WordSize0_NotRemoved)\r
184 {\r
185     enchant_dict_remove_from_session(_dict, "hello", 0);\r
186     CHECK(IsWordInDictionary("hello"));\r
187 }\r
188 \r
189 TEST_FIXTURE(EnchantDictionaryRemoveFromSession_TestFixture,\r
190              EnchantDictionaryRemoveFromSession_InvalidUtf8Word_NotRemoved)\r
191 {\r
192     enchant_dict_remove_from_session(_dict, "\xa5\xf1\x08", -1);\r
193     CHECK(IsWordInDictionary("hello"));\r
194 }\r