Debian Codegraph @

src/mocks.c

raw symbols SCIP
1 #include <assert.h>
2 #include <cgreen/breadcrumb.h>
3 #include <cgreen/mocks.h>
4 #include <cgreen/boxed_double.h>
5 #include <inttypes.h>
6 // TODO: report PC-Lint bug about undeserved 451
7 #include <stdarg.h>
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <string.h>
11
12 #include "cgreen_value_internal.h"
13 #include "cgreen/cgreen_value.h"
14 #include "parameters.h"
15 #include "constraint_internal.h"
16 #include "utils.h"
17 #include "runner.h"
18
19
20 #ifdef __ANDROID__
21 #include "cgreen/internal/android_headers/androidcompat.h"
22 #endif // #ifdef __ANDROID__
23
24 typedef struct RecordedExpectation_ {
25 const char *function;
26 const char *test_file;
27 int test_line;
28 int time_to_live;
29 CgreenVector *constraints;
30 int number_times_called;
31 /*
32 * Used to record the number of time this particular expectation was triggered.
33 * The main use at this point is to ensure that never_expect only adds itself
34 * as a successful test if it as never been called
35 */
36 int times_triggered;
37 } RecordedExpectation;
38
39 const int UNLIMITED_TIME_TO_LIVE = 0x0f314159;
40
41 static CgreenMockMode cgreen_mocks_are_ = strict_mocks;
42 static CgreenVector *learned_mock_calls = NULL;
43 static CgreenVector *successfully_mocked_calls = NULL;
44 static CgreenVector *global_expectation_queue = NULL;
45
46 static CgreenVector *create_vector_of_actuals(va_list actuals, int count);
47 static CgreenVector *create_equal_value_constraints_for(CgreenVector *parameter_names,
48 CgreenVector *actual_values);
49 static CgreenVector *create_constraints_vector(void);
50 static RecordedExpectation *create_recorded_expectation(const char *function, const char *test_file,
51 int test_line, CgreenVector *constraints);
52 static CgreenVector *constraints_vector_from_va_list(va_list constraints);
53 static void destroy_expectation(RecordedExpectation *expectation);
54 static void ensure_expectation_queue_exists(void);
55 static void ensure_learned_mock_calls_list_exists(void);
56 static void ensure_successfully_mocked_calls_list_exists(void);
57 static void remove_expectation_for(const char *function);
58 static void trigger_unfulfilled_expectations(CgreenVector *expectation_queue, TestReporter *reporter);
59 static RecordedExpectation *find_expectation(const char *function);
60 static void apply_any_read_only_parameter_constraints(RecordedExpectation *expectation,
61 const char *parameter,
62 CgreenValue actual,
63 TestReporter* test_reporter);
64 static void apply_any_content_setting_parameter_constraints(RecordedExpectation *expectation,
65 const char *parameter,
66 CgreenValue actual,
67 TestReporter* test_reporter);
68 static CgreenValue stored_result_or_default_for(CgreenVector* constraints);
69 static int number_of_parameters_in(const char *parameter_list);
70 static bool is_always_call(RecordedExpectation* expectation);
71 static bool have_always_expectation_for(const char* function);
72 static bool is_never_call(RecordedExpectation* expectation);
73 static bool have_never_call_expectation_for(const char* function);
74 static bool remove_never_call_expectation_for(const char* function);
75
76 static void report_violated_never_call(TestReporter*, RecordedExpectation*);
77 static void report_unexpected_call(TestReporter*, RecordedExpectation*);
78 static void report_mock_parameter_name_not_found(TestReporter *test_reporter,
79 RecordedExpectation *expectation,
80 const char *parameter);
81 static void destroy_expectation_if_time_to_die(RecordedExpectation *expectation);
82
83 static bool is_side_effect_constraint(const Constraint *constraint);
84 static void apply_side_effect(TestReporter *test_reporter,
85 const RecordedExpectation *expectation,
86 Constraint *constraint);
87
88 void cgreen_mocks_are(CgreenMockMode mock_mode) {
89 cgreen_mocks_are_ = mock_mode;
90 }
91
92
93 static int number_of_parameters_in(const char *parameter_list) {
94 int count = 1;
95 const char *current = parameter_list;
96
97 if (strlen(parameter_list) == 0) return 0;
98
99 while (*current != '\0') {
100 if (*current == ',') count++;
101 current++;
102 }
103
104 return count;
105 }
106
107 /* Not used anywhere, but might become handy so make it non-static to avoid warnings */
108 int number_of_parameter_constraints_in(const CgreenVector* constraints) {
109 int i, parameters = 0;
110
111 for (i = 0; i < cgreen_vector_size(constraints); i++) {
112 Constraint *constraint = (Constraint *)cgreen_vector_get(constraints, i);
113
114 if (is_comparing(constraint)) {
115 parameters++;
116 }
117 }
118
119 return parameters;
120 }
121
122 static void learn_mock_call_for(const char *function, const char *mock_file, int mock_line,
123 CgreenVector *parameter_names, CgreenVector *actual_values) {
124 CgreenVector *constraints = create_equal_value_constraints_for(parameter_names, actual_values);
125
126 RecordedExpectation *expectation = create_recorded_expectation(function, mock_file, mock_line,
127 constraints);
128 ensure_learned_mock_calls_list_exists();
129 cgreen_vector_add(learned_mock_calls, (void*)expectation);
130 }
131
132 static void handle_missing_expectation_for(const char *function, const char *mock_file, int mock_line,
133 CgreenVector *parameter_names, CgreenVector *actual_values,
134 TestReporter *test_reporter) {
135 RecordedExpectation *expectation;
136 CgreenVector *no_constraints;
137
138 switch (cgreen_mocks_are_) {
139 case loose_mocks:
140 break;
141
142 case learning_mocks:
143 learn_mock_call_for(function, mock_file, mock_line, parameter_names, actual_values);
144 break;
145
146 case strict_mocks:
147 no_constraints = create_constraints_vector();
148 expectation = create_recorded_expectation(function, mock_file, mock_line, no_constraints);
149 report_unexpected_call(test_reporter, expectation);
150
151 destroy_expectation(expectation);
152 break;
153 }
154 }
155
156
157 static CgreenValue convert_boxed_double_to_cgreen_value(CgreenValue actual) {
158 actual.type = CGREEN_DOUBLE;
159 actual.value.double_value = unbox_double(actual.value.integer_value);
160 return actual;
161 }
162
163 static bool parameter_is_boxed_double(CgreenVector *double_markers, int i) {
164 return *(bool*)cgreen_vector_get(double_markers, i) == true;
165 }
166
167
168 static void convert_boxed_doubles_to_cgreen_values(const char *parameters, CgreenVector *actual_values) {
169 CgreenVector *double_markers;
170 int i;
171 /* Since the caller must use 'box_double()' to pass doubles as
172 arguments to 'mock()' we can know which ones are such parameters
173 to be able to convert them to CgreenValues here */
174 double_markers = create_vector_of_double_markers_for(parameters);
175 for (i = 0; i < cgreen_vector_size(double_markers); i++) {
176 if (parameter_is_boxed_double(double_markers, i)) {
177 CgreenValue *actual = cgreen_vector_get(actual_values, i);
178 *actual = convert_boxed_double_to_cgreen_value(*actual);
179 }
180 }
181 destroy_cgreen_vector(double_markers);
182 }
183
184
185 intptr_t mock_(TestReporter* test_reporter, const char *function, const char *mock_file, int mock_line, const char *parameters, ...) {
186 va_list actuals;
187 CgreenVector *actual_values;
188 CgreenVector *parameter_names;
189 int failures_before_read_only_constraints_executed;
190 int failures_after_read_only_constraints_executed;
191 CgreenValue stored_result;
192 RecordedExpectation *expectation = find_expectation(function);
193
194 parameter_names = create_vector_of_names(parameters);
195
196 va_start(actuals, parameters);
197 actual_values = create_vector_of_actuals(actuals, number_of_parameters_in(parameters));
198 va_end(actuals);
199
200 convert_boxed_doubles_to_cgreen_values(parameters, actual_values);
201
202 if (expectation == NULL) {
203 handle_missing_expectation_for(function, mock_file, mock_line, parameter_names, actual_values, test_reporter);
204 destroy_cgreen_vector(actual_values);
205 destroy_cgreen_vector(parameter_names);
206 return 0;
207 }
208
209 if (is_never_call(expectation)) {
210 expectation->times_triggered++;
211 report_violated_never_call(test_reporter, expectation);
212 destroy_cgreen_vector(actual_values);
213 destroy_cgreen_vector(parameter_names);
214 return 0;
215 }
216
217 ensure_successfully_mocked_calls_list_exists();
218 cgreen_vector_add(successfully_mocked_calls, (void*)function);
219
220 stored_result = stored_result_or_default_for(expectation->constraints);
221 // FIXME: Should verify that return value is not a DOUBLE as `mock_()' can not
222 // return them. There should also be a 'mock_double_()' which does the same except
223 // returning a double.
224
225 // First check all actual constraints for validity...
226 for (int i = 0; i < cgreen_vector_size(expectation->constraints); i++) {
227 Constraint *constraint = (Constraint *)cgreen_vector_get(expectation->constraints, i);
228
229 if (constraint->type == CGREEN_CALL_COUNTER_CONSTRAINT) {
230 expectation->number_times_called++;
231 continue;
232 }
233
234 if (!is_parameter(constraint))
235 continue;
236
237 if (!constraint_is_for_parameter_in(constraint, parameters)) {
238 // if expectation parameter name isn't in parameter_names,
239 // fail test and skip applying constraints unlikely to match
240 report_mock_parameter_name_not_found(test_reporter, expectation, constraint->parameter_name);
241 destroy_expectation_if_time_to_die(expectation);
242 destroy_cgreen_vector(actual_values);
243 destroy_cgreen_vector(parameter_names);
244
245 return stored_result.value.integer_value;
246 }
247 }
248
249 // Now we can do the read-only constraints. If read-only
250 // constraints aren't matching, content-setting ones might corrupt
251 // memory so apply read-only ones first, and if they don't fail,
252 // then do the deeper constraints
253 failures_before_read_only_constraints_executed = test_reporter->failures;
254
255 for (int i = 0; i < cgreen_vector_size(parameter_names); i++) {
256 const char* parameter_name = (const char*)cgreen_vector_get(parameter_names, i);
257 CgreenValue actual = *(CgreenValue*)cgreen_vector_get(actual_values, i);
258 apply_any_read_only_parameter_constraints(expectation, parameter_name, actual, test_reporter);
259 }
260
261 failures_after_read_only_constraints_executed = test_reporter->failures;
262
263 // And now we can do the content setting constraints...
264 // FIXME: this comparison doesn't work because only parent
265 // processes' pass/fail counts are updated, and even then
266 // only once they read from the pipe
267 if (failures_before_read_only_constraints_executed == failures_after_read_only_constraints_executed) {
268 for (int i = 0; i < cgreen_vector_size(parameter_names); i++) {
269 const char* parameter_name = (const char*)cgreen_vector_get(parameter_names, i);
270 CgreenValue actual = *(CgreenValue*)cgreen_vector_get(actual_values, i);
271 apply_any_content_setting_parameter_constraints(expectation, parameter_name, actual, test_reporter);
272 }
273 }
274
275 // And finally run all side effects
276 for (int i = 0; i < cgreen_vector_size(expectation->constraints); i++) {
277 Constraint *constraint = (Constraint *)cgreen_vector_get(expectation->constraints, i);
278
279 if (is_side_effect_constraint(constraint)) {
280 apply_side_effect(test_reporter, expectation, constraint);
281 continue;
282 }
283 }
284
285
286
287 destroy_cgreen_vector(parameter_names);
288 destroy_cgreen_vector(actual_values);
289
290 expectation->times_triggered++;
291 destroy_expectation_if_time_to_die(expectation);
292
293 if (stored_result.type == CGREEN_DOUBLE) {
294 #ifdef FUTURE
295 /* TODO: for some future version we should ensure that the
296 user is not trying to return a double through 'mock()' when
297 there is a 'mock_double()' available, which there isn't
298 yet. So then
299
300 return unbox_double(mock(...));
301
302 should be replaced by
303
304 return mock_double(...);
305 */
306 test_reporter->assert_true(test_reporter,
307 mock_file,
308 mock_line,
309 false,
310 "Mocked function [%s] have a 'will_return_double()' expectation, "
311 "but 'mock()' cannot return doubles; Use 'mock_double()' instead",
312 function);
313 /* But we'll return it anyway, whatever it becomes is what he will get... */
314 return stored_result.value.double_value;
315 #else
316 /* ... but for now return a boxed double since the user is probably using
317
318 return unbox_double(mock(...));
319
320 as is the standard way in 1.x
321 */
322 return box_double(stored_result.value.double_value);
323 #endif
324 } else
325 return stored_result.value.integer_value;
326 }
327
328 static void apply_side_effect(TestReporter *test_reporter,
329 const RecordedExpectation *expectation,
330 Constraint *constraint)
331 {
332 CgreenValue actual = {};
333 constraint->execute(
334 constraint,
335 expectation->function,
336 actual,
337 expectation->test_file,
338 expectation->test_line,
339 test_reporter);
340 }
341
342 static bool is_side_effect_constraint(const Constraint *constraint) {
343 return constraint->type == CGREEN_CALL_CONSTRAINT;
344 }
345
346 static CgreenVector *create_vector_of_actuals(va_list actuals, int count) {
347 int i;
348 CgreenVector *actual_values = create_cgreen_vector(free);
349 for (i = 0; i < count; i++) {
350 uintptr_t actual = va_arg(actuals, uintptr_t);
351 cgreen_vector_add(actual_values, (void*)create_cgreen_value(make_cgreen_integer_value(actual)));
352 }
353 return actual_values;
354 }
355
356
357 static Constraint *create_appropriate_equal_constraint_for(const char *parameter_name,
358 CgreenValue actual) {
359 Constraint *constraint;
360 if (actual.type == CGREEN_DOUBLE)
361 constraint = create_equal_to_double_constraint(actual.value.double_value,
362 parameter_name);
363 else
364 constraint = create_equal_to_value_constraint(actual.value.integer_value,
365 parameter_name);
366 return constraint;
367 }
368
369 static CgreenVector *create_equal_value_constraints_for(CgreenVector *parameter_names,
370 CgreenVector *actual_values) {
371 int i;
372 CgreenVector *constraints = create_constraints_vector();
373 for (i = 0; i < cgreen_vector_size(parameter_names); i++) {
374 const char* parameter_name = (const char*)cgreen_vector_get(parameter_names, i);
375 CgreenValue actual = *(CgreenValue*)cgreen_vector_get(actual_values, i);
376 Constraint *constraint = create_appropriate_equal_constraint_for(parameter_name, actual);
377 cgreen_vector_add(constraints, constraint);
378 }
379 return constraints;
380 }
381
382
383 Constraint *when_(const char *parameter, Constraint* constraint) {
384 constraint->parameter_name = parameter;
385 return constraint;
386 }
387
388
389
390 void test_times_called(Constraint *constraint, const char *function, CgreenValue actual,
391 const char *test_file, int test_line, TestReporter *reporter) {
392 char *message;
393 char parameter_name_actual_string[255];
394
395 snprintf(parameter_name_actual_string, sizeof(parameter_name_actual_string) - 1, "%s", function);
396 message = constraint->failure_message(constraint, parameter_name_actual_string, actual.value.integer_value);
397
398 (*reporter->assert_true)(
399 reporter,
400 test_file,
401 test_line,
402 (*constraint->compare)(constraint, actual),
403 message);
404
405 free(message);
406 }
407
408 Constraint *times_(const int number_times_called) {
409 Constraint * time_constraint = create_constraint();
410 time_constraint->expected_value = make_cgreen_integer_value(number_times_called);
411 time_constraint->expected_value_name = string_dup("times");
412 time_constraint->type = CGREEN_CALL_COUNTER_CONSTRAINT;
413
414 time_constraint->compare = &compare_want_value;
415 time_constraint->execute = &test_times_called;
416 time_constraint->name = "be called";
417 time_constraint->size_of_expected_value = sizeof(intptr_t);
418 time_constraint->expected_value_message = "\t\texpected to have been called:\t[%" PRIdPTR "] times";
419 return time_constraint;
420 }
421
422 static void destroy_expectation_if_time_to_die(RecordedExpectation *expectation) {
423
424 if (is_always_call(expectation)) {
425 return;
426 }
427
428 /* TODO: expectation->decrement_time_to_live_if_necessary(); if (expectation->time_to_die()) { ... } */
429 expectation->time_to_live--;
430
431 if (expectation->time_to_live <= 0) {
432 remove_expectation_for(expectation->function);
433 destroy_expectation(expectation);
434 }
435 }
436
437 void expect_(TestReporter* test_reporter, const char *function, const char *test_file, int test_line, ...) {
438 va_list constraints;
439 RecordedExpectation *expectation;
440 CgreenVector *constraints_vector;
441
442 if (have_always_expectation_for(function)) {
443 test_reporter->assert_true(
444 test_reporter,
445 test_file,
446 test_line,
447 false,
448 "Mocked function [%s] already has an expectation that it will always be called a certain way; "
449 "any expectations declared after an always expectation are invalid", function);
450 va_start(constraints, test_line);
451 destroy_constraints(constraints);
452 va_end(constraints);
453
454 return;
455 }
456
457 if (have_never_call_expectation_for(function)) {
458 remove_never_call_expectation_for(function);
459
460 test_reporter->assert_true(
461 test_reporter,
462 test_file,
463 test_line,
464 false,
465 "Mocked function [%s] already has an expectation that it will never be called; "
466 "any expectations declared after a never call expectation are invalid", function);
467 va_start(constraints, test_line);
468 destroy_constraints(constraints);
469 va_end(constraints);
470
471 return;
472 }
473
474
475 va_start(constraints, test_line);
476 constraints_vector = constraints_vector_from_va_list(constraints);
477 expectation = create_recorded_expectation(function, test_file, test_line, constraints_vector);
478 va_end(constraints);
479
480 expectation->time_to_live = 1;
481 for (int i = 0 ; i < cgreen_vector_size(expectation->constraints) ; i++) {
482 Constraint * constraint = cgreen_vector_get(expectation->constraints, i);
483 if (constraint && constraint->type == CGREEN_CALL_COUNTER_CONSTRAINT) {
484 expectation->time_to_live = (int)constraint->expected_value.value.integer_value;
485 break;
486 }
487 }
488 cgreen_vector_add(global_expectation_queue, expectation);
489 }
490
491 void always_expect_(TestReporter* test_reporter, const char *function, const char *test_file, int test_line, ...) {
492 va_list constraints;
493 RecordedExpectation *expectation;
494 CgreenVector *constraints_vector;
495
496 if (have_always_expectation_for(function)) {
497 test_reporter->assert_true(
498 test_reporter,
499 test_file,
500 test_line,
501 false,
502 "Mocked function [%s] already has an expectation and will always be called a certain way; "
503 "any expectations declared after an always expectation are discarded", function);
504 va_start(constraints, test_line);
505 destroy_constraints(constraints);
506 va_end(constraints);
507
508 return;
509 }
510
511 if (have_never_call_expectation_for(function)) {
512 remove_never_call_expectation_for(function);
513
514 test_reporter->assert_true(
515 test_reporter,
516 test_file,
517 test_line,
518 false,
519 "Mocked function [%s] already has an expectation that it will never be called; "
520 "any expectations declared after a never call expectation are discarded", function);
521 va_start(constraints, test_line);
522 destroy_constraints(constraints);
523 va_end(constraints);
524
525 return;
526 }
527
528 va_start(constraints, test_line);
529 constraints_vector = constraints_vector_from_va_list(constraints);
530 expectation = create_recorded_expectation(function, test_file, test_line, constraints_vector);
531 va_end(constraints);
532 expectation->time_to_live = UNLIMITED_TIME_TO_LIVE;
533 cgreen_vector_add(global_expectation_queue, expectation);
534 }
535
536 void never_expect_(TestReporter* test_reporter, const char *function, const char *test_file, int test_line, ...) {
537 va_list constraints;
538 RecordedExpectation *expectation;
539 CgreenVector *constraints_vector;
540
541 if (have_always_expectation_for(function)) {
542 test_reporter->assert_true(
543 test_reporter,
544 test_file,
545 test_line,
546 false,
547 "Mocked function [%s] already has an expectation and will always be called a certain way; "
548 "declaring an expectation after an always expectation is not allowed", function);
549 va_start(constraints, test_line);
550 destroy_constraints(constraints);
551 va_end(constraints);
552
553 return;
554 }
555
556 if (have_never_call_expectation_for(function)) {
557 remove_never_call_expectation_for(function);
558
559 test_reporter->assert_true(
560 test_reporter,
561 test_file,
562 test_line,
563 false,
564 "Mocked function [%s] already has an expectation that it will never be called; "
565 "declaring an expectation for a function after a never call expectation is not allowed", function);
566 va_start(constraints, test_line);
567 destroy_constraints(constraints);
568 va_end(constraints);
569
570 return;
571 }
572
573 va_start(constraints, test_line);
574 constraints_vector = constraints_vector_from_va_list(constraints);
575 expectation = create_recorded_expectation(function, test_file, test_line, constraints_vector);
576 expectation->time_to_live = -UNLIMITED_TIME_TO_LIVE;
577 cgreen_vector_add(global_expectation_queue, expectation);
578 }
579
580
581 static void report_violated_never_call(TestReporter *test_reporter, RecordedExpectation* expectation) {
582 test_reporter->assert_true(
583 test_reporter,
584 expectation->test_file,
585 expectation->test_line,
586 false,
587 "Mocked function [%s] has an expectation that it will never be called, but it was",
588 expectation->function);
589 }
590
591
592 static bool successfully_mocked_call(const char *function_name) {
593 int i;
594
595 for (i = 0; i < cgreen_vector_size(successfully_mocked_calls); i++) {
596 const char *successfully_mocked_function_name = (const char *)cgreen_vector_get(successfully_mocked_calls, i);
597 if (strcmp(successfully_mocked_function_name, function_name) == 0) {
598 return true;
599 }
600 }
601
602 return false;
603 }
604
605
606 static void report_unexpected_call(TestReporter *test_reporter, RecordedExpectation* expectation) {
607 const char *message;
608 if (successfully_mocked_call(expectation->function)) {
609 message = "Mocked function [%s] was called too many times";
610 } else {
611 message = "Mocked function [%s] did not have an expectation that it would be called";
612 }
613
614 test_reporter->assert_true(
615 test_reporter,
616 current_test->filename,
617 current_test->line,
618 false,
619 message,
620 expectation->function);
621 }
622
623 void clear_mocks(void) {
624 if (global_expectation_queue != NULL) {
625 destroy_cgreen_vector(global_expectation_queue);
626 global_expectation_queue = NULL;
627 }
628
629 if (learned_mock_calls != NULL) {
630 int i;
631 for (i = 0; i < cgreen_vector_size(learned_mock_calls); i++) {
632 RecordedExpectation *expectation = (RecordedExpectation*)cgreen_vector_get(learned_mock_calls, i);
633 destroy_expectation(expectation);
634 }
635
636 destroy_cgreen_vector(learned_mock_calls);
637 learned_mock_calls = NULL;
638 }
639 }
640
641 static void show_breadcrumb(const char *name, void *memo) {
642 if (*(int *) memo > 1) {
643 fprintf(stderr, "-> ");
644 }
645 if (*(int *) memo > 0) {
646 fprintf(stderr, "%s ", name);
647 }
648 (*(int *) memo)++;
649 }
650
651
652 void print_learned_mocks(void) {
653 int e, c, i = 0;
654 CgreenBreadcrumb *breadcrumb = get_test_reporter()->breadcrumb;
655
656 walk_breadcrumb(breadcrumb, &show_breadcrumb, (void *) &i);
657
658 fprintf(stderr, ": Learned mocks are\n");
659
660 if (cgreen_vector_size(learned_mock_calls) == 0) {
661 fprintf(stderr, "\t<none>\n");
662 return;
663 }
664
665 for (e = 0; e < cgreen_vector_size(learned_mock_calls); e++) {
666 RecordedExpectation *expectation = (RecordedExpectation*)cgreen_vector_get(learned_mock_calls, e);
667 const char *function_name = expectation->function;
668 fprintf(stderr, "\texpect(%s", function_name);
669 for (c = 0; c < cgreen_vector_size(expectation->constraints); c++) {
670 Constraint *constraint = (Constraint *)cgreen_vector_get(expectation->constraints, c);
671 if (constraint->expected_value.type == CGREEN_DOUBLE)
672 fprintf(stderr, ", when(%s, is_equal_to_double(%f))", constraint->expected_value_name,
673 constraint->expected_value.value.double_value);
674 else
675 fprintf(stderr, ", when(%s, is_equal_to(%" PRIdPTR "))", constraint->expected_value_name,
676 constraint->expected_value.value.integer_value);
677 }
678 fprintf(stderr, ");\n");
679 }
680 }
681
682 void tally_mocks(TestReporter *reporter) {
683 if (cgreen_mocks_are_ == learning_mocks) {
684 print_learned_mocks();
685 }
686
687 trigger_unfulfilled_expectations(global_expectation_queue, reporter);
688 clear_mocks();
689 }
690
691
692 static CgreenVector *create_constraints_vector(void) {
693 return create_cgreen_vector((GenericDestructor)&destroy_constraint);
694 }
695
696
697 static CgreenVector *constraints_vector_from_va_list(va_list constraints) {
698 CgreenVector *vector = create_constraints_vector();
699 Constraint *constraint;
700 while ((constraint = va_arg(constraints, Constraint *)) != (Constraint *)0) {
701 cgreen_vector_add(vector, constraint);
702 }
703 return vector;
704 }
705
706
707 static RecordedExpectation *create_recorded_expectation(const char *function, const char *test_file, int test_line, CgreenVector *constraints) {
708 RecordedExpectation *expectation;
709
710 ensure_expectation_queue_exists();
711 expectation = (RecordedExpectation *)malloc(sizeof(RecordedExpectation));
712 expectation->function = function;
713 expectation->test_file = test_file;
714 expectation->test_line = test_line;
715 expectation->constraints = constraints;
716 expectation->number_times_called = 0;
717 expectation->times_triggered = 0;
718
719 return expectation;
720 }
721
722 static void destroy_expectation(RecordedExpectation *expectation) {
723 destroy_cgreen_vector(expectation->constraints);
724 expectation->constraints = NULL;
725 expectation->function = NULL;
726 expectation->test_file = NULL;
727 expectation->test_line = 0;
728 expectation->time_to_live = 0;
729 expectation->number_times_called = 0;
730 expectation->times_triggered = 0;
731
732 free(expectation);
733 }
734
735 static void ensure_successfully_mocked_calls_list_exists(void) {
736 if (successfully_mocked_calls == NULL) {
737 // successfully_mocked_calls are __func__, so there's nothing to destroy
738 successfully_mocked_calls = create_cgreen_vector(NULL);
739 }
740 }
741
742 static void ensure_learned_mock_calls_list_exists(void) {
743 if (learned_mock_calls == NULL) {
744 // learned_mock_calls are __func__, so there's nothing to destroy
745 learned_mock_calls = create_cgreen_vector(NULL);
746 }
747 }
748
749 static void ensure_expectation_queue_exists(void) {
750 if (global_expectation_queue == NULL) {
751 global_expectation_queue = create_cgreen_vector((GenericDestructor)&destroy_expectation);
752 }
753 }
754
755 static void remove_expectation_for(const char *function) {
756 int i;
757 for (i = 0; i < cgreen_vector_size(global_expectation_queue); i++) {
758 RecordedExpectation *expectation = (RecordedExpectation *)cgreen_vector_get(global_expectation_queue, i);
759
760 if (NULL == expectation) {
761 fprintf(stderr, "*** CGREEN: NULL expectation found -- maybe a previous incorrect removal?");
762 continue;
763 }
764
765 if (strcmp(expectation->function, function) == 0) {
766 cgreen_vector_remove(global_expectation_queue, i);
767 return;
768 }
769 }
770 }
771
772 static bool is_first_call_matching(RecordedExpectation *expectation) {
773 return expectation->times_triggered == 0;
774 }
775
776
777 static void trigger_unfulfilled_expectations(CgreenVector *expectation_queue, TestReporter *reporter) {
778 for (int e = 0; e < cgreen_vector_size(expectation_queue); e++) {
779 RecordedExpectation *expectation = (RecordedExpectation *)cgreen_vector_get(expectation_queue, e);
780
781 if (NULL == expectation) {
782 fprintf(stderr, "*** NULL unfulfilled cgreen expectation found -- maybe a previous incorrect removal?");
783 continue;
784 }
785
786 if (is_always_call(expectation)) {
787 continue;
788 }
789
790 if (is_never_call(expectation)) {
791 if (is_first_call_matching(expectation)) {
792 (*reporter->assert_true)(reporter,
793 expectation->test_file,
794 expectation->test_line,
795 1,
796 "The mocked function [%s] was never called", expectation->function);
797 }
798 continue;
799 }
800
801 bool call_counter_present = false;
802 for (int c = 0; c < cgreen_vector_size(expectation->constraints); c++) {
803 Constraint *constraint = (Constraint *) cgreen_vector_get(expectation->constraints, c);
804 if (constraint->type == CGREEN_CALL_COUNTER_CONSTRAINT) {
805 constraint->execute(
806 constraint,
807 expectation->function,
808 make_cgreen_integer_value(expectation->number_times_called),
809 expectation->test_file,
810 expectation->test_line,
811 get_test_reporter());
812 call_counter_present = true;
813 }
814 }
815
816 if (!call_counter_present)
817 (*reporter->assert_true)(
818 reporter,
819 expectation->test_file,
820 expectation->test_line,
821 0,
822 "Expected call was not made to mocked function [%s]", expectation->function);
823 }
824 }
825
826 static RecordedExpectation *find_expectation(const char *function) {
827 int i;
828 for (i = 0; i < cgreen_vector_size(global_expectation_queue); i++) {
829 RecordedExpectation *expectation =
830 (RecordedExpectation *)cgreen_vector_get(global_expectation_queue, i);
831
832 if (strcmp(expectation->function, function) == 0) {
833 return expectation;
834 }
835 }
836 return NULL;
837 }
838
839 static void report_mock_parameter_name_not_found(TestReporter *test_reporter, RecordedExpectation *expectation, const char *constraint_parameter_name) {
840
841 test_reporter->assert_true(
842 test_reporter,
843 expectation->test_file,
844 expectation->test_line,
845 false,
846 "Mocked function [%s] did not define a parameter named [%s]. Did you misspell it in the expectation or forget it in the mock's argument list?",
847 expectation->function,
848 constraint_parameter_name);
849 }
850
851 static void apply_any_read_only_parameter_constraints(RecordedExpectation *expectation,
852 const char *parameter,
853 CgreenValue actual,
854 TestReporter* test_reporter) {
855 int i;
856
857 for (i = 0; i < cgreen_vector_size(expectation->constraints); i++) {
858 Constraint *constraint = (Constraint *)cgreen_vector_get(expectation->constraints, i);
859
860 if (is_content_setting(constraint) || is_side_effect_constraint(constraint)) {
861 continue;
862 }
863
864 /* TODO: we need to classify constraints better, now this
865 filters on COMPARING to not execute RETURN constraints
866 here...*/
867 if (constraint_is_not_for_parameter(constraint, parameter)) {
868 continue;
869 }
870
871 constraint->execute(
872 constraint,
873 expectation->function,
874 actual,
875 expectation->test_file,
876 expectation->test_line,
877 test_reporter);
878 }
879 }
880
881 static void apply_any_content_setting_parameter_constraints(RecordedExpectation *expectation,
882 const char *parameter, CgreenValue actual,
883 TestReporter* test_reporter) {
884 for (int i = 0; i < cgreen_vector_size(expectation->constraints); i++) {
885 Constraint *constraint = (Constraint *)cgreen_vector_get(expectation->constraints, i);
886
887 if (constraint_is_not_for_parameter(constraint, parameter)) {
888 continue;
889 }
890
891 if (constraint->type != CGREEN_CONTENT_SETTER_CONSTRAINT) {
892 continue;
893 }
894
895 constraint->execute(
896 constraint,
897 expectation->function,
898 actual,
899 expectation->test_file,
900 expectation->test_line,
901 test_reporter);
902 }
903 }
904
905 static CgreenValue stored_result_or_default_for(CgreenVector* constraints) {
906 for (int i = 0; i < cgreen_vector_size(constraints); i++) {
907 Constraint *constraint = (Constraint *)cgreen_vector_get(constraints, i);
908
909 switch (constraint->type) {
910 case CGREEN_RETURN_VALUE_CONSTRAINT:
911 case CGREEN_RETURN_POINTER_CONSTRAINT:
912 return constraint->expected_value;
913 case CGREEN_RETURN_BY_VALUE_CONSTRAINT: {
914 /* When returning a struct by value we need to copy the struct
915 pointed out by the Cgreen pointer value so that it does
916 not get lost when the constraint is destroyed. The user
917 will be responsible for deallocating the copy of the struct. */
918 CgreenValue returnable = constraint->expected_value;
919 void *the_struct = malloc(returnable.value_size);
920 memcpy(the_struct, returnable.value.pointer_value, returnable.value_size);
921 returnable.value.pointer_value = the_struct;
922 return returnable;
923 }
924 default:
925 break;
926 }
927 }
928 return (CgreenValue){CGREEN_INTEGER, {0}, 0};
929 }
930
931 static bool is_always_call(RecordedExpectation* expectation) {
932 return expectation->time_to_live == UNLIMITED_TIME_TO_LIVE;
933 }
934
935 static bool have_always_expectation_for(const char* function) {
936 for (int i = 0; i < cgreen_vector_size(global_expectation_queue); i++) {
937 RecordedExpectation *expectation =
938 (RecordedExpectation *)cgreen_vector_get(global_expectation_queue, i);
939 if (strcmp(expectation->function, function) == 0) {
940 if (is_always_call(expectation)) {
941 return true;
942 }
943 }
944 }
945
946 return false;
947 }
948
949 static bool is_never_call(RecordedExpectation* expectation) {
950 return expectation->time_to_live == -UNLIMITED_TIME_TO_LIVE;
951 }
952
953 static bool have_never_call_expectation_for(const char* function) {
954 for (int i = 0; i < cgreen_vector_size(global_expectation_queue); i++) {
955 RecordedExpectation *expectation =
956 (RecordedExpectation *)cgreen_vector_get(global_expectation_queue, i);
957 if (strcmp(expectation->function, function) == 0) {
958 if (is_never_call(expectation)) {
959 return true;
960 }
961 }
962 }
963
964 return false;
965 }
966
967 static bool remove_never_call_expectation_for(const char* function) {
968 for (int i = 0; i < cgreen_vector_size(global_expectation_queue); i++) {
969 RecordedExpectation *expectation =
970 (RecordedExpectation *)cgreen_vector_get(global_expectation_queue, i);
971 if (strcmp(expectation->function, function) == 0) {
972 if (is_never_call(expectation)) {
973 cgreen_vector_remove(global_expectation_queue, i);
974 destroy_expectation(expectation);
975 }
976 }
977 }
978
979 return false;
980 }
981
982 /* vim: set ts=4 sw=4 et cindent: */