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

Side by Side Diff: base/values_unittest.cc

Issue 3041038: Make ValuesTest use std::strings (instead of wstrings) for dictionary keys. (Closed)
Patch Set: comment fix Created 10 years, 4 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.h ('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) 2010 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2010 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/scoped_ptr.h" 7 #include "base/scoped_ptr.h"
8 #include "base/string_util.h" 8 #include "base/string_util.h"
9 #include "base/string16.h" 9 #include "base/string16.h"
10 #include "base/values.h" 10 #include "base/values.h"
11 #include "testing/gtest/include/gtest/gtest.h" 11 #include "testing/gtest/include/gtest/gtest.h"
12 12
13 class ValuesTest: public testing::Test { 13 class ValuesTest: public testing::Test {
14 }; 14 };
15 15
16 // TODO(viettrungluu): I changed the keys for DictionaryValue from std::wstring
17 // to std::string. I've temporarily kept the old methods taking std::wstring for
18 // compatibility. The ...Deprecated tests are the old tests which use these
19 // methods, and remain to test compatibility. They will be removed once the old
20 // methods are removed.
21
16 TEST(ValuesTest, Basic) { 22 TEST(ValuesTest, Basic) {
17 // Test basic dictionary getting/setting 23 // Test basic dictionary getting/setting
18 DictionaryValue settings; 24 DictionaryValue settings;
25 std::string homepage = "http://google.com";
26 ASSERT_FALSE(settings.GetString("global.homepage", &homepage));
27 ASSERT_EQ(std::string("http://google.com"), homepage);
28
29 ASSERT_FALSE(settings.Get("global", NULL));
30 settings.Set("global", Value::CreateBooleanValue(true));
31 ASSERT_TRUE(settings.Get("global", NULL));
32 settings.SetString("global.homepage", "http://scurvy.com");
33 ASSERT_TRUE(settings.Get("global", NULL));
34 homepage = "http://google.com";
35 ASSERT_TRUE(settings.GetString("global.homepage", &homepage));
36 ASSERT_EQ(std::string("http://scurvy.com"), homepage);
37
38 // Test storing a dictionary in a list.
39 ListValue* toolbar_bookmarks;
40 ASSERT_FALSE(
41 settings.GetList("global.toolbar.bookmarks", &toolbar_bookmarks));
42
43 toolbar_bookmarks = new ListValue;
44 settings.Set("global.toolbar.bookmarks", toolbar_bookmarks);
45 ASSERT_TRUE(settings.GetList("global.toolbar.bookmarks", &toolbar_bookmarks));
46
47 DictionaryValue* new_bookmark = new DictionaryValue;
48 new_bookmark->SetString("name", "Froogle");
49 new_bookmark->SetString("url", "http://froogle.com");
50 toolbar_bookmarks->Append(new_bookmark);
51
52 ListValue* bookmark_list;
53 ASSERT_TRUE(settings.GetList("global.toolbar.bookmarks", &bookmark_list));
54 DictionaryValue* bookmark;
55 ASSERT_EQ(1U, bookmark_list->GetSize());
56 ASSERT_TRUE(bookmark_list->GetDictionary(0, &bookmark));
57 std::string bookmark_name = "Unnamed";
58 ASSERT_TRUE(bookmark->GetString("name", &bookmark_name));
59 ASSERT_EQ(std::string("Froogle"), bookmark_name);
60 std::string bookmark_url;
61 ASSERT_TRUE(bookmark->GetString("url", &bookmark_url));
62 ASSERT_EQ(std::string("http://froogle.com"), bookmark_url);
63 }
64
65 // TODO(viettrungluu): deprecate:
66 TEST(ValuesTest, BasicDeprecated) {
67 // Test basic dictionary getting/setting
68 DictionaryValue settings;
19 std::wstring homepage = L"http://google.com"; 69 std::wstring homepage = L"http://google.com";
20 ASSERT_FALSE( 70 ASSERT_FALSE(
21 settings.GetString(L"global.homepage", &homepage)); 71 settings.GetString(L"global.homepage", &homepage));
22 ASSERT_EQ(std::wstring(L"http://google.com"), homepage); 72 ASSERT_EQ(std::wstring(L"http://google.com"), homepage);
23 73
24 ASSERT_FALSE(settings.Get(L"global", NULL)); 74 ASSERT_FALSE(settings.Get(L"global", NULL));
25 settings.Set(L"global", Value::CreateBooleanValue(true)); 75 settings.Set(L"global", Value::CreateBooleanValue(true));
26 ASSERT_TRUE(settings.Get(L"global", NULL)); 76 ASSERT_TRUE(settings.Get(L"global", NULL));
27 settings.SetString(L"global.homepage", L"http://scurvy.com"); 77 settings.SetString(L"global.homepage", L"http://scurvy.com");
28 ASSERT_TRUE(settings.Get(L"global", NULL)); 78 ASSERT_TRUE(settings.Get(L"global", NULL));
(...skipping 220 matching lines...) Expand 10 before | Expand all | Expand 10 after
249 DeletionTestValue* value = new DeletionTestValue(&deletion_flag); 299 DeletionTestValue* value = new DeletionTestValue(&deletion_flag);
250 list.Append(value); 300 list.Append(value);
251 EXPECT_FALSE(deletion_flag); 301 EXPECT_FALSE(deletion_flag);
252 EXPECT_EQ(0, list.Remove(*value)); 302 EXPECT_EQ(0, list.Remove(*value));
253 EXPECT_TRUE(deletion_flag); 303 EXPECT_TRUE(deletion_flag);
254 EXPECT_EQ(0U, list.GetSize()); 304 EXPECT_EQ(0U, list.GetSize());
255 } 305 }
256 } 306 }
257 307
258 TEST(ValuesTest, DictionaryDeletion) { 308 TEST(ValuesTest, DictionaryDeletion) {
309 std::string key = "test";
310 bool deletion_flag = true;
311
312 {
313 DictionaryValue dict;
314 dict.Set(key, new DeletionTestValue(&deletion_flag));
315 EXPECT_FALSE(deletion_flag);
316 }
317 EXPECT_TRUE(deletion_flag);
318
319 {
320 DictionaryValue dict;
321 dict.Set(key, new DeletionTestValue(&deletion_flag));
322 EXPECT_FALSE(deletion_flag);
323 dict.Clear();
324 EXPECT_TRUE(deletion_flag);
325 }
326
327 {
328 DictionaryValue dict;
329 dict.Set(key, new DeletionTestValue(&deletion_flag));
330 EXPECT_FALSE(deletion_flag);
331 dict.Set(key, Value::CreateNullValue());
332 EXPECT_TRUE(deletion_flag);
333 }
334 }
335
336 // TODO(viettrungluu): deprecate:
337 TEST(ValuesTest, DictionaryDeletionDeprecated) {
259 std::wstring key = L"test"; 338 std::wstring key = L"test";
260 bool deletion_flag = true; 339 bool deletion_flag = true;
261 340
262 { 341 {
263 DictionaryValue dict; 342 DictionaryValue dict;
264 dict.Set(key, new DeletionTestValue(&deletion_flag)); 343 dict.Set(key, new DeletionTestValue(&deletion_flag));
265 EXPECT_FALSE(deletion_flag); 344 EXPECT_FALSE(deletion_flag);
266 } 345 }
267 EXPECT_TRUE(deletion_flag); 346 EXPECT_TRUE(deletion_flag);
268 347
269 { 348 {
270 DictionaryValue dict; 349 DictionaryValue dict;
271 dict.Set(key, new DeletionTestValue(&deletion_flag)); 350 dict.Set(key, new DeletionTestValue(&deletion_flag));
272 EXPECT_FALSE(deletion_flag); 351 EXPECT_FALSE(deletion_flag);
273 dict.Clear(); 352 dict.Clear();
274 EXPECT_TRUE(deletion_flag); 353 EXPECT_TRUE(deletion_flag);
275 } 354 }
276 355
277 { 356 {
278 DictionaryValue dict; 357 DictionaryValue dict;
279 dict.Set(key, new DeletionTestValue(&deletion_flag)); 358 dict.Set(key, new DeletionTestValue(&deletion_flag));
280 EXPECT_FALSE(deletion_flag); 359 EXPECT_FALSE(deletion_flag);
281 dict.Set(key, Value::CreateNullValue()); 360 dict.Set(key, Value::CreateNullValue());
282 EXPECT_TRUE(deletion_flag); 361 EXPECT_TRUE(deletion_flag);
283 } 362 }
284 } 363 }
285 364
286 TEST(ValuesTest, DictionaryRemoval) { 365 TEST(ValuesTest, DictionaryRemoval) {
366 std::string key = "test";
367 bool deletion_flag = true;
368 Value* removed_item = NULL;
369
370 {
371 DictionaryValue dict;
372 dict.Set(key, new DeletionTestValue(&deletion_flag));
373 EXPECT_FALSE(deletion_flag);
374 EXPECT_TRUE(dict.HasKey(key));
375 EXPECT_FALSE(dict.Remove("absent key", &removed_item));
376 EXPECT_TRUE(dict.Remove(key, &removed_item));
377 EXPECT_FALSE(dict.HasKey(key));
378 ASSERT_TRUE(removed_item);
379 }
380 EXPECT_FALSE(deletion_flag);
381 delete removed_item;
382 removed_item = NULL;
383 EXPECT_TRUE(deletion_flag);
384
385 {
386 DictionaryValue dict;
387 dict.Set(key, new DeletionTestValue(&deletion_flag));
388 EXPECT_FALSE(deletion_flag);
389 EXPECT_TRUE(dict.HasKey(key));
390 EXPECT_TRUE(dict.Remove(key, NULL));
391 EXPECT_TRUE(deletion_flag);
392 EXPECT_FALSE(dict.HasKey(key));
393 }
394 }
395
396 // TODO(viettrungluu): deprecate:
397 TEST(ValuesTest, DictionaryRemovalDeprecated) {
287 std::wstring key = L"test"; 398 std::wstring key = L"test";
288 bool deletion_flag = true; 399 bool deletion_flag = true;
289 Value* removed_item = NULL; 400 Value* removed_item = NULL;
290 401
291 { 402 {
292 DictionaryValue dict; 403 DictionaryValue dict;
293 dict.Set(key, new DeletionTestValue(&deletion_flag)); 404 dict.Set(key, new DeletionTestValue(&deletion_flag));
294 EXPECT_FALSE(deletion_flag); 405 EXPECT_FALSE(deletion_flag);
295 EXPECT_TRUE(dict.HasKey(key)); 406 EXPECT_TRUE(dict.HasKey(key));
296 EXPECT_FALSE(dict.Remove(L"absent key", &removed_item)); 407 EXPECT_FALSE(dict.Remove(L"absent key", &removed_item));
(...skipping 12 matching lines...) Expand all
309 EXPECT_FALSE(deletion_flag); 420 EXPECT_FALSE(deletion_flag);
310 EXPECT_TRUE(dict.HasKey(key)); 421 EXPECT_TRUE(dict.HasKey(key));
311 EXPECT_TRUE(dict.Remove(key, NULL)); 422 EXPECT_TRUE(dict.Remove(key, NULL));
312 EXPECT_TRUE(deletion_flag); 423 EXPECT_TRUE(deletion_flag);
313 EXPECT_FALSE(dict.HasKey(key)); 424 EXPECT_FALSE(dict.HasKey(key));
314 } 425 }
315 } 426 }
316 427
317 TEST(ValuesTest, DictionaryWithoutPathExpansion) { 428 TEST(ValuesTest, DictionaryWithoutPathExpansion) {
318 DictionaryValue dict; 429 DictionaryValue dict;
430 dict.Set("this.is.expanded", Value::CreateNullValue());
431 dict.SetWithoutPathExpansion("this.isnt.expanded", Value::CreateNullValue());
432
433 EXPECT_FALSE(dict.HasKey("this.is.expanded"));
434 EXPECT_TRUE(dict.HasKey("this"));
435 Value* value1;
436 EXPECT_TRUE(dict.Get("this", &value1));
437 DictionaryValue* value2;
438 ASSERT_TRUE(dict.GetDictionaryWithoutPathExpansion("this", &value2));
439 EXPECT_EQ(value1, value2);
440 EXPECT_EQ(1U, value2->size());
441
442 EXPECT_TRUE(dict.HasKey("this.isnt.expanded"));
443 Value* value3;
444 EXPECT_FALSE(dict.Get("this.isnt.expanded", &value3));
445 Value* value4;
446 ASSERT_TRUE(dict.GetWithoutPathExpansion("this.isnt.expanded", &value4));
447 EXPECT_EQ(Value::TYPE_NULL, value4->GetType());
448 }
449
450 // TODO(viettrungluu): deprecate:
451 TEST(ValuesTest, DictionaryWithoutPathExpansionDeprecated) {
452 DictionaryValue dict;
319 dict.Set(L"this.is.expanded", Value::CreateNullValue()); 453 dict.Set(L"this.is.expanded", Value::CreateNullValue());
320 dict.SetWithoutPathExpansion(L"this.isnt.expanded", Value::CreateNullValue()); 454 dict.SetWithoutPathExpansion(L"this.isnt.expanded", Value::CreateNullValue());
321 455
322 EXPECT_FALSE(dict.HasKey(L"this.is.expanded")); 456 EXPECT_FALSE(dict.HasKey(L"this.is.expanded"));
323 EXPECT_TRUE(dict.HasKey(L"this")); 457 EXPECT_TRUE(dict.HasKey(L"this"));
324 Value* value1; 458 Value* value1;
325 EXPECT_TRUE(dict.Get(L"this", &value1)); 459 EXPECT_TRUE(dict.Get(L"this", &value1));
326 DictionaryValue* value2; 460 DictionaryValue* value2;
327 ASSERT_TRUE(dict.GetDictionaryWithoutPathExpansion(L"this", &value2)); 461 ASSERT_TRUE(dict.GetDictionaryWithoutPathExpansion(L"this", &value2));
328 EXPECT_EQ(value1, value2); 462 EXPECT_EQ(value1, value2);
329 EXPECT_EQ(1U, value2->size()); 463 EXPECT_EQ(1U, value2->size());
330 464
331 EXPECT_TRUE(dict.HasKey(L"this.isnt.expanded")); 465 EXPECT_TRUE(dict.HasKey(L"this.isnt.expanded"));
332 Value* value3; 466 Value* value3;
333 EXPECT_FALSE(dict.Get(L"this.isnt.expanded", &value3)); 467 EXPECT_FALSE(dict.Get(L"this.isnt.expanded", &value3));
334 Value* value4; 468 Value* value4;
335 ASSERT_TRUE(dict.GetWithoutPathExpansion(L"this.isnt.expanded", &value4)); 469 ASSERT_TRUE(dict.GetWithoutPathExpansion(L"this.isnt.expanded", &value4));
336 EXPECT_EQ(Value::TYPE_NULL, value4->GetType()); 470 EXPECT_EQ(Value::TYPE_NULL, value4->GetType());
337 } 471 }
338 472
339 TEST(ValuesTest, DeepCopy) { 473 TEST(ValuesTest, DeepCopy) {
340 DictionaryValue original_dict; 474 DictionaryValue original_dict;
341 Value* original_null = Value::CreateNullValue(); 475 Value* original_null = Value::CreateNullValue();
476 original_dict.Set("null", original_null);
477 Value* original_bool = Value::CreateBooleanValue(true);
478 original_dict.Set("bool", original_bool);
479 Value* original_int = Value::CreateIntegerValue(42);
480 original_dict.Set("int", original_int);
481 Value* original_real = Value::CreateRealValue(3.14);
482 original_dict.Set("real", original_real);
483 Value* original_string = Value::CreateStringValue("hello");
484 original_dict.Set("string", original_string);
485 Value* original_wstring = Value::CreateStringValue(L"peek-a-boo");
486 original_dict.Set("wstring", original_wstring);
487 Value* original_utf16 =
488 Value::CreateStringValueFromUTF16(ASCIIToUTF16("hello16"));
489 original_dict.Set("utf16", original_utf16);
490
491 char* original_buffer = new char[42];
492 memset(original_buffer, '!', 42);
493 BinaryValue* original_binary = Value::CreateBinaryValue(original_buffer, 42);
494 original_dict.Set("binary", original_binary);
495
496 ListValue* original_list = new ListValue();
497 Value* original_list_element_0 = Value::CreateIntegerValue(0);
498 original_list->Append(original_list_element_0);
499 Value* original_list_element_1 = Value::CreateIntegerValue(1);
500 original_list->Append(original_list_element_1);
501 original_dict.Set("list", original_list);
502
503 scoped_ptr<DictionaryValue> copy_dict(
504 static_cast<DictionaryValue*>(original_dict.DeepCopy()));
505 ASSERT_TRUE(copy_dict.get());
506 ASSERT_NE(copy_dict.get(), &original_dict);
507
508 Value* copy_null = NULL;
509 ASSERT_TRUE(copy_dict->Get("null", &copy_null));
510 ASSERT_TRUE(copy_null);
511 ASSERT_NE(copy_null, original_null);
512 ASSERT_TRUE(copy_null->IsType(Value::TYPE_NULL));
513
514 Value* copy_bool = NULL;
515 ASSERT_TRUE(copy_dict->Get("bool", &copy_bool));
516 ASSERT_TRUE(copy_bool);
517 ASSERT_NE(copy_bool, original_bool);
518 ASSERT_TRUE(copy_bool->IsType(Value::TYPE_BOOLEAN));
519 bool copy_bool_value = false;
520 ASSERT_TRUE(copy_bool->GetAsBoolean(&copy_bool_value));
521 ASSERT_TRUE(copy_bool_value);
522
523 Value* copy_int = NULL;
524 ASSERT_TRUE(copy_dict->Get("int", &copy_int));
525 ASSERT_TRUE(copy_int);
526 ASSERT_NE(copy_int, original_int);
527 ASSERT_TRUE(copy_int->IsType(Value::TYPE_INTEGER));
528 int copy_int_value = 0;
529 ASSERT_TRUE(copy_int->GetAsInteger(&copy_int_value));
530 ASSERT_EQ(42, copy_int_value);
531
532 Value* copy_real = NULL;
533 ASSERT_TRUE(copy_dict->Get("real", &copy_real));
534 ASSERT_TRUE(copy_real);
535 ASSERT_NE(copy_real, original_real);
536 ASSERT_TRUE(copy_real->IsType(Value::TYPE_REAL));
537 double copy_real_value = 0;
538 ASSERT_TRUE(copy_real->GetAsReal(&copy_real_value));
539 ASSERT_EQ(3.14, copy_real_value);
540
541 Value* copy_string = NULL;
542 ASSERT_TRUE(copy_dict->Get("string", &copy_string));
543 ASSERT_TRUE(copy_string);
544 ASSERT_NE(copy_string, original_string);
545 ASSERT_TRUE(copy_string->IsType(Value::TYPE_STRING));
546 std::string copy_string_value;
547 std::wstring copy_wstring_value;
548 string16 copy_utf16_value;
549 ASSERT_TRUE(copy_string->GetAsString(&copy_string_value));
550 ASSERT_TRUE(copy_string->GetAsString(&copy_wstring_value));
551 ASSERT_TRUE(copy_string->GetAsUTF16(&copy_utf16_value));
552 ASSERT_EQ(std::string("hello"), copy_string_value);
553 ASSERT_EQ(std::wstring(L"hello"), copy_wstring_value);
554 ASSERT_EQ(ASCIIToUTF16("hello"), copy_utf16_value);
555
556 Value* copy_wstring = NULL;
557 ASSERT_TRUE(copy_dict->Get("wstring", &copy_wstring));
558 ASSERT_TRUE(copy_wstring);
559 ASSERT_NE(copy_wstring, original_wstring);
560 ASSERT_TRUE(copy_wstring->IsType(Value::TYPE_STRING));
561 ASSERT_TRUE(copy_wstring->GetAsString(&copy_string_value));
562 ASSERT_TRUE(copy_wstring->GetAsString(&copy_wstring_value));
563 ASSERT_TRUE(copy_wstring->GetAsUTF16(&copy_utf16_value));
564 ASSERT_EQ(std::string("peek-a-boo"), copy_string_value);
565 ASSERT_EQ(std::wstring(L"peek-a-boo"), copy_wstring_value);
566 ASSERT_EQ(ASCIIToUTF16("peek-a-boo"), copy_utf16_value);
567
568 Value* copy_utf16 = NULL;
569 ASSERT_TRUE(copy_dict->Get("utf16", &copy_utf16));
570 ASSERT_TRUE(copy_utf16);
571 ASSERT_NE(copy_utf16, original_utf16);
572 ASSERT_TRUE(copy_utf16->IsType(Value::TYPE_STRING));
573 ASSERT_TRUE(copy_utf16->GetAsString(&copy_string_value));
574 ASSERT_TRUE(copy_utf16->GetAsString(&copy_wstring_value));
575 ASSERT_TRUE(copy_utf16->GetAsUTF16(&copy_utf16_value));
576 ASSERT_EQ(std::string("hello16"), copy_string_value);
577 ASSERT_EQ(std::wstring(L"hello16"), copy_wstring_value);
578 ASSERT_EQ(ASCIIToUTF16("hello16"), copy_utf16_value);
579
580 Value* copy_binary = NULL;
581 ASSERT_TRUE(copy_dict->Get("binary", &copy_binary));
582 ASSERT_TRUE(copy_binary);
583 ASSERT_NE(copy_binary, original_binary);
584 ASSERT_TRUE(copy_binary->IsType(Value::TYPE_BINARY));
585 ASSERT_NE(original_binary->GetBuffer(),
586 static_cast<BinaryValue*>(copy_binary)->GetBuffer());
587 ASSERT_EQ(original_binary->GetSize(),
588 static_cast<BinaryValue*>(copy_binary)->GetSize());
589 ASSERT_EQ(0, memcmp(original_binary->GetBuffer(),
590 static_cast<BinaryValue*>(copy_binary)->GetBuffer(),
591 original_binary->GetSize()));
592
593 Value* copy_value = NULL;
594 ASSERT_TRUE(copy_dict->Get("list", &copy_value));
595 ASSERT_TRUE(copy_value);
596 ASSERT_NE(copy_value, original_list);
597 ASSERT_TRUE(copy_value->IsType(Value::TYPE_LIST));
598 ListValue* copy_list = static_cast<ListValue*>(copy_value);
599 ASSERT_EQ(2U, copy_list->GetSize());
600
601 Value* copy_list_element_0;
602 ASSERT_TRUE(copy_list->Get(0, &copy_list_element_0));
603 ASSERT_TRUE(copy_list_element_0);
604 ASSERT_NE(copy_list_element_0, original_list_element_0);
605 int copy_list_element_0_value;
606 ASSERT_TRUE(copy_list_element_0->GetAsInteger(&copy_list_element_0_value));
607 ASSERT_EQ(0, copy_list_element_0_value);
608
609 Value* copy_list_element_1;
610 ASSERT_TRUE(copy_list->Get(1, &copy_list_element_1));
611 ASSERT_TRUE(copy_list_element_1);
612 ASSERT_NE(copy_list_element_1, original_list_element_1);
613 int copy_list_element_1_value;
614 ASSERT_TRUE(copy_list_element_1->GetAsInteger(&copy_list_element_1_value));
615 ASSERT_EQ(1, copy_list_element_1_value);
616 }
617
618 // TODO(viettrungluu): deprecate:
619 TEST(ValuesTest, DeepCopyDeprecated) {
620 DictionaryValue original_dict;
621 Value* original_null = Value::CreateNullValue();
342 original_dict.Set(L"null", original_null); 622 original_dict.Set(L"null", original_null);
343 Value* original_bool = Value::CreateBooleanValue(true); 623 Value* original_bool = Value::CreateBooleanValue(true);
344 original_dict.Set(L"bool", original_bool); 624 original_dict.Set(L"bool", original_bool);
345 Value* original_int = Value::CreateIntegerValue(42); 625 Value* original_int = Value::CreateIntegerValue(42);
346 original_dict.Set(L"int", original_int); 626 original_dict.Set(L"int", original_int);
347 Value* original_real = Value::CreateRealValue(3.14); 627 Value* original_real = Value::CreateRealValue(3.14);
348 original_dict.Set(L"real", original_real); 628 original_dict.Set(L"real", original_real);
349 Value* original_string = Value::CreateStringValue("hello"); 629 Value* original_string = Value::CreateStringValue("hello");
350 original_dict.Set(L"string", original_string); 630 original_dict.Set(L"string", original_string);
351 Value* original_wstring = Value::CreateStringValue(L"peek-a-boo"); 631 Value* original_wstring = Value::CreateStringValue(L"peek-a-boo");
(...skipping 135 matching lines...) Expand 10 before | Expand all | Expand 10 after
487 EXPECT_NE(null1, null2); 767 EXPECT_NE(null1, null2);
488 EXPECT_TRUE(null1->Equals(null2)); 768 EXPECT_TRUE(null1->Equals(null2));
489 769
490 Value* boolean = Value::CreateBooleanValue(false); 770 Value* boolean = Value::CreateBooleanValue(false);
491 EXPECT_FALSE(null1->Equals(boolean)); 771 EXPECT_FALSE(null1->Equals(boolean));
492 delete null1; 772 delete null1;
493 delete null2; 773 delete null2;
494 delete boolean; 774 delete boolean;
495 775
496 DictionaryValue dv; 776 DictionaryValue dv;
777 dv.SetBoolean("a", false);
778 dv.SetInteger("b", 2);
779 dv.SetReal("c", 2.5);
780 dv.SetString("d1", "string");
781 dv.SetStringFromUTF16("d2", ASCIIToUTF16("http://google.com"));
782 dv.Set("e", Value::CreateNullValue());
783
784 DictionaryValue* copy = static_cast<DictionaryValue*>(dv.DeepCopy());
785 EXPECT_TRUE(dv.Equals(copy));
786
787 ListValue* list = new ListValue;
788 list->Append(Value::CreateNullValue());
789 list->Append(new DictionaryValue);
790 dv.Set("f", list);
791
792 EXPECT_FALSE(dv.Equals(copy));
793 copy->Set("f", list->DeepCopy());
794 EXPECT_TRUE(dv.Equals(copy));
795
796 list->Append(Value::CreateBooleanValue(true));
797 EXPECT_FALSE(dv.Equals(copy));
798 delete copy;
799 }
800
801 // TODO(viettrungluu): deprecate:
802 TEST(ValuesTest, EqualsDeprecated) {
803 Value* null1 = Value::CreateNullValue();
804 Value* null2 = Value::CreateNullValue();
805 EXPECT_NE(null1, null2);
806 EXPECT_TRUE(null1->Equals(null2));
807
808 Value* boolean = Value::CreateBooleanValue(false);
809 EXPECT_FALSE(null1->Equals(boolean));
810 delete null1;
811 delete null2;
812 delete boolean;
813
814 DictionaryValue dv;
497 dv.SetBoolean(L"a", false); 815 dv.SetBoolean(L"a", false);
498 dv.SetInteger(L"b", 2); 816 dv.SetInteger(L"b", 2);
499 dv.SetReal(L"c", 2.5); 817 dv.SetReal(L"c", 2.5);
500 dv.SetString(L"d1", "string"); 818 dv.SetString(L"d1", "string");
501 dv.SetString(L"d2", L"string"); 819 dv.SetString(L"d2", L"string");
502 dv.Set(L"e", Value::CreateNullValue()); 820 dv.Set(L"e", Value::CreateNullValue());
503 821
504 DictionaryValue* copy = static_cast<DictionaryValue*>(dv.DeepCopy()); 822 DictionaryValue* copy = static_cast<DictionaryValue*>(dv.DeepCopy());
505 EXPECT_TRUE(dv.Equals(copy)); 823 EXPECT_TRUE(dv.Equals(copy));
506 824
507 ListValue* list = new ListValue; 825 ListValue* list = new ListValue;
508 list->Append(Value::CreateNullValue()); 826 list->Append(Value::CreateNullValue());
509 list->Append(new DictionaryValue); 827 list->Append(new DictionaryValue);
510 dv.Set(L"f", list); 828 dv.Set(L"f", list);
511 829
512 EXPECT_FALSE(dv.Equals(copy)); 830 EXPECT_FALSE(dv.Equals(copy));
513 copy->Set(L"f", list->DeepCopy()); 831 copy->Set(L"f", list->DeepCopy());
514 EXPECT_TRUE(dv.Equals(copy)); 832 EXPECT_TRUE(dv.Equals(copy));
515 833
516 list->Append(Value::CreateBooleanValue(true)); 834 list->Append(Value::CreateBooleanValue(true));
517 EXPECT_FALSE(dv.Equals(copy)); 835 EXPECT_FALSE(dv.Equals(copy));
518 delete copy; 836 delete copy;
519 } 837 }
520 838
521 TEST(ValuesTest, RemoveEmptyChildren) { 839 TEST(ValuesTest, RemoveEmptyChildren) {
522 scoped_ptr<DictionaryValue> root(new DictionaryValue); 840 scoped_ptr<DictionaryValue> root(new DictionaryValue);
523 // Remove empty lists and dictionaries. 841 // Remove empty lists and dictionaries.
842 root->Set("empty_dict", new DictionaryValue);
843 root->Set("empty_list", new ListValue);
844 root->SetWithoutPathExpansion("a.b.c.d.e", new DictionaryValue);
845 root.reset(root->DeepCopyWithoutEmptyChildren());
846 EXPECT_TRUE(root->empty());
847
848 // Make sure we don't prune too much.
849 root->SetBoolean("bool", true);
850 root->Set("empty_dict", new DictionaryValue);
851 root->SetString("empty_string", "");
852 root.reset(root->DeepCopyWithoutEmptyChildren());
853 EXPECT_EQ(2U, root->size());
854
855 // Should do nothing.
856 root.reset(root->DeepCopyWithoutEmptyChildren());
857 EXPECT_EQ(2U, root->size());
858
859 // Nested test cases. These should all reduce back to the bool and string
860 // set above.
861 {
862 root->Set("a.b.c.d.e", new DictionaryValue);
863 root.reset(root->DeepCopyWithoutEmptyChildren());
864 EXPECT_EQ(2U, root->size());
865 }
866 {
867 DictionaryValue* inner = new DictionaryValue;
868 root->Set("dict_with_emtpy_children", inner);
869 inner->Set("empty_dict", new DictionaryValue);
870 inner->Set("empty_list", new ListValue);
871 root.reset(root->DeepCopyWithoutEmptyChildren());
872 EXPECT_EQ(2U, root->size());
873 }
874 {
875 ListValue* inner = new ListValue;
876 root->Set("list_with_empty_children", inner);
877 inner->Append(new DictionaryValue);
878 inner->Append(new ListValue);
879 root.reset(root->DeepCopyWithoutEmptyChildren());
880 EXPECT_EQ(2U, root->size());
881 }
882
883 // Nested with siblings.
884 {
885 ListValue* inner = new ListValue;
886 root->Set("list_with_empty_children", inner);
887 inner->Append(new DictionaryValue);
888 inner->Append(new ListValue);
889 DictionaryValue* inner2 = new DictionaryValue;
890 root->Set("dict_with_empty_children", inner2);
891 inner2->Set("empty_dict", new DictionaryValue);
892 inner2->Set("empty_list", new ListValue);
893 root.reset(root->DeepCopyWithoutEmptyChildren());
894 EXPECT_EQ(2U, root->size());
895 }
896
897 // Make sure nested values don't get pruned.
898 {
899 ListValue* inner = new ListValue;
900 root->Set("list_with_empty_children", inner);
901 ListValue* inner2 = new ListValue;
902 inner->Append(new DictionaryValue);
903 inner->Append(inner2);
904 inner2->Append(Value::CreateStringValue("hello"));
905 root.reset(root->DeepCopyWithoutEmptyChildren());
906 EXPECT_EQ(3U, root->size());
907 EXPECT_TRUE(root->GetList("list_with_empty_children", &inner));
908 EXPECT_EQ(1U, inner->GetSize()); // Dictionary was pruned.
909 EXPECT_TRUE(inner->GetList(0, &inner2));
910 EXPECT_EQ(1U, inner2->GetSize());
911 }
912 }
913
914 // TODO(viettrungluu): deprecate:
915 TEST(ValuesTest, RemoveEmptyChildrenDeprecated) {
916 scoped_ptr<DictionaryValue> root(new DictionaryValue);
917 // Remove empty lists and dictionaries.
524 root->Set(L"empty_dict", new DictionaryValue); 918 root->Set(L"empty_dict", new DictionaryValue);
525 root->Set(L"empty_list", new ListValue); 919 root->Set(L"empty_list", new ListValue);
526 root->SetWithoutPathExpansion(L"a.b.c.d.e", new DictionaryValue); 920 root->SetWithoutPathExpansion(L"a.b.c.d.e", new DictionaryValue);
527 root.reset(root->DeepCopyWithoutEmptyChildren()); 921 root.reset(root->DeepCopyWithoutEmptyChildren());
528 EXPECT_TRUE(root->empty()); 922 EXPECT_TRUE(root->empty());
529 923
530 // Make sure we don't prune too much. 924 // Make sure we don't prune too much.
531 root->SetBoolean(L"bool", true); 925 root->SetBoolean(L"bool", true);
532 root->Set(L"empty_dict", new DictionaryValue); 926 root->Set(L"empty_dict", new DictionaryValue);
533 root->SetString(L"empty_string", ""); 927 root->SetString(L"empty_string", "");
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after
588 EXPECT_EQ(3U, root->size()); 982 EXPECT_EQ(3U, root->size());
589 EXPECT_TRUE(root->GetList(L"list_with_empty_children", &inner)); 983 EXPECT_TRUE(root->GetList(L"list_with_empty_children", &inner));
590 EXPECT_EQ(1U, inner->GetSize()); // Dictionary was pruned. 984 EXPECT_EQ(1U, inner->GetSize()); // Dictionary was pruned.
591 EXPECT_TRUE(inner->GetList(0, &inner2)); 985 EXPECT_TRUE(inner->GetList(0, &inner2));
592 EXPECT_EQ(1U, inner2->GetSize()); 986 EXPECT_EQ(1U, inner2->GetSize());
593 } 987 }
594 } 988 }
595 989
596 TEST(ValuesTest, MergeDictionary) { 990 TEST(ValuesTest, MergeDictionary) {
597 scoped_ptr<DictionaryValue> base(new DictionaryValue); 991 scoped_ptr<DictionaryValue> base(new DictionaryValue);
992 base->SetString("base_key", "base_key_value_base");
993 base->SetString("collide_key", "collide_key_value_base");
994 DictionaryValue* base_sub_dict = new DictionaryValue;
995 base_sub_dict->SetString("sub_base_key", "sub_base_key_value_base");
996 base_sub_dict->SetString("sub_collide_key", "sub_collide_key_value_base");
997 base->Set("sub_dict_key", base_sub_dict);
998
999 scoped_ptr<DictionaryValue> merge(new DictionaryValue);
1000 merge->SetString("merge_key", "merge_key_value_merge");
1001 merge->SetString("collide_key", "collide_key_value_merge");
1002 DictionaryValue* merge_sub_dict = new DictionaryValue;
1003 merge_sub_dict->SetString("sub_merge_key", "sub_merge_key_value_merge");
1004 merge_sub_dict->SetString("sub_collide_key", "sub_collide_key_value_merge");
1005 merge->Set("sub_dict_key", merge_sub_dict);
1006
1007 base->MergeDictionary(merge.get());
1008
1009 EXPECT_EQ(4U, base->size());
1010 std::string base_key_value;
1011 EXPECT_TRUE(base->GetString("base_key", &base_key_value));
1012 EXPECT_EQ("base_key_value_base", base_key_value); // Base value preserved.
1013 std::string collide_key_value;
1014 EXPECT_TRUE(base->GetString("collide_key", &collide_key_value));
1015 EXPECT_EQ("collide_key_value_merge", collide_key_value); // Replaced.
1016 std::string merge_key_value;
1017 EXPECT_TRUE(base->GetString("merge_key", &merge_key_value));
1018 EXPECT_EQ("merge_key_value_merge", merge_key_value); // Merged in.
1019
1020 DictionaryValue* res_sub_dict;
1021 EXPECT_TRUE(base->GetDictionary("sub_dict_key", &res_sub_dict));
1022 EXPECT_EQ(3U, res_sub_dict->size());
1023 std::string sub_base_key_value;
1024 EXPECT_TRUE(res_sub_dict->GetString("sub_base_key", &sub_base_key_value));
1025 EXPECT_EQ("sub_base_key_value_base", sub_base_key_value); // Preserved.
1026 std::string sub_collide_key_value;
1027 EXPECT_TRUE(res_sub_dict->GetString("sub_collide_key",
1028 &sub_collide_key_value));
1029 EXPECT_EQ("sub_collide_key_value_merge", sub_collide_key_value); // Replaced.
1030 std::string sub_merge_key_value;
1031 EXPECT_TRUE(res_sub_dict->GetString("sub_merge_key", &sub_merge_key_value));
1032 EXPECT_EQ("sub_merge_key_value_merge", sub_merge_key_value); // Merged in.
1033 }
1034
1035 // TODO(viettrungluu): deprecate:
1036 TEST(ValuesTest, MergeDictionaryDeprecated) {
1037 scoped_ptr<DictionaryValue> base(new DictionaryValue);
598 base->SetString(L"base_key", "base_key_value_base"); 1038 base->SetString(L"base_key", "base_key_value_base");
599 base->SetString(L"collide_key", "collide_key_value_base"); 1039 base->SetString(L"collide_key", "collide_key_value_base");
600 DictionaryValue* base_sub_dict = new DictionaryValue; 1040 DictionaryValue* base_sub_dict = new DictionaryValue;
601 base_sub_dict->SetString(L"sub_base_key", "sub_base_key_value_base"); 1041 base_sub_dict->SetString(L"sub_base_key", "sub_base_key_value_base");
602 base_sub_dict->SetString(L"sub_collide_key", "sub_collide_key_value_base"); 1042 base_sub_dict->SetString(L"sub_collide_key", "sub_collide_key_value_base");
603 base->Set(L"sub_dict_key", base_sub_dict); 1043 base->Set(L"sub_dict_key", base_sub_dict);
604 1044
605 scoped_ptr<DictionaryValue> merge(new DictionaryValue); 1045 scoped_ptr<DictionaryValue> merge(new DictionaryValue);
606 merge->SetString(L"merge_key", "merge_key_value_merge"); 1046 merge->SetString(L"merge_key", "merge_key_value_merge");
607 merge->SetString(L"collide_key", "collide_key_value_merge"); 1047 merge->SetString(L"collide_key", "collide_key_value_merge");
(...skipping 22 matching lines...) Expand all
630 EXPECT_TRUE(res_sub_dict->GetString(L"sub_base_key", &sub_base_key_value)); 1070 EXPECT_TRUE(res_sub_dict->GetString(L"sub_base_key", &sub_base_key_value));
631 EXPECT_EQ("sub_base_key_value_base", sub_base_key_value); // Preserved. 1071 EXPECT_EQ("sub_base_key_value_base", sub_base_key_value); // Preserved.
632 std::string sub_collide_key_value; 1072 std::string sub_collide_key_value;
633 EXPECT_TRUE(res_sub_dict->GetString(L"sub_collide_key", 1073 EXPECT_TRUE(res_sub_dict->GetString(L"sub_collide_key",
634 &sub_collide_key_value)); 1074 &sub_collide_key_value));
635 EXPECT_EQ("sub_collide_key_value_merge", sub_collide_key_value); // Replaced. 1075 EXPECT_EQ("sub_collide_key_value_merge", sub_collide_key_value); // Replaced.
636 std::string sub_merge_key_value; 1076 std::string sub_merge_key_value;
637 EXPECT_TRUE(res_sub_dict->GetString(L"sub_merge_key", &sub_merge_key_value)); 1077 EXPECT_TRUE(res_sub_dict->GetString(L"sub_merge_key", &sub_merge_key_value));
638 EXPECT_EQ("sub_merge_key_value_merge", sub_merge_key_value); // Merged in. 1078 EXPECT_EQ("sub_merge_key_value_merge", sub_merge_key_value); // Merged in.
639 } 1079 }
OLDNEW
« no previous file with comments | « base/values.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698