[PATCH (3.5)] test_template_compile v2
From: Juhasz Viktor <jviktor@balabit.hu> test_template_compile unit test is refactorized The assert macros are merged into the assert_compile_template macro This macro fill the expected_elem global variable and checks the current_elem. Therefore we don't need the local expected_element variables and calling the fill_expected_template_element macro This patch based on log-template-refactor branch Signed-off-by: Juhasz Viktor <jviktor@balabit.hu> --- tests/unit/test_template_compile.c | 171 +++++++++--------------------------- 1 file changed, 41 insertions(+), 130 deletions(-) diff --git a/tests/unit/test_template_compile.c b/tests/unit/test_template_compile.c index fd6fd50..9378e34 100644 --- a/tests/unit/test_template_compile.c +++ b/tests/unit/test_template_compile.c @@ -26,22 +26,6 @@ Plugin hello_plugin = TEMPLATE_FUNCTION_PLUGIN(hello, "hello"); } \ assert_gint(current_elem->msg_ref, expected.msg_ref, ASSERTION_ERROR("Bad compiled template msg_ref")); -#define assert_value_element(expected) \ - assert_gint(current_elem->type, LTE_VALUE, ASSERTION_ERROR("Bad compiled template type")); \ - assert_common_element(expected); \ - assert_gint(current_elem->value_handle, expected.value_handle, ASSERTION_ERROR("Bad compiled template macro")); - -#define assert_macro_element(expected) \ - assert_gint(current_elem->type, LTE_MACRO, ASSERTION_ERROR("Bad compiled template type")); \ - assert_common_element(expected); \ - assert_gint(current_elem->macro, expected.macro, ASSERTION_ERROR("Bad compiled template macro")); - -#define assert_func_element(expected) \ - assert_gint(current_elem->type, LTE_FUNC, ASSERTION_ERROR("Bad compiled template type")); \ - assert_common_element(expected); \ - assert_gpointer(current_elem->func.ops, expected.func.ops, ASSERTION_ERROR("Bad compiled template macro")); - - #define fill_expected_template_element(element, text, default_value, spec, type, msg_ref) {\ element.text; \ element.default_value; \ @@ -49,6 +33,13 @@ Plugin hello_plugin = TEMPLATE_FUNCTION_PLUGIN(hello, "hello"); element.type; \ element.msg_ref; } +#define assert_compiled_template(text, default_value, spec, type, msg_ref) \ + fill_expected_template_element(expected_elem, text, default_value, spec, type, msg_ref); \ + assert_gint((current_elem->type), (expected_elem.type), ASSERTION_ERROR("Bad compiled template type")); \ + assert_common_element(expected_elem); \ + if ((expected_elem.type) == LTE_MACRO) assert_gint(current_elem->macro, expected_elem.macro, ASSERTION_ERROR("Bad compiled template macro")); \ + if ((expected_elem.type) == LTE_VALUE) assert_gint(current_elem->value_handle, expected_elem.value_handle, ASSERTION_ERROR("Bad compiled template macro")); \ + if ((expected_elem.type) == LTE_FUNC) assert_gpointer(current_elem->func.ops, expected_elem.func.ops, ASSERTION_ERROR("Bad compiled template macro")); \ #define TEMPLATE_TESTCASE(x, ...) do { template_testcase_begin(#x, #__VA_ARGS__); x(__VA_ARGS__); template_testcase_end(); } while(0) @@ -75,6 +66,7 @@ Plugin hello_plugin = TEMPLATE_FUNCTION_PLUGIN(hello, "hello"); static LogTemplate *template; static GList *current_elem_list; static LogTemplateElem *current_elem; +static LogTemplateElem expected_elem; static void select_current_element(void) @@ -121,163 +113,119 @@ assert_failed_template_compile(const gchar *template_string, const gchar *expect static void test_simple_string_literal(void) { - LogTemplateElem expected_element; gchar *text = "Test String"; assert_template_compile(text); - fill_expected_template_element(expected_element, text = text, default_value = NULL, macro = M_NONE, type = LTE_MACRO, msg_ref = 0); - assert_macro_element(expected_element); + assert_compiled_template(text = text, default_value = NULL, macro = M_NONE, type = LTE_MACRO, msg_ref = 0); } static void test_simple_macro(void) { - LogTemplateElem expected_element; - assert_template_compile("${MESSAGE}"); - fill_expected_template_element(expected_element, text = "", default_value = NULL, macro = M_MESSAGE, type = LTE_MACRO, msg_ref = 0); - assert_macro_element(expected_element); + assert_compiled_template(text = "", default_value = NULL, macro = M_MESSAGE, type = LTE_MACRO, msg_ref = 0); } static void test_macro_and_text(void) { - LogTemplateElem expected_element; assert_template_compile("${MESSAGE}test value"); - fill_expected_template_element(expected_element, text = "", default_value = NULL, macro = M_MESSAGE, type = LTE_MACRO, msg_ref = 0); - assert_macro_element(expected_element); + assert_compiled_template(text = "", default_value = NULL, macro = M_MESSAGE, type = LTE_MACRO, msg_ref = 0); select_next_element(); - fill_expected_template_element(expected_element, text = "test value", default_value = NULL, macro = M_NONE, type = LTE_MACRO, msg_ref = 0); - assert_macro_element(expected_element); + assert_compiled_template(text = "test value", default_value = NULL, macro = M_NONE, type = LTE_MACRO, msg_ref = 0); } static void test_macro_without_braces(void) { - LogTemplateElem expected_element; - assert_template_compile("$MESSAGE"); - fill_expected_template_element(expected_element, text = "", default_value = NULL, macro = M_MESSAGE, type = LTE_MACRO, msg_ref = 0); - assert_macro_element(expected_element); + assert_compiled_template(text = "", default_value = NULL, macro = M_MESSAGE, type = LTE_MACRO, msg_ref = 0); } static void test_macro_name_without_braces_are_terminated_with_non_identifier_characters(void) { - LogTemplateElem expected_element; - /* macro names consist of [A-Z0-9_] */ assert_template_compile("$MESSAGE test value"); - fill_expected_template_element(expected_element, text = "", default_value = NULL, macro = M_MESSAGE, type = LTE_MACRO, msg_ref = 0); - assert_macro_element(expected_element); + assert_compiled_template(text = "", default_value = NULL, macro = M_MESSAGE, type = LTE_MACRO, msg_ref = 0); select_next_element(); - fill_expected_template_element(expected_element, text = " test value", default_value = NULL, macro = M_NONE, type = LTE_MACRO, msg_ref = 0); - assert_macro_element(expected_element); + assert_compiled_template(text = " test value", default_value = NULL, macro = M_NONE, type = LTE_MACRO, msg_ref = 0); } static void test_macro_without_at_records_that_no_msgref_was_present_by_msgref_zero(void) { - LogTemplateElem expected_element; - assert_template_compile("${MESSAGE}"); - fill_expected_template_element(expected_element, text = "", default_value = NULL, macro = M_MESSAGE, type = LTE_MACRO, msg_ref = 0); - assert_macro_element(expected_element); + assert_compiled_template(text = "", default_value = NULL, macro = M_MESSAGE, type = LTE_MACRO, msg_ref = 0); } static void test_macro_with_at_references_a_single_msg_in_the_context_stack_by_setting_msgref(void) { - LogTemplateElem expected_element; - assert_template_compile("${MESSAGE}@0"); - fill_expected_template_element(expected_element, text = "", default_value = NULL, macro = M_MESSAGE, type = LTE_MACRO, msg_ref = 1); - assert_macro_element(expected_element); + assert_compiled_template(text = "", default_value = NULL, macro = M_MESSAGE, type = LTE_MACRO, msg_ref = 1); assert_template_compile("${MESSAGE}@1"); - fill_expected_template_element(expected_element, text = "", default_value = NULL, macro = M_MESSAGE, type = LTE_MACRO, msg_ref = 2); - assert_macro_element(expected_element); + assert_compiled_template(text = "", default_value = NULL, macro = M_MESSAGE, type = LTE_MACRO, msg_ref = 2); } static void test_macro_with_invalid_msgref_are_recognized_as_the_top_element_in_the_stack(void) { - LogTemplateElem expected_element; - assert_template_compile("${MESSAGE}@gmail.com"); - fill_expected_template_element(expected_element, text = "", default_value = NULL, macro = M_MESSAGE, type = LTE_MACRO, msg_ref = 1); - assert_macro_element(expected_element); + assert_compiled_template(text = "", default_value = NULL, macro = M_MESSAGE, type = LTE_MACRO, msg_ref = 1); select_next_element(); - fill_expected_template_element(expected_element, text = "gmail.com", default_value = NULL, macro = M_NONE, type = LTE_MACRO, msg_ref = 0); - assert_macro_element(expected_element); + assert_compiled_template(text = "gmail.com", default_value = NULL, macro = M_NONE, type = LTE_MACRO, msg_ref = 0); } static void test_dollar_prefixed_with_backslash_is_a_literal_dollar(void) { - LogTemplateElem expected_element; - assert_template_compile("Test \\$STRING"); - fill_expected_template_element(expected_element, text = "Test $STRING", default_value = NULL, macro = M_NONE, type = LTE_MACRO, msg_ref = 0); - assert_macro_element(expected_element); + assert_compiled_template(text = "Test $STRING", default_value = NULL, macro = M_NONE, type = LTE_MACRO, msg_ref = 0); } static void test_colon_dash_in_braces_is_parsed_as_default_value(void) { - LogTemplateElem expected_element; - assert_template_compile("${MESSAGE:-default value}"); - fill_expected_template_element(expected_element, text = "", default_value = "default value", macro = M_MESSAGE, type = LTE_MACRO, msg_ref = 0); - assert_macro_element(expected_element); + assert_compiled_template(text = "", default_value = "default value", macro = M_MESSAGE, type = LTE_MACRO, msg_ref = 0); assert_template_compile("${MESSAGE:-}"); - fill_expected_template_element(expected_element, text = "", default_value = "", macro = M_MESSAGE, type = LTE_MACRO, msg_ref = 0); - assert_macro_element(expected_element); + assert_compiled_template(text = "", default_value = "", macro = M_MESSAGE, type = LTE_MACRO, msg_ref = 0); } static void test_double_dollars_is_a_literal_dollar() { - LogTemplateElem expected_element; - assert_template_compile("$$VALUE_NAME"); - fill_expected_template_element(expected_element, text = "$VALUE_NAME", default_value = NULL, macro = M_NONE, type = LTE_MACRO, msg_ref = 0); - assert_macro_element(expected_element); + assert_compiled_template(text = "$VALUE_NAME", default_value = NULL, macro = M_NONE, type = LTE_MACRO, msg_ref = 0); assert_template_compile("$${VALUE_NAME}"); - fill_expected_template_element(expected_element, text = "${VALUE_NAME}", default_value = NULL, macro = M_NONE, type = LTE_MACRO, msg_ref = 0); - assert_macro_element(expected_element); + assert_compiled_template(text = "${VALUE_NAME}", default_value = NULL, macro = M_NONE, type = LTE_MACRO, msg_ref = 0); } static void test_dollar_with_an_invalid_macro_name_without_braces_is_parsed_as_a_literal_dollar(void) { - LogTemplateElem expected_element; - assert_template_compile("$:VALUE_NAME"); - fill_expected_template_element(expected_element, text = "$:VALUE_NAME", default_value = NULL, macro = M_NONE, type = LTE_MACRO, msg_ref = 0); - assert_macro_element(expected_element); + assert_compiled_template(text = "$:VALUE_NAME", default_value = NULL, macro = M_NONE, type = LTE_MACRO, msg_ref = 0); assert_template_compile("$"); - fill_expected_template_element(expected_element, text = "$", default_value = NULL, macro = M_NONE, type = LTE_MACRO, msg_ref = 0); - assert_macro_element(expected_element); + assert_compiled_template(text = "$", default_value = NULL, macro = M_NONE, type = LTE_MACRO, msg_ref = 0); } static void test_backslash_without_finishing_the_escape_sequence_is_ignored(void) { - LogTemplateElem expected_element; - assert_template_compile("foo\\"); - fill_expected_template_element(expected_element, text = "foo", default_value = NULL, macro = M_NONE, type = LTE_MACRO, msg_ref = 0); - assert_macro_element(expected_element); + assert_compiled_template(text = "foo", default_value = NULL, macro = M_NONE, type = LTE_MACRO, msg_ref = 0); } static void @@ -301,41 +249,29 @@ test_template_compile_macro(void) static void test_simple_value(void) { - LogTemplateElem expected_element; - assert_template_compile("${VALUE_NAME}"); - fill_expected_template_element(expected_element, text = "", default_value = NULL, value_handle = log_msg_get_value_handle("VALUE_NAME"), type = LTE_VALUE, msg_ref = 0); - assert_value_element(expected_element); + assert_compiled_template(text = "", default_value = NULL, value_handle = log_msg_get_value_handle("VALUE_NAME"), type = LTE_VALUE, msg_ref = 0); } static void test_value_without_braces(void) { - LogTemplateElem expected_element; - assert_template_compile("$VALUE_NAME"); - fill_expected_template_element(expected_element, text = "", default_value = NULL, value_handle = log_msg_get_value_handle("VALUE_NAME"), type = LTE_VALUE, msg_ref = 0); - assert_value_element(expected_element); + assert_compiled_template(text = "", default_value = NULL, value_handle = log_msg_get_value_handle("VALUE_NAME"), type = LTE_VALUE, msg_ref = 0); } static void test_backslash_within_braces_is_taken_literally(void) { - LogTemplateElem expected_element; - assert_template_compile("${VALUE\\}NAME}"); - fill_expected_template_element(expected_element, text = "", default_value = NULL, value_handle = log_msg_get_value_handle("VALUE\\"), type = LTE_VALUE, msg_ref = 0); - assert_value_element(expected_element); + assert_compiled_template(text = "", default_value = NULL, value_handle = log_msg_get_value_handle("VALUE\\"), type = LTE_VALUE, msg_ref = 0); } static void test_value_name_can_be_the_empty_string_when_referenced_using_braces(void) { - LogTemplateElem expected_element; - assert_template_compile("${}"); - fill_expected_template_element(expected_element, text = "", default_value = NULL, value_handle = log_msg_get_value_handle(""), type = LTE_VALUE, msg_ref = 0); - assert_value_element(expected_element); + assert_compiled_template(text = "", default_value = NULL, value_handle = log_msg_get_value_handle(""), type = LTE_VALUE, msg_ref = 0); } static void @@ -350,35 +286,25 @@ test_template_compile_value() static void test_simple_template_function(void) { - LogTemplateElem expected_element; - assert_template_compile("$(hello)"); - fill_expected_template_element(expected_element, text = "", default_value = NULL, func.ops = hello_construct(&hello_plugin, configuration, LL_CONTEXT_TEMPLATE_FUNC, "hello"), type = LTE_FUNC, msg_ref = 0); - assert_func_element(expected_element); + assert_compiled_template(text = "", default_value = NULL, func.ops = hello_construct(&hello_plugin, configuration, LL_CONTEXT_TEMPLATE_FUNC, "hello"), type = LTE_FUNC, msg_ref = 0); } static void test_complicated_template_function(void) { - LogTemplateElem expected_element; - assert_template_compile("$( hello \\tes\t\t\t value(xyz) \"value with spaces\" 'test value with spa\"ces')@2"); - fill_expected_template_element(expected_element, text = "", default_value = NULL, func.ops = hello_construct(&hello_plugin, configuration, LL_CONTEXT_TEMPLATE_FUNC, "hello"), type = LTE_FUNC, msg_ref = 3); - assert_func_element(expected_element); + assert_compiled_template(text = "", default_value = NULL, func.ops = hello_construct(&hello_plugin, configuration, LL_CONTEXT_TEMPLATE_FUNC, "hello"), type = LTE_FUNC, msg_ref = 3); } static void test_simple_template_function_with_additional_text(void) { - LogTemplateElem expected_element; - assert_template_compile("$(hello)test value"); - fill_expected_template_element(expected_element, text = "", default_value = NULL, func.ops = hello_construct(&hello_plugin, configuration, LL_CONTEXT_TEMPLATE_FUNC, "hello"), type = LTE_FUNC, msg_ref = 0); - assert_func_element(expected_element); + assert_compiled_template(text = "", default_value = NULL, func.ops = hello_construct(&hello_plugin, configuration, LL_CONTEXT_TEMPLATE_FUNC, "hello"), type = LTE_FUNC, msg_ref = 0); select_next_element(); - fill_expected_template_element(expected_element, text = "test value", default_value = NULL, macro = M_NONE, type = LTE_MACRO, msg_ref = 0); - assert_macro_element(expected_element); + assert_compiled_template(text = "test value", default_value = NULL, macro = M_NONE, type = LTE_MACRO, msg_ref = 0); } static void @@ -392,53 +318,38 @@ test_template_compile_func(void) static void test_invalid_macro(void) { - LogTemplateElem expected_element; - assert_failed_template_compile("${MESSAGE", "Invalid macro, '}' is missing, error_pos='9'"); - fill_expected_template_element(expected_element, text = "error in template: ${MESSAGE", default_value = NULL, macro = M_NONE, type = LTE_MACRO, msg_ref = 0); - assert_macro_element(expected_element); + assert_compiled_template(text = "error in template: ${MESSAGE", default_value = NULL, macro = M_NONE, type = LTE_MACRO, msg_ref = 0); } static void test_invalid_subst(void) { - LogTemplateElem expected_element; - assert_failed_template_compile("${MESSAGE:1}", "Unknown substitution function, error_pos='10'"); - fill_expected_template_element(expected_element, text = "error in template: ${MESSAGE:1}", default_value = NULL, macro = M_NONE, type = LTE_MACRO, msg_ref = 0); - assert_macro_element(expected_element); + assert_compiled_template(text = "error in template: ${MESSAGE:1}", default_value = NULL, macro = M_NONE, type = LTE_MACRO, msg_ref = 0); } static void test_template_function_bad1(void) { - LogTemplateElem expected_element; - assert_failed_template_compile("$( hello \\tes\t\t\t value(xyz \"value with spaces\" 'test value with spa\"ces')", "Invalid template function reference, missing function name or inbalanced '(', error_pos='73'"); - fill_expected_template_element(expected_element, text = "error in template: $( hello \\tes\t\t\t value(xyz \"value with spaces\" 'test value with spa\"ces')", default_value = NULL, macro = M_NONE, type = LTE_MACRO, msg_ref = 0); - assert_macro_element(expected_element); + assert_compiled_template(text = "error in template: $( hello \\tes\t\t\t value(xyz \"value with spaces\" 'test value with spa\"ces')", default_value = NULL, macro = M_NONE, type = LTE_MACRO, msg_ref = 0); } static void test_template_function_bad2(void) { - LogTemplateElem expected_element; - assert_failed_template_compile("$( hello \\tes\t\t\t value xyz \"value with spaces\" 'test value with spa\"ces'", "Invalid template function reference, missing function name or inbalanced '(', error_pos='72'"); - fill_expected_template_element(expected_element, text = "error in template: $( hello \\tes\t\t\t value xyz \"value with spaces\" 'test value with spa\"ces'", default_value = NULL, macro = M_NONE, type = LTE_MACRO, msg_ref = 0); - assert_macro_element(expected_element); + assert_compiled_template(text = "error in template: $( hello \\tes\t\t\t value xyz \"value with spaces\" 'test value with spa\"ces'", default_value = NULL, macro = M_NONE, type = LTE_MACRO, msg_ref = 0); } static void test_unknown_function(void) { - LogTemplateElem expected_element; - assert_failed_template_compile("$(unknown function)", "Unknown template function unknown"); - fill_expected_template_element(expected_element, text = "error in template: $(unknown function)", default_value = NULL, macro = M_NONE, type = LTE_MACRO, msg_ref = 0); - assert_macro_element(expected_element); + assert_compiled_template(text = "error in template: $(unknown function)", default_value = NULL, macro = M_NONE, type = LTE_MACRO, msg_ref = 0); } static void -- 1.7.9.5
Hi, Thanks, I've merged this to 3.5 master. As discussed I've moved the templates.{c,h} files into a separate subdirectory and moved the test program side-by-side. Please continue your subsequent refactoring steps there. Thanks. On Thu, 2013-08-29 at 10:51 +0200, Juhász Viktor wrote:
From: Juhasz Viktor <jviktor@balabit.hu>
participants (2)
-
Balazs Scheidler
-
Juhász Viktor