[gtk/wip/otte/json: 38/38] testsuite: Add a test for the json parser




commit a8a12b3a3b616395f32c56078373598872cba099
Author: Benjamin Otte <otte redhat com>
Date:   Thu Nov 25 06:34:21 2021 +0100

    testsuite: Add a test for the json parser
    
    No test files inclued yet, just the binary.

 gtk/json/meson.build              |   1 +
 testsuite/json/meson.build        |  43 +++++
 testsuite/json/test-json-parser.c | 351 ++++++++++++++++++++++++++++++++++++++
 testsuite/meson.build             |   1 +
 4 files changed, 396 insertions(+)
---
diff --git a/gtk/json/meson.build b/gtk/json/meson.build
index 859c3787cb..dc8cd223d1 100644
--- a/gtk/json/meson.build
+++ b/gtk/json/meson.build
@@ -25,5 +25,6 @@ libgtk_json = static_library('gtk_json',
 # "public" libgtk_dep used by internal executables.
 libgtk_json_dep = declare_dependency(include_directories: [ confinc, ],
   sources: [ ],
+  link_with: [ libgtk_json ],
   dependencies: gtk_json_deps,
 )
diff --git a/testsuite/json/meson.build b/testsuite/json/meson.build
new file mode 100644
index 0000000000..01a431b3f0
--- /dev/null
+++ b/testsuite/json/meson.build
@@ -0,0 +1,43 @@
+testexecdir = join_paths(installed_test_bindir, 'json')
+testdatadir = join_paths(installed_test_datadir, 'json')
+
+test_parser = executable('test-json-parser',
+                         'test-json-parser.c',
+                         '../testutils.c',
+                         c_args: common_cflags,
+                         include_directories: [confinc],
+                         install: get_option('install-tests'),
+                         install_dir: testexecdir,
+                         link_with: [ libgtk_json ],
+                         dependencies: libgtk_json_dep)
+
+test_data = [
+]
+
+foreach testname : test_data
+  if testname.endswith('.json') and not testname.endswith('.ref.json')
+    test('parser ' + testname, test_parser,
+         args: [ '--tap',
+                 '-k',
+                 join_paths(meson.current_source_dir(), testname),
+               ],
+         protocol: 'tap',
+         env: [
+                'G_TEST_SRCDIR=@0@'.format(meson.current_source_dir()),
+                'G_TEST_BUILDDIR=@0@'.format(meson.current_build_dir())
+              ],
+         suite: 'json')
+  endif
+endforeach
+
+if get_option('install-tests')
+  conf = configuration_data()
+  conf.set('libexecdir', gtk_libexecdir)
+  configure_file(input: 'parser.test.in',
+                 output: 'parser.test',
+                 configuration: conf,
+                 install_dir: testdatadir)
+
+  install_data(test_data, install_dir: testexecdir)
+
+endif
diff --git a/testsuite/json/test-json-parser.c b/testsuite/json/test-json-parser.c
new file mode 100644
index 0000000000..eb527eb110
--- /dev/null
+++ b/testsuite/json/test-json-parser.c
@@ -0,0 +1,351 @@
+/*
+ * Copyright (C) 2021 Red Hat Inc.
+ *
+ * Author:
+ *      Benjamin Otte <otte redhat com>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Library General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Library General Public License for more details.
+ *
+ * You should have received a copy of the GNU Library General Public
+ * License along with this library. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "config.h"
+
+#include <gio/gio.h>
+#include <locale.h>
+#include <string.h>
+
+#include "gtk/json/gtkjsonparserprivate.h"
+#include "gtk/json/gtkjsonprinterprivate.h"
+#include "testsuite/testutils.h"
+
+#ifdef G_OS_WIN32
+# include <io.h>
+#endif
+
+static char *
+test_get_comparison_file (GFile      *json_file,
+                          const char *extension)
+{
+  char *path;
+  GString *file = g_string_new (NULL);
+
+  path = g_file_get_path (json_file);
+  g_assert (path);
+
+  if (g_str_has_suffix (path, ".json"))
+    g_string_append_len (file, path, strlen (path) - 4);
+  else
+    g_string_append (file, path);
+  
+  g_string_append (file, extension);
+  g_free (path);
+
+  if (!g_file_test (file->str, G_FILE_TEST_EXISTS))
+    {
+      g_string_free (file, TRUE);
+      return NULL;
+    }
+
+  return g_string_free (file, FALSE);
+}
+
+static void
+parse_and_print (GtkJsonParser  *parser,
+                 GtkJsonPrinter *printer)
+{
+  while (TRUE)
+    {
+      char *name = gtk_json_parser_get_member_name (parser);
+
+      switch (gtk_json_parser_get_node (parser))
+        {
+        case GTK_JSON_NONE:
+          if (gtk_json_printer_get_depth (printer) == 0)
+            return;
+          gtk_json_printer_end (printer);
+          gtk_json_parser_end (parser);
+          gtk_json_parser_next (parser);
+          break;
+
+        case GTK_JSON_NULL:
+          gtk_json_printer_add_null (printer, name);
+          gtk_json_parser_next (parser);
+          break;
+
+        case GTK_JSON_BOOLEAN:
+          gtk_json_printer_add_boolean (printer,
+                                        name,
+                                        gtk_json_parser_get_boolean (parser));
+          gtk_json_parser_next (parser);
+          break;
+
+        case GTK_JSON_NUMBER:
+          gtk_json_printer_add_number (printer,
+                                       name,
+                                       gtk_json_parser_get_number (parser));
+          gtk_json_parser_next (parser);
+          break;
+
+        case GTK_JSON_STRING:
+          { 
+            char *s = gtk_json_parser_get_string (parser);
+            gtk_json_printer_add_string (printer, name, s);
+            g_free (s);
+          }
+          gtk_json_parser_next (parser);
+          break;
+
+        case GTK_JSON_OBJECT:
+          gtk_json_printer_start_object (printer, name);
+          gtk_json_parser_start_object (parser);
+          break;
+
+        case GTK_JSON_ARRAY:
+          gtk_json_printer_start_array (printer, name);
+          gtk_json_parser_start_array (parser);
+          break;
+
+        default:
+          g_assert_not_reached ();
+          return;
+        }
+
+      g_free (name);
+    }
+}
+
+static void
+string_append_func (GtkJsonPrinter *printer,
+                    const char     *s,
+                    gpointer        data)
+{
+  g_string_append (data, s);
+}
+
+static void
+test_json_file (GFile *file)
+{
+  GtkJsonPrinter *printer;
+  GtkJsonParser *parser;
+  char *reference_file, *error_file;
+  char *diff;
+  GBytes *bytes;
+  GError *error = NULL;
+
+  reference_file = test_get_comparison_file (file, ".ref.json");
+  error_file = test_get_comparison_file (file, ".error");
+
+  bytes = g_file_load_bytes (file, NULL, NULL, &error);
+  g_assert_no_error (error);
+
+  parser = gtk_json_parser_new_for_bytes (bytes);
+
+  if (reference_file)
+    {
+      GString *string;
+
+      string = g_string_new (NULL);
+      printer = gtk_json_printer_new (string_append_func,
+                                      string,
+                                      NULL);
+      gtk_json_printer_set_flags (printer, GTK_JSON_PRINTER_PRETTY);
+      parse_and_print (parser, printer);
+      g_string_append (string, "\n");
+      gtk_json_printer_free (printer);
+
+      diff = diff_with_file (reference_file, string->str, string->len, &error);
+      g_assert_no_error (error);
+      if (diff && diff[0])
+        {
+          g_test_message ("Resulting JSON doesn't match reference:\n%s", diff);
+          g_test_fail ();
+        }
+
+      g_free (diff);
+      g_string_free (string, TRUE);
+    }
+  else
+    {
+      while (gtk_json_parser_next (parser));
+    }
+
+  if (gtk_json_parser_get_error (parser))
+    {
+      const GError *json_error;
+      GString *string;
+      gsize start_line, start_bytes, end_line, end_bytes;
+
+      json_error = gtk_json_parser_get_error (parser);
+      gtk_json_parser_get_error_location (parser,
+                                          &start_line, &start_bytes,
+                                          &end_line, &end_bytes);
+      
+      string = g_string_new (NULL);
+      g_string_append_printf (string, "%zu:%zu", 
+                              start_line + 1,
+                              start_bytes + 1);
+      if (start_line != end_line || start_bytes != end_bytes)
+        {
+          g_string_append (string, "-");
+          if (start_line != end_line)
+            g_string_append_printf (string, "%zu:", end_line + 1);
+          g_string_append_printf (string, "%zu", end_bytes + 1);
+        }
+      g_string_append (string, ": ");
+      g_string_append (string, json_error->message);
+
+      if (error_file)
+        {
+          diff = diff_with_file (error_file, string->str, string->len, &error);
+          g_assert_no_error (error);
+          if (diff && diff[0])
+            {
+              g_test_message ("Error doesn't match:\n%s", diff);
+              g_test_fail ();
+            }
+          g_free (diff);
+        }
+      else
+        {
+          g_test_message ("Unexpected error:\n%s", string->str);
+          g_test_fail ();
+        }
+      g_string_free (string, TRUE);
+    }
+  else
+    {
+      diff = diff_with_file (error_file, "", 0, &error);
+      g_assert_no_error (error);
+      if (diff && diff[0])
+        {
+          g_test_message ("Error doesn't match:\n%s", diff);
+          g_test_fail ();
+        }
+      g_free (diff);
+    }
+
+  gtk_json_parser_free (parser);
+
+  g_free (reference_file);
+  g_bytes_unref (bytes);
+}
+
+static void
+add_test_for_file (GFile *file)
+{
+  char *path;
+
+  path = g_file_get_path (file);
+
+  g_test_add_vtable (path,
+                     0,
+                     g_object_ref (file),
+                     NULL,
+                     (GTestFixtureFunc) test_json_file,
+                     (GTestFixtureFunc) g_object_unref);
+
+  g_free (path);
+}
+
+static int
+compare_files (gconstpointer a, gconstpointer b)
+{
+  GFile *file1 = G_FILE (a);
+  GFile *file2 = G_FILE (b);
+  char *path1, *path2;
+  int result;
+
+  path1 = g_file_get_path (file1);
+  path2 = g_file_get_path (file2);
+
+  result = strcmp (path1, path2);
+
+  g_free (path1);
+  g_free (path2);
+
+  return result;
+}
+
+static void
+add_tests_for_files_in_directory (GFile *dir)
+{
+  GFileEnumerator *enumerator;
+  GFileInfo *info;
+  GList *files;
+  GError *error = NULL;
+
+  enumerator = g_file_enumerate_children (dir, G_FILE_ATTRIBUTE_STANDARD_NAME, 0, NULL, &error);
+  g_assert_no_error (error);
+  files = NULL;
+
+  while ((info = g_file_enumerator_next_file (enumerator, NULL, &error)))
+    {
+      const char *filename;
+
+      filename = g_file_info_get_name (info);
+
+      if (!g_str_has_suffix (filename, ".json") ||
+          g_str_has_suffix (filename, ".out.json") ||
+          g_str_has_suffix (filename, ".ref.json"))
+        {
+          g_object_unref (info);
+          continue;
+        }
+
+      files = g_list_prepend (files, g_file_get_child (dir, filename));
+
+      g_object_unref (info);
+    }
+  
+  g_assert_no_error (error);
+  g_object_unref (enumerator);
+
+  files = g_list_sort (files, compare_files);
+  g_list_foreach (files, (GFunc) add_test_for_file, NULL);
+  g_list_free_full (files, g_object_unref);
+}
+
+int
+main (int argc, char **argv)
+{
+  (g_test_init) (&argc, &argv, NULL);
+  setlocale (LC_ALL, "");
+
+  if (argc < 2)
+    {
+      const char *basedir;
+      GFile *dir;
+
+      basedir = g_test_get_dir (G_TEST_DIST);
+      dir = g_file_new_for_path (basedir);
+      add_tests_for_files_in_directory (dir);
+
+      g_object_unref (dir);
+    }
+  else
+    {
+      guint i;
+
+      for (i = 1; i < argc; i++)
+        {
+          GFile *file = g_file_new_for_commandline_arg (argv[i]);
+
+          add_test_for_file (file);
+
+          g_object_unref (file);
+        }
+    }
+
+  return g_test_run ();
+}
+
diff --git a/testsuite/meson.build b/testsuite/meson.build
index 97344f3062..f7d66659e3 100644
--- a/testsuite/meson.build
+++ b/testsuite/meson.build
@@ -57,6 +57,7 @@ subdir('gdk')
 subdir('gsk')
 subdir('gtk')
 subdir('css')
+subdir('json')
 subdir('a11y')
 subdir('tools')
 subdir('reftests')


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