diff --git a/tests/json-utils.c b/tests/json-utils.c new file mode 100644 index 0000000000000000000000000000000000000000..2cab98b0f77fb68489dde9eb1b3f60f6b36278a3 --- /dev/null +++ b/tests/json-utils.c @@ -0,0 +1,107 @@ +/* + * Copyright © 2019-2020 Collabora Ltd. + * + * SPDX-License-Identifier: MIT + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY + * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE + * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +#include <steam-runtime-tools/steam-runtime-tools.h> + +#include <glib.h> +#include <glib/gstdio.h> +#include <glib-object.h> + +#include "steam-runtime-tools/glib-backports-internal.h" +#include "steam-runtime-tools/json-glib-backports-internal.h" +#include "steam-runtime-tools/json-utils-internal.h" + +typedef struct +{ + int unused; +} Fixture; + +typedef struct +{ + int unused; +} Config; + +static void +setup (Fixture *f, + gconstpointer context) +{ + G_GNUC_UNUSED const Config *config = context; +} + +static void +teardown (Fixture *f, + gconstpointer context) +{ + G_GNUC_UNUSED const Config *config = context; +} + +static void +test_dup_strv_member (Fixture *f, + gconstpointer context) +{ + g_autoptr(JsonArray) arr = json_array_new (); + g_autoptr(JsonNode) arr_node = json_node_alloc (); + g_autoptr(JsonObject) obj = json_object_new (); + g_autoptr(JsonObject) empty = json_object_new (); + g_auto(GStrv) missing = NULL; + g_auto(GStrv) with_placeholder = NULL; + g_auto(GStrv) without_placeholder = NULL; + + json_array_add_string_element (arr, "one"); + json_array_add_string_element (arr, "two"); + json_array_add_object_element (arr, g_steal_pointer (&empty)); + json_array_add_string_element (arr, "four"); + json_node_init_array (arr_node, arr); + json_object_set_member (obj, "arr", g_steal_pointer (&arr_node)); + + missing = _srt_json_object_dup_strv_member (obj, "missing", NULL); + g_assert_null (missing); + + with_placeholder = _srt_json_object_dup_strv_member (obj, "arr", "?!"); + g_assert_nonnull (with_placeholder); + g_assert_cmpstr (with_placeholder[0], ==, "one"); + g_assert_cmpstr (with_placeholder[1], ==, "two"); + g_assert_cmpstr (with_placeholder[2], ==, "?!"); + g_assert_cmpstr (with_placeholder[3], ==, "four"); + g_assert_cmpstr (with_placeholder[4], ==, NULL); + + without_placeholder = _srt_json_object_dup_strv_member (obj, "arr", NULL); + g_assert_nonnull (without_placeholder); + g_assert_cmpstr (without_placeholder[0], ==, "one"); + g_assert_cmpstr (without_placeholder[1], ==, "two"); + g_assert_cmpstr (without_placeholder[2], ==, "four"); + g_assert_cmpstr (without_placeholder[3], ==, NULL); +} + +int +main (int argc, + char **argv) +{ + g_test_init (&argc, &argv, NULL); + g_test_add ("/json-utils/dup-strv-member", Fixture, NULL, + setup, test_dup_strv_member, teardown); + + return g_test_run (); +} diff --git a/tests/meson.build b/tests/meson.build index 84e24153020712d2beebf37eaea2dea76a3e0072..b2d5b1db772a355ebc60b5adc754d730136059b9 100644 --- a/tests/meson.build +++ b/tests/meson.build @@ -31,6 +31,7 @@ tests = [ {'name': 'architecture'}, {'name': 'desktop-entry'}, {'name': 'graphics'}, + {'name': 'json-utils', 'static': true}, {'name': 'library'}, {'name': 'locale'}, {'name': 'system-info'},