Imported Upstream version 0.4.0
[platform/upstream/c-mock.git] / test / cmock-function-class-mockers-old-style_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     MOCK_METHOD2(add, int(int, int));
11     MOCK_METHOD2(substract, int(int, int));
12     MOCK_METHOD1(negate, int(int));
13 };
14
15 CMOCK_MOCK_FUNCTION2(MathMocker, add, int(int, int));
16 CMOCK_MOCK_FUNCTION2(MathMocker, substract, int(int, int));
17 CMOCK_MOCK_FUNCTION1(MathMocker, negate, int(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(FunctionClassMockersOldStyleTest, 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(FunctionClassMockersOldStyleTest, 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 }