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

Side by Side Diff: base/values_unittest.cc

Issue 839143002: Roll Chrome into Mojo. (Closed) Base URL: git@github.com:domokit/mojo.git@master
Patch Set: Rebase Created 5 years, 11 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
« no previous file with comments | « base/values.cc ('k') | build/all.gyp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 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 <limits> 5 #include <limits>
6 6
7 #include "base/memory/scoped_ptr.h" 7 #include "base/memory/scoped_ptr.h"
8 #include "base/strings/string16.h" 8 #include "base/strings/string16.h"
9 #include "base/strings/utf_string_conversions.h" 9 #include "base/strings/utf_string_conversions.h"
10 #include "base/values.h" 10 #include "base/values.h"
(...skipping 294 matching lines...) Expand 10 before | Expand all | Expand 10 after
305 EXPECT_FALSE(deletion_flag); 305 EXPECT_FALSE(deletion_flag);
306 EXPECT_TRUE(dict.HasKey(key)); 306 EXPECT_TRUE(dict.HasKey(key));
307 EXPECT_TRUE(dict.Remove(key, NULL)); 307 EXPECT_TRUE(dict.Remove(key, NULL));
308 EXPECT_TRUE(deletion_flag); 308 EXPECT_TRUE(deletion_flag);
309 EXPECT_FALSE(dict.HasKey(key)); 309 EXPECT_FALSE(dict.HasKey(key));
310 } 310 }
311 } 311 }
312 312
313 TEST(ValuesTest, DictionaryWithoutPathExpansion) { 313 TEST(ValuesTest, DictionaryWithoutPathExpansion) {
314 DictionaryValue dict; 314 DictionaryValue dict;
315 dict.Set("this.is.expanded", make_scoped_ptr(Value::CreateNullValue()));
316 dict.SetWithoutPathExpansion("this.isnt.expanded",
317 make_scoped_ptr(Value::CreateNullValue()));
318
319 EXPECT_FALSE(dict.HasKey("this.is.expanded"));
320 EXPECT_TRUE(dict.HasKey("this"));
321 Value* value1;
322 EXPECT_TRUE(dict.Get("this", &value1));
323 DictionaryValue* value2;
324 ASSERT_TRUE(dict.GetDictionaryWithoutPathExpansion("this", &value2));
325 EXPECT_EQ(value1, value2);
326 EXPECT_EQ(1U, value2->size());
327
328 EXPECT_TRUE(dict.HasKey("this.isnt.expanded"));
329 Value* value3;
330 EXPECT_FALSE(dict.Get("this.isnt.expanded", &value3));
331 Value* value4;
332 ASSERT_TRUE(dict.GetWithoutPathExpansion("this.isnt.expanded", &value4));
333 EXPECT_EQ(Value::TYPE_NULL, value4->GetType());
334 }
335
336 // Tests the deprecated version of SetWithoutPathExpansion.
337 // TODO(estade): remove.
338 TEST(ValuesTest, DictionaryWithoutPathExpansionDeprecated) {
339 DictionaryValue dict;
315 dict.Set("this.is.expanded", Value::CreateNullValue()); 340 dict.Set("this.is.expanded", Value::CreateNullValue());
316 dict.SetWithoutPathExpansion("this.isnt.expanded", Value::CreateNullValue()); 341 dict.SetWithoutPathExpansion("this.isnt.expanded", Value::CreateNullValue());
317 342
318 EXPECT_FALSE(dict.HasKey("this.is.expanded")); 343 EXPECT_FALSE(dict.HasKey("this.is.expanded"));
319 EXPECT_TRUE(dict.HasKey("this")); 344 EXPECT_TRUE(dict.HasKey("this"));
320 Value* value1; 345 Value* value1;
321 EXPECT_TRUE(dict.Get("this", &value1)); 346 EXPECT_TRUE(dict.Get("this", &value1));
322 DictionaryValue* value2; 347 DictionaryValue* value2;
323 ASSERT_TRUE(dict.GetDictionaryWithoutPathExpansion("this", &value2)); 348 ASSERT_TRUE(dict.GetDictionaryWithoutPathExpansion("this", &value2));
324 EXPECT_EQ(value1, value2); 349 EXPECT_EQ(value1, value2);
325 EXPECT_EQ(1U, value2->size()); 350 EXPECT_EQ(1U, value2->size());
326 351
327 EXPECT_TRUE(dict.HasKey("this.isnt.expanded")); 352 EXPECT_TRUE(dict.HasKey("this.isnt.expanded"));
328 Value* value3; 353 Value* value3;
329 EXPECT_FALSE(dict.Get("this.isnt.expanded", &value3)); 354 EXPECT_FALSE(dict.Get("this.isnt.expanded", &value3));
330 Value* value4; 355 Value* value4;
331 ASSERT_TRUE(dict.GetWithoutPathExpansion("this.isnt.expanded", &value4)); 356 ASSERT_TRUE(dict.GetWithoutPathExpansion("this.isnt.expanded", &value4));
332 EXPECT_EQ(Value::TYPE_NULL, value4->GetType()); 357 EXPECT_EQ(Value::TYPE_NULL, value4->GetType());
333 } 358 }
334 359
335 TEST(ValuesTest, DictionaryRemovePath) { 360 TEST(ValuesTest, DictionaryRemovePath) {
336 DictionaryValue dict; 361 DictionaryValue dict;
337 dict.Set("a.long.way.down", new FundamentalValue(1)); 362 dict.SetInteger("a.long.way.down", 1);
338 dict.Set("a.long.key.path", new FundamentalValue(true)); 363 dict.SetBoolean("a.long.key.path", true);
339 364
340 scoped_ptr<Value> removed_item; 365 scoped_ptr<Value> removed_item;
341 EXPECT_TRUE(dict.RemovePath("a.long.way.down", &removed_item)); 366 EXPECT_TRUE(dict.RemovePath("a.long.way.down", &removed_item));
342 ASSERT_TRUE(removed_item); 367 ASSERT_TRUE(removed_item);
343 EXPECT_TRUE(removed_item->IsType(base::Value::TYPE_INTEGER)); 368 EXPECT_TRUE(removed_item->IsType(base::Value::TYPE_INTEGER));
344 EXPECT_FALSE(dict.HasKey("a.long.way.down")); 369 EXPECT_FALSE(dict.HasKey("a.long.way.down"));
345 EXPECT_FALSE(dict.HasKey("a.long.way")); 370 EXPECT_FALSE(dict.HasKey("a.long.way"));
346 EXPECT_TRUE(dict.Get("a.long.key.path", NULL)); 371 EXPECT_TRUE(dict.Get("a.long.key.path", NULL));
347 372
348 removed_item.reset(); 373 removed_item.reset();
349 EXPECT_FALSE(dict.RemovePath("a.long.way.down", &removed_item)); 374 EXPECT_FALSE(dict.RemovePath("a.long.way.down", &removed_item));
350 EXPECT_FALSE(removed_item); 375 EXPECT_FALSE(removed_item);
351 EXPECT_TRUE(dict.Get("a.long.key.path", NULL)); 376 EXPECT_TRUE(dict.Get("a.long.key.path", NULL));
352 377
353 removed_item.reset(); 378 removed_item.reset();
354 EXPECT_TRUE(dict.RemovePath("a.long.key.path", &removed_item)); 379 EXPECT_TRUE(dict.RemovePath("a.long.key.path", &removed_item));
355 ASSERT_TRUE(removed_item); 380 ASSERT_TRUE(removed_item);
356 EXPECT_TRUE(removed_item->IsType(base::Value::TYPE_BOOLEAN)); 381 EXPECT_TRUE(removed_item->IsType(base::Value::TYPE_BOOLEAN));
357 EXPECT_TRUE(dict.empty()); 382 EXPECT_TRUE(dict.empty());
358 } 383 }
359 384
360 TEST(ValuesTest, DeepCopy) { 385 TEST(ValuesTest, DeepCopy) {
361 DictionaryValue original_dict; 386 DictionaryValue original_dict;
362 Value* original_null = Value::CreateNullValue(); 387 Value* original_null = Value::CreateNullValue();
363 original_dict.Set("null", original_null); 388 original_dict.Set("null", make_scoped_ptr(original_null));
364 FundamentalValue* original_bool = new FundamentalValue(true); 389 FundamentalValue* original_bool = new FundamentalValue(true);
365 original_dict.Set("bool", original_bool); 390 original_dict.Set("bool", make_scoped_ptr(original_bool));
366 FundamentalValue* original_int = new FundamentalValue(42); 391 FundamentalValue* original_int = new FundamentalValue(42);
367 original_dict.Set("int", original_int); 392 original_dict.Set("int", make_scoped_ptr(original_int));
368 FundamentalValue* original_double = new FundamentalValue(3.14); 393 FundamentalValue* original_double = new FundamentalValue(3.14);
369 original_dict.Set("double", original_double); 394 original_dict.Set("double", make_scoped_ptr(original_double));
370 StringValue* original_string = new StringValue("hello"); 395 StringValue* original_string = new StringValue("hello");
371 original_dict.Set("string", original_string); 396 original_dict.Set("string", make_scoped_ptr(original_string));
372 StringValue* original_string16 = new StringValue(ASCIIToUTF16("hello16")); 397 StringValue* original_string16 = new StringValue(ASCIIToUTF16("hello16"));
373 original_dict.Set("string16", original_string16); 398 original_dict.Set("string16", make_scoped_ptr(original_string16));
374 399
375 scoped_ptr<char[]> original_buffer(new char[42]); 400 scoped_ptr<char[]> original_buffer(new char[42]);
376 memset(original_buffer.get(), '!', 42); 401 memset(original_buffer.get(), '!', 42);
377 BinaryValue* original_binary = new BinaryValue(original_buffer.Pass(), 42); 402 BinaryValue* original_binary = new BinaryValue(original_buffer.Pass(), 42);
378 original_dict.Set("binary", original_binary); 403 original_dict.Set("binary", original_binary);
379 404
380 ListValue* original_list = new ListValue(); 405 ListValue* original_list = new ListValue();
381 FundamentalValue* original_list_element_0 = new FundamentalValue(0); 406 FundamentalValue* original_list_element_0 = new FundamentalValue(0);
382 original_list->Append(original_list_element_0); 407 original_list->Append(original_list_element_0);
383 FundamentalValue* original_list_element_1 = new FundamentalValue(1); 408 FundamentalValue* original_list_element_1 = new FundamentalValue(1);
384 original_list->Append(original_list_element_1); 409 original_list->Append(original_list_element_1);
385 original_dict.Set("list", original_list); 410 original_dict.Set("list", make_scoped_ptr(original_list));
386 411
387 DictionaryValue* original_nested_dictionary = new DictionaryValue(); 412 DictionaryValue* original_nested_dictionary = new DictionaryValue();
388 original_nested_dictionary->Set("key", new StringValue("value")); 413 original_nested_dictionary->SetString("key", "value");
389 original_dict.Set("dictionary", original_nested_dictionary); 414 original_dict.Set("dictionary", make_scoped_ptr(original_nested_dictionary));
390 415
391 scoped_ptr<DictionaryValue> copy_dict(original_dict.DeepCopy()); 416 scoped_ptr<DictionaryValue> copy_dict(original_dict.DeepCopy());
392 ASSERT_TRUE(copy_dict.get()); 417 ASSERT_TRUE(copy_dict.get());
393 ASSERT_NE(copy_dict.get(), &original_dict); 418 ASSERT_NE(copy_dict.get(), &original_dict);
394 419
395 Value* copy_null = NULL; 420 Value* copy_null = NULL;
396 ASSERT_TRUE(copy_dict->Get("null", &copy_null)); 421 ASSERT_TRUE(copy_dict->Get("null", &copy_null));
397 ASSERT_TRUE(copy_null); 422 ASSERT_TRUE(copy_null);
398 ASSERT_NE(copy_null, original_null); 423 ASSERT_NE(copy_null, original_null);
399 ASSERT_TRUE(copy_null->IsType(Value::TYPE_NULL)); 424 ASSERT_TRUE(copy_null->IsType(Value::TYPE_NULL));
(...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after
508 delete null1; 533 delete null1;
509 delete null2; 534 delete null2;
510 delete boolean; 535 delete boolean;
511 536
512 DictionaryValue dv; 537 DictionaryValue dv;
513 dv.SetBoolean("a", false); 538 dv.SetBoolean("a", false);
514 dv.SetInteger("b", 2); 539 dv.SetInteger("b", 2);
515 dv.SetDouble("c", 2.5); 540 dv.SetDouble("c", 2.5);
516 dv.SetString("d1", "string"); 541 dv.SetString("d1", "string");
517 dv.SetString("d2", ASCIIToUTF16("http://google.com")); 542 dv.SetString("d2", ASCIIToUTF16("http://google.com"));
518 dv.Set("e", Value::CreateNullValue()); 543 dv.Set("e", make_scoped_ptr(Value::CreateNullValue()));
519 544
520 scoped_ptr<DictionaryValue> copy; 545 scoped_ptr<DictionaryValue> copy;
521 copy.reset(dv.DeepCopy()); 546 copy.reset(dv.DeepCopy());
522 EXPECT_TRUE(dv.Equals(copy.get())); 547 EXPECT_TRUE(dv.Equals(copy.get()));
523 548
524 ListValue* list = new ListValue; 549 ListValue* list = new ListValue;
525 list->Append(Value::CreateNullValue()); 550 list->Append(Value::CreateNullValue());
526 list->Append(new DictionaryValue); 551 list->Append(new DictionaryValue);
527 dv.Set("f", list); 552 dv.Set("f", make_scoped_ptr(list));
528 553
529 EXPECT_FALSE(dv.Equals(copy.get())); 554 EXPECT_FALSE(dv.Equals(copy.get()));
530 copy->Set("f", list->DeepCopy()); 555 copy->Set("f", list->DeepCopy());
531 EXPECT_TRUE(dv.Equals(copy.get())); 556 EXPECT_TRUE(dv.Equals(copy.get()));
532 557
533 list->Append(new FundamentalValue(true)); 558 list->Append(new FundamentalValue(true));
534 EXPECT_FALSE(dv.Equals(copy.get())); 559 EXPECT_FALSE(dv.Equals(copy.get()));
535 560
536 // Check if Equals detects differences in only the keys. 561 // Check if Equals detects differences in only the keys.
537 copy.reset(dv.DeepCopy()); 562 copy.reset(dv.DeepCopy());
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after
615 EXPECT_TRUE(original_double_value->Equals(copy_double_value.get())); 640 EXPECT_TRUE(original_double_value->Equals(copy_double_value.get()));
616 EXPECT_TRUE(original_string_value->Equals(copy_string_value.get())); 641 EXPECT_TRUE(original_string_value->Equals(copy_string_value.get()));
617 EXPECT_TRUE(original_string16_value->Equals(copy_string16_value.get())); 642 EXPECT_TRUE(original_string16_value->Equals(copy_string16_value.get()));
618 EXPECT_TRUE(original_binary_value->Equals(copy_binary_value.get())); 643 EXPECT_TRUE(original_binary_value->Equals(copy_binary_value.get()));
619 EXPECT_TRUE(original_list_value->Equals(copy_list_value.get())); 644 EXPECT_TRUE(original_list_value->Equals(copy_list_value.get()));
620 } 645 }
621 646
622 TEST(ValuesTest, RemoveEmptyChildren) { 647 TEST(ValuesTest, RemoveEmptyChildren) {
623 scoped_ptr<DictionaryValue> root(new DictionaryValue); 648 scoped_ptr<DictionaryValue> root(new DictionaryValue);
624 // Remove empty lists and dictionaries. 649 // Remove empty lists and dictionaries.
625 root->Set("empty_dict", new DictionaryValue); 650 root->Set("empty_dict", make_scoped_ptr(new DictionaryValue));
626 root->Set("empty_list", new ListValue); 651 root->Set("empty_list", make_scoped_ptr(new ListValue));
627 root->SetWithoutPathExpansion("a.b.c.d.e", new DictionaryValue); 652 root->SetWithoutPathExpansion("a.b.c.d.e",
653 make_scoped_ptr(new DictionaryValue));
628 root.reset(root->DeepCopyWithoutEmptyChildren()); 654 root.reset(root->DeepCopyWithoutEmptyChildren());
629 EXPECT_TRUE(root->empty()); 655 EXPECT_TRUE(root->empty());
630 656
631 // Make sure we don't prune too much. 657 // Make sure we don't prune too much.
632 root->SetBoolean("bool", true); 658 root->SetBoolean("bool", true);
633 root->Set("empty_dict", new DictionaryValue); 659 root->Set("empty_dict", new DictionaryValue);
634 root->SetString("empty_string", std::string()); 660 root->SetString("empty_string", std::string());
635 root.reset(root->DeepCopyWithoutEmptyChildren()); 661 root.reset(root->DeepCopyWithoutEmptyChildren());
636 EXPECT_EQ(2U, root->size()); 662 EXPECT_EQ(2U, root->size());
637 663
(...skipping 448 matching lines...) Expand 10 before | Expand all | Expand 10 after
1086 EXPECT_FALSE(main_list.GetList(1, NULL)); 1112 EXPECT_FALSE(main_list.GetList(1, NULL));
1087 EXPECT_FALSE(main_list.GetList(2, NULL)); 1113 EXPECT_FALSE(main_list.GetList(2, NULL));
1088 EXPECT_FALSE(main_list.GetList(3, NULL)); 1114 EXPECT_FALSE(main_list.GetList(3, NULL));
1089 EXPECT_FALSE(main_list.GetList(4, NULL)); 1115 EXPECT_FALSE(main_list.GetList(4, NULL));
1090 EXPECT_FALSE(main_list.GetList(5, NULL)); 1116 EXPECT_FALSE(main_list.GetList(5, NULL));
1091 EXPECT_TRUE(main_list.GetList(6, NULL)); 1117 EXPECT_TRUE(main_list.GetList(6, NULL));
1092 EXPECT_FALSE(main_list.GetList(7, NULL)); 1118 EXPECT_FALSE(main_list.GetList(7, NULL));
1093 } 1119 }
1094 1120
1095 } // namespace base 1121 } // namespace base
OLDNEW
« no previous file with comments | « base/values.cc ('k') | build/all.gyp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698