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

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

Issue 441008: Many changes to DictionaryValues:... (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: '' Created 11 years 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
OLDNEW
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2009 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 "testing/gtest/include/gtest/gtest.h" 5 #include "testing/gtest/include/gtest/gtest.h"
6 #include "base/json/json_reader.h" 6 #include "base/json/json_reader.h"
7 #include "base/scoped_ptr.h" 7 #include "base/scoped_ptr.h"
8 #include "base/values.h" 8 #include "base/values.h"
9 #include "build/build_config.h" 9 #include "build/build_config.h"
10 10
11 namespace base { 11 namespace base {
(...skipping 278 matching lines...) Expand 10 before | Expand all | Expand 10 after
290 EXPECT_FALSE(root.get()); 290 EXPECT_FALSE(root.get());
291 root.reset(JSONReader::Read("[true,,false]", true)); 291 root.reset(JSONReader::Read("[true,,false]", true));
292 EXPECT_FALSE(root.get()); 292 EXPECT_FALSE(root.get());
293 293
294 // Test objects 294 // Test objects
295 root.reset(JSONReader::Read("{}", false)); 295 root.reset(JSONReader::Read("{}", false));
296 ASSERT_TRUE(root.get()); 296 ASSERT_TRUE(root.get());
297 ASSERT_TRUE(root->IsType(Value::TYPE_DICTIONARY)); 297 ASSERT_TRUE(root->IsType(Value::TYPE_DICTIONARY));
298 298
299 root.reset(JSONReader::Read( 299 root.reset(JSONReader::Read(
300 "{\"number\":9.87654321, \"null\":null , \"\\x53\" : \"str\" }", 300 "{\"number\":9.87654321, \"null\":null , \"\\x53\" : \"str\" }", false));
301 false));
302 ASSERT_TRUE(root.get()); 301 ASSERT_TRUE(root.get());
303 ASSERT_TRUE(root->IsType(Value::TYPE_DICTIONARY)); 302 ASSERT_TRUE(root->IsType(Value::TYPE_DICTIONARY));
304 DictionaryValue* dict_val = static_cast<DictionaryValue*>(root.get()); 303 DictionaryValue* dict_val = static_cast<DictionaryValue*>(root.get());
305 real_val = 0.0; 304 real_val = 0.0;
306 ASSERT_TRUE(dict_val->GetReal(L"number", &real_val)); 305 ASSERT_TRUE(dict_val->GetReal(L"number", &real_val));
307 ASSERT_DOUBLE_EQ(9.87654321, real_val); 306 ASSERT_DOUBLE_EQ(9.87654321, real_val);
308 Value* null_val = NULL; 307 Value* null_val = NULL;
309 ASSERT_TRUE(dict_val->Get(L"null", &null_val)); 308 ASSERT_TRUE(dict_val->Get(L"null", &null_val));
310 ASSERT_TRUE(null_val->IsType(Value::TYPE_NULL)); 309 ASSERT_TRUE(null_val->IsType(Value::TYPE_NULL));
311 str_val.clear(); 310 str_val.clear();
312 ASSERT_TRUE(dict_val->GetString(L"S", &str_val)); 311 ASSERT_TRUE(dict_val->GetString(L"S", &str_val));
313 ASSERT_EQ(L"str", str_val); 312 ASSERT_EQ(L"str", str_val);
314 313
315 root2.reset(JSONReader::Read( 314 root2.reset(JSONReader::Read(
316 "{\"number\":9.87654321, \"null\":null , \"\\x53\" : \"str\", }", true)); 315 "{\"number\":9.87654321, \"null\":null , \"\\x53\" : \"str\", }", true));
317 ASSERT_TRUE(root2.get()); 316 ASSERT_TRUE(root2.get());
318 EXPECT_TRUE(root->Equals(root2.get())); 317 EXPECT_TRUE(root->Equals(root2.get()));
319 318
320 // Test newline equivalence. 319 // Test newline equivalence.
321 root2.reset(JSONReader::Read( 320 root2.reset(JSONReader::Read(
322 "{\n" 321 "{\n"
323 " \"number\":9.87654321,\n" 322 " \"number\":9.87654321,\n"
324 " \"null\":null,\n" 323 " \"null\":null,\n"
325 " \"\\x53\":\"str\",\n" 324 " \"\\x53\":\"str\",\n"
326 "}\n", true)); 325 "}\n", true));
327 ASSERT_TRUE(root2.get()); 326 ASSERT_TRUE(root2.get());
328 EXPECT_TRUE(root->Equals(root2.get())); 327 EXPECT_TRUE(root->Equals(root2.get()));
329 328
330 root2.reset(JSONReader::Read( 329 root2.reset(JSONReader::Read(
331 "{\r\n" 330 "{\r\n"
332 " \"number\":9.87654321,\r\n" 331 " \"number\":9.87654321,\r\n"
333 " \"null\":null,\r\n" 332 " \"null\":null,\r\n"
334 " \"\\x53\":\"str\",\r\n" 333 " \"\\x53\":\"str\",\r\n"
335 "}\r\n", true)); 334 "}\r\n", true));
336 ASSERT_TRUE(root2.get()); 335 ASSERT_TRUE(root2.get());
337 EXPECT_TRUE(root->Equals(root2.get())); 336 EXPECT_TRUE(root->Equals(root2.get()));
338 337
339 // Test nesting 338 // Test nesting
340 root.reset(JSONReader::Read( 339 root.reset(JSONReader::Read(
341 "{\"inner\":{\"array\":[true]},\"false\":false,\"d\":{}}", false)); 340 "{\"inner\":{\"array\":[true]},\"false\":false,\"d\":{}}", false));
342 ASSERT_TRUE(root.get()); 341 ASSERT_TRUE(root.get());
343 ASSERT_TRUE(root->IsType(Value::TYPE_DICTIONARY)); 342 ASSERT_TRUE(root->IsType(Value::TYPE_DICTIONARY));
344 dict_val = static_cast<DictionaryValue*>(root.get()); 343 dict_val = static_cast<DictionaryValue*>(root.get());
345 DictionaryValue* inner_dict = NULL; 344 DictionaryValue* inner_dict = NULL;
346 ASSERT_TRUE(dict_val->GetDictionary(L"inner", &inner_dict)); 345 ASSERT_TRUE(dict_val->GetDictionary(L"inner", &inner_dict));
347 ListValue* inner_array = NULL; 346 ListValue* inner_array = NULL;
348 ASSERT_TRUE(inner_dict->GetList(L"array", &inner_array)); 347 ASSERT_TRUE(inner_dict->GetList(L"array", &inner_array));
349 ASSERT_EQ(1U, inner_array->GetSize()); 348 ASSERT_EQ(1U, inner_array->GetSize());
350 bool_value = true; 349 bool_value = true;
351 ASSERT_TRUE(dict_val->GetBoolean(L"false", &bool_value)); 350 ASSERT_TRUE(dict_val->GetBoolean(L"false", &bool_value));
352 ASSERT_FALSE(bool_value); 351 ASSERT_FALSE(bool_value);
353 inner_dict = NULL; 352 inner_dict = NULL;
354 ASSERT_TRUE(dict_val->GetDictionary(L"d", &inner_dict)); 353 ASSERT_TRUE(dict_val->GetDictionary(L"d", &inner_dict));
355 354
356 root2.reset(JSONReader::Read( 355 root2.reset(JSONReader::Read(
357 "{\"inner\": {\"array\":[true] , },\"false\":false,\"d\":{},}", true)); 356 "{\"inner\": {\"array\":[true] , },\"false\":false,\"d\":{},}", true));
358 EXPECT_TRUE(root->Equals(root2.get())); 357 EXPECT_TRUE(root->Equals(root2.get()));
359 358
359 // Test keys with periods
360 root.reset(JSONReader::Read(
361 "{\"a.b\":3,\"c\":2,\"d.e.f\":{\"g.h.i.j\":1}}", false));
362 ASSERT_TRUE(root.get());
363 ASSERT_TRUE(root->IsType(Value::TYPE_DICTIONARY));
364 dict_val = static_cast<DictionaryValue*>(root.get());
365 int integer_value = 0;
366 EXPECT_TRUE(dict_val->GetIntegerWithoutPathExpansion(L"a.b", &integer_value));
367 EXPECT_EQ(3, integer_value);
368 EXPECT_TRUE(dict_val->GetIntegerWithoutPathExpansion(L"c", &integer_value));
369 EXPECT_EQ(2, integer_value);
370 inner_dict = NULL;
371 ASSERT_TRUE(dict_val->GetDictionaryWithoutPathExpansion(L"d.e.f",
372 &inner_dict));
373 ASSERT_EQ(1U, inner_dict->size());
374 EXPECT_TRUE(inner_dict->GetIntegerWithoutPathExpansion(L"g.h.i.j",
375 &integer_value));
376 EXPECT_EQ(1, integer_value);
377
378 root.reset(JSONReader::Read("{\"a\":{\"b\":2},\"a.b\":1}", false));
379 ASSERT_TRUE(root.get());
380 ASSERT_TRUE(root->IsType(Value::TYPE_DICTIONARY));
381 dict_val = static_cast<DictionaryValue*>(root.get());
382 EXPECT_TRUE(dict_val->GetInteger(L"a.b", &integer_value));
383 EXPECT_EQ(2, integer_value);
384 EXPECT_TRUE(dict_val->GetIntegerWithoutPathExpansion(L"a.b", &integer_value));
385 EXPECT_EQ(1, integer_value);
386
360 // Invalid, no closing brace 387 // Invalid, no closing brace
361 root.reset(JSONReader::Read("{\"a\": true", false)); 388 root.reset(JSONReader::Read("{\"a\": true", false));
362 ASSERT_FALSE(root.get()); 389 ASSERT_FALSE(root.get());
363 390
364 // Invalid, keys must be quoted 391 // Invalid, keys must be quoted
365 root.reset(JSONReader::Read("{foo:true}", false)); 392 root.reset(JSONReader::Read("{foo:true}", false));
366 ASSERT_FALSE(root.get()); 393 ASSERT_FALSE(root.get());
367 394
368 // Invalid, trailing comma 395 // Invalid, trailing comma
369 root.reset(JSONReader::Read("{\"a\":true,}", false)); 396 root.reset(JSONReader::Read("{\"a\":true,}", false));
(...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after
511 538
512 root.reset(JSONReader::ReadAndReturnError("[\"xxx\\q\"]", false, 539 root.reset(JSONReader::ReadAndReturnError("[\"xxx\\q\"]", false,
513 &error_message)); 540 &error_message));
514 EXPECT_FALSE(root.get()); 541 EXPECT_FALSE(root.get());
515 EXPECT_EQ(JSONReader::FormatErrorMessage(1, 7, JSONReader::kInvalidEscape), 542 EXPECT_EQ(JSONReader::FormatErrorMessage(1, 7, JSONReader::kInvalidEscape),
516 error_message); 543 error_message);
517 544
518 } 545 }
519 546
520 } // namespace base 547 } // namespace base
OLDNEW
« no previous file with comments | « base/json/json_reader.cc ('k') | base/json/json_writer.cc » ('j') | base/values.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698