Debian Codegraph @

tests/mock_messages_tests.c

raw symbols SCIP
1 #include <cgreen/boxed_double.h>
2 #include <cgreen/cgreen.h>
3 #include <cgreen/mocks.h>
4 #include <cgreen/unit.h>
5 #include <stdlib.h>
6
7 #ifdef __cplusplus
8 using namespace cgreen;
9 #endif
10
11 /* NOTE: These tests are not designed to pass, they are run to output
12 messages which need to be compared to some golden output */
13
14 Describe(Mocks);
15 BeforeEach(Mocks) {}
16 AfterEach(Mocks) {}
17
18 static int integer_out(void) {
19 return (int)mock();
20 }
21
22 static char *string_out(int p1) {
23 return (char *)mock(p1);
24 }
25
26 static int sample_mock(int i, const char *s) {
27 return (int)mock(i, s);
28 }
29
30 Ensure(Mocks, can_declare_function_never_called) {
31 never_expect(sample_mock);
32
33 sample_mock(0, "");
34 }
35
36 Ensure(Mocks, when_never_expected_mock_function_is_not_called_pass_counter_increments_by_one) {
37 never_expect(sample_mock);
38 }
39
40 Ensure(Mocks, calls_beyond_expected_sequence_fail_when_mocks_are_strict) {
41 expect(integer_out,
42 will_return(1)
43 );
44
45 expect(integer_out,
46 will_return(2)
47 );
48
49 expect(integer_out,
50 will_return(3)
51 );
52
53 assert_that(integer_out(), is_equal_to(1));
54 assert_that(integer_out(), is_equal_to(2));
55 assert_that(integer_out(), is_equal_to(3));
56 // expected fail
57 assert_that(integer_out(), is_equal_to(3));
58 }
59
60 static void unexpected_mock(void) { mock(); }
61
62 Ensure(Mocks, lists_unexpected_mock_calls) { unexpected_mock(); }
63
64
65 // These are tentative solutions to mocks returning doubles and
66 // trapping type errors when they do.
67
68 /* xEnsure(Mocks, traps_double_return_value_for_int_mock) { */
69 /* expect(integer_out, will_return_double(3.1415926)); */
70 /* integer_out(); */
71 /* } */
72
73 /* static double double_out() { */
74 /* return mock_double(); */
75 /* } */
76
77 /* xEnsure(Mocks, traps_int_return_value_for_double_mock) { */
78 /* expect(double_out, will_return_double(3.1415926)); */
79 /* assert_that(double_out(), is_equal_to_double(3.1415926)); */
80 /* } */
81
82 Ensure(Mocks, failure_when_no_presets_for_default_strict_mock) {
83 assert_that(integer_out(), is_equal_to(0));
84 }
85
86 Ensure(Mocks, failure_reported_when_expect_after_never_expect_for_same_function) {
87 never_expect(integer_out);
88
89 expect(integer_out);
90 }
91
92 Ensure(Mocks, failure_reported_when_expect_after_always_expect_for_same_function) {
93 always_expect(integer_out,
94 will_return(666)
95 );
96
97 expect(integer_out);
98 }
99
100 Ensure(Mocks, reports_multiple_always_expect) {
101 always_expect(integer_out);
102 always_expect(integer_out);
103 }
104
105 Ensure(Mocks, reports_always_expect_after_never_expect_for_same_function) {
106 never_expect(integer_out);
107 always_expect(integer_out);
108 }
109
110 Ensure(Mocks, reports_never_expect_after_always_expect_for_same_function) {
111 always_expect(integer_out);
112 never_expect(integer_out);
113 }
114
115 Ensure(Mocks, reports_multiple_never_expect) {
116 never_expect(integer_out);
117 never_expect(integer_out);
118 }
119
120 Ensure(Mocks, single_uncalled_expectation_fails_tally) {
121 expect(string_out,
122 will_return(5),
123 when(i, is_equal_to(666)),
124 when(s, is_equal_to_string("devil"))
125 );
126 }
127
128
129 Ensure(Mocks, should_detect_two_unfulfilled_expectations_on_unknown_functions) {
130 expect(f1, will_return('a'));
131 expect(f2, will_return('b'));
132 }
133
134 Ensure(Mocks, should_detect_two_unfulfilled_expectations_without_constraints_on_unknown_functions) {
135 expect(f1);
136 expect(f2);
137 }
138
139 static double double_out(void) {
140 return (double)mock();
141 }
142
143 static void double_in(double in) {
144 mock(box_double(in));
145 }
146
147 /* TODO: this will not report anything until v2 */
148 /* Why? */
149 xEnsure(Mocks, reports_mock_cannot_return_double) {
150 expect(double_out, will_return_double(4.123));
151 double_out();
152 }
153
154 Ensure(Mocks, learning_mocks_emit_none_when_learning_no_mocks) {
155 cgreen_mocks_are(learning_mocks);
156 }
157
158 Ensure(Mocks, learning_mocks_emit_pastable_code) {
159 cgreen_mocks_are(learning_mocks);
160 string_out(1);
161 string_out(2);
162 integer_out();
163 integer_out();
164 string_out(3);
165 integer_out();
166 }
167
168 Ensure(Mocks, can_learn_double_expects) {
169 cgreen_mocks_are(learning_mocks);
170 double_in(3.14);
171 }
172
173 /* It would be very nice if learning mocks could survive unexpected
174 terminations but since the child process is already gone when the
175 parent process detects this we need to either start catching
176 exceptions in the child or communicate the learned mock calls to
177 the parent some other way.
178 */
179 xEnsure(Mocks, learning_mocks_survive_termination) {
180 int *ip = 0;
181 cgreen_mocks_are(learning_mocks);
182 string_out(1);
183 *ip = 0;
184 }
185
186 static void simple_mocked_function(int first, int second) {
187 mock(first, second);
188 }
189
190 Ensure(Mocks, constraint_number_of_calls_when_not_called_enough_times) {
191 expect(simple_mocked_function, times(2));
192 simple_mocked_function(1, 2);
193 }
194
195 Ensure(Mocks, constraint_number_of_calls_out_of_order_expectations_fail) {
196 expect(simple_mocked_function, when(first, is_equal_to(1)), times(1));
197 expect(simple_mocked_function, when(first, is_equal_to(2)), times(1));
198 simple_mocked_function(2, 2);
199 simple_mocked_function(1, 2);
200 }