Skip to content

Commit

Permalink
filterx: add unit tests for unset_empties()
Browse files Browse the repository at this point in the history
Signed-off-by: Attila Szakacs <[email protected]>
  • Loading branch information
alltilla committed Jun 7, 2024
1 parent fd0fb8e commit 26e8f77
Show file tree
Hide file tree
Showing 3 changed files with 167 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/filterx/tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ add_unit_test(LIBTEST CRITERION TARGET test_object_integer DEPENDS json-plugin $
add_unit_test(LIBTEST CRITERION TARGET test_object_double DEPENDS json-plugin ${JSONC_LIBRARY})
add_unit_test(LIBTEST CRITERION TARGET test_type_registry DEPENDS json-plugin ${JSONC_LIBRARY})
add_unit_test(LIBTEST CRITERION TARGET test_func_istype DEPENDS json-plugin ${JSONC_LIBRARY})
add_unit_test(LIBTEST CRITERION TARGET test_func_unset_empties DEPENDS json-plugin ${JSONC_LIBRARY})
add_unit_test(LIBTEST CRITERION TARGET test_expr_function DEPENDS json-plugin ${JSONC_LIBRARY})
add_unit_test(LIBTEST CRITERION TARGET test_expr_regexp 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 @@ -18,6 +18,7 @@ lib_filterx_tests_TESTS = \
lib/filterx/tests/test_builtin_functions \
lib/filterx/tests/test_type_registry \
lib/filterx/tests/test_func_istype \
lib/filterx/tests/test_func_unset_empties \
lib/filterx/tests/test_expr_regexp

EXTRA_DIST += lib/filterx/tests/CMakeLists.txt
Expand Down Expand Up @@ -75,6 +76,9 @@ lib_filterx_tests_test_type_registry_LDADD = $(TEST_LDADD) $(JSON_LIBS)
lib_filterx_tests_test_func_istype_CFLAGS = $(TEST_CFLAGS)
lib_filterx_tests_test_func_istype_LDADD = $(TEST_LDADD) $(JSON_LIBS)

lib_filterx_tests_test_func_unset_empties_CFLAGS = $(TEST_CFLAGS)
lib_filterx_tests_test_func_unset_empties_LDADD = $(TEST_LDADD) $(JSON_LIBS)

lib_filterx_tests_test_expr_function_CFLAGS = $(TEST_CFLAGS)
lib_filterx_tests_test_expr_function_LDADD = $(TEST_LDADD) $(JSON_LIBS)

Expand Down
162 changes: 162 additions & 0 deletions lib/filterx/tests/test_func_unset_empties.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
/*
* Copyright (c) 2024 Attila Szakacs
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
* As an additional exemption you are allowed to compile & link against the
* OpenSSL libraries as published by the OpenSSL project. See the file
* COPYING for details.
*
*/

#include <criterion/criterion.h>
#include "libtest/filterx-lib.h"

#include "filterx/func-unset-empties.h"
#include "filterx/object-string.h"
#include "filterx/object-primitive.h"
#include "filterx/object-json.h"
#include "filterx/expr-literal.h"

#include "apphook.h"
#include "scratch-buffers.h"

static void
_assert_unset_empties_init_fail(GList *args)
{
GError *err = NULL;
GError *args_err = NULL;
FilterXFunction *func = filterx_function_unset_empties_new("test", filterx_function_args_new(args, &args_err), &err);
cr_assert(!func);
cr_assert(err);
g_error_free(err);
}

static void
_assert_unset_empties(GList *args, const gchar *expected_repr)
{
FilterXExpr *modifiable_object_expr = filterx_expr_ref(((FilterXFunctionArg *) args->data)->value);

GError *err = NULL;
GError *args_err = NULL;
FilterXFunction *func = filterx_function_unset_empties_new("test", filterx_function_args_new(args, &args_err), &err);
cr_assert(!err);

FilterXObject *obj = filterx_expr_eval(&func->super);
cr_assert(obj);
gboolean success;
cr_assert(filterx_boolean_unwrap(obj, &success));
cr_assert(success);

FilterXObject *modifiable_object = filterx_expr_eval(modifiable_object_expr);
cr_assert(modifiable_object);

GString *repr = g_string_new(NULL);
cr_assert(filterx_object_repr(modifiable_object, repr));
cr_assert_str_eq(repr->str, expected_repr, "unset_empties() result is unexpected. Actual: %s Expected: %s", repr->str,
expected_repr);

g_string_free(repr, TRUE);
filterx_object_unref(obj);
filterx_expr_unref(&func->super);
filterx_object_unref(modifiable_object);
filterx_expr_unref(modifiable_object_expr);
}

Test(filterx_func_unset_empties, invalid_args)
{
GList *args = NULL;

/* no args */
_assert_unset_empties_init_fail(NULL);

/* non-literal recursive */
args = g_list_append(args, filterx_function_arg_new(NULL, filterx_literal_new(filterx_test_dict_new())));
args = g_list_append(args, filterx_function_arg_new("recursive",
filterx_non_literal_new(filterx_boolean_new(TRUE))));
_assert_unset_empties_init_fail(args);
args = NULL;

/* non-boolean recursive */
args = g_list_append(args, filterx_function_arg_new(NULL, filterx_literal_new(filterx_test_dict_new())));
args = g_list_append(args, filterx_function_arg_new("recursive",
filterx_literal_new(filterx_string_new("", -1))));
_assert_unset_empties_init_fail(args);
args = NULL;

/* too many args */
args = g_list_append(args, filterx_function_arg_new(NULL, filterx_literal_new(filterx_test_dict_new())));
args = g_list_append(args, filterx_function_arg_new(NULL, filterx_literal_new(filterx_string_new("", -1))));
_assert_unset_empties_init_fail(args);
args = NULL;
}

Test(filterx_func_unset_empties, all_known_empties)
{
const gchar *input = "[\"\", \"n/a\", \"N/A\", \"-\", null, [], {}]";
GList *args = g_list_append(NULL, filterx_function_arg_new(NULL,
filterx_literal_new(filterx_json_new_from_repr(input, -1))));
_assert_unset_empties(args, "[]");
}

Test(filterx_func_unset_empties, recursive)
{
GList *args = NULL;

/* dict */

/* default is true */
args = g_list_append(args, filterx_function_arg_new(NULL,
filterx_literal_new(filterx_json_new_from_repr("[{\"foo\": \"\"}]", -1))));
_assert_unset_empties(args, "[]");
args = NULL;

args = g_list_append(args, filterx_function_arg_new(NULL,
filterx_literal_new(filterx_json_new_from_repr("[{\"foo\": \"\"}]", -1))));
args = g_list_append(args, filterx_function_arg_new("recursive", filterx_literal_new(filterx_boolean_new(FALSE))));
_assert_unset_empties(args, "[{\"foo\":\"\"}]");
args = NULL;

/* list */

/* default is true */
args = g_list_append(args, filterx_function_arg_new(NULL, filterx_literal_new(filterx_json_new_from_repr("[[\"\"]]",
-1))));
_assert_unset_empties(args, "[]");
args = NULL;

args = g_list_append(args, filterx_function_arg_new(NULL, filterx_literal_new(filterx_json_new_from_repr("[[\"\"]]",
-1))));
args = g_list_append(args, filterx_function_arg_new("recursive", filterx_literal_new(filterx_boolean_new(FALSE))));
_assert_unset_empties(args, "[[\"\"]]");
args = NULL;
}

static void
setup(void)
{
app_startup();
init_libtest_filterx();
}

static void
teardown(void)
{
scratch_buffers_explicit_gc();
deinit_libtest_filterx();
app_shutdown();
}

TestSuite(filterx_func_unset_empties, .init = setup, .fini = teardown);

0 comments on commit 26e8f77

Please sign in to comment.