Imported Upstream version 0.4.0
[platform/upstream/c-mock.git] / test / cmock-function-class-mockers_test.cc
1 #include <cmock/cmock.h>
2
3 #include "math.h"
4
5 using namespace ::testing;
6
7 class MathMocker : public CMockMocker<MathMocker>
8 {
9 public:
10     CMOCK_MOCK_METHOD(int, add, (int, int));
11     CMOCK_MOCK_METHOD(int, substract, (int, int));
12     CMOCK_MOCK_METHOD(int, negate, (int));
13 };
14
15 CMOCK_MOCK_FUNCTION(MathMocker, int, add, (int, int));
16 CMOCK_MOCK_FUNCTION(MathMocker, int, substract, (int, int));
17 CMOCK_MOCK_FUNCTION(MathMocker, int, negate, (int));
18
19 /**
20  * Functions add and substract are mocked as long as MathMocker instance exists.
21  * Once a mock function is destroyed, a call directs to a real function.
22  */
23 TEST(FunctionClassMockersTest, MocksFunctionAsLongAsMockerInstanceExists) {
24
25         {
26                 MathMocker mock;
27
28                 EXPECT_CALL(mock, add(1, 1)).WillOnce(Return(11));
29                 ASSERT_EQ(11, add(1, 1));
30
31                 EXPECT_CALL(mock, substract(1, 2)).WillOnce(Return(12));
32                 ASSERT_EQ(12, substract(1, 2));
33         }
34
35         ASSERT_EQ(2, add(1, 1));
36         ASSERT_EQ(-1, substract(1, 2));
37 }
38
39 TEST(FunctionClassMockersTest, ThrowsExceptionIfRealFunctionNotFound) {
40
41         {
42                 MathMocker mock;
43
44                 EXPECT_CALL(mock, negate(3)).WillOnce(Return(-3));
45                 ASSERT_EQ(-3, negate(3));
46         }
47
48         EXPECT_THROW(negate(3), std::logic_error);
49 }
50
51 TEST(FunctionClassMockersTest, ProvidesMeansToCallRealFunction) {
52
53         {
54                 MathMocker mock;
55
56                 ASSERT_EQ(2, CMOCK_REAL_FUNCTION(MathMocker, add)(1, 1));
57         }
58
59         ASSERT_EQ(2, CMOCK_REAL_FUNCTION(MathMocker, add)(1, 1));
60 }