File: test-json.c

package info (click to toggle)
foundry 1.1~beta-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 15,552 kB
  • sloc: ansic: 167,487; xml: 417; makefile: 21; sh: 19; javascript: 10
file content (169 lines) | stat: -rw-r--r-- 6,201 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/* test-json.c
 *
 * Copyright 2025 Christian Hergert <chergert@redhat.com>
 *
 * 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 General Public License along
 * with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * SPDX-License-Identifier: LGPL-2.1-or-later
 */

#include "config.h"

#include <foundry.h>

static char *
serialize (JsonNode *node)
{
  g_autoptr(JsonGenerator) generator = json_generator_new ();
  json_generator_set_root (generator, node);
  return json_generator_to_data (generator, NULL);
}

static void
compare_json (const char *filename,
              JsonNode   *node)
{
  g_autofree char *path = NULL;
  g_autofree char *contents = NULL;
  g_autofree char *serialized = NULL;
  g_autoptr(GError) error = NULL;
  gsize len;

  g_assert_nonnull (g_getenv ("G_TEST_SRCDIR"));
  g_assert_nonnull (filename);
  g_assert_nonnull (node);

  path = g_build_filename (g_getenv ("G_TEST_SRCDIR"), "test-json", filename, NULL);

  g_file_get_contents (path, &contents, &len, &error);
  g_assert_no_error (error);
  g_assert_nonnull (contents);

  serialized = serialize (node);
  g_assert_nonnull (serialized);

  g_strstrip (contents);
  g_strstrip (serialized);

  g_assert_cmpstr (contents, ==, serialized);

  json_node_unref (node);
}

static void
test_json_object_new (void)
{
  JsonNode *temp = FOUNDRY_JSON_OBJECT_NEW ("key", "val");

  compare_json ("test1.json", FOUNDRY_JSON_OBJECT_NEW ("a", "b"));
  compare_json ("test1.json", FOUNDRY_JSON_OBJECT_NEW ("a", FOUNDRY_JSON_NODE_PUT_STRING ("b")));
  compare_json ("test2.json", FOUNDRY_JSON_OBJECT_NEW ("a", FOUNDRY_JSON_NODE_PUT_BOOLEAN (TRUE)));
  compare_json ("test3.json", FOUNDRY_JSON_OBJECT_NEW ("a", FOUNDRY_JSON_NODE_PUT_BOOLEAN (FALSE)));
  compare_json ("test4.json", FOUNDRY_JSON_OBJECT_NEW ("a", FOUNDRY_JSON_NODE_PUT_DOUBLE (123.45)));
  compare_json ("test5.json", FOUNDRY_JSON_OBJECT_NEW ("a", FOUNDRY_JSON_NODE_PUT_STRV (FOUNDRY_STRV_INIT ("a", "b", "c"))));
  compare_json ("test6.json", FOUNDRY_JSON_OBJECT_NEW ("a", FOUNDRY_JSON_NODE_PUT_INT (G_MAXINT)));
  compare_json ("test7.json",
                FOUNDRY_JSON_OBJECT_NEW ("a", "{",
                                           "aa", "bb",
                                         "}"));
  compare_json ("test8.json",
                FOUNDRY_JSON_OBJECT_NEW ("a", "[",
                                           "aa", "bb",
                                         "]"));
  compare_json ("test9.json", FOUNDRY_JSON_ARRAY_NEW ("a", "b", "c"));
  compare_json ("test10.json", FOUNDRY_JSON_ARRAY_NEW (FOUNDRY_JSON_NODE_PUT_NODE (NULL)));
  compare_json ("test11.json", FOUNDRY_JSON_ARRAY_NEW (FOUNDRY_JSON_NODE_PUT_NODE (temp)));

  json_node_unref (temp);
}

