Line data Source code
1 : /* Selftest support for JSON.
2 : Copyright (C) 2024-2026 Free Software Foundation, Inc.
3 : Contributed by David Malcolm <dmalcolm@redhat.com>.
4 :
5 : This file is part of GCC.
6 :
7 : GCC is free software; you can redistribute it and/or modify it under
8 : the terms of the GNU General Public License as published by the Free
9 : Software Foundation; either version 3, or (at your option) any later
10 : version.
11 :
12 : GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 : WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 : FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 : for more details.
16 :
17 : You should have received a copy of the GNU General Public License
18 : along with GCC; see the file COPYING3. If not see
19 : <http://www.gnu.org/licenses/>. */
20 :
21 : #define INCLUDE_STRING
22 : #include "config.h"
23 : #include "system.h"
24 : #include "coretypes.h"
25 : #include "diagnostic.h"
26 : #include "selftest.h"
27 : #include "selftest-json.h"
28 :
29 : /* The selftest code should entirely disappear in a production
30 : configuration, hence we guard all of it with #if CHECKING_P. */
31 :
32 : #if CHECKING_P
33 :
34 : namespace selftest {
35 :
36 : /* Assert that VALUE is a non-null json::string
37 : equalling EXPECTED_VALUE.
38 : Use LOC for any failures. */
39 :
40 : void
41 1260 : assert_json_string_eq (const location &loc,
42 : const json::value *value,
43 : const char *expected_value)
44 : {
45 1260 : ASSERT_EQ_AT (loc, value->get_kind (), json::JSON_STRING);
46 1260 : const json::string *str = static_cast<const json::string *> (value);
47 1260 : ASSERT_STREQ_AT (loc, expected_value, str->get_string ());
48 1260 : }
49 :
50 : /* Assert that VALUE is a non-null json::object,
51 : returning it as such, failing at LOC if this isn't the case. */
52 :
53 : const json::object *
54 5872 : expect_json_object (const location &loc,
55 : const json::value *value)
56 : {
57 5872 : ASSERT_NE_AT (loc, value, nullptr);
58 5872 : ASSERT_EQ_AT (loc, value->get_kind (), json::JSON_OBJECT);
59 5872 : return static_cast<const json::object *> (value);
60 : }
61 :
62 : /* Assert that VALUE is a non-null json::object that has property
63 : PROPERTY_NAME.
64 : Return the value of the property.
65 : Use LOC for any failures. */
66 :
67 : const json::value *
68 5840 : expect_json_object_with_property (const location &loc,
69 : const json::value *value,
70 : const char *property_name)
71 : {
72 5840 : const json::object *obj = expect_json_object (loc, value);
73 5840 : const json::value *property_value = obj->get (property_name);
74 5840 : ASSERT_NE_AT (loc, property_value, nullptr);
75 5840 : return property_value;
76 : }
77 :
78 : /* Assert that VALUE is a non-null json::object that has property
79 : PROPERTY_NAME, and that the value of that property is a non-null
80 : json::integer_number equalling EXPECTED_VALUE.
81 : Use LOC for any failures. */
82 :
83 : void
84 2184 : assert_json_int_property_eq (const location &loc,
85 : const json::value *value,
86 : const char *property_name,
87 : long expected_value)
88 : {
89 2184 : const json::value *property_value
90 2184 : = expect_json_object_with_property (loc, value, property_name);
91 2184 : ASSERT_EQ_AT (loc, property_value->get_kind (), json::JSON_INTEGER);
92 2184 : long actual_value
93 2184 : = static_cast<const json::integer_number *> (property_value)->get ();
94 2184 : ASSERT_EQ_AT (loc, expected_value, actual_value);
95 2184 : }
96 :
97 : /* Assert that VALUE is a non-null json::object that has property
98 : PROPERTY_NAME, and that the property value is a non-null JSON object.
99 : Return the value of the property as a json::object.
100 : Use LOC for any failures. */
101 :
102 : const json::object *
103 1752 : expect_json_object_with_object_property (const location &loc,
104 : const json::value *value,
105 : const char *property_name)
106 : {
107 1752 : const json::value *property_value
108 1752 : = expect_json_object_with_property (loc, value, property_name);
109 1752 : ASSERT_EQ_AT (loc, property_value->get_kind (), json::JSON_OBJECT);
110 1752 : return static_cast<const json::object *> (property_value);
111 : }
112 :
113 : /* Assert that VALUE is a non-null json::object that has property
114 : PROPERTY_NAME, and that the property value is a non-null JSON array.
115 : Return the value of the property as a json::array.
116 : Use LOC for any failures. */
117 :
118 : const json::array *
119 632 : expect_json_object_with_array_property (const location &loc,
120 : const json::value *value,
121 : const char *property_name)
122 : {
123 632 : const json::value *property_value
124 632 : = expect_json_object_with_property (loc, value, property_name);
125 632 : ASSERT_EQ_AT (loc, property_value->get_kind (), json::JSON_ARRAY);
126 632 : return static_cast<const json::array *> (property_value);
127 : }
128 :
129 : /* Assert that VALUE is a non-null json::object that has property
130 : PROPERTY_NAME, and that the property value is a non-null JSON string.
131 : Return the value of the property as a json::string.
132 : Use LOC for any failures. */
133 :
134 : const json::string *
135 24 : expect_json_object_with_string_property (const location &loc,
136 : const json::value *value,
137 : const char *property_name)
138 : {
139 24 : const json::value *property_value
140 24 : = expect_json_object_with_property (loc, value, property_name);
141 24 : ASSERT_EQ_AT (loc, property_value->get_kind (), json::JSON_STRING);
142 24 : return static_cast<const json::string *> (property_value);
143 : }
144 :
145 : /* Assert that VALUE is a non-null json::object that has property
146 : PROPERTY_NAME, and that the value of that property is a non-null
147 : JSON string equalling EXPECTED_VALUE.
148 : Use LOC for any failures. */
149 :
150 : void
151 1248 : assert_json_string_property_eq (const location &loc,
152 : const json::value *value,
153 : const char *property_name,
154 : const char *expected_value)
155 : {
156 1248 : const json::value *property_value
157 1248 : = expect_json_object_with_property (loc, value, property_name);
158 1248 : assert_json_string_eq (loc, property_value, expected_value);
159 1248 : }
160 :
161 : } // namespace selftest
162 :
163 : #endif /* #if CHECKING_P */
|