Commit d0d72f6
Changed files (7)
.gitignore
@@ -0,0 +1,2 @@
+*.o
+main
.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
main.c
@@ -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());
+}
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
words.c
@@ -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);
+}
words.h
@@ -0,0 +1,2 @@
+int split_words(char *sentence);
+void words(const char *sentence, void (*callback)(const char *, void *), void *memo);
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;
+}