[libgdata/wip/pwithnall/tasks-json-tests] tasks: WIP work to add automated tests for JSON parsing
- From: Philip Withnall <pwithnall src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [libgdata/wip/pwithnall/tasks-json-tests] tasks: WIP work to add automated tests for JSON parsing
- Date: Mon, 28 Sep 2015 14:52:48 +0000 (UTC)
commit 83d410a32ed4e9d9b533c21e5ef63017a6ca6d27
Author: Philip Withnall <philip withnall collabora co uk>
Date: Mon Sep 28 15:52:16 2015 +0100
tasks: WIP work to add automated tests for JSON parsing
configure.ac | 9 +++
gdata/tests/Makefile.am | 22 ++++++++
gdata/tests/tasks-task.schema.json | 79 +++++++++++++++++++++++++++
gdata/tests/tasks-tasklist.schema.json | 31 +++++++++++
gdata/tests/tasks.c | 93 ++++++++++++++++++++++++++++++++
5 files changed, 234 insertions(+), 0 deletions(-)
---
diff --git a/configure.ac b/configure.ac
index 22ce3bc..1620460 100644
--- a/configure.ac
+++ b/configure.ac
@@ -33,6 +33,15 @@ PKG_PROG_PKG_CONFIG
AC_PATH_PROG([GLIB_GENMARSHAL],[glib-genmarshal])
AC_PATH_PROG([GLIB_MKENUMS],[glib-mkenums])
+# Walbottle support for JSON testing
+AC_PATH_PROG([JSON_SCHEMA_VALIDATE],[json-schema-validate])
+AC_PATH_PROG([JSON_SCHEMA_GENERATE],[json-schema-generate])
+
+AS_IF([test "$JSON_SCHEMA_VALIDATE" == ""],
+ [AC_MSG_ERROR([json-schema-validate not found])])
+AS_IF([test "$JSON_SCHEMA_GENERATE" == ""],
+ [AC_MSG_ERROR([json-schema-generate not found])])
+
# Requirements
GLIB_REQS=2.31.0
GLIB_MIN_REQUIRED=GLIB_VERSION_2_32
diff --git a/gdata/tests/Makefile.am b/gdata/tests/Makefile.am
index a3ca626..aae711d 100644
--- a/gdata/tests/Makefile.am
+++ b/gdata/tests/Makefile.am
@@ -19,6 +19,26 @@ libgdata_test_la_LIBADD = \
$(UHTTPMOCK_LIBS) \
$(NULL)
+# JSON schemas.
+json_schemas = \
+ tasks-tasklist.schema.json \
+ tasks-task.schema.json \
+ $(NULL)
+
+EXTRA_DIST += $(json_schemas)
+
+check-json-schema: $(json_schemas)
+ $(AM_V_GEN)$(JSON_SCHEMA_VALIDATE) $^
+check-local: check-json-schema
+.PHONY: check-json-schema
+
+json_schemas_c = $(json_schemas:.schema.json=.schema.c)
+
+CLEANFILES += $(json_schemas_c)
+
+%.schema.c: %.schema.json
+ $(AM_V_GEN)$(JSON_SCHEMA_GENERATE) --c-variable-name=$(subst -,_,$(notdir $*))_json_instances
--format c $^ > $@
+
# Flags for the helper library and all test binaries
AM_CPPFLAGS = \
-I$(top_srcdir)/ \
@@ -61,6 +81,8 @@ test_programs = \
youtube \
$(NULL)
+tasks_SOURCES = tasks.c tasks-task.schema.c tasks-tasklist.schema.c
+
# FIXME: Temporarily disabled until they are ported for the changes in the v2
# to v3 API transitions.
#all_test_programs += documents
diff --git a/gdata/tests/tasks-task.schema.json b/gdata/tests/tasks-task.schema.json
new file mode 100644
index 0000000..558d5f2
--- /dev/null
+++ b/gdata/tests/tasks-task.schema.json
@@ -0,0 +1,79 @@
+{
+ "title": "Task",
+ "description":
"https://developers.google.com/google-apps/tasks/v1/reference/tasks#resource-representations",
+ "type": "object",
+ "properties": {
+ "kind": {
+ "type": "string",
+ "enum": [ "tasks#task" ]
+ },
+ "id": {
+ "type": "string",
+ "minLength": 1
+ },
+ "etag": {
+ "type": "string",
+ "minLength": 1
+ },
+ "title": {
+ "type": "string"
+ },
+ "selfLink": {
+ "type": "string",
+ "format": "uri"
+ },
+ "updated": {
+ "type": "string",
+ "format": "date-time"
+ },
+ "parent": {
+ "type": "string"
+ },
+ "position": {
+ "type": "string"
+ },
+ "notes": {
+ "type": "string"
+ },
+ "status": {
+ "type": "string",
+ "enum": [ "needsAction", "completed" ]
+ },
+ "due": {
+ "type": "string",
+ "format": "date-time"
+ },
+ "completed": {
+ "type": "string",
+ "format": "date-time"
+ },
+ "deleted": {
+ "type": "boolean",
+ "default": false
+ },
+ "hidden": {
+ "type": "boolean",
+ "default": false
+ },
+ "links": {
+ "type": "array",
+ "items": {
+ "type": "object",
+ "properties": {
+ "type": {
+ "type": "string"
+ },
+ "description": {
+ "type": "string"
+ },
+ "link": {
+ "type": "string",
+ "format": "uri"
+ }
+ },
+ "required": [ "type", "link" ]
+ }
+ }
+ },
+ "additionalProperties": true
+}
diff --git a/gdata/tests/tasks-tasklist.schema.json b/gdata/tests/tasks-tasklist.schema.json
new file mode 100644
index 0000000..567d9ac
--- /dev/null
+++ b/gdata/tests/tasks-tasklist.schema.json
@@ -0,0 +1,31 @@
+{
+ "title": "Tasklist",
+ "description":
"https://developers.google.com/google-apps/tasks/v1/reference/tasklists#resource-representations",
+ "type": "object",
+ "properties": {
+ "kind": {
+ "type": "string",
+ "enum": [ "tasks#taskList" ]
+ },
+ "id": {
+ "type": "string",
+ "minLength": 1
+ },
+ "etag": {
+ "type": "string",
+ "minLength": 1
+ },
+ "title": {
+ "type": "string"
+ },
+ "selfLink": {
+ "type": "string",
+ "format": "uri"
+ },
+ "updated": {
+ "type": "string",
+ "format": "date-time"
+ }
+ },
+ "additionalProperties": true
+}
diff --git a/gdata/tests/tasks.c b/gdata/tests/tasks.c
index 7535c97..cdd1eb9 100644
--- a/gdata/tests/tasks.c
+++ b/gdata/tests/tasks.c
@@ -25,6 +25,9 @@
#include "common.h"
#include "gdata-dummy-authorizer.h"
+#include "tasks-task.schema.c"
+#include "tasks-tasklist.schema.c"
+
static UhmServer *mock_server = NULL; /* owned */
#undef CLIENT_ID /* from common.h */
@@ -519,6 +522,40 @@ test_task_parser_normal (void)
g_object_unref (task);
}
+/* Test the task parser with each generated test vector from the JSON schema. */
+static void
+test_task_parser_generated (gconstpointer user_data)
+{
+ guint i;
+ GDataParsable *parsable = NULL; /* owned */
+ GError *error = NULL;
+
+ i = GPOINTER_TO_UINT (user_data);
+
+ parsable = gdata_parsable_new_from_json (GDATA_TYPE_TASKS_TASK,
+ tasks_task_json_instances[i].json,
+ tasks_task_json_instances[i].size,
+ &error);
+
+ if (tasks_task_json_instances[i].is_valid) {
+ g_assert_no_error (error);
+ g_assert (GDATA_IS_TASKS_TASK (parsable));
+ gdata_test_compare_kind (GDATA_ENTRY (parsable), "tasks#task", NULL);
+ } else {
+ if (error != NULL && error->domain == GDATA_PARSER_ERROR) {
+ g_assert_error (error, GDATA_PARSER_ERROR,
+ GDATA_PARSER_ERROR_PARSING_STRING);
+ } else {
+ g_assert_error (error, GDATA_SERVICE_ERROR,
+ GDATA_SERVICE_ERROR_PROTOCOL_ERROR);
+ }
+ g_assert (parsable == NULL);
+ }
+
+ g_clear_error (&error);
+ g_clear_object (&parsable);
+}
+
/* Test that inserting a tasklist works. */
typedef struct {
GDataTasksTasklist *new_tasklist;
@@ -1265,6 +1302,41 @@ test_tasklist_parser_normal (void)
g_object_unref (tasklist);
}
+/* Test the tasklist parser with each generated test vector from the JSON
+ * schema. */
+static void
+test_tasklist_parser_generated (gconstpointer user_data)
+{
+ guint i;
+ GDataParsable *parsable = NULL; /* owned */
+ GError *error = NULL;
+
+ i = GPOINTER_TO_UINT (user_data);
+
+ parsable = gdata_parsable_new_from_json (GDATA_TYPE_TASKS_TASKLIST,
+ tasks_tasklist_json_instances[i].json,
+ tasks_tasklist_json_instances[i].size,
+ &error);
+
+ if (tasks_tasklist_json_instances[i].is_valid) {
+ g_assert_no_error (error);
+ g_assert (GDATA_IS_TASKS_TASKLIST (parsable));
+ gdata_test_compare_kind (GDATA_ENTRY (parsable), "tasks#taskList", NULL);
+ } else {
+ if (error != NULL && error->domain == GDATA_PARSER_ERROR) {
+ g_assert_error (error, GDATA_PARSER_ERROR,
+ GDATA_PARSER_ERROR_PARSING_STRING);
+ } else {
+ g_assert_error (error, GDATA_SERVICE_ERROR,
+ GDATA_SERVICE_ERROR_PROTOCOL_ERROR);
+ }
+ g_assert (parsable == NULL);
+ }
+
+ g_clear_error (&error);
+ g_clear_object (&parsable);
+}
+
static void
mock_server_notify_resolver_cb (GObject *object, GParamSpec *pspec,
gpointer user_data)
@@ -1343,6 +1415,7 @@ skip_test:
int
main (int argc, char *argv[])
{
+ guint i;
gint retval;
GDataAuthorizer *authorizer = NULL; /* owned */
GDataAuthorizer *unauthorised_authorizer = NULL; /* owned */
@@ -1409,12 +1482,32 @@ main (int argc, char *argv[])
test_task_parser_minimal);
g_test_add_func ("/tasks/task/parser/normal", test_task_parser_normal);
+ for (i = 0; i < G_N_ELEMENTS (tasks_task_json_instances); i++) {
+ gchar *test_name = NULL;
+
+ test_name = g_strdup_printf ("/tasks/task/parser/generated/%u",
+ i);
+ g_test_add_data_func (test_name, GUINT_TO_POINTER (i),
+ test_task_parser_generated);
+ g_free (test_name);
+ }
+
g_test_add_func ("/tasks/tasklist/properties",
test_tasklist_properties);
g_test_add_func ("/tasks/tasklist/escaping", test_tasklist_escaping);
g_test_add_func ("/tasks/tasklist/parser/normal",
test_tasklist_parser_normal);
+ for (i = 0; i < G_N_ELEMENTS (tasks_tasklist_json_instances); i++) {
+ gchar *test_name = NULL;
+
+ test_name = g_strdup_printf ("/tasks/tasklist/parser/generated/%u",
+ i);
+ g_test_add_data_func (test_name, GUINT_TO_POINTER (i),
+ test_tasklist_parser_generated);
+ g_free (test_name);
+ }
+
g_test_add_func ("/tasks/query/uri", test_query_uri);
g_test_add_func ("/tasks/query/etag", test_query_etag);
g_test_add_func ("/tasks/query/properties", test_query_properties);
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]