GCC Middle and Back End API Reference
selftest-json.h
Go to the documentation of this file.
1/* Selftest support for JSON.
2 Copyright (C) 2024-2025 Free Software Foundation, Inc.
3 Contributed by David Malcolm <dmalcolm@redhat.com>.
4
5This file is part of GCC.
6
7GCC is free software; you can redistribute it and/or modify it under
8the terms of the GNU General Public License as published by the Free
9Software Foundation; either version 3, or (at your option) any later
10version.
11
12GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13WARRANTY; without even the implied warranty of MERCHANTABILITY or
14FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15for more details.
16
17You should have received a copy of the GNU General Public License
18along with GCC; see the file COPYING3. If not see
19<http://www.gnu.org/licenses/>. */
20
21#ifndef GCC_SELFTEST_JSON_H
22#define GCC_SELFTEST_JSON_H
23
24#include "json.h"
25
26/* The selftest code should entirely disappear in a production
27 configuration, hence we guard all of it with #if CHECKING_P. */
28
29#if CHECKING_P
30
31namespace selftest {
32
33/* Assert that VALUE is a non-null json::string
34 equalling EXPECTED_VALUE.
35 Use LOC for any failures. */
36
37void
38assert_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), \
43 (JSON_VALUE), \
44 (EXPECTED_VALUE))
45
46/* Assert that VALUE is a non-null json::object,
47 returning it as such, failing at LOC if this isn't the case. */
48
49const json::object *
50expect_json_object (const location &loc,
51 const json::value *value);
52
53/* Assert that VALUE is a non-null json::object that has property
54 PROPERTY_NAME.
55 Return the value of the property.
56 Use LOC for any failures. */
57
58const json::value *
59expect_json_object_with_property (const location &loc,
60 const json::value *value,
61 const char *property_name);
62
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. */
67
68void
69assert_json_int_property_eq (const location &loc,
70 const json::value *value,
71 const char *property_name,
72 long expected_value);
73#define ASSERT_JSON_INT_PROPERTY_EQ(JSON_VALUE, PROPERTY_NAME, EXPECTED_VALUE) \
74 assert_json_int_property_eq ((SELFTEST_LOCATION), \
75 (JSON_VALUE), \
76 (PROPERTY_NAME), \
77 (EXPECTED_VALUE))
78
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. */
83
84const json::object *
85expect_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), \
90 (JSON_VALUE), \
91 (PROPERTY_NAME))
92
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. */
97
98const json::array *
99expect_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), \
104 (JSON_VALUE), \
105 (PROPERTY_NAME))
106
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. */
111
112const json::string *
113expect_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), \
118 (JSON_VALUE), \
119 (PROPERTY_NAME))
120
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. */
125
126void
127assert_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), \
133 (JSON_VALUE), \
134 (PROPERTY_NAME), \
135 (EXPECTED_VALUE))
136
137} // namespace selftest
138
139#endif /* #if CHECKING_P */
140
141#endif /* GCC_SELFTEST_JSON_H */
Definition json.h:142
Definition json.h:96
Definition json.h:216
Definition json.h:79
Definition dump-context.h:31
Definition predict.cc:522