| OLD | NEW |
| 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/string16.h" | 8 #include "base/string16.h" |
| 9 #include "base/utf_string_conversions.h" | 9 #include "base/utf_string_conversions.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 protected: | |
| 15 void CompareDictionariesAndCheckResult( | |
| 16 const DictionaryValue* dict1, | |
| 17 const DictionaryValue* dict2, | |
| 18 const char* expected_paths[], | |
| 19 size_t expected_paths_count) { | |
| 20 std::vector<std::string> differing_paths; | |
| 21 std::vector<std::string> expected_paths_vector(expected_paths, | |
| 22 expected_paths+expected_paths_count); | |
| 23 // All comparisons should be commutative, check dict1 against dict2 | |
| 24 // and vice-versa. | |
| 25 dict1->GetDifferingPaths(dict2, &differing_paths); | |
| 26 ASSERT_EQ(expected_paths_count, differing_paths.size()); | |
| 27 EXPECT_TRUE(equal(differing_paths.begin(), differing_paths.end(), | |
| 28 expected_paths_vector.begin())); | |
| 29 dict2->GetDifferingPaths(dict1, &differing_paths); | |
| 30 ASSERT_EQ(expected_paths_count, differing_paths.size()); | |
| 31 EXPECT_TRUE(equal(differing_paths.begin(), differing_paths.end(), | |
| 32 expected_paths_vector.begin())); | |
| 33 } | |
| 34 }; | 14 }; |
| 35 | 15 |
| 36 TEST_F(ValuesTest, Basic) { | 16 TEST_F(ValuesTest, Basic) { |
| 37 // Test basic dictionary getting/setting | 17 // Test basic dictionary getting/setting |
| 38 DictionaryValue settings; | 18 DictionaryValue settings; |
| 39 std::string homepage = "http://google.com"; | 19 std::string homepage = "http://google.com"; |
| 40 ASSERT_FALSE(settings.GetString("global.homepage", &homepage)); | 20 ASSERT_FALSE(settings.GetString("global.homepage", &homepage)); |
| 41 ASSERT_EQ(std::string("http://google.com"), homepage); | 21 ASSERT_EQ(std::string("http://google.com"), homepage); |
| 42 | 22 |
| 43 ASSERT_FALSE(settings.Get("global", NULL)); | 23 ASSERT_FALSE(settings.Get("global", NULL)); |
| (...skipping 599 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 643 EXPECT_TRUE(res_sub_dict->GetString("sub_base_key", &sub_base_key_value)); | 623 EXPECT_TRUE(res_sub_dict->GetString("sub_base_key", &sub_base_key_value)); |
| 644 EXPECT_EQ("sub_base_key_value_base", sub_base_key_value); // Preserved. | 624 EXPECT_EQ("sub_base_key_value_base", sub_base_key_value); // Preserved. |
| 645 std::string sub_collide_key_value; | 625 std::string sub_collide_key_value; |
| 646 EXPECT_TRUE(res_sub_dict->GetString("sub_collide_key", | 626 EXPECT_TRUE(res_sub_dict->GetString("sub_collide_key", |
| 647 &sub_collide_key_value)); | 627 &sub_collide_key_value)); |
| 648 EXPECT_EQ("sub_collide_key_value_merge", sub_collide_key_value); // Replaced. | 628 EXPECT_EQ("sub_collide_key_value_merge", sub_collide_key_value); // Replaced. |
| 649 std::string sub_merge_key_value; | 629 std::string sub_merge_key_value; |
| 650 EXPECT_TRUE(res_sub_dict->GetString("sub_merge_key", &sub_merge_key_value)); | 630 EXPECT_TRUE(res_sub_dict->GetString("sub_merge_key", &sub_merge_key_value)); |
| 651 EXPECT_EQ("sub_merge_key_value_merge", sub_merge_key_value); // Merged in. | 631 EXPECT_EQ("sub_merge_key_value_merge", sub_merge_key_value); // Merged in. |
| 652 } | 632 } |
| 653 | |
| 654 TEST_F(ValuesTest, GetDifferingPaths) { | |
| 655 scoped_ptr<DictionaryValue> dict1(new DictionaryValue()); | |
| 656 scoped_ptr<DictionaryValue> dict2(new DictionaryValue()); | |
| 657 std::vector<std::string> differing_paths; | |
| 658 | |
| 659 // Test comparing empty dictionaries. | |
| 660 dict1->GetDifferingPaths(dict2.get(), &differing_paths); | |
| 661 EXPECT_EQ(differing_paths.size(), 0UL); | |
| 662 | |
| 663 // Compare an empty dictionary with various non-empty dictionaries. | |
| 664 static const char* expected_paths1[] = { | |
| 665 "segment1" | |
| 666 }; | |
| 667 dict1->SetString("segment1", "value1"); | |
| 668 CompareDictionariesAndCheckResult(dict1.get(), dict2.get(), expected_paths1, | |
| 669 arraysize(expected_paths1)); | |
| 670 | |
| 671 static const char* expected_paths2[] = { | |
| 672 "segment1", | |
| 673 "segment2", | |
| 674 "segment2.segment3" | |
| 675 }; | |
| 676 dict1->SetString("segment2.segment3", "value2"); | |
| 677 CompareDictionariesAndCheckResult(dict1.get(), dict2.get(), expected_paths2, | |
| 678 arraysize(expected_paths2)); | |
| 679 | |
| 680 static const char* expected_paths3[] = { | |
| 681 "segment1", | |
| 682 "segment2", | |
| 683 "segment2.segment3", | |
| 684 "segment4", | |
| 685 "segment4.segment5" | |
| 686 }; | |
| 687 dict1->SetString("segment4.segment5", "value3"); | |
| 688 CompareDictionariesAndCheckResult(dict1.get(), dict2.get(), expected_paths3, | |
| 689 arraysize(expected_paths3)); | |
| 690 | |
| 691 // Now various tests with two populated dictionaries. | |
| 692 static const char* expected_paths4[] = { | |
| 693 "segment1", | |
| 694 "segment2", | |
| 695 "segment2.segment3", | |
| 696 "segment4", | |
| 697 "segment4.segment5" | |
| 698 }; | |
| 699 dict2->Set("segment2", new DictionaryValue()); | |
| 700 CompareDictionariesAndCheckResult(dict1.get(), dict2.get(), expected_paths4, | |
| 701 arraysize(expected_paths4)); | |
| 702 | |
| 703 static const char* expected_paths5[] = { | |
| 704 "segment1", | |
| 705 "segment4", | |
| 706 "segment4.segment5" | |
| 707 }; | |
| 708 dict2->SetString("segment2.segment3", "value2"); | |
| 709 CompareDictionariesAndCheckResult(dict1.get(), dict2.get(), expected_paths5, | |
| 710 arraysize(expected_paths5)); | |
| 711 | |
| 712 dict2->SetBoolean("segment2.segment3", true); | |
| 713 CompareDictionariesAndCheckResult(dict1.get(), dict2.get(), expected_paths4, | |
| 714 arraysize(expected_paths4)); | |
| 715 | |
| 716 // Test two identical dictionaries. | |
| 717 dict2.reset(static_cast<DictionaryValue*>(dict1->DeepCopy())); | |
| 718 dict2->GetDifferingPaths(dict1.get(), &differing_paths); | |
| 719 EXPECT_EQ(differing_paths.size(), 0UL); | |
| 720 | |
| 721 // Test a deep dictionary structure. | |
| 722 static const char* expected_paths6[] = { | |
| 723 "s1", | |
| 724 "s1.s2", | |
| 725 "s1.s2.s3", | |
| 726 "s1.s2.s3.s4", | |
| 727 "s1.s2.s3.s4.s5" | |
| 728 }; | |
| 729 dict1.reset(new DictionaryValue()); | |
| 730 dict2.reset(new DictionaryValue()); | |
| 731 dict1->Set("s1.s2.s3.s4.s5", new DictionaryValue()); | |
| 732 CompareDictionariesAndCheckResult(dict1.get(), dict2.get(), expected_paths6, | |
| 733 arraysize(expected_paths6)); | |
| 734 | |
| 735 // Make sure disjoint dictionaries generate the right differing path list. | |
| 736 static const char* expected_paths7[] = { | |
| 737 "a", | |
| 738 "b", | |
| 739 "c", | |
| 740 "d" | |
| 741 }; | |
| 742 dict1.reset(new DictionaryValue()); | |
| 743 dict1->SetBoolean("a", true); | |
| 744 dict1->SetBoolean("c", true); | |
| 745 dict2.reset(new DictionaryValue()); | |
| 746 dict1->SetBoolean("b", true); | |
| 747 dict1->SetBoolean("d", true); | |
| 748 CompareDictionariesAndCheckResult(dict1.get(), dict2.get(), expected_paths7, | |
| 749 arraysize(expected_paths7)); | |
| 750 | |
| 751 // For code coverage completeness. Make sure that all branches | |
| 752 // that were not covered are executed. | |
| 753 static const char* expected_paths8[] = { | |
| 754 "s1", | |
| 755 "s1.s2" | |
| 756 }; | |
| 757 dict1.reset(new DictionaryValue()); | |
| 758 dict1->Set("s1.s2", new DictionaryValue()); | |
| 759 dict2.reset(new DictionaryValue()); | |
| 760 dict2->SetInteger("s1", 1); | |
| 761 CompareDictionariesAndCheckResult(dict1.get(), dict2.get(), expected_paths8, | |
| 762 arraysize(expected_paths8)); | |
| 763 } | |
| OLD | NEW |