aboutsummaryrefslogtreecommitdiff
path: root/gcc/selftest-json.cc
blob: 1480ded6ba00f34d915202e7facd4a99eb5f5389 (plain)
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
/* Selftest support for JSON.
   Copyright (C) 2024 Free Software Foundation, Inc.
   Contributed by David Malcolm <dmalcolm@redhat.com>.

This file is part of GCC.

GCC is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free
Software Foundation; either version 3, or (at your option) any later
version.

GCC 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 General Public License
for more details.

You should have received a copy of the GNU General Public License
along with GCC; see the file COPYING3.  If not see
<http://www.gnu.org/licenses/>.  */

#include "config.h"
#include "system.h"
#include "coretypes.h"
#include "diagnostic.h"
#include "selftest.h"
#include "selftest-json.h"

/* The selftest code should entirely disappear in a production
   configuration, hence we guard all of it with #if CHECKING_P.  */

#if CHECKING_P

namespace selftest {

/* Assert that VALUE is a non-null json::string
   equalling EXPECTED_VALUE.
   Use LOC for any failures.  */

void
assert_json_string_eq (const location &loc,
		       const json::value *value,
		       const char *expected_value)
{
  ASSERT_EQ_AT (loc, value->get_kind (), json::JSON_STRING);
  const json::string *str = static_cast<const json::string *> (value);
  ASSERT_STREQ_AT (loc, expected_value, str->get_string ());
}

/* Assert that VALUE is a non-null json::object,
   returning it as such, failing at LOC if this isn't the case.  */

const json::object *
expect_json_object (const location &loc,
		    const json::value *value)
{
  ASSERT_NE_AT (loc, value, nullptr);
  ASSERT_EQ_AT (loc, value->get_kind (), json::JSON_OBJECT);
  return static_cast<const json::object *> (value);
}

/* Assert that VALUE is a non-null json::object that has property
   PROPERTY_NAME.
   Return the value of the property.
   Use LOC for any failures.  */

const json::value *
expect_json_object_with_property (const location &loc,
				  const json::value *value,
				  const char *property_name)
{
  const json::object *obj = expect_json_object (loc, value);
  const json::value *property_value = obj->get (property_name);
  ASSERT_NE_AT (loc, property_value, nullptr);
  return property_value;
}

/* Assert that VALUE is a non-null json::object that has property
   PROPERTY_NAME, and that the value of that property is a non-null
   json::integer_number equalling EXPECTED_VALUE.
   Use LOC for any failures.  */

void
assert_json_int_property_eq (const location &loc,
			     const json::value *value,
			     const char *property_name,
			     long expected_value)
{
  const json::value *property_value
    = expect_json_object_with_property (loc, value, property_name);
  ASSERT_EQ_AT (loc, property_value->get_kind (), json::JSON_INTEGER);
  long actual_value
    = static_cast<const json::integer_number *> (property_value)->get ();
  ASSERT_EQ_AT (loc, expected_value, actual_value);
}

/* Assert that VALUE is a non-null json::object that has property
   PROPERTY_NAME, and that the property value is a non-null JSON object.
   Return the value of the property as a json::object.
   Use LOC for any failures.  */

const json::object *
expect_json_object_with_object_property (const location &loc,
					 const json::value *value,
					 const char *property_name)
{
  const json::value *property_value
    = expect_json_object_with_property (loc, value, property_name);
  ASSERT_EQ_AT (loc, property_value->get_kind (), json::JSON_OBJECT);
  return static_cast<const json::object *> (property_value);
}

/* Assert that VALUE is a non-null json::object that has property
   PROPERTY_NAME, and that the property value is a non-null JSON array.
   Return the value of the property as a json::array.
   Use LOC for any failures.  */

const json::array *
expect_json_object_with_array_property (const location &loc,
					const json::value *value,
					const char *property_name)
{
  const json::value *property_value
    = expect_json_object_with_property (loc, value, property_name);
  ASSERT_EQ_AT (loc, property_value->get_kind (), json::JSON_ARRAY);
  return static_cast<const json::array *> (property_value);
}

/* Assert that VALUE is a non-null json::object that has property
   PROPERTY_NAME, and that the property value is a non-null JSON string.
   Return the value of the property as a json::string.
   Use LOC for any failures.  */

const json::string *
expect_json_object_with_string_property (const location &loc,
					 const json::value *value,
					 const char *property_name)
{
  const json::value *property_value
    = expect_json_object_with_property (loc, value, property_name);
  ASSERT_EQ_AT (loc, property_value->get_kind (), json::JSON_STRING);
  return static_cast<const json::string *> (property_value);
}

/* Assert that VALUE is a non-null json::object that has property
   PROPERTY_NAME, and that the value of that property is a non-null
   JSON string equalling EXPECTED_VALUE.
   Use LOC for any failures.  */

void
assert_json_string_property_eq (const location &loc,
				const json::value *value,
				const char *property_name,
				const char *expected_value)
{
  const json::value *property_value
    = expect_json_object_with_property (loc, value, property_name);
  assert_json_string_eq (loc, property_value, expected_value);
}

} // namespace selftest

#endif /* #if CHECKING_P */