diff options
author | Joshua Haberman <jhaberman@gmail.com> | 2016-04-14 16:28:47 -0700 |
---|---|---|
committer | Joshua Haberman <jhaberman@gmail.com> | 2016-04-14 16:28:47 -0700 |
commit | 89197b9358c127e41dd9f428578783ec09a94d82 (patch) | |
tree | a970d947ecfd7dba9df92b92982ae08d5f2dd9a2 /tests | |
parent | 534b5c98baf02b2101d25917d46a1605906a6c53 (diff) |
JSON parser: always accept both name variants, flag controls which we generate.
* JSON parser: always accept both name variants, flag controls which we generate.
My previous commit was based on wrong information about the
proto3 JSON spec. It turns out we need to accept both field
name formats all the time, and a runtime flag should control
which we generate.
* Documented the preserve_proto_fieldnames option.
* Fix bugs in PR.
Diffstat (limited to 'tests')
-rw-r--r-- | tests/json/test_json.cc | 41 |
1 files changed, 40 insertions, 1 deletions
diff --git a/tests/json/test_json.cc b/tests/json/test_json.cc index 5ca04fc..6874131 100644 --- a/tests/json/test_json.cc +++ b/tests/json/test_json.cc @@ -36,6 +36,17 @@ static TestCase kTestRoundtripMessages[] = { "{\"foo\":2}]}"), EXPECT_SAME }, + // We must also recognize raw proto names. + { + TEST("{\"optional_int32\":-42,\"optional_string\":\"Test\\u0001Message\"," + "\"optional_msg\":{\"foo\":42}," + "\"optional_bool\":true,\"repeated_msg\":[{\"foo\":1}," + "{\"foo\":2}]}"), + EXPECT("{\"optionalInt32\":-42,\"optionalString\":\"Test\\u0001Message\"," + "\"optionalMsg\":{\"foo\":42}," + "\"optionalBool\":true,\"repeatedMsg\":[{\"foo\":1}," + "{\"foo\":2}]}") + }, // Test special escapes in strings. { TEST("{\"repeatedString\":[\"\\b\",\"\\r\",\"\\n\",\"\\f\",\"\\t\"," @@ -112,6 +123,18 @@ static TestCase kTestRoundtripMessages[] = { TEST_SENTINEL }; +static TestCase kTestRoundtripMessagesPreserve[] = { + // Test most fields here. + { + TEST("{\"optional_int32\":-42,\"optional_string\":\"Test\\u0001Message\"," + "\"optional_msg\":{\"foo\":42}," + "\"optional_bool\":true,\"repeated_msg\":[{\"foo\":1}," + "{\"foo\":2}]}"), + EXPECT_SAME + }, + TEST_SENTINEL +}; + static void AddField(upb::MessageDef* message, int number, const char* name, @@ -323,7 +346,7 @@ void test_json_roundtrip() { upb::reffed_ptr<upb::SymbolTable> symtab(upb::SymbolTable::New()); const upb::MessageDef* md = BuildTestMessage(symtab.get()); upb::reffed_ptr<const upb::Handlers> serialize_handlers( - upb::json::Printer::NewHandlers(md)); + upb::json::Printer::NewHandlers(md, false)); upb::reffed_ptr<const upb::json::ParserMethod> parser_method( upb::json::ParserMethod::New(md)); @@ -340,6 +363,22 @@ void test_json_roundtrip() { i); } } + + serialize_handlers = upb::json::Printer::NewHandlers(md, true); + + for (const TestCase* test_case = kTestRoundtripMessagesPreserve; + test_case->input != NULL; test_case++) { + const char *expected = + (test_case->expected == EXPECT_SAME) ? + test_case->input : + test_case->expected; + + for (size_t i = 0; i < strlen(test_case->input); i++) { + test_json_roundtrip_message(test_case->input, expected, + serialize_handlers.get(), parser_method.get(), + i); + } + } } extern "C" { |