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

Side by Side Diff: base/values_unittest.cc

Issue 2771923005: Implement comparison operators for base::Value (Closed)
Patch Set: Declare Operators out of line and update tests Created 3 years, 9 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.cc ('k') | chrome/browser/ui/webui/options/preferences_browsertest.cc » ('j') | 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 "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 771 matching lines...) Expand 10 before | Expand all | Expand 10 after
782 DictionaryValue* copy_nested_dictionary = NULL; 782 DictionaryValue* copy_nested_dictionary = NULL;
783 ASSERT_TRUE(copy_value->GetAsDictionary(&copy_nested_dictionary)); 783 ASSERT_TRUE(copy_value->GetAsDictionary(&copy_nested_dictionary));
784 ASSERT_TRUE(copy_nested_dictionary); 784 ASSERT_TRUE(copy_nested_dictionary);
785 EXPECT_TRUE(copy_nested_dictionary->HasKey("key")); 785 EXPECT_TRUE(copy_nested_dictionary->HasKey("key"));
786 } 786 }
787 787
788 TEST(ValuesTest, Equals) { 788 TEST(ValuesTest, Equals) {
789 std::unique_ptr<Value> null1(Value::CreateNullValue()); 789 std::unique_ptr<Value> null1(Value::CreateNullValue());
790 std::unique_ptr<Value> null2(Value::CreateNullValue()); 790 std::unique_ptr<Value> null2(Value::CreateNullValue());
791 EXPECT_NE(null1.get(), null2.get()); 791 EXPECT_NE(null1.get(), null2.get());
792 EXPECT_TRUE(null1->Equals(null2.get())); 792 EXPECT_EQ(*null1, *null2);
793 793
794 Value boolean(false); 794 Value boolean(false);
795 EXPECT_FALSE(null1->Equals(&boolean)); 795 EXPECT_NE(*null1, boolean);
796 796
797 DictionaryValue dv; 797 DictionaryValue dv;
798 dv.SetBoolean("a", false); 798 dv.SetBoolean("a", false);
799 dv.SetInteger("b", 2); 799 dv.SetInteger("b", 2);
800 dv.SetDouble("c", 2.5); 800 dv.SetDouble("c", 2.5);
801 dv.SetString("d1", "string"); 801 dv.SetString("d1", "string");
802 dv.SetString("d2", ASCIIToUTF16("http://google.com")); 802 dv.SetString("d2", ASCIIToUTF16("http://google.com"));
803 dv.Set("e", Value::CreateNullValue()); 803 dv.Set("e", Value::CreateNullValue());
804 804
805 std::unique_ptr<DictionaryValue> copy = dv.CreateDeepCopy(); 805 std::unique_ptr<DictionaryValue> copy = dv.CreateDeepCopy();
806 EXPECT_TRUE(dv.Equals(copy.get())); 806 EXPECT_EQ(dv, *copy);
807 807
808 std::unique_ptr<ListValue> list(new ListValue); 808 std::unique_ptr<ListValue> list(new ListValue);
809 ListValue* original_list = list.get(); 809 ListValue* original_list = list.get();
810 list->Append(Value::CreateNullValue()); 810 list->Append(Value::CreateNullValue());
811 list->Append(WrapUnique(new DictionaryValue)); 811 list->Append(WrapUnique(new DictionaryValue));
812 std::unique_ptr<Value> list_copy(list->CreateDeepCopy()); 812 std::unique_ptr<Value> list_copy(list->CreateDeepCopy());
813 813
814 dv.Set("f", std::move(list)); 814 dv.Set("f", std::move(list));
815 EXPECT_FALSE(dv.Equals(copy.get())); 815 EXPECT_NE(dv, *copy);
816 copy->Set("f", std::move(list_copy)); 816 copy->Set("f", std::move(list_copy));
817 EXPECT_TRUE(dv.Equals(copy.get())); 817 EXPECT_EQ(dv, *copy);
818 818
819 original_list->Append(MakeUnique<Value>(true)); 819 original_list->Append(MakeUnique<Value>(true));
820 EXPECT_FALSE(dv.Equals(copy.get())); 820 EXPECT_NE(dv, *copy);
821 821
822 // Check if Equals detects differences in only the keys. 822 // Check if Equals detects differences in only the keys.
823 copy = dv.CreateDeepCopy(); 823 copy = dv.CreateDeepCopy();
824 EXPECT_TRUE(dv.Equals(copy.get())); 824 EXPECT_EQ(dv, *copy);
825 copy->Remove("a", NULL); 825 copy->Remove("a", NULL);
826 copy->SetBoolean("aa", false); 826 copy->SetBoolean("aa", false);
827 EXPECT_FALSE(dv.Equals(copy.get())); 827 EXPECT_NE(dv, *copy);
828 } 828 }
829 829
830 TEST(ValuesTest, StaticEquals) { 830 TEST(ValuesTest, StaticEquals) {
831 std::unique_ptr<Value> null1(Value::CreateNullValue()); 831 std::unique_ptr<Value> null1(Value::CreateNullValue());
832 std::unique_ptr<Value> null2(Value::CreateNullValue()); 832 std::unique_ptr<Value> null2(Value::CreateNullValue());
833 EXPECT_TRUE(Value::Equals(null1.get(), null2.get())); 833 EXPECT_TRUE(Value::Equals(null1.get(), null2.get()));
834 EXPECT_TRUE(Value::Equals(NULL, NULL)); 834 EXPECT_TRUE(Value::Equals(NULL, NULL));
835 835
836 std::unique_ptr<Value> i42(new Value(42)); 836 std::unique_ptr<Value> i42(new Value(42));
837 std::unique_ptr<Value> j42(new Value(42)); 837 std::unique_ptr<Value> j42(new Value(42));
838 std::unique_ptr<Value> i17(new Value(17)); 838 std::unique_ptr<Value> i17(new Value(17));
839 EXPECT_TRUE(Value::Equals(i42.get(), i42.get())); 839 EXPECT_TRUE(Value::Equals(i42.get(), i42.get()));
840 EXPECT_TRUE(Value::Equals(j42.get(), i42.get())); 840 EXPECT_TRUE(Value::Equals(j42.get(), i42.get()));
841 EXPECT_TRUE(Value::Equals(i42.get(), j42.get())); 841 EXPECT_TRUE(Value::Equals(i42.get(), j42.get()));
842 EXPECT_FALSE(Value::Equals(i42.get(), i17.get())); 842 EXPECT_FALSE(Value::Equals(i42.get(), i17.get()));
843 EXPECT_FALSE(Value::Equals(i42.get(), NULL)); 843 EXPECT_FALSE(Value::Equals(i42.get(), NULL));
844 EXPECT_FALSE(Value::Equals(NULL, i42.get())); 844 EXPECT_FALSE(Value::Equals(NULL, i42.get()));
845 845
846 // NULL and Value::CreateNullValue() are intentionally different: We need 846 // NULL and Value::CreateNullValue() are intentionally different: We need
847 // support for NULL as a return value for "undefined" without caring for 847 // support for NULL as a return value for "undefined" without caring for
848 // ownership of the pointer. 848 // ownership of the pointer.
849 EXPECT_FALSE(Value::Equals(null1.get(), NULL)); 849 EXPECT_FALSE(Value::Equals(null1.get(), NULL));
850 EXPECT_FALSE(Value::Equals(NULL, null1.get())); 850 EXPECT_FALSE(Value::Equals(NULL, null1.get()));
851 } 851 }
852 852
853 TEST(ValuesTest, Comparisons) {
854 // Test None Values.
855 Value null1;
856 Value null2;
857 EXPECT_EQ(null1, null2);
858 EXPECT_FALSE(null1 != null2);
859 EXPECT_FALSE(null1 < null2);
860 EXPECT_FALSE(null1 > null2);
861 EXPECT_LE(null1, null2);
862 EXPECT_GE(null1, null2);
863
864 // Test Bool Values.
865 Value bool1(false);
866 Value bool2(true);
867 EXPECT_FALSE(bool1 == bool2);
868 EXPECT_NE(bool1, bool2);
869 EXPECT_LT(bool1, bool2);
870 EXPECT_FALSE(bool1 > bool2);
871 EXPECT_LE(bool1, bool2);
872 EXPECT_FALSE(bool1 >= bool2);
873
874 // Test Int Values.
875 Value int1(1);
876 Value int2(2);
877 EXPECT_FALSE(int1 == int2);
878 EXPECT_NE(int1, int2);
879 EXPECT_LT(int1, int2);
880 EXPECT_FALSE(int1 > int2);
881 EXPECT_LE(int1, int2);
882 EXPECT_FALSE(int1 >= int2);
883
884 // Test Double Values.
885 Value double1(1.0);
886 Value double2(2.0);
887 EXPECT_FALSE(double1 == double2);
888 EXPECT_NE(double1, double2);
889 EXPECT_LT(double1, double2);
890 EXPECT_FALSE(double1 > double2);
891 EXPECT_LE(double1, double2);
892 EXPECT_FALSE(double1 >= double2);
893
894 // Test String Values.
895 Value string1("1");
896 Value string2("2");
897 EXPECT_FALSE(string1 == string2);
898 EXPECT_NE(string1, string2);
899 EXPECT_LT(string1, string2);
900 EXPECT_FALSE(string1 > string2);
901 EXPECT_LE(string1, string2);
902 EXPECT_FALSE(string1 >= string2);
903
904 // Test Binary Values.
905 Value binary1(std::vector<char>{0x01});
906 Value binary2(std::vector<char>{0x02});
907 EXPECT_FALSE(binary1 == binary2);
908 EXPECT_NE(binary1, binary2);
909 EXPECT_LT(binary1, binary2);
910 EXPECT_FALSE(binary1 > binary2);
911 EXPECT_LE(binary1, binary2);
912 EXPECT_FALSE(binary1 >= binary2);
913
914 // Test Empty List Values.
915 ListValue null_list1;
916 ListValue null_list2;
917 EXPECT_EQ(null_list1, null_list2);
918 EXPECT_FALSE(null_list1 != null_list2);
919 EXPECT_FALSE(null_list1 < null_list2);
920 EXPECT_FALSE(null_list1 > null_list2);
921 EXPECT_LE(null_list1, null_list2);
922 EXPECT_GE(null_list1, null_list2);
923
924 // Test Non Empty List Values.
925 ListValue int_list1;
926 ListValue int_list2;
927 int_list1.AppendInteger(1);
928 int_list2.AppendInteger(2);
929 EXPECT_FALSE(int_list1 == int_list2);
930 EXPECT_NE(int_list1, int_list2);
931 EXPECT_LT(int_list1, int_list2);
932 EXPECT_FALSE(int_list1 > int_list2);
933 EXPECT_LE(int_list1, int_list2);
934 EXPECT_FALSE(int_list1 >= int_list2);
935
936 // Test Empty Dict Values.
937 DictionaryValue null_dict1;
938 DictionaryValue null_dict2;
939 EXPECT_EQ(null_dict1, null_dict2);
940 EXPECT_FALSE(null_dict1 != null_dict2);
941 EXPECT_FALSE(null_dict1 < null_dict2);
942 EXPECT_FALSE(null_dict1 > null_dict2);
943 EXPECT_LE(null_dict1, null_dict2);
944 EXPECT_GE(null_dict1, null_dict2);
945
946 // Test Non Empty Dict Values.
947 DictionaryValue int_dict1;
948 DictionaryValue int_dict2;
949 int_dict1.SetInteger("key", 1);
950 int_dict2.SetInteger("key", 2);
951 EXPECT_FALSE(int_dict1 == int_dict2);
952 EXPECT_NE(int_dict1, int_dict2);
953 EXPECT_LT(int_dict1, int_dict2);
954 EXPECT_FALSE(int_dict1 > int_dict2);
955 EXPECT_LE(int_dict1, int_dict2);
956 EXPECT_FALSE(int_dict1 >= int_dict2);
957
958 // Test Values of different types.
959 std::vector<Value> values = {null1, bool1, int1, double1,
960 string1, binary1, int_dict1, int_list1};
961 for (size_t i = 0; i < values.size(); ++i) {
962 for (size_t j = i + 1; j < values.size(); ++j) {
963 EXPECT_FALSE(values[i] == values[j]);
964 EXPECT_NE(values[i], values[j]);
965 EXPECT_LT(values[i], values[j]);
966 EXPECT_FALSE(values[i] > values[j]);
967 EXPECT_LE(values[i], values[j]);
968 EXPECT_FALSE(values[i] >= values[j]);
969 }
970 }
971 }
972
853 TEST(ValuesTest, DeepCopyCovariantReturnTypes) { 973 TEST(ValuesTest, DeepCopyCovariantReturnTypes) {
854 DictionaryValue original_dict; 974 DictionaryValue original_dict;
855 std::unique_ptr<Value> scoped_null(Value::CreateNullValue()); 975 std::unique_ptr<Value> scoped_null(Value::CreateNullValue());
856 Value* original_null = scoped_null.get(); 976 Value* original_null = scoped_null.get();
857 original_dict.Set("null", std::move(scoped_null)); 977 original_dict.Set("null", std::move(scoped_null));
858 std::unique_ptr<Value> scoped_bool(new Value(true)); 978 std::unique_ptr<Value> scoped_bool(new Value(true));
859 Value* original_bool = scoped_bool.get(); 979 Value* original_bool = scoped_bool.get();
860 original_dict.Set("bool", std::move(scoped_bool)); 980 original_dict.Set("bool", std::move(scoped_bool));
861 std::unique_ptr<Value> scoped_int(new Value(42)); 981 std::unique_ptr<Value> scoped_int(new Value(42));
862 Value* original_int = scoped_int.get(); 982 Value* original_int = scoped_int.get();
(...skipping 25 matching lines...) Expand all
888 std::unique_ptr<Value> copy_dict = original_dict.CreateDeepCopy(); 1008 std::unique_ptr<Value> copy_dict = original_dict.CreateDeepCopy();
889 std::unique_ptr<Value> copy_null = original_null->CreateDeepCopy(); 1009 std::unique_ptr<Value> copy_null = original_null->CreateDeepCopy();
890 std::unique_ptr<Value> copy_bool = original_bool->CreateDeepCopy(); 1010 std::unique_ptr<Value> copy_bool = original_bool->CreateDeepCopy();
891 std::unique_ptr<Value> copy_int = original_int->CreateDeepCopy(); 1011 std::unique_ptr<Value> copy_int = original_int->CreateDeepCopy();
892 std::unique_ptr<Value> copy_double = original_double->CreateDeepCopy(); 1012 std::unique_ptr<Value> copy_double = original_double->CreateDeepCopy();
893 std::unique_ptr<Value> copy_string = original_string->CreateDeepCopy(); 1013 std::unique_ptr<Value> copy_string = original_string->CreateDeepCopy();
894 std::unique_ptr<Value> copy_string16 = original_string16->CreateDeepCopy(); 1014 std::unique_ptr<Value> copy_string16 = original_string16->CreateDeepCopy();
895 std::unique_ptr<Value> copy_binary = original_binary->CreateDeepCopy(); 1015 std::unique_ptr<Value> copy_binary = original_binary->CreateDeepCopy();
896 std::unique_ptr<Value> copy_list = original_list->CreateDeepCopy(); 1016 std::unique_ptr<Value> copy_list = original_list->CreateDeepCopy();
897 1017
898 EXPECT_TRUE(original_dict.Equals(copy_dict.get())); 1018 EXPECT_EQ(original_dict, *copy_dict);
899 EXPECT_TRUE(original_null->Equals(copy_null.get())); 1019 EXPECT_EQ(*original_null, *copy_null);
900 EXPECT_TRUE(original_bool->Equals(copy_bool.get())); 1020 EXPECT_EQ(*original_bool, *copy_bool);
901 EXPECT_TRUE(original_int->Equals(copy_int.get())); 1021 EXPECT_EQ(*original_int, *copy_int);
902 EXPECT_TRUE(original_double->Equals(copy_double.get())); 1022 EXPECT_EQ(*original_double, *copy_double);
903 EXPECT_TRUE(original_string->Equals(copy_string.get())); 1023 EXPECT_EQ(*original_string, *copy_string);
904 EXPECT_TRUE(original_string16->Equals(copy_string16.get())); 1024 EXPECT_EQ(*original_string16, *copy_string16);
905 EXPECT_TRUE(original_binary->Equals(copy_binary.get())); 1025 EXPECT_EQ(*original_binary, *copy_binary);
906 EXPECT_TRUE(original_list->Equals(copy_list.get())); 1026 EXPECT_EQ(*original_list, *copy_list);
907 } 1027 }
908 1028
909 TEST(ValuesTest, RemoveEmptyChildren) { 1029 TEST(ValuesTest, RemoveEmptyChildren) {
910 std::unique_ptr<DictionaryValue> root(new DictionaryValue); 1030 std::unique_ptr<DictionaryValue> root(new DictionaryValue);
911 // Remove empty lists and dictionaries. 1031 // Remove empty lists and dictionaries.
912 root->Set("empty_dict", WrapUnique(new DictionaryValue)); 1032 root->Set("empty_dict", WrapUnique(new DictionaryValue));
913 root->Set("empty_list", WrapUnique(new ListValue)); 1033 root->Set("empty_list", WrapUnique(new ListValue));
914 root->SetWithoutPathExpansion("a.b.c.d.e", 1034 root->SetWithoutPathExpansion("a.b.c.d.e",
915 WrapUnique(new DictionaryValue)); 1035 WrapUnique(new DictionaryValue));
916 root = root->DeepCopyWithoutEmptyChildren(); 1036 root = root->DeepCopyWithoutEmptyChildren();
(...skipping 149 matching lines...) Expand 10 before | Expand all | Expand 10 after
1066 for (DictionaryValue::Iterator it(dict); !it.IsAtEnd(); it.Advance()) { 1186 for (DictionaryValue::Iterator it(dict); !it.IsAtEnd(); it.Advance()) {
1067 ADD_FAILURE(); 1187 ADD_FAILURE();
1068 } 1188 }
1069 1189
1070 Value value1("value1"); 1190 Value value1("value1");
1071 dict.Set("key1", value1.CreateDeepCopy()); 1191 dict.Set("key1", value1.CreateDeepCopy());
1072 bool seen1 = false; 1192 bool seen1 = false;
1073 for (DictionaryValue::Iterator it(dict); !it.IsAtEnd(); it.Advance()) { 1193 for (DictionaryValue::Iterator it(dict); !it.IsAtEnd(); it.Advance()) {
1074 EXPECT_FALSE(seen1); 1194 EXPECT_FALSE(seen1);
1075 EXPECT_EQ("key1", it.key()); 1195 EXPECT_EQ("key1", it.key());
1076 EXPECT_TRUE(value1.Equals(&it.value())); 1196 EXPECT_EQ(value1, it.value());
1077 seen1 = true; 1197 seen1 = true;
1078 } 1198 }
1079 EXPECT_TRUE(seen1); 1199 EXPECT_TRUE(seen1);
1080 1200
1081 Value value2("value2"); 1201 Value value2("value2");
1082 dict.Set("key2", value2.CreateDeepCopy()); 1202 dict.Set("key2", value2.CreateDeepCopy());
1083 bool seen2 = seen1 = false; 1203 bool seen2 = seen1 = false;
1084 for (DictionaryValue::Iterator it(dict); !it.IsAtEnd(); it.Advance()) { 1204 for (DictionaryValue::Iterator it(dict); !it.IsAtEnd(); it.Advance()) {
1085 if (it.key() == "key1") { 1205 if (it.key() == "key1") {
1086 EXPECT_FALSE(seen1); 1206 EXPECT_FALSE(seen1);
1087 EXPECT_TRUE(value1.Equals(&it.value())); 1207 EXPECT_EQ(value1, it.value());
1088 seen1 = true; 1208 seen1 = true;
1089 } else if (it.key() == "key2") { 1209 } else if (it.key() == "key2") {
1090 EXPECT_FALSE(seen2); 1210 EXPECT_FALSE(seen2);
1091 EXPECT_TRUE(value2.Equals(&it.value())); 1211 EXPECT_EQ(value2, it.value());
1092 seen2 = true; 1212 seen2 = true;
1093 } else { 1213 } else {
1094 ADD_FAILURE(); 1214 ADD_FAILURE();
1095 } 1215 }
1096 } 1216 }
1097 EXPECT_TRUE(seen1); 1217 EXPECT_TRUE(seen1);
1098 EXPECT_TRUE(seen2); 1218 EXPECT_TRUE(seen2);
1099 } 1219 }
1100 1220
1101 // DictionaryValue/ListValue's Get*() methods should accept NULL as an out-value 1221 // DictionaryValue/ListValue's Get*() methods should accept NULL as an out-value
(...skipping 275 matching lines...) Expand 10 before | Expand all | Expand 10 after
1377 EXPECT_FALSE(main_list.GetList(1, NULL)); 1497 EXPECT_FALSE(main_list.GetList(1, NULL));
1378 EXPECT_FALSE(main_list.GetList(2, NULL)); 1498 EXPECT_FALSE(main_list.GetList(2, NULL));
1379 EXPECT_FALSE(main_list.GetList(3, NULL)); 1499 EXPECT_FALSE(main_list.GetList(3, NULL));
1380 EXPECT_FALSE(main_list.GetList(4, NULL)); 1500 EXPECT_FALSE(main_list.GetList(4, NULL));
1381 EXPECT_FALSE(main_list.GetList(5, NULL)); 1501 EXPECT_FALSE(main_list.GetList(5, NULL));
1382 EXPECT_TRUE(main_list.GetList(6, NULL)); 1502 EXPECT_TRUE(main_list.GetList(6, NULL));
1383 EXPECT_FALSE(main_list.GetList(7, NULL)); 1503 EXPECT_FALSE(main_list.GetList(7, NULL));
1384 } 1504 }
1385 1505
1386 } // namespace base 1506 } // namespace base
OLDNEW
« no previous file with comments | « base/values.cc ('k') | chrome/browser/ui/webui/options/preferences_browsertest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698