From abcb6428ad9bf7d650455a0a180647a05183fd9d Mon Sep 17 00:00:00 2001 From: Josh Haberman Date: Thu, 30 Jul 2015 14:54:03 -0700 Subject: Changed parser semantics around skipping. Prior to this change: parse(buf, len) -> len + N ...would indicate that the next N bytes of the input are not needed, *and* would advance the decoding position by this much. After this change: parse(buf, len) -> len + N parse(NULL, N) -> N ...can be used to achieve the same thing. But skipping the N bytes is not explicitly performed by the user. A user that doesn't want/need to skip can just say: parsed = parse(buf, len); if (parsed < len) { // Handle suspend, advance stream by "parsed". } else { // Stream was advanced by "len" (even if parsed > len). } Updated unit tests to test this new behavior, and refactored test utility code a bit to support it. --- tests/json/test_json.cc | 37 +++++++++++++------------------------ 1 file changed, 13 insertions(+), 24 deletions(-) (limited to 'tests/json/test_json.cc') diff --git a/tests/json/test_json.cc b/tests/json/test_json.cc index 8dd4062..c483cf0 100644 --- a/tests/json/test_json.cc +++ b/tests/json/test_json.cc @@ -288,33 +288,22 @@ void test_json_roundtrip_message(const char* json_src, const char* json_expected, const upb::Handlers* serialize_handlers, int seam) { - upb::Status st; - upb::Environment env; - env.ReportErrorsTo(&st); + VerboseParserEnvironment env(verbose); StringSink data_sink; - upb::json::Printer* printer = - upb::json::Printer::Create(&env, serialize_handlers, data_sink.Sink()); - upb::json::Parser* parser = upb::json::Parser::Create(&env, printer->input()); - - upb::BytesSink* input = parser->input(); - void *sub; - size_t len = strlen(json_src); - size_t ofs = 0; - - bool ok = input->Start(0, &sub) && - parse_buffer(input, sub, json_src, 0, seam, &ofs, &st, verbose) && - parse_buffer(input, sub, json_src, seam, len, &ofs, &st, verbose) && - ofs == len; - - if (ok) { - if (verbose) { - fprintf(stderr, "calling end()\n"); - } - ok = input->End(); - } + upb::json::Printer* printer = upb::json::Printer::Create( + env.env(), serialize_handlers, data_sink.Sink()); + upb::json::Parser* parser = + upb::json::Parser::Create(env.env(), printer->input()); + env.ResetBytesSink(parser->input()); + env.Reset(json_src, strlen(json_src), false); + + bool ok = env.Start() && + env.ParseBuffer(seam) && + env.ParseBuffer(-1) && + env.End(); if (!ok) { - fprintf(stderr, "upb parse error: %s\n", st.error_message()); + fprintf(stderr, "upb parse error: %s\n", env.status().error_message()); } ASSERT(ok); -- cgit v1.2.3