From 4b64a64124d7481bfdb44db8a8e854fe7b141f60 Mon Sep 17 00:00:00 2001 From: Marek Kasik Date: Wed, 30 Jul 2014 14:38:50 +0200 Subject: [PATCH] printers: Add test for canonicalize_device_name() Add a test which tests function canonicalize_device_name() which canonicalize name of printer model so it doesn't contain unwanted strings/characters. Test data are in canonicalization-test.txt file. https://bugzilla.gnome.org/show_bug.cgi?id=695564 --- panels/printers/Makefile.am | 11 ++- panels/printers/canonicalization-test.txt | 16 ++++ panels/printers/test-canonicalization.c | 112 ++++++++++++++++++++++ 3 files changed, 136 insertions(+), 3 deletions(-) create mode 100644 panels/printers/canonicalization-test.txt create mode 100644 panels/printers/test-canonicalization.c diff --git a/panels/printers/Makefile.am b/panels/printers/Makefile.am index 590601af2..148349b5a 100644 --- a/panels/printers/Makefile.am +++ b/panels/printers/Makefile.am @@ -66,12 +66,17 @@ 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 +noinst_PROGRAMS = test-shift test-canonicalization test_shift_SOURCES = pp-utils.c pp-utils.h test-shift.c test_shift_LDADD = $(PANEL_LIBS) $(PRINTERS_PANEL_LIBS) $(CUPS_LIBS) +test_canonicalization_SOURCES = pp-utils.c pp-utils.h test-canonicalization.c +test_canonicalization_LDADD = $(PANEL_LIBS) $(PRINTERS_PANEL_LIBS) $(CUPS_LIBS) -EXTRA_DIST += shift-test.txt -check-local: test-shift +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/canonicalization-test.txt b/panels/printers/canonicalization-test.txt new file mode 100644 index 000000000..10ceee17d --- /dev/null +++ b/panels/printers/canonicalization-test.txt @@ -0,0 +1,16 @@ +# already_present_printer1, space, already_present_printer2, ...., tab, +# new device id, tab, +# new device make and model, tab, +# new device original name, tab, +# new device info, tab, +# canonicalized name +# - stripped leading and trailing spaces +# - characters not in ALLOWED_CHARACTERS replaced by '-' +# - everything behind strings in residues removed +# - leading and trailing '-' removed +# - recurring '-' merged +# - indexed if the name is already in any of given lists +Canon-BJ-30 Canon-BJ-30-2 Canon BJ-30 - CUPS+Gutenprint v5.2.9 Simplified Canon-BJ-30-3 +Canon-BJ-30 HP Business Inkjet 1000, hpcups 3.14.6 HP-Business-Inkjet-1000 + ...A---strange;;printer ;;;;info A-strange-printer-info +HP-Something MFG:HP;MDL:Officejet 7300 series; HP Officejet 7300 series Officejet 7300 series Officejet-7300 diff --git a/panels/printers/test-canonicalization.c b/panels/printers/test-canonicalization.c new file mode 100644 index 000000000..596e6fa53 --- /dev/null +++ b/panels/printers/test-canonicalization.c @@ -0,0 +1,112 @@ +#include "config.h" + +#include +#include +#include + +#include "pp-utils.h" + +int +main (int argc, char **argv) +{ + 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; + } + + for (i = 0; lines[i] != NULL; i++) + { + char **items; + + if (*lines[i] == '#') + continue; + + if (*lines[i] == '\0') + break; + + items = g_strsplit (lines[i], "\t", -1); + if (g_strv_length (items) == 6) + { + PpPrintDevice *device, *tmp; + gchar **already_present_printers; + gchar *canonicalized_name; + GList *devices = NULL; + + already_present_printers = g_strsplit (items[0], " ", -1); + + for (j = 0; already_present_printers[j] != NULL; j++) + { + tmp = g_new0 (PpPrintDevice, 1); + tmp->device_original_name = g_strdup (already_present_printers[j]); + + devices = g_list_append (devices, tmp); + } + + device = g_new0 (PpPrintDevice, 1); + device->device_id = g_strdup (items[1]); + device->device_make_and_model = g_strdup (items[2]); + device->device_original_name = g_strdup (items[3]); + device->device_info = g_strdup (items[4]); + + canonicalized_name = + canonicalize_device_name (devices, NULL, NULL, 0, device); + + if (g_strcmp0 (canonicalized_name, items[5]) != 0) + { + 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); + } + else + { + g_debug ("Result for ('%s', '%s', '%s', '%s') matches '%s'", + items[1], items[2], items[3], items[4], canonicalized_name); + } + + g_free (canonicalized_name); + pp_print_device_free (device); + g_list_free_full (devices, (GDestroyNotify) pp_print_device_free); + g_strfreev (already_present_printers); + } + else + { + g_warning ("Line number %u has not correct number of items!", i); + } + + g_strfreev (items); + } + + g_strfreev (lines); + g_free (contents); + + return 0; +}