From 4c32ffecd544b73299af41b30a078d1e0565b2a3 Mon Sep 17 00:00:00 2001 From: Simon McVittie Date: Tue, 14 Nov 2023 16:16:13 +0000 Subject: [PATCH] tests: Expand test coverage for flatpak_get_lang_from_locale() Signed-off-by: Simon McVittie --- tests/Makefile.am.inc | 5 +++ tests/meson.build | 1 + tests/test-locale-utils.c | 64 +++++++++++++++++++++++++++++++++++++++ tests/testcommon.c | 14 --------- 4 files changed, 70 insertions(+), 14 deletions(-) create mode 100644 tests/test-locale-utils.c diff --git a/tests/Makefile.am.inc b/tests/Makefile.am.inc index 728683e1..847eac0b 100644 --- a/tests/Makefile.am.inc +++ b/tests/Makefile.am.inc @@ -120,6 +120,10 @@ test_instance_SOURCES = \ tests/test-instance.c \ $(NULL) +test_locale_utils_CFLAGS = $(testcommon_CFLAGS) +test_locale_utils_LDADD = $(testcommon_LDADD) libtestlib.la +test_locale_utils_SOURCES = tests/test-locale-utils.c + test_portal_CFLAGS = $(testcommon_CFLAGS) test_portal_LDADD = $(testcommon_LDADD) libtestlib.la test_portal_SOURCES = \ @@ -300,6 +304,7 @@ test_programs = \ test-context \ test-exports \ test-instance \ + test-locale-utils \ test-portal \ testapp \ testcommon \ diff --git a/tests/meson.build b/tests/meson.build index aeff5362..4eacb724 100644 --- a/tests/meson.build +++ b/tests/meson.build @@ -108,6 +108,7 @@ c_tests = [ libglnx_testlib_dep, ], }], + ['test-locale-utils', {}], ['test-portal', { 'extra_sources' : [ portal_gdbus[0], diff --git a/tests/test-locale-utils.c b/tests/test-locale-utils.c new file mode 100644 index 00000000..a6da7ea3 --- /dev/null +++ b/tests/test-locale-utils.c @@ -0,0 +1,64 @@ +/* + * Copyright 2023 Collabora Ltd. + * SPDX-License-Identifier: LGPL-2.1-or-later + */ + +#include "config.h" + +#include +#include "libglnx.h" +#include "flatpak.h" + +#include "flatpak-utils-private.h" + +#include "tests/testlib.h" + +static const struct +{ + const char *in; + const char *expected; +} lang_from_locale_tests[] = +{ + { "C", NULL }, + { "C.UTF-8", NULL }, + { "en.ISO-8859-15", "en" }, + { "en@cantab", "en" }, + { "en_GB", "en" }, + { "en_US.utf8", "en" }, + { "sv_FI@euro", "sv" }, +}; + +static void +test_lang_from_locale (void) +{ + gsize i; + + for (i = 0; i < G_N_ELEMENTS (lang_from_locale_tests); i++) + { + const char *in = lang_from_locale_tests[i].in; + const char *expected = lang_from_locale_tests[i].expected; + g_autofree char *actual = NULL; + + g_test_message ("%s", in); + actual = flatpak_get_lang_from_locale (in); + g_test_message ("-> %s", actual ? actual : "(null)"); + g_assert_cmpstr (actual, ==, expected); + } +} + +int +main (int argc, char *argv[]) +{ + int res; + + g_test_init (&argc, &argv, NULL); + isolated_test_dir_global_setup (); + + g_test_add_func ("/locale/lang-from-locale", test_lang_from_locale); + + res = g_test_run (); + + isolated_test_dir_global_teardown (); + + return res; +} diff --git a/tests/testcommon.c b/tests/testcommon.c index 11a00769..23e126f0 100644 --- a/tests/testcommon.c +++ b/tests/testcommon.c @@ -632,19 +632,6 @@ test_subpaths_merge (void) g_clear_pointer (&res, g_strfreev); } -static void -test_lang_from_locale (void) -{ - g_autofree char *lang = NULL; - - lang = flatpak_get_lang_from_locale ("en_US.utf8"); - g_assert_cmpstr (lang, ==, "en"); - g_clear_pointer (&lang, g_free); - - lang = flatpak_get_lang_from_locale ("sv_FI@euro"); - g_assert_cmpstr (lang, ==, "sv"); -} - static void test_parse_appdata (void) { @@ -1376,7 +1363,6 @@ main (int argc, char *argv[]) g_test_add_func ("/common/string-to-unsigned", test_string_to_unsigned); g_test_add_func ("/common/levenshtein", test_levenshtein); g_test_add_func ("/common/subpaths-merge", test_subpaths_merge); - g_test_add_func ("/common/lang-from-locale", test_lang_from_locale); g_test_add_func ("/common/appdata", test_parse_appdata); g_test_add_func ("/common/name-matching", test_name_matching); g_test_add_func ("/common/filter_parser", test_filter_parser);