diff --git a/tests/silly_test.cpp b/tests/silly_test.cpp index 3e4001ea35fdedbc156b6a9af3c585a2174718d7..975c88b9613a06976ada1221e8403d4a0613d28a 100644 --- a/tests/silly_test.cpp +++ b/tests/silly_test.cpp @@ -1,35 +1,72 @@ #include "gtest/gtest.h" #include "gmock/gmock.h" +// use the gmock Return action directly using ::testing::Return; -class Thing { +// Just a dummy test that it will fail! +TEST(silly_test, silly_test) { + int a = 0; + int b = a++; + b++; + EXPECT_EQ(a, b) << "a ("<<a<<") should be equal to b ("<<b<<")"; +} + +// +// "Real" class. A client where you should connect... +// [it would normally reside on the src directory] +const int STATUS_DISCONNECTED = 0; +const int STATUS_CONNECTED = 1; + +class Client { private: - int ans; + int status; public: - Thing(): ans(42) {}; - virtual int lue() { - return ans; + Client(): status(STATUS_DISCONNECTED) {}; + virtual int connect() { + // + // Do whatever it needs to connect... + // int rc = socket(AF_INET, SOCK_STREAM, 0); + // ... + // if (rc != -1) + return STATUS_CONNECTED; } }; -class MockThing : public Thing { +// +// Mocked class. It seems a Client but +// it does what we want it to do. +// +class MockClient : public Client { public: - MOCK_METHOD0(lue, int()); + MOCK_METHOD0(connect, int()); }; -// Just a dummy test that it will fail! -TEST(silly_test, silly_test) { - int a = 0; - int b = 1; - EXPECT_EQ(a, b) << "a ("<<a<<") should be equal to b ("<<b<<")"; -} - TEST(silly_test, silly_mock) { - MockThing m; - EXPECT_CALL(m, lue()).WillOnce(Return(44)); + // Mock object + MockClient m; + // Announce how our mock object will + // behave. It will always work as stated here + // because it does not depends on "real world". + EXPECT_CALL(m, connect()) + .WillOnce(Return(STATUS_CONNECTED)) + .WillOnce(Return(STATUS_DISCONNECTED)); + + // This "tests" our code in normal condition... + int rc = m.connect(); + EXPECT_EQ(rc, STATUS_CONNECTED) << + "The second one it should connect Ok"; + + // This "tests" our code in case there is no connection... + rc = m.connect(); + EXPECT_EQ(rc, STATUS_DISCONNECTED) << + "The first time it should fail to connect" ; - int a = m.lue(); + // Uncomment the following 3 lines + // and the test will FAIL, because we only + // announced 2 calls (with WillOnce) to Client.connect() - EXPECT_EQ(a, 42) << "The answer to Life, the Universe and Everything should be 42!!!" ; + // int a = 0; + // if(m.connect()==STATUS_CONNECTED) + // a = 1; }