diff options
author | Rafael G. Martins <rafael@rafaelmartins.eng.br> | 2015-04-21 19:45:51 -0300 |
---|---|---|
committer | Rafael G. Martins <rafael@rafaelmartins.eng.br> | 2015-04-21 19:45:51 -0300 |
commit | d9755b37ca240cf1c088dfc9b209ea699fbcb7df (patch) | |
tree | 36ee2e6d20f45bc41459cc9075e453cf444144bd | |
parent | 42d0c90a19f558ceaef5a276cbad5686f49aa309 (diff) | |
download | blogc-d9755b37ca240cf1c088dfc9b209ea699fbcb7df.tar.gz blogc-d9755b37ca240cf1c088dfc9b209ea699fbcb7df.tar.bz2 blogc-d9755b37ca240cf1c088dfc9b209ea699fbcb7df.zip |
source parse: improved, added tests
-rw-r--r-- | src/error.c | 2 | ||||
-rw-r--r-- | src/source-parser.c | 40 | ||||
-rw-r--r-- | tests/check_error.c | 2 | ||||
-rw-r--r-- | tests/check_source_parser.c | 153 |
4 files changed, 191 insertions, 6 deletions
diff --git a/src/error.c b/src/error.c index 8e19e6e..3850304 100644 --- a/src/error.c +++ b/src/error.c @@ -69,7 +69,7 @@ blogc_error_parser(blogc_error_type_t type, const char *src, size_t src_len, rv = blogc_error_new(type, msg); else rv = blogc_error_new_printf(type, - "%s\nError occurred near to \"%s\".", msg, line); + "%s\nError occurred near to '%s'", msg, line); free(msg); free(line); diff --git a/src/source-parser.c b/src/source-parser.c index 322906f..60b5fe5 100644 --- a/src/source-parser.c +++ b/src/source-parser.c @@ -117,7 +117,7 @@ blogc_source_parse(const char *src, size_t src_len, blogc_error_t **err) } *err = blogc_error_parser(BLOGC_ERROR_SOURCE_PARSER, src, src_len, current, - "Invalid content separator. Must be one or more '-' characters."); + "Invalid content separator. Must be more than one '-' characters."); break; case SOURCE_CONTENT_START: @@ -125,9 +125,9 @@ blogc_source_parse(const char *src, size_t src_len, blogc_error_t **err) state = SOURCE_CONTENT; break; - case SOURCE_CONTENT: - if (current == (src_len - 1)) - b_trie_insert(rv, "CONTENT", + case SOURCE_CONTENT: + if (current == (src_len - 1)) + b_trie_insert(rv, "CONTENT", b_strndup(src + start, src_len - start)); break; } @@ -138,6 +138,38 @@ blogc_source_parse(const char *src, size_t src_len, blogc_error_t **err) current++; } + if (*err == NULL && b_trie_size(rv) == 0) { + + // ok, nothing found in the config trie, but no error set either. + // let's try to be nice with the users and provide some reasonable + // output. :) + switch (state) { + case SOURCE_START: + *err = blogc_error_parser(BLOGC_ERROR_SOURCE_PARSER, src, src_len, + current, "Your config file is empty."); + break; + case SOURCE_CONFIG_KEY: + *err = blogc_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, + current, "Configuration value not provided for '%s'.", + key); + break; + case SOURCE_CONFIG_VALUE: + *err = blogc_error_parser(BLOGC_ERROR_SOURCE_PARSER, src, src_len, + current, "No line ending after the configuration value for " + "'%s'.", key); + break; + case SOURCE_SEPARATOR: + case SOURCE_CONTENT_START: + case SOURCE_CONTENT: + break; // won't happen, and if even happen, shouldn't be fatal + } + } + if (*err != NULL) { free(key); b_trie_free(rv); diff --git a/tests/check_error.c b/tests/check_error.c index 59ac2f9..c4c08a9 100644 --- a/tests/check_error.c +++ b/tests/check_error.c @@ -48,7 +48,7 @@ test_error_parser(void **state) 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 to \"hunda\"."); + assert_string_equal(error->msg, "asd 10\nError occurred near to 'hunda'"); blogc_error_free(error); } diff --git a/tests/check_source_parser.c b/tests/check_source_parser.c index 90ebcc1..0838f41 100644 --- a/tests/check_source_parser.c +++ b/tests/check_source_parser.c @@ -72,12 +72,165 @@ test_source_parse_with_spaces(void **state) } +static void +test_source_parse_config_empty(void **state) +{ + const char *a = ""; + blogc_error_t *err = NULL; + b_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 config file is empty."); + blogc_error_free(err); + b_trie_free(source); +} + + +static void +test_source_parse_config_invalid_key(void **state) +{ + const char *a = "bola: guda"; + blogc_error_t *err = NULL; + b_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 to 'bola: guda'"); + blogc_error_free(err); + b_trie_free(source); +} + + +static void +test_source_parse_config_no_key(void **state) +{ + const char *a = "BOLa"; + blogc_error_t *err = NULL; + b_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 to 'a'"); + blogc_error_free(err); + b_trie_free(source); +} + + +static void +test_source_parse_config_no_key2(void **state) +{ + const char *a = "BOLA"; + blogc_error_t *err = NULL; + b_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"); + blogc_error_free(err); + b_trie_free(source); +} + + +static void +test_source_parse_config_no_value(void **state) +{ + const char *a = "BOLA:\r\n"; + blogc_error_t *err = NULL; + b_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, + "Configuration value not provided for 'BOLA'."); + blogc_error_free(err); + b_trie_free(source); +} + + +static void +test_source_parse_config_no_value2(void **state) +{ + const char *a = "BOLA:"; + blogc_error_t *err = NULL; + b_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, + "Configuration value not provided for 'BOLA'."); + blogc_error_free(err); + b_trie_free(source); +} + + +static void +test_source_parse_config_reserved_name(void **state) +{ + const char *a = "FILENAME: asd\r\n"; + blogc_error_t *err = NULL; + b_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, + "'FILENAME' variable is forbidden in source files. It will be set " + "for you by the compiler."); + blogc_error_free(err); + b_trie_free(source); +} + + +static void +test_source_parse_config_value_no_line_ending(void **state) +{ + const char *a = "BOLA: asd"; + blogc_error_t *err = NULL; + b_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, + "No line ending after the configuration value for 'BOLA'."); + blogc_error_free(err); + b_trie_free(source); +} + + +static void +test_source_parse_invalid_separator(void **state) +{ + const char *a = "BOLA: asd\n---#"; + blogc_error_t *err = NULL; + b_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, + "Invalid content separator. Must be more than one '-' characters.\n" + "Error occurred near to '#'"); + blogc_error_free(err); + b_trie_free(source); +} + + int main(void) { const UnitTest tests[] = { unit_test(test_source_parse), unit_test(test_source_parse_with_spaces), + unit_test(test_source_parse_config_empty), + unit_test(test_source_parse_config_invalid_key), + unit_test(test_source_parse_config_no_key), + unit_test(test_source_parse_config_no_key2), + unit_test(test_source_parse_config_no_value), + unit_test(test_source_parse_config_no_value2), + unit_test(test_source_parse_config_reserved_name), + unit_test(test_source_parse_config_value_no_line_ending), + unit_test(test_source_parse_invalid_separator), }; return run_tests(tests); } |