OLD | NEW |
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 "base/values.h" | 5 #include "base/values.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 | 8 |
9 #include <limits> | 9 #include <limits> |
10 #include <memory> | 10 #include <memory> |
(...skipping 565 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
576 DictionaryValue dict; | 576 DictionaryValue dict; |
577 dict.Set(key, MakeUnique<Value>()); | 577 dict.Set(key, MakeUnique<Value>()); |
578 EXPECT_TRUE(dict.HasKey(key)); | 578 EXPECT_TRUE(dict.HasKey(key)); |
579 EXPECT_TRUE(dict.Remove(key, NULL)); | 579 EXPECT_TRUE(dict.Remove(key, NULL)); |
580 EXPECT_FALSE(dict.HasKey(key)); | 580 EXPECT_FALSE(dict.HasKey(key)); |
581 } | 581 } |
582 } | 582 } |
583 | 583 |
584 TEST(ValuesTest, DictionaryWithoutPathExpansion) { | 584 TEST(ValuesTest, DictionaryWithoutPathExpansion) { |
585 DictionaryValue dict; | 585 DictionaryValue dict; |
586 dict.Set("this.is.expanded", Value::CreateNullValue()); | 586 dict.Set("this.is.expanded", MakeUnique<Value>()); |
587 dict.SetWithoutPathExpansion("this.isnt.expanded", Value::CreateNullValue()); | 587 dict.SetWithoutPathExpansion("this.isnt.expanded", MakeUnique<Value>()); |
588 | 588 |
589 EXPECT_FALSE(dict.HasKey("this.is.expanded")); | 589 EXPECT_FALSE(dict.HasKey("this.is.expanded")); |
590 EXPECT_TRUE(dict.HasKey("this")); | 590 EXPECT_TRUE(dict.HasKey("this")); |
591 Value* value1; | 591 Value* value1; |
592 EXPECT_TRUE(dict.Get("this", &value1)); | 592 EXPECT_TRUE(dict.Get("this", &value1)); |
593 DictionaryValue* value2; | 593 DictionaryValue* value2; |
594 ASSERT_TRUE(dict.GetDictionaryWithoutPathExpansion("this", &value2)); | 594 ASSERT_TRUE(dict.GetDictionaryWithoutPathExpansion("this", &value2)); |
595 EXPECT_EQ(value1, value2); | 595 EXPECT_EQ(value1, value2); |
596 EXPECT_EQ(1U, value2->size()); | 596 EXPECT_EQ(1U, value2->size()); |
597 | 597 |
598 EXPECT_TRUE(dict.HasKey("this.isnt.expanded")); | 598 EXPECT_TRUE(dict.HasKey("this.isnt.expanded")); |
599 Value* value3; | 599 Value* value3; |
600 EXPECT_FALSE(dict.Get("this.isnt.expanded", &value3)); | 600 EXPECT_FALSE(dict.Get("this.isnt.expanded", &value3)); |
601 Value* value4; | 601 Value* value4; |
602 ASSERT_TRUE(dict.GetWithoutPathExpansion("this.isnt.expanded", &value4)); | 602 ASSERT_TRUE(dict.GetWithoutPathExpansion("this.isnt.expanded", &value4)); |
603 EXPECT_EQ(Value::Type::NONE, value4->GetType()); | 603 EXPECT_EQ(Value::Type::NONE, value4->GetType()); |
604 } | 604 } |
605 | 605 |
606 // Tests the deprecated version of SetWithoutPathExpansion. | 606 // Tests the deprecated version of SetWithoutPathExpansion. |
607 // TODO(estade): remove. | 607 // TODO(estade): remove. |
608 TEST(ValuesTest, DictionaryWithoutPathExpansionDeprecated) { | 608 TEST(ValuesTest, DictionaryWithoutPathExpansionDeprecated) { |
609 DictionaryValue dict; | 609 DictionaryValue dict; |
610 dict.Set("this.is.expanded", Value::CreateNullValue()); | 610 dict.Set("this.is.expanded", MakeUnique<Value>()); |
611 dict.SetWithoutPathExpansion("this.isnt.expanded", Value::CreateNullValue()); | 611 dict.SetWithoutPathExpansion("this.isnt.expanded", MakeUnique<Value>()); |
612 | 612 |
613 EXPECT_FALSE(dict.HasKey("this.is.expanded")); | 613 EXPECT_FALSE(dict.HasKey("this.is.expanded")); |
614 EXPECT_TRUE(dict.HasKey("this")); | 614 EXPECT_TRUE(dict.HasKey("this")); |
615 Value* value1; | 615 Value* value1; |
616 EXPECT_TRUE(dict.Get("this", &value1)); | 616 EXPECT_TRUE(dict.Get("this", &value1)); |
617 DictionaryValue* value2; | 617 DictionaryValue* value2; |
618 ASSERT_TRUE(dict.GetDictionaryWithoutPathExpansion("this", &value2)); | 618 ASSERT_TRUE(dict.GetDictionaryWithoutPathExpansion("this", &value2)); |
619 EXPECT_EQ(value1, value2); | 619 EXPECT_EQ(value1, value2); |
620 EXPECT_EQ(1U, value2->size()); | 620 EXPECT_EQ(1U, value2->size()); |
621 | 621 |
(...skipping 25 matching lines...) Expand all Loading... |
647 | 647 |
648 removed_item.reset(); | 648 removed_item.reset(); |
649 EXPECT_TRUE(dict.RemovePath("a.long.key.path", &removed_item)); | 649 EXPECT_TRUE(dict.RemovePath("a.long.key.path", &removed_item)); |
650 ASSERT_TRUE(removed_item); | 650 ASSERT_TRUE(removed_item); |
651 EXPECT_TRUE(removed_item->IsType(base::Value::Type::BOOLEAN)); | 651 EXPECT_TRUE(removed_item->IsType(base::Value::Type::BOOLEAN)); |
652 EXPECT_TRUE(dict.empty()); | 652 EXPECT_TRUE(dict.empty()); |
653 } | 653 } |
654 | 654 |
655 TEST(ValuesTest, DeepCopy) { | 655 TEST(ValuesTest, DeepCopy) { |
656 DictionaryValue original_dict; | 656 DictionaryValue original_dict; |
657 std::unique_ptr<Value> scoped_null = Value::CreateNullValue(); | 657 auto scoped_null = MakeUnique<Value>(); |
658 Value* original_null = scoped_null.get(); | 658 Value* original_null = scoped_null.get(); |
659 original_dict.Set("null", std::move(scoped_null)); | 659 original_dict.Set("null", std::move(scoped_null)); |
660 std::unique_ptr<Value> scoped_bool(new Value(true)); | 660 std::unique_ptr<Value> scoped_bool(new Value(true)); |
661 Value* original_bool = scoped_bool.get(); | 661 Value* original_bool = scoped_bool.get(); |
662 original_dict.Set("bool", std::move(scoped_bool)); | 662 original_dict.Set("bool", std::move(scoped_bool)); |
663 std::unique_ptr<Value> scoped_int(new Value(42)); | 663 std::unique_ptr<Value> scoped_int(new Value(42)); |
664 Value* original_int = scoped_int.get(); | 664 Value* original_int = scoped_int.get(); |
665 original_dict.Set("int", std::move(scoped_int)); | 665 original_dict.Set("int", std::move(scoped_int)); |
666 std::unique_ptr<Value> scoped_double(new Value(3.14)); | 666 std::unique_ptr<Value> scoped_double(new Value(3.14)); |
667 Value* original_double = scoped_double.get(); | 667 Value* original_double = scoped_double.get(); |
(...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
795 ASSERT_TRUE(copy_value); | 795 ASSERT_TRUE(copy_value); |
796 ASSERT_NE(copy_value, original_nested_dictionary); | 796 ASSERT_NE(copy_value, original_nested_dictionary); |
797 ASSERT_TRUE(copy_value->IsType(Value::Type::DICTIONARY)); | 797 ASSERT_TRUE(copy_value->IsType(Value::Type::DICTIONARY)); |
798 DictionaryValue* copy_nested_dictionary = NULL; | 798 DictionaryValue* copy_nested_dictionary = NULL; |
799 ASSERT_TRUE(copy_value->GetAsDictionary(©_nested_dictionary)); | 799 ASSERT_TRUE(copy_value->GetAsDictionary(©_nested_dictionary)); |
800 ASSERT_TRUE(copy_nested_dictionary); | 800 ASSERT_TRUE(copy_nested_dictionary); |
801 EXPECT_TRUE(copy_nested_dictionary->HasKey("key")); | 801 EXPECT_TRUE(copy_nested_dictionary->HasKey("key")); |
802 } | 802 } |
803 | 803 |
804 TEST(ValuesTest, Equals) { | 804 TEST(ValuesTest, Equals) { |
805 std::unique_ptr<Value> null1(Value::CreateNullValue()); | 805 auto null1 = MakeUnique<Value>(); |
806 std::unique_ptr<Value> null2(Value::CreateNullValue()); | 806 auto null2 = MakeUnique<Value>(); |
807 EXPECT_NE(null1.get(), null2.get()); | 807 EXPECT_NE(null1.get(), null2.get()); |
808 EXPECT_EQ(*null1, *null2); | 808 EXPECT_EQ(*null1, *null2); |
809 | 809 |
810 Value boolean(false); | 810 Value boolean(false); |
811 EXPECT_NE(*null1, boolean); | 811 EXPECT_NE(*null1, boolean); |
812 | 812 |
813 DictionaryValue dv; | 813 DictionaryValue dv; |
814 dv.SetBoolean("a", false); | 814 dv.SetBoolean("a", false); |
815 dv.SetInteger("b", 2); | 815 dv.SetInteger("b", 2); |
816 dv.SetDouble("c", 2.5); | 816 dv.SetDouble("c", 2.5); |
817 dv.SetString("d1", "string"); | 817 dv.SetString("d1", "string"); |
818 dv.SetString("d2", ASCIIToUTF16("http://google.com")); | 818 dv.SetString("d2", ASCIIToUTF16("http://google.com")); |
819 dv.Set("e", Value::CreateNullValue()); | 819 dv.Set("e", MakeUnique<Value>()); |
820 | 820 |
821 auto copy = MakeUnique<DictionaryValue>(dv); | 821 auto copy = MakeUnique<DictionaryValue>(dv); |
822 EXPECT_EQ(dv, *copy); | 822 EXPECT_EQ(dv, *copy); |
823 | 823 |
824 std::unique_ptr<ListValue> list(new ListValue); | 824 std::unique_ptr<ListValue> list(new ListValue); |
825 ListValue* original_list = list.get(); | 825 ListValue* original_list = list.get(); |
826 list->Append(Value::CreateNullValue()); | 826 list->Append(MakeUnique<Value>()); |
827 list->Append(WrapUnique(new DictionaryValue)); | 827 list->Append(WrapUnique(new DictionaryValue)); |
828 auto list_copy = MakeUnique<Value>(*list); | 828 auto list_copy = MakeUnique<Value>(*list); |
829 | 829 |
830 dv.Set("f", std::move(list)); | 830 dv.Set("f", std::move(list)); |
831 EXPECT_NE(dv, *copy); | 831 EXPECT_NE(dv, *copy); |
832 copy->Set("f", std::move(list_copy)); | 832 copy->Set("f", std::move(list_copy)); |
833 EXPECT_EQ(dv, *copy); | 833 EXPECT_EQ(dv, *copy); |
834 | 834 |
835 original_list->Append(MakeUnique<Value>(true)); | 835 original_list->Append(MakeUnique<Value>(true)); |
836 EXPECT_NE(dv, *copy); | 836 EXPECT_NE(dv, *copy); |
837 | 837 |
838 // Check if Equals detects differences in only the keys. | 838 // Check if Equals detects differences in only the keys. |
839 copy = MakeUnique<DictionaryValue>(dv); | 839 copy = MakeUnique<DictionaryValue>(dv); |
840 EXPECT_EQ(dv, *copy); | 840 EXPECT_EQ(dv, *copy); |
841 copy->Remove("a", NULL); | 841 copy->Remove("a", NULL); |
842 copy->SetBoolean("aa", false); | 842 copy->SetBoolean("aa", false); |
843 EXPECT_NE(dv, *copy); | 843 EXPECT_NE(dv, *copy); |
844 } | 844 } |
845 | 845 |
846 TEST(ValuesTest, StaticEquals) { | 846 TEST(ValuesTest, StaticEquals) { |
847 std::unique_ptr<Value> null1(Value::CreateNullValue()); | 847 auto null1 = MakeUnique<Value>(); |
848 std::unique_ptr<Value> null2(Value::CreateNullValue()); | 848 auto null2 = MakeUnique<Value>(); |
849 EXPECT_TRUE(Value::Equals(null1.get(), null2.get())); | 849 EXPECT_TRUE(Value::Equals(null1.get(), null2.get())); |
850 EXPECT_TRUE(Value::Equals(NULL, NULL)); | 850 EXPECT_TRUE(Value::Equals(NULL, NULL)); |
851 | 851 |
852 std::unique_ptr<Value> i42(new Value(42)); | 852 std::unique_ptr<Value> i42(new Value(42)); |
853 std::unique_ptr<Value> j42(new Value(42)); | 853 std::unique_ptr<Value> j42(new Value(42)); |
854 std::unique_ptr<Value> i17(new Value(17)); | 854 std::unique_ptr<Value> i17(new Value(17)); |
855 EXPECT_TRUE(Value::Equals(i42.get(), i42.get())); | 855 EXPECT_TRUE(Value::Equals(i42.get(), i42.get())); |
856 EXPECT_TRUE(Value::Equals(j42.get(), i42.get())); | 856 EXPECT_TRUE(Value::Equals(j42.get(), i42.get())); |
857 EXPECT_TRUE(Value::Equals(i42.get(), j42.get())); | 857 EXPECT_TRUE(Value::Equals(i42.get(), j42.get())); |
858 EXPECT_FALSE(Value::Equals(i42.get(), i17.get())); | 858 EXPECT_FALSE(Value::Equals(i42.get(), i17.get())); |
859 EXPECT_FALSE(Value::Equals(i42.get(), NULL)); | 859 EXPECT_FALSE(Value::Equals(i42.get(), NULL)); |
860 EXPECT_FALSE(Value::Equals(NULL, i42.get())); | 860 EXPECT_FALSE(Value::Equals(NULL, i42.get())); |
861 | 861 |
862 // NULL and Value::CreateNullValue() are intentionally different: We need | 862 // NULL and MakeUnique<Value>() are intentionally different: We need |
863 // support for NULL as a return value for "undefined" without caring for | 863 // support for NULL as a return value for "undefined" without caring for |
864 // ownership of the pointer. | 864 // ownership of the pointer. |
865 EXPECT_FALSE(Value::Equals(null1.get(), NULL)); | 865 EXPECT_FALSE(Value::Equals(null1.get(), NULL)); |
866 EXPECT_FALSE(Value::Equals(NULL, null1.get())); | 866 EXPECT_FALSE(Value::Equals(NULL, null1.get())); |
867 } | 867 } |
868 | 868 |
869 TEST(ValuesTest, Comparisons) { | 869 TEST(ValuesTest, Comparisons) { |
870 // Test None Values. | 870 // Test None Values. |
871 Value null1; | 871 Value null1; |
872 Value null2; | 872 Value null2; |
(...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
981 EXPECT_LT(values[i], values[j]); | 981 EXPECT_LT(values[i], values[j]); |
982 EXPECT_FALSE(values[i] > values[j]); | 982 EXPECT_FALSE(values[i] > values[j]); |
983 EXPECT_LE(values[i], values[j]); | 983 EXPECT_LE(values[i], values[j]); |
984 EXPECT_FALSE(values[i] >= values[j]); | 984 EXPECT_FALSE(values[i] >= values[j]); |
985 } | 985 } |
986 } | 986 } |
987 } | 987 } |
988 | 988 |
989 TEST(ValuesTest, DeepCopyCovariantReturnTypes) { | 989 TEST(ValuesTest, DeepCopyCovariantReturnTypes) { |
990 DictionaryValue original_dict; | 990 DictionaryValue original_dict; |
991 std::unique_ptr<Value> scoped_null(Value::CreateNullValue()); | 991 auto scoped_null = MakeUnique<Value>(); |
992 Value* original_null = scoped_null.get(); | 992 Value* original_null = scoped_null.get(); |
993 original_dict.Set("null", std::move(scoped_null)); | 993 original_dict.Set("null", std::move(scoped_null)); |
994 std::unique_ptr<Value> scoped_bool(new Value(true)); | 994 std::unique_ptr<Value> scoped_bool(new Value(true)); |
995 Value* original_bool = scoped_bool.get(); | 995 Value* original_bool = scoped_bool.get(); |
996 original_dict.Set("bool", std::move(scoped_bool)); | 996 original_dict.Set("bool", std::move(scoped_bool)); |
997 std::unique_ptr<Value> scoped_int(new Value(42)); | 997 std::unique_ptr<Value> scoped_int(new Value(42)); |
998 Value* original_int = scoped_int.get(); | 998 Value* original_int = scoped_int.get(); |
999 original_dict.Set("int", std::move(scoped_int)); | 999 original_dict.Set("int", std::move(scoped_int)); |
1000 std::unique_ptr<Value> scoped_double(new Value(3.14)); | 1000 std::unique_ptr<Value> scoped_double(new Value(3.14)); |
1001 Value* original_double = scoped_double.get(); | 1001 Value* original_double = scoped_double.get(); |
(...skipping 518 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1520 } | 1520 } |
1521 | 1521 |
1522 TEST(ValuesTest, SelfSwap) { | 1522 TEST(ValuesTest, SelfSwap) { |
1523 base::Value test(1); | 1523 base::Value test(1); |
1524 using namespace std; | 1524 using namespace std; |
1525 swap(test, test); | 1525 swap(test, test); |
1526 EXPECT_TRUE(test.GetInt() == 1); | 1526 EXPECT_TRUE(test.GetInt() == 1); |
1527 } | 1527 } |
1528 | 1528 |
1529 } // namespace base | 1529 } // namespace base |
OLD | NEW |