13 out->fail_msg = g_string_new(
14 "No failures recorded. This message should never print."
16 out->expected_calls = NULL;
17 out->actual_calls = NULL;
20 void Mock_destroy(
Mock_s *
self)
24 (GDestroyNotify)RecordedCall_destroy);
27 (GDestroyNotify)RecordedCall_destroy);
29 self->fail_msg,
true);
34 self->expected_calls = g_list_append(
41 self->actual_calls = g_list_append(
48 self->inputs = g_list_append(
55 char const * ListAllCalls(
Mock_s *
self)
62 GList *expected_calls =
self->expected_calls;
63 GList *actual_calls =
self->actual_calls;
64 g_string_append_printf( self->fail_msg,
67 while (expected_calls != NULL) {
69 g_string_append_printf( self->fail_msg,
" `%s()`", this_call->name);
70 expected_calls = expected_calls->next;
72 g_string_append_printf( self->fail_msg,
75 while (actual_calls != NULL) {
77 g_string_append_printf( self->fail_msg,
" `%s()`", this_call->name);
78 actual_calls = actual_calls->next;
80 return self->fail_msg->str;
88 bool SilentAssertCall(
94 bool call_n_matches_name =
false;
96 uint16_t this_call_num = 0;
97 GList *actual_calls =
self->actual_calls;
98 while( (actual_calls != NULL) && (++this_call_num < n)){
99 actual_calls = actual_calls->next;
101 if (actual_calls != NULL)
104 if (0 == g_strcmp0(name, nth_call->name))
106 call_n_matches_name =
true;
109 return call_n_matches_name;
118 printf(
"\n- Call %d is named %s? ",n,name);
121 bool call_n_matches_name =
false;
124 uint16_t this_call_num = 0;
126 GList *actual_calls =
self->actual_calls;
127 while( (actual_calls != NULL) && (++this_call_num < n)){
128 actual_calls = actual_calls->next;
130 if (actual_calls == NULL)
132 printf(
"Error: less than %d calls.\n", n);
144 if (0 == g_strcmp0(name, nth_call->name))
146 call_n_matches_name =
true;
151 printf(
"\n - No, call %d is %s.\n", this_call_num, nth_call->name);
155 return call_n_matches_name;
170 printf(
"\n- Value passed to call %d, arg %d? ",call_n,arg_k);
171 bool arg_k_matches_value =
false;
173 uint8_t this_call_num = 0;
175 GList *actual_calls =
self->actual_calls;
176 while( (actual_calls != NULL) && (++this_call_num < call_n)){
177 actual_calls = actual_calls->next;
179 if (actual_calls == NULL)
181 printf(
"Error: less than %d calls.\n", call_n);
186 GList *inputs = nth_call->inputs;
189 uint8_t this_arg_num = 0;
190 while( (inputs != NULL) && (++this_arg_num < arg_k)){
191 inputs = inputs->next;
195 printf(
"Error: call %d has less than %d args.\n", this_call_num, arg_k);
205 arg_k_matches_value = kth_arg->CheckArg(kth_arg, p_assert_val);
207 GString *printed_arg = g_string_new(
" ");
208 kth_arg->Print(printed_arg, kth_arg);
212 printf(
" - %s is arg %d to call %s.\n", printed_arg->str, arg_k, nth_call->name);
216 return arg_k_matches_value;
229 bool AssertArgPointsToValue(
246 printf(
"Value at address passed to call %d, arg %d? ",call_n,arg_k);
247 bool arg_k_matches_value =
false;
249 uint8_t this_call_num = 0;
251 GList *actual_calls =
self->actual_calls;
252 while( (actual_calls != NULL) && (++this_call_num < call_n)){
253 actual_calls = actual_calls->next;
255 if (actual_calls == NULL)
257 printf(
"Error: less than %d calls.\n", call_n);
262 GList *inputs = nth_call->inputs;
265 uint8_t this_arg_num = 0;
266 while( (inputs != NULL) && (++this_arg_num < arg_k)){
267 inputs = inputs->next;
271 printf(
"Error: call %d has less than %d args.\n", this_call_num, arg_k);
284 arg_k_matches_value = kth_arg->CheckValArgPointsTo(kth_arg, pp_assert_val);
287 GString *printed_arg = g_string_new(
" ");
288 kth_arg->Print(printed_arg, kth_arg);
292 printf(
"%s.\n", printed_arg->str);
295 return arg_k_matches_value;
297 uint16_t NumberOfActualCalls(
Mock_s *
self)
300 GList *actual_calls =
self->actual_calls;
301 uint16_t num_actual_calls = g_list_length(actual_calls);
302 return num_actual_calls;
305 static void PrintAllInputs(GList *inputs)
307 GString *printed_arg = g_string_new(
308 "Error: `RecordedArg->Print` should have overwritten this message."
310 while (inputs != NULL)
313 (this_arg->Print(printed_arg, this_arg));
314 printf(
", %s", printed_arg->str);
316 inputs = inputs->next;
319 g_string_free(printed_arg,
true);
321 void PrintAllCalls(
Mock_s *
self)
323 GList *expected_calls =
self->expected_calls;
324 GList *actual_calls =
self->actual_calls;
325 uint8_t num_expected_calls = g_list_length(expected_calls);
326 uint8_t num_actual_calls = g_list_length(actual_calls);
328 printf(
"\n- Expected %d calls:\n", num_expected_calls);
329 while (expected_calls != NULL) {
331 printf(
" - \"%s\"", this_call->name);
332 PrintAllInputs(this_call->inputs);
333 expected_calls = expected_calls->next;
335 printf(
"- Received %d calls:\n", num_actual_calls);
336 while (actual_calls != NULL) {
338 printf(
" - \"%s\"", this_call->name);
339 PrintAllInputs(this_call->inputs);
340 actual_calls = actual_calls->next;
345 static bool Each_call_has_the_correct_number_of_inputs(
Mock_s *
self);
346 static void print_first_call_with_wrong_number_of_inputs(
Mock_s *
self);
349 static bool Call_lists_are_the_same_length(
Mock_s *
self);
350 static bool EachCallMatches(
Mock_s *
self);
351 static void print_number_of_expected_and_actual_calls(
Mock_s *
self);
352 static void print_first_unexpected_call(
Mock_s *
self);
353 static void print_first_missed_call(
Mock_s *
self);
354 static void print_all_wrong_calls(
Mock_s *
self);
356 bool RanAsHoped(
Mock_s *
self)
360 g_string_printf( self->fail_msg,
" ");
362 if (!Call_lists_are_the_same_length(
self))
364 print_number_of_expected_and_actual_calls(
self);
365 print_first_unexpected_call(
self);
366 print_first_missed_call(
self);
368 if (!Each_call_has_the_correct_number_of_inputs(
self))
369 print_first_call_with_wrong_number_of_inputs(
self);
370 if (!EachCallMatches(
self))
371 print_all_wrong_calls(
self);
373 Call_lists_are_the_same_length(
self) &&
374 Each_call_has_the_correct_number_of_inputs(
self) &&
375 EachCallMatches(
self)
378 char const * WhyDidItFail(
Mock_s *
self)
380 return self->fail_msg->str;
383 static bool Call_lists_are_the_same_length(
Mock_s *
self)
386 g_list_length(self->expected_calls)
388 g_list_length(self->actual_calls)
391 static bool Each_call_has_the_correct_number_of_inputs(
Mock_s *
self)
393 GList *expected_calls =
self->expected_calls;
394 GList *actual_calls =
self->actual_calls;
395 while (expected_calls != NULL && actual_calls != NULL)
399 if (NumberOfArgs(this_expected_call) != NumberOfArgs(this_actual_call))
402 expected_calls = expected_calls->next;
403 actual_calls = actual_calls->next;
407 static bool CallNamesMatch(
char const *expected_name,
char const *actual_name)
409 int names_do_not_match = g_strcmp0(expected_name, actual_name);
410 return names_do_not_match ? false :
true;
416 if (expected_arg->Match != actual_arg->Match)
return false;
418 return expected_arg->Match(expected_arg, actual_arg);
420 static bool CallInputsMatch(GList *expected_inputs, GList *actual_inputs)
422 while (expected_inputs != NULL && actual_inputs != NULL)
426 if (!RecordedArgsMatch(
428 this_actual_arg))
return false;
430 expected_inputs = expected_inputs->next;
431 actual_inputs = actual_inputs->next;
436 static bool EachCallMatches(
Mock_s *
self)
438 GList *expected_calls =
self->expected_calls;
439 GList *actual_calls =
self->actual_calls;
440 while (expected_calls != NULL && actual_calls != NULL)
445 this_expected_call->name,
446 this_actual_call->name))
return false;
447 if (!CallInputsMatch(
448 this_expected_call->inputs,
449 this_actual_call->inputs))
return false;
451 expected_calls = expected_calls->next;
452 actual_calls = actual_calls->next;
456 static void print_number_of_expected_and_actual_calls(
Mock_s *
self)
458 GList *expected_calls =
self->expected_calls;
459 GList *actual_calls =
self->actual_calls;
460 uint8_t num_expected_calls = g_list_length(expected_calls);
461 uint8_t num_actual_calls = g_list_length(actual_calls);
462 g_string_append_printf( self->fail_msg,
463 "Expected %d calls, received %d calls. ",
464 num_expected_calls, num_actual_calls
468 return g_list_length(self->inputs);
470 static void print_first_call_with_wrong_number_of_inputs(
Mock_s *
self)
472 GList *expected_calls =
self->expected_calls;
473 GList *actual_calls =
self->actual_calls;
475 while (expected_calls != NULL && actual_calls != NULL)
479 if (NumberOfArgs(this_expected_call) != NumberOfArgs(this_actual_call))
480 g_string_append_printf( self->fail_msg,
481 "Wrong number of args in call #%d '%s', expected %d, was %d. ",
483 this_expected_call->name,
484 NumberOfArgs(this_expected_call),
485 NumberOfArgs(this_actual_call)
488 expected_calls = expected_calls->next;
489 actual_calls = actual_calls->next;
492 static void print_first_unexpected_call(
Mock_s *
self)
494 GList *expected_calls =
self->expected_calls;
495 GList *actual_calls =
self->actual_calls;
496 uint8_t num_expected_calls = g_list_length(expected_calls);
497 uint8_t num_actual_calls = g_list_length(actual_calls);
498 if (num_expected_calls < num_actual_calls)
499 g_string_append_printf( self->fail_msg,
500 "First unexpected call: received #%d:'%s'. ",
501 num_expected_calls + 1,
503 actual_calls, num_expected_calls)
507 static void print_first_missed_call(
Mock_s *
self)
509 GList *expected_calls =
self->expected_calls;
510 GList *actual_calls =
self->actual_calls;
511 uint8_t num_expected_calls = g_list_length(expected_calls);
512 uint8_t num_actual_calls = g_list_length(actual_calls);
513 if (num_expected_calls > num_actual_calls)
514 g_string_append_printf( self->fail_msg,
515 "First missed call: expected #%d:'%s'. ",
516 num_actual_calls + 1,
518 expected_calls, num_actual_calls)
526 static void PrintExpectedAndActualNames( GString *fail_msg,
527 uint8_t call_number,
char const *expected_name,
char const *actual_name)
529 g_string_append_printf( fail_msg,
530 "Call #%d: expected '%s', was '%s'. ",
531 call_number, expected_name, actual_name);
533 static void PrintExpectedAndActualInputs(GString *fail_msg,
534 uint8_t call_number,
char const *expected_name, GList *expected_inputs, GList *actual_inputs)
536 GString *printed_expected_arg = g_string_new(
537 "Error: `RecordedArg->Print` should have overwritten this message."
539 GString *printed_actual_arg = g_string_new(
540 "Error: `RecordedArg->Print` should have overwritten this message."
542 while (expected_inputs != NULL && actual_inputs != NULL)
546 if (!RecordedArgsMatch(
550 this_expected_arg->Print(
551 printed_expected_arg, this_expected_arg);
552 this_actual_arg->Print(
553 printed_actual_arg, this_actual_arg);
554 g_string_append_printf(fail_msg,
555 "Call #%d: '%s' expected input '%s', but was '%s'. ",
556 call_number, expected_name,
557 printed_expected_arg->str,
558 printed_actual_arg->str);
560 expected_inputs = expected_inputs->next;
561 actual_inputs = actual_inputs->next;
563 g_string_free(printed_expected_arg,
true);
564 g_string_free(printed_actual_arg,
true);
566 static void print_all_wrong_calls(
Mock_s *
self)
568 uint8_t call_number = 1;
569 GList *expected_calls =
self->expected_calls;
570 GList *actual_calls =
self->actual_calls;
571 while (expected_calls!=NULL && actual_calls!=NULL)
576 this_expected_call->name,
577 this_actual_call->name)
578 ) PrintExpectedAndActualNames(
581 this_expected_call->name,
582 this_actual_call->name);
583 if (!CallInputsMatch(
584 this_expected_call->inputs,
585 this_actual_call->inputs)
586 ) PrintExpectedAndActualInputs(
588 call_number, this_expected_call->name,
589 this_expected_call->inputs,
590 this_actual_call->inputs);
592 expected_calls = expected_calls->next;
593 actual_calls = actual_calls->next;