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

Side by Side Diff: base/values_unittest.cc

Issue 1129083003: More base::Values-related bare pointer -> scoped_ptr conversions (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: rebase Created 5 years, 7 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) 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 300 matching lines...) Expand 10 before | Expand all | Expand 10 after
311 EXPECT_FALSE(deletion_flag); 311 EXPECT_FALSE(deletion_flag);
312 EXPECT_TRUE(dict.HasKey(key)); 312 EXPECT_TRUE(dict.HasKey(key));
313 EXPECT_TRUE(dict.Remove(key, NULL)); 313 EXPECT_TRUE(dict.Remove(key, NULL));
314 EXPECT_TRUE(deletion_flag); 314 EXPECT_TRUE(deletion_flag);
315 EXPECT_FALSE(dict.HasKey(key)); 315 EXPECT_FALSE(dict.HasKey(key));
316 } 316 }
317 } 317 }
318 318
319 TEST(ValuesTest, DictionaryWithoutPathExpansion) { 319 TEST(ValuesTest, DictionaryWithoutPathExpansion) {
320 DictionaryValue dict; 320 DictionaryValue dict;
321 dict.Set("this.is.expanded", make_scoped_ptr(Value::CreateNullValue())); 321 dict.Set("this.is.expanded", Value::CreateNullValue());
322 dict.SetWithoutPathExpansion("this.isnt.expanded", 322 dict.SetWithoutPathExpansion("this.isnt.expanded", Value::CreateNullValue());
323 make_scoped_ptr(Value::CreateNullValue()));
324 323
325 EXPECT_FALSE(dict.HasKey("this.is.expanded")); 324 EXPECT_FALSE(dict.HasKey("this.is.expanded"));
326 EXPECT_TRUE(dict.HasKey("this")); 325 EXPECT_TRUE(dict.HasKey("this"));
327 Value* value1; 326 Value* value1;
328 EXPECT_TRUE(dict.Get("this", &value1)); 327 EXPECT_TRUE(dict.Get("this", &value1));
329 DictionaryValue* value2; 328 DictionaryValue* value2;
330 ASSERT_TRUE(dict.GetDictionaryWithoutPathExpansion("this", &value2)); 329 ASSERT_TRUE(dict.GetDictionaryWithoutPathExpansion("this", &value2));
331 EXPECT_EQ(value1, value2); 330 EXPECT_EQ(value1, value2);
332 EXPECT_EQ(1U, value2->size()); 331 EXPECT_EQ(1U, value2->size());
333 332
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
383 382
384 removed_item.reset(); 383 removed_item.reset();
385 EXPECT_TRUE(dict.RemovePath("a.long.key.path", &removed_item)); 384 EXPECT_TRUE(dict.RemovePath("a.long.key.path", &removed_item));
386 ASSERT_TRUE(removed_item); 385 ASSERT_TRUE(removed_item);
387 EXPECT_TRUE(removed_item->IsType(base::Value::TYPE_BOOLEAN)); 386 EXPECT_TRUE(removed_item->IsType(base::Value::TYPE_BOOLEAN));
388 EXPECT_TRUE(dict.empty()); 387 EXPECT_TRUE(dict.empty());
389 } 388 }
390 389
391 TEST(ValuesTest, DeepCopy) { 390 TEST(ValuesTest, DeepCopy) {
392 DictionaryValue original_dict; 391 DictionaryValue original_dict;
393 Value* original_null = Value::CreateNullValue(); 392 Value* original_null = Value::CreateNullValue().release();
danakj 2015/05/08 18:07:26 can you make original_null a scoped_ptr?
Evan Stade 2015/05/08 18:32:09 not really --- it's referenced below; this test ne
danakj 2015/05/08 18:33:42 Can you put the weak ref in a separate variable so
Evan Stade 2015/05/08 18:38:27 I don't think expressing ownership with a raw poin
danakj 2015/05/08 18:48:28 This test was written before scoped_ptr existed fr
Evan Stade 2015/05/08 18:54:12 All the lines with "make_scoped_ptr" were not writ
394 original_dict.Set("null", make_scoped_ptr(original_null)); 393 original_dict.Set("null", make_scoped_ptr(original_null));
395 FundamentalValue* original_bool = new FundamentalValue(true); 394 FundamentalValue* original_bool = new FundamentalValue(true);
396 original_dict.Set("bool", make_scoped_ptr(original_bool)); 395 original_dict.Set("bool", make_scoped_ptr(original_bool));
397 FundamentalValue* original_int = new FundamentalValue(42); 396 FundamentalValue* original_int = new FundamentalValue(42);
398 original_dict.Set("int", make_scoped_ptr(original_int)); 397 original_dict.Set("int", make_scoped_ptr(original_int));
399 FundamentalValue* original_double = new FundamentalValue(3.14); 398 FundamentalValue* original_double = new FundamentalValue(3.14);
400 original_dict.Set("double", make_scoped_ptr(original_double)); 399 original_dict.Set("double", make_scoped_ptr(original_double));
401 StringValue* original_string = new StringValue("hello"); 400 StringValue* original_string = new StringValue("hello");
402 original_dict.Set("string", make_scoped_ptr(original_string)); 401 original_dict.Set("string", make_scoped_ptr(original_string));
403 StringValue* original_string16 = new StringValue(ASCIIToUTF16("hello16")); 402 StringValue* original_string16 = new StringValue(ASCIIToUTF16("hello16"));
(...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after
522 ASSERT_TRUE(copy_value); 521 ASSERT_TRUE(copy_value);
523 ASSERT_NE(copy_value, original_nested_dictionary); 522 ASSERT_NE(copy_value, original_nested_dictionary);
524 ASSERT_TRUE(copy_value->IsType(Value::TYPE_DICTIONARY)); 523 ASSERT_TRUE(copy_value->IsType(Value::TYPE_DICTIONARY));
525 DictionaryValue* copy_nested_dictionary = NULL; 524 DictionaryValue* copy_nested_dictionary = NULL;
526 ASSERT_TRUE(copy_value->GetAsDictionary(&copy_nested_dictionary)); 525 ASSERT_TRUE(copy_value->GetAsDictionary(&copy_nested_dictionary));
527 ASSERT_TRUE(copy_nested_dictionary); 526 ASSERT_TRUE(copy_nested_dictionary);
528 EXPECT_TRUE(copy_nested_dictionary->HasKey("key")); 527 EXPECT_TRUE(copy_nested_dictionary->HasKey("key"));
529 } 528 }
530 529
531 TEST(ValuesTest, Equals) { 530 TEST(ValuesTest, Equals) {
532 Value* null1 = Value::CreateNullValue(); 531 scoped_ptr<Value> null1(Value::CreateNullValue());
533 Value* null2 = Value::CreateNullValue(); 532 scoped_ptr<Value> null2(Value::CreateNullValue());
534 EXPECT_NE(null1, null2); 533 EXPECT_NE(null1.get(), null2.get());
535 EXPECT_TRUE(null1->Equals(null2)); 534 EXPECT_TRUE(null1->Equals(null2.get()));
536 535
537 Value* boolean = new FundamentalValue(false); 536 FundamentalValue boolean(false);
538 EXPECT_FALSE(null1->Equals(boolean)); 537 EXPECT_FALSE(null1->Equals(&boolean));
539 delete null1;
540 delete null2;
541 delete boolean;
542 538
543 DictionaryValue dv; 539 DictionaryValue dv;
544 dv.SetBoolean("a", false); 540 dv.SetBoolean("a", false);
545 dv.SetInteger("b", 2); 541 dv.SetInteger("b", 2);
546 dv.SetDouble("c", 2.5); 542 dv.SetDouble("c", 2.5);
547 dv.SetString("d1", "string"); 543 dv.SetString("d1", "string");
548 dv.SetString("d2", ASCIIToUTF16("http://google.com")); 544 dv.SetString("d2", ASCIIToUTF16("http://google.com"));
549 dv.Set("e", make_scoped_ptr(Value::CreateNullValue())); 545 dv.Set("e", Value::CreateNullValue());
550 546
551 scoped_ptr<DictionaryValue> copy = dv.CreateDeepCopy(); 547 scoped_ptr<DictionaryValue> copy = dv.CreateDeepCopy();
552 EXPECT_TRUE(dv.Equals(copy.get())); 548 EXPECT_TRUE(dv.Equals(copy.get()));
553 549
554 ListValue* list = new ListValue; 550 ListValue* list = new ListValue;
555 list->Append(Value::CreateNullValue()); 551 list->Append(Value::CreateNullValue());
556 list->Append(new DictionaryValue); 552 list->Append(new DictionaryValue);
557 dv.Set("f", make_scoped_ptr(list)); 553 dv.Set("f", make_scoped_ptr(list));
558 554
559 EXPECT_FALSE(dv.Equals(copy.get())); 555 EXPECT_FALSE(dv.Equals(copy.get()));
(...skipping 29 matching lines...) Expand all
589 585
590 // NULL and Value::CreateNullValue() are intentionally different: We need 586 // NULL and Value::CreateNullValue() are intentionally different: We need
591 // support for NULL as a return value for "undefined" without caring for 587 // support for NULL as a return value for "undefined" without caring for
592 // ownership of the pointer. 588 // ownership of the pointer.
593 EXPECT_FALSE(Value::Equals(null1.get(), NULL)); 589 EXPECT_FALSE(Value::Equals(null1.get(), NULL));
594 EXPECT_FALSE(Value::Equals(NULL, null1.get())); 590 EXPECT_FALSE(Value::Equals(NULL, null1.get()));
595 } 591 }
596 592
597 TEST(ValuesTest, DeepCopyCovariantReturnTypes) { 593 TEST(ValuesTest, DeepCopyCovariantReturnTypes) {
598 DictionaryValue original_dict; 594 DictionaryValue original_dict;
599 Value* original_null = Value::CreateNullValue(); 595 Value* original_null = Value::CreateNullValue().release();
danakj 2015/05/08 18:07:26 can you make original_null a scoped_ptr too?
Evan Stade 2015/05/08 18:32:09 Done.
600 original_dict.Set("null", make_scoped_ptr(original_null)); 596 original_dict.Set("null", make_scoped_ptr(original_null));
601 FundamentalValue* original_bool = new FundamentalValue(true); 597 FundamentalValue* original_bool = new FundamentalValue(true);
602 original_dict.Set("bool", make_scoped_ptr(original_bool)); 598 original_dict.Set("bool", make_scoped_ptr(original_bool));
603 FundamentalValue* original_int = new FundamentalValue(42); 599 FundamentalValue* original_int = new FundamentalValue(42);
604 original_dict.Set("int", make_scoped_ptr(original_int)); 600 original_dict.Set("int", make_scoped_ptr(original_int));
605 FundamentalValue* original_double = new FundamentalValue(3.14); 601 FundamentalValue* original_double = new FundamentalValue(3.14);
606 original_dict.Set("double", make_scoped_ptr(original_double)); 602 original_dict.Set("double", make_scoped_ptr(original_double));
607 StringValue* original_string = new StringValue("hello"); 603 StringValue* original_string = new StringValue("hello");
608 original_dict.Set("string", make_scoped_ptr(original_string)); 604 original_dict.Set("string", make_scoped_ptr(original_string));
609 StringValue* original_string16 = new StringValue(ASCIIToUTF16("hello16")); 605 StringValue* original_string16 = new StringValue(ASCIIToUTF16("hello16"));
(...skipping 510 matching lines...) Expand 10 before | Expand all | Expand 10 after
1120 EXPECT_FALSE(main_list.GetList(1, NULL)); 1116 EXPECT_FALSE(main_list.GetList(1, NULL));
1121 EXPECT_FALSE(main_list.GetList(2, NULL)); 1117 EXPECT_FALSE(main_list.GetList(2, NULL));
1122 EXPECT_FALSE(main_list.GetList(3, NULL)); 1118 EXPECT_FALSE(main_list.GetList(3, NULL));
1123 EXPECT_FALSE(main_list.GetList(4, NULL)); 1119 EXPECT_FALSE(main_list.GetList(4, NULL));
1124 EXPECT_FALSE(main_list.GetList(5, NULL)); 1120 EXPECT_FALSE(main_list.GetList(5, NULL));
1125 EXPECT_TRUE(main_list.GetList(6, NULL)); 1121 EXPECT_TRUE(main_list.GetList(6, NULL));
1126 EXPECT_FALSE(main_list.GetList(7, NULL)); 1122 EXPECT_FALSE(main_list.GetList(7, NULL));
1127 } 1123 }
1128 1124
1129 } // namespace base 1125 } // namespace base
OLDNEW
« base/values.cc ('K') | « base/values.cc ('k') | cc/debug/invalidation_benchmark.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698