printers: Port tests to GTest

This commit is contained in:
Bastien Nocera 2016-06-10 14:57:04 +02:00
parent 1428b52fdc
commit e48b3cc061
3 changed files with 89 additions and 66 deletions

View file

@ -1,3 +1,5 @@
include $(top_srcdir)/Makefile.decl
cappletname = printers
AM_CPPFLAGS = \
@ -7,6 +9,7 @@ AM_CPPFLAGS = \
$(SMBCLIENT_CFLAGS) \
-I$(top_srcdir)/shell/ \
-DGNOMELOCALEDIR="\"$(datadir)/locale\"" \
-DTEST_SRCDIR=\""$(srcdir)/"\" \
$(NULL)
noinst_LTLIBRARIES = libprinters.la
@ -68,17 +71,15 @@ desktop_DATA = $(desktop_in_files:.desktop.in=.desktop)
CLEANFILES = $(desktop_in_files) $(desktop_DATA) $(BUILT_SOURCES)
EXTRA_DIST = $(resource_files) printers.gresource.xml
noinst_PROGRAMS = test-shift test-canonicalization
noinst_PROGRAMS = $(TEST_PROGS)
TEST_PROGS += test-shift test-canonicalization
test_shift_SOURCES = pp-print-device.c pp-print-device.h pp-utils.c pp-utils.h test-shift.c
test_shift_LDADD = $(PANEL_LIBS) $(PRINTERS_PANEL_LIBS) $(CUPS_LIBS)
test_canonicalization_SOURCES = pp-print-device.c pp-print-device.h pp-utils.c pp-utils.h test-canonicalization.c
test_canonicalization_LDADD = $(PANEL_LIBS) $(PRINTERS_PANEL_LIBS) $(CUPS_LIBS)
EXTRA_DIST += \
EXTRA_DIST += \
shift-test.txt \
canonicalization-test.txt
check-local: test-shift test-canonicalization
$(builddir)/test-shift $(srcdir)/shift-test.txt > /dev/null
$(builddir)/test-canonicalization $(srcdir)/canonicalization-test.txt > /dev/null
-include $(top_srcdir)/git.mk

View file

@ -6,42 +6,19 @@
#include "pp-utils.h"
int
main (int argc, char **argv)
static void
test_canonicalization (gconstpointer data)
{
const char *contents = data;
guint i, j;
char *contents;
char **lines;
char *locale;
/* Running in some locales will
* break the tests as "ü" will be transliterated to
* "ue" in de_DE, and 'u"' in the C locale.
*
* Work around that by forcing en_US with UTF-8 in
* our tests
* https://bugzilla.gnome.org/show_bug.cgi?id=650342 */
locale = setlocale (LC_ALL, "en_US.UTF-8");
if (locale == NULL)
{
g_debug("Missing en_US.UTF-8 locale, ignoring test.");
return 0;
}
bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
if (g_file_get_contents (argv[1], &contents, NULL, NULL) == FALSE)
{
g_warning ("Failed to load '%s'", argv[1]);
return 1;
}
lines = g_strsplit (contents, "\n", -1);
if (lines == NULL)
{
g_warning ("Test file is empty");
return 1;
g_test_fail ();
return;
}
for (i = 0; lines[i] != NULL; i++)
@ -81,6 +58,7 @@ main (int argc, char **argv)
{
g_error ("Result for ('%s', '%s', '%s', '%s') doesn't match '%s' (got: '%s')",
items[1], items[2], items[3], items[4], items[5], canonicalized_name);
g_test_fail ();
}
else
{
@ -96,13 +74,46 @@ main (int argc, char **argv)
else
{
g_warning ("Line number %u has not correct number of items!", i);
g_test_fail ();
}
g_strfreev (items);
}
g_strfreev (lines);
g_free (contents);
return 0;
}
int
main (int argc, char **argv)
{
char *locale;
char *contents;
/* Running in some locales will
* break the tests as "ü" will be transliterated to
* "ue" in de_DE, and 'u"' in the C locale.
*
* Work around that by forcing en_US with UTF-8 in
* our tests
* https://bugzilla.gnome.org/show_bug.cgi?id=650342 */
locale = setlocale (LC_ALL, "en_US.UTF-8");
if (locale == NULL)
{
g_debug("Missing en_US.UTF-8 locale, ignoring test.");
return 0;
}
bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
g_test_init (&argc, &argv, NULL);
if (g_file_get_contents (TEST_SRCDIR "/canonicalization-test.txt", &contents, NULL, NULL) == FALSE)
{
g_warning ("Failed to load '%s'", TEST_SRCDIR "/canonicalization-test.txt");
return 1;
}
g_test_add_data_func ("/printers/canonicalization", contents, test_canonicalization);
return g_test_run ();
}

View file

@ -6,43 +6,20 @@
#include "pp-utils.h"
int
main (int argc, char **argv)
static void
test_shift (gconstpointer data)
{
const char *contents = data;
guint i;
char *str;
char *contents;
char **lines;
char *locale;
/* Running in some locales will
* break the tests as "ü" will be transliterated to
* "ue" in de_DE, and 'u"' in the C locale.
*
* Work around that by forcing en_US with UTF-8 in
* our tests
* https://bugzilla.gnome.org/show_bug.cgi?id=650342 */
locale = setlocale (LC_ALL, "en_US.UTF-8");
if (locale == NULL)
{
g_debug("Missing en_US.UTF-8 locale, ignoring test.");
return 0;
}
bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
if (g_file_get_contents (argv[1], &contents, NULL, NULL) == FALSE)
{
g_warning ("Failed to load '%s'", argv[1]);
return 1;
}
lines = g_strsplit (contents, "\n", -1);
if (lines == NULL)
{
g_warning ("Test file is empty");
return 1;
g_test_fail ();
return;
}
for (i = 0; lines[i] != NULL; i++)
@ -64,6 +41,7 @@ main (int argc, char **argv)
{
g_error ("Result for '%s' doesn't match '%s' (got: '%s')",
utf8, items[1], str);
g_test_fail ();
}
else
{
@ -78,7 +56,40 @@ main (int argc, char **argv)
}
g_strfreev (lines);
g_free (contents);
return 0;
}
int
main (int argc, char **argv)
{
char *locale;
char *contents;
/* Running in some locales will
* break the tests as "ü" will be transliterated to
* "ue" in de_DE, and 'u"' in the C locale.
*
* Work around that by forcing en_US with UTF-8 in
* our tests
* https://bugzilla.gnome.org/show_bug.cgi?id=650342 */
locale = setlocale (LC_ALL, "en_US.UTF-8");
if (locale == NULL)
{
g_debug("Missing en_US.UTF-8 locale, ignoring test.");
return 0;
}
bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
g_test_init (&argc, &argv, NULL);
if (g_file_get_contents (TEST_SRCDIR "/shift-test.txt", &contents, NULL, NULL) == FALSE)
{
g_warning ("Failed to load '%s'", TEST_SRCDIR "/shift-test.txt");
return 1;
}
g_test_add_data_func ("/printers/shift", contents, test_shift);
return g_test_run ();
}