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

Side by Side Diff: base/json/json_value_serializer_unittest.cc

Issue 8423079: Allow trailing comma in JSON policy files. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Make the tests windows friendly too. Created 9 years, 1 month 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 unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « base/json/json_value_serializer.cc ('k') | chrome/browser/policy/config_dir_policy_provider.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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 "base/json/json_value_serializer.h"
6
7 #include <string>
8
9 #include "base/file_util.h"
10 #include "base/json/json_reader.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "base/scoped_temp_dir.h"
13 #include "base/string_util.h"
14 #include "base/values.h"
15 #include "testing/gtest/include/gtest/gtest.h"
16
17 namespace base {
18
19 namespace {
20
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";
32
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";
41
42 const char kWinLineEnds[] = "\r\n";
43 const char kLinuxLineEnds[] = "\n";
44
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);
57 }
58
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);
64
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);
74 }
75
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);
82
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);
97 }
98
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)));
107
108 // Try to deserialize it through the serializer.
109 JSONFileValueSerializer file_deserializer(temp_file);
110
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);
120 }
121
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)));
133
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);
151 }
152
153 } // namespace
154
155 } // namespace base
156
OLDNEW
« no previous file with comments | « base/json/json_value_serializer.cc ('k') | chrome/browser/policy/config_dir_policy_provider.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698