diff --git a/lib/common/tests/strings/Makefile.am b/lib/common/tests/strings/Makefile.am index 33f8b98d12..1d6c7aabbe 100644 --- a/lib/common/tests/strings/Makefile.am +++ b/lib/common/tests/strings/Makefile.am @@ -1,38 +1,39 @@ # # Copyright 2020-2021 the Pacemaker project contributors # # The version control history for this file may have further details. # # This source code is licensed under the GNU General Public License version 2 # or later (GPLv2+) WITHOUT ANY WARRANTY. # AM_CPPFLAGS = -I$(top_srcdir)/include -I$(top_builddir)/include LDADD = $(top_builddir)/lib/common/libcrmcommon.la include $(top_srcdir)/mk/glib-tap.mk # Add each test program here. Each test should be written as a little standalone # program using the glib unit testing functions. See the documentation for more # information. # # https://developer.gnome.org/glib/unstable/glib-Testing.html # # Add "_test" to the end of all test program names to simplify .gitignore. -test_programs = crm_str_to_boolean_test \ +test_programs = crm_is_true_test \ + crm_str_to_boolean_test \ pcmk__add_word_test \ pcmk__btoa_test \ pcmk__char_in_any_str_test \ pcmk__parse_ll_range_test \ pcmk__scan_double_test \ pcmk__str_any_of_test \ pcmk__str_in_list_test \ pcmk__strcmp_test # If any extra data needs to be added to the source distribution, add it to the # following list. dist_test_data = # If any extra data needs to be used by tests but should not be added to the # source distribution, add it to the following list. test_data = diff --git a/lib/common/tests/strings/crm_is_true_test.c b/lib/common/tests/strings/crm_is_true_test.c new file mode 100644 index 0000000000..20def0d9b7 --- /dev/null +++ b/lib/common/tests/strings/crm_is_true_test.c @@ -0,0 +1,61 @@ +/* + * Copyright 2021 the Pacemaker project contributors + * + * The version control history for this file may have further details. + * + * This source code is licensed under the GNU Lesser General Public License + * version 2.1 or later (LGPLv2.1+) WITHOUT ANY WARRANTY. + */ + +#include + +static void +bad_input(void) { + g_assert_false(crm_is_true(NULL)); +} + +static void +is_true(void) { + g_assert_true(crm_is_true("true")); + g_assert_true(crm_is_true("TrUe")); + g_assert_true(crm_is_true("on")); + g_assert_true(crm_is_true("ON")); + g_assert_true(crm_is_true("yes")); + g_assert_true(crm_is_true("yES")); + g_assert_true(crm_is_true("y")); + g_assert_true(crm_is_true("Y")); + g_assert_true(crm_is_true("1")); +} + +static void +is_false(void) { + g_assert_false(crm_is_true("false")); + g_assert_false(crm_is_true("fAlSe")); + g_assert_false(crm_is_true("off")); + g_assert_false(crm_is_true("OFF")); + g_assert_false(crm_is_true("no")); + g_assert_false(crm_is_true("No")); + g_assert_false(crm_is_true("n")); + g_assert_false(crm_is_true("N")); + g_assert_false(crm_is_true("0")); + + g_assert_false(crm_is_true("")); + g_assert_false(crm_is_true("blahblah")); + + g_assert_false(crm_is_true("truedat")); + g_assert_false(crm_is_true("onnn")); + g_assert_false(crm_is_true("yep")); + g_assert_false(crm_is_true("Y!")); + g_assert_false(crm_is_true("100")); +} + +int +main(int argc, char **argv) +{ + g_test_init(&argc, &argv, NULL); + + g_test_add_func("/common/strings/crm_is_true/bad_input", bad_input); + g_test_add_func("/common/strings/crm_is_true/is_true", is_true); + g_test_add_func("/common/strings/crm_is_true/is_false", is_false); + return g_test_run(); +}