Debian Codegraph @

doc/tutorial_src/side_effect.c

raw symbols SCIP
1 #include <cgreen/cgreen.h>
2 #include <cgreen/mocks.h>
3
4 static int reader(void *stream) {
5 return (int)mock(stream);
6 }
7
8 static void writer(void *stream, char *paragraph) {
9 mock(stream, paragraph);
10 }
11
12 char *read_paragraph(int (*read)(void *), void *stream);
13
14 void by_paragraph(int (*read)(void *), void *in, void (*write)(void *, char *), void *out) {
15 while (1) {
16 char *line = read_paragraph(read, in);
17 if ((line == NULL) || (strlen(line) == 0)) {
18 return;
19 }
20 (*write)(out, line);
21 free(line);
22 }
23 }
24
25 static int stub_stream(void *stream) {
26 return (int)mock(stream);
27 }
28
29 static void update_counter(void * counter) {
30 *(int*)counter = *(int*)counter + 1;
31 }
32
33 Ensure(using_side_effect) {
34 int number_of_times_reader_was_called = 0;
35 expect(reader, will_return('\n'));
36 always_expect(reader,
37 will_return(EOF),
38 with_side_effect(&update_counter,
39 &number_of_times_reader_was_called));
40 never_expect(writer);
41 by_paragraph(&reader, NULL, &writer, NULL);
42
43 assert_that(number_of_times_reader_was_called, is_equal_to(1));
44 }