From 5b26ef2e46de291efbb2b50427e2ce7c684550f5 Mon Sep 17 00:00:00 2001 From: Philip Withnall Date: Mon, 3 Jun 2024 15:19:45 +0100 Subject: [PATCH] tests: Add basic tests for gdump.c in libgirepository MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit It’s currently completely untested. Let’s add a few basic tests so that adding more tests in future is easier. Signed-off-by: Philip Withnall --- girepository/tests/dump.c | 182 +++++++++++++++++++++++++++++++++ girepository/tests/meson.build | 4 + 2 files changed, 186 insertions(+) create mode 100644 girepository/tests/dump.c diff --git a/girepository/tests/dump.c b/girepository/tests/dump.c new file mode 100644 index 000000000..7c8683914 --- /dev/null +++ b/girepository/tests/dump.c @@ -0,0 +1,182 @@ +/* + * Copyright 2024 GNOME Foundation + * + * SPDX-License-Identifier: LGPL-2.1-or-later + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General + * Public License along with this library; if not, see . + */ + +#include "config.h" + +#include +#include + +#include "girepository.h" +#include "test-common.h" + +/* Dummy GTypes which can be introspected by the dumper. */ + +/* Dummy object type with no properties or signals. */ +struct _TestObject +{ + GObject parent_instance; +}; + +#define TEST_TYPE_OBJECT test_object_get_type () +G_MODULE_EXPORT G_DECLARE_FINAL_TYPE (TestObject, test_object, TEST, OBJECT, GObject) +G_DEFINE_FINAL_TYPE (TestObject, test_object, G_TYPE_OBJECT) + +static void +test_object_class_init (TestObjectClass *klass) +{ +} + +static void +test_object_init (TestObject *self) +{ +} + +/* Dummy interface type with no properties or signals. */ +struct _TestInterfaceInterface +{ + GTypeInterface g_iface; +}; + +#define TEST_TYPE_INTERFACE test_interface_get_type () +G_MODULE_EXPORT G_DECLARE_INTERFACE (TestInterface, test_interface, TEST, INTERFACE, GObject) +G_DEFINE_INTERFACE (TestInterface, test_interface, G_TYPE_OBJECT) + +static void +test_interface_default_init (TestInterfaceInterface *iface) +{ +} + +/* Test functions */ +static void +assert_dump (const char *input, + const char *expected_output) +{ + int fd = -1; + char *in_file_path = NULL; + char *out_file_path = NULL; + char *output = NULL; + gboolean retval; + GError *local_error = NULL; + + fd = g_file_open_tmp ("dump_XXXXXX", &in_file_path, NULL); + g_assert_cmpint (fd, >=, 0); + g_assert_true (g_close (fd, NULL)); + + out_file_path = g_strconcat (in_file_path, ".out", NULL); + + g_file_set_contents (in_file_path, input, -1, &local_error); + g_assert_no_error (local_error); + + retval = gi_repository_dump (in_file_path, out_file_path, &local_error); + g_assert_no_error (local_error); + g_assert_true (retval); + + g_file_get_contents (out_file_path, &output, NULL, &local_error); + g_assert_no_error (local_error); + + g_assert_cmpstr (output, ==, expected_output); + + g_unlink (out_file_path); + g_unlink (in_file_path); + g_free (output); + g_free (out_file_path); + g_free (in_file_path); +} + +static void +assert_dump_error (const char *input, + GQuark expected_error_domain, + int expected_error_code) +{ + int fd = -1; + char *in_file_path = NULL; + char *out_file_path = NULL; + gboolean retval; + GError *local_error = NULL; + + fd = g_file_open_tmp ("dump_XXXXXX", &in_file_path, NULL); + g_assert_cmpint (fd, >=, 0); + g_assert_true (g_close (fd, NULL)); + + out_file_path = g_strconcat (in_file_path, ".out", NULL); + + g_file_set_contents (in_file_path, input, -1, &local_error); + g_assert_no_error (local_error); + + retval = gi_repository_dump (in_file_path, out_file_path, &local_error); + g_assert_error (local_error, expected_error_domain, expected_error_code); + g_assert_false (retval); + g_clear_error (&local_error); + + g_unlink (out_file_path); + g_unlink (in_file_path); + g_free (out_file_path); + g_free (in_file_path); +} + +static void +test_empty_file (void) +{ + assert_dump ("", + "\n" + "\n" + "\n"); +} + +static void +test_missing_get_type (void) +{ + assert_dump_error ("get-type:does_not_exist_get_type", + G_FILE_ERROR, G_FILE_ERROR_FAILED); +} + +static void +test_missing_quark (void) +{ + assert_dump_error ("error-quark:does_not_exist_error", + G_FILE_ERROR, G_FILE_ERROR_FAILED); +} + +static void +test_basic (void) +{ + assert_dump ("get-type:test_object_get_type\n" + "get-type:test_interface_get_type\n", + "\n" + "\n" + " \n" + " \n" + " \n" + " \n" + "\n"); +} + +int +main (int argc, + char *argv[]) +{ + repository_init (&argc, &argv); + + g_test_add_func ("/dump/empty-file", test_empty_file); + g_test_add_func ("/dump/missing-get-type", test_missing_get_type); + g_test_add_func ("/dump/missing-quark", test_missing_quark); + g_test_add_func ("/dump/basic", test_basic); + + return g_test_run (); +} diff --git a/girepository/tests/meson.build b/girepository/tests/meson.build index 6fa947cfd..ba9a6e10d 100644 --- a/girepository/tests/meson.build +++ b/girepository/tests/meson.build @@ -5,6 +5,9 @@ girepository_tests = { 'cmph-bdz': { 'dependencies': [cmph_dep], }, + 'dump' : { + 'export_dynamic': true, + }, 'gthash' : { 'dependencies': [girepo_gthash_dep], }, @@ -121,6 +124,7 @@ foreach test_name, extra_args : girepository_tests override_options: extra_args.get('override_options', []), dependencies: test_deps + extra_args.get('dependencies', []), link_with: extra_args.get('link_with', []), + export_dynamic: extra_args.get('export_dynamic', false), install_dir: installed_tests_execdir, install_tag: 'tests', install: install,