From 61263a9e96d19e81198aa47a3ac653a96fec752b Mon Sep 17 00:00:00 2001 From: shifter Date: Wed, 27 Nov 2024 02:59:13 +0100 Subject: [PATCH] filterx: regexp_subst match group handling unit tests Signed-off-by: shifter --- lib/filterx/tests/test_expr_regexp.c | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/lib/filterx/tests/test_expr_regexp.c b/lib/filterx/tests/test_expr_regexp.c index b60009a6c..71d63744c 100644 --- a/lib/filterx/tests/test_expr_regexp.c +++ b/lib/filterx/tests/test_expr_regexp.c @@ -278,6 +278,9 @@ _build_subst_func(const gchar *pattern, const gchar *repr, const gchar *str, Fil 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); @@ -529,6 +532,23 @@ Test(filterx_expr_regexp, regexp_subst_match_opt_ignorecase_nojit) 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); +} + static void setup(void) {