Update .DEPS.git
[chromium-blink-merge.git] / base / json / json_value_serializer_unittest.cc
blobdc103dd56512bfcc7ef5709ffc01324b6f99c47f
1 // Copyright (c) 2012 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.
5 #include <string>
7 #include "base/file_util.h"
8 #include "base/files/scoped_temp_dir.h"
9 #include "base/json/json_file_value_serializer.h"
10 #include "base/json/json_reader.h"
11 #include "base/json/json_string_value_serializer.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "base/string_util.h"
14 #include "base/values.h"
15 #include "testing/gtest/include/gtest/gtest.h"
17 namespace base {
19 namespace {
21 // Some proper JSON to test with:
22 const char kProperJSON[] =
23 "{\n"
24 " \"compound\": {\n"
25 " \"a\": 1,\n"
26 " \"b\": 2\n"
27 " },\n"
28 " \"some_String\": \"1337\",\n"
29 " \"some_int\": 42,\n"
30 " \"the_list\": [ \"val1\", \"val2\" ]\n"
31 "}\n";
33 // Some proper JSON with trailing commas:
34 const char kProperJSONWithCommas[] =
35 "{\n"
36 "\t\"some_int\": 42,\n"
37 "\t\"some_String\": \"1337\",\n"
38 "\t\"the_list\": [\"val1\", \"val2\", ],\n"
39 "\t\"compound\": { \"a\": 1, \"b\": 2, },\n"
40 "}\n";
42 const char kWinLineEnds[] = "\r\n";
43 const char kLinuxLineEnds[] = "\n";
45 // Verifies the generated JSON against the expected output.
46 void CheckJSONIsStillTheSame(Value& value) {
47 // Serialize back the output.
48 std::string serialized_json;
49 JSONStringValueSerializer str_serializer(&serialized_json);
50 str_serializer.set_pretty_print(true);
51 ASSERT_TRUE(str_serializer.Serialize(value));
52 // Unify line endings between platforms.
53 ReplaceSubstringsAfterOffset(&serialized_json, 0,
54 kWinLineEnds, kLinuxLineEnds);
55 // Now compare the input with the output.
56 ASSERT_EQ(kProperJSON, serialized_json);
59 // Test proper JSON [de]serialization from string is working.
60 TEST(JSONValueSerializerTest, ReadProperJSONFromString) {
61 // Try to deserialize it through the serializer.
62 std::string proper_json(kProperJSON);
63 JSONStringValueSerializer str_deserializer(proper_json);
65 int error_code = 0;
66 std::string error_message;
67 scoped_ptr<Value> value(
68 str_deserializer.Deserialize(&error_code, &error_message));
69 ASSERT_TRUE(value.get());
70 ASSERT_EQ(0, error_code);
71 ASSERT_TRUE(error_message.empty());
72 // Verify if the same JSON is still there.
73 CheckJSONIsStillTheSame(*value);
76 // Test that trialing commas are only properly deserialized from string when
77 // the proper flag for that is set.
78 TEST(JSONValueSerializerTest, ReadJSONWithTrailingCommasFromString) {
79 // Try to deserialize it through the serializer.
80 std::string proper_json(kProperJSONWithCommas);
81 JSONStringValueSerializer str_deserializer(proper_json);
83 int error_code = 0;
84 std::string error_message;
85 scoped_ptr<Value> value(
86 str_deserializer.Deserialize(&error_code, &error_message));
87 ASSERT_FALSE(value.get());
88 ASSERT_NE(0, error_code);
89 ASSERT_FALSE(error_message.empty());
90 // Now the flag is set and it must pass.
91 str_deserializer.set_allow_trailing_comma(true);
92 value.reset(str_deserializer.Deserialize(&error_code, &error_message));
93 ASSERT_TRUE(value.get());
94 ASSERT_EQ(JSONReader::JSON_TRAILING_COMMA, error_code);
95 // Verify if the same JSON is still there.
96 CheckJSONIsStillTheSame(*value);
99 // Test proper JSON [de]serialization from file is working.
100 TEST(JSONValueSerializerTest, ReadProperJSONFromFile) {
101 ScopedTempDir tempdir;
102 ASSERT_TRUE(tempdir.CreateUniqueTempDir());
103 // Write it down in the file.
104 FilePath temp_file(tempdir.path().AppendASCII("test.json"));
105 ASSERT_EQ(static_cast<int>(strlen(kProperJSON)),
106 file_util::WriteFile(temp_file, kProperJSON, strlen(kProperJSON)));
108 // Try to deserialize it through the serializer.
109 JSONFileValueSerializer file_deserializer(temp_file);
111 int error_code = 0;
112 std::string error_message;
113 scoped_ptr<Value> value(
114 file_deserializer.Deserialize(&error_code, &error_message));
115 ASSERT_TRUE(value.get());
116 ASSERT_EQ(0, error_code);
117 ASSERT_TRUE(error_message.empty());
118 // Verify if the same JSON is still there.
119 CheckJSONIsStillTheSame(*value);
122 // Test that trialing commas are only properly deserialized from file when
123 // the proper flag for that is set.
124 TEST(JSONValueSerializerTest, ReadJSONWithCommasFromFile) {
125 ScopedTempDir tempdir;
126 ASSERT_TRUE(tempdir.CreateUniqueTempDir());
127 // Write it down in the file.
128 FilePath temp_file(tempdir.path().AppendASCII("test.json"));
129 ASSERT_EQ(static_cast<int>(strlen(kProperJSONWithCommas)),
130 file_util::WriteFile(temp_file,
131 kProperJSONWithCommas,
132 strlen(kProperJSONWithCommas)));
134 // Try to deserialize it through the serializer.
135 JSONFileValueSerializer file_deserializer(temp_file);
136 // This must fail without the proper flag.
137 int error_code = 0;
138 std::string error_message;
139 scoped_ptr<Value> value(
140 file_deserializer.Deserialize(&error_code, &error_message));
141 ASSERT_FALSE(value.get());
142 ASSERT_NE(0, error_code);
143 ASSERT_FALSE(error_message.empty());
144 // Now the flag is set and it must pass.
145 file_deserializer.set_allow_trailing_comma(true);
146 value.reset(file_deserializer.Deserialize(&error_code, &error_message));
147 ASSERT_TRUE(value.get());
148 ASSERT_EQ(JSONReader::JSON_TRAILING_COMMA, error_code);
149 // Verify if the same JSON is still there.
150 CheckJSONIsStillTheSame(*value);
153 } // namespace
155 } // namespace base