gstrutils: Set locale explicitly for search-utils test

The test checks `g_str_match_string()` function, which performs matches
based on user's locale. For this reason, some tests may fail, e.g., see
issue #868.

Now we explicitly set locale for each test, with C locale as a fallback
when the locale is not available.
This commit is contained in:
nightuser 2020-03-18 08:58:02 +00:00 committed by Philip Withnall
parent 417eb64852
commit 5466a64e28

View File

@ -7,74 +7,120 @@ typedef struct
{ {
const gchar *string; const gchar *string;
const gchar *prefix; const gchar *prefix;
const gchar *locale;
gboolean should_match; gboolean should_match;
} SearchTest; } SearchTest;
static void /* Test word separators and case */
test_search (void) SearchTest basic[] = {
{ { "Hello World", "he", "C", TRUE },
SearchTest tests[] = { "Hello World", "wo", "C", TRUE },
{ { "Hello World", "lo", "C", FALSE },
/* Test word separators and case */ { "Hello World", "ld", "C", FALSE },
{ "Hello World", "he", TRUE }, { "Hello-World", "wo", "C", TRUE },
{ "Hello World", "wo", TRUE }, { "HelloWorld", "wo", "C", FALSE },
{ "Hello World", "lo", FALSE }, { NULL, NULL, NULL, FALSE }
{ "Hello World", "ld", FALSE }, };
{ "Hello-World", "wo", TRUE },
{ "HelloWorld", "wo", FALSE },
/* Test composed chars (accentued letters) */ /* Test composed chars (accentued letters) */
{ "Jörgen", "jor", TRUE }, SearchTest composed[] = {
{ "Gaëtan", "gaetan", TRUE }, { "Jörgen", "jor", "sv_SE.UTF-8", TRUE },
{ "élève", "ele", TRUE }, { "Gaëtan", "gaetan", "fr_FR.UTF-8", TRUE },
{ "Azais", "AzaÏs", FALSE }, { "élève", "ele", "fr_FR.UTF-8", TRUE },
{ "AzaÏs", "Azais", TRUE }, { "Azais", "AzaÏs", "fr_FR.UTF-8", FALSE },
{ "AzaÏs", "Azais", "fr_FR.UTF-8", TRUE },
{ NULL, NULL, NULL, FALSE }
};
/* Test decomposed chars, they looks the same, but are actually /* Test decomposed chars, they looks the same, but are actually
* composed of multiple unicodes */ * composed of multiple unicodes */
{ "Jorgen", "Jör", FALSE }, SearchTest decomposed[] = {
{ "Jörgen", "jor", TRUE }, { "Jorgen", "Jör", "sv_SE.UTF-8", FALSE },
{ "Jörgen", "jor", "sv_SE.UTF-8", TRUE },
{ NULL, NULL, NULL, FALSE }
};
/* Turkish special case */ /* Turkish special case */
{ "İstanbul", "ist", TRUE }, SearchTest turkish[] = {
{ "Diyarbakır", "diyarbakir", TRUE }, { "İstanbul", "ist", "tr_TR.UTF-8", TRUE },
{ "Diyarbakır", "diyarbakir", "tr_TR.UTF-8", TRUE },
{ NULL, NULL, NULL, FALSE }
};
/* Multi words */ /* Test unicode chars when no locale is available */
{ "Xavier Claessens", "Xav Cla", TRUE }, SearchTest c_locale_unicode[] = {
{ "Xavier Claessens", "Cla Xav", TRUE }, { "Jörgen", "jor", "C", TRUE },
{ "Foo Bar Baz", " b ", TRUE }, { "Jorgen", "Jör", "C", FALSE },
{ "Foo Bar Baz", "bar bazz", FALSE }, { "Jörgen", "jor", "C", TRUE },
{ NULL, NULL, NULL, FALSE }
};
{ NULL, NULL, FALSE } /* Multi words */
}; SearchTest multi_words[] = {
{ "Xavier Claessens", "Xav Cla", "C", TRUE },
{ "Xavier Claessens", "Cla Xav", "C", TRUE },
{ "Foo Bar Baz", " b ", "C", TRUE },
{ "Foo Bar Baz", "bar bazz", "C", FALSE },
{ NULL, NULL, NULL, FALSE }
};
static void
test_search (gconstpointer d)
{
const SearchTest *tests = d;
guint i; guint i;
gboolean all_skipped = TRUE;
setlocale(LC_ALL, "");
g_debug ("Started"); g_debug ("Started");
for (i = 0; tests[i].string != NULL; i ++)
for (i = 0; tests[i].string != NULL; i++)
{ {
gboolean match; gboolean match;
gboolean ok; gboolean ok;
gboolean skipped;
if (setlocale (LC_ALL, tests[i].locale))
{
skipped = FALSE;
all_skipped = FALSE;
match = g_str_match_string (tests[i].prefix, tests[i].string, TRUE); match = g_str_match_string (tests[i].prefix, tests[i].string, TRUE);
ok = (match == tests[i].should_match); ok = (match == tests[i].should_match);
}
else
{
skipped = TRUE;
g_test_message ("Locale '%s' is unavailable", tests[i].locale);
}
g_debug ("'%s' - '%s' %s: %s", tests[i].prefix, tests[i].string, g_debug ("'%s' - '%s' %s: %s", tests[i].prefix, tests[i].string,
tests[i].should_match ? "should match" : "should NOT match", tests[i].should_match ? "should match" : "should NOT match",
ok ? "OK" : "FAILED"); skipped ? "SKIPPED" : ok ? "OK" : "FAILED");
g_assert (ok); g_assert (skipped || ok);
} }
if (all_skipped)
g_test_skip ("No locales for the test set are available");
} }
int int
main (int argc, main (int argc,
char **argv) char **argv)
{ {
gchar *user_locale;
g_test_init (&argc, &argv, NULL); g_test_init (&argc, &argv, NULL);
g_test_add_func ("/search", test_search); setlocale (LC_ALL, "");
user_locale = setlocale (LC_ALL, NULL);
g_debug ("Current user locale: %s", user_locale);
g_test_add_data_func ("/search/basic", basic, test_search);
g_test_add_data_func ("/search/composed", composed, test_search);
g_test_add_data_func ("/search/decomposed", decomposed, test_search);
g_test_add_data_func ("/search/turkish", turkish, test_search);
g_test_add_data_func ("/search/c_locale_unicode", c_locale_unicode, test_search);
g_test_add_data_func ("/search/multi_words", multi_words, test_search);
return g_test_run (); return g_test_run ();
} }