1 
2 /* Additional include from CppUTestExt */
3 #include "CppUTest/TestHarness.h"
4 #include "CppUTestExt/MockSupport.h"
5 
6 /* Stubbed out product code using linker, function pointer, or overriding */
foo(const char * param_string,int param_int)7 static int foo(const char* param_string, int param_int)
8 {
9     /* Tell CppUTest Mocking what we mock. Also return recorded value */
10     return mock().actualCall("Foo")
11             .withParameter("param_string", param_string)
12             .withParameter("param_int", param_int)
13             .returnValue().getIntValue();
14 }
15 
bar(double param_double,const char * param_string)16 static void bar(double param_double, const char* param_string)
17 {
18     mock().actualCall("Bar")
19         .withParameter("param_double", param_double)
20         .withParameter("param_string", param_string);
21 }
22 
23 /* Production code calls to the methods we stubbed */
productionCodeFooCalls()24 static int productionCodeFooCalls()
25 {
26     int return_value;
27     return_value = foo("value_string", 10);
28     (void)return_value;
29     return_value = foo("value_string", 10);
30     return return_value;
31 }
32 
productionCodeBarCalls()33 static void productionCodeBarCalls()
34 {
35     bar(1.5, "more");
36     bar(1.5, "more");
37 }
38 
39 /* Actual test */
TEST_GROUP(MockCheatSheet)40 TEST_GROUP(MockCheatSheet)
41 {
42     void teardown()
43     {
44         /* Check expectations. Alternatively use MockSupportPlugin */
45         mock().checkExpectations();
46 
47         mock().clear();
48     }
49 };
50 
TEST(MockCheatSheet,foo)51 TEST(MockCheatSheet, foo)
52 {
53     /* Record 2 calls to Foo. Return different values on each call */
54     mock().expectOneCall("Foo")
55         .withParameter("param_string", "value_string")
56         .withParameter("param_int", 10)
57         .andReturnValue(30);
58     mock().expectOneCall("Foo")
59         .ignoreOtherParameters()
60         .andReturnValue(50);
61 
62     /* Call production code */
63     productionCodeFooCalls();
64 }
65 
TEST(MockCheatSheet,bar)66 TEST(MockCheatSheet, bar)
67 {
68     /* Expect 2 calls on Bar. Check only one parameter */
69     mock().expectNCalls(2, "Bar")
70         .withParameter("param_double", 1.5)
71         .ignoreOtherParameters();
72 
73     /* And the production code call */
74     productionCodeBarCalls();
75 }
76 
77