aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/google/protobuf/util/json_util_test.cc
diff options
context:
space:
mode:
authorGravatar Jisi Liu <jisi.liu@gmail.com>2016-03-30 11:39:59 -0700
committerGravatar Jisi Liu <jisi.liu@gmail.com>2016-03-30 11:39:59 -0700
commit3b3c8abb9635eb3ea078a821a99c9ef29d66dff7 (patch)
tree7d2ec154f15c9f9153d890e76b6cf30e471ea488 /src/google/protobuf/util/json_util_test.cc
parent78105897a8f01c7be9cf8502b6c58d47eb1ccdd7 (diff)
Integrate google internal changes.
Diffstat (limited to 'src/google/protobuf/util/json_util_test.cc')
-rw-r--r--src/google/protobuf/util/json_util_test.cc68
1 files changed, 44 insertions, 24 deletions
diff --git a/src/google/protobuf/util/json_util_test.cc b/src/google/protobuf/util/json_util_test.cc
index da68495f..a4d3cc98 100644
--- a/src/google/protobuf/util/json_util_test.cc
+++ b/src/google/protobuf/util/json_util_test.cc
@@ -77,8 +77,11 @@ class JsonUtilTest : public testing::Test {
bool FromJson(const string& json, Message* message) {
string binary;
- GOOGLE_CHECK_OK(JsonToBinaryString(
- resolver_.get(), GetTypeUrl(message->GetDescriptor()), json, &binary));
+ if (!JsonToBinaryString(resolver_.get(),
+ GetTypeUrl(message->GetDescriptor()), json, &binary)
+ .ok()) {
+ return false;
+ }
return message->ParseFromString(binary);
}
@@ -99,28 +102,36 @@ TEST_F(JsonUtilTest, TestWhitespaces) {
ToJson(m, options));
}
-// TODO(skarvaje): Uncomment after cl/96232915 is submitted.
-// TEST_F(JsonUtilTest, TestDefaultValues) {
- // TestMessage m;
- // JsonOptions options;
- // EXPECT_EQ("{}", ToJson(m, options));
- // options.always_print_primitive_fields = true;
- // EXPECT_EQ(
- // "{\"boolValue\":false,"
- // "\"int32Value\":0,"
- // "\"int64Value\":\"0\","
- // "\"uint32Value\":0,"
- // "\"uint64Value\":\"0\","
- // "\"floatValue\":0,"
- // "\"doubleValue\":0,"
- // "\"stringValue\":\"\","
- // "\"bytesValue\":\"\","
- // // TODO(xiaofeng): The default enum value should be FOO. I believe
- // // this is a bug in DefaultValueObjectWriter.
- // "\"enumValue\":null"
- // "}",
- // ToJson(m, options));
-// }
+TEST_F(JsonUtilTest, TestDefaultValues) {
+ TestMessage m;
+ JsonOptions options;
+ EXPECT_EQ("{}", ToJson(m, options));
+ options.always_print_primitive_fields = true;
+ EXPECT_EQ(
+ "{\"boolValue\":false,"
+ "\"int32Value\":0,"
+ "\"int64Value\":\"0\","
+ "\"uint32Value\":0,"
+ "\"uint64Value\":\"0\","
+ "\"floatValue\":0,"
+ "\"doubleValue\":0,"
+ "\"stringValue\":\"\","
+ "\"bytesValue\":\"\","
+ "\"enumValue\":\"FOO\","
+ "\"repeatedBoolValue\":[],"
+ "\"repeatedInt32Value\":[],"
+ "\"repeatedInt64Value\":[],"
+ "\"repeatedUint32Value\":[],"
+ "\"repeatedUint64Value\":[],"
+ "\"repeatedFloatValue\":[],"
+ "\"repeatedDoubleValue\":[],"
+ "\"repeatedStringValue\":[],"
+ "\"repeatedBytesValue\":[],"
+ "\"repeatedEnumValue\":[],"
+ "\"repeatedMessageValue\":[]"
+ "}",
+ ToJson(m, options));
+}
TEST_F(JsonUtilTest, ParseMessage) {
// Some random message but good enough to verify that the parsing warpper
@@ -158,6 +169,15 @@ TEST_F(JsonUtilTest, ParseMap) {
EXPECT_EQ(message.DebugString(), other.DebugString());
}
+TEST_F(JsonUtilTest, TestParseErrors) {
+ TestMessage m;
+ JsonOptions options;
+ // Parsing should fail if the field name can not be recognized.
+ EXPECT_FALSE(FromJson("{\"unknownName\":0}", &m));
+ // Parsing should fail if the value is invalid.
+ EXPECT_FALSE(FromJson("{\"int32Value\":2147483648}", &m));
+}
+
typedef pair<char*, int> Segment;
// A ZeroCopyOutputStream that writes to multiple buffers.
class SegmentedZeroCopyOutputStream : public io::ZeroCopyOutputStream {