| 1 |
/* Selftest support for JSON. |
| 2 |
Copyright (C) 2024-2025 Free Software Foundation, Inc. |
| 3 |
Contributed by David Malcolm <[email protected]>. |
| 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 |
#include "config.h" |
| 22 |
#include "system.h" |
| 23 |
#include "coretypes.h" |
| 24 |
#include "diagnostic.h" |
| 25 |
#include "selftest.h" |
| 26 |
#include "selftest-json.h" |
| 27 |
|
| 28 |
/* The selftest code should entirely disappear in a production |
| 29 |
configuration, hence we guard all of it with #if CHECKING_P. */ |
| 30 |
|
| 31 |
#if CHECKING_P |
| 32 |
|
| 33 |
namespace selftest { |
| 34 |
|
| 35 |
/* Assert that VALUE is a non-null json::string |
| 36 |
equalling EXPECTED_VALUE. |
| 37 |
Use LOC for any failures. */ |
| 38 |
|
| 39 |
void |
| 40 |
assert_json_string_eq (const location &loc, |
| 41 |
const json::value *value, |
| 42 |
const char *expected_value) |
| 43 |
{ |
| 44 |
ASSERT_EQ_AT (loc, value->get_kind (), json::JSON_STRING); |
| 45 |
const json::string *str = static_cast<const json::string *> (value); |
| 46 |
ASSERT_STREQ_AT (loc, expected_value, str->get_string ()); |
| 47 |
} |
| 48 |
|
| 49 |
/* Assert that VALUE is a non-null json::object, |
| 50 |
returning it as such, failing at LOC if this isn't the case. */ |
| 51 |
|
| 52 |
const json::object * |
| 53 |
expect_json_object (const location &loc, |
| 54 |
const json::value *value) |
| 55 |
{ |
| 56 |
ASSERT_NE_AT (loc, value, nullptr); |
| 57 |
ASSERT_EQ_AT (loc, value->get_kind (), json::JSON_OBJECT); |
| 58 |
return static_cast<const json::object *> (value); |
| 59 |
} |
| 60 |
|
| 61 |
/* Assert that VALUE is a non-null json::object that has property |
| 62 |
PROPERTY_NAME. |
| 63 |
Return the value of the property. |
| 64 |
Use LOC for any failures. */ |
| 65 |
|
| 66 |
const json::value * |
| 67 |
expect_json_object_with_property (const location &loc, |
| 68 |
const json::value *value, |
| 69 |
const char *property_name) |
| 70 |
{ |
| 71 |
const json::object *obj = expect_json_object (loc, value); |
| 72 |
const json::value *property_value = obj->get (property_name); |
| 73 |
ASSERT_NE_AT (loc, property_value, nullptr); |
| 74 |
return property_value; |
| 75 |
} |
| 76 |
|
| 77 |
/* Assert that VALUE is a non-null json::object that has property |
| 78 |
PROPERTY_NAME, and that the value of that property is a non-null |
| 79 |
json::integer_number equalling EXPECTED_VALUE. |
| 80 |
Use LOC for any failures. */ |
| 81 |
|
| 82 |
void |
| 83 |
assert_json_int_property_eq (const location &loc, |
| 84 |
const json::value *value, |
| 85 |
const char *property_name, |
| 86 |
long expected_value) |
| 87 |
{ |
| 88 |
const json::value *property_value |
| 89 |
= expect_json_object_with_property (loc, value, property_name); |
| 90 |
ASSERT_EQ_AT (loc, property_value->get_kind (), json::JSON_INTEGER); |
| 91 |
long actual_value |
| 92 |
= static_cast<const json::integer_number *> (property_value)->get (); |
| 93 |
ASSERT_EQ_AT (loc, expected_value, actual_value); |
| 94 |
} |
| 95 |
|
| 96 |
/* Assert that VALUE is a non-null json::object that has property |
| 97 |
PROPERTY_NAME, and that the property value is a non-null JSON object. |
| 98 |
Return the value of the property as a json::object. |
| 99 |
Use LOC for any failures. */ |
| 100 |
|
| 101 |
const json::object * |
| 102 |
expect_json_object_with_object_property (const location &loc, |
| 103 |
const json::value *value, |
| 104 |
const char *property_name) |
| 105 |
{ |
| 106 |
const json::value *property_value |
| 107 |
= expect_json_object_with_property (loc, value, property_name); |
| 108 |
ASSERT_EQ_AT (loc, property_value->get_kind (), json::JSON_OBJECT); |
| 109 |
return static_cast<const json::object *> (property_value); |
| 110 |
} |
| 111 |
|
| 112 |
/* Assert that VALUE is a non-null json::object that has property |
| 113 |
PROPERTY_NAME, and that the property value is a non-null JSON array. |
| 114 |
Return the value of the property as a json::array. |
| 115 |
Use LOC for any failures. */ |
| 116 |
|
| 117 |
const json::array * |
| 118 |
expect_json_object_with_array_property (const location &loc, |
| 119 |
const json::value *value, |
| 120 |
const char *property_name) |
| 121 |
{ |
| 122 |
const json::value *property_value |
| 123 |
= expect_json_object_with_property (loc, value, property_name); |
| 124 |
ASSERT_EQ_AT (loc, property_value->get_kind (), json::JSON_ARRAY); |
| 125 |
return static_cast<const json::array *> (property_value); |
| 126 |
} |
| 127 |
|
| 128 |
/* Assert that VALUE is a non-null json::object that has property |
| 129 |
PROPERTY_NAME, and that the property value is a non-null JSON string. |
| 130 |
Return the value of the property as a json::string. |
| 131 |
Use LOC for any failures. */ |
| 132 |
|
| 133 |
const json::string * |
| 134 |
expect_json_object_with_string_property (const location &loc, |
| 135 |
const json::value *value, |
| 136 |
const char *property_name) |
| 137 |
{ |
| 138 |
const json::value *property_value |
| 139 |
= expect_json_object_with_property (loc, value, property_name); |
| 140 |
ASSERT_EQ_AT (loc, property_value->get_kind (), json::JSON_STRING); |
| 141 |
return static_cast<const json::string *> (property_value); |
| 142 |
} |
| 143 |
|
| 144 |
/* Assert that VALUE is a non-null json::object that has property |
| 145 |
PROPERTY_NAME, and that the value of that property is a non-null |
| 146 |
JSON string equalling EXPECTED_VALUE. |
| 147 |
Use LOC for any failures. */ |
| 148 |
|
| 149 |
void |
| 150 |
assert_json_string_property_eq (const location &loc, |
| 151 |
const json::value *value, |
| 152 |
const char *property_name, |
| 153 |
const char *expected_value) |
| 154 |
{ |
| 155 |
const json::value *property_value |
| 156 |
= expect_json_object_with_property (loc, value, property_name); |
| 157 |
assert_json_string_eq (loc, property_value, expected_value); |
| 158 |
} |
| 159 |
|
| 160 |
} // namespace selftest |
| 161 |
|
| 162 |
#endif /* #if CHECKING_P */ |