[geocode-glib] tests: Load tests from external files



commit 41218e21d244bcb5e599740e0dce72f75b726a98
Author: Bastien Nocera <hadess hadess net>
Date:   Mon Nov 26 10:17:23 2012 +0100

    tests: Load tests from external files
    
    Having to escape the JSON in a C file to test it just wasn't
    very practical (and would slow down vim too).

 geocode-glib/Makefile.am                      |    2 +-
 geocode-glib/data/placefinder-area.json       |    1 +
 geocode-glib/data/placefinder-got-error.json  |    1 +
 geocode-glib/data/placefinder-no-results.json |    1 +
 geocode-glib/test-gcglib.c                    |   21 ++++++++++++++++-----
 5 files changed, 20 insertions(+), 6 deletions(-)
---
diff --git a/geocode-glib/Makefile.am b/geocode-glib/Makefile.am
index fdad924..d742251 100644
--- a/geocode-glib/Makefile.am
+++ b/geocode-glib/Makefile.am
@@ -33,7 +33,7 @@ gcglib_HEADERS = \
 	geocode-reverse.h				\
 	geocode-error.h
 
-AM_CFLAGS = -I$(top_srcdir) $(GEOCODE_CFLAGS) $(COMMON_CFLAGS) $(WARN_CFLAGS) $(DISABLE_DEPRECATED)
+AM_CFLAGS = -I$(top_srcdir) $(GEOCODE_CFLAGS) $(COMMON_CFLAGS) $(WARN_CFLAGS) $(DISABLE_DEPRECATED) -DTEST_SRCDIR=\""$(srcdir)/data/"\"
 
 -include $(INTROSPECTION_MAKEFILE)
 INTROSPECTION_GIRS =
