Index: base/json/json_value_serializer_unittest.cc |
diff --git a/base/json/json_value_serializer_unittest.cc b/base/json/json_value_serializer_unittest.cc |
index d2a84dea89622b60c5898a6f0d2c34c961ab2bf3..225ee675c40317ba6ec746927e6aeb820882703a 100644 |
--- a/base/json/json_value_serializer_unittest.cc |
+++ b/base/json/json_value_serializer_unittest.cc |
@@ -86,28 +86,10 @@ void ValidateJsonList(const std::string& json) { |
ASSERT_EQ(1, value); |
} |
-// Test proper JSON [de]serialization from string is working. |
-TEST(JSONValueSerializerTest, ReadProperJSONFromString) { |
+// Test proper JSON deserialization from string is working. |
+TEST(JSONValueDeserializerTest, ReadProperJSONFromString) { |
// Try to deserialize it through the serializer. |
- JSONStringValueSerializer str_deserializer(kProperJSON); |
- |
- int error_code = 0; |
- std::string error_message; |
- scoped_ptr<Value> value( |
- str_deserializer.Deserialize(&error_code, &error_message)); |
- ASSERT_TRUE(value.get()); |
- ASSERT_EQ(0, error_code); |
- ASSERT_TRUE(error_message.empty()); |
- // Verify if the same JSON is still there. |
- CheckJSONIsStillTheSame(*value); |
-} |
- |
-// Test proper JSON deserialization from a string pointer is working. |
-TEST(JSONValueSerializerTest, ReadProperJSONFromStringPointer) { |
- // Try to deserialize a string pointer through the serializer. (This exercises |
- // a separate code path to passing a StringPiece.) |
- std::string proper_json(kProperJSON); |
- JSONStringValueSerializer str_deserializer(&proper_json); |
+ JSONStringValueDeserializer str_deserializer(kProperJSON); |
int error_code = 0; |
std::string error_message; |
@@ -121,12 +103,12 @@ TEST(JSONValueSerializerTest, ReadProperJSONFromStringPointer) { |
} |
// Test proper JSON deserialization from a StringPiece substring. |
-TEST(JSONValueSerializerTest, ReadProperJSONFromStringPiece) { |
+TEST(JSONValueDeserializerTest, ReadProperJSONFromStringPiece) { |
// Create a StringPiece for the substring of kProperJSONPadded that matches |
// kProperJSON. |
base::StringPiece proper_json(kProperJSONPadded); |
proper_json = proper_json.substr(5, proper_json.length() - 10); |
- JSONStringValueSerializer str_deserializer(proper_json); |
+ JSONStringValueDeserializer str_deserializer(proper_json); |
int error_code = 0; |
std::string error_message; |
@@ -141,9 +123,9 @@ TEST(JSONValueSerializerTest, ReadProperJSONFromStringPiece) { |
// Test that trialing commas are only properly deserialized from string when |
// the proper flag for that is set. |
-TEST(JSONValueSerializerTest, ReadJSONWithTrailingCommasFromString) { |
+TEST(JSONValueDeserializerTest, ReadJSONWithTrailingCommasFromString) { |
// Try to deserialize it through the serializer. |
- JSONStringValueSerializer str_deserializer(kProperJSONWithCommas); |
+ JSONStringValueDeserializer str_deserializer(kProperJSONWithCommas); |
int error_code = 0; |
std::string error_message; |
@@ -161,8 +143,8 @@ TEST(JSONValueSerializerTest, ReadJSONWithTrailingCommasFromString) { |
CheckJSONIsStillTheSame(*value); |
} |
-// Test proper JSON [de]serialization from file is working. |
-TEST(JSONValueSerializerTest, ReadProperJSONFromFile) { |
+// Test proper JSON deserialization from file is working. |
+TEST(JSONValueDeserializerTest, ReadProperJSONFromFile) { |
ScopedTempDir tempdir; |
ASSERT_TRUE(tempdir.CreateUniqueTempDir()); |
// Write it down in the file. |
@@ -171,7 +153,7 @@ TEST(JSONValueSerializerTest, ReadProperJSONFromFile) { |
WriteFile(temp_file, kProperJSON, strlen(kProperJSON))); |
// Try to deserialize it through the serializer. |
- JSONFileValueSerializer file_deserializer(temp_file); |
+ JSONFileValueDeserializer file_deserializer(temp_file); |
int error_code = 0; |
std::string error_message; |
@@ -186,7 +168,7 @@ TEST(JSONValueSerializerTest, ReadProperJSONFromFile) { |
// Test that trialing commas are only properly deserialized from file when |
// the proper flag for that is set. |
-TEST(JSONValueSerializerTest, ReadJSONWithCommasFromFile) { |
+TEST(JSONValueDeserializerTest, ReadJSONWithCommasFromFile) { |
ScopedTempDir tempdir; |
ASSERT_TRUE(tempdir.CreateUniqueTempDir()); |
// Write it down in the file. |
@@ -196,7 +178,7 @@ TEST(JSONValueSerializerTest, ReadJSONWithCommasFromFile) { |
strlen(kProperJSONWithCommas))); |
// Try to deserialize it through the serializer. |
- JSONFileValueSerializer file_deserializer(temp_file); |
+ JSONFileValueDeserializer file_deserializer(temp_file); |
// This must fail without the proper flag. |
int error_code = 0; |
std::string error_message; |
@@ -214,11 +196,27 @@ TEST(JSONValueSerializerTest, ReadJSONWithCommasFromFile) { |
CheckJSONIsStillTheSame(*value); |
} |
+TEST(JSONValueDeserializerTest, AllowTrailingComma) { |
+ scoped_ptr<Value> root; |
+ scoped_ptr<Value> root_expected; |
+ static const char kTestWithCommas[] = "{\"key\": [true,],}"; |
+ static const char kTestNoCommas[] = "{\"key\": [true]}"; |
+ |
+ JSONStringValueDeserializer deserializer(kTestWithCommas); |
+ deserializer.set_allow_trailing_comma(true); |
+ JSONStringValueDeserializer deserializer_expected(kTestNoCommas); |
+ root.reset(deserializer.Deserialize(NULL, NULL)); |
+ ASSERT_TRUE(root.get()); |
+ root_expected.reset(deserializer_expected.Deserialize(NULL, NULL)); |
+ ASSERT_TRUE(root_expected.get()); |
+ ASSERT_TRUE(root->Equals(root_expected.get())); |
+} |
+ |
TEST(JSONValueSerializerTest, Roundtrip) { |
static const char kOriginalSerialization[] = |
"{\"bool\":true,\"double\":3.14,\"int\":42,\"list\":[1,2],\"null\":null}"; |
- JSONStringValueSerializer serializer(kOriginalSerialization); |
- scoped_ptr<Value> root(serializer.Deserialize(NULL, NULL)); |
+ JSONStringValueDeserializer deserializer(kOriginalSerialization); |
+ scoped_ptr<Value> root(deserializer.Deserialize(NULL, NULL)); |
ASSERT_TRUE(root.get()); |
ASSERT_TRUE(root->IsType(Value::TYPE_DICTIONARY)); |
@@ -241,10 +239,6 @@ TEST(JSONValueSerializerTest, Roundtrip) { |
ASSERT_TRUE(root_dict->GetDouble("double", &double_value)); |
ASSERT_DOUBLE_EQ(3.14, double_value); |
- // We shouldn't be able to write using this serializer, since it was |
- // initialized with a const string. |
- ASSERT_FALSE(serializer.Serialize(*root_dict)); |
- |
std::string test_serialization; |
JSONStringValueSerializer mutable_serializer(&test_serialization); |
ASSERT_TRUE(mutable_serializer.Serialize(*root_dict)); |
@@ -331,7 +325,7 @@ TEST(JSONValueSerializerTest, UnicodeStrings) { |
ASSERT_EQ(kExpected, actual); |
// escaped ascii text -> json |
- JSONStringValueSerializer deserializer(kExpected); |
+ JSONStringValueDeserializer deserializer(kExpected); |
scoped_ptr<Value> deserial_root(deserializer.Deserialize(NULL, NULL)); |
ASSERT_TRUE(deserial_root.get()); |
DictionaryValue* dict_root = |
@@ -355,7 +349,7 @@ TEST(JSONValueSerializerTest, HexStrings) { |
ASSERT_EQ(kExpected, actual); |
// escaped ascii text -> json |
- JSONStringValueSerializer deserializer(kExpected); |
+ JSONStringValueDeserializer deserializer(kExpected); |
scoped_ptr<Value> deserial_root(deserializer.Deserialize(NULL, NULL)); |
ASSERT_TRUE(deserial_root.get()); |
DictionaryValue* dict_root = |
@@ -366,7 +360,7 @@ TEST(JSONValueSerializerTest, HexStrings) { |
// Test converting escaped regular chars |
static const char kEscapedChars[] = "{\"test\":\"\\u0067\\u006f\"}"; |
- JSONStringValueSerializer deserializer2(kEscapedChars); |
+ JSONStringValueDeserializer deserializer2(kEscapedChars); |
deserial_root.reset(deserializer2.Deserialize(NULL, NULL)); |
ASSERT_TRUE(deserial_root.get()); |
dict_root = static_cast<DictionaryValue*>(deserial_root.get()); |
@@ -374,22 +368,6 @@ TEST(JSONValueSerializerTest, HexStrings) { |
ASSERT_EQ(ASCIIToUTF16("go"), test_value); |
} |
-TEST(JSONValueSerializerTest, AllowTrailingComma) { |
- scoped_ptr<Value> root; |
- scoped_ptr<Value> root_expected; |
- static const char kTestWithCommas[] = "{\"key\": [true,],}"; |
- static const char kTestNoCommas[] = "{\"key\": [true]}"; |
- |
- JSONStringValueSerializer serializer(kTestWithCommas); |
- serializer.set_allow_trailing_comma(true); |
- JSONStringValueSerializer serializer_expected(kTestNoCommas); |
- root.reset(serializer.Deserialize(NULL, NULL)); |
- ASSERT_TRUE(root.get()); |
- root_expected.reset(serializer_expected.Deserialize(NULL, NULL)); |
- ASSERT_TRUE(root_expected.get()); |
- ASSERT_TRUE(root->Equals(root_expected.get())); |
-} |
- |
TEST(JSONValueSerializerTest, JSONReaderComments) { |
ValidateJsonList("[ // 2, 3, ignore me ] \n1 ]"); |
ValidateJsonList("[ /* 2, \n3, ignore me ]*/ \n1 ]"); |
@@ -435,7 +413,7 @@ TEST_F(JSONFileValueSerializerTest, Roundtrip) { |
ASSERT_TRUE(PathExists(original_file_path)); |
- JSONFileValueSerializer deserializer(original_file_path); |
+ JSONFileValueDeserializer deserializer(original_file_path); |
scoped_ptr<Value> root; |
root.reset(deserializer.Deserialize(NULL, NULL)); |
@@ -483,7 +461,7 @@ TEST_F(JSONFileValueSerializerTest, RoundtripNested) { |
ASSERT_TRUE(PathExists(original_file_path)); |
- JSONFileValueSerializer deserializer(original_file_path); |
+ JSONFileValueDeserializer deserializer(original_file_path); |
scoped_ptr<Value> root; |
root.reset(deserializer.Deserialize(NULL, NULL)); |
ASSERT_TRUE(root.get()); |
@@ -508,9 +486,9 @@ TEST_F(JSONFileValueSerializerTest, NoWhitespace) { |
source_file_path = source_file_path.Append( |
FILE_PATH_LITERAL("serializer_test_nowhitespace.json")); |
ASSERT_TRUE(PathExists(source_file_path)); |
- JSONFileValueSerializer serializer(source_file_path); |
+ JSONFileValueDeserializer deserializer(source_file_path); |
scoped_ptr<Value> root; |
- root.reset(serializer.Deserialize(NULL, NULL)); |
+ root.reset(deserializer.Deserialize(NULL, NULL)); |
ASSERT_TRUE(root.get()); |
} |