From e48b3cc061d3ee717248e738dc94985f907793be Mon Sep 17 00:00:00 2001 From: Bastien Nocera Date: Fri, 10 Jun 2016 14:57:04 +0200 Subject: [PATCH] printers: Port tests to GTest --- panels/printers/Makefile.am | 11 ++-- panels/printers/test-canonicalization.c | 73 ++++++++++++++----------- panels/printers/test-shift.c | 71 ++++++++++++++---------- 3 files changed, 89 insertions(+), 66 deletions(-) diff --git a/panels/printers/Makefile.am b/panels/printers/Makefile.am index 7fb2c17eb..938e3b008 100644 --- a/panels/printers/Makefile.am +++ b/panels/printers/Makefile.am @@ -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 diff --git a/panels/printers/test-canonicalization.c b/panels/printers/test-canonicalization.c index 1500f681f..d5556600f 100644 --- a/panels/printers/test-canonicalization.c +++ b/panels/printers/test-canonicalization.c @@ -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 (); } diff --git a/panels/printers/test-shift.c b/panels/printers/test-shift.c index 630ae1665..e85fe9af3 100644 --- a/panels/printers/test-shift.c +++ b/panels/printers/test-shift.c @@ -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 (); }