Skip to content

Commit

Permalink
filterx: type_registry tests
Browse files Browse the repository at this point in the history
Signed-off-by: shifter <[email protected]>
  • Loading branch information
bshifter committed Apr 11, 2024
1 parent c358964 commit 3c35708
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 1 deletion.
1 change: 1 addition & 0 deletions lib/filterx/tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ add_unit_test(LIBTEST CRITERION TARGET test_object_protobuf DEPENDS json-plugin
add_unit_test(LIBTEST CRITERION TARGET test_object_boolean DEPENDS json-plugin ${JSONC_LIBRARY})
add_unit_test(LIBTEST CRITERION TARGET test_object_integer DEPENDS json-plugin ${JSONC_LIBRARY})
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})
6 changes: 5 additions & 1 deletion lib/filterx/tests/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ lib_filterx_tests_TESTS = \
lib/filterx/tests/test_filterx_expr \
lib/filterx/tests/test_expr_comparison \
lib/filterx/tests/test_expr_condition \
lib/filterx/tests/test_builtin_functions
lib/filterx/tests/test_builtin_functions \
lib/filterx/tests/test_type_registry

EXTRA_DIST += lib/filterx/tests/CMakeLists.txt \
lib/filterx/tests/filterx-lib.h
Expand Down Expand Up @@ -59,3 +60,6 @@ lib_filterx_tests_test_object_integer_LDADD = $(TEST_LDADD) $(JSON_LIBS)

lib_filterx_tests_test_object_double_CFLAGS = $(TEST_CFLAGS)
lib_filterx_tests_test_object_double_LDADD = $(TEST_LDADD) $(JSON_LIBS)

lib_filterx_tests_test_type_registry_CFLAGS = $(TEST_CFLAGS)
lib_filterx_tests_test_type_registry_LDADD = $(TEST_LDADD) $(JSON_LIBS)
80 changes: 80 additions & 0 deletions lib/filterx/tests/test_type_registry.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/*
* Copyright (c) 2024 shifter <[email protected]>
*
* 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/cr_template.h"

#include "filterx/filterx-object.h"
#include "filterx/filterx-private.h"

#include "apphook.h"


FILTERX_DEFINE_TYPE(dummy_base, FILTERX_TYPE_NAME(object));

Test(type_registry, test_type_registry_registering_existing_key_returns_false)
{
GHashTable *ht;
filterx_types_init_private(&ht);
// first register returns TRUE
cr_assert(filterx_type_register_private(ht, "base", &FILTERX_TYPE_NAME(dummy_base)));
// second attampt of register must return FALSE
cr_assert(!filterx_type_register_private(ht, "base", &FILTERX_TYPE_NAME(dummy_base)));
filterx_types_deinit_private(ht);
}

Test(type_registry, test_type_registry_lookup)
{
GHashTable *ht;
filterx_types_init_private(&ht);

// type not found
FilterXType *fxtype = filterx_type_lookup_private(ht, "base");
cr_assert(fxtype == NULL);

// add dummy function
cr_assert(filterx_type_register_private(ht, "base", &FILTERX_TYPE_NAME(dummy_base)));

// lookup returns dummy function
fxtype = filterx_type_lookup_private(ht, "base");
cr_assert(fxtype != NULL);

cr_assert_eq(&FILTERX_TYPE_NAME(dummy_base), fxtype);

filterx_types_deinit_private(ht);
}

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

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

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

0 comments on commit 3c35708

Please sign in to comment.