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

Side by Side Diff: chrome/common/json_value_serializer_unittest.cc

Issue 16270: Change the signature of JSONReader::Read() and related methods to be more (Closed)
Patch Set: fixens Created 11 years, 12 months 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
OLDNEW
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "base/basictypes.h" 5 #include "base/basictypes.h"
6 #include "base/file_util.h" 6 #include "base/file_util.h"
7 #include "base/json_reader.h" 7 #include "base/json_reader.h"
8 #include "base/json_writer.h" 8 #include "base/json_writer.h"
9 #include "base/path_service.h" 9 #include "base/path_service.h"
10 #include "base/string_util.h" 10 #include "base/string_util.h"
11 #include "base/values.h" 11 #include "base/values.h"
12 #include "chrome/common/chrome_paths.h" 12 #include "chrome/common/chrome_paths.h"
13 #include "chrome/common/json_value_serializer.h" 13 #include "chrome/common/json_value_serializer.h"
14 #include "testing/gtest/include/gtest/gtest.h" 14 #include "testing/gtest/include/gtest/gtest.h"
15 15
16 TEST(JSONValueSerializerTest, Roundtrip) { 16 TEST(JSONValueSerializerTest, Roundtrip) {
17 const std::string original_serialization = 17 const std::string original_serialization =
18 "{\"bool\":true,\"int\":42,\"list\":[1,2],\"null\":null,\"real\":3.14}"; 18 "{\"bool\":true,\"int\":42,\"list\":[1,2],\"null\":null,\"real\":3.14}";
19 Value* root = NULL;
20 JSONStringValueSerializer serializer(original_serialization); 19 JSONStringValueSerializer serializer(original_serialization);
21 ASSERT_TRUE(serializer.Deserialize(&root, NULL)); 20 scoped_ptr<Value> root(serializer.Deserialize(NULL));
22 ASSERT_TRUE(root); 21 ASSERT_TRUE(root.get());
23 ASSERT_TRUE(root->IsType(Value::TYPE_DICTIONARY)); 22 ASSERT_TRUE(root->IsType(Value::TYPE_DICTIONARY));
24 23
25 DictionaryValue* root_dict = static_cast<DictionaryValue*>(root); 24 DictionaryValue* root_dict = static_cast<DictionaryValue*>(root.get());
26 25
27 Value* null_value = NULL; 26 Value* null_value = NULL;
28 ASSERT_TRUE(root_dict->Get(L"null", &null_value)); 27 ASSERT_TRUE(root_dict->Get(L"null", &null_value));
29 ASSERT_TRUE(null_value); 28 ASSERT_TRUE(null_value);
30 ASSERT_TRUE(null_value->IsType(Value::TYPE_NULL)); 29 ASSERT_TRUE(null_value->IsType(Value::TYPE_NULL));
31 30
32 bool bool_value = false; 31 bool bool_value = false;
33 ASSERT_TRUE(root_dict->GetBoolean(L"bool", &bool_value)); 32 ASSERT_TRUE(root_dict->GetBoolean(L"bool", &bool_value));
34 ASSERT_TRUE(bool_value); 33 ASSERT_TRUE(bool_value);
35 34
(...skipping 18 matching lines...) Expand all
54 ASSERT_TRUE(mutable_serializer.Serialize(*root_dict)); 53 ASSERT_TRUE(mutable_serializer.Serialize(*root_dict));
55 const std::string pretty_serialization = 54 const std::string pretty_serialization =
56 "{\r\n" 55 "{\r\n"
57 " \"bool\": true,\r\n" 56 " \"bool\": true,\r\n"
58 " \"int\": 42,\r\n" 57 " \"int\": 42,\r\n"
59 " \"list\": [ 1, 2 ],\r\n" 58 " \"list\": [ 1, 2 ],\r\n"
60 " \"null\": null,\r\n" 59 " \"null\": null,\r\n"
61 " \"real\": 3.14\r\n" 60 " \"real\": 3.14\r\n"
62 "}\r\n"; 61 "}\r\n";
63 ASSERT_EQ(pretty_serialization, test_serialization); 62 ASSERT_EQ(pretty_serialization, test_serialization);
64
65 delete root;
66 } 63 }
67 64
68 TEST(JSONValueSerializerTest, StringEscape) { 65 TEST(JSONValueSerializerTest, StringEscape) {
69 std::wstring all_chars; 66 std::wstring all_chars;
70 for (int i = 1; i < 256; ++i) { 67 for (int i = 1; i < 256; ++i) {
71 all_chars += static_cast<wchar_t>(i); 68 all_chars += static_cast<wchar_t>(i);
72 } 69 }
73 // Generated in in Firefox using the following js (with an extra backslash for 70 // Generated in in Firefox using the following js (with an extra backslash for
74 // double quote): 71 // double quote):
75 // var s = ''; 72 // var s = '';
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
112 root.SetString(L"web", test); 109 root.SetString(L"web", test);
113 110
114 std::string expected = "{\"web\":\"\\u7F51\\u9875\"}"; 111 std::string expected = "{\"web\":\"\\u7F51\\u9875\"}";
115 112
116 std::string actual; 113 std::string actual;
117 JSONStringValueSerializer serializer(&actual); 114 JSONStringValueSerializer serializer(&actual);
118 ASSERT_TRUE(serializer.Serialize(root)); 115 ASSERT_TRUE(serializer.Serialize(root));
119 ASSERT_EQ(expected, actual); 116 ASSERT_EQ(expected, actual);
120 117
121 // escaped ascii text -> json 118 // escaped ascii text -> json
122 Value* deserial_root = NULL;
123 JSONStringValueSerializer deserializer(expected); 119 JSONStringValueSerializer deserializer(expected);
124 ASSERT_TRUE(deserializer.Deserialize(&deserial_root, NULL)); 120 scoped_ptr<Value> deserial_root(deserializer.Deserialize(NULL));
125 DictionaryValue* dict_root = static_cast<DictionaryValue*>(deserial_root); 121 ASSERT_TRUE(deserial_root.get());
122 DictionaryValue* dict_root =
123 static_cast<DictionaryValue*>(deserial_root.get());
126 std::wstring web_value; 124 std::wstring web_value;
127 ASSERT_TRUE(dict_root->GetString(L"web", &web_value)); 125 ASSERT_TRUE(dict_root->GetString(L"web", &web_value));
128 ASSERT_EQ(test, web_value); 126 ASSERT_EQ(test, web_value);
129 delete deserial_root;
130 } 127 }
131 128
132 TEST(JSONValueSerializerTest, HexStrings) { 129 TEST(JSONValueSerializerTest, HexStrings) {
133 // hex string json -> escaped ascii text 130 // hex string json -> escaped ascii text
134 DictionaryValue root; 131 DictionaryValue root;
135 std::wstring test(L"\x01\x02"); 132 std::wstring test(L"\x01\x02");
136 root.SetString(L"test", test); 133 root.SetString(L"test", test);
137 134
138 std::string expected = "{\"test\":\"\\x01\\x02\"}"; 135 std::string expected = "{\"test\":\"\\x01\\x02\"}";
139 136
140 std::string actual; 137 std::string actual;
141 JSONStringValueSerializer serializer(&actual); 138 JSONStringValueSerializer serializer(&actual);
142 ASSERT_TRUE(serializer.Serialize(root)); 139 ASSERT_TRUE(serializer.Serialize(root));
143 ASSERT_EQ(expected, actual); 140 ASSERT_EQ(expected, actual);
144 141
145 // escaped ascii text -> json 142 // escaped ascii text -> json
146 Value* deserial_root = NULL;
147 JSONStringValueSerializer deserializer(expected); 143 JSONStringValueSerializer deserializer(expected);
148 ASSERT_TRUE(deserializer.Deserialize(&deserial_root, NULL)); 144 scoped_ptr<Value> deserial_root(deserializer.Deserialize(NULL));
149 DictionaryValue* dict_root = static_cast<DictionaryValue*>(deserial_root); 145 ASSERT_TRUE(deserial_root.get());
146 DictionaryValue* dict_root =
147 static_cast<DictionaryValue*>(deserial_root.get());
150 std::wstring test_value; 148 std::wstring test_value;
151 ASSERT_TRUE(dict_root->GetString(L"test", &test_value)); 149 ASSERT_TRUE(dict_root->GetString(L"test", &test_value));
152 ASSERT_EQ(test, test_value); 150 ASSERT_EQ(test, test_value);
153 delete deserial_root;
154 151
155 // Test converting escaped regular chars 152 // Test converting escaped regular chars
156 deserial_root = NULL;
157 std::string escaped_chars = "{\"test\":\"\\x67\\x6f\"}"; 153 std::string escaped_chars = "{\"test\":\"\\x67\\x6f\"}";
158 JSONStringValueSerializer deserializer2(escaped_chars); 154 JSONStringValueSerializer deserializer2(escaped_chars);
159 ASSERT_TRUE(deserializer2.Deserialize(&deserial_root, NULL)); 155 deserial_root.reset(deserializer2.Deserialize(NULL));
160 dict_root = static_cast<DictionaryValue*>(deserial_root); 156 ASSERT_TRUE(deserial_root.get());
157 dict_root = static_cast<DictionaryValue*>(deserial_root.get());
161 ASSERT_TRUE(dict_root->GetString(L"test", &test_value)); 158 ASSERT_TRUE(dict_root->GetString(L"test", &test_value));
162 ASSERT_EQ(std::wstring(L"go"), test_value); 159 ASSERT_EQ(std::wstring(L"go"), test_value);
163 delete deserial_root;
164 } 160 }
165 161
166 TEST(JSONValueSerializerTest, AllowTrailingComma) { 162 TEST(JSONValueSerializerTest, AllowTrailingComma) {
167 Value* root = NULL; 163 scoped_ptr<Value> root;
168 Value* root_expected = NULL; 164 scoped_ptr<Value> root_expected;
169 std::string test_with_commas("{\"key\": [true,],}"); 165 std::string test_with_commas("{\"key\": [true,],}");
170 std::string test_no_commas("{\"key\": [true]}"); 166 std::string test_no_commas("{\"key\": [true]}");
171 167
172 JSONStringValueSerializer serializer(test_with_commas); 168 JSONStringValueSerializer serializer(test_with_commas);
173 serializer.set_allow_trailing_comma(true); 169 serializer.set_allow_trailing_comma(true);
174 JSONStringValueSerializer serializer_expected(test_no_commas); 170 JSONStringValueSerializer serializer_expected(test_no_commas);
175 ASSERT_TRUE(serializer.Deserialize(&root, NULL)); 171 root.reset(serializer.Deserialize(NULL));
176 ASSERT_TRUE(serializer_expected.Deserialize(&root_expected, NULL)); 172 ASSERT_TRUE(root.get());
177 ASSERT_TRUE(root->Equals(root_expected)); 173 root_expected.reset(serializer_expected.Deserialize(NULL));
178 174 ASSERT_TRUE(root_expected.get());
179 delete root; 175 ASSERT_TRUE(root->Equals(root_expected.get()));
180 delete root_expected;
181 } 176 }
182 177
183 namespace { 178 namespace {
184 179
185 void ValidateJsonList(const std::string& json) { 180 void ValidateJsonList(const std::string& json) {
186 Value* root = NULL; 181 scoped_ptr<Value> root(JSONReader::Read(json, false));
187 ASSERT_TRUE(JSONReader::Read(json, &root, false)); 182 ASSERT_TRUE(root.get() && root->IsType(Value::TYPE_LIST));
188 ASSERT_TRUE(root && root->IsType(Value::TYPE_LIST)); 183 ListValue* list = static_cast<ListValue*>(root.get());
189 ListValue* list = static_cast<ListValue*>(root);
190 ASSERT_EQ(1U, list->GetSize()); 184 ASSERT_EQ(1U, list->GetSize());
191 Value* elt = NULL; 185 Value* elt = NULL;
192 ASSERT_TRUE(list->Get(0, &elt)); 186 ASSERT_TRUE(list->Get(0, &elt));
193 int value = 0; 187 int value = 0;
194 ASSERT_TRUE(elt && elt->GetAsInteger(&value)); 188 ASSERT_TRUE(elt && elt->GetAsInteger(&value));
195 ASSERT_EQ(1, value); 189 ASSERT_EQ(1, value);
196 delete root;
197 } 190 }
198 191
199 } // namespace 192 } // namespace
200 193
201 TEST(JSONValueSerializerTest, JSONReaderComments) { 194 TEST(JSONValueSerializerTest, JSONReaderComments) {
202 ValidateJsonList("[ // 2, 3, ignore me ] \n1 ]"); 195 ValidateJsonList("[ // 2, 3, ignore me ] \n1 ]");
203 ValidateJsonList("[ /* 2, \n3, ignore me ]*/ \n1 ]"); 196 ValidateJsonList("[ /* 2, \n3, ignore me ]*/ \n1 ]");
204 ValidateJsonList("//header\n[ // 2, \n// 3, \n1 ]// footer"); 197 ValidateJsonList("//header\n[ // 2, \n// 3, \n1 ]// footer");
205 ValidateJsonList("/*\n[ // 2, \n// 3, \n1 ]*/[1]"); 198 ValidateJsonList("/*\n[ // 2, \n// 3, \n1 ]*/[1]");
206 ValidateJsonList("[ 1 /* one */ ] /* end */"); 199 ValidateJsonList("[ 1 /* one */ ] /* end */");
207 ValidateJsonList("[ 1 //// ,2\r\n ]"); 200 ValidateJsonList("[ 1 //// ,2\r\n ]");
208 201
209 Value* root = NULL; 202 scoped_ptr<Value> root;
203
210 // It's ok to have a comment in a string. 204 // It's ok to have a comment in a string.
211 ASSERT_TRUE(JSONReader::Read("[\"// ok\\n /* foo */ \"]", &root, false)); 205 root.reset(JSONReader::Read("[\"// ok\\n /* foo */ \"]", false));
212 ASSERT_TRUE(root && root->IsType(Value::TYPE_LIST)); 206 ASSERT_TRUE(root.get() && root->IsType(Value::TYPE_LIST));
213 ListValue* list = static_cast<ListValue*>(root); 207 ListValue* list = static_cast<ListValue*>(root.get());
214 ASSERT_EQ(1U, list->GetSize()); 208 ASSERT_EQ(1U, list->GetSize());
215 Value* elt = NULL; 209 Value* elt = NULL;
216 ASSERT_TRUE(list->Get(0, &elt)); 210 ASSERT_TRUE(list->Get(0, &elt));
217 std::wstring value; 211 std::wstring value;
218 ASSERT_TRUE(elt && elt->GetAsString(&value)); 212 ASSERT_TRUE(elt && elt->GetAsString(&value));
219 ASSERT_EQ(L"// ok\n /* foo */ ", value); 213 ASSERT_EQ(L"// ok\n /* foo */ ", value);
220 delete root;
221 214
222 root = NULL;
223 // You can't nest comments. 215 // You can't nest comments.
224 ASSERT_FALSE(JSONReader::Read("/* /* inner */ outer */ [ 1 ]", &root, false)); 216 root.reset(JSONReader::Read("/* /* inner */ outer */ [ 1 ]", false));
217 ASSERT_FALSE(root.get());
225 218
226 // Not a open comment token. 219 // Not a open comment token.
227 ASSERT_FALSE(JSONReader::Read("/ * * / [1]", &root, false)); 220 root.reset(JSONReader::Read("/ * * / [1]", false));
221 ASSERT_FALSE(root.get());
228 } 222 }
229 223
230 namespace { 224 namespace {
231 class JSONFileValueSerializerTest : public testing::Test { 225 class JSONFileValueSerializerTest : public testing::Test {
232 protected: 226 protected:
233 virtual void SetUp() { 227 virtual void SetUp() {
234 // Name a subdirectory of the temp directory. 228 // Name a subdirectory of the temp directory.
235 ASSERT_TRUE(PathService::Get(base::DIR_TEMP, &test_dir_)); 229 ASSERT_TRUE(PathService::Get(base::DIR_TEMP, &test_dir_));
236 file_util::AppendToPath(&test_dir_, L"JSONFileValueSerializerTest"); 230 file_util::AppendToPath(&test_dir_, L"JSONFileValueSerializerTest");
237 231
(...skipping 16 matching lines...) Expand all
254 #if defined(OS_WIN) 248 #if defined(OS_WIN)
255 TEST_F(JSONFileValueSerializerTest, Roundtrip) { 249 TEST_F(JSONFileValueSerializerTest, Roundtrip) {
256 std::wstring original_file_path; 250 std::wstring original_file_path;
257 ASSERT_TRUE( 251 ASSERT_TRUE(
258 PathService::Get(chrome::DIR_TEST_DATA, &original_file_path)); 252 PathService::Get(chrome::DIR_TEST_DATA, &original_file_path));
259 file_util::AppendToPath(&original_file_path, L"serializer_test.js"); 253 file_util::AppendToPath(&original_file_path, L"serializer_test.js");
260 254
261 ASSERT_TRUE(file_util::PathExists(original_file_path)); 255 ASSERT_TRUE(file_util::PathExists(original_file_path));
262 256
263 JSONFileValueSerializer deserializer(original_file_path); 257 JSONFileValueSerializer deserializer(original_file_path);
264 Value* root; 258 scoped_ptr<Value> root;
265 ASSERT_TRUE(deserializer.Deserialize(&root, NULL)); 259 root.reset(deserializer.Deserialize(NULL));
266 260
267 ASSERT_TRUE(root); 261 ASSERT_TRUE(root.get());
268 ASSERT_TRUE(root->IsType(Value::TYPE_DICTIONARY)); 262 ASSERT_TRUE(root->IsType(Value::TYPE_DICTIONARY));
269 263
270 DictionaryValue* root_dict = static_cast<DictionaryValue*>(root); 264 DictionaryValue* root_dict = static_cast<DictionaryValue*>(root.get());
271 265
272 Value* null_value = NULL; 266 Value* null_value = NULL;
273 ASSERT_TRUE(root_dict->Get(L"null", &null_value)); 267 ASSERT_TRUE(root_dict->Get(L"null", &null_value));
274 ASSERT_TRUE(null_value); 268 ASSERT_TRUE(null_value);
275 ASSERT_TRUE(null_value->IsType(Value::TYPE_NULL)); 269 ASSERT_TRUE(null_value->IsType(Value::TYPE_NULL));
276 270
277 bool bool_value = false; 271 bool bool_value = false;
278 ASSERT_TRUE(root_dict->GetBoolean(L"bool", &bool_value)); 272 ASSERT_TRUE(root_dict->GetBoolean(L"bool", &bool_value));
279 ASSERT_TRUE(bool_value); 273 ASSERT_TRUE(bool_value);
280 274
(...skipping 10 matching lines...) Expand all
291 file_util::AppendToPath(&written_file_path, L"test_output.js"); 285 file_util::AppendToPath(&written_file_path, L"test_output.js");
292 286
293 ASSERT_FALSE(file_util::PathExists(written_file_path)); 287 ASSERT_FALSE(file_util::PathExists(written_file_path));
294 JSONFileValueSerializer serializer(written_file_path); 288 JSONFileValueSerializer serializer(written_file_path);
295 ASSERT_TRUE(serializer.Serialize(*root)); 289 ASSERT_TRUE(serializer.Serialize(*root));
296 ASSERT_TRUE(file_util::PathExists(written_file_path)); 290 ASSERT_TRUE(file_util::PathExists(written_file_path));
297 291
298 // Now compare file contents. 292 // Now compare file contents.
299 EXPECT_TRUE(file_util::ContentsEqual(original_file_path, written_file_path)); 293 EXPECT_TRUE(file_util::ContentsEqual(original_file_path, written_file_path));
300 EXPECT_TRUE(file_util::Delete(written_file_path, false)); 294 EXPECT_TRUE(file_util::Delete(written_file_path, false));
301
302 delete root;
303 } 295 }
304 296
305 TEST_F(JSONFileValueSerializerTest, RoundtripNested) { 297 TEST_F(JSONFileValueSerializerTest, RoundtripNested) {
306 std::wstring original_file_path; 298 std::wstring original_file_path;
307 ASSERT_TRUE( 299 ASSERT_TRUE(
308 PathService::Get(chrome::DIR_TEST_DATA, &original_file_path)); 300 PathService::Get(chrome::DIR_TEST_DATA, &original_file_path));
309 file_util::AppendToPath(&original_file_path, L"serializer_nested_test.js"); 301 file_util::AppendToPath(&original_file_path, L"serializer_nested_test.js");
310 302
311 ASSERT_TRUE(file_util::PathExists(original_file_path)); 303 ASSERT_TRUE(file_util::PathExists(original_file_path));
312 304
313 JSONFileValueSerializer deserializer(original_file_path); 305 JSONFileValueSerializer deserializer(original_file_path);
314 Value* root; 306 scoped_ptr<Value> root;
315 ASSERT_TRUE(deserializer.Deserialize(&root, NULL)); 307 root.reset(deserializer.Deserialize(NULL));
308 ASSERT_TRUE(root.get());
316 309
317 // Now try writing. 310 // Now try writing.
318 std::wstring written_file_path = test_dir_; 311 std::wstring written_file_path = test_dir_;
319 file_util::AppendToPath(&written_file_path, L"test_output.js"); 312 file_util::AppendToPath(&written_file_path, L"test_output.js");
320 313
321 ASSERT_FALSE(file_util::PathExists(written_file_path)); 314 ASSERT_FALSE(file_util::PathExists(written_file_path));
322 JSONFileValueSerializer serializer(written_file_path); 315 JSONFileValueSerializer serializer(written_file_path);
323 ASSERT_TRUE(serializer.Serialize(*root)); 316 ASSERT_TRUE(serializer.Serialize(*root));
324 ASSERT_TRUE(file_util::PathExists(written_file_path)); 317 ASSERT_TRUE(file_util::PathExists(written_file_path));
325 318
326 // Now compare file contents. 319 // Now compare file contents.
327 EXPECT_TRUE(file_util::ContentsEqual(original_file_path, written_file_path)); 320 EXPECT_TRUE(file_util::ContentsEqual(original_file_path, written_file_path));
328 EXPECT_TRUE(file_util::Delete(written_file_path, false)); 321 EXPECT_TRUE(file_util::Delete(written_file_path, false));
329
330 delete root;
331 } 322 }
332 323
333 TEST_F(JSONFileValueSerializerTest, NoWhitespace) { 324 TEST_F(JSONFileValueSerializerTest, NoWhitespace) {
334 std::wstring source_file_path; 325 std::wstring source_file_path;
335 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &source_file_path)); 326 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &source_file_path));
336 file_util::AppendToPath(&source_file_path, 327 file_util::AppendToPath(&source_file_path,
337 L"serializer_test_nowhitespace.js"); 328 L"serializer_test_nowhitespace.js");
338 ASSERT_TRUE(file_util::PathExists(source_file_path)); 329 ASSERT_TRUE(file_util::PathExists(source_file_path));
339 JSONFileValueSerializer serializer(source_file_path); 330 JSONFileValueSerializer serializer(source_file_path);
340 Value* root; 331 scoped_ptr<Value> root;
341 ASSERT_TRUE(serializer.Deserialize(&root, NULL)); 332 root.reset(serializer.Deserialize(NULL));
342 ASSERT_TRUE(root); 333 ASSERT_TRUE(root.get());
343 delete root;
344 } 334 }
345 #endif // defined(OS_WIN) 335 #endif // defined(OS_WIN)
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698