mirror of
https://gitlab.gnome.org/GNOME/glib.git
synced 2025-02-02 17:26:17 +01:00
Replace setlocale() with uselocale() in GSettings l10n tests
It's not safe to use setlocale() to mutate the locale in a threaded program. Lots of other tests still do this, and I'm not putting in the effort to fix them comprehensively in the absense of actual failures on CI, but I figured it'd be good to fix the tests that I was touching.
This commit is contained in:
parent
736000e49e
commit
306d4567d2
@ -1,3 +1,5 @@
|
|||||||
|
#include "config.h"
|
||||||
|
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <locale.h>
|
#include <locale.h>
|
||||||
#include <libintl.h>
|
#include <libintl.h>
|
||||||
@ -10,6 +12,11 @@
|
|||||||
|
|
||||||
#include "testenum.h"
|
#include "testenum.h"
|
||||||
|
|
||||||
|
#ifdef HAVE_XLOCALE_H
|
||||||
|
/* Needed on macOS and FreeBSD for uselocale() */
|
||||||
|
#include <xlocale.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
static const gchar *locale_dir = ".";
|
static const gchar *locale_dir = ".";
|
||||||
|
|
||||||
static gboolean backend_set;
|
static gboolean backend_set;
|
||||||
@ -747,7 +754,7 @@ test_atomic (void)
|
|||||||
* 1) The C library doesn't use LC_MESSAGES, that is implemented only
|
* 1) The C library doesn't use LC_MESSAGES, that is implemented only
|
||||||
* in libintl (defined in its <libintl.h>).
|
* in libintl (defined in its <libintl.h>).
|
||||||
*
|
*
|
||||||
* 2) The locale names that setlocale() accepts and returns aren't in
|
* 2) The locale names that uselocale() accepts and returns aren't in
|
||||||
* the "de_DE" style, but like "German_Germany".
|
* the "de_DE" style, but like "German_Germany".
|
||||||
*
|
*
|
||||||
* 3) libintl looks at the Win32 thread locale and not the C library
|
* 3) libintl looks at the Win32 thread locale and not the C library
|
||||||
@ -765,26 +772,46 @@ test_atomic (void)
|
|||||||
static void
|
static void
|
||||||
test_l10n (void)
|
test_l10n (void)
|
||||||
{
|
{
|
||||||
|
#ifndef HAVE_USELOCALE
|
||||||
|
g_test_skip ("Unsafe to change locale because platform does not support uselocale()");
|
||||||
|
#else
|
||||||
GSettings *settings;
|
GSettings *settings;
|
||||||
gchar *str;
|
gchar *str;
|
||||||
gchar *locale;
|
locale_t original_locale;
|
||||||
|
locale_t new_locale;
|
||||||
|
locale_t result;
|
||||||
|
|
||||||
bindtextdomain ("test", locale_dir);
|
bindtextdomain ("test", locale_dir);
|
||||||
bind_textdomain_codeset ("test", "UTF-8");
|
bind_textdomain_codeset ("test", "UTF-8");
|
||||||
|
|
||||||
locale = g_strdup (setlocale (LC_MESSAGES, NULL));
|
original_locale = uselocale ((locale_t) 0);
|
||||||
|
g_assert_true (original_locale != (locale_t) 0);
|
||||||
|
new_locale = newlocale (LC_MESSAGES_MASK, "C", (locale_t) 0);
|
||||||
|
g_assert_true (new_locale != (locale_t) 0);
|
||||||
|
result = uselocale (new_locale);
|
||||||
|
g_assert_true (result == original_locale);
|
||||||
|
|
||||||
settings = g_settings_new ("org.gtk.test.localized");
|
settings = g_settings_new ("org.gtk.test.localized");
|
||||||
|
|
||||||
setlocale (LC_MESSAGES, "C");
|
|
||||||
str = g_settings_get_string (settings, "error-message");
|
str = g_settings_get_string (settings, "error-message");
|
||||||
setlocale (LC_MESSAGES, locale);
|
|
||||||
|
result = uselocale (original_locale);
|
||||||
|
g_assert_true (result == new_locale);
|
||||||
|
freelocale (new_locale);
|
||||||
|
|
||||||
g_assert_cmpstr (str, ==, "Unnamed");
|
g_assert_cmpstr (str, ==, "Unnamed");
|
||||||
g_free (str);
|
g_free (str);
|
||||||
str = NULL;
|
str = NULL;
|
||||||
|
|
||||||
setlocale (LC_MESSAGES, "de_DE.UTF-8");
|
new_locale = newlocale (LC_MESSAGES_MASK, "de_DE.UTF-8", (locale_t) 0);
|
||||||
|
if (new_locale == (locale_t) 0)
|
||||||
|
{
|
||||||
|
g_test_skip ("Cannot run test becaues de_DE.UTF-8 locale is not available");
|
||||||
|
g_object_unref (settings);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
result = uselocale (new_locale);
|
||||||
|
g_assert_true (result == original_locale);
|
||||||
|
|
||||||
/* Only do the test if translation is actually working... */
|
/* Only do the test if translation is actually working... */
|
||||||
if (g_str_equal (dgettext ("test", "\"Unnamed\""), "\"Unbenannt\""))
|
if (g_str_equal (dgettext ("test", "\"Unnamed\""), "\"Unbenannt\""))
|
||||||
{
|
{
|
||||||
@ -799,9 +826,12 @@ test_l10n (void)
|
|||||||
g_test_skip ("translation is not working");
|
g_test_skip ("translation is not working");
|
||||||
}
|
}
|
||||||
|
|
||||||
setlocale (LC_MESSAGES, locale);
|
result = uselocale (original_locale);
|
||||||
g_free (locale);
|
g_assert_true (result == new_locale);
|
||||||
|
freelocale (new_locale);
|
||||||
|
|
||||||
g_object_unref (settings);
|
g_object_unref (settings);
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Test that message context works as expected with translated
|
/* Test that message context works as expected with translated
|
||||||
@ -814,44 +844,73 @@ test_l10n (void)
|
|||||||
static void
|
static void
|
||||||
test_l10n_context (void)
|
test_l10n_context (void)
|
||||||
{
|
{
|
||||||
|
#ifndef HAVE_USELOCALE
|
||||||
|
g_test_skip ("Unsafe to change locale because platform does not support uselocale()");
|
||||||
|
#else
|
||||||
GSettings *settings;
|
GSettings *settings;
|
||||||
gchar *str;
|
gchar *str;
|
||||||
gchar *locale;
|
locale_t original_locale;
|
||||||
|
locale_t new_locale;
|
||||||
|
locale_t result;
|
||||||
|
|
||||||
bindtextdomain ("test", locale_dir);
|
bindtextdomain ("test", locale_dir);
|
||||||
bind_textdomain_codeset ("test", "UTF-8");
|
bind_textdomain_codeset ("test", "UTF-8");
|
||||||
|
|
||||||
locale = g_strdup (setlocale (LC_MESSAGES, NULL));
|
|
||||||
|
|
||||||
settings = g_settings_new ("org.gtk.test.localized");
|
settings = g_settings_new ("org.gtk.test.localized");
|
||||||
|
|
||||||
setlocale (LC_MESSAGES, "C");
|
original_locale = uselocale ((locale_t) 0);
|
||||||
|
g_assert_true (original_locale != (locale_t) 0);
|
||||||
|
new_locale = newlocale (LC_MESSAGES_MASK, "C", (locale_t) 0);
|
||||||
|
g_assert_true (new_locale != (locale_t) 0);
|
||||||
|
result = uselocale (new_locale);
|
||||||
|
g_assert_true (result == original_locale);
|
||||||
|
|
||||||
g_settings_get (settings, "backspace", "s", &str);
|
g_settings_get (settings, "backspace", "s", &str);
|
||||||
setlocale (LC_MESSAGES, locale);
|
|
||||||
|
result = uselocale (original_locale);
|
||||||
|
g_assert_true (result == new_locale);
|
||||||
|
freelocale (new_locale);
|
||||||
|
|
||||||
g_assert_cmpstr (str, ==, "BackSpace");
|
g_assert_cmpstr (str, ==, "BackSpace");
|
||||||
g_free (str);
|
g_free (str);
|
||||||
str = NULL;
|
str = NULL;
|
||||||
|
|
||||||
setlocale (LC_MESSAGES, "de_DE.UTF-8");
|
new_locale = newlocale (LC_MESSAGES_MASK, "de_DE.UTF-8", (locale_t) 0);
|
||||||
|
if (new_locale == (locale_t) 0)
|
||||||
|
{
|
||||||
|
g_test_skip ("Cannot run test becaues de_DE.UTF-8 locale is not available");
|
||||||
|
g_object_unref (settings);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
result = uselocale (new_locale);
|
||||||
|
g_assert_true (result == original_locale);
|
||||||
|
|
||||||
/* Only do the test if translation is actually working... */
|
/* Only do the test if translation is actually working... */
|
||||||
if (g_str_equal (dgettext ("test", "\"Unnamed\""), "\"Unbenannt\""))
|
if (g_str_equal (dgettext ("test", "\"Unnamed\""), "\"Unbenannt\""))
|
||||||
settings_assert_cmpstr (settings, "backspace", ==, "Löschen");
|
settings_assert_cmpstr (settings, "backspace", ==, "Löschen");
|
||||||
else
|
else
|
||||||
g_test_skip ("translation is not working");
|
g_test_skip ("translation is not working");
|
||||||
|
|
||||||
setlocale (LC_MESSAGES, locale);
|
result = uselocale (original_locale);
|
||||||
g_free (locale);
|
g_assert_true (result == new_locale);
|
||||||
|
freelocale (new_locale);
|
||||||
|
|
||||||
g_object_unref (settings);
|
g_object_unref (settings);
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Test use of l10n="time" and LC_TIME. */
|
/* Test use of l10n="time" and LC_TIME. */
|
||||||
static void
|
static void
|
||||||
test_l10n_time (void)
|
test_l10n_time (void)
|
||||||
{
|
{
|
||||||
|
#ifndef HAVE_USELOCALE
|
||||||
|
g_test_skip ("Unsafe to change locale because platform does not support uselocale()");
|
||||||
|
#else
|
||||||
GSettings *settings;
|
GSettings *settings;
|
||||||
gchar *str;
|
gchar *str;
|
||||||
gchar *locale;
|
locale_t original_locale;
|
||||||
|
locale_t new_locale;
|
||||||
|
locale_t result;
|
||||||
|
|
||||||
g_test_summary ("Test that l10n='time' attribute uses the correct category for translations");
|
g_test_summary ("Test that l10n='time' attribute uses the correct category for translations");
|
||||||
g_test_bug ("https://gitlab.gnome.org/GNOME/glib/-/issues/2575");
|
g_test_bug ("https://gitlab.gnome.org/GNOME/glib/-/issues/2575");
|
||||||
@ -859,19 +918,36 @@ test_l10n_time (void)
|
|||||||
bindtextdomain ("test", locale_dir);
|
bindtextdomain ("test", locale_dir);
|
||||||
bind_textdomain_codeset ("test", "UTF-8");
|
bind_textdomain_codeset ("test", "UTF-8");
|
||||||
|
|
||||||
locale = g_strdup (setlocale (LC_TIME, NULL));
|
|
||||||
|
|
||||||
settings = g_settings_new ("org.gtk.test.localized");
|
settings = g_settings_new ("org.gtk.test.localized");
|
||||||
|
|
||||||
setlocale (LC_TIME, "C");
|
original_locale = uselocale ((locale_t) 0);
|
||||||
|
g_assert_true (original_locale != (locale_t) 0);
|
||||||
|
new_locale = duplocale (original_locale);
|
||||||
|
g_assert_true (new_locale != (locale_t) 0);
|
||||||
|
new_locale = newlocale (LC_TIME_MASK, "C", new_locale);
|
||||||
|
g_assert_true (new_locale != (locale_t) 0);
|
||||||
|
result = uselocale (new_locale);
|
||||||
|
g_assert_true (result == original_locale);
|
||||||
|
|
||||||
str = g_settings_get_string (settings, "midnight");
|
str = g_settings_get_string (settings, "midnight");
|
||||||
setlocale (LC_TIME, locale);
|
|
||||||
|
result = uselocale (original_locale);
|
||||||
|
g_assert_true (result == new_locale);
|
||||||
|
|
||||||
g_assert_cmpstr (str, ==, "12:00 AM");
|
g_assert_cmpstr (str, ==, "12:00 AM");
|
||||||
g_free (str);
|
g_free (str);
|
||||||
str = NULL;
|
str = NULL;
|
||||||
|
|
||||||
setlocale (LC_TIME, "de_DE.UTF-8");
|
new_locale = newlocale (LC_TIME_MASK, "de_DE.UTF-8", new_locale);
|
||||||
|
if (new_locale == (locale_t) 0)
|
||||||
|
{
|
||||||
|
g_test_skip ("Cannot run test becaues de_DE.UTF-8 locale is not available");
|
||||||
|
g_object_unref (settings);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
result = uselocale (new_locale);
|
||||||
|
g_assert_true (result != (locale_t) 0);
|
||||||
|
|
||||||
/* Only do the test if translation is actually working... */
|
/* Only do the test if translation is actually working... */
|
||||||
if (g_str_equal (dgettext ("test", "\"12:00 AM\""), "\"00:00\""))
|
if (g_str_equal (dgettext ("test", "\"12:00 AM\""), "\"00:00\""))
|
||||||
{
|
{
|
||||||
@ -886,9 +962,12 @@ test_l10n_time (void)
|
|||||||
g_test_skip ("translation is not working");
|
g_test_skip ("translation is not working");
|
||||||
}
|
}
|
||||||
|
|
||||||
setlocale (LC_TIME, locale);
|
result = uselocale (original_locale);
|
||||||
g_free (locale);
|
g_assert_true (result == new_locale);
|
||||||
|
freelocale (new_locale);
|
||||||
|
|
||||||
g_object_unref (settings);
|
g_object_unref (settings);
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
enum
|
enum
|
||||||
|
Loading…
Reference in New Issue
Block a user