| 1 |
/* |
| 2 |
* Copyright (C) 2016 Red Hat, Inc. |
| 3 |
* SPDX-License-Identifier: LGPL-2.1-or-later |
| 4 |
*/ |
| 5 |
|
| 6 |
#include "config.h" |
| 7 |
|
| 8 |
#include "cockpitlocale.h" |
| 9 |
|
| 10 |
#include "testlib/cockpittest.h" |
| 11 |
|
| 12 |
#include <libintl.h> |
| 13 |
#include <string.h> |
| 14 |
|
| 15 |
typedef struct { |
| 16 |
const gchar *language; |
| 17 |
const gchar *encoding; |
| 18 |
const gchar *locale; |
| 19 |
const gchar *shorter; |
| 20 |
} FromFixture; |
| 21 |
|
| 22 |
static FromFixture from_fixtures[] = { |
| 23 |
{ "en", NULL, "en", "en" }, |
| 24 |
{ "en-us", NULL, "en_US", "en" }, |
| 25 |
{ "en-us", "UTF-8", "en_US.UTF-8", "en" }, |
| 26 |
{ "zh-cn", NULL, "zh_CN", "zh" }, |
| 27 |
{ "zh-cn", "UTF-8", "zh_CN.UTF-8", "zh" }, |
| 28 |
{ NULL, NULL, "C", "C" }, |
| 29 |
}; |
| 30 |
|
| 31 |
static void |
| 32 |
test_from_language (gconstpointer data) |
| 33 |
{ |
| 34 |
const FromFixture *fixture = data; |
| 35 |
gchar *locale; |
| 36 |
gchar *shorter; |
| 37 |
|
| 38 |
locale = cockpit_locale_from_language (fixture->language, fixture->encoding, &shorter); |
| 39 |
g_assert_cmpstr (locale, ==, fixture->locale); |
| 40 |
if (locale) |
| 41 |
g_assert_cmpstr (shorter, ==, fixture->shorter); |
| 42 |
g_free (locale); |
| 43 |
g_free (shorter); |
| 44 |
|
| 45 |
locale = cockpit_locale_from_language (fixture->language, fixture->encoding, NULL); |
| 46 |
g_assert_cmpstr (locale, ==, fixture->locale); |
| 47 |
g_free (locale); |
| 48 |
} |
| 49 |
|
| 50 |
int |
| 51 |
main (int argc, |
| 52 |
char *argv[]) |
| 53 |
{ |
| 54 |
gchar *name; |
| 55 |
gint i; |
| 56 |
|
| 57 |
g_unsetenv ("LANGUAGE"); |
| 58 |
g_unsetenv ("LANG"); |
| 59 |
g_unsetenv ("LC_ALL"); |
| 60 |
g_unsetenv ("LC_MESSAGES"); |
| 61 |
|
| 62 |
bindtextdomain ("test", BUILDDIR "/src/ws/mock-locale"); |
| 63 |
cockpit_test_init (&argc, &argv); |
| 64 |
|
| 65 |
for (i = 0; i < G_N_ELEMENTS (from_fixtures); i++) |
| 66 |
{ |
| 67 |
name = g_strdup_printf ("/locale/from-language/%s", from_fixtures[i].locale); |
| 68 |
g_test_add_data_func (name, from_fixtures + i, test_from_language); |
| 69 |
g_free (name); |
| 70 |
} |
| 71 |
|
| 72 |
return g_test_run (); |
| 73 |
} |