[gnome-control-center] info: Split test cases into separate file



commit 11571bf609659008a0093abaf71ded91b9c98c6a
Author: Bastien Nocera <hadess hadess net>
Date:   Tue May 17 15:19:46 2011 +0100

    info: Split test cases into separate file
    
    This fixes errors when run in non-UTF-8 locales.

 panels/info/Makefile.am        |    4 ++-
 panels/info/hostnames-test.txt |   11 +++++++
 panels/info/test-hostname.c    |   66 ++++++++++++++++++++++-----------------
 3 files changed, 51 insertions(+), 30 deletions(-)
---
diff --git a/panels/info/Makefile.am b/panels/info/Makefile.am
index 7ffee41..e20be67 100644
--- a/panels/info/Makefile.am
+++ b/panels/info/Makefile.am
@@ -19,7 +19,7 @@ test_hostname_CFLAGS = $(INCLUDES)
 all-local: check-local
 
 check-local: test-hostname
-	$(builddir)/test-hostname > /dev/null
+	$(builddir)/test-hostname $(srcdir)/hostnames-test.txt > /dev/null
 
 ccpanelsdir = $(PANELS_DIR)
 ccpanels_LTLIBRARIES = libinfo.la
@@ -45,4 +45,6 @@ desktop_DATA = $(desktop_in_files:.desktop.in=.desktop)
 
 CLEANFILES = $(desktop_in_files) $(desktop_DATA)
 
+EXTRA_DIST = hostnames-test.txt
+
 -include $(top_srcdir)/git.mk
diff --git a/panels/info/hostnames-test.txt b/panels/info/hostnames-test.txt
new file mode 100644
index 0000000..5a31ce2
--- /dev/null
+++ b/panels/info/hostnames-test.txt
@@ -0,0 +1,11 @@
+# Pretty hostname, tab, display hostname, tab, real hostname
+Lennart's PC	Lennarts-PC	lennarts-pc
+Müllers Computer	Mullers-Computer	mullers-computer
+Voran!	Voran	voran
+Es war einmal ein Männlein	Es-war-einmal-ein-Mannlein	es-war-einmal-ein-mannlein
+Jawoll. Ist doch wahr!	Jawoll-Ist-doch-wahr	jawoll-ist-doch-wahr
+ã?¬ã??ã?¼ã??	localhost	localhost
+!!!	localhost	localhost
+...zack!!! zack!...	zack-zack	zack-zack
+Bãstien's computer... Foo-bar	Bastiens-computer-Foo-bar	bastiens-computer-foo-bar
+	localhost	localhost
diff --git a/panels/info/test-hostname.c b/panels/info/test-hostname.c
index fc8afce..807b952 100644
--- a/panels/info/test-hostname.c
+++ b/panels/info/test-hostname.c
@@ -6,54 +6,62 @@
 
 #include "hostname-helper.h"
 
-struct {
-	char *input;
-	char *output_display;
-	char *output_real;
-} tests[] = {
-	{ "Lennart's PC", "Lennarts-PC", "lennarts-pc" },
-	{ "Müllers Computer", "Mullers-Computer", "mullers-computer" },
-	{ "Voran!", "Voran", "voran" },
-	{ "Es war einmal ein Männlein", "Es-war-einmal-ein-Mannlein", "es-war-einmal-ein-mannlein" },
-	{ "Jawoll. Ist doch wahr!", "Jawoll-Ist-doch-wahr", "jawoll-ist-doch-wahr" },
-	{ "ã?¬ã??ã?¼ã??", "localhost", "localhost" },
-	{ "!!!", "localhost", "localhost" },
-	{ "...zack!!! zack!...", "zack-zack", "zack-zack" },
-	{ "Bãstien's computer... Foo-bar", "Bastiens-computer-Foo-bar", "bastiens-computer-foo-bar" },
-	{ "", "localhost", "localhost" },
-};
-
 int main (int argc, char **argv)
 {
 	char *result;
 	guint i;
+	char *contents;
+	char **lines;
 
 	setlocale (LC_ALL, "");
 	bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
 
-	/* FIXME:
-	 * - Tests don't work in non-UTF-8 locales
-	 * - They also fail in de_DE.UTF-8 because of "ü" -> "ue" conversions */
+	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 *utf8;
+		char **items;
 
-	for (i = 0; i < G_N_ELEMENTS (tests); i++) {
-		result = pretty_hostname_to_static (tests[i].input, FALSE);
-		if (g_strcmp0 (result, tests[i].output_real) != 0)
+		if (*lines[i] == '#')
+			continue;
+		if (*lines[i] == '\0')
+			break;
+
+		items = g_strsplit (lines[i], "\t", -1);
+		utf8 = g_locale_from_utf8 (items[0], -1, NULL, NULL, NULL);
+		result = pretty_hostname_to_static (items[0], FALSE);
+		if (g_strcmp0 (result, items[2]) != 0)
 			g_error ("Result for '%s' doesn't match '%s' (got: '%s')",
-				 tests[i].input, tests[i].output_real, result);
+				 utf8, items[2], result);
 		else
 			g_debug ("Result for '%s' matches '%s'",
-				 tests[i].input, result);
+				 utf8, result);
 		g_free (result);
+		g_free (utf8);
 
-		result = pretty_hostname_to_static (tests[i].input, TRUE);
-		if (g_strcmp0 (result, tests[i].output_display) != 0)
+		result = pretty_hostname_to_static (items[0], TRUE);
+		utf8 = g_locale_from_utf8 (items[0], -1, NULL, NULL, NULL);
+		if (g_strcmp0 (result, items[1]) != 0)
 			g_error ("Result for '%s' doesn't match '%s' (got: '%s')",
-				 tests[i].input, tests[i].output_display, result);
+				 utf8, items[1], result);
 		else
 			g_debug ("Result for '%s' matches '%s'",
-				 tests[i].input, result);
+				 utf8, result);
 		g_free (result);
+		g_free (utf8);
+
+		g_strfreev (items);
 	}
 
+	g_strfreev (lines);
+
 	return 0;
 }



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]