Imported Upstream version 0.4.0
[platform/upstream/c-mock.git] / test / cmock-function-mockers_test.cc
1 #include <cmock/cmock.h>
2
3 #include "math.h"
4
5 using namespace ::testing;
6
7 DECLARE_FUNCTION_MOCK2(AddFunctionMock, add, int(int, int));
8 DECLARE_FUNCTION_MOCK1(NegateFunctionMock, negate, int(int));
9
10 IMPLEMENT_FUNCTION_MOCK2(AddFunctionMock, add, int(int, int));
11 IMPLEMENT_FUNCTION_MOCK1(NegateFunctionMock, negate, int(int));
12
13 /**
14  * Function add is mocked as long as AddFunctionMock instance exists.
15  * Once mock function is destroyed, a call directs to a real function.
16  */
17 TEST(FunctionMockersTest, MocksFunctionAsLongAsMockInstanceExists) {
18
19         {
20                 AddFunctionMock mock;
21
22                 EXPECT_FUNCTION_CALL(mock, (1, 2)).WillOnce(Return(12));
23                 ASSERT_EQ(12, add(1, 2));
24         }
25
26         ASSERT_EQ(3, add(1, 2));
27 }
28
29 /**
30  * real static mock class field holds pointer to a real function.
31  */
32 TEST(FunctionMockersTest, FunctionMockExportsRealFunctionPointer) {
33         EXPECT_EQ(3, AddFunctionMock::real(1, 2));
34 }
35
36 /**
37  * Function negate doesn't exist, but can be mocked.
38  */
39 TEST(FunctionMockersTest, ThrowsExceptionIfRealFunctionNotFound)
40 {
41         {
42                 NegateFunctionMock mock;
43
44                 EXPECT_FUNCTION_CALL(mock, (9)).WillOnce(Return(3));
45
46                 ASSERT_EQ(3, negate(9));
47         }
48
49         EXPECT_THROW(negate(9), std::logic_error);
50 }