From 502ed6760d9d2d674d0dcc87a6cb223ff079745d Mon Sep 17 00:00:00 2001 From: "Rafael G. Martins" Date: Sat, 9 May 2015 20:09:55 -0300 Subject: loader: added tests --- Makefile.am | 3 ++ configure.ac | 21 +++++++- src/file.c | 50 ++++++++++++++++++ src/file.h | 19 +++++++ src/loader.c | 32 +----------- src/loader.h | 3 -- tests/check_loader.c | 143 +++++++++++++++++++++++++++++++++++++++++++++++++-- 7 files changed, 232 insertions(+), 39 deletions(-) create mode 100644 src/file.c create mode 100644 src/file.h diff --git a/Makefile.am b/Makefile.am index 4537fcb..0932a3b 100644 --- a/Makefile.am +++ b/Makefile.am @@ -20,6 +20,7 @@ CLEANFILES = \ noinst_HEADERS = \ src/content-parser.h \ + src/file.h \ src/error.h \ src/loader.h \ src/renderer.h \ @@ -45,6 +46,7 @@ check_PROGRAMS = \ libblogc_la_SOURCES = \ src/content-parser.c \ + src/file.c \ src/error.c \ src/loader.c \ src/renderer.c \ @@ -120,6 +122,7 @@ tests_check_loader_CFLAGS = \ tests_check_loader_LDFLAGS = \ -no-install \ + -Wl,--wrap=blogc_file_get_contents \ $(NULL) tests_check_loader_LDADD = \ diff --git a/configure.ac b/configure.ac index 716daa7..a2cace4 100644 --- a/configure.ac +++ b/configure.ac @@ -43,8 +43,25 @@ AC_ARG_ENABLE([tests], AS_HELP_STRING([--disable-tests], AS_IF([test "x$enable_tests" != "xno"], [ PKG_PROG_PKG_CONFIG PKG_CHECK_MODULES([CMOCKA], [cmocka], [ - TESTS="enabled" - have_cmocka=yes + AC_MSG_CHECKING([whether the linker supports -wrap]) + save_LDFLAGS="$LDFLAGS" + LDFLAGS="$LDFLAGS -Wl,-wrap,exit" + AC_RUN_IFELSE([ + AC_LANG_PROGRAM([ + [void __wrap_exit(int s){__real_exit(0);}] + ], [ + [exit(1);] + ]) + ], [ + TESTS="enabled" + have_cmocka=yes + ], [ + have_cmocka=no + ], [ + have_cmocka=no + ]) + AC_MSG_RESULT([$have_cmocka]) + LDFLAGS="$save_LDFLAGS" ], [ have_cmocka=no ]) diff --git a/src/file.c b/src/file.c new file mode 100644 index 0000000..7648340 --- /dev/null +++ b/src/file.c @@ -0,0 +1,50 @@ +/* + * blogc: A blog compiler. + * Copyright (C) 2015 Rafael G. Martins + * + * This program can be distributed under the terms of the BSD License. + * See the file COPYING. + */ + +#ifdef HAVE_CONFIG_H +#include +#endif /* HAVE_CONFIG_H */ + +#include +#include +#include +#include "utils/utils.h" +#include "file.h" +#include "error.h" + +// this would belong to loader.c, but we need it in a separated file to be +// able to mock it when unit testing the loader functions. + + +char* +blogc_file_get_contents(const char *path, size_t *len, blogc_error_t **err) +{ + if (path == NULL || err == NULL || *err != NULL) + return NULL; + + *len = 0; + FILE *fp = fopen(path, "r"); + + if (fp == NULL) { + int tmp_errno = errno; + *err = blogc_error_new_printf(BLOGC_ERROR_LOADER, + "Failed to open file (%s): %s", path, strerror(tmp_errno)); + return NULL; + } + + b_string_t *str = b_string_new(); + char buffer[BLOGC_FILE_CHUNK_SIZE]; + + while (!feof(fp)) { + size_t read_len = fread(buffer, sizeof(char), BLOGC_FILE_CHUNK_SIZE, fp); + *len += read_len; + b_string_append_len(str, buffer, read_len); + } + fclose(fp); + return b_string_free(str, false); +} diff --git a/src/file.h b/src/file.h new file mode 100644 index 0000000..5abdba7 --- /dev/null +++ b/src/file.h @@ -0,0 +1,19 @@ +/* + * blogc: A blog compiler. + * Copyright (C) 2015 Rafael G. Martins + * + * This program can be distributed under the terms of the BSD License. + * See the file COPYING. + */ + +#ifndef _FILE_H +#define _FILE_H + +#include "utils/utils.h" +#include "error.h" + +#define BLOGC_FILE_CHUNK_SIZE 1024 + +char* blogc_file_get_contents(const char *path, size_t *len, blogc_error_t **err); + +#endif /* _FILE_H */ diff --git a/src/loader.c b/src/loader.c index 87abd8e..786fc05 100644 --- a/src/loader.c +++ b/src/loader.c @@ -10,45 +10,15 @@ #include #endif /* HAVE_CONFIG_H */ -#include -#include #include #include "utils/utils.h" +#include "file.h" #include "source-parser.h" #include "template-parser.h" #include "loader.h" #include "error.h" -char* -blogc_file_get_contents(const char *path, size_t *len, blogc_error_t **err) -{ - if (err == NULL || *err != NULL) - return NULL; - - *len = 0; - FILE *fp = fopen(path, "r"); - - if (fp == NULL) { - int tmp_errno = errno; - *err = blogc_error_new_printf(BLOGC_ERROR_LOADER, - "Failed to open file (%s): %s", path, strerror(tmp_errno)); - return NULL; - } - - b_string_t *str = b_string_new(); - char buffer[BLOGC_FILE_CHUNK_SIZE]; - - while (!feof(fp)) { - size_t read_len = fread(buffer, sizeof(char), BLOGC_FILE_CHUNK_SIZE, fp); - *len += read_len; - b_string_append_len(str, buffer, read_len); - } - fclose(fp); - return b_string_free(str, false); -} - - char* blogc_get_filename(const char *f) { diff --git a/src/loader.h b/src/loader.h index 2b6b306..683465b 100644 --- a/src/loader.h +++ b/src/loader.h @@ -12,9 +12,6 @@ #include "utils/utils.h" #include "error.h" -#define BLOGC_FILE_CHUNK_SIZE 1024 - -char* blogc_file_get_contents(const char *path, size_t *len, blogc_error_t **err); char* blogc_get_filename(const char *f); b_slist_t* blogc_template_parse_from_file(const char *f, blogc_error_t **err); b_trie_t* blogc_source_parse_from_file(const char *f, blogc_error_t **err); diff --git a/tests/check_loader.c b/tests/check_loader.c index 1961180..158ce1d 100644 --- a/tests/check_loader.c +++ b/tests/check_loader.c @@ -15,13 +15,11 @@ #include #include #include +#include "../src/template-parser.h" #include "../src/loader.h" #include "../src/utils/utils.h" -// FIXME: test the file functions - - static void test_get_filename(void **state) { @@ -52,11 +50,150 @@ test_get_filename(void **state) } +char* +__wrap_blogc_file_get_contents(const char *path, size_t *len, blogc_error_t **err) +{ + assert_null(*err); + const char *_path = mock_type(const char*); + if (_path != NULL) + assert_string_equal(path, _path); + char *rv = mock_type(char*); + *len = 0; + if (rv != NULL) + *len = strlen(rv); + return rv; +} + + +static void +test_template_parse_from_file(void **state) +{ + blogc_error_t *err = NULL; + will_return(__wrap_blogc_file_get_contents, "bola"); + will_return(__wrap_blogc_file_get_contents, b_strdup("{{ BOLA }}\n")); + b_slist_t *l = blogc_template_parse_from_file("bola", &err); + assert_null(err); + assert_non_null(l); + assert_int_equal(b_slist_length(l), 2); + blogc_template_free_stmts(l); +} + + +static void +test_template_parse_from_file_null(void **state) +{ + blogc_error_t *err = NULL; + will_return(__wrap_blogc_file_get_contents, "bola"); + will_return(__wrap_blogc_file_get_contents, NULL); + b_slist_t *l = blogc_template_parse_from_file("bola", &err); + assert_null(err); + assert_null(l); +} + + +static void +test_source_parse_from_file(void **state) +{ + blogc_error_t *err = NULL; + will_return(__wrap_blogc_file_get_contents, "bola.txt"); + will_return(__wrap_blogc_file_get_contents, b_strdup( + "ASD: 123\n" + "--------\n" + "bola")); + b_trie_t *t = blogc_source_parse_from_file("bola.txt", &err); + assert_null(err); + assert_non_null(t); + assert_int_equal(b_trie_size(t), 4); + assert_string_equal(b_trie_lookup(t, "ASD"), "123"); + assert_string_equal(b_trie_lookup(t, "FILENAME"), "bola"); + assert_string_equal(b_trie_lookup(t, "CONTENT"), "

bola

\n"); + assert_string_equal(b_trie_lookup(t, "RAW_CONTENT"), "bola"); + b_trie_free(t); +} + + +static void +test_source_parse_from_file_null(void **state) +{ + blogc_error_t *err = NULL; + will_return(__wrap_blogc_file_get_contents, "bola.txt"); + will_return(__wrap_blogc_file_get_contents, NULL); + b_trie_t *t = blogc_source_parse_from_file("bola.txt", &err); + assert_null(err); + assert_null(t); +} + + +static void +test_source_parse_from_files(void **state) +{ + will_return(__wrap_blogc_file_get_contents, "bola1.txt"); + will_return(__wrap_blogc_file_get_contents, b_strdup( + "ASD: 123\n" + "DATE: 2001-02-03 04:05:06\n" + "--------\n" + "bola")); + will_return(__wrap_blogc_file_get_contents, "bola2.txt"); + will_return(__wrap_blogc_file_get_contents, b_strdup( + "ASD: 456\n" + "DATE: 2002-02-03 04:05:06\n" + "--------\n" + "bola")); + will_return(__wrap_blogc_file_get_contents, "bola3.txt"); + will_return(__wrap_blogc_file_get_contents, b_strdup( + "ASD: 789\n" + "DATE: 2003-02-03 04:05:06\n" + "--------\n" + "bola")); + blogc_error_t *err = NULL; + b_slist_t *s = NULL; + s = b_slist_append(s, b_strdup("bola1.txt")); + s = b_slist_append(s, b_strdup("bola2.txt")); + s = b_slist_append(s, b_strdup("bola3.txt")); + b_trie_t *c = b_trie_new(free); + b_slist_t *t = blogc_source_parse_from_files(c, s, &err); + assert_null(err); + assert_non_null(t); + assert_int_equal(b_slist_length(t), 3); // it is enough, no need to look at the items + assert_int_equal(b_trie_size(c), 4); + assert_string_equal(b_trie_lookup(c, "FILENAME_FIRST"), "bola1"); + assert_string_equal(b_trie_lookup(c, "FILENAME_LAST"), "bola3"); + assert_string_equal(b_trie_lookup(c, "DATE_FIRST"), "2001-02-03 04:05:06"); + assert_string_equal(b_trie_lookup(c, "DATE_LAST"), "2003-02-03 04:05:06"); + b_trie_free(c); + b_slist_free_full(s, free); + b_slist_free_full(t, (b_free_func_t) b_trie_free); +} + + +static void +test_source_parse_from_files_null(void **state) +{ + blogc_error_t *err = NULL; + b_slist_t *s = NULL; + b_trie_t *c = b_trie_new(free); + b_slist_t *t = blogc_source_parse_from_files(c, s, &err); + assert_null(err); + assert_null(t); + assert_int_equal(b_slist_length(t), 0); + assert_int_equal(b_trie_size(c), 0); + b_trie_free(c); + b_slist_free_full(s, free); + b_slist_free_full(t, (b_free_func_t) b_trie_free); +} + + int main(void) { const UnitTest tests[] = { unit_test(test_get_filename), + unit_test(test_template_parse_from_file), + unit_test(test_template_parse_from_file_null), + unit_test(test_source_parse_from_file), + unit_test(test_source_parse_from_file_null), + unit_test(test_source_parse_from_files), + unit_test(test_source_parse_from_files_null), }; return run_tests(tests); } -- cgit v1.2.3-18-g5258