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

Side by Side Diff: base/values_unittest.cc

Issue 1138103002: base: Use scoped_ptr for ownership of pointers in unittests. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: scopedptrtests: heretoo 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
« no previous file with comments | « base/json/json_writer_unittest.cc ('k') | no next file » | 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 15 matching lines...) Expand all
26 ASSERT_TRUE(settings.Get("global", NULL)); 26 ASSERT_TRUE(settings.Get("global", NULL));
27 homepage = "http://google.com"; 27 homepage = "http://google.com";
28 ASSERT_TRUE(settings.GetString("global.homepage", &homepage)); 28 ASSERT_TRUE(settings.GetString("global.homepage", &homepage));
29 ASSERT_EQ(std::string("http://scurvy.com"), homepage); 29 ASSERT_EQ(std::string("http://scurvy.com"), homepage);
30 30
31 // Test storing a dictionary in a list. 31 // Test storing a dictionary in a list.
32 ListValue* toolbar_bookmarks; 32 ListValue* toolbar_bookmarks;
33 ASSERT_FALSE( 33 ASSERT_FALSE(
34 settings.GetList("global.toolbar.bookmarks", &toolbar_bookmarks)); 34 settings.GetList("global.toolbar.bookmarks", &toolbar_bookmarks));
35 35
36 toolbar_bookmarks = new ListValue; 36 scoped_ptr<ListValue> new_toolbar_bookmarks(new ListValue);
37 settings.Set("global.toolbar.bookmarks", make_scoped_ptr(toolbar_bookmarks)); 37 settings.Set("global.toolbar.bookmarks", new_toolbar_bookmarks.Pass());
38 ASSERT_TRUE(settings.GetList("global.toolbar.bookmarks", &toolbar_bookmarks)); 38 ASSERT_TRUE(settings.GetList("global.toolbar.bookmarks", &toolbar_bookmarks));
39 39
40 DictionaryValue* new_bookmark = new DictionaryValue; 40 scoped_ptr<DictionaryValue> new_bookmark(new DictionaryValue);
41 new_bookmark->SetString("name", "Froogle"); 41 new_bookmark->SetString("name", "Froogle");
42 new_bookmark->SetString("url", "http://froogle.com"); 42 new_bookmark->SetString("url", "http://froogle.com");
43 toolbar_bookmarks->Append(new_bookmark); 43 toolbar_bookmarks->Append(new_bookmark.Pass());
44 44
45 ListValue* bookmark_list; 45 ListValue* bookmark_list;
46 ASSERT_TRUE(settings.GetList("global.toolbar.bookmarks", &bookmark_list)); 46 ASSERT_TRUE(settings.GetList("global.toolbar.bookmarks", &bookmark_list));
47 DictionaryValue* bookmark; 47 DictionaryValue* bookmark;
48 ASSERT_EQ(1U, bookmark_list->GetSize()); 48 ASSERT_EQ(1U, bookmark_list->GetSize());
49 ASSERT_TRUE(bookmark_list->GetDictionary(0, &bookmark)); 49 ASSERT_TRUE(bookmark_list->GetDictionary(0, &bookmark));
50 std::string bookmark_name = "Unnamed"; 50 std::string bookmark_name = "Unnamed";
51 ASSERT_TRUE(bookmark->GetString("name", &bookmark_name)); 51 ASSERT_TRUE(bookmark->GetString("name", &bookmark_name));
52 ASSERT_EQ(std::string("Froogle"), bookmark_name); 52 ASSERT_EQ(std::string("Froogle"), bookmark_name);
53 std::string bookmark_url; 53 std::string bookmark_url;
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
105 } 105 }
106 106
107 TEST(ValuesTest, BinaryValue) { 107 TEST(ValuesTest, BinaryValue) {
108 // Default constructor creates a BinaryValue with a null buffer and size 0. 108 // Default constructor creates a BinaryValue with a null buffer and size 0.
109 scoped_ptr<BinaryValue> binary(new BinaryValue()); 109 scoped_ptr<BinaryValue> binary(new BinaryValue());
110 ASSERT_TRUE(binary.get()); 110 ASSERT_TRUE(binary.get());
111 ASSERT_EQ(NULL, binary->GetBuffer()); 111 ASSERT_EQ(NULL, binary->GetBuffer());
112 ASSERT_EQ(0U, binary->GetSize()); 112 ASSERT_EQ(0U, binary->GetSize());
113 113
114 // Test the common case of a non-empty buffer 114 // Test the common case of a non-empty buffer
115 char* buffer = new char[15]; 115 scoped_ptr<char[]> buffer(new char[15]);
116 binary.reset(new BinaryValue(scoped_ptr<char[]>(buffer), 15)); 116 char* original_buffer = buffer.get();
117 binary.reset(new BinaryValue(buffer.Pass(), 15));
117 ASSERT_TRUE(binary.get()); 118 ASSERT_TRUE(binary.get());
118 ASSERT_TRUE(binary->GetBuffer()); 119 ASSERT_TRUE(binary->GetBuffer());
119 ASSERT_EQ(buffer, binary->GetBuffer()); 120 ASSERT_EQ(original_buffer, binary->GetBuffer());
120 ASSERT_EQ(15U, binary->GetSize()); 121 ASSERT_EQ(15U, binary->GetSize());
121 122
122 char stack_buffer[42]; 123 char stack_buffer[42];
123 memset(stack_buffer, '!', 42); 124 memset(stack_buffer, '!', 42);
124 binary.reset(BinaryValue::CreateWithCopiedBuffer(stack_buffer, 42)); 125 binary.reset(BinaryValue::CreateWithCopiedBuffer(stack_buffer, 42));
125 ASSERT_TRUE(binary.get()); 126 ASSERT_TRUE(binary.get());
126 ASSERT_TRUE(binary->GetBuffer()); 127 ASSERT_TRUE(binary->GetBuffer());
127 ASSERT_NE(stack_buffer, binary->GetBuffer()); 128 ASSERT_NE(stack_buffer, binary->GetBuffer());
128 ASSERT_EQ(42U, binary->GetSize()); 129 ASSERT_EQ(42U, binary->GetSize());
129 ASSERT_EQ(0, memcmp(stack_buffer, binary->GetBuffer(), binary->GetSize())); 130 ASSERT_EQ(0, memcmp(stack_buffer, binary->GetBuffer(), binary->GetSize()));
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after
187 188
188 private: 189 private:
189 bool* deletion_flag_; 190 bool* deletion_flag_;
190 }; 191 };
191 192
192 TEST(ValuesTest, ListDeletion) { 193 TEST(ValuesTest, ListDeletion) {
193 bool deletion_flag = true; 194 bool deletion_flag = true;
194 195
195 { 196 {
196 ListValue list; 197 ListValue list;
197 list.Append(new DeletionTestValue(&deletion_flag)); 198 list.Append(make_scoped_ptr(new DeletionTestValue(&deletion_flag)));
198 EXPECT_FALSE(deletion_flag); 199 EXPECT_FALSE(deletion_flag);
199 } 200 }
200 EXPECT_TRUE(deletion_flag); 201 EXPECT_TRUE(deletion_flag);
201 202
202 { 203 {
203 ListValue list; 204 ListValue list;
204 list.Append(new DeletionTestValue(&deletion_flag)); 205 list.Append(make_scoped_ptr(new DeletionTestValue(&deletion_flag)));
205 EXPECT_FALSE(deletion_flag); 206 EXPECT_FALSE(deletion_flag);
206 list.Clear(); 207 list.Clear();
207 EXPECT_TRUE(deletion_flag); 208 EXPECT_TRUE(deletion_flag);
208 } 209 }
209 210
210 { 211 {
211 ListValue list; 212 ListValue list;
212 list.Append(new DeletionTestValue(&deletion_flag)); 213 list.Append(make_scoped_ptr(new DeletionTestValue(&deletion_flag)));
213 EXPECT_FALSE(deletion_flag); 214 EXPECT_FALSE(deletion_flag);
214 EXPECT_TRUE(list.Set(0, Value::CreateNullValue())); 215 EXPECT_TRUE(list.Set(0, Value::CreateNullValue()));
215 EXPECT_TRUE(deletion_flag); 216 EXPECT_TRUE(deletion_flag);
216 } 217 }
217 } 218 }
218 219
219 TEST(ValuesTest, ListRemoval) { 220 TEST(ValuesTest, ListRemoval) {
220 bool deletion_flag = true; 221 bool deletion_flag = true;
221 scoped_ptr<Value> removed_item; 222 scoped_ptr<Value> removed_item;
222 223
223 { 224 {
224 ListValue list; 225 ListValue list;
225 list.Append(new DeletionTestValue(&deletion_flag)); 226 list.Append(make_scoped_ptr(new DeletionTestValue(&deletion_flag)));
226 EXPECT_FALSE(deletion_flag); 227 EXPECT_FALSE(deletion_flag);
227 EXPECT_EQ(1U, list.GetSize()); 228 EXPECT_EQ(1U, list.GetSize());
228 EXPECT_FALSE(list.Remove(std::numeric_limits<size_t>::max(), 229 EXPECT_FALSE(list.Remove(std::numeric_limits<size_t>::max(),
229 &removed_item)); 230 &removed_item));
230 EXPECT_FALSE(list.Remove(1, &removed_item)); 231 EXPECT_FALSE(list.Remove(1, &removed_item));
231 EXPECT_TRUE(list.Remove(0, &removed_item)); 232 EXPECT_TRUE(list.Remove(0, &removed_item));
232 ASSERT_TRUE(removed_item); 233 ASSERT_TRUE(removed_item);
233 EXPECT_EQ(0U, list.GetSize()); 234 EXPECT_EQ(0U, list.GetSize());
234 } 235 }
235 EXPECT_FALSE(deletion_flag); 236 EXPECT_FALSE(deletion_flag);
236 removed_item.reset(); 237 removed_item.reset();
237 EXPECT_TRUE(deletion_flag); 238 EXPECT_TRUE(deletion_flag);
238 239
239 { 240 {
240 ListValue list; 241 ListValue list;
241 list.Append(new DeletionTestValue(&deletion_flag)); 242 list.Append(make_scoped_ptr(new DeletionTestValue(&deletion_flag)));
242 EXPECT_FALSE(deletion_flag); 243 EXPECT_FALSE(deletion_flag);
243 EXPECT_TRUE(list.Remove(0, NULL)); 244 EXPECT_TRUE(list.Remove(0, NULL));
244 EXPECT_TRUE(deletion_flag); 245 EXPECT_TRUE(deletion_flag);
245 EXPECT_EQ(0U, list.GetSize()); 246 EXPECT_EQ(0U, list.GetSize());
246 } 247 }
247 248
248 { 249 {
249 ListValue list; 250 ListValue list;
250 DeletionTestValue* value = new DeletionTestValue(&deletion_flag); 251 scoped_ptr<DeletionTestValue> value(new DeletionTestValue(&deletion_flag));
251 list.Append(value); 252 DeletionTestValue* original_value = value.get();
253 list.Append(value.Pass());
252 EXPECT_FALSE(deletion_flag); 254 EXPECT_FALSE(deletion_flag);
253 size_t index = 0; 255 size_t index = 0;
254 list.Remove(*value, &index); 256 list.Remove(*original_value, &index);
255 EXPECT_EQ(0U, index); 257 EXPECT_EQ(0U, index);
256 EXPECT_TRUE(deletion_flag); 258 EXPECT_TRUE(deletion_flag);
257 EXPECT_EQ(0U, list.GetSize()); 259 EXPECT_EQ(0U, list.GetSize());
258 } 260 }
259 } 261 }
260 262
261 TEST(ValuesTest, DictionaryDeletion) { 263 TEST(ValuesTest, DictionaryDeletion) {
262 std::string key = "test"; 264 std::string key = "test";
263 bool deletion_flag = true; 265 bool deletion_flag = true;
264 266
(...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after
383 385
384 removed_item.reset(); 386 removed_item.reset();
385 EXPECT_TRUE(dict.RemovePath("a.long.key.path", &removed_item)); 387 EXPECT_TRUE(dict.RemovePath("a.long.key.path", &removed_item));
386 ASSERT_TRUE(removed_item); 388 ASSERT_TRUE(removed_item);
387 EXPECT_TRUE(removed_item->IsType(base::Value::TYPE_BOOLEAN)); 389 EXPECT_TRUE(removed_item->IsType(base::Value::TYPE_BOOLEAN));
388 EXPECT_TRUE(dict.empty()); 390 EXPECT_TRUE(dict.empty());
389 } 391 }
390 392
391 TEST(ValuesTest, DeepCopy) { 393 TEST(ValuesTest, DeepCopy) {
392 DictionaryValue original_dict; 394 DictionaryValue original_dict;
393 Value* original_null = Value::CreateNullValue(); 395 scoped_ptr<Value> scoped_null(Value::CreateNullValue());
394 original_dict.Set("null", make_scoped_ptr(original_null)); 396 Value* original_null = scoped_null.get();
395 FundamentalValue* original_bool = new FundamentalValue(true); 397 original_dict.Set("null", scoped_null.Pass());
396 original_dict.Set("bool", make_scoped_ptr(original_bool)); 398 scoped_ptr<FundamentalValue> scoped_bool(new FundamentalValue(true));
397 FundamentalValue* original_int = new FundamentalValue(42); 399 FundamentalValue* original_bool = scoped_bool.get();
398 original_dict.Set("int", make_scoped_ptr(original_int)); 400 original_dict.Set("bool", scoped_bool.Pass());
399 FundamentalValue* original_double = new FundamentalValue(3.14); 401 scoped_ptr<FundamentalValue> scoped_int(new FundamentalValue(42));
400 original_dict.Set("double", make_scoped_ptr(original_double)); 402 FundamentalValue* original_int = scoped_int.get();
401 StringValue* original_string = new StringValue("hello"); 403 original_dict.Set("int", scoped_int.Pass());
402 original_dict.Set("string", make_scoped_ptr(original_string)); 404 scoped_ptr<FundamentalValue> scoped_double(new FundamentalValue(3.14));
403 StringValue* original_string16 = new StringValue(ASCIIToUTF16("hello16")); 405 FundamentalValue* original_double = scoped_double.get();
404 original_dict.Set("string16", make_scoped_ptr(original_string16)); 406 original_dict.Set("double", scoped_double.Pass());
407 scoped_ptr<StringValue> scoped_string(new StringValue("hello"));
408 StringValue* original_string = scoped_string.get();
409 original_dict.Set("string", scoped_string.Pass());
410 scoped_ptr<StringValue> scoped_string16(
411 new StringValue(ASCIIToUTF16("hello16")));
412 StringValue* original_string16 = scoped_string16.get();
413 original_dict.Set("string16", scoped_string16.Pass());
405 414
406 scoped_ptr<char[]> original_buffer(new char[42]); 415 scoped_ptr<char[]> original_buffer(new char[42]);
407 memset(original_buffer.get(), '!', 42); 416 memset(original_buffer.get(), '!', 42);
408 BinaryValue* original_binary = new BinaryValue(original_buffer.Pass(), 42); 417 scoped_ptr<BinaryValue> scoped_binary(
409 original_dict.Set("binary", original_binary); 418 new BinaryValue(original_buffer.Pass(), 42));
419 BinaryValue* original_binary = scoped_binary.get();
420 original_dict.Set("binary", scoped_binary.Pass());
410 421
411 ListValue* original_list = new ListValue(); 422 scoped_ptr<ListValue> scoped_list(new ListValue());
412 FundamentalValue* original_list_element_0 = new FundamentalValue(0); 423 Value* original_list = scoped_list.get();
413 original_list->Append(original_list_element_0); 424 scoped_ptr<FundamentalValue> scoped_list_element_0(new FundamentalValue(0));
414 FundamentalValue* original_list_element_1 = new FundamentalValue(1); 425 Value* original_list_element_0 = scoped_list_element_0.get();
415 original_list->Append(original_list_element_1); 426 scoped_list->Append(scoped_list_element_0.Pass());
416 original_dict.Set("list", make_scoped_ptr(original_list)); 427 scoped_ptr<FundamentalValue> scoped_list_element_1(new FundamentalValue(1));
428 Value* original_list_element_1 = scoped_list_element_1.get();
429 scoped_list->Append(scoped_list_element_1.Pass());
430 original_dict.Set("list", scoped_list.Pass());
417 431
418 DictionaryValue* original_nested_dictionary = new DictionaryValue(); 432 scoped_ptr<DictionaryValue> scoped_nested_dictionary(new DictionaryValue());
419 original_nested_dictionary->SetString("key", "value"); 433 Value* original_nested_dictionary = scoped_nested_dictionary.get();
420 original_dict.Set("dictionary", make_scoped_ptr(original_nested_dictionary)); 434 scoped_nested_dictionary->SetString("key", "value");
435 original_dict.Set("dictionary", scoped_nested_dictionary.Pass());
421 436
422 scoped_ptr<DictionaryValue> copy_dict = original_dict.CreateDeepCopy(); 437 scoped_ptr<DictionaryValue> copy_dict = original_dict.CreateDeepCopy();
423 ASSERT_TRUE(copy_dict.get()); 438 ASSERT_TRUE(copy_dict.get());
424 ASSERT_NE(copy_dict.get(), &original_dict); 439 ASSERT_NE(copy_dict.get(), &original_dict);
425 440
426 Value* copy_null = NULL; 441 Value* copy_null = NULL;
427 ASSERT_TRUE(copy_dict->Get("null", &copy_null)); 442 ASSERT_TRUE(copy_dict->Get("null", &copy_null));
428 ASSERT_TRUE(copy_null); 443 ASSERT_TRUE(copy_null);
429 ASSERT_NE(copy_null, original_null); 444 ASSERT_NE(copy_null, original_null);
430 ASSERT_TRUE(copy_null->IsType(Value::TYPE_NULL)); 445 ASSERT_TRUE(copy_null->IsType(Value::TYPE_NULL));
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after
522 ASSERT_TRUE(copy_value); 537 ASSERT_TRUE(copy_value);
523 ASSERT_NE(copy_value, original_nested_dictionary); 538 ASSERT_NE(copy_value, original_nested_dictionary);
524 ASSERT_TRUE(copy_value->IsType(Value::TYPE_DICTIONARY)); 539 ASSERT_TRUE(copy_value->IsType(Value::TYPE_DICTIONARY));
525 DictionaryValue* copy_nested_dictionary = NULL; 540 DictionaryValue* copy_nested_dictionary = NULL;
526 ASSERT_TRUE(copy_value->GetAsDictionary(&copy_nested_dictionary)); 541 ASSERT_TRUE(copy_value->GetAsDictionary(&copy_nested_dictionary));
527 ASSERT_TRUE(copy_nested_dictionary); 542 ASSERT_TRUE(copy_nested_dictionary);
528 EXPECT_TRUE(copy_nested_dictionary->HasKey("key")); 543 EXPECT_TRUE(copy_nested_dictionary->HasKey("key"));
529 } 544 }
530 545
531 TEST(ValuesTest, Equals) { 546 TEST(ValuesTest, Equals) {
532 Value* null1 = Value::CreateNullValue(); 547 scoped_ptr<Value> null1(Value::CreateNullValue());
533 Value* null2 = Value::CreateNullValue(); 548 scoped_ptr<Value> null2(Value::CreateNullValue());
534 EXPECT_NE(null1, null2); 549 EXPECT_NE(null1.get(), null2.get());
535 EXPECT_TRUE(null1->Equals(null2)); 550 EXPECT_TRUE(null1->Equals(null2.get()));
536 551
537 Value* boolean = new FundamentalValue(false); 552 scoped_ptr<Value> boolean(new FundamentalValue(false));
538 EXPECT_FALSE(null1->Equals(boolean)); 553 EXPECT_FALSE(null1->Equals(boolean.get()));
539 delete null1;
540 delete null2;
541 delete boolean;
542 554
543 DictionaryValue dv; 555 DictionaryValue dv;
544 dv.SetBoolean("a", false); 556 dv.SetBoolean("a", false);
545 dv.SetInteger("b", 2); 557 dv.SetInteger("b", 2);
546 dv.SetDouble("c", 2.5); 558 dv.SetDouble("c", 2.5);
547 dv.SetString("d1", "string"); 559 dv.SetString("d1", "string");
548 dv.SetString("d2", ASCIIToUTF16("http://google.com")); 560 dv.SetString("d2", ASCIIToUTF16("http://google.com"));
549 dv.Set("e", make_scoped_ptr(Value::CreateNullValue())); 561 dv.Set("e", make_scoped_ptr(Value::CreateNullValue()));
550 562
551 scoped_ptr<DictionaryValue> copy = dv.CreateDeepCopy(); 563 scoped_ptr<DictionaryValue> copy = dv.CreateDeepCopy();
552 EXPECT_TRUE(dv.Equals(copy.get())); 564 EXPECT_TRUE(dv.Equals(copy.get()));
553 565
554 ListValue* list = new ListValue; 566 scoped_ptr<ListValue> list(new ListValue);
555 list->Append(Value::CreateNullValue()); 567 ListValue* original_list = list.get();
556 list->Append(new DictionaryValue); 568 list->Append(make_scoped_ptr(Value::CreateNullValue()));
557 dv.Set("f", make_scoped_ptr(list)); 569 list->Append(make_scoped_ptr(new DictionaryValue));
570 scoped_ptr<Value> list_copy(list->CreateDeepCopy());
558 571
572 dv.Set("f", list.Pass());
559 EXPECT_FALSE(dv.Equals(copy.get())); 573 EXPECT_FALSE(dv.Equals(copy.get()));
560 copy->Set("f", list->CreateDeepCopy()); 574 copy->Set("f", list_copy.Pass());
561 EXPECT_TRUE(dv.Equals(copy.get())); 575 EXPECT_TRUE(dv.Equals(copy.get()));
562 576
563 list->Append(new FundamentalValue(true)); 577 original_list->Append(make_scoped_ptr(new FundamentalValue(true)));
564 EXPECT_FALSE(dv.Equals(copy.get())); 578 EXPECT_FALSE(dv.Equals(copy.get()));
565 579
566 // Check if Equals detects differences in only the keys. 580 // Check if Equals detects differences in only the keys.
567 copy = dv.CreateDeepCopy(); 581 copy = dv.CreateDeepCopy();
568 EXPECT_TRUE(dv.Equals(copy.get())); 582 EXPECT_TRUE(dv.Equals(copy.get()));
569 copy->Remove("a", NULL); 583 copy->Remove("a", NULL);
570 copy->SetBoolean("aa", false); 584 copy->SetBoolean("aa", false);
571 EXPECT_FALSE(dv.Equals(copy.get())); 585 EXPECT_FALSE(dv.Equals(copy.get()));
572 } 586 }
573 587
(...skipping 15 matching lines...) Expand all
589 603
590 // NULL and Value::CreateNullValue() are intentionally different: We need 604 // NULL and Value::CreateNullValue() are intentionally different: We need
591 // support for NULL as a return value for "undefined" without caring for 605 // support for NULL as a return value for "undefined" without caring for
592 // ownership of the pointer. 606 // ownership of the pointer.
593 EXPECT_FALSE(Value::Equals(null1.get(), NULL)); 607 EXPECT_FALSE(Value::Equals(null1.get(), NULL));
594 EXPECT_FALSE(Value::Equals(NULL, null1.get())); 608 EXPECT_FALSE(Value::Equals(NULL, null1.get()));
595 } 609 }
596 610
597 TEST(ValuesTest, DeepCopyCovariantReturnTypes) { 611 TEST(ValuesTest, DeepCopyCovariantReturnTypes) {
598 DictionaryValue original_dict; 612 DictionaryValue original_dict;
599 Value* original_null = Value::CreateNullValue(); 613 scoped_ptr<Value> scoped_null(Value::CreateNullValue());
600 original_dict.Set("null", make_scoped_ptr(original_null)); 614 Value* original_null = scoped_null.get();
601 FundamentalValue* original_bool = new FundamentalValue(true); 615 original_dict.Set("null", scoped_null.Pass());
602 original_dict.Set("bool", make_scoped_ptr(original_bool)); 616 scoped_ptr<FundamentalValue> scoped_bool(new FundamentalValue(true));
603 FundamentalValue* original_int = new FundamentalValue(42); 617 Value* original_bool = scoped_bool.get();
604 original_dict.Set("int", make_scoped_ptr(original_int)); 618 original_dict.Set("bool", scoped_bool.Pass());
605 FundamentalValue* original_double = new FundamentalValue(3.14); 619 scoped_ptr<FundamentalValue> scoped_int(new FundamentalValue(42));
606 original_dict.Set("double", make_scoped_ptr(original_double)); 620 Value* original_int = scoped_int.get();
607 StringValue* original_string = new StringValue("hello"); 621 original_dict.Set("int", scoped_int.Pass());
608 original_dict.Set("string", make_scoped_ptr(original_string)); 622 scoped_ptr<FundamentalValue> scoped_double(new FundamentalValue(3.14));
609 StringValue* original_string16 = new StringValue(ASCIIToUTF16("hello16")); 623 Value* original_double = scoped_double.get();
610 original_dict.Set("string16", make_scoped_ptr(original_string16)); 624 original_dict.Set("double", scoped_double.Pass());
625 scoped_ptr<StringValue> scoped_string(new StringValue("hello"));
626 Value* original_string = scoped_string.get();
627 original_dict.Set("string", scoped_string.Pass());
628 scoped_ptr<StringValue> scoped_string16(
629 new StringValue(ASCIIToUTF16("hello16")));
630 Value* original_string16 = scoped_string16.get();
631 original_dict.Set("string16", scoped_string16.Pass());
611 632
612 scoped_ptr<char[]> original_buffer(new char[42]); 633 scoped_ptr<char[]> original_buffer(new char[42]);
613 memset(original_buffer.get(), '!', 42); 634 memset(original_buffer.get(), '!', 42);
614 BinaryValue* original_binary = new BinaryValue(original_buffer.Pass(), 42); 635 scoped_ptr<BinaryValue> scoped_binary(
615 original_dict.Set("binary", make_scoped_ptr(original_binary)); 636 new BinaryValue(original_buffer.Pass(), 42));
637 Value* original_binary = scoped_binary.get();
638 original_dict.Set("binary", scoped_binary.Pass());
616 639
617 ListValue* original_list = new ListValue(); 640 scoped_ptr<ListValue> scoped_list(new ListValue());
618 FundamentalValue* original_list_element_0 = new FundamentalValue(0); 641 Value* original_list = scoped_list.get();
619 original_list->Append(original_list_element_0); 642 scoped_ptr<FundamentalValue> scoped_list_element_0(new FundamentalValue(0));
620 FundamentalValue* original_list_element_1 = new FundamentalValue(1); 643 scoped_list->Append(scoped_list_element_0.Pass());
621 original_list->Append(original_list_element_1); 644 scoped_ptr<FundamentalValue> scoped_list_element_1(new FundamentalValue(1));
622 original_dict.Set("list", make_scoped_ptr(original_list)); 645 scoped_list->Append(scoped_list_element_1.Pass());
646 original_dict.Set("list", scoped_list.Pass());
623 647
624 Value* original_dict_value = &original_dict; 648 scoped_ptr<Value> copy_dict = original_dict.CreateDeepCopy();
625 Value* original_bool_value = original_bool; 649 scoped_ptr<Value> copy_null = original_null->CreateDeepCopy();
626 Value* original_int_value = original_int; 650 scoped_ptr<Value> copy_bool = original_bool->CreateDeepCopy();
627 Value* original_double_value = original_double; 651 scoped_ptr<Value> copy_int = original_int->CreateDeepCopy();
628 Value* original_string_value = original_string; 652 scoped_ptr<Value> copy_double = original_double->CreateDeepCopy();
629 Value* original_string16_value = original_string16; 653 scoped_ptr<Value> copy_string = original_string->CreateDeepCopy();
630 Value* original_binary_value = original_binary; 654 scoped_ptr<Value> copy_string16 = original_string16->CreateDeepCopy();
631 Value* original_list_value = original_list; 655 scoped_ptr<Value> copy_binary = original_binary->CreateDeepCopy();
656 scoped_ptr<Value> copy_list = original_list->CreateDeepCopy();
632 657
633 scoped_ptr<Value> copy_dict_value = original_dict_value->CreateDeepCopy(); 658 EXPECT_TRUE(original_dict.Equals(copy_dict.get()));
634 scoped_ptr<Value> copy_bool_value = original_bool_value->CreateDeepCopy(); 659 EXPECT_TRUE(original_null->Equals(copy_null.get()));
635 scoped_ptr<Value> copy_int_value = original_int_value->CreateDeepCopy(); 660 EXPECT_TRUE(original_bool->Equals(copy_bool.get()));
636 scoped_ptr<Value> copy_double_value = original_double_value->CreateDeepCopy(); 661 EXPECT_TRUE(original_int->Equals(copy_int.get()));
637 scoped_ptr<Value> copy_string_value = original_string_value->CreateDeepCopy(); 662 EXPECT_TRUE(original_double->Equals(copy_double.get()));
638 scoped_ptr<Value> copy_string16_value = 663 EXPECT_TRUE(original_string->Equals(copy_string.get()));
639 original_string16_value->CreateDeepCopy(); 664 EXPECT_TRUE(original_string16->Equals(copy_string16.get()));
640 scoped_ptr<Value> copy_binary_value = original_binary_value->CreateDeepCopy(); 665 EXPECT_TRUE(original_binary->Equals(copy_binary.get()));
641 scoped_ptr<Value> copy_list_value = original_list_value->CreateDeepCopy(); 666 EXPECT_TRUE(original_list->Equals(copy_list.get()));
642
643 EXPECT_TRUE(original_dict_value->Equals(copy_dict_value.get()));
644 EXPECT_TRUE(original_bool_value->Equals(copy_bool_value.get()));
645 EXPECT_TRUE(original_int_value->Equals(copy_int_value.get()));
646 EXPECT_TRUE(original_double_value->Equals(copy_double_value.get()));
647 EXPECT_TRUE(original_string_value->Equals(copy_string_value.get()));
648 EXPECT_TRUE(original_string16_value->Equals(copy_string16_value.get()));
649 EXPECT_TRUE(original_binary_value->Equals(copy_binary_value.get()));
650 EXPECT_TRUE(original_list_value->Equals(copy_list_value.get()));
651 } 667 }
652 668
653 TEST(ValuesTest, RemoveEmptyChildren) { 669 TEST(ValuesTest, RemoveEmptyChildren) {
654 scoped_ptr<DictionaryValue> root(new DictionaryValue); 670 scoped_ptr<DictionaryValue> root(new DictionaryValue);
655 // Remove empty lists and dictionaries. 671 // Remove empty lists and dictionaries.
656 root->Set("empty_dict", make_scoped_ptr(new DictionaryValue)); 672 root->Set("empty_dict", make_scoped_ptr(new DictionaryValue));
657 root->Set("empty_list", make_scoped_ptr(new ListValue)); 673 root->Set("empty_list", make_scoped_ptr(new ListValue));
658 root->SetWithoutPathExpansion("a.b.c.d.e", 674 root->SetWithoutPathExpansion("a.b.c.d.e",
659 make_scoped_ptr(new DictionaryValue)); 675 make_scoped_ptr(new DictionaryValue));
660 root.reset(root->DeepCopyWithoutEmptyChildren()); 676 root.reset(root->DeepCopyWithoutEmptyChildren());
661 EXPECT_TRUE(root->empty()); 677 EXPECT_TRUE(root->empty());
662 678
663 // Make sure we don't prune too much. 679 // Make sure we don't prune too much.
664 root->SetBoolean("bool", true); 680 root->SetBoolean("bool", true);
665 root->Set("empty_dict", new DictionaryValue); 681 root->Set("empty_dict", make_scoped_ptr(new DictionaryValue));
666 root->SetString("empty_string", std::string()); 682 root->SetString("empty_string", std::string());
667 root.reset(root->DeepCopyWithoutEmptyChildren()); 683 root.reset(root->DeepCopyWithoutEmptyChildren());
668 EXPECT_EQ(2U, root->size()); 684 EXPECT_EQ(2U, root->size());
669 685
670 // Should do nothing. 686 // Should do nothing.
671 root.reset(root->DeepCopyWithoutEmptyChildren()); 687 root.reset(root->DeepCopyWithoutEmptyChildren());
672 EXPECT_EQ(2U, root->size()); 688 EXPECT_EQ(2U, root->size());
673 689
674 // Nested test cases. These should all reduce back to the bool and string 690 // Nested test cases. These should all reduce back to the bool and string
675 // set above. 691 // set above.
676 { 692 {
677 root->Set("a.b.c.d.e", new DictionaryValue); 693 root->Set("a.b.c.d.e", make_scoped_ptr(new DictionaryValue));
678 root.reset(root->DeepCopyWithoutEmptyChildren()); 694 root.reset(root->DeepCopyWithoutEmptyChildren());
679 EXPECT_EQ(2U, root->size()); 695 EXPECT_EQ(2U, root->size());
680 } 696 }
681 { 697 {
682 scoped_ptr<DictionaryValue> inner(new DictionaryValue); 698 scoped_ptr<DictionaryValue> inner(new DictionaryValue);
683 inner->Set("empty_dict", new DictionaryValue); 699 inner->Set("empty_dict", make_scoped_ptr(new DictionaryValue));
684 inner->Set("empty_list", new ListValue); 700 inner->Set("empty_list", make_scoped_ptr(new ListValue));
685 root->Set("dict_with_empty_children", inner.Pass()); 701 root->Set("dict_with_empty_children", inner.Pass());
686 root.reset(root->DeepCopyWithoutEmptyChildren()); 702 root.reset(root->DeepCopyWithoutEmptyChildren());
687 EXPECT_EQ(2U, root->size()); 703 EXPECT_EQ(2U, root->size());
688 } 704 }
689 { 705 {
690 scoped_ptr<ListValue> inner(new ListValue); 706 scoped_ptr<ListValue> inner(new ListValue);
691 inner->Append(make_scoped_ptr(new DictionaryValue)); 707 inner->Append(make_scoped_ptr(new DictionaryValue));
692 inner->Append(make_scoped_ptr(new ListValue)); 708 inner->Append(make_scoped_ptr(new ListValue));
693 root->Set("list_with_empty_children", inner.Pass()); 709 root->Set("list_with_empty_children", inner.Pass());
694 root.reset(root->DeepCopyWithoutEmptyChildren()); 710 root.reset(root->DeepCopyWithoutEmptyChildren());
(...skipping 11 matching lines...) Expand all
706 inner2->Set("empty_list", make_scoped_ptr(new ListValue)); 722 inner2->Set("empty_list", make_scoped_ptr(new ListValue));
707 root->Set("dict_with_empty_children", inner2.Pass()); 723 root->Set("dict_with_empty_children", inner2.Pass());
708 root.reset(root->DeepCopyWithoutEmptyChildren()); 724 root.reset(root->DeepCopyWithoutEmptyChildren());
709 EXPECT_EQ(2U, root->size()); 725 EXPECT_EQ(2U, root->size());
710 } 726 }
711 727
712 // Make sure nested values don't get pruned. 728 // Make sure nested values don't get pruned.
713 { 729 {
714 scoped_ptr<ListValue> inner(new ListValue); 730 scoped_ptr<ListValue> inner(new ListValue);
715 scoped_ptr<ListValue> inner2(new ListValue); 731 scoped_ptr<ListValue> inner2(new ListValue);
716 inner2->Append(new StringValue("hello")); 732 inner2->Append(make_scoped_ptr(new StringValue("hello")));
717 inner->Append(make_scoped_ptr(new DictionaryValue)); 733 inner->Append(make_scoped_ptr(new DictionaryValue));
718 inner->Append(inner2.Pass()); 734 inner->Append(inner2.Pass());
719 root->Set("list_with_empty_children", inner.Pass()); 735 root->Set("list_with_empty_children", inner.Pass());
720 root.reset(root->DeepCopyWithoutEmptyChildren()); 736 root.reset(root->DeepCopyWithoutEmptyChildren());
721 EXPECT_EQ(3U, root->size()); 737 EXPECT_EQ(3U, root->size());
722 738
723 ListValue* inner_value, *inner_value2; 739 ListValue* inner_value, *inner_value2;
724 EXPECT_TRUE(root->GetList("list_with_empty_children", &inner_value)); 740 EXPECT_TRUE(root->GetList("list_with_empty_children", &inner_value));
725 EXPECT_EQ(1U, inner_value->GetSize()); // Dictionary was pruned. 741 EXPECT_EQ(1U, inner_value->GetSize()); // Dictionary was pruned.
726 EXPECT_TRUE(inner_value->GetList(0, &inner_value2)); 742 EXPECT_TRUE(inner_value->GetList(0, &inner_value2));
727 EXPECT_EQ(1U, inner_value2->GetSize()); 743 EXPECT_EQ(1U, inner_value2->GetSize());
728 } 744 }
729 } 745 }
730 746
731 TEST(ValuesTest, MergeDictionary) { 747 TEST(ValuesTest, MergeDictionary) {
732 scoped_ptr<DictionaryValue> base(new DictionaryValue); 748 scoped_ptr<DictionaryValue> base(new DictionaryValue);
733 base->SetString("base_key", "base_key_value_base"); 749 base->SetString("base_key", "base_key_value_base");
734 base->SetString("collide_key", "collide_key_value_base"); 750 base->SetString("collide_key", "collide_key_value_base");
735 DictionaryValue* base_sub_dict = new DictionaryValue; 751 scoped_ptr<DictionaryValue> base_sub_dict(new DictionaryValue);
736 base_sub_dict->SetString("sub_base_key", "sub_base_key_value_base"); 752 base_sub_dict->SetString("sub_base_key", "sub_base_key_value_base");
737 base_sub_dict->SetString("sub_collide_key", "sub_collide_key_value_base"); 753 base_sub_dict->SetString("sub_collide_key", "sub_collide_key_value_base");
738 base->Set("sub_dict_key", base_sub_dict); 754 base->Set("sub_dict_key", base_sub_dict.Pass());
739 755
740 scoped_ptr<DictionaryValue> merge(new DictionaryValue); 756 scoped_ptr<DictionaryValue> merge(new DictionaryValue);
741 merge->SetString("merge_key", "merge_key_value_merge"); 757 merge->SetString("merge_key", "merge_key_value_merge");
742 merge->SetString("collide_key", "collide_key_value_merge"); 758 merge->SetString("collide_key", "collide_key_value_merge");
743 DictionaryValue* merge_sub_dict = new DictionaryValue; 759 scoped_ptr<DictionaryValue> merge_sub_dict(new DictionaryValue);
744 merge_sub_dict->SetString("sub_merge_key", "sub_merge_key_value_merge"); 760 merge_sub_dict->SetString("sub_merge_key", "sub_merge_key_value_merge");
745 merge_sub_dict->SetString("sub_collide_key", "sub_collide_key_value_merge"); 761 merge_sub_dict->SetString("sub_collide_key", "sub_collide_key_value_merge");
746 merge->Set("sub_dict_key", merge_sub_dict); 762 merge->Set("sub_dict_key", merge_sub_dict.Pass());
747 763
748 base->MergeDictionary(merge.get()); 764 base->MergeDictionary(merge.get());
749 765
750 EXPECT_EQ(4U, base->size()); 766 EXPECT_EQ(4U, base->size());
751 std::string base_key_value; 767 std::string base_key_value;
752 EXPECT_TRUE(base->GetString("base_key", &base_key_value)); 768 EXPECT_TRUE(base->GetString("base_key", &base_key_value));
753 EXPECT_EQ("base_key_value_base", base_key_value); // Base value preserved. 769 EXPECT_EQ("base_key_value_base", base_key_value); // Base value preserved.
754 std::string collide_key_value; 770 std::string collide_key_value;
755 EXPECT_TRUE(base->GetString("collide_key", &collide_key_value)); 771 EXPECT_TRUE(base->GetString("collide_key", &collide_key_value));
756 EXPECT_EQ("collide_key_value_merge", collide_key_value); // Replaced. 772 EXPECT_EQ("collide_key_value_merge", collide_key_value); // Replaced.
(...skipping 10 matching lines...) Expand all
767 std::string sub_collide_key_value; 783 std::string sub_collide_key_value;
768 EXPECT_TRUE(res_sub_dict->GetString("sub_collide_key", 784 EXPECT_TRUE(res_sub_dict->GetString("sub_collide_key",
769 &sub_collide_key_value)); 785 &sub_collide_key_value));
770 EXPECT_EQ("sub_collide_key_value_merge", sub_collide_key_value); // Replaced. 786 EXPECT_EQ("sub_collide_key_value_merge", sub_collide_key_value); // Replaced.
771 std::string sub_merge_key_value; 787 std::string sub_merge_key_value;
772 EXPECT_TRUE(res_sub_dict->GetString("sub_merge_key", &sub_merge_key_value)); 788 EXPECT_TRUE(res_sub_dict->GetString("sub_merge_key", &sub_merge_key_value));
773 EXPECT_EQ("sub_merge_key_value_merge", sub_merge_key_value); // Merged in. 789 EXPECT_EQ("sub_merge_key_value_merge", sub_merge_key_value); // Merged in.
774 } 790 }
775 791
776 TEST(ValuesTest, MergeDictionaryDeepCopy) { 792 TEST(ValuesTest, MergeDictionaryDeepCopy) {
777 DictionaryValue* child = new DictionaryValue; 793 scoped_ptr<DictionaryValue> child(new DictionaryValue);
794 DictionaryValue* original_child = child.get();
778 child->SetString("test", "value"); 795 child->SetString("test", "value");
779 EXPECT_EQ(1U, child->size()); 796 EXPECT_EQ(1U, child->size());
780 797
781 std::string value; 798 std::string value;
782 EXPECT_TRUE(child->GetString("test", &value)); 799 EXPECT_TRUE(child->GetString("test", &value));
783 EXPECT_EQ("value", value); 800 EXPECT_EQ("value", value);
784 801
785 scoped_ptr<DictionaryValue> base(new DictionaryValue); 802 scoped_ptr<DictionaryValue> base(new DictionaryValue);
786 base->Set("dict", child); 803 base->Set("dict", child.Pass());
787 EXPECT_EQ(1U, base->size()); 804 EXPECT_EQ(1U, base->size());
788 805
789 DictionaryValue* ptr; 806 DictionaryValue* ptr;
790 EXPECT_TRUE(base->GetDictionary("dict", &ptr)); 807 EXPECT_TRUE(base->GetDictionary("dict", &ptr));
791 EXPECT_EQ(child, ptr); 808 EXPECT_EQ(original_child, ptr);
792 809
793 scoped_ptr<DictionaryValue> merged(new DictionaryValue); 810 scoped_ptr<DictionaryValue> merged(new DictionaryValue);
794 merged->MergeDictionary(base.get()); 811 merged->MergeDictionary(base.get());
795 EXPECT_EQ(1U, merged->size()); 812 EXPECT_EQ(1U, merged->size());
796 EXPECT_TRUE(merged->GetDictionary("dict", &ptr)); 813 EXPECT_TRUE(merged->GetDictionary("dict", &ptr));
797 EXPECT_NE(child, ptr); 814 EXPECT_NE(original_child, ptr);
798 EXPECT_TRUE(ptr->GetString("test", &value)); 815 EXPECT_TRUE(ptr->GetString("test", &value));
799 EXPECT_EQ("value", value); 816 EXPECT_EQ("value", value);
800 817
801 child->SetString("test", "overwrite"); 818 original_child->SetString("test", "overwrite");
802 base.reset(); 819 base.reset();
803 EXPECT_TRUE(ptr->GetString("test", &value)); 820 EXPECT_TRUE(ptr->GetString("test", &value));
804 EXPECT_EQ("value", value); 821 EXPECT_EQ("value", value);
805 } 822 }
806 823
807 TEST(ValuesTest, DictionaryIterator) { 824 TEST(ValuesTest, DictionaryIterator) {
808 DictionaryValue dict; 825 DictionaryValue dict;
809 for (DictionaryValue::Iterator it(dict); !it.IsAtEnd(); it.Advance()) { 826 for (DictionaryValue::Iterator it(dict); !it.IsAtEnd(); it.Advance()) {
810 ADD_FAILURE(); 827 ADD_FAILURE();
811 } 828 }
(...skipping 308 matching lines...) Expand 10 before | Expand all | Expand 10 after
1120 EXPECT_FALSE(main_list.GetList(1, NULL)); 1137 EXPECT_FALSE(main_list.GetList(1, NULL));
1121 EXPECT_FALSE(main_list.GetList(2, NULL)); 1138 EXPECT_FALSE(main_list.GetList(2, NULL));
1122 EXPECT_FALSE(main_list.GetList(3, NULL)); 1139 EXPECT_FALSE(main_list.GetList(3, NULL));
1123 EXPECT_FALSE(main_list.GetList(4, NULL)); 1140 EXPECT_FALSE(main_list.GetList(4, NULL));
1124 EXPECT_FALSE(main_list.GetList(5, NULL)); 1141 EXPECT_FALSE(main_list.GetList(5, NULL));
1125 EXPECT_TRUE(main_list.GetList(6, NULL)); 1142 EXPECT_TRUE(main_list.GetList(6, NULL));
1126 EXPECT_FALSE(main_list.GetList(7, NULL)); 1143 EXPECT_FALSE(main_list.GetList(7, NULL));
1127 } 1144 }
1128 1145
1129 } // namespace base 1146 } // namespace base
OLDNEW
« no previous file with comments | « base/json/json_writer_unittest.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698