diff options
| author | mo khan <mo.khan@gmail.com> | 2020-06-15 20:18:41 -0600 |
|---|---|---|
| committer | mo khan <mo.khan@gmail.com> | 2020-06-15 20:18:41 -0600 |
| commit | d0d72f679a6a5e7ffcc65d297ed2760a611c7084 (patch) | |
| tree | f0e581f3d502512ad83397d33bd5cb86d3b728de | |
| parent | 1a7727b5c1f3cf2808e6c376f6763fbb2393e22a (diff) | |
Create example cgreen program
| -rw-r--r-- | .gitignore | 2 | ||||
| -rw-r--r-- | .gitlab-ci.yml | 10 | ||||
| -rw-r--r-- | Makefile | 26 | ||||
| -rw-r--r-- | main.c | 12 | ||||
| -rw-r--r-- | words.c | 25 | ||||
| -rw-r--r-- | words.h | 2 | ||||
| -rw-r--r-- | words_test.c | 51 |
7 files changed, 128 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..f0c9b81 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +*.o +main diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml new file mode 100644 index 0000000..ed6467b --- /dev/null +++ b/.gitlab-ci.yml @@ -0,0 +1,10 @@ +test: + stage: test + script: + - wget https://github.com/cgreen-devs/cgreen/releases/download/1.3.0/cgreen_1.3.0_amd64.deb + - dpkg -i cgreen_1.3.0_amd64.deb + - make ci + artifacts: + reports: + junit: + - ./junit/*.xml diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..58231b0 --- /dev/null +++ b/Makefile @@ -0,0 +1,26 @@ +CC=gcc + +test : main + cgreen-runner -c main + +ci : main + mkdir -p junit + cgreen-runner -c --xml=junit/ main + +run : main + ./main + +main : main.o words_test.o words.o + $(CC) main.o words_test.o words.o -lcgreen -o main + +main.o : main.c + $(CC) -c main.c + +words.o : words.c + $(CC) -c words.c + +words_test.o : words_test.c + $(CC) -c words_test.c + +clean: + rm main *.o @@ -0,0 +1,12 @@ +#include <cgreen/cgreen.h> + +TestSuite *words_tests(); + +int main(int argc, char **argv) { + TestSuite *suite = create_test_suite(); + add_suite(suite, words_tests()); + if (argc > 1) { + return run_single_test(suite, argv[1], create_text_reporter()); + } + return run_test_suite(suite, create_text_reporter()); +} @@ -0,0 +1,25 @@ +#include <stdlib.h> +#include <string.h> + +int split_words(char *sentence) { + int i, count = 1, length = strlen(sentence); + + for (i = 0; i < length; i++) { + if (sentence[i] == ' ') { + sentence[i] = '\0'; + count++; + } + } + return count; +} + +void words(const char *sentence, void (*callback)(const char *, void *), void *memo) { + char *words = strdup(sentence); + int word_count = split_words(words); + char *word = words; + while (word_count-- > 0) { + (*callback)(word, memo); + word = word + strlen(word) + 1; + } + free(words); +} @@ -0,0 +1,2 @@ +int split_words(char *sentence); +void words(const char *sentence, void (*callback)(const char *, void *), void *memo); diff --git a/words_test.c b/words_test.c new file mode 100644 index 0000000..c7af6b9 --- /dev/null +++ b/words_test.c @@ -0,0 +1,51 @@ +#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; +} |
