Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1173)

Unified Diff: third_party/protobuf/src/google/protobuf/util/json_util_test.cc

Issue 2590803003: Revert "third_party/protobuf: Update to HEAD (83d681ee2c)" (Closed)
Patch Set: Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: third_party/protobuf/src/google/protobuf/util/json_util_test.cc
diff --git a/third_party/protobuf/src/google/protobuf/util/json_util_test.cc b/third_party/protobuf/src/google/protobuf/util/json_util_test.cc
index 3ce779c922fd065c50774842101572425af6b703..a4d3cc983fe1102a98b7de5b34442eba56fa4036 100644
--- a/third_party/protobuf/src/google/protobuf/util/json_util_test.cc
+++ b/third_party/protobuf/src/google/protobuf/util/json_util_test.cc
@@ -34,8 +34,6 @@
#include <string>
#include <google/protobuf/io/zero_copy_stream.h>
-#include <google/protobuf/descriptor_database.h>
-#include <google/protobuf/dynamic_message.h>
#include <google/protobuf/util/json_format_proto3.pb.h>
#include <google/protobuf/util/type_resolver.h>
#include <google/protobuf/util/type_resolver_util.h>
@@ -65,21 +63,26 @@ static string GetTypeUrl(const Descriptor* message) {
class JsonUtilTest : public testing::Test {
protected:
JsonUtilTest() {
+ resolver_.reset(NewTypeResolverForDescriptorPool(
+ kTypeUrlPrefix, DescriptorPool::generated_pool()));
}
- string ToJson(const Message& message, const JsonPrintOptions& options) {
+ string ToJson(const Message& message, const JsonOptions& options) {
string result;
- GOOGLE_CHECK_OK(MessageToJsonString(message, &result, options));
+ GOOGLE_CHECK_OK(BinaryToJsonString(resolver_.get(),
+ GetTypeUrl(message.GetDescriptor()),
+ message.SerializeAsString(), &result, options));
return result;
}
- bool FromJson(const string& json, Message* message,
- const JsonParseOptions& options) {
- return JsonStringToMessage(json, message, options).ok();
- }
-
bool FromJson(const string& json, Message* message) {
- return FromJson(json, message, JsonParseOptions());
+ string binary;
+ if (!JsonToBinaryString(resolver_.get(),
+ GetTypeUrl(message->GetDescriptor()), json, &binary)
+ .ok()) {
+ return false;
+ }
+ return message->ParseFromString(binary);
}
google::protobuf::scoped_ptr<TypeResolver> resolver_;
@@ -89,7 +92,7 @@ TEST_F(JsonUtilTest, TestWhitespaces) {
TestMessage m;
m.mutable_message_value();
- JsonPrintOptions options;
+ JsonOptions options;
EXPECT_EQ("{\"messageValue\":{}}", ToJson(m, options));
options.add_whitespace = true;
EXPECT_EQ(
@@ -101,7 +104,7 @@ TEST_F(JsonUtilTest, TestWhitespaces) {
TEST_F(JsonUtilTest, TestDefaultValues) {
TestMessage m;
- JsonPrintOptions options;
+ JsonOptions options;
EXPECT_EQ("{}", ToJson(m, options));
options.always_print_primitive_fields = true;
EXPECT_EQ(
@@ -128,34 +131,6 @@ TEST_F(JsonUtilTest, TestDefaultValues) {
"\"repeatedMessageValue\":[]"
"}",
ToJson(m, options));
-
- options.always_print_primitive_fields = true;
- m.set_string_value("i am a test string value");
- m.set_bytes_value("i am a test bytes value");
- EXPECT_EQ(
- "{\"boolValue\":false,"
- "\"int32Value\":0,"
- "\"int64Value\":\"0\","
- "\"uint32Value\":0,"
- "\"uint64Value\":\"0\","
- "\"floatValue\":0,"
- "\"doubleValue\":0,"
- "\"stringValue\":\"i am a test string value\","
- "\"bytesValue\":\"aSBhbSBhIHRlc3QgYnl0ZXMgdmFsdWU=\","
- "\"enumValue\":\"FOO\","
- "\"repeatedBoolValue\":[],"
- "\"repeatedInt32Value\":[],"
- "\"repeatedInt64Value\":[],"
- "\"repeatedUint32Value\":[],"
- "\"repeatedUint64Value\":[],"
- "\"repeatedFloatValue\":[],"
- "\"repeatedDoubleValue\":[],"
- "\"repeatedStringValue\":[],"
- "\"repeatedBytesValue\":[],"
- "\"repeatedEnumValue\":[],"
- "\"repeatedMessageValue\":[]"
- "}",
- ToJson(m, options));
}
TEST_F(JsonUtilTest, ParseMessage) {
@@ -172,9 +147,8 @@ TEST_F(JsonUtilTest, ParseMessage) {
" {\"value\": 40}, {\"value\": 96}\n"
" ]\n"
"}\n";
- JsonParseOptions options;
TestMessage m;
- ASSERT_TRUE(FromJson(input, &m, options));
+ ASSERT_TRUE(FromJson(input, &m));
EXPECT_EQ(1024, m.int32_value());
ASSERT_EQ(2, m.repeated_int32_value_size());
EXPECT_EQ(1, m.repeated_int32_value(0));
@@ -188,74 +162,27 @@ TEST_F(JsonUtilTest, ParseMessage) {
TEST_F(JsonUtilTest, ParseMap) {
TestMap message;
(*message.mutable_string_map())["hello"] = 1234;
- JsonPrintOptions print_options;
- JsonParseOptions parse_options;
- EXPECT_EQ("{\"stringMap\":{\"hello\":1234}}", ToJson(message, print_options));
+ JsonOptions options;
+ EXPECT_EQ("{\"stringMap\":{\"hello\":1234}}", ToJson(message, options));
TestMap other;
- ASSERT_TRUE(FromJson(ToJson(message, print_options), &other, parse_options));
+ ASSERT_TRUE(FromJson(ToJson(message, options), &other));
EXPECT_EQ(message.DebugString(), other.DebugString());
}
-TEST_F(JsonUtilTest, TestParseIgnoreUnknownFields) {
- TestMessage m;
- JsonParseOptions options;
- options.ignore_unknown_fields = true;
- EXPECT_TRUE(FromJson("{\"unknownName\":0}", &m, options));
-}
-
TEST_F(JsonUtilTest, TestParseErrors) {
TestMessage m;
- JsonParseOptions options;
+ JsonOptions options;
// Parsing should fail if the field name can not be recognized.
- EXPECT_FALSE(FromJson("{\"unknownName\":0}", &m, options));
+ EXPECT_FALSE(FromJson("{\"unknownName\":0}", &m));
// Parsing should fail if the value is invalid.
- EXPECT_FALSE(FromJson("{\"int32Value\":2147483648}", &m, options));
-}
-
-TEST_F(JsonUtilTest, TestDynamicMessage) {
- // Some random message but good enough to test the wrapper functions.
- string input =
- "{\n"
- " \"int32Value\": 1024,\n"
- " \"repeatedInt32Value\": [1, 2],\n"
- " \"messageValue\": {\n"
- " \"value\": 2048\n"
- " },\n"
- " \"repeatedMessageValue\": [\n"
- " {\"value\": 40}, {\"value\": 96}\n"
- " ]\n"
- "}\n";
-
- // Create a new DescriptorPool with the same protos as the generated one.
- DescriptorPoolDatabase database(*DescriptorPool::generated_pool());
- DescriptorPool pool(&database);
- // A dynamic version of the test proto.
- DynamicMessageFactory factory;
- google::protobuf::scoped_ptr<Message> message(factory.GetPrototype(
- pool.FindMessageTypeByName("proto3.TestMessage"))->New());
- EXPECT_TRUE(FromJson(input, message.get()));
-
- // Convert to generated message for easy inspection.
- TestMessage generated;
- EXPECT_TRUE(generated.ParseFromString(message->SerializeAsString()));
- EXPECT_EQ(1024, generated.int32_value());
- ASSERT_EQ(2, generated.repeated_int32_value_size());
- EXPECT_EQ(1, generated.repeated_int32_value(0));
- EXPECT_EQ(2, generated.repeated_int32_value(1));
- EXPECT_EQ(2048, generated.message_value().value());
- ASSERT_EQ(2, generated.repeated_message_value_size());
- EXPECT_EQ(40, generated.repeated_message_value(0).value());
- EXPECT_EQ(96, generated.repeated_message_value(1).value());
-
- JsonOptions options;
- EXPECT_EQ(ToJson(generated, options), ToJson(*message, options));
+ EXPECT_FALSE(FromJson("{\"int32Value\":2147483648}", &m));
}
-typedef std::pair<char*, int> Segment;
+typedef pair<char*, int> Segment;
// A ZeroCopyOutputStream that writes to multiple buffers.
class SegmentedZeroCopyOutputStream : public io::ZeroCopyOutputStream {
public:
- explicit SegmentedZeroCopyOutputStream(std::list<Segment> segments)
+ explicit SegmentedZeroCopyOutputStream(list<Segment> segments)
: segments_(segments), last_segment_(static_cast<char*>(NULL), 0), byte_count_(0) {}
virtual bool Next(void** buffer, int* length) {
@@ -281,7 +208,7 @@ class SegmentedZeroCopyOutputStream : public io::ZeroCopyOutputStream {
virtual int64 ByteCount() const { return byte_count_; }
private:
- std::list<Segment> segments_;
+ list<Segment> segments_;
Segment last_segment_;
int64 byte_count_;
};
@@ -299,7 +226,7 @@ TEST(ZeroCopyStreamByteSinkTest, TestAllInputOutputPatterns) {
for (int split_pattern = 0; split_pattern < (1 << (kOutputBufferLength - 1));
split_pattern += kSkippedPatternCount) {
// Split the buffer into small segments according to the split_pattern.
- std::list<Segment> segments;
+ list<Segment> segments;
int segment_start = 0;
for (int i = 0; i < kOutputBufferLength - 1; ++i) {
if (split_pattern & (1 << i)) {

Powered by Google App Engine
This is Rietveld 408576698