https://gcc.gnu.org/g:b958cb790e142d5393e30da65ad8c8a6369af3e3
commit r17-5824-gb958cb790e142d5393e30da65ad8c8a6369af3e3 Author: David Malcolm <[email protected]> Date: Sat Oct 31 11:07:22 2026 -0400 json: expose ASSERT_PRINT_EQ as ASSERT_JSON_PRINT_EQ Replace a couple of repeated ASSERT_PRINT_EQ for selftests of json with a single ASSERT_JSON_PRINT_EQ in json.h. No functional change intended. gcc/ChangeLog: * json-parsing.cc (ASSERT_PRINT_EQ): Move to json.h and rename to ASSERT_JSON_PRINT_EQ. (selftest::test_parse_string): Update for above renaming. (selftest::test_parse_number): Likewise. (selftest::test_parse_array): Likewise. (selftest::test_parse_literals): Likewise. (selftest::test_parse_empty_object): Likewise. * json.cc (selftest::assert_print_eq): Rename to... (selftest::assert_json_print_eq): ...this. (ASSERT_PRINT_EQ): Delete. (selftest::test_writing_objects): Update for renaming of ASSERT_PRINT_EQ to ASSERT_JSON_PRINT_EQ. (selftest::test_writing_arrays): Likewise. (selftest::test_writing_float_numbers): Likewise. (selftest::test_writing_integer_numbers): Likewise. (selftest::test_writing_strings): Likewise. (selftest::test_writing_literals): Likewise. (selftest::test_formatting): Likewise. (selftest::test_comparisons): Likewise. * json.h (selftest::assert_print_eq): Rename decl to... (selftest::assert_json_print_eq): ...this. (ASSERT_JSON_PRINT_EQ): New, moved here from json-parsing.cc. Signed-off-by: David Malcolm <[email protected]> Diff: --- gcc/json-parsing.cc | 27 ++++++------- gcc/json.cc | 106 ++++++++++++++++++++++++++-------------------------- gcc/json.h | 13 +++++-- 3 files changed, 74 insertions(+), 72 deletions(-) diff --git a/gcc/json-parsing.cc b/gcc/json-parsing.cc index acbcff8ed443..db9566853f81 100644 --- a/gcc/json-parsing.cc +++ b/gcc/json-parsing.cc @@ -1420,9 +1420,6 @@ namespace selftest { /* Selftests. */ -#define ASSERT_PRINT_EQ(JV, FORMATTED, EXPECTED_JSON) \ - assert_print_eq (SELFTEST_LOCATION, JV, FORMATTED, EXPECTED_JSON) - /* Implementation detail of ASSERT_RANGE_EQ. */ static void @@ -1921,7 +1918,7 @@ test_parse_string () const json::value *jv = tc.get_value (); ASSERT_EQ (jv->get_kind (), JSON_STRING); ASSERT_STREQ (as_a <const json::string *> (jv)->get_string (), "foo"); - ASSERT_PRINT_EQ (*jv, true, "\"foo\""); + ASSERT_JSON_PRINT_EQ (*jv, true, "\"foo\""); auto range = tc.get_range_for_value (jv); ASSERT_TRUE (range); ASSERT_RANGE_EQ (*range, @@ -1937,7 +1934,7 @@ test_parse_string () ASSERT_EQ (jv->get_kind (), JSON_STRING); ASSERT_STREQ (as_a <const json::string *> (jv)->get_string (), "before \"quoted\" after"); - ASSERT_PRINT_EQ (*jv, true, contains_quotes); + ASSERT_JSON_PRINT_EQ (*jv, true, contains_quotes); auto range = tc.get_range_for_value (jv); ASSERT_TRUE (range); ASSERT_RANGE_EQ (*range, @@ -1976,7 +1973,7 @@ test_parse_string () ASSERT_STREQ (as_a <const json::string *> (jv)->get_string (), "\346\226\207" "\345\255\227" "\345\214\226" "\343\201\221"); /* Result of dump should be UTF-8 encoded, with quotes. */ - ASSERT_PRINT_EQ (*jv, false, mojibake); + ASSERT_JSON_PRINT_EQ (*jv, false, mojibake); auto range = tc.get_range_for_value (jv); ASSERT_TRUE (range); ASSERT_RANGE_EQ (*range, @@ -1995,7 +1992,7 @@ test_parse_string () ASSERT_STREQ (as_a <const json::string *> (jv)->get_string (), "\346\226\207" "\345\255\227" "\345\214\226" "\343\201\221"); /* Result of dump should be UTF-8 encoded, with quotes. */ - ASSERT_PRINT_EQ (*jv, false, mojibake); + ASSERT_JSON_PRINT_EQ (*jv, false, mojibake); auto range = tc.get_range_for_value (jv); ASSERT_TRUE (range); ASSERT_RANGE_EQ (*range, @@ -2017,7 +2014,7 @@ test_parse_number () const json::value *jv = tc.get_value (); ASSERT_EQ (jv->get_kind (), JSON_INTEGER); ASSERT_EQ (as_a <const json::integer_number *> (jv)->get (), 42.0); - ASSERT_PRINT_EQ (*jv, true, "42"); + ASSERT_JSON_PRINT_EQ (*jv, true, "42"); auto range = tc.get_range_for_value (jv); ASSERT_TRUE (range); ASSERT_RANGE_EQ (*range, @@ -2032,7 +2029,7 @@ test_parse_number () const json::value *jv = tc.get_value (); ASSERT_EQ (jv->get_kind (), JSON_INTEGER); ASSERT_EQ (as_a<const json::integer_number *> (jv)->get (), -17.0); - ASSERT_PRINT_EQ (*jv, true, "-17"); + ASSERT_JSON_PRINT_EQ (*jv, true, "-17"); auto range = tc.get_range_for_value (jv); ASSERT_TRUE (range); ASSERT_RANGE_EQ (*range, @@ -2074,7 +2071,7 @@ test_parse_number () const json::value *jv = tc.get_value (); ASSERT_EQ (jv->get_kind (), JSON_INTEGER); ASSERT_EQ (as_a <const json::integer_number *> (jv)->get (), 4200); - ASSERT_PRINT_EQ (*jv, true, "4200"); + ASSERT_JSON_PRINT_EQ (*jv, true, "4200"); auto range = tc.get_range_for_value (jv); ASSERT_TRUE (range); ASSERT_RANGE_EQ (*range, @@ -2126,7 +2123,7 @@ test_parse_array () offset, line_1, offset, offset, line_1, offset); } - ASSERT_PRINT_EQ (*jv, false, "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]"); + ASSERT_JSON_PRINT_EQ (*jv, false, "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]"); } /* Verify that JSON object parsing works. */ @@ -2208,7 +2205,7 @@ test_parse_literals () const json::value *jv = tc.get_value (); ASSERT_NE (jv, nullptr); ASSERT_EQ (jv->get_kind (), JSON_TRUE); - ASSERT_PRINT_EQ (*jv, false, "true"); + ASSERT_JSON_PRINT_EQ (*jv, false, "true"); auto range = tc.get_range_for_value (jv); ASSERT_TRUE (range); ASSERT_RANGE_EQ (*range, @@ -2222,7 +2219,7 @@ test_parse_literals () const json::value *jv = tc.get_value (); ASSERT_NE (jv, nullptr); ASSERT_EQ (jv->get_kind (), JSON_FALSE); - ASSERT_PRINT_EQ (*jv, false, "false"); + ASSERT_JSON_PRINT_EQ (*jv, false, "false"); auto range = tc.get_range_for_value (jv); ASSERT_TRUE (range); ASSERT_RANGE_EQ (*range, @@ -2236,7 +2233,7 @@ test_parse_literals () const json::value *jv = tc.get_value (); ASSERT_NE (jv, nullptr); ASSERT_EQ (jv->get_kind (), JSON_NULL); - ASSERT_PRINT_EQ (*jv, false, "null"); + ASSERT_JSON_PRINT_EQ (*jv, false, "null"); auto range = tc.get_range_for_value (jv); ASSERT_TRUE (range); ASSERT_RANGE_EQ (*range, @@ -2284,7 +2281,7 @@ test_parse_empty_object () const json::value *jv = tc.get_value (); ASSERT_NE (jv, nullptr); ASSERT_EQ (jv->get_kind (), JSON_OBJECT); - ASSERT_PRINT_EQ (*jv, true, "{}"); + ASSERT_JSON_PRINT_EQ (*jv, true, "{}"); auto range = tc.get_range_for_value (jv); ASSERT_TRUE (range); ASSERT_RANGE_EQ (*range, diff --git a/gcc/json.cc b/gcc/json.cc index c776d02d3351..af3346516fe7 100644 --- a/gcc/json.cc +++ b/gcc/json.cc @@ -714,19 +714,16 @@ namespace selftest { /* Verify that JV->print () prints EXPECTED_JSON. */ void -assert_print_eq (const location &loc, - const json::value &jv, - bool formatted, - const char *expected_json) +assert_json_print_eq (const location &loc, + const json::value &jv, + bool formatted, + const char *expected_json) { pretty_printer pp; jv.print (&pp, formatted); ASSERT_STREQ_AT (loc, expected_json, pp_formatted_text (&pp)); } -#define ASSERT_PRINT_EQ(JV, FORMATTED, EXPECTED_JSON) \ - assert_print_eq (SELFTEST_LOCATION, JV, FORMATTED, EXPECTED_JSON) - /* Verify that object::get works as expected. */ static void @@ -751,13 +748,15 @@ test_writing_objects () /* This test relies on json::object writing out key/value pairs in key-insertion order. */ - ASSERT_PRINT_EQ (obj, true, - "{\"foo\": \"bar\",\n" - " \"baz\": \"quux\",\n" - " \"\\\"\\\\\\b\\f\\n\\r\\t\": \"value for awkward key\"}"); - ASSERT_PRINT_EQ (obj, false, - "{\"foo\": \"bar\", \"baz\": \"quux\"" - ", \"\\\"\\\\\\b\\f\\n\\r\\t\": \"value for awkward key\"}"); + ASSERT_JSON_PRINT_EQ + (obj, true, + "{\"foo\": \"bar\",\n" + " \"baz\": \"quux\",\n" + " \"\\\"\\\\\\b\\f\\n\\r\\t\": \"value for awkward key\"}"); + ASSERT_JSON_PRINT_EQ + (obj, false, + "{\"foo\": \"bar\", \"baz\": \"quux\"" + ", \"\\\"\\\\\\b\\f\\n\\r\\t\": \"value for awkward key\"}"); } /* Verify that JSON arrays are written correctly. */ @@ -766,17 +765,17 @@ static void test_writing_arrays () { array arr; - ASSERT_PRINT_EQ (arr, true, "[]"); + ASSERT_JSON_PRINT_EQ (arr, true, "[]"); arr.append (new json::string ("foo")); - ASSERT_PRINT_EQ (arr, true, "[\"foo\"]"); + ASSERT_JSON_PRINT_EQ (arr, true, "[\"foo\"]"); arr.append_string ("bar"); - ASSERT_PRINT_EQ (arr, true, - "[\"foo\",\n" - " \"bar\"]"); - ASSERT_PRINT_EQ (arr, false, - "[\"foo\", \"bar\"]"); + ASSERT_JSON_PRINT_EQ (arr, true, + "[\"foo\",\n" + " \"bar\"]"); + ASSERT_JSON_PRINT_EQ (arr, false, + "[\"foo\", \"bar\"]"); } /* Verify that JSON numbers are written correctly. */ @@ -784,20 +783,20 @@ test_writing_arrays () static void test_writing_float_numbers () { - ASSERT_PRINT_EQ (float_number (0), true, "0"); - ASSERT_PRINT_EQ (float_number (42), true, "42"); - ASSERT_PRINT_EQ (float_number (-100), true, "-100"); - ASSERT_PRINT_EQ (float_number (123456789), true, "1.23457e+08"); + ASSERT_JSON_PRINT_EQ (float_number (0), true, "0"); + ASSERT_JSON_PRINT_EQ (float_number (42), true, "42"); + ASSERT_JSON_PRINT_EQ (float_number (-100), true, "-100"); + ASSERT_JSON_PRINT_EQ (float_number (123456789), true, "1.23457e+08"); } static void test_writing_integer_numbers () { - ASSERT_PRINT_EQ (integer_number (0), true, "0"); - ASSERT_PRINT_EQ (integer_number (42), true, "42"); - ASSERT_PRINT_EQ (integer_number (-100), true, "-100"); - ASSERT_PRINT_EQ (integer_number (123456789), true, "123456789"); - ASSERT_PRINT_EQ (integer_number (-123456789), true, "-123456789"); + ASSERT_JSON_PRINT_EQ (integer_number (0), true, "0"); + ASSERT_JSON_PRINT_EQ (integer_number (42), true, "42"); + ASSERT_JSON_PRINT_EQ (integer_number (-100), true, "-100"); + ASSERT_JSON_PRINT_EQ (integer_number (123456789), true, "123456789"); + ASSERT_JSON_PRINT_EQ (integer_number (-123456789), true, "-123456789"); } /* Verify that JSON strings are written correctly. */ @@ -806,16 +805,17 @@ static void test_writing_strings () { string foo ("foo"); - ASSERT_PRINT_EQ (foo, true, "\"foo\""); + ASSERT_JSON_PRINT_EQ (foo, true, "\"foo\""); string contains_quotes ("before \"quoted\" after"); - ASSERT_PRINT_EQ (contains_quotes, true, "\"before \\\"quoted\\\" after\""); + ASSERT_JSON_PRINT_EQ (contains_quotes, true, + "\"before \\\"quoted\\\" after\""); const char data[] = {'a', 'b', 'c', 'd', '\0', 'e', 'f'}; string not_terminated (data, 3); - ASSERT_PRINT_EQ (not_terminated, true, "\"abc\""); + ASSERT_JSON_PRINT_EQ (not_terminated, true, "\"abc\""); string embedded_null (data, sizeof data); - ASSERT_PRINT_EQ (embedded_null, true, "\"abcd\\0ef\""); + ASSERT_JSON_PRINT_EQ (embedded_null, true, "\"abcd\\0ef\""); } /* Verify that JSON literals are written correctly. */ @@ -823,12 +823,12 @@ test_writing_strings () static void test_writing_literals () { - ASSERT_PRINT_EQ (literal (JSON_TRUE), true, "true"); - ASSERT_PRINT_EQ (literal (JSON_FALSE), true, "false"); - ASSERT_PRINT_EQ (literal (JSON_NULL), true, "null"); + ASSERT_JSON_PRINT_EQ (literal (JSON_TRUE), true, "true"); + ASSERT_JSON_PRINT_EQ (literal (JSON_FALSE), true, "false"); + ASSERT_JSON_PRINT_EQ (literal (JSON_NULL), true, "null"); - ASSERT_PRINT_EQ (literal (true), true, "true"); - ASSERT_PRINT_EQ (literal (false), true, "false"); + ASSERT_JSON_PRINT_EQ (literal (true), true, "true"); + ASSERT_JSON_PRINT_EQ (literal (false), true, "false"); } /* Verify that nested values are formatted correctly when written. @@ -858,18 +858,18 @@ test_formatting () /* This test relies on json::object writing out key/value pairs in key-insertion order. */ - ASSERT_PRINT_EQ (obj, true, - ("{\"str\": \"bar\",\n" - " \"child\": {\"grandchild\": {\"arr\": [0,\n" - " 1,\n" - " 2],\n" - " \"int\": 1066},\n" - " \"int\": 1776},\n" - " \"int\": 42}")); - ASSERT_PRINT_EQ (obj, false, - ("{\"str\": \"bar\", \"child\": {\"grandchild\":" - " {\"arr\": [0, 1, 2], \"int\": 1066}," - " \"int\": 1776}, \"int\": 42}")); + ASSERT_JSON_PRINT_EQ (obj, true, + ("{\"str\": \"bar\",\n" + " \"child\": {\"grandchild\": {\"arr\": [0,\n" + " 1,\n" + " 2],\n" + " \"int\": 1066},\n" + " \"int\": 1776},\n" + " \"int\": 42}")); + ASSERT_JSON_PRINT_EQ (obj, false, + ("{\"str\": \"bar\", \"child\": {\"grandchild\":" + " {\"arr\": [0, 1, 2], \"int\": 1066}," + " \"int\": 1776}, \"int\": 42}")); } /* Helper function for reporting failure of JSON comparisons. */ @@ -1043,8 +1043,8 @@ test_comparisons () obj_4.set_integer ("year", 1066); obj_4.set_string ("foo", "bar"); ASSERT_JSON_EQ (obj_2, obj_4); - ASSERT_PRINT_EQ (obj_2, false, "{\"foo\": \"bar\", \"year\": 1066}"); - ASSERT_PRINT_EQ (obj_4, false, "{\"year\": 1066, \"foo\": \"bar\"}"); + ASSERT_JSON_PRINT_EQ (obj_2, false, "{\"foo\": \"bar\", \"year\": 1066}"); + ASSERT_JSON_PRINT_EQ (obj_4, false, "{\"year\": 1066, \"foo\": \"bar\"}"); /* Arrays. */ diff --git a/gcc/json.h b/gcc/json.h index e32cd99d08a3..1d98e803db01 100644 --- a/gcc/json.h +++ b/gcc/json.h @@ -529,13 +529,18 @@ namespace selftest { class location; -extern void assert_print_eq (const location &loc, - const json::value &jv, - bool formatted, - const char *expected_json); +extern void assert_json_print_eq (const location &loc, + const json::value &jv, + bool formatted, + const char *expected_json); } // namespace selftest +#define ASSERT_JSON_PRINT_EQ(JV, FORMATTED, EXPECTED_JSON) \ + SELFTEST_BEGIN_STMT \ + assert_json_print_eq ((SELFTEST_LOCATION), (JV), (FORMATTED), (EXPECTED_JSON)); \ + SELFTEST_END_STMT + #endif /* #if CHECKING_P */ #endif /* GCC_JSON_H */
