Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include <string> | |
| 6 | |
| 7 #include "testing/gtest/include/gtest/gtest.h" | |
|
willchan no longer on Chromium
2011/11/15 02:36:22
This one should get sorted among the rest below.
pastarmovj
2011/11/17 10:35:35
Done.
| |
| 8 | |
| 9 #include "base/file_util.h" | |
| 10 #include "base/json/json_reader.h" | |
| 11 #include "base/json/json_value_serializer.h" | |
|
willchan no longer on Chromium
2011/11/15 02:36:22
This one is supposed to go first, even ahead of th
pastarmovj
2011/11/17 10:35:35
Done.
| |
| 12 #include "base/memory/scoped_ptr.h" | |
| 13 #include "base/scoped_temp_dir.h" | |
| 14 #include "base/values.h" | |
| 15 | |
| 16 namespace { | |
| 17 | |
| 18 // Some proper JSON to test with: | |
| 19 std::string proper_json = | |
|
willchan no longer on Chromium
2011/11/15 02:36:22
This is creating a static intiializer. Please use
pastarmovj
2011/11/17 10:35:35
Done.
| |
| 20 "{\n" | |
| 21 " \"compound\": {\n" | |
| 22 " \"a\": 1,\n" | |
| 23 " \"b\": 2\n" | |
| 24 " },\n" | |
| 25 " \"some_String\": \"1337\",\n" | |
| 26 " \"some_int\": 42,\n" | |
| 27 " \"the_list\": [ \"val1\", \"val2\" ]\n" | |
| 28 "}\n"; | |
| 29 | |
| 30 // Some proper JSON with trailing commas: | |
| 31 std::string proper_json_with_commas = | |
| 32 "{\n" | |
| 33 "\t\"some_int\": 42,\n" | |
| 34 "\t\"some_String\": \"1337\",\n" | |
| 35 "\t\"the_list\": [\"val1\", \"val2\", ],\n" | |
| 36 "\t\"compound\": { \"a\": 1, \"b\": 2, },\n" | |
| 37 "}\n"; | |
| 38 | |
| 39 // Some broken JSON: | |
| 40 std::string broken_json = | |
| 41 "{\n" | |
| 42 "\t\"some_int\": a42,\n" | |
| 43 "\tsome_String: \"1337\",\n" | |
| 44 "\t\"the_list: [\"val1\", \"val2\", ],\n" | |
| 45 "\t\"compound\": { \"a\" 1, \"b\": 2, },\n" | |
| 46 "},\n"; | |
| 47 | |
| 48 void CheckJSONIsStillTheSame(base::Value& value) { | |
| 49 // Serialize back the output. | |
| 50 std::string serialized_json; | |
| 51 JSONStringValueSerializer str_serializer(&serialized_json); | |
| 52 str_serializer.set_pretty_print(true); | |
| 53 ASSERT_TRUE(str_serializer.Serialize(value)); | |
| 54 // Now compare the input with the output. | |
| 55 ASSERT_EQ(proper_json, serialized_json); | |
| 56 } | |
| 57 | |
| 58 } // namespace | |
| 59 | |
| 60 namespace base { | |
|
willchan no longer on Chromium
2011/11/15 02:36:22
You should probably just put everything within the
pastarmovj
2011/11/17 10:35:35
Done.
| |
| 61 | |
| 62 TEST(JSONValueSerializerTest, ReadProperJSONFromString) { | |
| 63 // Try to deserialize it through the serializer. | |
| 64 JSONStringValueSerializer str_deserializer(proper_json); | |
| 65 | |
| 66 int error_code = 0; | |
| 67 std::string error_message; | |
| 68 scoped_ptr<Value> value( | |
| 69 str_deserializer.Deserialize(&error_code, &error_message)); | |
| 70 ASSERT_TRUE(value.get()); | |
| 71 ASSERT_EQ(0, error_code); | |
| 72 ASSERT_TRUE(error_message.empty()); | |
| 73 // Verify if the same JSON is still there. | |
| 74 CheckJSONIsStillTheSame(*value); | |
| 75 } | |
| 76 | |
| 77 TEST(JSONValueSerializerTest, ReadJSONWithTrailingCommasFromString) { | |
| 78 // Try to deserialize it through the serializer. | |
| 79 JSONStringValueSerializer str_deserializer(proper_json_with_commas); | |
| 80 | |
| 81 int error_code = 0; | |
| 82 std::string error_message; | |
| 83 scoped_ptr<Value> value( | |
| 84 str_deserializer.Deserialize(&error_code, &error_message)); | |
| 85 ASSERT_FALSE(value.get()); | |
| 86 ASSERT_NE(0, error_code); | |
| 87 ASSERT_FALSE(error_message.empty()); | |
| 88 // Now the flag is set and it must pass. | |
| 89 str_deserializer.set_allow_trailing_comma(true); | |
| 90 value.reset(str_deserializer.Deserialize(&error_code, &error_message)); | |
| 91 ASSERT_TRUE(value.get()); | |
| 92 ASSERT_EQ(JSONReader::JSON_TRAILING_COMMA, error_code); | |
| 93 // Verify if the same JSON is still there. | |
| 94 CheckJSONIsStillTheSame(*value); | |
| 95 } | |
| 96 | |
| 97 TEST(JSONValueSerializerTest, ReadProperJSONFromFile) { | |
| 98 ScopedTempDir tempdir; | |
| 99 ASSERT_TRUE(tempdir.CreateUniqueTempDir()); | |
| 100 // Write it down in the file. | |
| 101 FilePath temp_file(tempdir.path().AppendASCII("test.json")); | |
| 102 ASSERT_EQ(static_cast<int>(proper_json.length()), | |
| 103 file_util::WriteFile(temp_file, | |
| 104 proper_json.data(), | |
| 105 proper_json.length())); | |
| 106 | |
| 107 // Try to deserialize it through the serializer. | |
| 108 JSONFileValueSerializer file_deserializer(temp_file); | |
| 109 | |
| 110 int error_code = 0; | |
| 111 std::string error_message; | |
| 112 scoped_ptr<Value> value( | |
| 113 file_deserializer.Deserialize(&error_code, &error_message)); | |
| 114 ASSERT_TRUE(value.get()); | |
| 115 ASSERT_EQ(0, error_code); | |
| 116 ASSERT_TRUE(error_message.empty()); | |
| 117 // Verify if the same JSON is still there. | |
| 118 CheckJSONIsStillTheSame(*value); | |
| 119 } | |
| 120 | |
| 121 TEST(JSONValueSerializerTest, ReadJSONWithCommasFromFile) { | |
| 122 ScopedTempDir tempdir; | |
| 123 ASSERT_TRUE(tempdir.CreateUniqueTempDir()); | |
| 124 // Write it down in the file. | |
| 125 FilePath temp_file(tempdir.path().AppendASCII("test.json")); | |
| 126 ASSERT_EQ(static_cast<int>(proper_json_with_commas.length()), | |
| 127 file_util::WriteFile(temp_file, | |
| 128 proper_json_with_commas.data(), | |
| 129 proper_json_with_commas.length())); | |
| 130 | |
| 131 // Try to deserialize it through the serializer. | |
| 132 JSONFileValueSerializer file_deserializer(temp_file); | |
| 133 // This must fail without the proper flag. | |
| 134 int error_code = 0; | |
| 135 std::string error_message; | |
| 136 scoped_ptr<Value> value( | |
| 137 file_deserializer.Deserialize(&error_code, &error_message)); | |
| 138 ASSERT_FALSE(value.get()); | |
| 139 ASSERT_NE(0, error_code); | |
| 140 ASSERT_FALSE(error_message.empty()); | |
| 141 // Now the flag is set and it must pass. | |
| 142 file_deserializer.set_allow_trailing_comma(true); | |
| 143 value.reset(file_deserializer.Deserialize(&error_code, &error_message)); | |
| 144 ASSERT_TRUE(value.get()); | |
| 145 ASSERT_EQ(JSONReader::JSON_TRAILING_COMMA, error_code); | |
| 146 // Verify if the same JSON is still there. | |
| 147 CheckJSONIsStillTheSame(*value); | |
| 148 } | |
| 149 | |
| 150 } // namespace base | |
| 151 | |
| OLD | NEW |