Skip to content

Commit

Permalink
filterx: add unit tests for keys function
Browse files Browse the repository at this point in the history
Signed-off-by: shifter <[email protected]>
  • Loading branch information
bshifter committed Dec 30, 2024
1 parent 7089d4c commit 5ca6983
Show file tree
Hide file tree
Showing 3 changed files with 136 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 @@ -27,3 +27,4 @@ add_unit_test(LIBTEST CRITERION TARGET test_metrics_labels DEPENDS json-plugin $
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})
add_unit_test(LIBTEST CRITERION TARGET test_object_dict_interface DEPENDS json-plugin ${JSONC_LIBRARY})
add_unit_test(LIBTEST CRITERION TARGET test_func_keys 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 @@ -30,6 +30,7 @@ lib_filterx_tests_TESTS = \
lib/filterx/tests/test_expr_plus \
lib/filterx/tests/test_metrics_labels \
lib/filterx/tests/test_object_dict_interface \
lib/filterx/tests/test_func_keys

EXTRA_DIST += lib/filterx/tests/CMakeLists.txt

Expand Down Expand Up @@ -121,3 +122,6 @@ lib_filterx_tests_test_metrics_labels_LDADD = $(TEST_LDADD) $(JSON_LIBS)

lib_filterx_tests_test_object_dict_interface_CFLAGS = $(TEST_CFLAGS)
lib_filterx_tests_test_object_dict_interface_LDADD = $(TEST_LDADD) $(JSON_LIBS)

lib_filterx_tests_test_func_keys_CFLAGS = $(TEST_CFLAGS)
lib_filterx_tests_test_func_keys_LDADD = $(TEST_LDADD) $(JSON_LIBS)
131 changes: 131 additions & 0 deletions lib/filterx/tests/test_func_keys.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
/*
* Copyright (c) 2024 Axoflow
* Copyright (c) 2024 shifter
*
* 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-flatten.h"
#include "filterx/object-string.h"
#include "filterx/object-primitive.h"
#include "filterx/object-json.h"
#include "filterx/expr-literal.h"
#include "filterx/func-keys.h"
#include "filterx/filterx-eval.h"
#include "filterx/object-dict-interface.h"
#include "filterx/object-list-interface.h"

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

Test(filterx_func_keys, empty_args)
{
GList *args = NULL;

GError *error = NULL;
FilterXExpr *fn = filterx_function_keys_new(filterx_function_args_new(args, NULL), &error);
cr_assert_null(fn);
cr_assert_not_null(error);

cr_assert_str_eq(error->message, FILTERX_FUNC_KEYS_ERR_NUM_ARGS FILTERX_FUNC_KEYS_USAGE);
g_clear_error(&error);
}

Test(filterx_func_keys, invalid_args_number)
{
GList *args = NULL;
args = g_list_append(args, filterx_function_arg_new(NULL, filterx_non_literal_new(filterx_string_new("not a dict",
-1))));
args = g_list_append(args, filterx_function_arg_new(NULL, filterx_non_literal_new(filterx_string_new("more args",
-1))));

GError *error = NULL;
FilterXExpr *fn = filterx_function_keys_new(filterx_function_args_new(args, NULL), &error);
cr_assert_null(fn);
cr_assert_not_null(error);

cr_assert_str_eq(error->message, FILTERX_FUNC_KEYS_ERR_NUM_ARGS FILTERX_FUNC_KEYS_USAGE);
g_clear_error(&error);
}

Test(filterx_func_keys, invalid_arg_type)
{
GList *args = NULL;
args = g_list_append(args, filterx_function_arg_new(NULL, filterx_non_literal_new(filterx_string_new("not a dict",
-1))));

GError *error = NULL;
FilterXExpr *fn = filterx_function_keys_new(filterx_function_args_new(args, NULL), &error);
cr_assert_not_null(fn);
cr_assert_null(error);
FilterXObject *res = filterx_expr_eval(fn);
cr_assert_null(res);

const gchar *last_error = filterx_eval_get_last_error();

cr_assert_str_eq(last_error, FILTERX_FUNC_KEYS_ERR_NONDICT FILTERX_FUNC_KEYS_USAGE);

filterx_expr_unref(fn);
g_clear_error(&error);
}

Test(filterx_func_keys, valid_input)
{
GList *args = NULL;
FilterXObject *dict = filterx_json_new_from_repr("{\"foo\":1,\"bar\":[3,2,1],\"baz\":{\"tik\":\"tak\"}}", -1);
args = g_list_append(args, filterx_function_arg_new(NULL, filterx_non_literal_new(dict)));

GError *error = NULL;
FilterXExpr *fn = filterx_function_keys_new(filterx_function_args_new(args, NULL), &error);
cr_assert_not_null(fn);
cr_assert_null(error);
FilterXObject *res = filterx_expr_eval(fn);
cr_assert_not_null(res);
cr_assert(filterx_object_is_type(res, &FILTERX_TYPE_NAME(list)));

GString *repr = scratch_buffers_alloc();
cr_assert(filterx_object_repr(res, repr));

cr_assert_str_eq(repr->str, "[\"foo\",\"bar\",\"baz\"]");

filterx_object_unref(res);
filterx_expr_unref(fn);
g_clear_error(&error);
}

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_keys, .init = setup, .fini = teardown);

0 comments on commit 5ca6983

Please sign in to comment.