1 /* Selftest support for JSON.
2 Copyright (C) 2024-2025 Free Software Foundation, Inc.
3 Contributed by David Malcolm <dmalcolm@redhat.com>.
5 This file is part of GCC.
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
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
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/>. */
21 #ifndef GCC_SELFTEST_JSON_H
22 #define GCC_SELFTEST_JSON_H
26 /* The selftest code should entirely disappear in a production
27 configuration, hence we guard all of it with #if CHECKING_P. */
33 /* Assert that VALUE is a non-null json::string
34 equalling EXPECTED_VALUE.
35 Use LOC for any failures. */
38 assert_json_string_eq (const location
&loc
,
39 const json::value
*value
,
40 const char *expected_value
);
41 #define ASSERT_JSON_STRING_EQ(JSON_VALUE, EXPECTED_VALUE) \
42 assert_json_string_eq ((SELFTEST_LOCATION), \
46 /* Assert that VALUE is a non-null json::object,
47 returning it as such, failing at LOC if this isn't the case. */
50 expect_json_object (const location
&loc
,
51 const json::value
*value
);
53 /* Assert that VALUE is a non-null json::object that has property
55 Return the value of the property.
56 Use LOC for any failures. */
59 expect_json_object_with_property (const location
&loc
,
60 const json::value
*value
,
61 const char *property_name
);
63 /* Assert that VALUE is a non-null json::object that has property
64 PROPERTY_NAME, and that the value of that property is a non-null
65 json::integer_number equalling EXPECTED_VALUE.
66 Use LOC for any failures. */
69 assert_json_int_property_eq (const location
&loc
,
70 const json::value
*value
,
71 const char *property_name
,
73 #define ASSERT_JSON_INT_PROPERTY_EQ(JSON_VALUE, PROPERTY_NAME, EXPECTED_VALUE) \
74 assert_json_int_property_eq ((SELFTEST_LOCATION), \
79 /* Assert that VALUE is a non-null json::object that has property
80 PROPERTY_NAME, and that the property value is a non-null JSON object.
81 Return the value of the property as a json::object.
82 Use LOC for any failures. */
85 expect_json_object_with_object_property (const location
&loc
,
86 const json::value
*value
,
87 const char *property_name
);
88 #define EXPECT_JSON_OBJECT_WITH_OBJECT_PROPERTY(JSON_VALUE, PROPERTY_NAME) \
89 expect_json_object_with_object_property ((SELFTEST_LOCATION), \
93 /* Assert that VALUE is a non-null json::object that has property
94 PROPERTY_NAME, and that the property value is a non-null JSON array.
95 Return the value of the property as a json::array.
96 Use LOC for any failures. */
99 expect_json_object_with_array_property (const location
&loc
,
100 const json::value
*value
,
101 const char *property_name
);
102 #define EXPECT_JSON_OBJECT_WITH_ARRAY_PROPERTY(JSON_VALUE, PROPERTY_NAME) \
103 expect_json_object_with_array_property ((SELFTEST_LOCATION), \
107 /* Assert that VALUE is a non-null json::object that has property
108 PROPERTY_NAME, and that the property value is a non-null JSON string.
109 Return the value of the property as a json::string.
110 Use LOC for any failures. */
113 expect_json_object_with_string_property (const location
&loc
,
114 const json::value
*value
,
115 const char *property_name
);
116 #define EXPECT_JSON_OBJECT_WITH_STRING_PROPERTY(JSON_VALUE, PROPERTY_NAME) \
117 expect_json_object_with_string_property ((SELFTEST_LOCATION), \
121 /* Assert that VALUE is a non-null json::object that has property
122 PROPERTY_NAME, and that the value of that property is a non-null
123 JSON string equalling EXPECTED_VALUE.
124 Use LOC for any failures. */
127 assert_json_string_property_eq (const location
&loc
,
128 const json::value
*value
,
129 const char *property_name
,
130 const char *expected_value
);
131 #define ASSERT_JSON_STRING_PROPERTY_EQ(JSON_VALUE, PROPERTY_NAME, EXPECTED_VALUE) \
132 assert_json_string_property_eq ((SELFTEST_LOCATION), \
137 } // namespace selftest
139 #endif /* #if CHECKING_P */
141 #endif /* GCC_SELFTEST_JSON_H */