| 1 |
#include <cgreen/cgreen.h> |
| 2 |
#include <cgreen/mocks.h> |
| 3 |
|
| 4 |
char *read_paragraph(int (*read)(void *), void *stream); |
| 5 |
|
| 6 |
static int stream_stub(void *stream) { |
| 7 |
return (int)mock(stream); |
| 8 |
} |
| 9 |
|
| 10 |
Describe(ParagraphReader); |
| 11 |
BeforeEach(ParagraphReader) {} |
| 12 |
AfterEach(ParagraphReader) {} |
| 13 |
|
| 14 |
Ensure(ParagraphReader, gives_null_when_reading_empty_stream) { |
| 15 |
always_expect(stream_stub, will_return(EOF)); |
| 16 |
assert_that(read_paragraph(&stream_stub, NULL), is_null); |
| 17 |
} |
| 18 |
|
| 19 |
Ensure(ParagraphReader, gives_one_character_line_for_one_character_stream) { |
| 20 |
expect(stream_stub, will_return('a')); |
| 21 |
expect(stream_stub, will_return(EOF)); |
| 22 |
char *line = read_paragraph(&stream_stub, NULL); |
| 23 |
assert_that(line, is_equal_to_string("a")); |
| 24 |
free(line); |
| 25 |
} |
| 26 |
|
| 27 |
Ensure(ParagraphReader, gives_one_word_line_for_one_word_stream) { |
| 28 |
expect(stream_stub, will_return('t')); |
| 29 |
expect(stream_stub, will_return('h')); |
| 30 |
expect(stream_stub, will_return('e')); |
| 31 |
always_expect(stream_stub, will_return(EOF)); |
| 32 |
assert_that(read_paragraph(&stream_stub, NULL), is_equal_to_string("the")); |
| 33 |
} |
| 34 |
|
| 35 |
Ensure(ParagraphReader, drops_line_ending_from_word_and_stops) { |
| 36 |
expect(stream_stub, will_return('t')); |
| 37 |
expect(stream_stub, will_return('h')); |
| 38 |
expect(stream_stub, will_return('e')); |
| 39 |
expect(stream_stub, will_return('\n')); |
| 40 |
assert_that(read_paragraph(&stream_stub, NULL), is_equal_to_string("the")); |
| 41 |
} |
| 42 |
|
| 43 |
Ensure(ParagraphReader, gives_empty_line_for_single_line_ending) { |
| 44 |
expect(stream_stub, will_return('\n')); |
| 45 |
assert_that(read_paragraph(&stream_stub, NULL), is_equal_to_string("")); |
| 46 |
} |