00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #include "Qt.h"
00024 #include "../BoundFunction.h"
00025 #include "../ExpectedFunction.h"
00026 #include "Imports.cpp"
00027
00029 #define TEST_VOID_FOO(type) \
00030 type##Ok = false; foo.call(); \
00031 CPPUNIT_ASSERT (type##Ok == true)
00032
00034 #define TEST_FOO(type, ret) \
00035 type##Ok = false; \
00036 foo.willReturn (ret); \
00037 foo.call(); \
00038 CPPUNIT_ASSERT (type##Ok == true)
00039
00041 #define TEST_MESSAGE(summary, description) \
00042 CPPUNIT_ASSERT (e.message() == Message (summary, description))
00043
00045 #define TEST_SOURCELINE(filename, line) \
00046 CPPUNIT_ASSERT (e.sourceLine() == SourceLine (filename, line))
00047
00048 namespace Utils {
00049 namespace CalitkoMocks {
00050 namespace Testing {
00051
00053
00075 class BoundFunctionTest : public CppUnit::TestFixture
00076 {
00077 CPPUNIT_TEST_SUITE (BoundFunctionTest);
00078 CPPUNIT_TEST (testVoidVoid);
00079 CPPUNIT_TEST (testIntVoid);
00080 CPPUNIT_TEST (testIntVoidReturnIncorrect);
00081 CPPUNIT_TEST (testIntVoidReturnNotSet);
00082 CPPUNIT_TEST (testIntVoidReturnNotSetWithContext);
00083 CPPUNIT_TEST (testIntInt);
00084 CPPUNIT_TEST (testBoolRefIntRefConstIntRef);
00085 CPPUNIT_TEST (testIntIntWithExpectationsOk);
00086 CPPUNIT_TEST (testIntIntWithExpectationsBadCheckFail);
00087 CPPUNIT_TEST (testIntIntWithExpectationsNotCheckedFail);
00088 CPPUNIT_TEST (testVoidIntWithExpectationsNotCheckedFail);
00089 CPPUNIT_TEST (testVoidIntFailSetOwnContextIfNotSet);
00090 CPPUNIT_TEST_SUITE_END();
00091
00092 public:
00093 bool VoidVoidOk;
00094 void VoidVoid() { VoidVoidOk = true; }
00096 void testVoidVoid()
00097 {
00098 BoundFunction <BoundFunctionTest, void ()>
00099 foo (this, &BoundFunctionTest::VoidVoid);
00100 TEST_VOID_FOO (VoidVoid);
00101 }
00102
00103 bool IntVoidOk;
00104 int IntVoid() { IntVoidOk = true; return 3; }
00106 void testIntVoid()
00107 {
00108 BoundFunction <BoundFunctionTest, int ()>
00109 foo (this, &BoundFunctionTest::IntVoid);
00110 TEST_FOO (IntVoid, 3);
00111 }
00112
00114 void testIntVoidReturnIncorrect()
00115 {
00116 BoundFunction <BoundFunctionTest, int ()>
00117 foo (this, &BoundFunctionTest::IntVoid);
00118 try {
00119 TEST_FOO (IntVoid, 2);
00120 } catch (Exception &e) {
00121 TEST_MESSAGE ("willReturn() expectation failed",
00122 "function name unavailable");
00123 TEST_SOURCELINE ("", -1);
00124 return;
00125 }
00126 CPPUNIT_ASSERT (false);
00127 }
00128
00130 void testIntVoidReturnNotSet()
00131 {
00132 BoundFunction <BoundFunctionTest, int ()>
00133 foo (this, &BoundFunctionTest::IntVoid);
00134 try {
00135 foo.call();
00136 } catch (Exception &e) {
00137 TEST_MESSAGE ("willReturn() not called for",
00138 "function name unavailable");
00139 TEST_SOURCELINE ("", -1);
00140 return;
00141 }
00142 CPPUNIT_ASSERT (false);
00143 }
00144
00146 void testIntVoidReturnNotSetWithContext()
00147 {
00148 BoundFunction <BoundFunctionTest, int ()>
00149 foo (this, &BoundFunctionTest::IntVoid);
00150 foo.setContext ("test expectation string",
00151 SourceLine ("file.cpp", 123));
00152 try {
00153 foo.call();
00154 } catch (Exception &e) {
00155 TEST_MESSAGE ("willReturn() not called for",
00156 "test expectation string");
00157 TEST_SOURCELINE ("file.cpp", 123);
00158 return;
00159 }
00160 CPPUNIT_ASSERT (false);
00161 }
00162
00163 bool IntIntOk;
00164 int IntInt (int a) { IntIntOk = (a == 1); return 3; }
00166 void testIntInt()
00167 {
00168 BoundFunction <BoundFunctionTest, int (int)>
00169 foo (this, &BoundFunctionTest::IntInt, 1);
00170 TEST_FOO (IntInt, 3);
00171 }
00172
00173 bool BoolRefIntRefConstIntRefOk;
00174 bool & BoolRefIntRefConstIntRef (int &a, const int &b)
00175 { BoolRefIntRefConstIntRefOk = (a == 1 && b == 2);
00176 return BoolRefIntRefConstIntRefOk; }
00178 void testBoolRefIntRefConstIntRef()
00179 {
00180 int a = 1;
00181 bool res = true;
00182 BoundFunction <BoundFunctionTest, bool & (int &, const int &)>
00183 foo (this, &BoundFunctionTest::BoolRefIntRefConstIntRef, a, 2);
00184 TEST_FOO (BoolRefIntRefConstIntRef, res);
00185 }
00186
00187 auto_ptr <ExpectedFunction <void (int)> > IntIntExpectation1;
00188 auto_ptr <ExpectedFunction <void (int)> > IntIntExpectation2;
00189
00190 int IntIntWithExpectations (int)
00191 {
00192 IntIntExpectation1->check (1);
00193 IntIntExpectation2->check (2);
00194 return 3;
00195 }
00196
00198 void testIntIntWithExpectationsOk()
00199 {
00200 BoundFunction <BoundFunctionTest, int (int)>
00201 foo (this, &BoundFunctionTest::IntIntWithExpectations, 1);
00202
00203 IntIntExpectation1.reset (new ExpectedFunction <void (int)> (1));
00204 IntIntExpectation2.reset (new ExpectedFunction <void (int)> (2));
00205
00206 foo.addExpectation (IntIntExpectation1.get());
00207 foo.addExpectation (IntIntExpectation2.get());
00208 foo.willReturn (3);
00209 foo.call();
00210 }
00211
00213 void testIntIntWithExpectationsBadCheckFail()
00214 {
00215 BoundFunction <BoundFunctionTest, int (int)>
00216 foo (this, &BoundFunctionTest::IntIntWithExpectations, 1);
00217
00218 IntIntExpectation1.reset (new ExpectedFunction <void (int)> (1));
00219 IntIntExpectation2.reset (new ExpectedFunction <void (int)> (3));
00220
00221 foo.addExpectation (IntIntExpectation1.get());
00222 foo.addExpectation (IntIntExpectation2.get());
00223 foo.willReturn (3);
00224 try {
00225 foo.call();
00226 } catch (Exception &e) {
00227
00228 TEST_MESSAGE ("willCall() expectation failed",
00229 "function name unavailable");
00230 return;
00231 }
00232 CPPUNIT_ASSERT (false);
00233 }
00234
00236 void testIntIntWithExpectationsNotCheckedFail()
00237 {
00238 BoundFunction <BoundFunctionTest, int (int)>
00239 foo (this, &BoundFunctionTest::IntIntWithExpectations, 1);
00240
00241 IntIntExpectation1.reset (new ExpectedFunction <void (int)> (1));
00242 IntIntExpectation2.reset (new ExpectedFunction <void (int)> (2));
00243
00244 auto_ptr <ExpectedFunction <void (int)> > IntIntExpectation2;
00245 IntIntExpectation2.reset (new ExpectedFunction <void (int)> (2));
00246
00247 foo.addExpectation (IntIntExpectation1.get());
00248 foo.addExpectation (IntIntExpectation2.get());
00249 foo.willReturn (3);
00250 try {
00251 foo.call();
00252 } catch (Exception &e) {
00253
00254 TEST_MESSAGE ("function not called", "function name unavailable");
00255 return;
00256 }
00257 CPPUNIT_ASSERT (false);
00258 }
00259
00260 void VoidIntWithExpectations (int v)
00261 {
00262 IntIntWithExpectations (v);
00263 }
00264
00266 void testVoidIntWithExpectationsNotCheckedFail()
00267 {
00268 BoundFunction <BoundFunctionTest, void (int)>
00269 foo (this, &BoundFunctionTest::VoidIntWithExpectations, 1);
00270
00271 IntIntExpectation1.reset (new ExpectedFunction <void (int)> (1));
00272 IntIntExpectation2.reset (new ExpectedFunction <void (int)> (2));
00273
00274 auto_ptr <ExpectedFunction <void (int)> > IntIntExpectation2;
00275 IntIntExpectation2.reset (new ExpectedFunction <void (int)> (2));
00276
00277 foo.addExpectation (IntIntExpectation1.get());
00278 foo.addExpectation (IntIntExpectation2.get());
00279 try {
00280 foo.call();
00281 } catch (Exception &e) {
00282
00283 TEST_MESSAGE ("function not called", "function name unavailable");
00284 return;
00285 }
00286 CPPUNIT_ASSERT (false);
00287 }
00288
00290
00295 void testVoidIntFailSetOwnContextIfNotSet()
00296 {
00297 BoundFunction <BoundFunctionTest, void (int)>
00298 foo (this, &BoundFunctionTest::VoidIntWithExpectations, 1);
00299 foo.setContext ("blah", SourceLine ("file.cpp", 123));
00300
00301 IntIntExpectation1.reset (new ExpectedFunction <void (int)> (1));
00302 IntIntExpectation2.reset (new ExpectedFunction <void (int)> (2));
00303
00304 auto_ptr <ExpectedFunction <void (int)> > IntIntExpectation2;
00305 IntIntExpectation2.reset (new ExpectedFunction <void (int)> (2));
00306
00307 foo.addExpectation (IntIntExpectation1.get());
00308 foo.addExpectation (IntIntExpectation2.get());
00309 try {
00310 foo.call();
00311 } catch (Exception &e) {
00312
00313 TEST_MESSAGE ("function not called", "function name unavailable");
00314 CPPUNIT_ASSERT (e.sourceLine() == SourceLine ("file.cpp", 123));
00315 return;
00316 }
00317 CPPUNIT_ASSERT (false);
00318 }
00319 };
00320
00321 CPPUNIT_TEST_SUITE_REGISTRATION (BoundFunctionTest);
00322
00323 }
00324 }
00325 }