Skip to content

Commit

Permalink
filterx: regexp subst unit tests separated
Browse files Browse the repository at this point in the history
Signed-off-by: shifter <shifter@axoflow.com>
  • Loading branch information
bshifter committed Dec 9, 2024
1 parent 199f067 commit 73cad04
Show file tree
Hide file tree
Showing 4 changed files with 373 additions and 304 deletions.
1 change: 1 addition & 0 deletions lib/filterx/tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,4 @@ add_unit_test(LIBTEST CRITERION TARGET test_expr_plus DEPENDS json-plugin ${JSON
add_unit_test(LIBTEST CRITERION TARGET test_expr_plus_generator DEPENDS json-plugin ${JSONC_LIBRARY})
add_unit_test(LIBTEST CRITERION TARGET test_metrics_labels DEPENDS json-plugin ${JSONC_LIBRARY})
add_unit_test(LIBTEST CRITERION TARGET test_expr_regexp_search DEPENDS json-plugin ${JSONC_LIBRARY})
add_unit_test(LIBTEST CRITERION TARGET test_expr_regexp_subst DEPENDS json-plugin ${JSONC_LIBRARY})
4 changes: 4 additions & 0 deletions lib/filterx/tests/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ lib_filterx_tests_TESTS = \
lib/filterx/tests/test_func_unset_empties \
lib/filterx/tests/test_func_flatten \
lib/filterx/tests/test_expr_regexp_search \
lib/filterx/tests/test_expr_regexp_subst \
lib/filterx/tests/test_expr_regexp \
lib/filterx/tests/test_expr_null_coalesce \
lib/filterx/tests/test_expr_plus \
Expand Down Expand Up @@ -99,6 +100,9 @@ lib_filterx_tests_test_expr_function_LDADD = $(TEST_LDADD) $(JSON_LIBS)
lib_filterx_tests_test_expr_regexp_search_CFLAGS = $(TEST_CFLAGS)
lib_filterx_tests_test_expr_regexp_search_LDADD = $(TEST_LDADD) $(JSON_LIBS)

lib_filterx_tests_test_expr_regexp_subst_CFLAGS = $(TEST_CFLAGS)
lib_filterx_tests_test_expr_regexp_subst_LDADD = $(TEST_LDADD) $(JSON_LIBS)

lib_filterx_tests_test_expr_regexp_CFLAGS = $(TEST_CFLAGS)
lib_filterx_tests_test_expr_regexp_LDADD = $(TEST_LDADD) $(JSON_LIBS)

Expand Down
304 changes: 0 additions & 304 deletions lib/filterx/tests/test_expr_regexp.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
#include "libtest/filterx-lib.h"

#include "filterx/expr-regexp.h"
#include "filterx/expr-regexp-subst.h"
#include "filterx/expr-literal.h"
#include "filterx/object-string.h"
#include "filterx/object-primitive.h"
Expand Down Expand Up @@ -79,309 +78,6 @@ Test(filterx_expr_regexp, regexp_match)
_assert_match_init_error("abc", "(");
}

static FilterXExpr *
_build_subst_func(const gchar *pattern, const gchar *repr, const gchar *str, FilterXFuncRegexpSubstOpts opts)
{
GList *args = NULL;
args = g_list_append(args, filterx_function_arg_new(NULL, filterx_non_literal_new(filterx_string_new(str, -1))));
args = g_list_append(args, filterx_function_arg_new(NULL, filterx_literal_new(filterx_string_new(pattern, -1))));
args = g_list_append(args, filterx_function_arg_new(NULL, filterx_literal_new(filterx_string_new(repr, -1))));
if (opts.global)
args = g_list_append(args, filterx_function_arg_new(FILTERX_FUNC_REGEXP_SUBST_FLAG_GLOBAL_NAME,
filterx_literal_new(filterx_boolean_new(TRUE))));
if (!opts.jit)
args = g_list_append(args, filterx_function_arg_new(FILTERX_FUNC_REGEXP_SUBST_FLAG_JIT_NAME,
filterx_literal_new(filterx_boolean_new(FALSE))));
if (opts.ignorecase)
args = g_list_append(args, filterx_function_arg_new(FILTERX_FUNC_REGEXP_SUBST_FLAG_IGNORECASE_NAME,
filterx_literal_new(filterx_boolean_new(TRUE))));
if (opts.newline)
args = g_list_append(args, filterx_function_arg_new(FILTERX_FUNC_REGEXP_SUBST_FLAG_NEWLINE_NAME,
filterx_literal_new(filterx_boolean_new(TRUE))));
if (opts.utf8)
args = g_list_append(args, filterx_function_arg_new(FILTERX_FUNC_REGEXP_SUBST_FLAG_UTF8_NAME,
filterx_literal_new(filterx_boolean_new(TRUE))));
if (opts.groups)
args = g_list_append(args, filterx_function_arg_new(FILTERX_FUNC_REGEXP_SUBST_FLAG_GROUPS_NAME,
filterx_literal_new(filterx_boolean_new(TRUE))));

GError *err = NULL;
FilterXExpr *func = filterx_function_regexp_subst_new(filterx_function_args_new(args, NULL), &err);
cr_assert_null(err);
return func;
}

static FilterXObject *
_sub(const gchar *pattern, const gchar *repr, const gchar *str, FilterXFuncRegexpSubstOpts opts)
{
FilterXExpr *func = _build_subst_func(pattern, repr, str, opts);

FilterXObject *res = filterx_expr_eval(func);
filterx_expr_unref(func);
return res;
}

// disabling jit compiler since it confuses valgrind in some cases
// in some test cases we test jit against non-jit, those tests will produce invalid reads in valgrind
// further info: https://stackoverflow.com/questions/74777619/valgrind-conditional-jump-error-with-pcre2-jit-when-reading-from-file

Test(filterx_expr_regexp, regexp_subst_single_replace)
{
FilterXFuncRegexpSubstOpts opts = {};
FilterXObject *result = _sub("oo", "X", "foobarbaz", opts);
cr_assert(filterx_object_is_type(result, &FILTERX_TYPE_NAME(string)));
const gchar *res = filterx_string_get_value_ref(result, NULL);
cr_assert_str_eq(res, "fXbarbaz");
filterx_object_unref(result);
}

Test(filterx_expr_regexp, regexp_subst_single_replace_with_global)
{
FilterXFuncRegexpSubstOpts opts = {.global = TRUE};
FilterXObject *result = _sub("oo", "X", "foobarbaz", opts);
cr_assert(filterx_object_is_type(result, &FILTERX_TYPE_NAME(string)));
const gchar *res = filterx_string_get_value_ref(result, NULL);
cr_assert_str_eq(res, "fXbarbaz");
filterx_object_unref(result);
}

Test(filterx_expr_regexp, regexp_subst_multi_replace)
{
FilterXFuncRegexpSubstOpts opts = {};
FilterXObject *result = _sub("a", "X", "foobarbaz", opts);
cr_assert(filterx_object_is_type(result, &FILTERX_TYPE_NAME(string)));
const gchar *res = filterx_string_get_value_ref(result, NULL);
cr_assert_str_eq(res, "foobXrbaz");
filterx_object_unref(result);
}

Test(filterx_expr_regexp, regexp_subst_multi_replace_with_global)
{
FilterXFuncRegexpSubstOpts opts = {.global = TRUE};
FilterXObject *result = _sub("a", "X", "foobarbaz", opts);
cr_assert(filterx_object_is_type(result, &FILTERX_TYPE_NAME(string)));
const gchar *res = filterx_string_get_value_ref(result, NULL);
cr_assert_str_eq(res, "foobXrbXz");
filterx_object_unref(result);
}

Test(filterx_expr_regexp, regexp_subst_zero_length_matches)
{
FilterXFuncRegexpSubstOpts opts = {};
FilterXObject *result = _sub("u*", "X", "foobarbaz", opts);
cr_assert(filterx_object_is_type(result, &FILTERX_TYPE_NAME(string)));
const gchar *res = filterx_string_get_value_ref(result, NULL);
cr_assert_str_eq(res, "XfoobarbazX");
filterx_object_unref(result);
}

Test(filterx_expr_regexp, regexp_subst_zero_length_matches_with_global)
{
FilterXFuncRegexpSubstOpts opts = {.global = TRUE};
FilterXObject *result = _sub("u*", "X", "foobarbaz", opts);
cr_assert(filterx_object_is_type(result, &FILTERX_TYPE_NAME(string)));
const gchar *res = filterx_string_get_value_ref(result, NULL);
cr_assert_str_eq(res, "XfXoXoXbXaXrXbXaXzX");
filterx_object_unref(result);
}

Test(filterx_expr_regexp, regexp_subst_zero_length_matches_with_char_matches)
{
FilterXFuncRegexpSubstOpts opts = {};
FilterXObject *result = _sub("a*", "X", "foobarbaz", opts);
cr_assert(filterx_object_is_type(result, &FILTERX_TYPE_NAME(string)));
const gchar *res = filterx_string_get_value_ref(result, NULL);
cr_assert_str_eq(res, "XfoobarbazX");
filterx_object_unref(result);
}

Test(filterx_expr_regexp, regexp_subst_zero_length_matches_with_char_matches_with_global)
{
FilterXFuncRegexpSubstOpts opts = {.global = TRUE, .jit=FALSE};
FilterXObject *result = _sub("a*", "X", "foobarbaz", opts);
cr_assert(filterx_object_is_type(result, &FILTERX_TYPE_NAME(string)));
const gchar *res = filterx_string_get_value_ref(result, NULL);
cr_assert_str_eq(res, "XfXoXoXbXXrXbXXzX");
filterx_object_unref(result);
}

Test(filterx_expr_regexp, regexp_subst_at_beginning)
{
FilterXFuncRegexpSubstOpts opts = {};
FilterXObject *result = _sub("fo", "X", "foobarbaz", opts);
cr_assert(filterx_object_is_type(result, &FILTERX_TYPE_NAME(string)));
const gchar *res = filterx_string_get_value_ref(result, NULL);
cr_assert_str_eq(res, "Xobarbaz");
filterx_object_unref(result);
}

Test(filterx_expr_regexp, regexp_subst_at_beginning_with_global)
{
FilterXFuncRegexpSubstOpts opts = {.global = TRUE};
FilterXObject *result = _sub("fo", "X", "foobarbaz", opts);
cr_assert(filterx_object_is_type(result, &FILTERX_TYPE_NAME(string)));
const gchar *res = filterx_string_get_value_ref(result, NULL);
cr_assert_str_eq(res, "Xobarbaz");
filterx_object_unref(result);
}

Test(filterx_expr_regexp, regexp_subst_at_the_end)
{
FilterXFuncRegexpSubstOpts opts = {};
FilterXObject *result = _sub("az", "X", "foobarbaz", opts);
cr_assert(filterx_object_is_type(result, &FILTERX_TYPE_NAME(string)));
const gchar *res = filterx_string_get_value_ref(result, NULL);
cr_assert_str_eq(res, "foobarbX");
filterx_object_unref(result);
}

Test(filterx_expr_regexp, regexp_subst_at_the_end_with_global)
{
FilterXFuncRegexpSubstOpts opts = {.global = TRUE};
FilterXObject *result = _sub("az", "X", "foobarbaz", opts);
cr_assert(filterx_object_is_type(result, &FILTERX_TYPE_NAME(string)));
const gchar *res = filterx_string_get_value_ref(result, NULL);
cr_assert_str_eq(res, "foobarbX");
filterx_object_unref(result);
}

Test(filterx_expr_regexp, regexp_subst_multi_replace_multi_pattern)
{
FilterXFuncRegexpSubstOpts opts = {};
FilterXObject *result = _sub("(a|o)", "X", "foobarbaz", opts);
cr_assert(filterx_object_is_type(result, &FILTERX_TYPE_NAME(string)));
const gchar *res = filterx_string_get_value_ref(result, NULL);
cr_assert_str_eq(res, "fXobarbaz");
filterx_object_unref(result);
}

Test(filterx_expr_regexp, regexp_subst_multi_replace_multi_pattern_with_global)
{
FilterXFuncRegexpSubstOpts opts = {.global = TRUE};
FilterXObject *result = _sub("(a|o)", "X", "foobarbaz", opts);
cr_assert(filterx_object_is_type(result, &FILTERX_TYPE_NAME(string)));
const gchar *res = filterx_string_get_value_ref(result, NULL);
cr_assert_str_eq(res, "fXXbXrbXz");
filterx_object_unref(result);
}

Test(filterx_expr_regexp, regexp_subst_accept_end_literal)
{
FilterXFuncRegexpSubstOpts opts = {};
FilterXObject *result = _sub("ba.$", "X", "foobarbaz", opts);
cr_assert(filterx_object_is_type(result, &FILTERX_TYPE_NAME(string)));
const gchar *res = filterx_string_get_value_ref(result, NULL);
cr_assert_str_eq(res, "foobarX");
filterx_object_unref(result);
}

Test(filterx_expr_regexp, regexp_subst_accept_end_literal_with_global)
{
FilterXFuncRegexpSubstOpts opts = {.global = TRUE};
FilterXObject *result = _sub("ba.$", "X", "foobarbaz", opts);
cr_assert(filterx_object_is_type(result, &FILTERX_TYPE_NAME(string)));
const gchar *res = filterx_string_get_value_ref(result, NULL);
cr_assert_str_eq(res, "foobarX");
filterx_object_unref(result);
}

Test(filterx_expr_regexp, regexp_subst_accept_groups)
{
FilterXFuncRegexpSubstOpts opts = {};
FilterXObject *result = _sub("(o)*(ba)", "X", "foobarbaz", opts);
cr_assert(filterx_object_is_type(result, &FILTERX_TYPE_NAME(string)));
const gchar *res = filterx_string_get_value_ref(result, NULL);
cr_assert_str_eq(res, "fXrbaz");
filterx_object_unref(result);
}

Test(filterx_expr_regexp, regexp_subst_accept_groups_with_global)
{
FilterXFuncRegexpSubstOpts opts = {.global = TRUE};
FilterXObject *result = _sub("(o)*(ba)", "X", "foobarbaz", opts);
cr_assert(filterx_object_is_type(result, &FILTERX_TYPE_NAME(string)));
const gchar *res = filterx_string_get_value_ref(result, NULL);
cr_assert_str_eq(res, "fXrXz");
filterx_object_unref(result);
}

Test(filterx_expr_regexp, regexp_subst_nojit_arg)
{
FilterXFuncRegexpSubstOpts opts = {.jit = TRUE};
FilterXExpr *func = _build_subst_func("o", "X", "foobarbaz", opts);
cr_assert_not_null(func);
cr_assert(filterx_regexp_subst_is_jit_enabled(func));
filterx_expr_unref(func);

FilterXFuncRegexpSubstOpts opts_nojit = {};
FilterXExpr *func_nojit = _build_subst_func("o", "X", "foobarbaz", opts_nojit);
cr_assert_not_null(func_nojit);
cr_assert(!filterx_regexp_subst_is_jit_enabled(func_nojit));
filterx_expr_unref(func_nojit);
}

Test(filterx_expr_regexp, regexp_subst_match_opt_ignorecase)
{
FilterXFuncRegexpSubstOpts opts = {.global = TRUE};
FilterXObject *result = _sub("(O|A)", "X", "foobarbaz", opts);
cr_assert(filterx_object_is_type(result, &FILTERX_TYPE_NAME(string)));
const gchar *res = filterx_string_get_value_ref(result, NULL);
cr_assert_str_eq(res, "foobarbaz");
filterx_object_unref(result);

FilterXFuncRegexpSubstOpts opts_alt = {.ignorecase = TRUE, .global = TRUE};
FilterXObject *result_alt = _sub("(O|A)", "X", "foobarbaz", opts_alt);
cr_assert(filterx_object_is_type(result_alt, &FILTERX_TYPE_NAME(string)));
const gchar *res_alt = filterx_string_get_value_ref(result_alt, NULL);
cr_assert_str_eq(res_alt, "fXXbXrbXz");
filterx_object_unref(result_alt);
}

Test(filterx_expr_regexp, regexp_subst_match_opt_ignorecase_nojit)
{
// check whether the CASELESS option applied with non-jit pattern
FilterXFuncRegexpSubstOpts opts = {.global=TRUE};
FilterXObject *result = _sub("(O|A)", "X", "foobarbaz", opts);
cr_assert(filterx_object_is_type(result, &FILTERX_TYPE_NAME(string)));
const gchar *res = filterx_string_get_value_ref(result, NULL);
cr_assert_str_eq(res, "foobarbaz");
filterx_object_unref(result);

FilterXFuncRegexpSubstOpts opts_alt = {.ignorecase = TRUE, .global = TRUE, .jit = TRUE};
FilterXObject *result_alt = _sub("(O|A)", "X", "foobarbaz", opts_alt);
cr_assert(filterx_object_is_type(result_alt, &FILTERX_TYPE_NAME(string)));
const gchar *res_alt = filterx_string_get_value_ref(result_alt, NULL);
cr_assert_str_eq(res_alt, "fXXbXrbXz");
filterx_object_unref(result_alt);
}

Test(filterx_expr_regexp, regexp_subst_group_subst)
{
FilterXFuncRegexpSubstOpts opts = {};
FilterXObject *result = _sub("(\\d{2})-(\\d{2})-(\\d{4})", "\\3-\\2-\\1", "25-02-2022", opts);
cr_assert(filterx_object_is_type(result, &FILTERX_TYPE_NAME(string)));
const gchar *res = filterx_string_get_value_ref(result, NULL);
cr_assert_str_eq(res, "\\3-\\2-\\1");
filterx_object_unref(result);

FilterXFuncRegexpSubstOpts opts_alt = {.groups = TRUE};
FilterXObject *result_alt = _sub("(\\d{2})-(\\d{2})-(\\d{4})", "\\3-\\2-\\1", "25-02-2022", opts_alt);
cr_assert(filterx_object_is_type(result_alt, &FILTERX_TYPE_NAME(string)));
const gchar *res_alt = filterx_string_get_value_ref(result_alt, NULL);
cr_assert_str_eq(res_alt, "2022-02-25");
filterx_object_unref(result_alt);
}

Test(filterx_expr_regexp, regexp_subst_group_subst_without_ref)
{
FilterXFuncRegexpSubstOpts opts = {.groups = TRUE};
FilterXObject *result = _sub("(\\d{2})-(\\d{2})-(\\d{4})", "group without ref", "25-02-2022", opts);
cr_assert(filterx_object_is_type(result, &FILTERX_TYPE_NAME(string)));
const gchar *res = filterx_string_get_value_ref(result, NULL);
cr_assert_str_eq(res, "group without ref");
filterx_object_unref(result);
}

static void
setup(void)
{
Expand Down
Loading

0 comments on commit 73cad04

Please sign in to comment.