diff --git a/geocode-glib/data/placefinder-area.json b/geocode-glib/data/placefinder-area.json
new file mode 100644
index 0000000..2328eca
--- /dev/null
+++ b/geocode-glib/data/placefinder-area.json
@@ -0,0 +1 @@
+{"ResultSet":{"version":"1.0","Error":0,"ErrorMessage":"No error","Locale":"us_US","Quality":99,"Found":1,"Results":[{"quality":99,"latitude":"51.237100","longitude":"-0.589669","offsetlat":"51.237100","offsetlon":"-0.589669","radius":500,"name":"51.2371, -0.589669","line1":"9 Old Palace Road","line2":"Guildford","line3":"GU2 7","line4":"United Kingdom","house":"9","street":"Old Palace Road","xstreet":"","unittype":"","unit":"","postal":"GU2 7","neighborhood":"Onslow Village","city":"Guildford","county":"Surrey","state":"England","country":"United Kingdom","countrycode":"GB","statecode":"ENG","countycode":"SRY","timezone":"Europe\/London","hash":"","woeid":26347368,"woetype":11,"uzip":"GU2 7","airport":"LHR"}]}}
\ No newline at end of file
diff --git a/geocode-glib/data/placefinder-got-error.json b/geocode-glib/data/placefinder-got-error.json
new file mode 100644
index 0000000..32c4385
--- /dev/null
+++ b/geocode-glib/data/placefinder-got-error.json
@@ -0,0 +1 @@
+{"ResultSet":{"version":"1.0","Error":107,"ErrorMessage":"You gotz done!","Locale":"us_US","Quality":99,"Found":1,"Results":[{"quality":99,"latitude":"51.237100","longitude":"-0.589669","offsetlat":"51.237100","offsetlon":"-0.589669","radius":500,"name":"51.2371, -0.589669","line1":"9 Old Palace Road","line2":"Guildford","line3":"GU2 7","line4":"United Kingdom","house":"9","street":"Old Palace Road","xstreet":"","unittype":"","unit":"","postal":"GU2 7","neighborhood":"Onslow Village","city":"Guildford","county":"Surrey","state":"England","country":"United Kingdom","countrycode":"GB","statecode":"ENG","countycode":"SRY","timezone":"Europe\/London","hash":"","woeid":26347368,"woetype":11,"uzip":"GU2 7","airport":"LHR"}]}}
\ No newline at end of file
diff --git a/geocode-glib/data/placefinder-no-results.json b/geocode-glib/data/placefinder-no-results.json
new file mode 100644
index 0000000..00b892c
--- /dev/null
+++ b/geocode-glib/data/placefinder-no-results.json
@@ -0,0 +1 @@
+{"ResultSet":{"version":"1.0","Error":0,"ErrorMessage":"No error","Locale":"us_US","Quality":10,"Found":0}}
\ No newline at end of file
diff --git a/geocode-glib/test-gcglib.c b/geocode-glib/test-gcglib.c
index ab92d74..752d6dc 100644
--- a/geocode-glib/test-gcglib.c
+++ b/geocode-glib/test-gcglib.c
@@ -335,18 +335,29 @@ test_resolve_json (void)
 	GError *error = NULL;
 	guint i;
 	struct {
-		const char *test;
+		const char *fname;
 		const char *error;
 		const char *key;
 		const char *value;
 	} tests[] = {
-		{ "{\"ResultSet\":{\"version\":\"1.0\",\"Error\":0,\"ErrorMessage\":\"No error\",\"Locale\":\"us_US\",\"Quality\":99,\"Found\":1,\"Results\":[{\"quality\":99,\"latitude\":\"51.237100\",\"longitude\":\"-0.589669\",\"offsetlat\":\"51.237100\",\"offsetlon\":\"-0.589669\",\"radius\":500,\"name\":\"51.2371, -0.589669\",\"line1\":\"9 Old Palace Road\",\"line2\":\"Guildford\",\"line3\":\"GU2 7\",\"line4\":\"United Kingdom\",\"house\":\"9\",\"street\":\"Old Palace Road\",\"xstreet\":\"\",\"unittype\":\"\",\"unit\":\"\",\"postal\":\"GU2 7\",\"neighborhood\":\"Onslow Village\",\"city\":\"Guildford\",\"county\":\"Surrey\",\"state\":\"England\",\"country\":\"United Kingdom\",\"countrycode\":\"GB\",\"statecode\":\"ENG\",\"countycode\":\"SRY\",\"timezone\":\"Europe\\/London\",\"hash\":\"\",\"woeid\":26347368,\"woetype\":11,\"uzip\":\"GU2 7\",\"airport\":\"LHR\"}]}}", NULL, "area", "Onslow Village" },
-		{ "{\"ResultSet\":{\"version\":\"1.0\",\"Error\":107,\"ErrorMessage\":\"You gotz done!\",\"Locale\":\"us_US\",\"Quality\":99,\"Found\":1,\"Results\":[{\"quality\":99,\"latitude\":\"51.237100\",\"longitude\":\"-0.589669\",\"offsetlat\":\"51.237100\",\"offsetlon\":\"-0.589669\",\"radius\":500,\"name\":\"51.2371, -0.589669\",\"line1\":\"9 Old Palace Road\",\"line2\":\"Guildford\",\"line3\":\"GU2 7\",\"line4\":\"United Kingdom\",\"house\":\"9\",\"street\":\"Old Palace Road\",\"xstreet\":\"\",\"unittype\":\"\",\"unit\":\"\",\"postal\":\"GU2 7\",\"neighborhood\":\"Onslow Village\",\"city\":\"Guildford\",\"county\":\"Surrey\",\"state\":\"England\",\"country\":\"United Kingdom\",\"countrycode\":\"GB\",\"statecode\":\"ENG\",\"countycode\":\"SRY\",\"timezone\":\"Europe\\/London\",\"hash\":\"\",\"woeid\":26347368,\"woetype\":11,\"uzip\":\"GU2 7\",\"airport\":\"LHR\"}]}}", "You gotz done!" },
-		{ "{\"ResultSet\":{\"version\":\"1.0\",\"Error\":0,\"ErrorMessage\":\"No error\",\"Locale\":\"us_US\",\"Quality\":10,\"Found\":0}}", "No matches found for request", NULL, NULL },
+		{ "placefinder-area.json", NULL, "area", "Onslow Village" },
+		{ "placefinder-got-error.json", "You gotz done!" },
+		{ "placefinder-no-results.json", "No matches found for request", NULL, NULL },
 	};
 
 	for (i = 0; i < G_N_ELEMENTS (tests); i++) {
-		ht = _geocode_parse_resolve_json (tests[i].test, &error);
+		char *contents;
+		char *filename;
+
+		filename = g_strdup_printf (TEST_SRCDIR "/%s", tests[i].fname);
+		if (g_file_get_contents (filename, &contents, NULL, &error) == FALSE) {
+			g_critical ("Couldn't load contents of '%s': %s",
+				    filename, error->message);
+		}
+		g_free (filename);
+
+		ht = _geocode_parse_resolve_json (contents, &error);
+		g_free (contents);
 
 		if (tests[i].error) {
 			g_assert (ht == NULL);



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