static JsonNode *
load_json (const char *name)
{
  g_autofree char *path = g_build_filename (g_getenv ("G_TEST_SRCDIR"), "test-json", name, NULL);
  g_autofree char *contents = NULL;
  g_autoptr(JsonParser) parser = json_parser_new ();
  g_autoptr(GError) error = NULL;
  gsize len;

  g_file_get_contents (path, &contents, &len, &error);
  g_assert_no_error (error);

  json_parser_load_from_data (parser, contents, len, &error);
  g_assert_no_error (error);

  return json_node_ref (json_parser_get_root (parser));
}

static void
test_json_node_parse (void)
{
  g_autoptr(JsonNode) test1 = load_json ("test1.json");
  g_autoptr(JsonNode) test2 = load_json ("test2.json");
  g_autoptr(JsonNode) test3 = load_json ("test3.json");
  g_autoptr(JsonNode) test4 = load_json ("test4.json");
  g_autoptr(JsonNode) test5 = load_json ("test5.json");
  g_autoptr(JsonNode) test6 = load_json ("test6.json");
  g_autoptr(JsonNode) test7 = load_json ("test7.json");
  g_autoptr(JsonNode) test8 = load_json ("test8.json");
  g_autoptr(JsonNode) test9 = load_json ("test9.json");
  const char *v_str;
  gboolean v_bool;
  double v_dbl;
  const char *v_str1;
  const char *v_str2;
  const char *v_str3;
  gint64 v_int;

  g_assert_true (FOUNDRY_JSON_OBJECT_PARSE (test1, "a", "b"));
  g_assert_false (FOUNDRY_JSON_OBJECT_PARSE (test1, "a", "b", "c", "d"));

  g_assert_true (FOUNDRY_JSON_OBJECT_PARSE (test2, "a", FOUNDRY_JSON_NODE_GET_BOOLEAN (&v_bool)));
  g_assert_false (FOUNDRY_JSON_OBJECT_PARSE (test2, "a", FOUNDRY_JSON_NODE_GET_STRING (&v_str)));
  g_assert_true (v_bool);

  g_assert_true (FOUNDRY_JSON_OBJECT_PARSE (test3, "a", FOUNDRY_JSON_NODE_GET_BOOLEAN (&v_bool)));
  g_assert_false (v_bool);

  g_assert_true (FOUNDRY_JSON_OBJECT_PARSE (test4, "a", FOUNDRY_JSON_NODE_GET_DOUBLE (&v_dbl)));
  g_assert_cmpfloat (v_dbl, ==, 123.45);

  g_assert_true (FOUNDRY_JSON_OBJECT_PARSE (test5,
                                            "a", "[",
                                              FOUNDRY_JSON_NODE_GET_STRING (&v_str1),
                                              FOUNDRY_JSON_NODE_GET_STRING (&v_str2),
                                              FOUNDRY_JSON_NODE_GET_STRING (&v_str3),
                                            "]"));
  g_assert_cmpstr (v_str1, ==, "a");
  g_assert_cmpstr (v_str2, ==, "b");
  g_assert_cmpstr (v_str3, ==, "c");

  g_assert_true (FOUNDRY_JSON_OBJECT_PARSE (test6, "a", FOUNDRY_JSON_NODE_GET_INT (&v_int)));
  g_assert_cmpint (2147483647, ==, v_int);

  g_assert_true (FOUNDRY_JSON_OBJECT_PARSE (test7, "a", "{", "aa", "bb", "}"));
  g_assert_true (FOUNDRY_JSON_OBJECT_PARSE (test8, "a", "[", "aa", "bb", "]"));

  g_assert_true (FOUNDRY_JSON_ARRAY_PARSE (test9, "a", "b", "c"));
}

int
main (int argc,
      char *argv[])
{
  g_test_init (&argc, &argv, NULL);
  g_test_add_func ("/Foundry/Json/Node/new", test_json_object_new);
  g_test_add_func ("/Foundry/Json/Node/parse", test_json_node_parse);
  return g_test_run ();
}