1 /* Copyright (c) 2007 Eric Scott Albright
2  *
3  * Permission is hereby granted, free of charge, to any person obtaining a copy
4  * of this software and associated documentation files (the "Software"), to deal
5  * in the Software without restriction, including without limitation the rights
6  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7  * copies of the Software, and to permit persons to whom the Software is
8  * furnished to do so, subject to the following conditions:
9  *
10  * The above copyright notice and this permission notice shall be included in
11  * all copies or substantial portions of the Software.
12  *
13  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19  * THE SOFTWARE.
20  */
21 
22 #include <UnitTest++/UnitTest++.h>
23 #include <enchant.h>
24 #include "EnchantDictionaryTestFixture.h"
25 
26 static bool callbackCalled;
27 
EnchantSingleDictionaryDescribeCallback(const char * const lang_tag,const char * const provider_name,const char * const provider_desc,const char * const provider_file,void * user_data)28 void EnchantSingleDictionaryDescribeCallback (const char * const lang_tag,
29 				                          const char * const provider_name,
30 				                          const char * const provider_desc,
31 				                          const char * const provider_file,
32 				                          void * user_data)
33 {
34     DictionaryDescription* description = reinterpret_cast<DictionaryDescription*>(user_data);
35     *description = DictionaryDescription(lang_tag, provider_name, provider_desc, provider_file);
36     callbackCalled = true;
37 }
38 
39 static void * global_user_data;
EnchantSingleDictionaryDescribeAssignUserDataToStaticCallback(const char * const,const char * const,const char * const,const char * const,void * user_data)40 void EnchantSingleDictionaryDescribeAssignUserDataToStaticCallback (const char * const,
41 				                                              const char * const,
42 				                                              const char * const,
43 				                                              const char * const,
44 				                                              void * user_data)
45 {
46     global_user_data = user_data;
47     callbackCalled = true;
48 }
49 
50 struct EnchantDictionaryDescribe_TestFixtureBase : EnchantDictionaryTestFixture
51 {
52     //Setup
EnchantDictionaryDescribe_TestFixtureBaseEnchantDictionaryDescribe_TestFixtureBase53     EnchantDictionaryDescribe_TestFixtureBase(ConfigureHook userConfig):
54             EnchantDictionaryTestFixture(userConfig)
55     {
56         callbackCalled = false;
57     }
58     DictionaryDescription _description;
59 };
60 
61 struct EnchantDictionaryDescribe_TestFixture : EnchantDictionaryDescribe_TestFixtureBase
62 {
63     //Setup
EnchantDictionaryDescribe_TestFixtureEnchantDictionaryDescribe_TestFixture64     EnchantDictionaryDescribe_TestFixture():
65             EnchantDictionaryDescribe_TestFixtureBase(EmptyDictionary_ProviderConfiguration)
66     { }
67 };
68 
69 
70 /**
71  * enchant_dict_describe
72  * @broker: A non-null #EnchantDict
73  * @dict: A non-null #EnchantDictDescribeFn
74  * @user_data: Optional user-data
75  *
76  * Describes an individual dictionary
77  */
78 
79 /////////////////////////////////////////////////////////////////////////////
80 // Test Normal Operation
TEST_FIXTURE(EnchantDictionaryDescribe_TestFixture,EnchantDictionaryDescribe)81 TEST_FIXTURE(EnchantDictionaryDescribe_TestFixture,
82              EnchantDictionaryDescribe)
83 {
84     enchant_dict_describe(_dict, EnchantSingleDictionaryDescribeCallback, &_description);
85     CHECK(callbackCalled);
86     CHECK(_description.DataIsComplete());
87     CHECK_EQUAL(std::string("qaa"), _description.LanguageTag);
88 }
89 
TEST_FIXTURE(EnchantDictionaryDescribe_TestFixture,EnchantDictionaryDescribe_Pwl)90 TEST_FIXTURE(EnchantDictionaryDescribe_TestFixture,
91              EnchantDictionaryDescribe_Pwl)
92 {
93     enchant_dict_describe(_pwl, EnchantSingleDictionaryDescribeCallback, &_description);
94     CHECK(callbackCalled);
95     CHECK(_description.DataIsComplete());
96     CHECK_EQUAL(std::string("Personal Wordlist"), _description.LanguageTag);
97     CHECK_EQUAL(std::string("Personal Wordlist"), _description.Name);
98     CHECK_EQUAL(std::string("Personal Wordlist"), _description.Description);
99 }
100 
TEST_FIXTURE(EnchantDictionaryDescribe_TestFixture,EnchantDictionaryDescribe_NullUserdata_PassedToCallback)101 TEST_FIXTURE(EnchantDictionaryDescribe_TestFixture,
102              EnchantDictionaryDescribe_NullUserdata_PassedToCallback)
103 {
104     enchant_dict_describe(_dict, EnchantSingleDictionaryDescribeAssignUserDataToStaticCallback, NULL);
105     CHECK(callbackCalled);
106     CHECK_EQUAL((void*)NULL, global_user_data);
107 }
108 
TEST_FIXTURE(EnchantDictionaryDescribe_TestFixture,EnchantDictionaryDescribe_NonNullUserdata_PassedToCallback)109 TEST_FIXTURE(EnchantDictionaryDescribe_TestFixture,
110              EnchantDictionaryDescribe_NonNullUserdata_PassedToCallback)
111 {
112     const char* userData = "some user data";
113     enchant_dict_describe(_dict, EnchantSingleDictionaryDescribeAssignUserDataToStaticCallback, (void*)userData);
114     CHECK(callbackCalled);
115     CHECK_EQUAL(userData, global_user_data);
116 }
117 
TEST_FIXTURE(EnchantDictionaryDescribe_TestFixture,EnchantDictionaryDescribe_HasPreviousError_ErrorCleared)118 TEST_FIXTURE(EnchantDictionaryDescribe_TestFixture,
119              EnchantDictionaryDescribe_HasPreviousError_ErrorCleared)
120 {
121     SetErrorOnMockDictionary("something bad happened");
122 
123     enchant_dict_describe(_dict, EnchantSingleDictionaryDescribeAssignUserDataToStaticCallback, NULL);
124     CHECK_EQUAL((void*)NULL, (void*)enchant_dict_get_error(_dict));
125 }
126 /////////////////////////////////////////////////////////////////////////////
127 // Test Error Conditions
TEST_FIXTURE(EnchantDictionaryDescribe_TestFixture,EnchantDictionaryDescribe_NullDict_DoNothing)128 TEST_FIXTURE(EnchantDictionaryDescribe_TestFixture,
129              EnchantDictionaryDescribe_NullDict_DoNothing)
130 {
131     enchant_dict_describe(NULL, EnchantSingleDictionaryDescribeCallback, &_description);
132     CHECK(!callbackCalled);
133 }
134 
TEST_FIXTURE(EnchantDictionaryDescribe_TestFixture,EnchantDictionaryDescribe_NullCallback_DoNothing)135 TEST_FIXTURE(EnchantDictionaryDescribe_TestFixture,
136              EnchantDictionaryDescribe_NullCallback_DoNothing)
137 {
138     enchant_dict_describe(_dict, NULL, &_description);
139     CHECK(!callbackCalled);
140 }
141