From e5f9cd349da231eae42ec9d35e3ebcd0d8fff973 Mon Sep 17 00:00:00 2001 From: "Rafael G. Martins" Date: Sat, 3 Sep 2016 21:23:27 +0200 Subject: *: moved error handling to src/common/ --- .gitignore | 2 +- Makefile.am | 43 ++++----- src/blogc/datetime-parser.c | 61 ++++++------ src/blogc/datetime-parser.h | 5 +- src/blogc/error.c | 139 ---------------------------- src/blogc/error.h | 34 ------- src/blogc/errors.c | 40 ++++++++ src/blogc/errors.h | 24 +++++ src/blogc/file.c | 9 +- src/blogc/file.h | 5 +- src/blogc/loader.c | 15 +-- src/blogc/loader.h | 9 +- src/blogc/main.c | 7 +- src/blogc/renderer.c | 7 +- src/blogc/source-parser.c | 23 ++--- src/blogc/source-parser.h | 5 +- src/blogc/template-parser.c | 65 ++++++------- src/blogc/template-parser.h | 5 +- src/common/error.c | 111 ++++++++++++++++++++++ src/common/error.h | 26 ++++++ tests/blogc/check_datetime_parser.c | 141 ++++++++++++++-------------- tests/blogc/check_error.c | 109 ---------------------- tests/blogc/check_loader.c | 35 +++---- tests/blogc/check_renderer.c | 55 +++++------ tests/blogc/check_source_parser.c | 91 +++++++++--------- tests/blogc/check_template_parser.c | 179 ++++++++++++++++++------------------ tests/common/check_error.c | 109 ++++++++++++++++++++++ 27 files changed, 700 insertions(+), 654 deletions(-) delete mode 100644 src/blogc/error.c delete mode 100644 src/blogc/error.h create mode 100644 src/blogc/errors.c create mode 100644 src/blogc/errors.h create mode 100644 src/common/error.c create mode 100644 src/common/error.h delete mode 100644 tests/blogc/check_error.c create mode 100644 tests/common/check_error.c diff --git a/.gitignore b/.gitignore index 85b133e..22ac84a 100644 --- a/.gitignore +++ b/.gitignore @@ -52,11 +52,11 @@ blogc*.html # tests /tests/blogc/check_content_parser /tests/blogc/check_datetime_parser -/tests/blogc/check_error /tests/blogc/check_loader /tests/blogc/check_renderer /tests/blogc/check_source_parser /tests/blogc/check_template_parser +/tests/common/check_error /tests/common/check_utf8 /tests/common/check_utils diff --git a/Makefile.am b/Makefile.am index 179321c..8012ce5 100644 --- a/Makefile.am +++ b/Makefile.am @@ -35,12 +35,13 @@ noinst_HEADERS = \ src/blogc/content-parser.h \ src/blogc/datetime-parser.h \ src/blogc/debug.h \ - src/blogc/error.h \ + src/blogc/errors.h \ src/blogc/file.h \ src/blogc/loader.h \ src/blogc/renderer.h \ src/blogc/source-parser.h \ src/blogc/template-parser.h \ + src/common/error.h \ src/common/utf8.h \ src/common/utils.h \ $(NULL) @@ -76,7 +77,7 @@ check_PROGRAMS = \ libblogc_la_SOURCES = \ src/blogc/content-parser.c \ src/blogc/datetime-parser.c \ - src/blogc/error.c \ + src/blogc/errors.c \ src/blogc/file.c \ src/blogc/loader.c \ src/blogc/renderer.c \ @@ -94,6 +95,7 @@ libblogc_la_LIBADD = \ libblogc_common_la_SOURCES = \ + src/common/error.c \ src/common/utf8.c \ src/common/utils.c \ $(NULL) @@ -269,33 +271,15 @@ if USE_CMOCKA check_PROGRAMS += \ tests/blogc/check_content_parser \ tests/blogc/check_datetime_parser \ - tests/blogc/check_error \ tests/blogc/check_loader \ tests/blogc/check_renderer \ tests/blogc/check_source_parser \ tests/blogc/check_template_parser \ + tests/common/check_error \ tests/common/check_utf8 \ tests/common/check_utils \ $(NULL) -tests_blogc_check_error_SOURCES = \ - tests/blogc/check_error.c \ - $(NULL) - -tests_blogc_check_error_CFLAGS = \ - $(CMOCKA_CFLAGS) \ - $(NULL) - -tests_blogc_check_error_LDFLAGS = \ - -no-install \ - $(NULL) - -tests_blogc_check_error_LDADD = \ - $(CMOCKA_LIBS) \ - libblogc.la \ - libblogc_common.la \ - $(NULL) - tests_blogc_check_loader_SOURCES = \ tests/blogc/check_loader.c \ $(NULL) @@ -406,6 +390,23 @@ tests_blogc_check_template_parser_LDADD = \ libblogc_common.la \ $(NULL) +tests_common_check_error_SOURCES = \ + tests/common/check_error.c \ + $(NULL) + +tests_common_check_error_CFLAGS = \ + $(CMOCKA_CFLAGS) \ + $(NULL) + +tests_common_check_error_LDFLAGS = \ + -no-install \ + $(NULL) + +tests_common_check_error_LDADD = \ + $(CMOCKA_LIBS) \ + libblogc_common.la \ + $(NULL) + tests_common_check_utf8_SOURCES = \ tests/common/check_utf8.c \ $(NULL) diff --git a/src/blogc/datetime-parser.c b/src/blogc/datetime-parser.c index a685c77..2fd3b4f 100644 --- a/src/blogc/datetime-parser.c +++ b/src/blogc/datetime-parser.c @@ -16,8 +16,9 @@ #include -#include "error.h" #include "datetime-parser.h" +#include "errors.h" +#include "../common/error.h" #include "../common/utils.h" @@ -47,14 +48,14 @@ typedef enum { char* blogc_convert_datetime(const char *orig, const char *format, - blogc_error_t **err) + bc_error_t **err) { if (err == NULL || *err != NULL) return NULL; #ifndef HAVE_TIME_H - *err = blogc_error_new(BLOGC_WARNING_DATETIME_PARSER, + *err = bc_error_new(BLOGC_WARNING_DATETIME_PARSER, "Your operating system does not supports the datetime functionalities " "used by blogc. Sorry."); return NULL; @@ -80,7 +81,7 @@ blogc_convert_datetime(const char *orig, const char *format, state = DATETIME_SECOND_YEAR; break; } - *err = blogc_error_new_printf(BLOGC_WARNING_DATETIME_PARSER, + *err = bc_error_new_printf(BLOGC_WARNING_DATETIME_PARSER, "Invalid first digit of year. " "Found '%c', must be integer >= 0 and <= 9.", c); break; @@ -91,7 +92,7 @@ blogc_convert_datetime(const char *orig, const char *format, state = DATETIME_THIRD_YEAR; break; } - *err = blogc_error_new_printf(BLOGC_WARNING_DATETIME_PARSER, + *err = bc_error_new_printf(BLOGC_WARNING_DATETIME_PARSER, "Invalid second digit of year. " "Found '%c', must be integer >= 0 and <= 9.", c); break; @@ -102,7 +103,7 @@ blogc_convert_datetime(const char *orig, const char *format, state = DATETIME_FOURTH_YEAR; break; } - *err = blogc_error_new_printf(BLOGC_WARNING_DATETIME_PARSER, + *err = bc_error_new_printf(BLOGC_WARNING_DATETIME_PARSER, "Invalid third digit of year. " "Found '%c', must be integer >= 0 and <= 9.", c); break; @@ -111,7 +112,7 @@ blogc_convert_datetime(const char *orig, const char *format, if (c >= '0' && c <= '9') { tmp += c - diff - 1900; if (tmp < 0) { - *err = blogc_error_new_printf(BLOGC_WARNING_DATETIME_PARSER, + *err = bc_error_new_printf(BLOGC_WARNING_DATETIME_PARSER, "Invalid year. Found %d, must be >= 1900.", tmp + 1900); break; @@ -120,7 +121,7 @@ blogc_convert_datetime(const char *orig, const char *format, state = DATETIME_FIRST_HYPHEN; break; } - *err = blogc_error_new_printf(BLOGC_WARNING_DATETIME_PARSER, + *err = bc_error_new_printf(BLOGC_WARNING_DATETIME_PARSER, "Invalid fourth digit of year. " "Found '%c', must be integer >= 0 and <= 9.", c); break; @@ -131,7 +132,7 @@ blogc_convert_datetime(const char *orig, const char *format, state = DATETIME_FIRST_MONTH; break; } - *err = blogc_error_new_printf(BLOGC_WARNING_DATETIME_PARSER, + *err = bc_error_new_printf(BLOGC_WARNING_DATETIME_PARSER, "Invalid separator between year and month. " "Found '%c', must be '-'.", c); break; @@ -142,7 +143,7 @@ blogc_convert_datetime(const char *orig, const char *format, state = DATETIME_SECOND_MONTH; break; } - *err = blogc_error_new_printf(BLOGC_WARNING_DATETIME_PARSER, + *err = bc_error_new_printf(BLOGC_WARNING_DATETIME_PARSER, "Invalid first digit of month. " "Found '%c', must be integer >= 0 and <= 1.", c); break; @@ -151,7 +152,7 @@ blogc_convert_datetime(const char *orig, const char *format, if (c >= '0' && c <= '9') { tmp += c - diff - 1; if (tmp < 0 || tmp > 11) { - *err = blogc_error_new_printf(BLOGC_WARNING_DATETIME_PARSER, + *err = bc_error_new_printf(BLOGC_WARNING_DATETIME_PARSER, "Invalid month. Found %d, must be >= 1 and <= 12.", tmp + 1); break; @@ -160,7 +161,7 @@ blogc_convert_datetime(const char *orig, const char *format, state = DATETIME_SECOND_HYPHEN; break; } - *err = blogc_error_new_printf(BLOGC_WARNING_DATETIME_PARSER, + *err = bc_error_new_printf(BLOGC_WARNING_DATETIME_PARSER, "Invalid second digit of month. " "Found '%c', must be integer >= 0 and <= 9.", c); break; @@ -171,7 +172,7 @@ blogc_convert_datetime(const char *orig, const char *format, state = DATETIME_FIRST_DAY; break; } - *err = blogc_error_new_printf(BLOGC_WARNING_DATETIME_PARSER, + *err = bc_error_new_printf(BLOGC_WARNING_DATETIME_PARSER, "Invalid separator between month and day. " "Found '%c', must be '-'.", c); break; @@ -182,7 +183,7 @@ blogc_convert_datetime(const char *orig, const char *format, state = DATETIME_SECOND_DAY; break; } - *err = blogc_error_new_printf(BLOGC_WARNING_DATETIME_PARSER, + *err = bc_error_new_printf(BLOGC_WARNING_DATETIME_PARSER, "Invalid first digit of day. " "Found '%c', must be integer >= 0 and <= 3.", c); break; @@ -191,7 +192,7 @@ blogc_convert_datetime(const char *orig, const char *format, if (c >= '0' && c <= '9') { tmp += c - diff; if (tmp < 1 || tmp > 31) { - *err = blogc_error_new_printf(BLOGC_WARNING_DATETIME_PARSER, + *err = bc_error_new_printf(BLOGC_WARNING_DATETIME_PARSER, "Invalid day. Found %d, must be >= 1 and <= 31.", tmp); break; @@ -200,7 +201,7 @@ blogc_convert_datetime(const char *orig, const char *format, state = DATETIME_SPACE; break; } - *err = blogc_error_new_printf(BLOGC_WARNING_DATETIME_PARSER, + *err = bc_error_new_printf(BLOGC_WARNING_DATETIME_PARSER, "Invalid second digit of day. " "Found '%c', must be integer >= 0 and <= 9.", c); break; @@ -211,7 +212,7 @@ blogc_convert_datetime(const char *orig, const char *format, state = DATETIME_FIRST_HOUR; break; } - *err = blogc_error_new_printf(BLOGC_WARNING_DATETIME_PARSER, + *err = bc_error_new_printf(BLOGC_WARNING_DATETIME_PARSER, "Invalid separator between date and time. " "Found '%c', must be ' ' (empty space).", c); break; @@ -222,7 +223,7 @@ blogc_convert_datetime(const char *orig, const char *format, state = DATETIME_SECOND_HOUR; break; } - *err = blogc_error_new_printf(BLOGC_WARNING_DATETIME_PARSER, + *err = bc_error_new_printf(BLOGC_WARNING_DATETIME_PARSER, "Invalid first digit of hours. " "Found '%c', must be integer >= 0 and <= 2.", c); break; @@ -231,7 +232,7 @@ blogc_convert_datetime(const char *orig, const char *format, if (c >= '0' && c <= '9') { tmp += c - diff; if (tmp < 0 || tmp > 23) { - *err = blogc_error_new_printf(BLOGC_WARNING_DATETIME_PARSER, + *err = bc_error_new_printf(BLOGC_WARNING_DATETIME_PARSER, "Invalid hours. Found %d, must be >= 0 and <= 23.", tmp); break; @@ -240,7 +241,7 @@ blogc_convert_datetime(const char *orig, const char *format, state = DATETIME_FIRST_COLON; break; } - *err = blogc_error_new_printf(BLOGC_WARNING_DATETIME_PARSER, + *err = bc_error_new_printf(BLOGC_WARNING_DATETIME_PARSER, "Invalid second digit of hours. " "Found '%c', must be integer >= 0 and <= 9.", c); break; @@ -251,7 +252,7 @@ blogc_convert_datetime(const char *orig, const char *format, state = DATETIME_FIRST_MINUTE; break; } - *err = blogc_error_new_printf(BLOGC_WARNING_DATETIME_PARSER, + *err = bc_error_new_printf(BLOGC_WARNING_DATETIME_PARSER, "Invalid separator between hours and minutes. " "Found '%c', must be ':'.", c); break; @@ -262,7 +263,7 @@ blogc_convert_datetime(const char *orig, const char *format, state = DATETIME_SECOND_MINUTE; break; } - *err = blogc_error_new_printf(BLOGC_WARNING_DATETIME_PARSER, + *err = bc_error_new_printf(BLOGC_WARNING_DATETIME_PARSER, "Invalid first digit of minutes. " "Found '%c', must be integer >= 0 and <= 5.", c); break; @@ -274,7 +275,7 @@ blogc_convert_datetime(const char *orig, const char *format, // this won't happen because we are restricting the digits // to 00-59 already, but lets keep the code here for // reference. - *err = blogc_error_new_printf(BLOGC_WARNING_DATETIME_PARSER, + *err = bc_error_new_printf(BLOGC_WARNING_DATETIME_PARSER, "Invalid minutes. Found %d, must be >= 0 and <= 59.", tmp); break; @@ -283,7 +284,7 @@ blogc_convert_datetime(const char *orig, const char *format, state = DATETIME_SECOND_COLON; break; } - *err = blogc_error_new_printf(BLOGC_WARNING_DATETIME_PARSER, + *err = bc_error_new_printf(BLOGC_WARNING_DATETIME_PARSER, "Invalid second digit of minutes. " "Found '%c', must be integer >= 0 and <= 9.", c); break; @@ -294,7 +295,7 @@ blogc_convert_datetime(const char *orig, const char *format, state = DATETIME_FIRST_SECOND; break; } - *err = blogc_error_new_printf(BLOGC_WARNING_DATETIME_PARSER, + *err = bc_error_new_printf(BLOGC_WARNING_DATETIME_PARSER, "Invalid separator between minutes and seconds. " "Found '%c', must be ':'.", c); break; @@ -305,7 +306,7 @@ blogc_convert_datetime(const char *orig, const char *format, state = DATETIME_SECOND_SECOND; break; } - *err = blogc_error_new_printf(BLOGC_WARNING_DATETIME_PARSER, + *err = bc_error_new_printf(BLOGC_WARNING_DATETIME_PARSER, "Invalid first digit of seconds. " "Found '%c', must be integer >= 0 and <= 6.", c); break; @@ -314,7 +315,7 @@ blogc_convert_datetime(const char *orig, const char *format, if (c >= '0' && c <= '9') { tmp += c - diff; if (tmp < 0 || tmp > 60) { - *err = blogc_error_new_printf(BLOGC_WARNING_DATETIME_PARSER, + *err = bc_error_new_printf(BLOGC_WARNING_DATETIME_PARSER, "Invalid seconds. Found %d, must be >= 0 and <= 60.", tmp); break; @@ -323,7 +324,7 @@ blogc_convert_datetime(const char *orig, const char *format, state = DATETIME_DONE; break; } - *err = blogc_error_new_printf(BLOGC_WARNING_DATETIME_PARSER, + *err = bc_error_new_printf(BLOGC_WARNING_DATETIME_PARSER, "Invalid second digit of seconds. " "Found '%c', must be integer >= 0 and <= 9.", c); break; @@ -355,7 +356,7 @@ blogc_convert_datetime(const char *orig, const char *format, case DATETIME_SECOND_MINUTE: case DATETIME_FIRST_SECOND: case DATETIME_SECOND_SECOND: - *err = blogc_error_new_printf(BLOGC_WARNING_DATETIME_PARSER, + *err = bc_error_new_printf(BLOGC_WARNING_DATETIME_PARSER, "Invalid datetime string. " "Found '%s', formats allowed are: 'yyyy-mm-dd hh:mm:ss', " "'yyyy-mm-dd hh:ss', 'yyyy-mm-dd hh' and 'yyyy-mm-dd'.", @@ -374,7 +375,7 @@ blogc_convert_datetime(const char *orig, const char *format, char buf[1024]; if (0 == strftime(buf, sizeof(buf), format, &t)) { - *err = blogc_error_new_printf(BLOGC_WARNING_DATETIME_PARSER, + *err = bc_error_new_printf(BLOGC_WARNING_DATETIME_PARSER, "Failed to format DATE variable, FORMAT is too long: %s", format); return NULL; diff --git a/src/blogc/datetime-parser.h b/src/blogc/datetime-parser.h index a5087b3..08ad3c9 100644 --- a/src/blogc/datetime-parser.h +++ b/src/blogc/datetime-parser.h @@ -9,9 +9,10 @@ #ifndef _DATETIME_H #define _DATETIME_H -#include "error.h" +#include "errors.h" +#include "../common/error.h" char* blogc_convert_datetime(const char *orig, const char *format, - blogc_error_t **err); + bc_error_t **err); #endif /* _DATETIME_H */ diff --git a/src/blogc/error.c b/src/blogc/error.c deleted file mode 100644 index cca8c03..0000000 --- a/src/blogc/error.c +++ /dev/null @@ -1,139 +0,0 @@ -/* - * blogc: A blog compiler. - * Copyright (C) 2015-2016 Rafael G. Martins - * - * This program can be distributed under the terms of the BSD License. - * See the file LICENSE. - */ - -#include -#include -#include -#include "error.h" -#include "../common/utils.h" - - -blogc_error_t* -blogc_error_new(blogc_error_type_t type, const char *msg) -{ - blogc_error_t *err = bc_malloc(sizeof(blogc_error_t)); - err->type = type; - err->msg = bc_strdup(msg); - return err; -} - - -blogc_error_t* -blogc_error_new_printf(blogc_error_type_t type, const char *format, ...) -{ - va_list ap; - va_start(ap, format); - char *tmp = bc_strdup_vprintf(format, ap); - va_end(ap); - blogc_error_t *rv = blogc_error_new(type, tmp); - free(tmp); - return rv; -} - - -blogc_error_t* -blogc_error_parser(blogc_error_type_t type, const char *src, size_t src_len, - size_t current, const char *format, ...) -{ - va_list ap; - va_start(ap, format); - char *msg = bc_strdup_vprintf(format, ap); - va_end(ap); - - size_t lineno = 1; - size_t linestart = 0; - size_t lineend = 0; - size_t pos = 1; - - for (size_t i = 0; i < src_len; i++) { - char c = src[i]; - if (i < current) { - if ((i + 1) < src_len) { - if ((c == '\n' && src[i + 1] == '\r') || - (c == '\r' && src[i + 1] == '\n')) - { - lineno++; - i++; - pos = 1; - if ((i + 1) < src_len) - linestart = i + 1; - continue; - } - } - if (c == '\n' || c == '\r') { - lineno++; - pos = 1; - if ((i + 1) < src_len) - linestart = i + 1; - continue; - } - pos++; - } - else if (c == '\n' || c == '\r') { - lineend = i; - break; - } - } - - if (lineend <= linestart && src_len >= linestart) - lineend = src_len; - - char *line = bc_strndup(src + linestart, lineend - linestart); - - blogc_error_t *rv = NULL; - - if (line[0] == '\0') // "near" message isn't useful if line is empty - rv = blogc_error_new(type, msg); - else - rv = blogc_error_new_printf(type, - "%s\nError occurred near line %d, position %d: %s", msg, lineno, - pos, line); - - free(msg); - free(line); - - return rv; -} - - -void -blogc_error_print(blogc_error_t *err) -{ - if (err == NULL) - return; - - switch(err->type) { - case BLOGC_ERROR_SOURCE_PARSER: - fprintf(stderr, "blogc: error: source: %s\n", err->msg); - break; - case BLOGC_ERROR_TEMPLATE_PARSER: - fprintf(stderr, "blogc: error: template: %s\n", err->msg); - break; - case BLOGC_ERROR_LOADER: - fprintf(stderr, "blogc: error: loader: %s\n", err->msg); - break; - case BLOGC_ERROR_FILE: - fprintf(stderr, "blogc: error: file: %s\n", err->msg); - break; - case BLOGC_WARNING_DATETIME_PARSER: - fprintf(stderr, "blogc: warning: datetime: %s\n", err->msg); - break; - default: - fprintf(stderr, "blogc: error: %s\n", err->msg); - } -} - - -void -blogc_error_free(blogc_error_t *err) -{ - if (err == NULL) - return; - free(err->msg); - free(err); -} diff --git a/src/blogc/error.h b/src/blogc/error.h deleted file mode 100644 index 31fbaf2..0000000 --- a/src/blogc/error.h +++ /dev/null @@ -1,34 +0,0 @@ -/* - * blogc: A blog compiler. - * Copyright (C) 2015-2016 Rafael G. Martins - * - * This program can be distributed under the terms of the BSD License. - * See the file LICENSE. - */ - -#ifndef _ERROR_H -#define _ERROR_H - -#include - -typedef enum { - BLOGC_ERROR_SOURCE_PARSER = 1, - BLOGC_ERROR_TEMPLATE_PARSER, - BLOGC_ERROR_LOADER, - BLOGC_ERROR_FILE, - BLOGC_WARNING_DATETIME_PARSER, -} blogc_error_type_t; - -typedef struct { - char *msg; - blogc_error_type_t type; -} blogc_error_t; - -blogc_error_t* blogc_error_new(blogc_error_type_t type, const char *msg); -blogc_error_t* blogc_error_new_printf(blogc_error_type_t type, const char *format, ...); -blogc_error_t* blogc_error_parser(blogc_error_type_t type, const char *src, - size_t src_len, size_t current, const char *format, ...); -void blogc_error_print(blogc_error_t *err); -void blogc_error_free(blogc_error_t *err); - -#endif /* _ERROR_H */ diff --git a/src/blogc/errors.c b/src/blogc/errors.c new file mode 100644 index 0000000..6447a4a --- /dev/null +++ b/src/blogc/errors.c @@ -0,0 +1,40 @@ +/* + * blogc: A blog compiler. + * Copyright (C) 2015-2016 Rafael G. Martins + * + * This program can be distributed under the terms of the BSD License. + * See the file LICENSE. + */ + +#include +#include +#include "errors.h" +#include "../common/error.h" + + +void +blogc_error_print(bc_error_t *err) +{ + if (err == NULL) + return; + + switch((blogc_error_type_t) err->type) { + case BLOGC_ERROR_SOURCE_PARSER: + fprintf(stderr, "blogc: error: source: %s\n", err->msg); + break; + case BLOGC_ERROR_TEMPLATE_PARSER: + fprintf(stderr, "blogc: error: template: %s\n", err->msg); + break; + case BLOGC_ERROR_LOADER: + fprintf(stderr, "blogc: error: loader: %s\n", err->msg); + break; + case BLOGC_ERROR_FILE: + fprintf(stderr, "blogc: error: file: %s\n", err->msg); + break; + case BLOGC_WARNING_DATETIME_PARSER: + fprintf(stderr, "blogc: warning: datetime: %s\n", err->msg); + break; + default: + fprintf(stderr, "blogc: error: %s\n", err->msg); + } +} diff --git a/src/blogc/errors.h b/src/blogc/errors.h new file mode 100644 index 0000000..427d9e7 --- /dev/null +++ b/src/blogc/errors.h @@ -0,0 +1,24 @@ +/* + * blogc: A blog compiler. + * Copyright (C) 2015-2016 Rafael G. Martins + * + * This program can be distributed under the terms of the BSD License. + * See the file LICENSE. + */ + +#ifndef _ERRORS_H +#define _ERRORS_H + +#include "../common/error.h" + +typedef enum { + BLOGC_ERROR_SOURCE_PARSER = 1, + BLOGC_ERROR_TEMPLATE_PARSER, + BLOGC_ERROR_LOADER, + BLOGC_ERROR_FILE, + BLOGC_WARNING_DATETIME_PARSER, +} blogc_error_type_t; + +void blogc_error_print(bc_error_t *err); + +#endif /* _ERRORS_H */ diff --git a/src/blogc/file.c b/src/blogc/file.c index cdd6df4..94148fe 100644 --- a/src/blogc/file.c +++ b/src/blogc/file.c @@ -13,7 +13,8 @@ #include #include #include "file.h" -#include "error.h" +#include "errors.h" +#include "../common/error.h" #include "../common/utf8.h" #include "../common/utils.h" @@ -22,7 +23,7 @@ char* -blogc_file_get_contents(const char *path, size_t *len, blogc_error_t **err) +blogc_file_get_contents(const char *path, size_t *len, bc_error_t **err) { if (path == NULL || err == NULL || *err != NULL) return NULL; @@ -32,7 +33,7 @@ blogc_file_get_contents(const char *path, size_t *len, blogc_error_t **err) if (fp == NULL) { int tmp_errno = errno; - *err = blogc_error_new_printf(BLOGC_ERROR_FILE, + *err = bc_error_new_printf(BLOGC_ERROR_FILE, "Failed to open file (%s): %s", path, strerror(tmp_errno)); return NULL; } @@ -60,7 +61,7 @@ blogc_file_get_contents(const char *path, size_t *len, blogc_error_t **err) fclose(fp); if (!blogc_utf8_validate_str(str)) { - *err = blogc_error_new_printf(BLOGC_ERROR_FILE, + *err = bc_error_new_printf(BLOGC_ERROR_FILE, "File content is not valid UTF-8: %s", path); bc_string_free(str, true); return NULL; diff --git a/src/blogc/file.h b/src/blogc/file.h index d2c4390..c509874 100644 --- a/src/blogc/file.h +++ b/src/blogc/file.h @@ -11,11 +11,12 @@ #include #include -#include "error.h" +#include "errors.h" +#include "../common/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_file_get_contents(const char *path, size_t *len, bc_error_t **err); int blogc_fprintf(FILE *stream, const char *format, ...); #endif /* _FILE_H */ diff --git a/src/blogc/loader.c b/src/blogc/loader.c index 914f07f..93d9eef 100644 --- a/src/blogc/loader.c +++ b/src/blogc/loader.c @@ -16,7 +16,8 @@ #include "source-parser.h" #include "template-parser.h" #include "loader.h" -#include "error.h" +#include "errors.h" +#include "../common/error.h" #include "../common/utils.h" @@ -58,7 +59,7 @@ blogc_get_filename(const char *f) bc_slist_t* -blogc_template_parse_from_file(const char *f, blogc_error_t **err) +blogc_template_parse_from_file(const char *f, bc_error_t **err) { if (err == NULL || *err != NULL) return NULL; @@ -73,7 +74,7 @@ blogc_template_parse_from_file(const char *f, blogc_error_t **err) bc_trie_t* -blogc_source_parse_from_file(const char *f, blogc_error_t **err) +blogc_source_parse_from_file(const char *f, bc_error_t **err) { if (err == NULL || *err != NULL) return NULL; @@ -96,9 +97,9 @@ blogc_source_parse_from_file(const char *f, blogc_error_t **err) bc_slist_t* -blogc_source_parse_from_files(bc_trie_t *conf, bc_slist_t *l, blogc_error_t **err) +blogc_source_parse_from_files(bc_trie_t *conf, bc_slist_t *l, bc_error_t **err) { - blogc_error_t *tmp_err = NULL; + bc_error_t *tmp_err = NULL; bc_slist_t *rv = NULL; unsigned int with_date = 0; @@ -123,10 +124,10 @@ blogc_source_parse_from_files(bc_trie_t *conf, bc_slist_t *l, blogc_error_t **er char *f = tmp->data; bc_trie_t *s = blogc_source_parse_from_file(f, &tmp_err); if (s == NULL) { - *err = blogc_error_new_printf(BLOGC_ERROR_LOADER, + *err = bc_error_new_printf(BLOGC_ERROR_LOADER, "An error occurred while parsing source file: %s\n\n%s", f, tmp_err->msg); - blogc_error_free(tmp_err); + bc_error_free(tmp_err); tmp_err = NULL; bc_slist_free_full(rv, (bc_free_func_t) bc_trie_free); rv = NULL; diff --git a/src/blogc/loader.h b/src/blogc/loader.h index 97286a0..c8a7230 100644 --- a/src/blogc/loader.h +++ b/src/blogc/loader.h @@ -9,13 +9,14 @@ #ifndef _LOADER_H #define _LOADER_H -#include "error.h" +#include "errors.h" +#include "../common/error.h" #include "../common/utils.h" char* blogc_get_filename(const char *f); -bc_slist_t* blogc_template_parse_from_file(const char *f, blogc_error_t **err); -bc_trie_t* blogc_source_parse_from_file(const char *f, blogc_error_t **err); +bc_slist_t* blogc_template_parse_from_file(const char *f, bc_error_t **err); +bc_trie_t* blogc_source_parse_from_file(const char *f, bc_error_t **err); bc_slist_t* blogc_source_parse_from_files(bc_trie_t *conf, bc_slist_t *l, - blogc_error_t **err); + bc_error_t **err); #endif /* _LOADER_H */ diff --git a/src/blogc/main.c b/src/blogc/main.c index 0f174d4..8563c68 100644 --- a/src/blogc/main.c +++ b/src/blogc/main.c @@ -26,7 +26,8 @@ #include "template-parser.h" #include "loader.h" #include "renderer.h" -#include "error.h" +#include "errors.h" +#include "../common/error.h" #include "../common/utf8.h" #include "../common/utils.h" @@ -219,7 +220,7 @@ main(int argc, char **argv) goto cleanup; } - blogc_error_t *err = NULL; + bc_error_t *err = NULL; bc_slist_t *s = blogc_source_parse_from_files(config, sources, &err); if (err != NULL) { @@ -286,7 +287,7 @@ cleanup3: blogc_template_free_stmts(l); cleanup2: bc_slist_free_full(s, (bc_free_func_t) bc_trie_free); - blogc_error_free(err); + bc_error_free(err); cleanup: bc_trie_free(config); free(template); diff --git a/src/blogc/renderer.c b/src/blogc/renderer.c index 409c75e..7c29818 100644 --- a/src/blogc/renderer.c +++ b/src/blogc/renderer.c @@ -13,9 +13,10 @@ #include #include #include "datetime-parser.h" -#include "error.h" +#include "errors.h" #include "template-parser.h" #include "renderer.h" +#include "../common/error.h" #include "../common/utils.h" @@ -43,11 +44,11 @@ blogc_format_date(const char *date, bc_trie_t *global, bc_trie_t *local) if (date_format == NULL) return bc_strdup(date); - blogc_error_t *err = NULL; + bc_error_t *err = NULL; char *rv = blogc_convert_datetime(date, date_format, &err); if (err != NULL) { blogc_error_print(err); - blogc_error_free(err); + bc_error_free(err); return bc_strdup(date); } return rv; diff --git a/src/blogc/source-parser.c b/src/blogc/source-parser.c index dc39760..7cb929e 100644 --- a/src/blogc/source-parser.c +++ b/src/blogc/source-parser.c @@ -11,7 +11,8 @@ #include "content-parser.h" #include "source-parser.h" -#include "error.h" +#include "errors.h" +#include "../common/error.h" #include "../common/utils.h" @@ -27,7 +28,7 @@ typedef enum { bc_trie_t* -blogc_source_parse(const char *src, size_t src_len, blogc_error_t **err) +blogc_source_parse(const char *src, size_t src_len, bc_error_t **err) { if (err == NULL || *err != NULL) return NULL; @@ -60,7 +61,7 @@ blogc_source_parse(const char *src, size_t src_len, blogc_error_t **err) state = SOURCE_SEPARATOR; break; } - *err = blogc_error_parser(BLOGC_ERROR_SOURCE_PARSER, src, src_len, + *err = bc_error_parser(BLOGC_ERROR_SOURCE_PARSER, src, src_len, current, "Can't find a configuration key or the content separator."); break; @@ -93,7 +94,7 @@ blogc_source_parse(const char *src, size_t src_len, blogc_error_t **err) ((current - start == 13) && (0 == strncmp("BLOGC_VERSION", src + start, 13)))) { - *err = blogc_error_new_printf(BLOGC_ERROR_SOURCE_PARSER, + *err = bc_error_new_printf(BLOGC_ERROR_SOURCE_PARSER, "'%s' variable is forbidden in source files. It will " "be set for you by the compiler.", key); break; @@ -101,7 +102,7 @@ blogc_source_parse(const char *src, size_t src_len, blogc_error_t **err) state = SOURCE_CONFIG_VALUE_START; break; } - *err = blogc_error_parser(BLOGC_ERROR_SOURCE_PARSER, src, src_len, + *err = bc_error_parser(BLOGC_ERROR_SOURCE_PARSER, src, src_len, current, "Invalid configuration key."); break; @@ -111,7 +112,7 @@ blogc_source_parse(const char *src, size_t src_len, blogc_error_t **err) start = current; break; } - *err = blogc_error_parser(BLOGC_ERROR_SOURCE_PARSER, src, src_len, + *err = bc_error_parser(BLOGC_ERROR_SOURCE_PARSER, src, src_len, current, "Configuration value not provided for '%s'.", key); break; @@ -134,7 +135,7 @@ blogc_source_parse(const char *src, size_t src_len, blogc_error_t **err) state = SOURCE_CONTENT_START; break; } - *err = blogc_error_parser(BLOGC_ERROR_SOURCE_PARSER, src, src_len, + *err = bc_error_parser(BLOGC_ERROR_SOURCE_PARSER, src, src_len, current, "Invalid content separator. Must be more than one '-' characters."); break; @@ -183,21 +184,21 @@ blogc_source_parse(const char *src, size_t src_len, blogc_error_t **err) // output. :) switch (state) { case SOURCE_START: - *err = blogc_error_parser(BLOGC_ERROR_SOURCE_PARSER, src, src_len, + *err = bc_error_parser(BLOGC_ERROR_SOURCE_PARSER, src, src_len, current, "Your source file is empty."); break; case SOURCE_CONFIG_KEY: - *err = blogc_error_parser(BLOGC_ERROR_SOURCE_PARSER, src, src_len, + *err = bc_error_parser(BLOGC_ERROR_SOURCE_PARSER, src, src_len, current, "Your last configuration key is missing ':' and " "the value"); break; case SOURCE_CONFIG_VALUE_START: - *err = blogc_error_parser(BLOGC_ERROR_SOURCE_PARSER, src, src_len, + *err = bc_error_parser(BLOGC_ERROR_SOURCE_PARSER, src, src_len, current, "Configuration value not provided for '%s'.", key); break; case SOURCE_CONFIG_VALUE: - *err = blogc_error_parser(BLOGC_ERROR_SOURCE_PARSER, src, src_len, + *err = bc_error_parser(BLOGC_ERROR_SOURCE_PARSER, src, src_len, current, "No line ending after the configuration value for " "'%s'.", key); break; diff --git a/src/blogc/source-parser.h b/src/blogc/source-parser.h index d135f23..d920775 100644 --- a/src/blogc/source-parser.h +++ b/src/blogc/source-parser.h @@ -10,10 +10,11 @@ #define _SOURCE_PARSER_H #include -#include "error.h" +#include "errors.h" +#include "../common/error.h" #include "../common/utils.h" bc_trie_t* blogc_source_parse(const char *src, size_t src_len, - blogc_error_t **err); + bc_error_t **err); #endif /* _SOURCE_PARSER_H */ diff --git a/src/blogc/template-parser.c b/src/blogc/template-parser.c index 1cca885..acf8f7c 100644 --- a/src/blogc/template-parser.c +++ b/src/blogc/template-parser.c @@ -11,7 +11,8 @@ #include #include "template-parser.h" -#include "error.h" +#include "errors.h" +#include "../common/error.h" #include "../common/utils.h" @@ -42,7 +43,7 @@ typedef enum { bc_slist_t* -blogc_template_parse(const char *src, size_t src_len, blogc_error_t **err) +blogc_template_parse(const char *src, size_t src_len, bc_error_t **err) { if (err == NULL || *err != NULL) return NULL; @@ -169,13 +170,13 @@ blogc_template_parse(const char *src, size_t src_len, blogc_error_t **err) break; } if (c == '-') { - *err = blogc_error_parser(BLOGC_ERROR_TEMPLATE_PARSER, src, + *err = bc_error_parser(BLOGC_ERROR_TEMPLATE_PARSER, src, src_len, current, "Invalid statement syntax. Duplicated whitespace " "cleaner before statement."); break; } - *err = blogc_error_parser(BLOGC_ERROR_TEMPLATE_PARSER, src, + *err = bc_error_parser(BLOGC_ERROR_TEMPLATE_PARSER, src, src_len, current, "Invalid statement syntax. Must begin with lowercase letter."); break; @@ -195,7 +196,7 @@ blogc_template_parse(const char *src, size_t src_len, blogc_error_t **err) block_foreach_open = foreach_open; break; } - *err = blogc_error_parser(BLOGC_ERROR_TEMPLATE_PARSER, + *err = bc_error_parser(BLOGC_ERROR_TEMPLATE_PARSER, src, src_len, current, "Blocks can't be nested."); break; } @@ -204,14 +205,14 @@ blogc_template_parse(const char *src, size_t src_len, blogc_error_t **err) { if (block_open) { if (if_count != block_if_count) { - *err = blogc_error_new_printf(BLOGC_ERROR_TEMPLATE_PARSER, + *err = bc_error_new_printf(BLOGC_ERROR_TEMPLATE_PARSER, "%d open 'if', 'ifdef' and/or 'ifndef' statements " "were not closed inside a '%s' block!", if_count - block_if_count, block_type); break; } if (!block_foreach_open && foreach_open) { - *err = blogc_error_new_printf(BLOGC_ERROR_TEMPLATE_PARSER, + *err = bc_error_new_printf(BLOGC_ERROR_TEMPLATE_PARSER, "An open 'foreach' statement was not closed " "inside a '%s' block!", block_type); break; @@ -221,7 +222,7 @@ blogc_template_parse(const char *src, size_t src_len, blogc_error_t **err) block_open = false; break; } - *err = blogc_error_parser(BLOGC_ERROR_TEMPLATE_PARSER, + *err = bc_error_parser(BLOGC_ERROR_TEMPLATE_PARSER, src, src_len, current, "'endblock' statement without an open 'block' statement."); break; @@ -268,13 +269,13 @@ blogc_template_parse(const char *src, size_t src_len, blogc_error_t **err) else_open = true; break; } - *err = blogc_error_parser(BLOGC_ERROR_TEMPLATE_PARSER, + *err = bc_error_parser(BLOGC_ERROR_TEMPLATE_PARSER, src, src_len, current, "More than one 'else' statement for an open 'if', " "'ifdef' or 'ifndef' statement."); break; } - *err = blogc_error_parser(BLOGC_ERROR_TEMPLATE_PARSER, + *err = bc_error_parser(BLOGC_ERROR_TEMPLATE_PARSER, src, src_len, current, "'else' statement without an open 'if', 'ifdef' or " "'ifndef' statement."); @@ -292,7 +293,7 @@ blogc_template_parse(const char *src, size_t src_len, blogc_error_t **err) else_open = false; break; } - *err = blogc_error_parser(BLOGC_ERROR_TEMPLATE_PARSER, + *err = bc_error_parser(BLOGC_ERROR_TEMPLATE_PARSER, src, src_len, current, "'endif' statement without an open 'if', 'ifdef' or " "'ifndef' statement."); @@ -308,7 +309,7 @@ blogc_template_parse(const char *src, size_t src_len, blogc_error_t **err) foreach_open = true; break; } - *err = blogc_error_parser(BLOGC_ERROR_TEMPLATE_PARSER, + *err = bc_error_parser(BLOGC_ERROR_TEMPLATE_PARSER, src, src_len, current, "'foreach' statements can't " "be nested."); break; @@ -324,14 +325,14 @@ blogc_template_parse(const char *src, size_t src_len, blogc_error_t **err) foreach_open = false; break; } - *err = blogc_error_parser(BLOGC_ERROR_TEMPLATE_PARSER, + *err = bc_error_parser(BLOGC_ERROR_TEMPLATE_PARSER, src, src_len, current, "'endforeach' statement without an open 'foreach' " "statement."); break; } } - *err = blogc_error_parser(BLOGC_ERROR_TEMPLATE_PARSER, src, + *err = bc_error_parser(BLOGC_ERROR_TEMPLATE_PARSER, src, src_len, current, "Invalid statement type: Allowed types are: 'block', " "'endblock', 'if', 'ifdef', 'ifndef', 'else', 'endif', " @@ -346,7 +347,7 @@ blogc_template_parse(const char *src, size_t src_len, blogc_error_t **err) start = current; break; } - *err = blogc_error_parser(BLOGC_ERROR_TEMPLATE_PARSER, src, + *err = bc_error_parser(BLOGC_ERROR_TEMPLATE_PARSER, src, src_len, current, "Invalid block syntax. Must begin with lowercase letter."); break; @@ -380,7 +381,7 @@ blogc_template_parse(const char *src, size_t src_len, blogc_error_t **err) break; } } - *err = blogc_error_parser(BLOGC_ERROR_TEMPLATE_PARSER, src, + *err = bc_error_parser(BLOGC_ERROR_TEMPLATE_PARSER, src, src_len, current, "Invalid block type. Allowed types are: 'entry', 'listing' " "and 'listing_once'."); @@ -394,7 +395,7 @@ blogc_template_parse(const char *src, size_t src_len, blogc_error_t **err) start = current; break; } - *err = blogc_error_parser(BLOGC_ERROR_TEMPLATE_PARSER, src, + *err = bc_error_parser(BLOGC_ERROR_TEMPLATE_PARSER, src, src_len, current, "Invalid variable name. Must begin with uppercase letter."); break; @@ -410,7 +411,7 @@ blogc_template_parse(const char *src, size_t src_len, blogc_error_t **err) state = TEMPLATE_BLOCK_END_WHITESPACE_CLEANER; break; } - *err = blogc_error_parser(BLOGC_ERROR_TEMPLATE_PARSER, src, + *err = bc_error_parser(BLOGC_ERROR_TEMPLATE_PARSER, src, src_len, current, "Invalid variable name. Must be uppercase letter, number " "or '_'."); @@ -446,7 +447,7 @@ blogc_template_parse(const char *src, size_t src_len, blogc_error_t **err) } op_start = 0; op_end = 0; - *err = blogc_error_parser(BLOGC_ERROR_TEMPLATE_PARSER, src, + *err = bc_error_parser(BLOGC_ERROR_TEMPLATE_PARSER, src, src_len, current, "Invalid 'if' operand. Must be double-quoted static " "string or variable."); @@ -476,7 +477,7 @@ blogc_template_parse(const char *src, size_t src_len, blogc_error_t **err) start = current; break; } - *err = blogc_error_parser(BLOGC_ERROR_TEMPLATE_PARSER, src, + *err = bc_error_parser(BLOGC_ERROR_TEMPLATE_PARSER, src, src_len, current, "Invalid foreach variable name. Must begin with uppercase " "letter."); @@ -490,7 +491,7 @@ blogc_template_parse(const char *src, size_t src_len, blogc_error_t **err) state = TEMPLATE_BLOCK_END_WHITESPACE_CLEANER; break; } - *err = blogc_error_parser(BLOGC_ERROR_TEMPLATE_PARSER, src, + *err = bc_error_parser(BLOGC_ERROR_TEMPLATE_PARSER, src, src_len, current, "Invalid foreach variable name. Must be uppercase letter, " "number or '_'."); @@ -512,13 +513,13 @@ blogc_template_parse(const char *src, size_t src_len, blogc_error_t **err) break; } if (c == '-') { - *err = blogc_error_parser(BLOGC_ERROR_TEMPLATE_PARSER, src, + *err = bc_error_parser(BLOGC_ERROR_TEMPLATE_PARSER, src, src_len, current, "Invalid statement syntax. Duplicated whitespace " "cleaner after statement."); break; } - *err = blogc_error_parser(BLOGC_ERROR_TEMPLATE_PARSER, src, + *err = bc_error_parser(BLOGC_ERROR_TEMPLATE_PARSER, src, src_len, current, "Invalid statement syntax. Must end with '%%}'."); break; @@ -532,7 +533,7 @@ blogc_template_parse(const char *src, size_t src_len, blogc_error_t **err) start = current; break; } - *err = blogc_error_parser(BLOGC_ERROR_TEMPLATE_PARSER, src, + *err = bc_error_parser(BLOGC_ERROR_TEMPLATE_PARSER, src, src_len, current, "Invalid variable name. Must begin with uppercase letter."); break; @@ -550,7 +551,7 @@ blogc_template_parse(const char *src, size_t src_len, blogc_error_t **err) state = TEMPLATE_CLOSE_BRACKET; break; } - *err = blogc_error_parser(BLOGC_ERROR_TEMPLATE_PARSER, src, + *err = bc_error_parser(BLOGC_ERROR_TEMPLATE_PARSER, src, src_len, current, "Invalid variable name. Must be uppercase letter, number " "or '_'."); @@ -563,7 +564,7 @@ blogc_template_parse(const char *src, size_t src_len, blogc_error_t **err) state = TEMPLATE_CLOSE_BRACKET; break; } - *err = blogc_error_parser(BLOGC_ERROR_TEMPLATE_PARSER, src, + *err = bc_error_parser(BLOGC_ERROR_TEMPLATE_PARSER, src, src_len, current, "Invalid statement syntax. Must end with '}}'."); break; @@ -589,7 +590,7 @@ blogc_template_parse(const char *src, size_t src_len, blogc_error_t **err) tmp_op = BLOGC_TEMPLATE_OP_NEQ; } if (tmp_op == 0) { - *err = blogc_error_parser(BLOGC_ERROR_TEMPLATE_PARSER, + *err = bc_error_parser(BLOGC_ERROR_TEMPLATE_PARSER, src, src_len, op_start, "Invalid 'if' operator. Must be '<', '>', " "'<=', '>=', '==' or '!='."); @@ -622,7 +623,7 @@ blogc_template_parse(const char *src, size_t src_len, blogc_error_t **err) start = current + 1; break; } - *err = blogc_error_parser(BLOGC_ERROR_TEMPLATE_PARSER, src, + *err = bc_error_parser(BLOGC_ERROR_TEMPLATE_PARSER, src, src_len, current, "Invalid statement syntax. Must end with '}'."); break; @@ -637,17 +638,17 @@ blogc_template_parse(const char *src, size_t src_len, blogc_error_t **err) if (*err == NULL) { if (state == TEMPLATE_BLOCK_IF_STRING_OPERAND) - *err = blogc_error_parser(BLOGC_ERROR_TEMPLATE_PARSER, src, src_len, + *err = bc_error_parser(BLOGC_ERROR_TEMPLATE_PARSER, src, src_len, start2, "Found an open double-quoted string."); else if (if_count != 0) - *err = blogc_error_new_printf(BLOGC_ERROR_TEMPLATE_PARSER, + *err = bc_error_new_printf(BLOGC_ERROR_TEMPLATE_PARSER, "%d open 'if', 'ifdef' and/or 'ifndef' statements were not closed!", if_count); else if (block_open) - *err = blogc_error_new(BLOGC_ERROR_TEMPLATE_PARSER, + *err = bc_error_new(BLOGC_ERROR_TEMPLATE_PARSER, "An open block was not closed!"); else if (foreach_open) - *err = blogc_error_new(BLOGC_ERROR_TEMPLATE_PARSER, + *err = bc_error_new(BLOGC_ERROR_TEMPLATE_PARSER, "An open 'foreach' statement was not closed!"); } diff --git a/src/blogc/template-parser.h b/src/blogc/template-parser.h index b22c2e7..3ed3b07 100644 --- a/src/blogc/template-parser.h +++ b/src/blogc/template-parser.h @@ -10,7 +10,8 @@ #define _TEMPLATE_PARSER_H #include -#include "error.h" +#include "errors.h" +#include "../common/error.h" #include "../common/utils.h" /* @@ -47,7 +48,7 @@ typedef struct { } blogc_template_stmt_t; bc_slist_t* blogc_template_parse(const char *src, size_t src_len, - blogc_error_t **err); + bc_error_t **err); void blogc_template_free_stmts(bc_slist_t *stmts); #endif /* _TEMPLATE_PARSER_H */ diff --git a/src/common/error.c b/src/common/error.c new file mode 100644 index 0000000..dbc1d81 --- /dev/null +++ b/src/common/error.c @@ -0,0 +1,111 @@ +/* + * blogc: A blog compiler. + * Copyright (C) 2015-2016 Rafael G. Martins + * + * This program can be distributed under the terms of the BSD License. + * See the file LICENSE. + */ + +#include +#include +#include +#include "error.h" +#include "utils.h" + + +bc_error_t* +bc_error_new(int type, const char *msg) +{ + bc_error_t *err = bc_malloc(sizeof(bc_error_t)); + err->type = type; + err->msg = bc_strdup(msg); + return err; +} + + +bc_error_t* +bc_error_new_printf(int type, const char *format, ...) +{ + va_list ap; + va_start(ap, format); + char *tmp = bc_strdup_vprintf(format, ap); + va_end(ap); + bc_error_t *rv = bc_error_new(type, tmp); + free(tmp); + return rv; +} + + +bc_error_t* +bc_error_parser(int type, const char *src, size_t src_len, + size_t current, const char *format, ...) +{ + va_list ap; + va_start(ap, format); + char *msg = bc_strdup_vprintf(format, ap); + va_end(ap); + + size_t lineno = 1; + size_t linestart = 0; + size_t lineend = 0; + size_t pos = 1; + + for (size_t i = 0; i < src_len; i++) { + char c = src[i]; + if (i < current) { + if ((i + 1) < src_len) { + if ((c == '\n' && src[i + 1] == '\r') || + (c == '\r' && src[i + 1] == '\n')) + { + lineno++; + i++; + pos = 1; + if ((i + 1) < src_len) + linestart = i + 1; + continue; + } + } + if (c == '\n' || c == '\r') { + lineno++; + pos = 1; + if ((i + 1) < src_len) + linestart = i + 1; + continue; + } + pos++; + } + else if (c == '\n' || c == '\r') { + lineend = i; + break; + } + } + + if (lineend <= linestart && src_len >= linestart) + lineend = src_len; + + char *line = bc_strndup(src + linestart, lineend - linestart); + + bc_error_t *rv = NULL; + + if (line[0] == '\0') // "near" message isn't useful if line is empty + rv = bc_error_new(type, msg); + else + rv = bc_error_new_printf(type, + "%s\nError occurred near line %d, position %d: %s", msg, lineno, + pos, line); + + free(msg); + free(line); + + return rv; +} + + +void +bc_error_free(bc_error_t *err) +{ + if (err == NULL) + return; + free(err->msg); + free(err); +} diff --git a/src/common/error.h b/src/common/error.h new file mode 100644 index 0000000..17843ed --- /dev/null +++ b/src/common/error.h @@ -0,0 +1,26 @@ +/* + * blogc: A blog compiler. + * Copyright (C) 2015-2016 Rafael G. Martins + * + * This program can be distributed under the terms of the BSD License. + * See the file LICENSE. + */ + +#ifndef _ERROR_H +#define _ERROR_H + +#include + +typedef struct { + char *msg; + int type; +} bc_error_t; + +bc_error_t* bc_error_new(int type, const char *msg); +bc_error_t* bc_error_new_printf(int type, const char *format, ...); +bc_error_t* bc_error_parser(int type, const char *src, size_t src_len, + size_t current, const char *format, ...); +void bc_error_print(bc_error_t *err); +void bc_error_free(bc_error_t *err); + +#endif /* _ERROR_H */ diff --git a/tests/blogc/check_datetime_parser.c b/tests/blogc/check_datetime_parser.c index ec0f120..e8791d1 100644 --- a/tests/blogc/check_datetime_parser.c +++ b/tests/blogc/check_datetime_parser.c @@ -12,14 +12,15 @@ #include #include #include -#include "../../src/blogc/error.h" +#include "../../src/common/error.h" #include "../../src/blogc/datetime-parser.h" +#include "../../src/blogc/errors.h" static void test_convert_datetime(void **state) { - blogc_error_t *err = NULL; + bc_error_t *err = NULL; char *dt = blogc_convert_datetime("2010-11-30 12:13:14", "%b %d, %Y, %I:%M:%S %p GMT", &err); assert_null(err); @@ -31,7 +32,7 @@ test_convert_datetime(void **state) static void test_convert_datetime_implicit_seconds(void **state) { - blogc_error_t *err = NULL; + bc_error_t *err = NULL; char *dt = blogc_convert_datetime("2010-11-30 12:13", "%b %d, %Y, %I:%M:%S %p GMT", &err); assert_null(err); @@ -43,7 +44,7 @@ test_convert_datetime_implicit_seconds(void **state) static void test_convert_datetime_implicit_minutes(void **state) { - blogc_error_t *err = NULL; + bc_error_t *err = NULL; char *dt = blogc_convert_datetime("2010-11-30 12", "%b %d, %Y, %I:%M:%S %p GMT", &err); assert_null(err); @@ -55,7 +56,7 @@ test_convert_datetime_implicit_minutes(void **state) static void test_convert_datetime_implicit_hours(void **state) { - blogc_error_t *err = NULL; + bc_error_t *err = NULL; char *dt = blogc_convert_datetime("2010-11-30", "%b %d, %Y, %I:%M:%S %p GMT", &err); assert_null(err); @@ -67,7 +68,7 @@ test_convert_datetime_implicit_hours(void **state) static void test_convert_datetime_invalid_formats(void **state) { - blogc_error_t *err = NULL; + bc_error_t *err = NULL; char *dt = blogc_convert_datetime("", "%b %d, %Y, %I:%M:%S %p GMT", &err); assert_null(dt); assert_non_null(err); @@ -76,7 +77,7 @@ test_convert_datetime_invalid_formats(void **state) "Invalid datetime string. Found '', formats allowed are: " "'yyyy-mm-dd hh:mm:ss', 'yyyy-mm-dd hh:ss', 'yyyy-mm-dd hh' and " "'yyyy-mm-dd'."); - blogc_error_free(err); + bc_error_free(err); err = NULL; dt = blogc_convert_datetime("2", "%b %d, %Y, %I:%M:%S %p GMT", &err); @@ -87,7 +88,7 @@ test_convert_datetime_invalid_formats(void **state) "Invalid datetime string. Found '2', formats allowed are: " "'yyyy-mm-dd hh:mm:ss', 'yyyy-mm-dd hh:ss', 'yyyy-mm-dd hh' and " "'yyyy-mm-dd'."); - blogc_error_free(err); + bc_error_free(err); err = NULL; dt = blogc_convert_datetime("20", "%b %d, %Y, %I:%M:%S %p GMT", &err); @@ -98,7 +99,7 @@ test_convert_datetime_invalid_formats(void **state) "Invalid datetime string. Found '20', formats allowed are: " "'yyyy-mm-dd hh:mm:ss', 'yyyy-mm-dd hh:ss', 'yyyy-mm-dd hh' and " "'yyyy-mm-dd'."); - blogc_error_free(err); + bc_error_free(err); err = NULL; dt = blogc_convert_datetime("201", "%b %d, %Y, %I:%M:%S %p GMT", &err); @@ -109,7 +110,7 @@ test_convert_datetime_invalid_formats(void **state) "Invalid datetime string. Found '201', formats allowed are: " "'yyyy-mm-dd hh:mm:ss', 'yyyy-mm-dd hh:ss', 'yyyy-mm-dd hh' and " "'yyyy-mm-dd'."); - blogc_error_free(err); + bc_error_free(err); err = NULL; dt = blogc_convert_datetime("2010", "%b %d, %Y, %I:%M:%S %p GMT", &err); @@ -120,7 +121,7 @@ test_convert_datetime_invalid_formats(void **state) "Invalid datetime string. Found '2010', formats allowed are: " "'yyyy-mm-dd hh:mm:ss', 'yyyy-mm-dd hh:ss', 'yyyy-mm-dd hh' and " "'yyyy-mm-dd'."); - blogc_error_free(err); + bc_error_free(err); err = NULL; dt = blogc_convert_datetime("2010-", "%b %d, %Y, %I:%M:%S %p GMT", &err); @@ -131,7 +132,7 @@ test_convert_datetime_invalid_formats(void **state) "Invalid datetime string. Found '2010-', formats allowed are: " "'yyyy-mm-dd hh:mm:ss', 'yyyy-mm-dd hh:ss', 'yyyy-mm-dd hh' and " "'yyyy-mm-dd'."); - blogc_error_free(err); + bc_error_free(err); err = NULL; dt = blogc_convert_datetime("2010-1", "%b %d, %Y, %I:%M:%S %p GMT", &err); @@ -142,7 +143,7 @@ test_convert_datetime_invalid_formats(void **state) "Invalid datetime string. Found '2010-1', formats allowed are: " "'yyyy-mm-dd hh:mm:ss', 'yyyy-mm-dd hh:ss', 'yyyy-mm-dd hh' and " "'yyyy-mm-dd'."); - blogc_error_free(err); + bc_error_free(err); err = NULL; dt = blogc_convert_datetime("2010-11", "%b %d, %Y, %I:%M:%S %p GMT", &err); @@ -153,7 +154,7 @@ test_convert_datetime_invalid_formats(void **state) "Invalid datetime string. Found '2010-11', formats allowed are: " "'yyyy-mm-dd hh:mm:ss', 'yyyy-mm-dd hh:ss', 'yyyy-mm-dd hh' and " "'yyyy-mm-dd'."); - blogc_error_free(err); + bc_error_free(err); err = NULL; dt = blogc_convert_datetime("2010-11-", "%b %d, %Y, %I:%M:%S %p GMT", &err); @@ -164,7 +165,7 @@ test_convert_datetime_invalid_formats(void **state) "Invalid datetime string. Found '2010-11-', formats allowed are: " "'yyyy-mm-dd hh:mm:ss', 'yyyy-mm-dd hh:ss', 'yyyy-mm-dd hh' and " "'yyyy-mm-dd'."); - blogc_error_free(err); + bc_error_free(err); err = NULL; dt = blogc_convert_datetime("2010-11-3", "%b %d, %Y, %I:%M:%S %p GMT", &err); @@ -175,7 +176,7 @@ test_convert_datetime_invalid_formats(void **state) "Invalid datetime string. Found '2010-11-3', formats allowed are: " "'yyyy-mm-dd hh:mm:ss', 'yyyy-mm-dd hh:ss', 'yyyy-mm-dd hh' and " "'yyyy-mm-dd'."); - blogc_error_free(err); + bc_error_free(err); err = NULL; dt = blogc_convert_datetime("2010-11-30 ", "%b %d, %Y, %I:%M:%S %p GMT", &err); @@ -186,7 +187,7 @@ test_convert_datetime_invalid_formats(void **state) "Invalid datetime string. Found '2010-11-30 ', formats allowed are: " "'yyyy-mm-dd hh:mm:ss', 'yyyy-mm-dd hh:ss', 'yyyy-mm-dd hh' and " "'yyyy-mm-dd'."); - blogc_error_free(err); + bc_error_free(err); err = NULL; dt = blogc_convert_datetime("2010-11-30 1", "%b %d, %Y, %I:%M:%S %p GMT", &err); @@ -197,7 +198,7 @@ test_convert_datetime_invalid_formats(void **state) "Invalid datetime string. Found '2010-11-30 1', formats allowed are: " "'yyyy-mm-dd hh:mm:ss', 'yyyy-mm-dd hh:ss', 'yyyy-mm-dd hh' and " "'yyyy-mm-dd'."); - blogc_error_free(err); + bc_error_free(err); err = NULL; dt = blogc_convert_datetime("2010-11-30 12:1", "%b %d, %Y, %I:%M:%S %p GMT", @@ -209,7 +210,7 @@ test_convert_datetime_invalid_formats(void **state) "Invalid datetime string. Found '2010-11-30 12:1', formats allowed are: " "'yyyy-mm-dd hh:mm:ss', 'yyyy-mm-dd hh:ss', 'yyyy-mm-dd hh' and " "'yyyy-mm-dd'."); - blogc_error_free(err); + bc_error_free(err); err = NULL; dt = blogc_convert_datetime("2010-11-30 12:13:1", "%b %d, %Y, %I:%M:%S %p GMT", @@ -221,14 +222,14 @@ test_convert_datetime_invalid_formats(void **state) "Invalid datetime string. Found '2010-11-30 12:13:1', formats allowed are: " "'yyyy-mm-dd hh:mm:ss', 'yyyy-mm-dd hh:ss', 'yyyy-mm-dd hh' and " "'yyyy-mm-dd'."); - blogc_error_free(err); + bc_error_free(err); } static void test_convert_datetime_invalid_1st_year(void **state) { - blogc_error_t *err = NULL; + bc_error_t *err = NULL; char *dt = blogc_convert_datetime("a010-11-30 12:13:14", "%b %d, %Y, %I:%M:%S %p GMT", &err); assert_null(dt); @@ -236,14 +237,14 @@ test_convert_datetime_invalid_1st_year(void **state) assert_int_equal(err->type, BLOGC_WARNING_DATETIME_PARSER); assert_string_equal(err->msg, "Invalid first digit of year. Found 'a', must be integer >= 0 and <= 9."); - blogc_error_free(err); + bc_error_free(err); } static void test_convert_datetime_invalid_2nd_year(void **state) { - blogc_error_t *err = NULL; + bc_error_t *err = NULL; char *dt = blogc_convert_datetime("2a10-11-30 12:13:14", "%b %d, %Y, %I:%M:%S %p GMT", &err); assert_null(dt); @@ -251,14 +252,14 @@ test_convert_datetime_invalid_2nd_year(void **state) assert_int_equal(err->type, BLOGC_WARNING_DATETIME_PARSER); assert_string_equal(err->msg, "Invalid second digit of year. Found 'a', must be integer >= 0 and <= 9."); - blogc_error_free(err); + bc_error_free(err); } static void test_convert_datetime_invalid_3rd_year(void **state) { - blogc_error_t *err = NULL; + bc_error_t *err = NULL; char *dt = blogc_convert_datetime("20a0-11-30 12:13:14", "%b %d, %Y, %I:%M:%S %p GMT", &err); assert_null(dt); @@ -266,14 +267,14 @@ test_convert_datetime_invalid_3rd_year(void **state) assert_int_equal(err->type, BLOGC_WARNING_DATETIME_PARSER); assert_string_equal(err->msg, "Invalid third digit of year. Found 'a', must be integer >= 0 and <= 9."); - blogc_error_free(err); + bc_error_free(err); } static void test_convert_datetime_invalid_4th_year(void **state) { - blogc_error_t *err = NULL; + bc_error_t *err = NULL; char *dt = blogc_convert_datetime("201a-11-30 12:13:14", "%b %d, %Y, %I:%M:%S %p GMT", &err); assert_null(dt); @@ -281,14 +282,14 @@ test_convert_datetime_invalid_4th_year(void **state) assert_int_equal(err->type, BLOGC_WARNING_DATETIME_PARSER); assert_string_equal(err->msg, "Invalid fourth digit of year. Found 'a', must be integer >= 0 and <= 9."); - blogc_error_free(err); + bc_error_free(err); } static void test_convert_datetime_invalid_year(void **state) { - blogc_error_t *err = NULL; + bc_error_t *err = NULL; char *dt = blogc_convert_datetime("1899-11-30 12:13:14", "%b %d, %Y, %I:%M:%S %p GMT", &err); assert_null(dt); @@ -296,14 +297,14 @@ test_convert_datetime_invalid_year(void **state) assert_int_equal(err->type, BLOGC_WARNING_DATETIME_PARSER); assert_string_equal(err->msg, "Invalid year. Found 1899, must be >= 1900."); - blogc_error_free(err); + bc_error_free(err); } static void test_convert_datetime_invalid_1st_hyphen(void **state) { - blogc_error_t *err = NULL; + bc_error_t *err = NULL; char *dt = blogc_convert_datetime("2010 11-30 12:13:14", "%b %d, %Y, %I:%M:%S %p GMT", &err); assert_null(dt); @@ -311,14 +312,14 @@ test_convert_datetime_invalid_1st_hyphen(void **state) assert_int_equal(err->type, BLOGC_WARNING_DATETIME_PARSER); assert_string_equal(err->msg, "Invalid separator between year and month. Found ' ', must be '-'."); - blogc_error_free(err); + bc_error_free(err); } static void test_convert_datetime_invalid_1st_month(void **state) { - blogc_error_t *err = NULL; + bc_error_t *err = NULL; char *dt = blogc_convert_datetime("2010-a1-30 12:13:14", "%b %d, %Y, %I:%M:%S %p GMT", &err); assert_null(dt); @@ -326,14 +327,14 @@ test_convert_datetime_invalid_1st_month(void **state) assert_int_equal(err->type, BLOGC_WARNING_DATETIME_PARSER); assert_string_equal(err->msg, "Invalid first digit of month. Found 'a', must be integer >= 0 and <= 1."); - blogc_error_free(err); + bc_error_free(err); } static void test_convert_datetime_invalid_2nd_month(void **state) { - blogc_error_t *err = NULL; + bc_error_t *err = NULL; char *dt = blogc_convert_datetime("2010-1a-30 12:13:14", "%b %d, %Y, %I:%M:%S %p GMT", &err); assert_null(dt); @@ -341,14 +342,14 @@ test_convert_datetime_invalid_2nd_month(void **state) assert_int_equal(err->type, BLOGC_WARNING_DATETIME_PARSER); assert_string_equal(err->msg, "Invalid second digit of month. Found 'a', must be integer >= 0 and <= 9."); - blogc_error_free(err); + bc_error_free(err); } static void test_convert_datetime_invalid_month(void **state) { - blogc_error_t *err = NULL; + bc_error_t *err = NULL; char *dt = blogc_convert_datetime("2010-13-30 12:13:14", "%b %d, %Y, %I:%M:%S %p GMT", &err); assert_null(dt); @@ -356,14 +357,14 @@ test_convert_datetime_invalid_month(void **state) assert_int_equal(err->type, BLOGC_WARNING_DATETIME_PARSER); assert_string_equal(err->msg, "Invalid month. Found 13, must be >= 1 and <= 12."); - blogc_error_free(err); + bc_error_free(err); } static void test_convert_datetime_invalid_2nd_hyphen(void **state) { - blogc_error_t *err = NULL; + bc_error_t *err = NULL; char *dt = blogc_convert_datetime("2010-11 30 12:13:14", "%b %d, %Y, %I:%M:%S %p GMT", &err); assert_null(dt); @@ -371,14 +372,14 @@ test_convert_datetime_invalid_2nd_hyphen(void **state) assert_int_equal(err->type, BLOGC_WARNING_DATETIME_PARSER); assert_string_equal(err->msg, "Invalid separator between month and day. Found ' ', must be '-'."); - blogc_error_free(err); + bc_error_free(err); } static void test_convert_datetime_invalid_1st_day(void **state) { - blogc_error_t *err = NULL; + bc_error_t *err = NULL; char *dt = blogc_convert_datetime("2010-11-a0 12:13:14", "%b %d, %Y, %I:%M:%S %p GMT", &err); assert_null(dt); @@ -386,14 +387,14 @@ test_convert_datetime_invalid_1st_day(void **state) assert_int_equal(err->type, BLOGC_WARNING_DATETIME_PARSER); assert_string_equal(err->msg, "Invalid first digit of day. Found 'a', must be integer >= 0 and <= 3."); - blogc_error_free(err); + bc_error_free(err); } static void test_convert_datetime_invalid_2nd_day(void **state) { - blogc_error_t *err = NULL; + bc_error_t *err = NULL; char *dt = blogc_convert_datetime("2010-11-3a 12:13:14", "%b %d, %Y, %I:%M:%S %p GMT", &err); assert_null(dt); @@ -401,14 +402,14 @@ test_convert_datetime_invalid_2nd_day(void **state) assert_int_equal(err->type, BLOGC_WARNING_DATETIME_PARSER); assert_string_equal(err->msg, "Invalid second digit of day. Found 'a', must be integer >= 0 and <= 9."); - blogc_error_free(err); + bc_error_free(err); } static void test_convert_datetime_invalid_day(void **state) { - blogc_error_t *err = NULL; + bc_error_t *err = NULL; char *dt = blogc_convert_datetime("2010-12-32 12:13:14", "%b %d, %Y, %I:%M:%S %p GMT", &err); assert_null(dt); @@ -416,14 +417,14 @@ test_convert_datetime_invalid_day(void **state) assert_int_equal(err->type, BLOGC_WARNING_DATETIME_PARSER); assert_string_equal(err->msg, "Invalid day. Found 32, must be >= 1 and <= 31."); - blogc_error_free(err); + bc_error_free(err); } static void test_convert_datetime_invalid_space(void **state) { - blogc_error_t *err = NULL; + bc_error_t *err = NULL; char *dt = blogc_convert_datetime("2010-11-30-12:13:14", "%b %d, %Y, %I:%M:%S %p GMT", &err); assert_null(dt); @@ -432,14 +433,14 @@ test_convert_datetime_invalid_space(void **state) assert_string_equal(err->msg, "Invalid separator between date and time. Found '-', must be ' ' " "(empty space)."); - blogc_error_free(err); + bc_error_free(err); } static void test_convert_datetime_invalid_1st_hours(void **state) { - blogc_error_t *err = NULL; + bc_error_t *err = NULL; char *dt = blogc_convert_datetime("2010-11-30 a2:13:14", "%b %d, %Y, %I:%M:%S %p GMT", &err); assert_null(dt); @@ -447,14 +448,14 @@ test_convert_datetime_invalid_1st_hours(void **state) assert_int_equal(err->type, BLOGC_WARNING_DATETIME_PARSER); assert_string_equal(err->msg, "Invalid first digit of hours. Found 'a', must be integer >= 0 and <= 2."); - blogc_error_free(err); + bc_error_free(err); } static void test_convert_datetime_invalid_2nd_hours(void **state) { - blogc_error_t *err = NULL; + bc_error_t *err = NULL; char *dt = blogc_convert_datetime("2010-11-30 1a:13:14", "%b %d, %Y, %I:%M:%S %p GMT", &err); assert_null(dt); @@ -462,14 +463,14 @@ test_convert_datetime_invalid_2nd_hours(void **state) assert_int_equal(err->type, BLOGC_WARNING_DATETIME_PARSER); assert_string_equal(err->msg, "Invalid second digit of hours. Found 'a', must be integer >= 0 and <= 9."); - blogc_error_free(err); + bc_error_free(err); } static void test_convert_datetime_invalid_hours(void **state) { - blogc_error_t *err = NULL; + bc_error_t *err = NULL; char *dt = blogc_convert_datetime("2010-12-30 24:13:14", "%b %d, %Y, %I:%M:%S %p GMT", &err); assert_null(dt); @@ -477,14 +478,14 @@ test_convert_datetime_invalid_hours(void **state) assert_int_equal(err->type, BLOGC_WARNING_DATETIME_PARSER); assert_string_equal(err->msg, "Invalid hours. Found 24, must be >= 0 and <= 23."); - blogc_error_free(err); + bc_error_free(err); } static void test_convert_datetime_invalid_1st_colon(void **state) { - blogc_error_t *err = NULL; + bc_error_t *err = NULL; char *dt = blogc_convert_datetime("2010-11-30 12 13:14", "%b %d, %Y, %I:%M:%S %p GMT", &err); assert_null(dt); @@ -492,14 +493,14 @@ test_convert_datetime_invalid_1st_colon(void **state) assert_int_equal(err->type, BLOGC_WARNING_DATETIME_PARSER); assert_string_equal(err->msg, "Invalid separator between hours and minutes. Found ' ', must be ':'."); - blogc_error_free(err); + bc_error_free(err); } static void test_convert_datetime_invalid_1st_minutes(void **state) { - blogc_error_t *err = NULL; + bc_error_t *err = NULL; char *dt = blogc_convert_datetime("2010-11-30 12:a3:14", "%b %d, %Y, %I:%M:%S %p GMT", &err); assert_null(dt); @@ -507,14 +508,14 @@ test_convert_datetime_invalid_1st_minutes(void **state) assert_int_equal(err->type, BLOGC_WARNING_DATETIME_PARSER); assert_string_equal(err->msg, "Invalid first digit of minutes. Found 'a', must be integer >= 0 and <= 5."); - blogc_error_free(err); + bc_error_free(err); } static void test_convert_datetime_invalid_2nd_minutes(void **state) { - blogc_error_t *err = NULL; + bc_error_t *err = NULL; char *dt = blogc_convert_datetime("2010-11-30 12:1a:14", "%b %d, %Y, %I:%M:%S %p GMT", &err); assert_null(dt); @@ -522,14 +523,14 @@ test_convert_datetime_invalid_2nd_minutes(void **state) assert_int_equal(err->type, BLOGC_WARNING_DATETIME_PARSER); assert_string_equal(err->msg, "Invalid second digit of minutes. Found 'a', must be integer >= 0 and <= 9."); - blogc_error_free(err); + bc_error_free(err); } static void test_convert_datetime_invalid_2nd_colon(void **state) { - blogc_error_t *err = NULL; + bc_error_t *err = NULL; char *dt = blogc_convert_datetime("2010-11-30 12:13 14", "%b %d, %Y, %I:%M:%S %p GMT", &err); assert_null(dt); @@ -537,14 +538,14 @@ test_convert_datetime_invalid_2nd_colon(void **state) assert_int_equal(err->type, BLOGC_WARNING_DATETIME_PARSER); assert_string_equal(err->msg, "Invalid separator between minutes and seconds. Found ' ', must be ':'."); - blogc_error_free(err); + bc_error_free(err); } static void test_convert_datetime_invalid_1st_seconds(void **state) { - blogc_error_t *err = NULL; + bc_error_t *err = NULL; char *dt = blogc_convert_datetime("2010-11-30 12:13:a4", "%b %d, %Y, %I:%M:%S %p GMT", &err); assert_null(dt); @@ -552,14 +553,14 @@ test_convert_datetime_invalid_1st_seconds(void **state) assert_int_equal(err->type, BLOGC_WARNING_DATETIME_PARSER); assert_string_equal(err->msg, "Invalid first digit of seconds. Found 'a', must be integer >= 0 and <= 6."); - blogc_error_free(err); + bc_error_free(err); } static void test_convert_datetime_invalid_2nd_seconds(void **state) { - blogc_error_t *err = NULL; + bc_error_t *err = NULL; char *dt = blogc_convert_datetime("2010-11-30 12:13:1a", "%b %d, %Y, %I:%M:%S %p GMT", &err); assert_null(dt); @@ -567,14 +568,14 @@ test_convert_datetime_invalid_2nd_seconds(void **state) assert_int_equal(err->type, BLOGC_WARNING_DATETIME_PARSER); assert_string_equal(err->msg, "Invalid second digit of seconds. Found 'a', must be integer >= 0 and <= 9."); - blogc_error_free(err); + bc_error_free(err); } static void test_convert_datetime_invalid_seconds(void **state) { - blogc_error_t *err = NULL; + bc_error_t *err = NULL; char *dt = blogc_convert_datetime("2010-12-30 12:13:69", "%b %d, %Y, %I:%M:%S %p GMT", &err); assert_null(dt); @@ -582,14 +583,14 @@ test_convert_datetime_invalid_seconds(void **state) assert_int_equal(err->type, BLOGC_WARNING_DATETIME_PARSER); assert_string_equal(err->msg, "Invalid seconds. Found 69, must be >= 0 and <= 60."); - blogc_error_free(err); + bc_error_free(err); } static void test_convert_datetime_invalid_format_long(void **state) { - blogc_error_t *err = NULL; + bc_error_t *err = NULL; char *dt = blogc_convert_datetime("2010-12-30 12:13:14", "bovhsuhxwybfrxoluiejaoqpmoylgvkrjtnuntmcgtupwabexkapnklvkwmddmplfqopvb" "yjsiimtfdeveeeayqvvnthimbqotumngxxenurxhsvyaftwsfdtxqnjluvtcwfkomfffrk" @@ -626,7 +627,7 @@ test_convert_datetime_invalid_format_long(void **state) "uaeruwnphdjonqagjatjladqhvlxppyaqgvwpjqggnsccmkjvbxqykaejvgeajqpitkwsq" "gmjiaopomnnlewidhgbgqlblotrnuyokspuvbckqhwnhmgcwyyitmlelnehdvclojvyswj" "jgipsincitulscikxviaruryfraeqssykeftcphtndlfhdxokg"); - blogc_error_free(err); + bc_error_free(err); } diff --git a/tests/blogc/check_error.c b/tests/blogc/check_error.c deleted file mode 100644 index 9976ac2..0000000 --- a/tests/blogc/check_error.c +++ /dev/null @@ -1,109 +0,0 @@ -/* - * blogc: A blog compiler. - * Copyright (C) 2015-2016 Rafael G. Martins - * - * This program can be distributed under the terms of the BSD License. - * See the file LICENSE. - */ - -#include -#include -#include -#include -#include -#include "../../src/blogc/error.h" - - -static void -test_error_new(void **state) -{ - blogc_error_t *error = blogc_error_new(1, "bola %s"); - assert_non_null(error); - assert_int_equal(error->type, 1); - assert_string_equal(error->msg, "bola %s"); - blogc_error_free(error); -} - - -static void -test_error_new_printf(void **state) -{ - blogc_error_t *error = blogc_error_new_printf(2, "bola %s", "guda"); - assert_non_null(error); - assert_int_equal(error->type, 2); - assert_string_equal(error->msg, "bola guda"); - blogc_error_free(error); -} - - -static void -test_error_parser(void **state) -{ - const char *a = "bola\nguda\nchunda\n"; - blogc_error_t *error = blogc_error_parser(1, a, strlen(a), 11, "asd %d", 10); - assert_non_null(error); - assert_int_equal(error->type, 1); - assert_string_equal(error->msg, - "asd 10\nError occurred near line 3, position 2: chunda"); - blogc_error_free(error); - a = "bola\nguda\nchunda"; - error = blogc_error_parser(1, a, strlen(a), 11, "asd %d", 10); - assert_non_null(error); - assert_int_equal(error->type, 1); - assert_string_equal(error->msg, - "asd 10\nError occurred near line 3, position 2: chunda"); - blogc_error_free(error); - a = "bola\nguda\nchunda"; - error = blogc_error_parser(1, a, strlen(a), 0, "asd %d", 10); - assert_non_null(error); - assert_int_equal(error->type, 1); - assert_string_equal(error->msg, - "asd 10\nError occurred near line 1, position 1: bola"); - blogc_error_free(error); - a = ""; - error = blogc_error_parser(1, a, strlen(a), 0, "asd %d", 10); - assert_non_null(error); - assert_int_equal(error->type, 1); - assert_string_equal(error->msg, "asd 10"); - blogc_error_free(error); -} - - -static void -test_error_parser_crlf(void **state) -{ - const char *a = "bola\r\nguda\r\nchunda\r\n"; - blogc_error_t *error = blogc_error_parser(1, a, strlen(a), 13, "asd %d", 10); - assert_non_null(error); - assert_int_equal(error->type, 1); - assert_string_equal(error->msg, - "asd 10\nError occurred near line 3, position 2: chunda"); - blogc_error_free(error); - a = "bola\r\nguda\r\nchunda"; - error = blogc_error_parser(1, a, strlen(a), 13, "asd %d", 10); - assert_non_null(error); - assert_int_equal(error->type, 1); - assert_string_equal(error->msg, - "asd 10\nError occurred near line 3, position 2: chunda"); - blogc_error_free(error); - a = "bola\r\nguda\r\nchunda"; - error = blogc_error_parser(1, a, strlen(a), 0, "asd %d", 10); - assert_non_null(error); - assert_int_equal(error->type, 1); - assert_string_equal(error->msg, - "asd 10\nError occurred near line 1, position 1: bola"); - blogc_error_free(error); -} - - -int -main(void) -{ - const UnitTest tests[] = { - unit_test(test_error_new), - unit_test(test_error_new_printf), - unit_test(test_error_parser), - unit_test(test_error_parser_crlf), - }; - return run_tests(tests); -} diff --git a/tests/blogc/check_loader.c b/tests/blogc/check_loader.c index abe1f29..8f5ccd2 100644 --- a/tests/blogc/check_loader.c +++ b/tests/blogc/check_loader.c @@ -13,10 +13,11 @@ #include #include #include -#include "../../src/blogc/error.h" +#include "../../src/common/error.h" +#include "../../src/common/utils.h" +#include "../../src/blogc/errors.h" #include "../../src/blogc/template-parser.h" #include "../../src/blogc/loader.h" -#include "../../src/common/utils.h" static void @@ -50,7 +51,7 @@ test_get_filename(void **state) char* -__wrap_blogc_file_get_contents(const char *path, size_t *len, blogc_error_t **err) +__wrap_blogc_file_get_contents(const char *path, size_t *len, bc_error_t **err) { assert_null(*err); const char *_path = mock_type(const char*); @@ -76,7 +77,7 @@ __wrap_blogc_fprintf(FILE *stream, const char *format, ...) static void test_template_parse_from_file(void **state) { - blogc_error_t *err = NULL; + bc_error_t *err = NULL; will_return(__wrap_blogc_file_get_contents, "bola"); will_return(__wrap_blogc_file_get_contents, bc_strdup("{{ BOLA }}\n")); bc_slist_t *l = blogc_template_parse_from_file("bola", &err); @@ -90,7 +91,7 @@ test_template_parse_from_file(void **state) static void test_template_parse_from_file_null(void **state) { - blogc_error_t *err = NULL; + bc_error_t *err = NULL; will_return(__wrap_blogc_file_get_contents, "bola"); will_return(__wrap_blogc_file_get_contents, NULL); bc_slist_t *l = blogc_template_parse_from_file("bola", &err); @@ -102,7 +103,7 @@ test_template_parse_from_file_null(void **state) static void test_source_parse_from_file(void **state) { - blogc_error_t *err = NULL; + bc_error_t *err = NULL; will_return(__wrap_blogc_file_get_contents, "bola.txt"); will_return(__wrap_blogc_file_get_contents, bc_strdup( "ASD: 123\n" @@ -125,7 +126,7 @@ test_source_parse_from_file(void **state) static void test_source_parse_from_file_null(void **state) { - blogc_error_t *err = NULL; + bc_error_t *err = NULL; will_return(__wrap_blogc_file_get_contents, "bola.txt"); will_return(__wrap_blogc_file_get_contents, NULL); bc_trie_t *t = blogc_source_parse_from_file("bola.txt", &err); @@ -155,7 +156,7 @@ test_source_parse_from_files(void **state) "DATE: 2003-02-03 04:05:06\n" "--------\n" "bola")); - blogc_error_t *err = NULL; + bc_error_t *err = NULL; bc_slist_t *s = NULL; s = bc_slist_append(s, bc_strdup("bola1.txt")); s = bc_slist_append(s, bc_strdup("bola2.txt")); @@ -199,7 +200,7 @@ test_source_parse_from_files_filter_by_tag(void **state) "DATE: 2003-02-03 04:05:06\n" "--------\n" "bola")); - blogc_error_t *err = NULL; + bc_error_t *err = NULL; bc_slist_t *s = NULL; s = bc_slist_append(s, bc_strdup("bola1.txt")); s = bc_slist_append(s, bc_strdup("bola2.txt")); @@ -267,7 +268,7 @@ test_source_parse_from_files_filter_by_page(void **state) "DATE: 2007-02-03 04:05:06\n" "--------\n" "bola")); - blogc_error_t *err = NULL; + bc_error_t *err = NULL; bc_slist_t *s = NULL; s = bc_slist_append(s, bc_strdup("bola1.txt")); s = bc_slist_append(s, bc_strdup("bola2.txt")); @@ -345,7 +346,7 @@ test_source_parse_from_files_filter_by_page2(void **state) "DATE: 2007-02-03 04:05:06\n" "--------\n" "bola")); - blogc_error_t *err = NULL; + bc_error_t *err = NULL; bc_slist_t *s = NULL; s = bc_slist_append(s, bc_strdup("bola1.txt")); s = bc_slist_append(s, bc_strdup("bola2.txt")); @@ -424,7 +425,7 @@ test_source_parse_from_files_filter_by_page3(void **state) "DATE: 2007-02-03 04:05:06\n" "--------\n" "bola")); - blogc_error_t *err = NULL; + bc_error_t *err = NULL; bc_slist_t *s = NULL; s = bc_slist_append(s, bc_strdup("bola1.txt")); s = bc_slist_append(s, bc_strdup("bola2.txt")); @@ -507,7 +508,7 @@ test_source_parse_from_files_filter_by_page_and_tag(void **state) "TAGS: yay chunda\n" "--------\n" "bola")); - blogc_error_t *err = NULL; + bc_error_t *err = NULL; bc_slist_t *s = NULL; s = bc_slist_append(s, bc_strdup("bola1.txt")); s = bc_slist_append(s, bc_strdup("bola2.txt")); @@ -587,7 +588,7 @@ test_source_parse_from_files_filter_by_page_invalid(void **state) "DATE: 2007-02-03 04:05:06\n" "--------\n" "bola")); - blogc_error_t *err = NULL; + bc_error_t *err = NULL; bc_slist_t *s = NULL; s = bc_slist_append(s, bc_strdup("bola1.txt")); s = bc_slist_append(s, bc_strdup("bola2.txt")); @@ -665,7 +666,7 @@ test_source_parse_from_files_filter_by_page_invalid2(void **state) "DATE: 2007-02-03 04:05:06\n" "--------\n" "bola")); - blogc_error_t *err = NULL; + bc_error_t *err = NULL; bc_slist_t *s = NULL; s = bc_slist_append(s, bc_strdup("bola1.txt")); s = bc_slist_append(s, bc_strdup("bola2.txt")); @@ -710,7 +711,7 @@ test_source_parse_from_files_without_all_dates(void **state) "DATE: 2003-02-03 04:05:06\n" "--------\n" "bola")); - blogc_error_t *err = NULL; + bc_error_t *err = NULL; bc_slist_t *s = NULL; s = bc_slist_append(s, bc_strdup("bola1.txt")); s = bc_slist_append(s, bc_strdup("bola2.txt")); @@ -733,7 +734,7 @@ test_source_parse_from_files_without_all_dates(void **state) static void test_source_parse_from_files_null(void **state) { - blogc_error_t *err = NULL; + bc_error_t *err = NULL; bc_slist_t *s = NULL; bc_trie_t *c = bc_trie_new(free); bc_slist_t *t = blogc_source_parse_from_files(c, s, &err); diff --git a/tests/blogc/check_renderer.c b/tests/blogc/check_renderer.c index fa50601..1ed5dea 100644 --- a/tests/blogc/check_renderer.c +++ b/tests/blogc/check_renderer.c @@ -13,11 +13,12 @@ #include #include #include -#include "../../src/blogc/error.h" +#include "../../src/common/error.h" +#include "../../src/common/utils.h" +#include "../../src/blogc/errors.h" #include "../../src/blogc/renderer.h" #include "../../src/blogc/source-parser.h" #include "../../src/blogc/template-parser.h" -#include "../../src/common/utils.h" static bc_slist_t* @@ -44,7 +45,7 @@ create_sources(unsigned int count) "ahahahahahahahaha3", }; assert_false(count > 3); - blogc_error_t *err = NULL; + bc_error_t *err = NULL; bc_slist_t *l = NULL; for (unsigned int i = 0; i < count; i++) { l = bc_slist_append(l, blogc_source_parse(s[i], strlen(s[i]), &err)); @@ -76,7 +77,7 @@ test_render_entry(void **state) "{% if GUDA <= \"zxc\" %}LOL4{% endif %}\n" "{% foreach TAGS %}lol {{ FOREACH_ITEM }} haha {% endforeach %}\n" "{% foreach TAGS_ASD %}yay{% endforeach %}\n"; - blogc_error_t *err = NULL; + bc_error_t *err = NULL; bc_slist_t *l = blogc_template_parse(str, strlen(str), &err); assert_non_null(l); assert_null(err); @@ -123,7 +124,7 @@ test_render_listing(void **state) "{% foreach TAGS %}lol {{ FOREACH_ITEM }} haha {% endforeach %}\n" "{% foreach TAGS_ASD %}yay{% endforeach %}\n" "{% endblock %}\n"; - blogc_error_t *err = NULL; + bc_error_t *err = NULL; bc_slist_t *l = blogc_template_parse(str, strlen(str), &err); assert_non_null(l); assert_null(err); @@ -171,7 +172,7 @@ test_render_listing_empty(void **state) "bola: {% ifdef BOLA %}{{ BOLA }}{% endif %}\n" "{% foreach TAGS %}lol {{ FOREACH_ITEM }} haha {% endforeach %}\n" "{% endblock %}\n"; - blogc_error_t *err = NULL; + bc_error_t *err = NULL; bc_slist_t *l = blogc_template_parse(str, strlen(str), &err); assert_non_null(l); assert_null(err); @@ -198,7 +199,7 @@ test_render_ifdef(void **state) "{% endif %}\n" "{% endif %}\n" "{% endblock %}\n"; - blogc_error_t *err = NULL; + bc_error_t *err = NULL; bc_slist_t *l = blogc_template_parse(str, strlen(str), &err); assert_non_null(l); assert_null(err); @@ -227,7 +228,7 @@ test_render_ifdef2(void **state) "{% endif %}\n" "{% endif %}\n" "{% endblock %}\n"; - blogc_error_t *err = NULL; + bc_error_t *err = NULL; bc_slist_t *l = blogc_template_parse(str, strlen(str), &err); assert_non_null(l); assert_null(err); @@ -258,7 +259,7 @@ test_render_ifdef3(void **state) "{% endif %}\n" "{% endif %}\n" "{% endblock %}\n"; - blogc_error_t *err = NULL; + bc_error_t *err = NULL; bc_slist_t *l = blogc_template_parse(str, strlen(str), &err); assert_non_null(l); assert_null(err); @@ -293,7 +294,7 @@ test_render_ifdef4(void **state) "{% else %}lol\n" "{% endif %}\n" "{% endblock %}\n"; - blogc_error_t *err = NULL; + bc_error_t *err = NULL; bc_slist_t *l = blogc_template_parse(str, strlen(str), &err); assert_non_null(l); assert_null(err); @@ -329,7 +330,7 @@ test_render_ifdef5(void **state) "{% else %}lol\n" "{% endif %}\n" "{% endblock %}\n"; - blogc_error_t *err = NULL; + bc_error_t *err = NULL; bc_slist_t *l = blogc_template_parse(str, strlen(str), &err); assert_non_null(l); assert_null(err); @@ -363,7 +364,7 @@ test_render_ifdef6(void **state) "{% else %}lol\n" "{% endif %}\n" "{% endblock %}\n"; - blogc_error_t *err = NULL; + bc_error_t *err = NULL; bc_slist_t *l = blogc_template_parse(str, strlen(str), &err); assert_non_null(l); assert_null(err); @@ -396,7 +397,7 @@ test_render_ifdef7(void **state) "{% endif %}\n" "{% endif %}\n" "{% endblock %}\n"; - blogc_error_t *err = NULL; + bc_error_t *err = NULL; bc_slist_t *l = blogc_template_parse(str, strlen(str), &err); assert_non_null(l); assert_null(err); @@ -432,7 +433,7 @@ test_render_ifndef(void **state) "{% endif %}\n" "{% endif %}\n" "{% endblock %}\n"; - blogc_error_t *err = NULL; + bc_error_t *err = NULL; bc_slist_t *l = blogc_template_parse(str, strlen(str), &err); assert_non_null(l); assert_null(err); @@ -468,7 +469,7 @@ test_render_if_eq(void **state) "{% endif %}\n" "{% endif %}\n" "{% endblock %}\n"; - blogc_error_t *err = NULL; + bc_error_t *err = NULL; bc_slist_t *l = blogc_template_parse(str, strlen(str), &err); assert_non_null(l); assert_null(err); @@ -505,7 +506,7 @@ test_render_if_neq(void **state) "{% endif %}\n" "{% endif %}\n" "{% endblock %}\n"; - blogc_error_t *err = NULL; + bc_error_t *err = NULL; bc_slist_t *l = blogc_template_parse(str, strlen(str), &err); assert_non_null(l); assert_null(err); @@ -542,7 +543,7 @@ test_render_if_lt(void **state) "{% endif %}\n" "{% endif %}\n" "{% endblock %}\n"; - blogc_error_t *err = NULL; + bc_error_t *err = NULL; bc_slist_t *l = blogc_template_parse(str, strlen(str), &err); assert_non_null(l); assert_null(err); @@ -579,7 +580,7 @@ test_render_if_gt(void **state) "{% endif %}\n" "{% endif %}\n" "{% endblock %}\n"; - blogc_error_t *err = NULL; + bc_error_t *err = NULL; bc_slist_t *l = blogc_template_parse(str, strlen(str), &err); assert_non_null(l); assert_null(err); @@ -618,7 +619,7 @@ test_render_if_lt_eq(void **state) "{% endif %}\n" "{% endif %}\n" "{% endblock %}\n"; - blogc_error_t *err = NULL; + bc_error_t *err = NULL; bc_slist_t *l = blogc_template_parse(str, strlen(str), &err); assert_non_null(l); assert_null(err); @@ -659,7 +660,7 @@ test_render_if_gt_eq(void **state) "{% endif %}\n" "{% endif %}\n" "{% endblock %}\n"; - blogc_error_t *err = NULL; + bc_error_t *err = NULL; bc_slist_t *l = blogc_template_parse(str, strlen(str), &err); assert_non_null(l); assert_null(err); @@ -691,7 +692,7 @@ test_render_foreach(void **state) "{% block entry %}\n" "{% foreach TAGS %} {{ FOREACH_ITEM }} {% endforeach %}\n" "{% endblock %}\n"; - blogc_error_t *err = NULL; + bc_error_t *err = NULL; bc_slist_t *l = blogc_template_parse(str, strlen(str), &err); assert_non_null(l); assert_null(err); @@ -716,7 +717,7 @@ test_render_foreach_if(void **state) "{% foreach TAGS %} {% if FOREACH_ITEM == \"bar\" %}{{ FOREACH_ITEM }}" "{% endif %} {% endforeach %}\n" "{% endblock %}\n"; - blogc_error_t *err = NULL; + bc_error_t *err = NULL; bc_slist_t *l = blogc_template_parse(str, strlen(str), &err); assert_non_null(l); assert_null(err); @@ -742,7 +743,7 @@ test_render_foreach_if_else(void **state) "{% else %}{{ FOREACH_ITEM }}" "{% endif %} {% endforeach %}\n" "{% endblock %}\n"; - blogc_error_t *err = NULL; + bc_error_t *err = NULL; bc_slist_t *l = blogc_template_parse(str, strlen(str), &err); assert_non_null(l); assert_null(err); @@ -773,7 +774,7 @@ test_render_outside_block(void **state) "{% ifdef GUDA %}bola{% endif %}\n" "{{ BOLA }}\n" "{% ifndef CHUNDA %}lol{% endif %}\n"; - blogc_error_t *err = NULL; + bc_error_t *err = NULL; bc_slist_t *l = blogc_template_parse(str, strlen(str), &err); assert_non_null(l); assert_null(err); @@ -806,7 +807,7 @@ test_render_prefer_local_variable(void **state) "{% endif %}\n" "{% endif %}\n" "{% endblock %}\n"; - blogc_error_t *err = NULL; + bc_error_t *err = NULL; bc_slist_t *l = blogc_template_parse(str, strlen(str), &err); assert_non_null(l); assert_null(err); @@ -842,7 +843,7 @@ test_render_respect_variable_scope(void **state) "{% ifdef LOL %}{{ LOL }}{% endif %}\n" "{% ifdef BOLA %}{{ BOLA }}{% endif %}\n" "{% endblock %}\n"; - blogc_error_t *err = NULL; + bc_error_t *err = NULL; bc_slist_t *l = blogc_template_parse(str, strlen(str), &err); assert_non_null(l); assert_null(err); @@ -874,7 +875,7 @@ test_render_ifcount_bug(void **state) "{% ifdef ASD %}ASD{% endif %}\n" "{% endif %}\n" "{% endblock %}\n"; - blogc_error_t *err = NULL; + bc_error_t *err = NULL; bc_slist_t *l = blogc_template_parse(str, strlen(str), &err); assert_non_null(l); assert_null(err); diff --git a/tests/blogc/check_source_parser.c b/tests/blogc/check_source_parser.c index 4606673..8753687 100644 --- a/tests/blogc/check_source_parser.c +++ b/tests/blogc/check_source_parser.c @@ -11,9 +11,10 @@ #include #include #include -#include "../../src/blogc/source-parser.h" -#include "../../src/blogc/error.h" +#include "../../src/common/error.h" #include "../../src/common/utils.h" +#include "../../src/blogc/errors.h" +#include "../../src/blogc/source-parser.h" static void @@ -26,7 +27,7 @@ test_source_parse(void **state) "# This is a test\n" "\n" "bola\n"; - blogc_error_t *err = NULL; + bc_error_t *err = NULL; bc_trie_t *source = blogc_source_parse(a, strlen(a), &err); assert_null(err); assert_non_null(source); @@ -58,7 +59,7 @@ test_source_parse_crlf(void **state) "# This is a test\r\n" "\r\n" "bola\r\n"; - blogc_error_t *err = NULL; + bc_error_t *err = NULL; bc_trie_t *source = blogc_source_parse(a, strlen(a), &err); assert_null(err); assert_non_null(source); @@ -92,7 +93,7 @@ test_source_parse_with_spaces(void **state) "# This is a test\n" "\n" "bola\n"; - blogc_error_t *err = NULL; + bc_error_t *err = NULL; bc_trie_t *source = blogc_source_parse(a, strlen(a), &err); assert_null(err); assert_non_null(source); @@ -129,7 +130,7 @@ test_source_parse_with_excerpt(void **state) "\n" "guda\n" "yay"; - blogc_error_t *err = NULL; + bc_error_t *err = NULL; bc_trie_t *source = blogc_source_parse(a, strlen(a), &err); assert_null(err); assert_non_null(source); @@ -169,7 +170,7 @@ test_source_parse_with_description(void **state) "# This is a test\n" "\n" "bola\n"; - blogc_error_t *err = NULL; + bc_error_t *err = NULL; bc_trie_t *source = blogc_source_parse(a, strlen(a), &err); assert_null(err); assert_non_null(source); @@ -195,13 +196,13 @@ static void test_source_parse_config_empty(void **state) { const char *a = ""; - blogc_error_t *err = NULL; + bc_error_t *err = NULL; bc_trie_t *source = blogc_source_parse(a, strlen(a), &err); assert_null(source); assert_non_null(err); assert_int_equal(err->type, BLOGC_ERROR_SOURCE_PARSER); assert_string_equal(err->msg, "Your source file is empty."); - blogc_error_free(err); + bc_error_free(err); bc_trie_free(source); } @@ -210,14 +211,14 @@ static void test_source_parse_config_invalid_key(void **state) { const char *a = "bola: guda"; - blogc_error_t *err = NULL; + bc_error_t *err = NULL; bc_trie_t *source = blogc_source_parse(a, strlen(a), &err); assert_non_null(err); assert_int_equal(err->type, BLOGC_ERROR_SOURCE_PARSER); assert_string_equal(err->msg, "Can't find a configuration key or the content separator.\n" "Error occurred near line 1, position 1: bola: guda"); - blogc_error_free(err); + bc_error_free(err); bc_trie_free(source); } @@ -226,14 +227,14 @@ static void test_source_parse_config_no_key(void **state) { const char *a = "BOLa"; - blogc_error_t *err = NULL; + bc_error_t *err = NULL; bc_trie_t *source = blogc_source_parse(a, strlen(a), &err); assert_non_null(err); assert_int_equal(err->type, BLOGC_ERROR_SOURCE_PARSER); assert_string_equal(err->msg, "Invalid configuration key.\n" "Error occurred near line 1, position 4: BOLa"); - blogc_error_free(err); + bc_error_free(err); bc_trie_free(source); } @@ -242,14 +243,14 @@ static void test_source_parse_config_no_key2(void **state) { const char *a = "BOLA"; - blogc_error_t *err = NULL; + bc_error_t *err = NULL; bc_trie_t *source = blogc_source_parse(a, strlen(a), &err); assert_non_null(err); assert_int_equal(err->type, BLOGC_ERROR_SOURCE_PARSER); assert_string_equal(err->msg, "Your last configuration key is missing ':' and the value\n" "Error occurred near line 1, position 5: BOLA"); - blogc_error_free(err); + bc_error_free(err); bc_trie_free(source); } @@ -258,7 +259,7 @@ static void test_source_parse_config_no_value(void **state) { const char *a = "BOLA:\r\n"; - blogc_error_t *err = NULL; + bc_error_t *err = NULL; bc_trie_t *source = blogc_source_parse(a, strlen(a), &err); assert_null(source); assert_non_null(err); @@ -266,7 +267,7 @@ test_source_parse_config_no_value(void **state) assert_string_equal(err->msg, "Configuration value not provided for 'BOLA'.\n" "Error occurred near line 1, position 6: BOLA:"); - blogc_error_free(err); + bc_error_free(err); bc_trie_free(source); } @@ -275,7 +276,7 @@ static void test_source_parse_config_no_value2(void **state) { const char *a = "BOLA:"; - blogc_error_t *err = NULL; + bc_error_t *err = NULL; bc_trie_t *source = blogc_source_parse(a, strlen(a), &err); assert_null(source); assert_non_null(err); @@ -283,7 +284,7 @@ test_source_parse_config_no_value2(void **state) assert_string_equal(err->msg, "Configuration value not provided for 'BOLA'.\n" "Error occurred near line 1, position 6: BOLA:"); - blogc_error_free(err); + bc_error_free(err); bc_trie_free(source); } @@ -292,7 +293,7 @@ static void test_source_parse_config_reserved_name(void **state) { const char *a = "FILENAME: asd\r\n"; - blogc_error_t *err = NULL; + bc_error_t *err = NULL; bc_trie_t *source = blogc_source_parse(a, strlen(a), &err); assert_null(source); assert_non_null(err); @@ -300,7 +301,7 @@ test_source_parse_config_reserved_name(void **state) assert_string_equal(err->msg, "'FILENAME' variable is forbidden in source files. It will be set " "for you by the compiler."); - blogc_error_free(err); + bc_error_free(err); bc_trie_free(source); } @@ -309,7 +310,7 @@ static void test_source_parse_config_reserved_name2(void **state) { const char *a = "CONTENT: asd\r\n"; - blogc_error_t *err = NULL; + bc_error_t *err = NULL; bc_trie_t *source = blogc_source_parse(a, strlen(a), &err); assert_null(source); assert_non_null(err); @@ -317,7 +318,7 @@ test_source_parse_config_reserved_name2(void **state) assert_string_equal(err->msg, "'CONTENT' variable is forbidden in source files. It will be set " "for you by the compiler."); - blogc_error_free(err); + bc_error_free(err); bc_trie_free(source); } @@ -326,7 +327,7 @@ static void test_source_parse_config_reserved_name3(void **state) { const char *a = "DATE_FORMATTED: asd\r\n"; - blogc_error_t *err = NULL; + bc_error_t *err = NULL; bc_trie_t *source = blogc_source_parse(a, strlen(a), &err); assert_null(source); assert_non_null(err); @@ -334,7 +335,7 @@ test_source_parse_config_reserved_name3(void **state) assert_string_equal(err->msg, "'DATE_FORMATTED' variable is forbidden in source files. It will be set " "for you by the compiler."); - blogc_error_free(err); + bc_error_free(err); bc_trie_free(source); } @@ -343,7 +344,7 @@ static void test_source_parse_config_reserved_name4(void **state) { const char *a = "DATE_FIRST_FORMATTED: asd\r\n"; - blogc_error_t *err = NULL; + bc_error_t *err = NULL; bc_trie_t *source = blogc_source_parse(a, strlen(a), &err); assert_null(source); assert_non_null(err); @@ -351,7 +352,7 @@ test_source_parse_config_reserved_name4(void **state) assert_string_equal(err->msg, "'DATE_FIRST_FORMATTED' variable is forbidden in source files. It will be set " "for you by the compiler."); - blogc_error_free(err); + bc_error_free(err); bc_trie_free(source); } @@ -360,7 +361,7 @@ static void test_source_parse_config_reserved_name5(void **state) { const char *a = "DATE_LAST_FORMATTED: asd\r\n"; - blogc_error_t *err = NULL; + bc_error_t *err = NULL; bc_trie_t *source = blogc_source_parse(a, strlen(a), &err); assert_null(source); assert_non_null(err); @@ -368,7 +369,7 @@ test_source_parse_config_reserved_name5(void **state) assert_string_equal(err->msg, "'DATE_LAST_FORMATTED' variable is forbidden in source files. It will be set " "for you by the compiler."); - blogc_error_free(err); + bc_error_free(err); bc_trie_free(source); } @@ -377,7 +378,7 @@ static void test_source_parse_config_reserved_name6(void **state) { const char *a = "PAGE_FIRST: asd\r\n"; - blogc_error_t *err = NULL; + bc_error_t *err = NULL; bc_trie_t *source = blogc_source_parse(a, strlen(a), &err); assert_null(source); assert_non_null(err); @@ -385,7 +386,7 @@ test_source_parse_config_reserved_name6(void **state) assert_string_equal(err->msg, "'PAGE_FIRST' variable is forbidden in source files. It will be set " "for you by the compiler."); - blogc_error_free(err); + bc_error_free(err); bc_trie_free(source); } @@ -394,7 +395,7 @@ static void test_source_parse_config_reserved_name7(void **state) { const char *a = "PAGE_PREVIOUS: asd\r\n"; - blogc_error_t *err = NULL; + bc_error_t *err = NULL; bc_trie_t *source = blogc_source_parse(a, strlen(a), &err); assert_null(source); assert_non_null(err); @@ -402,7 +403,7 @@ test_source_parse_config_reserved_name7(void **state) assert_string_equal(err->msg, "'PAGE_PREVIOUS' variable is forbidden in source files. It will be set " "for you by the compiler."); - blogc_error_free(err); + bc_error_free(err); bc_trie_free(source); } @@ -411,7 +412,7 @@ static void test_source_parse_config_reserved_name8(void **state) { const char *a = "PAGE_CURRENT: asd\r\n"; - blogc_error_t *err = NULL; + bc_error_t *err = NULL; bc_trie_t *source = blogc_source_parse(a, strlen(a), &err); assert_null(source); assert_non_null(err); @@ -419,7 +420,7 @@ test_source_parse_config_reserved_name8(void **state) assert_string_equal(err->msg, "'PAGE_CURRENT' variable is forbidden in source files. It will be set " "for you by the compiler."); - blogc_error_free(err); + bc_error_free(err); bc_trie_free(source); } @@ -428,7 +429,7 @@ static void test_source_parse_config_reserved_name9(void **state) { const char *a = "PAGE_NEXT: asd\r\n"; - blogc_error_t *err = NULL; + bc_error_t *err = NULL; bc_trie_t *source = blogc_source_parse(a, strlen(a), &err); assert_null(source); assert_non_null(err); @@ -436,7 +437,7 @@ test_source_parse_config_reserved_name9(void **state) assert_string_equal(err->msg, "'PAGE_NEXT' variable is forbidden in source files. It will be set " "for you by the compiler."); - blogc_error_free(err); + bc_error_free(err); bc_trie_free(source); } @@ -445,7 +446,7 @@ static void test_source_parse_config_reserved_name10(void **state) { const char *a = "PAGE_LAST: asd\r\n"; - blogc_error_t *err = NULL; + bc_error_t *err = NULL; bc_trie_t *source = blogc_source_parse(a, strlen(a), &err); assert_null(source); assert_non_null(err); @@ -453,7 +454,7 @@ test_source_parse_config_reserved_name10(void **state) assert_string_equal(err->msg, "'PAGE_LAST' variable is forbidden in source files. It will be set " "for you by the compiler."); - blogc_error_free(err); + bc_error_free(err); bc_trie_free(source); } @@ -462,7 +463,7 @@ static void test_source_parse_config_reserved_name11(void **state) { const char *a = "BLOGC_VERSION: 1.0\r\n"; - blogc_error_t *err = NULL; + bc_error_t *err = NULL; bc_trie_t *source = blogc_source_parse(a, strlen(a), &err); assert_null(source); assert_non_null(err); @@ -470,7 +471,7 @@ test_source_parse_config_reserved_name11(void **state) assert_string_equal(err->msg, "'BLOGC_VERSION' variable is forbidden in source files. It will be set " "for you by the compiler."); - blogc_error_free(err); + bc_error_free(err); bc_trie_free(source); } @@ -479,7 +480,7 @@ static void test_source_parse_config_value_no_line_ending(void **state) { const char *a = "BOLA: asd"; - blogc_error_t *err = NULL; + bc_error_t *err = NULL; bc_trie_t *source = blogc_source_parse(a, strlen(a), &err); assert_null(source); assert_non_null(err); @@ -487,7 +488,7 @@ test_source_parse_config_value_no_line_ending(void **state) assert_string_equal(err->msg, "No line ending after the configuration value for 'BOLA'.\n" "Error occurred near line 1, position 10: BOLA: asd"); - blogc_error_free(err); + bc_error_free(err); bc_trie_free(source); } @@ -496,7 +497,7 @@ static void test_source_parse_invalid_separator(void **state) { const char *a = "BOLA: asd\n---#"; - blogc_error_t *err = NULL; + bc_error_t *err = NULL; bc_trie_t *source = blogc_source_parse(a, strlen(a), &err); assert_null(source); assert_non_null(err); @@ -504,7 +505,7 @@ test_source_parse_invalid_separator(void **state) assert_string_equal(err->msg, "Invalid content separator. Must be more than one '-' characters.\n" "Error occurred near line 2, position 4: ---#"); - blogc_error_free(err); + bc_error_free(err); bc_trie_free(source); } diff --git a/tests/blogc/check_template_parser.c b/tests/blogc/check_template_parser.c index b4c978f..c520e94 100644 --- a/tests/blogc/check_template_parser.c +++ b/tests/blogc/check_template_parser.c @@ -11,9 +11,10 @@ #include #include #include -#include "../../src/blogc/template-parser.h" -#include "../../src/blogc/error.h" +#include "../../src/common/error.h" #include "../../src/common/utils.h" +#include "../../src/blogc/errors.h" +#include "../../src/blogc/template-parser.h" static void @@ -59,7 +60,7 @@ test_template_parse(void **state) "{% block listing_once %}asd{% endblock %}\n" "{%- foreach BOLA %}hahaha{% endforeach %}\n" "{% if BOLA == \"1\\\"0\" %}aee{% else %}fffuuuuuuu{% endif %}"; - blogc_error_t *err = NULL; + bc_error_t *err = NULL; bc_slist_t *stmts = blogc_template_parse(a, strlen(a), &err); assert_null(err); assert_non_null(stmts); @@ -143,7 +144,7 @@ test_template_parse_crlf(void **state) "{% block listing_once %}asd{% endblock %}\r\n" "{%- foreach BOLA %}hahaha{% endforeach %}\r\n" "{% if BOLA == \"1\\\"0\" %}aee{% else %}fffuuuuuuu{% endif %}"; - blogc_error_t *err = NULL; + bc_error_t *err = NULL; bc_slist_t *stmts = blogc_template_parse(a, strlen(a), &err); assert_null(err); assert_non_null(stmts); @@ -235,7 +236,7 @@ test_template_parse_html(void **state) " {% block listing_once %}{% endblock %}\n" " \n" "\n"; - blogc_error_t *err = NULL; + bc_error_t *err = NULL; bc_slist_t *stmts = blogc_template_parse(a, strlen(a), &err); assert_null(err); assert_non_null(stmts); @@ -345,7 +346,7 @@ test_template_parse_ifdef_and_var_outside_block(void **state) "{% ifdef GUDA %}bola{% endif %}\n" "{{ BOLA }}\n" "{% ifndef CHUNDA %}{{ CHUNDA }}{% endif %}\n"; - blogc_error_t *err = NULL; + bc_error_t *err = NULL; bc_slist_t *stmts = blogc_template_parse(a, strlen(a), &err); assert_null(err); assert_non_null(stmts); @@ -392,7 +393,7 @@ test_template_parse_nested_else(void **state) "bnm\n" "{% endif %}\n" "{% endif %}\n"; - blogc_error_t *err = NULL; + bc_error_t *err = NULL; bc_slist_t *stmts = blogc_template_parse(a, strlen(a), &err); assert_null(err); assert_non_null(stmts); @@ -444,7 +445,7 @@ static void test_template_parse_invalid_block_start(void **state) { const char *a = "{% ASD %}\n"; - blogc_error_t *err = NULL; + bc_error_t *err = NULL; bc_slist_t *stmts = blogc_template_parse(a, strlen(a), &err); assert_non_null(err); assert_null(stmts); @@ -452,7 +453,7 @@ test_template_parse_invalid_block_start(void **state) assert_string_equal(err->msg, "Invalid statement syntax. Must begin with lowercase letter.\n" "Error occurred near line 1, position 4: {% ASD %}"); - blogc_error_free(err); + bc_error_free(err); a = "{%-- block entry %}\n"; err = NULL; stmts = blogc_template_parse(a, strlen(a), &err); @@ -462,7 +463,7 @@ test_template_parse_invalid_block_start(void **state) assert_string_equal(err->msg, "Invalid statement syntax. Duplicated whitespace cleaner before statement.\n" "Error occurred near line 1, position 4: {%-- block entry %}"); - blogc_error_free(err); + bc_error_free(err); a = "{% block entry --%}\n"; err = NULL; stmts = blogc_template_parse(a, strlen(a), &err); @@ -472,7 +473,7 @@ test_template_parse_invalid_block_start(void **state) assert_string_equal(err->msg, "Invalid statement syntax. Duplicated whitespace cleaner after statement.\n" "Error occurred near line 1, position 17: {% block entry --%}"); - blogc_error_free(err); + bc_error_free(err); } @@ -482,7 +483,7 @@ test_template_parse_invalid_block_nested(void **state) const char *a = "{% block entry %}\n" "{% block listing %}\n"; - blogc_error_t *err = NULL; + bc_error_t *err = NULL; bc_slist_t *stmts = blogc_template_parse(a, strlen(a), &err); assert_non_null(err); assert_null(stmts); @@ -490,7 +491,7 @@ test_template_parse_invalid_block_nested(void **state) assert_string_equal(err->msg, "Blocks can't be nested.\n" "Error occurred near line 2, position 9: {% block listing %}"); - blogc_error_free(err); + bc_error_free(err); } @@ -500,7 +501,7 @@ test_template_parse_invalid_foreach_nested(void **state) const char *a = "{% foreach A %}\n" "{% foreach B %}\n"; - blogc_error_t *err = NULL; + bc_error_t *err = NULL; bc_slist_t *stmts = blogc_template_parse(a, strlen(a), &err); assert_non_null(err); assert_null(stmts); @@ -508,7 +509,7 @@ test_template_parse_invalid_foreach_nested(void **state) assert_string_equal(err->msg, "'foreach' statements can't be nested.\n" "Error occurred near line 2, position 11: {% foreach B %}"); - blogc_error_free(err); + bc_error_free(err); } @@ -516,7 +517,7 @@ static void test_template_parse_invalid_block_not_open(void **state) { const char *a = "{% endblock %}\n"; - blogc_error_t *err = NULL; + bc_error_t *err = NULL; bc_slist_t *stmts = blogc_template_parse(a, strlen(a), &err); assert_non_null(err); assert_null(stmts); @@ -524,7 +525,7 @@ test_template_parse_invalid_block_not_open(void **state) assert_string_equal(err->msg, "'endblock' statement without an open 'block' statement.\n" "Error occurred near line 1, position 12: {% endblock %}"); - blogc_error_free(err); + bc_error_free(err); } @@ -532,7 +533,7 @@ static void test_template_parse_invalid_endif_not_open(void **state) { const char *a = "{% block listing %}{% endif %}{% endblock %}\n"; - blogc_error_t *err = NULL; + bc_error_t *err = NULL; bc_slist_t *stmts = blogc_template_parse(a, strlen(a), &err); assert_non_null(err); assert_null(stmts); @@ -541,7 +542,7 @@ test_template_parse_invalid_endif_not_open(void **state) "'endif' statement without an open 'if', 'ifdef' or 'ifndef' statement.\n" "Error occurred near line 1, position 28: " "{% block listing %}{% endif %}{% endblock %}"); - blogc_error_free(err); + bc_error_free(err); } @@ -549,7 +550,7 @@ static void test_template_parse_invalid_endif_not_open_inside_block(void **state) { const char *a = "{% ifdef BOLA %}{% block listing %}{% endif %}{% endblock %}"; - blogc_error_t *err = NULL; + bc_error_t *err = NULL; bc_slist_t *stmts = blogc_template_parse(a, strlen(a), &err); assert_non_null(err); assert_null(stmts); @@ -558,7 +559,7 @@ test_template_parse_invalid_endif_not_open_inside_block(void **state) "'endif' statement without an open 'if', 'ifdef' or 'ifndef' statement.\n" "Error occurred near line 1, position 44: {% ifdef BOLA %}{% block " "listing %}{% endif %}{% endblock %}"); - blogc_error_free(err); + bc_error_free(err); } @@ -566,7 +567,7 @@ static void test_template_parse_invalid_else_not_open_inside_block(void **state) { const char *a = "{% ifdef BOLA %}{% block listing %}{% else %}{% endif %}{% endblock %}"; - blogc_error_t *err = NULL; + bc_error_t *err = NULL; bc_slist_t *stmts = blogc_template_parse(a, strlen(a), &err); assert_non_null(err); assert_null(stmts); @@ -575,7 +576,7 @@ test_template_parse_invalid_else_not_open_inside_block(void **state) "'else' statement without an open 'if', 'ifdef' or 'ifndef' statement.\n" "Error occurred near line 1, position 43: {% ifdef BOLA %}" "{% block listing %}{% else %}{% endif %}{% endblock %}"); - blogc_error_free(err); + bc_error_free(err); } @@ -583,7 +584,7 @@ static void test_template_parse_invalid_endforeach_not_open(void **state) { const char *a = "{% endforeach %}\n"; - blogc_error_t *err = NULL; + bc_error_t *err = NULL; bc_slist_t *stmts = blogc_template_parse(a, strlen(a), &err); assert_non_null(err); assert_null(stmts); @@ -591,7 +592,7 @@ test_template_parse_invalid_endforeach_not_open(void **state) assert_string_equal(err->msg, "'endforeach' statement without an open 'foreach' statement.\n" "Error occurred near line 1, position 14: {% endforeach %}"); - blogc_error_free(err); + bc_error_free(err); } @@ -600,7 +601,7 @@ test_template_parse_invalid_endforeach_not_open_inside_block(void **state) { const char *a = "{% foreach TAGS %}{% block entry %}{% endforeach %}" "{% endblock %}\n"; - blogc_error_t *err = NULL; + bc_error_t *err = NULL; bc_slist_t *stmts = blogc_template_parse(a, strlen(a), &err); assert_non_null(err); assert_null(stmts); @@ -609,7 +610,7 @@ test_template_parse_invalid_endforeach_not_open_inside_block(void **state) "'endforeach' statement without an open 'foreach' statement.\n" "Error occurred near line 1, position 49: {% foreach TAGS %}" "{% block entry %}{% endforeach %}{% endblock %}"); - blogc_error_free(err); + bc_error_free(err); } @@ -618,7 +619,7 @@ test_template_parse_invalid_endforeach_not_open_inside_block2(void **state) { const char *a = "{% block entry %}{% foreach TAGS %}" "{% endforeach %}{% endforeach %}{% endblock %}\n"; - blogc_error_t *err = NULL; + bc_error_t *err = NULL; bc_slist_t *stmts = blogc_template_parse(a, strlen(a), &err); assert_non_null(err); assert_null(stmts); @@ -627,7 +628,7 @@ test_template_parse_invalid_endforeach_not_open_inside_block2(void **state) "'endforeach' statement without an open 'foreach' statement.\n" "Error occurred near line 1, position 65: {% block entry %}" "{% foreach TAGS %}{% endforeach %}{% endforeach %}{% endblock %}"); - blogc_error_free(err); + bc_error_free(err); } @@ -636,14 +637,14 @@ test_template_parse_invalid_endforeach_not_closed_inside_block(void **state) { const char *a = "{% block entry %}{% foreach TAGS %}{% endblock %}" "{% endforeach %}\n"; - blogc_error_t *err = NULL; + bc_error_t *err = NULL; bc_slist_t *stmts = blogc_template_parse(a, strlen(a), &err); assert_non_null(err); assert_null(stmts); assert_int_equal(err->type, BLOGC_ERROR_TEMPLATE_PARSER); assert_string_equal(err->msg, "An open 'foreach' statement was not closed inside a 'entry' block!"); - blogc_error_free(err); + bc_error_free(err); } @@ -652,14 +653,14 @@ test_template_parse_invalid_endforeach_not_closed_inside_block2(void **state) { const char *a = "{% block entry %}{% foreach TAGS %}{% endforeach %}" "{% foreach TAGS %}{% endblock %}{% endforeach %}\n"; - blogc_error_t *err = NULL; + bc_error_t *err = NULL; bc_slist_t *stmts = blogc_template_parse(a, strlen(a), &err); assert_non_null(err); assert_null(stmts); assert_int_equal(err->type, BLOGC_ERROR_TEMPLATE_PARSER); assert_string_equal(err->msg, "An open 'foreach' statement was not closed inside a 'entry' block!"); - blogc_error_free(err); + bc_error_free(err); } @@ -667,7 +668,7 @@ static void test_template_parse_invalid_block_name(void **state) { const char *a = "{% chunda %}\n"; - blogc_error_t *err = NULL; + bc_error_t *err = NULL; bc_slist_t *stmts = blogc_template_parse(a, strlen(a), &err); assert_non_null(err); assert_null(stmts); @@ -676,7 +677,7 @@ test_template_parse_invalid_block_name(void **state) "Invalid statement type: Allowed types are: 'block', 'endblock', 'if', " "'ifdef', 'ifndef', 'else', 'endif', 'foreach' and 'endforeach'.\n" "Error occurred near line 1, position 10: {% chunda %}"); - blogc_error_free(err); + bc_error_free(err); } @@ -684,7 +685,7 @@ static void test_template_parse_invalid_block_type_start(void **state) { const char *a = "{% block ENTRY %}\n"; - blogc_error_t *err = NULL; + bc_error_t *err = NULL; bc_slist_t *stmts = blogc_template_parse(a, strlen(a), &err); assert_non_null(err); assert_null(stmts); @@ -692,7 +693,7 @@ test_template_parse_invalid_block_type_start(void **state) assert_string_equal(err->msg, "Invalid block syntax. Must begin with lowercase letter.\n" "Error occurred near line 1, position 10: {% block ENTRY %}"); - blogc_error_free(err); + bc_error_free(err); } @@ -700,7 +701,7 @@ static void test_template_parse_invalid_block_type(void **state) { const char *a = "{% block chunda %}\n"; - blogc_error_t *err = NULL; + bc_error_t *err = NULL; bc_slist_t *stmts = blogc_template_parse(a, strlen(a), &err); assert_non_null(err); assert_null(stmts); @@ -708,7 +709,7 @@ test_template_parse_invalid_block_type(void **state) assert_string_equal(err->msg, "Invalid block type. Allowed types are: 'entry', 'listing' and 'listing_once'.\n" "Error occurred near line 1, position 16: {% block chunda %}"); - blogc_error_free(err); + bc_error_free(err); } @@ -716,7 +717,7 @@ static void test_template_parse_invalid_ifdef_start(void **state) { const char *a = "{% block entry %}{% ifdef guda %}\n"; - blogc_error_t *err = NULL; + bc_error_t *err = NULL; bc_slist_t *stmts = blogc_template_parse(a, strlen(a), &err); assert_non_null(err); assert_null(stmts); @@ -725,7 +726,7 @@ test_template_parse_invalid_ifdef_start(void **state) "Invalid variable name. Must begin with uppercase letter.\n" "Error occurred near line 1, position 27: " "{% block entry %}{% ifdef guda %}"); - blogc_error_free(err); + bc_error_free(err); } @@ -733,7 +734,7 @@ static void test_template_parse_invalid_foreach_start(void **state) { const char *a = "{% block entry %}{% foreach guda %}\n"; - blogc_error_t *err = NULL; + bc_error_t *err = NULL; bc_slist_t *stmts = blogc_template_parse(a, strlen(a), &err); assert_non_null(err); assert_null(stmts); @@ -742,7 +743,7 @@ test_template_parse_invalid_foreach_start(void **state) "Invalid foreach variable name. Must begin with uppercase letter.\n" "Error occurred near line 1, position 29: " "{% block entry %}{% foreach guda %}"); - blogc_error_free(err); + bc_error_free(err); } @@ -750,7 +751,7 @@ static void test_template_parse_invalid_ifdef_variable(void **state) { const char *a = "{% block entry %}{% ifdef BoLA %}\n"; - blogc_error_t *err = NULL; + bc_error_t *err = NULL; bc_slist_t *stmts = blogc_template_parse(a, strlen(a), &err); assert_non_null(err); assert_null(stmts); @@ -759,7 +760,7 @@ test_template_parse_invalid_ifdef_variable(void **state) "Invalid variable name. Must be uppercase letter, number or '_'.\n" "Error occurred near line 1, position 28: " "{% block entry %}{% ifdef BoLA %}"); - blogc_error_free(err); + bc_error_free(err); } @@ -767,7 +768,7 @@ static void test_template_parse_invalid_ifdef_variable2(void **state) { const char *a = "{% block entry %}{% ifdef 0123 %}\n"; - blogc_error_t *err = NULL; + bc_error_t *err = NULL; bc_slist_t *stmts = blogc_template_parse(a, strlen(a), &err); assert_non_null(err); assert_null(stmts); @@ -776,7 +777,7 @@ test_template_parse_invalid_ifdef_variable2(void **state) "Invalid variable name. Must begin with uppercase letter.\n" "Error occurred near line 1, position 27: " "{% block entry %}{% ifdef 0123 %}"); - blogc_error_free(err); + bc_error_free(err); } @@ -784,7 +785,7 @@ static void test_template_parse_invalid_foreach_variable(void **state) { const char *a = "{% block entry %}{% foreach BoLA %}\n"; - blogc_error_t *err = NULL; + bc_error_t *err = NULL; bc_slist_t *stmts = blogc_template_parse(a, strlen(a), &err); assert_non_null(err); assert_null(stmts); @@ -793,7 +794,7 @@ test_template_parse_invalid_foreach_variable(void **state) "Invalid foreach variable name. Must be uppercase letter, number or '_'.\n" "Error occurred near line 1, position 30: " "{% block entry %}{% foreach BoLA %}"); - blogc_error_free(err); + bc_error_free(err); } @@ -801,7 +802,7 @@ static void test_template_parse_invalid_foreach_variable2(void **state) { const char *a = "{% block entry %}{% foreach 0123 %}\n"; - blogc_error_t *err = NULL; + bc_error_t *err = NULL; bc_slist_t *stmts = blogc_template_parse(a, strlen(a), &err); assert_non_null(err); assert_null(stmts); @@ -810,7 +811,7 @@ test_template_parse_invalid_foreach_variable2(void **state) "Invalid foreach variable name. Must begin with uppercase letter.\n" "Error occurred near line 1, position 29: {% block entry %}" "{% foreach 0123 %}"); - blogc_error_free(err); + bc_error_free(err); } @@ -818,7 +819,7 @@ static void test_template_parse_invalid_if_operator(void **state) { const char *a = "{% block entry %}{% if BOLA = \"asd\" %}\n"; - blogc_error_t *err = NULL; + bc_error_t *err = NULL; bc_slist_t *stmts = blogc_template_parse(a, strlen(a), &err); assert_non_null(err); assert_null(stmts); @@ -827,7 +828,7 @@ test_template_parse_invalid_if_operator(void **state) "Invalid 'if' operator. Must be '<', '>', '<=', '>=', '==' or '!='.\n" "Error occurred near line 1, position 29: " "{% block entry %}{% if BOLA = \"asd\" %}"); - blogc_error_free(err); + bc_error_free(err); } @@ -835,7 +836,7 @@ static void test_template_parse_invalid_if_operand(void **state) { const char *a = "{% block entry %}{% if BOLA == asd %}\n"; - blogc_error_t *err = NULL; + bc_error_t *err = NULL; bc_slist_t *stmts = blogc_template_parse(a, strlen(a), &err); assert_non_null(err); assert_null(stmts); @@ -844,7 +845,7 @@ test_template_parse_invalid_if_operand(void **state) "Invalid 'if' operand. Must be double-quoted static string or variable.\n" "Error occurred near line 1, position 32: " "{% block entry %}{% if BOLA == asd %}"); - blogc_error_free(err); + bc_error_free(err); } @@ -852,7 +853,7 @@ static void test_template_parse_invalid_if_operand2(void **state) { const char *a = "{% block entry %}{% if BOLA == \"asd %}\n"; - blogc_error_t *err = NULL; + bc_error_t *err = NULL; bc_slist_t *stmts = blogc_template_parse(a, strlen(a), &err); assert_non_null(err); assert_null(stmts); @@ -861,7 +862,7 @@ test_template_parse_invalid_if_operand2(void **state) "Found an open double-quoted string.\n" "Error occurred near line 1, position 32: " "{% block entry %}{% if BOLA == \"asd %}"); - blogc_error_free(err); + bc_error_free(err); } @@ -869,7 +870,7 @@ static void test_template_parse_invalid_if_operand3(void **state) { const char *a = "{% block entry %}{% if BOLA == 0123 %}\n"; - blogc_error_t *err = NULL; + bc_error_t *err = NULL; bc_slist_t *stmts = blogc_template_parse(a, strlen(a), &err); assert_non_null(err); assert_null(stmts); @@ -878,7 +879,7 @@ test_template_parse_invalid_if_operand3(void **state) "Invalid 'if' operand. Must be double-quoted static string or variable.\n" "Error occurred near line 1, position 32: " "{% block entry %}{% if BOLA == 0123 %}"); - blogc_error_free(err); + bc_error_free(err); } @@ -886,7 +887,7 @@ static void test_template_parse_invalid_else1(void **state) { const char *a = "{% else %}\n"; - blogc_error_t *err = NULL; + bc_error_t *err = NULL; bc_slist_t *stmts = blogc_template_parse(a, strlen(a), &err); assert_non_null(err); assert_null(stmts); @@ -894,7 +895,7 @@ test_template_parse_invalid_else1(void **state) assert_string_equal(err->msg, "'else' statement without an open 'if', 'ifdef' or 'ifndef' statement.\n" "Error occurred near line 1, position 8: {% else %}"); - blogc_error_free(err); + bc_error_free(err); } @@ -902,7 +903,7 @@ static void test_template_parse_invalid_else2(void **state) { const char *a = "{% if BOLA == \"123\" %}{% if GUDA == \"1\" %}{% else %}{% else %}\n"; - blogc_error_t *err = NULL; + bc_error_t *err = NULL; bc_slist_t *stmts = blogc_template_parse(a, strlen(a), &err); assert_non_null(err); assert_null(stmts); @@ -911,7 +912,7 @@ test_template_parse_invalid_else2(void **state) "More than one 'else' statement for an open 'if', 'ifdef' or 'ifndef' " "statement.\nError occurred near line 1, position 60: {% if BOLA == \"123\" " "%}{% if GUDA == \"1\" %}{% else %}{% else %}"); - blogc_error_free(err); + bc_error_free(err); } @@ -926,7 +927,7 @@ test_template_parse_invalid_else3(void **state) "{% endif %}\n" "{% else %}\n" "{% else %}\n"; - blogc_error_t *err = NULL; + bc_error_t *err = NULL; bc_slist_t *stmts = blogc_template_parse(a, strlen(a), &err); assert_non_null(err); assert_null(stmts); @@ -934,7 +935,7 @@ test_template_parse_invalid_else3(void **state) assert_string_equal(err->msg, "More than one 'else' statement for an open 'if', 'ifdef' or 'ifndef' " "statement.\nError occurred near line 7, position 8: {% else %}"); - blogc_error_free(err); + bc_error_free(err); } @@ -942,7 +943,7 @@ static void test_template_parse_invalid_block_end(void **state) { const char *a = "{% block entry }}\n"; - blogc_error_t *err = NULL; + bc_error_t *err = NULL; bc_slist_t *stmts = blogc_template_parse(a, strlen(a), &err); assert_non_null(err); assert_null(stmts); @@ -950,7 +951,7 @@ test_template_parse_invalid_block_end(void **state) assert_string_equal(err->msg, "Invalid statement syntax. Must end with '%}'.\n" "Error occurred near line 1, position 16: {% block entry }}"); - blogc_error_free(err); + bc_error_free(err); } @@ -958,7 +959,7 @@ static void test_template_parse_invalid_variable_name(void **state) { const char *a = "{% block entry %}{{ bola }}{% endblock %}\n"; - blogc_error_t *err = NULL; + bc_error_t *err = NULL; bc_slist_t *stmts = blogc_template_parse(a, strlen(a), &err); assert_non_null(err); assert_null(stmts); @@ -967,7 +968,7 @@ test_template_parse_invalid_variable_name(void **state) "Invalid variable name. Must begin with uppercase letter.\n" "Error occurred near line 1, position 21: " "{% block entry %}{{ bola }}{% endblock %}"); - blogc_error_free(err); + bc_error_free(err); } @@ -975,7 +976,7 @@ static void test_template_parse_invalid_variable_name2(void **state) { const char *a = "{% block entry %}{{ Bola }}{% endblock %}\n"; - blogc_error_t *err = NULL; + bc_error_t *err = NULL; bc_slist_t *stmts = blogc_template_parse(a, strlen(a), &err); assert_non_null(err); assert_null(stmts); @@ -984,7 +985,7 @@ test_template_parse_invalid_variable_name2(void **state) "Invalid variable name. Must be uppercase letter, number or '_'.\n" "Error occurred near line 1, position 22: " "{% block entry %}{{ Bola }}{% endblock %}"); - blogc_error_free(err); + bc_error_free(err); } @@ -992,7 +993,7 @@ static void test_template_parse_invalid_variable_name3(void **state) { const char *a = "{% block entry %}{{ 0123 }}{% endblock %}\n"; - blogc_error_t *err = NULL; + bc_error_t *err = NULL; bc_slist_t *stmts = blogc_template_parse(a, strlen(a), &err); assert_non_null(err); assert_null(stmts); @@ -1001,7 +1002,7 @@ test_template_parse_invalid_variable_name3(void **state) "Invalid variable name. Must begin with uppercase letter.\n" "Error occurred near line 1, position 21: {% block entry %}{{ 0123 }}" "{% endblock %}"); - blogc_error_free(err); + bc_error_free(err); } @@ -1009,7 +1010,7 @@ static void test_template_parse_invalid_variable_end(void **state) { const char *a = "{% block entry %}{{ BOLA %}{% endblock %}\n"; - blogc_error_t *err = NULL; + bc_error_t *err = NULL; bc_slist_t *stmts = blogc_template_parse(a, strlen(a), &err); assert_non_null(err); assert_null(stmts); @@ -1018,7 +1019,7 @@ test_template_parse_invalid_variable_end(void **state) "Invalid statement syntax. Must end with '}}'.\n" "Error occurred near line 1, position 26: " "{% block entry %}{{ BOLA %}{% endblock %}"); - blogc_error_free(err); + bc_error_free(err); } @@ -1026,7 +1027,7 @@ static void test_template_parse_invalid_close(void **state) { const char *a = "{% block entry %%\n"; - blogc_error_t *err = NULL; + bc_error_t *err = NULL; bc_slist_t *stmts = blogc_template_parse(a, strlen(a), &err); assert_non_null(err); assert_null(stmts); @@ -1034,7 +1035,7 @@ test_template_parse_invalid_close(void **state) assert_string_equal(err->msg, "Invalid statement syntax. Must end with '}'.\n" "Error occurred near line 1, position 17: {% block entry %%"); - blogc_error_free(err); + bc_error_free(err); } @@ -1042,7 +1043,7 @@ static void test_template_parse_invalid_close2(void **state) { const char *a = "{% block entry %}{{ BOLA }%{% endblock %}\n"; - blogc_error_t *err = NULL; + bc_error_t *err = NULL; bc_slist_t *stmts = blogc_template_parse(a, strlen(a), &err); assert_non_null(err); assert_null(stmts); @@ -1051,7 +1052,7 @@ test_template_parse_invalid_close2(void **state) "Invalid statement syntax. Must end with '}'.\n" "Error occurred near line 1, position 27: " "{% block entry %}{{ BOLA }%{% endblock %}"); - blogc_error_free(err); + bc_error_free(err); } @@ -1059,14 +1060,14 @@ static void test_template_parse_invalid_endif_not_closed(void **state) { const char *a = "{% block entry %}{% endblock %}{% ifdef BOLA %}\n"; - blogc_error_t *err = NULL; + bc_error_t *err = NULL; bc_slist_t *stmts = blogc_template_parse(a, strlen(a), &err); assert_non_null(err); assert_null(stmts); assert_int_equal(err->type, BLOGC_ERROR_TEMPLATE_PARSER); assert_string_equal(err->msg, "1 open 'if', 'ifdef' and/or 'ifndef' statements " "were not closed!"); - blogc_error_free(err); + bc_error_free(err); } @@ -1074,7 +1075,7 @@ static void test_template_parse_invalid_endif_not_closed_inside_block(void **state) { const char *a = "{% block listing %}{% ifdef BOLA %}{% endblock %}{% endif %}"; - blogc_error_t *err = NULL; + bc_error_t *err = NULL; bc_slist_t *stmts = blogc_template_parse(a, strlen(a), &err); assert_non_null(err); assert_null(stmts); @@ -1082,7 +1083,7 @@ test_template_parse_invalid_endif_not_closed_inside_block(void **state) assert_string_equal(err->msg, "1 open 'if', 'ifdef' and/or 'ifndef' statements were not closed inside " "a 'listing' block!"); - blogc_error_free(err); + bc_error_free(err); } @@ -1090,7 +1091,7 @@ static void test_template_parse_invalid_else_not_closed_inside_block(void **state) { const char *a = "{% block listing %}{% ifdef BOLA %}{% else %}{% endblock %}{% endif %}"; - blogc_error_t *err = NULL; + bc_error_t *err = NULL; bc_slist_t *stmts = blogc_template_parse(a, strlen(a), &err); assert_non_null(err); assert_null(stmts); @@ -1098,7 +1099,7 @@ test_template_parse_invalid_else_not_closed_inside_block(void **state) assert_string_equal(err->msg, "1 open 'if', 'ifdef' and/or 'ifndef' statements were not closed inside " "a 'listing' block!"); - blogc_error_free(err); + bc_error_free(err); } @@ -1106,13 +1107,13 @@ static void test_template_parse_invalid_block_not_closed(void **state) { const char *a = "{% block entry %}\n"; - blogc_error_t *err = NULL; + bc_error_t *err = NULL; bc_slist_t *stmts = blogc_template_parse(a, strlen(a), &err); assert_non_null(err); assert_null(stmts); assert_int_equal(err->type, BLOGC_ERROR_TEMPLATE_PARSER); assert_string_equal(err->msg, "An open block was not closed!"); - blogc_error_free(err); + bc_error_free(err); } @@ -1120,13 +1121,13 @@ static void test_template_parse_invalid_foreach_not_closed(void **state) { const char *a = "{% foreach ASD %}\n"; - blogc_error_t *err = NULL; + bc_error_t *err = NULL; bc_slist_t *stmts = blogc_template_parse(a, strlen(a), &err); assert_non_null(err); assert_null(stmts); assert_int_equal(err->type, BLOGC_ERROR_TEMPLATE_PARSER); assert_string_equal(err->msg, "An open 'foreach' statement was not closed!"); - blogc_error_free(err); + bc_error_free(err); } diff --git a/tests/common/check_error.c b/tests/common/check_error.c new file mode 100644 index 0000000..9c72916 --- /dev/null +++ b/tests/common/check_error.c @@ -0,0 +1,109 @@ +/* + * blogc: A blog compiler. + * Copyright (C) 2015-2016 Rafael G. Martins + * + * This program can be distributed under the terms of the BSD License. + * See the file LICENSE. + */ + +#include +#include +#include +#include +#include +#include "../../src/common/error.h" + + +static void +test_error_new(void **state) +{ + bc_error_t *error = bc_error_new(1, "bola %s"); + assert_non_null(error); + assert_int_equal(error->type, 1); + assert_string_equal(error->msg, "bola %s"); + bc_error_free(error); +} + + +static void +test_error_new_printf(void **state) +{ + bc_error_t *error = bc_error_new_printf(2, "bola %s", "guda"); + assert_non_null(error); + assert_int_equal(error->type, 2); + assert_string_equal(error->msg, "bola guda"); + bc_error_free(error); +} + + +static void +test_error_parser(void **state) +{ + const char *a = "bola\nguda\nchunda\n"; + bc_error_t *error = bc_error_parser(1, a, strlen(a), 11, "asd %d", 10); + assert_non_null(error); + assert_int_equal(error->type, 1); + assert_string_equal(error->msg, + "asd 10\nError occurred near line 3, position 2: chunda"); + bc_error_free(error); + a = "bola\nguda\nchunda"; + error = bc_error_parser(1, a, strlen(a), 11, "asd %d", 10); + assert_non_null(error); + assert_int_equal(error->type, 1); + assert_string_equal(error->msg, + "asd 10\nError occurred near line 3, position 2: chunda"); + bc_error_free(error); + a = "bola\nguda\nchunda"; + error = bc_error_parser(1, a, strlen(a), 0, "asd %d", 10); + assert_non_null(error); + assert_int_equal(error->type, 1); + assert_string_equal(error->msg, + "asd 10\nError occurred near line 1, position 1: bola"); + bc_error_free(error); + a = ""; + error = bc_error_parser(1, a, strlen(a), 0, "asd %d", 10); + assert_non_null(error); + assert_int_equal(error->type, 1); + assert_string_equal(error->msg, "asd 10"); + bc_error_free(error); +} + + +static void +test_error_parser_crlf(void **state) +{ + const char *a = "bola\r\nguda\r\nchunda\r\n"; + bc_error_t *error = bc_error_parser(1, a, strlen(a), 13, "asd %d", 10); + assert_non_null(error); + assert_int_equal(error->type, 1); + assert_string_equal(error->msg, + "asd 10\nError occurred near line 3, position 2: chunda"); + bc_error_free(error); + a = "bola\r\nguda\r\nchunda"; + error = bc_error_parser(1, a, strlen(a), 13, "asd %d", 10); + assert_non_null(error); + assert_int_equal(error->type, 1); + assert_string_equal(error->msg, + "asd 10\nError occurred near line 3, position 2: chunda"); + bc_error_free(error); + a = "bola\r\nguda\r\nchunda"; + error = bc_error_parser(1, a, strlen(a), 0, "asd %d", 10); + assert_non_null(error); + assert_int_equal(error->type, 1); + assert_string_equal(error->msg, + "asd 10\nError occurred near line 1, position 1: bola"); + bc_error_free(error); +} + + +int +main(void) +{ + const UnitTest tests[] = { + unit_test(test_error_new), + unit_test(test_error_new_printf), + unit_test(test_error_parser), + unit_test(test_error_parser_crlf), + }; + return run_tests(tests); +} -- cgit v1.2.3-18-g5258