File: side_effect.c

package info (click to toggle)
cgreen 1.6.4-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,588 kB
  • sloc: ansic: 12,276; sh: 558; makefile: 474; cpp: 403; python: 181; xml: 33; sed: 13
file content (44 lines) | stat: -rw-r--r-- 1,166 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <cgreen/cgreen.h>
#include <cgreen/mocks.h>

static int reader(void *stream) {
    return (int)mock(stream);
}

static void writer(void *stream, char *paragraph) {
    mock(stream, paragraph);
}

char *read_paragraph(int (*read)(void *), void *stream);

void by_paragraph(int (*read)(void *), void *in, void (*write)(void *, char *), void *out) {
    while (1) {
        char *line = read_paragraph(read, in);
        if ((line == NULL) || (strlen(line) == 0)) {
            return;
        }
        (*write)(out, line);
        free(line);
    }
}

static int stub_stream(void *stream) {
    return (int)mock(stream);
}

static void update_counter(void * counter) {
    *(int*)counter = *(int*)counter + 1;
}

Ensure(using_side_effect) {
    int number_of_times_reader_was_called = 0;
    expect(reader, will_return('\n'));
    always_expect(reader,
                  will_return(EOF),
                  with_side_effect(&update_counter,
                                  &number_of_times_reader_was_called));
    never_expect(writer);
    by_paragraph(&reader, NULL, &writer, NULL);

    assert_that(number_of_times_reader_was_called, is_equal_to(1));
}