ExpectedFunctionTest.cpp
Go to the documentation of this file.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 "../ExpectedFunction.h"
00025 #include "../BoundFunction.h"
00026 #include "Imports.cpp"
00027
00029 #define TEST_MESSAGE(summary, description) \
00030 CPPUNIT_ASSERT (e.message() == Message (summary, description))
00031
00033 #define TEST_SOURCELINE(filename, line) \
00034 CPPUNIT_ASSERT (e.sourceLine() == SourceLine (filename, line))
00035
00036 namespace Utils {
00037 namespace CalitkoMocks {
00038 namespace Testing {
00039
00041
00069 class ExpectedFunctionTest : public CppUnit::TestFixture
00070 {
00071 CPPUNIT_TEST_SUITE (ExpectedFunctionTest);
00072 CPPUNIT_TEST (testVoidVoidChecked);
00073 CPPUNIT_TEST (testVoidVoidNotChecked);
00074 CPPUNIT_TEST (testIntIntOk);
00075 CPPUNIT_TEST (testIntIntCallFails);
00076 CPPUNIT_TEST_FAIL (testIntIntReturnFails);
00077 CPPUNIT_TEST (testIntIntNoReturnSetFail);
00078 CPPUNIT_TEST (testIntIntNoReturnSetFailWithContext);
00079 CPPUNIT_TEST (testIntRefVoidOk);
00080 CPPUNIT_TEST (testVoidConstIntRefOk);
00081 CPPUNIT_TEST (testVoidIntRefOk);
00082 CPPUNIT_TEST_FAIL (testVoidIntRefNotSameIdentity);
00083 CPPUNIT_TEST (testVoidVoidWithCallsOk);
00084 CPPUNIT_TEST (testVoidVoidWithCallReturnIncorrect);
00085 CPPUNIT_TEST_SUITE_END();
00086
00087 public:
00089 void testVoidVoidChecked()
00090 {
00091 ExpectedFunction <void ()> foo;
00092 foo.check();
00093 foo.verifyChecked();
00094 }
00095
00097 void testVoidVoidNotChecked()
00098 {
00099 ExpectedFunction <void ()> foo;
00100 try {
00101 foo.verifyChecked();
00102 } catch (CPPUNIT_NS::Exception &e) {
00103 TEST_MESSAGE ("function not called", "function name unavailable");
00104 TEST_SOURCELINE ("", -1);
00105 return;
00106 }
00107 CPPUNIT_ASSERT (false);
00108 }
00109
00111 void testIntIntOk()
00112 {
00113 ExpectedFunction <int (int)> foo (1);
00114 foo.willReturn (2);
00115 CPPUNIT_ASSERT (foo.check (1) == 2);
00116 }
00117
00119 void testIntIntCallFails()
00120 {
00121 ExpectedFunction <int (int)> foo (1);
00122 foo.willReturn (2);
00123 try {
00124 foo.check (2);
00125 } catch (CPPUNIT_NS::Exception &e) {
00126 TEST_MESSAGE ("willCall() expectation failed",
00127 "function name unavailable");
00128 TEST_SOURCELINE ("", -1);
00129 return;
00130 }
00131 CPPUNIT_ASSERT (false);
00132 }
00133
00135 void testIntIntReturnFails()
00136 {
00137 ExpectedFunction <int (int)> foo (1);
00138 foo.willReturn (2);
00139 CPPUNIT_ASSERT (foo.check (1) == 1);
00140 }
00141
00143 void testIntIntNoReturnSetFail()
00144 {
00145 ExpectedFunction <int (int)> foo (1);
00146 try {
00147 foo.check (1);
00148 } catch (CPPUNIT_NS::Exception &e) {
00149 TEST_MESSAGE ("willReturn() not called for",
00150 "function name unavailable");
00151 TEST_SOURCELINE ("", -1);
00152 return;
00153 }
00154 CPPUNIT_ASSERT (false);
00155 }
00156
00158 void testIntIntNoReturnSetFailWithContext()
00159 {
00160 ExpectedFunction <int (int)> foo (1);
00161 foo.setContext ("test expectation string",
00162 CPPUNIT_NS::SourceLine ("file.cpp", 123));
00163 try {
00164 foo.check (1);
00165 } catch (CPPUNIT_NS::Exception &e) {
00166 TEST_MESSAGE ("willReturn() not called for",
00167 "test expectation string");
00168 TEST_SOURCELINE ("file.cpp", 123);
00169 return;
00170 }
00171 CPPUNIT_ASSERT (false);
00172 }
00173
00175 void testIntRefVoidOk()
00176 {
00177 ExpectedFunction <int & ()> foo;
00178 int i = 0;
00179 foo.willReturn (i);
00180 int &y = foo.check();
00181 CPPUNIT_ASSERT (&y == &i);
00182 }
00183
00185
00190 void testVoidConstIntRefOk()
00191 {
00192 ExpectedFunction <void (const int &)> foo (1);
00193 foo.check (1);
00194 }
00195
00197 void testVoidIntRefOk()
00198 {
00199 int x = 1;
00200 ExpectedFunction <void (int &)> foo (x);
00201 foo.check (x);
00202 }
00203
00205 void testVoidIntRefNotSameIdentity()
00206 {
00207 int x = 1;
00208 int y = 1;
00209 ExpectedFunction <void (int &)> foo (x);
00210 foo.check (y);
00211 }
00212
00213 void VoidVoidCall (int &a) { ++a; }
00214 typedef BoundFunction <ExpectedFunctionTest, void (int &)> VoidVoidCallType;
00216 void testVoidVoidWithCallsOk()
00217 {
00218 int a = 0;
00219 ExpectedFunction <void ()> foo;
00220
00221 auto_ptr <VoidVoidCallType> call1 (new VoidVoidCallType
00222 (this, &ExpectedFunctionTest::VoidVoidCall, a));
00223 auto_ptr <VoidVoidCallType> call2 (new VoidVoidCallType
00224 (this, &ExpectedFunctionTest::VoidVoidCall, a));
00225
00226 foo.addCall (auto_ptr <BoundFunctionBase> (call1.get()));
00227 call1.release();
00228 foo.addCall (auto_ptr <BoundFunctionBase> (call2.get()));
00229 call2.release();
00230 foo.check();
00231
00232
00233 CPPUNIT_ASSERT (a == 2);
00234 }
00235
00236 int VoidVoidCall2() { return 1; }
00237 typedef BoundFunction <ExpectedFunctionTest, int ()> VoidVoidCall2Type;
00239 void testVoidVoidWithCallReturnIncorrect()
00240 {
00241 ExpectedFunction <void ()> foo;
00242
00243 auto_ptr <VoidVoidCall2Type> call1 (new VoidVoidCall2Type
00244 (this, &ExpectedFunctionTest::VoidVoidCall2));
00245 call1->willReturn (2);
00246
00247 foo.addCall (auto_ptr <BoundFunctionBase> (call1.get()));
00248 call1.release();
00249 try {
00250 foo.check();
00251 } catch (CPPUNIT_NS::Exception &e) {
00252 TEST_MESSAGE ("willReturn() expectation failed",
00253 "function name unavailable");
00254 TEST_SOURCELINE ("", -1);
00255 return;
00256 }
00257 CPPUNIT_ASSERT (false);
00258 }
00259 };
00260
00261 CPPUNIT_TEST_SUITE_REGISTRATION (ExpectedFunctionTest);
00262
00263 }
00264 }
00265 }