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 45 46 47 48 49 50 51 52
|
#include <cgreen/cgreen.h>
#include <cgreen/mocks.h>
#include "words.h"
#include <string.h>
Describe(Words);
BeforeEach(Words) {}
AfterEach(Words) {}
Ensure(Words, returns_word_count) {
char *sentence = strdup("Birds of a feather");
int word_count = split_words(sentence);
assert_that(word_count, is_equal_to(4));
free(sentence);
}
Ensure(Words, converts_spaces_to_zeroes) {
char *sentence = strdup("Birds of a feather");
split_words(sentence);
int comparison = memcmp("Birds\0of\0a\0feather", sentence, strlen(sentence));
assert_that(comparison, is_equal_to(0));
free(sentence);
}
void mocked_callback(const char *word, void *memo) {
mock(word, memo);
}
Ensure(Words, invokes_callback_once_for_single_word_sentence) {
expect(mocked_callback,
when(word, is_equal_to_string("Word")), when(memo, is_null));
words("Word", &mocked_callback, NULL);
}
Ensure(Words, invokes_callback_for_each_word_in_a_phrase) {
expect(mocked_callback, when(word, is_equal_to_string("Birds")));
expect(mocked_callback, when(word, is_equal_to_string("of")));
expect(mocked_callback, when(word, is_equal_to_string("a")));
expect(mocked_callback, when(word, is_equal_to_string("feather")));
words("Birds of a feather", &mocked_callback, NULL);
}
TestSuite *words_tests() {
TestSuite *suite = create_test_suite();
add_test_with_context(suite, Words, returns_word_count);
add_test_with_context(suite, Words, converts_spaces_to_zeroes);
add_test_with_context(suite, Words, invokes_callback_once_for_single_word_sentence);
add_test_with_context(suite, Words, invokes_callback_for_each_word_in_a_phrase);
return suite;
}
|