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 --- src/file.c | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ src/file.h | 19 +++++++++++++++++++ src/loader.c | 32 +------------------------------- src/loader.h | 3 --- 4 files changed, 70 insertions(+), 34 deletions(-) create mode 100644 src/file.c create mode 100644 src/file.h (limited to 'src') 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); -- cgit v1.2.3-18-g5258