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

Side by Side Diff: base/values_unittest.cc

Issue 2781983003: Deprecate Value::(Create)DeepCopy (Closed)
Patch Set: Remove unneccessary base:: namespace qualifier Created 3 years, 8 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
« base/values.cc ('K') | « base/values.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 "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 661 matching lines...) Expand 10 before | Expand all | Expand 10 after
672 Value* original_list_element_1 = scoped_list_element_1.get(); 672 Value* original_list_element_1 = scoped_list_element_1.get();
673 scoped_list->Append(std::move(scoped_list_element_1)); 673 scoped_list->Append(std::move(scoped_list_element_1));
674 original_dict.Set("list", std::move(scoped_list)); 674 original_dict.Set("list", std::move(scoped_list));
675 675
676 std::unique_ptr<DictionaryValue> scoped_nested_dictionary( 676 std::unique_ptr<DictionaryValue> scoped_nested_dictionary(
677 new DictionaryValue()); 677 new DictionaryValue());
678 Value* original_nested_dictionary = scoped_nested_dictionary.get(); 678 Value* original_nested_dictionary = scoped_nested_dictionary.get();
679 scoped_nested_dictionary->SetString("key", "value"); 679 scoped_nested_dictionary->SetString("key", "value");
680 original_dict.Set("dictionary", std::move(scoped_nested_dictionary)); 680 original_dict.Set("dictionary", std::move(scoped_nested_dictionary));
681 681
682 std::unique_ptr<DictionaryValue> copy_dict = original_dict.CreateDeepCopy(); 682 auto copy_dict = MakeUnique<DictionaryValue>(original_dict);
683 ASSERT_TRUE(copy_dict.get()); 683 ASSERT_TRUE(copy_dict.get());
684 ASSERT_NE(copy_dict.get(), &original_dict); 684 ASSERT_NE(copy_dict.get(), &original_dict);
685 685
686 Value* copy_null = NULL; 686 Value* copy_null = NULL;
687 ASSERT_TRUE(copy_dict->Get("null", &copy_null)); 687 ASSERT_TRUE(copy_dict->Get("null", &copy_null));
688 ASSERT_TRUE(copy_null); 688 ASSERT_TRUE(copy_null);
689 ASSERT_NE(copy_null, original_null); 689 ASSERT_NE(copy_null, original_null);
690 ASSERT_TRUE(copy_null->IsType(Value::Type::NONE)); 690 ASSERT_TRUE(copy_null->IsType(Value::Type::NONE));
691 691
692 Value* copy_bool = NULL; 692 Value* copy_bool = NULL;
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after
795 EXPECT_NE(*null1, 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 auto copy = MakeUnique<DictionaryValue>(dv);
806 EXPECT_EQ(dv, *copy); 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 auto list_copy = MakeUnique<Value>(*list);
813 813
814 dv.Set("f", std::move(list)); 814 dv.Set("f", std::move(list));
815 EXPECT_NE(dv, *copy); 815 EXPECT_NE(dv, *copy);
816 copy->Set("f", std::move(list_copy)); 816 copy->Set("f", std::move(list_copy));
817 EXPECT_EQ(dv, *copy); 817 EXPECT_EQ(dv, *copy);
818 818
819 original_list->Append(MakeUnique<Value>(true)); 819 original_list->Append(MakeUnique<Value>(true));
820 EXPECT_NE(dv, *copy); 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 = MakeUnique<DictionaryValue>(dv);
824 EXPECT_EQ(dv, *copy); 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_NE(dv, *copy); 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()));
(...skipping 164 matching lines...) Expand 10 before | Expand all | Expand 10 after
998 original_dict.Set("binary", std::move(scoped_binary)); 998 original_dict.Set("binary", std::move(scoped_binary));
999 999
1000 std::unique_ptr<ListValue> scoped_list(new ListValue()); 1000 std::unique_ptr<ListValue> scoped_list(new ListValue());
1001 Value* original_list = scoped_list.get(); 1001 Value* original_list = scoped_list.get();
1002 std::unique_ptr<Value> scoped_list_element_0(new Value(0)); 1002 std::unique_ptr<Value> scoped_list_element_0(new Value(0));
1003 scoped_list->Append(std::move(scoped_list_element_0)); 1003 scoped_list->Append(std::move(scoped_list_element_0));
1004 std::unique_ptr<Value> scoped_list_element_1(new Value(1)); 1004 std::unique_ptr<Value> scoped_list_element_1(new Value(1));
1005 scoped_list->Append(std::move(scoped_list_element_1)); 1005 scoped_list->Append(std::move(scoped_list_element_1));
1006 original_dict.Set("list", std::move(scoped_list)); 1006 original_dict.Set("list", std::move(scoped_list));
1007 1007
1008 std::unique_ptr<Value> copy_dict = original_dict.CreateDeepCopy(); 1008 auto copy_dict = MakeUnique<Value>(original_dict);
1009 std::unique_ptr<Value> copy_null = original_null->CreateDeepCopy(); 1009 auto copy_null = MakeUnique<Value>(*original_null);
1010 std::unique_ptr<Value> copy_bool = original_bool->CreateDeepCopy(); 1010 auto copy_bool = MakeUnique<Value>(*original_bool);
1011 std::unique_ptr<Value> copy_int = original_int->CreateDeepCopy(); 1011 auto copy_int = MakeUnique<Value>(*original_int);
1012 std::unique_ptr<Value> copy_double = original_double->CreateDeepCopy(); 1012 auto copy_double = MakeUnique<Value>(*original_double);
1013 std::unique_ptr<Value> copy_string = original_string->CreateDeepCopy(); 1013 auto copy_string = MakeUnique<Value>(*original_string);
1014 std::unique_ptr<Value> copy_string16 = original_string16->CreateDeepCopy(); 1014 auto copy_string16 = MakeUnique<Value>(*original_string16);
1015 std::unique_ptr<Value> copy_binary = original_binary->CreateDeepCopy(); 1015 auto copy_binary = MakeUnique<Value>(*original_binary);
1016 std::unique_ptr<Value> copy_list = original_list->CreateDeepCopy(); 1016 auto copy_list = MakeUnique<Value>(*original_list);
1017 1017
1018 EXPECT_EQ(original_dict, *copy_dict); 1018 EXPECT_EQ(original_dict, *copy_dict);
1019 EXPECT_EQ(*original_null, *copy_null); 1019 EXPECT_EQ(*original_null, *copy_null);
1020 EXPECT_EQ(*original_bool, *copy_bool); 1020 EXPECT_EQ(*original_bool, *copy_bool);
1021 EXPECT_EQ(*original_int, *copy_int); 1021 EXPECT_EQ(*original_int, *copy_int);
1022 EXPECT_EQ(*original_double, *copy_double); 1022 EXPECT_EQ(*original_double, *copy_double);
1023 EXPECT_EQ(*original_string, *copy_string); 1023 EXPECT_EQ(*original_string, *copy_string);
1024 EXPECT_EQ(*original_string16, *copy_string16); 1024 EXPECT_EQ(*original_string16, *copy_string16);
1025 EXPECT_EQ(*original_binary, *copy_binary); 1025 EXPECT_EQ(*original_binary, *copy_binary);
1026 EXPECT_EQ(*original_list, *copy_list); 1026 EXPECT_EQ(*original_list, *copy_list);
(...skipping 154 matching lines...) Expand 10 before | Expand all | Expand 10 after
1181 EXPECT_EQ("value", value); 1181 EXPECT_EQ("value", value);
1182 } 1182 }
1183 1183
1184 TEST(ValuesTest, DictionaryIterator) { 1184 TEST(ValuesTest, DictionaryIterator) {
1185 DictionaryValue dict; 1185 DictionaryValue dict;
1186 for (DictionaryValue::Iterator it(dict); !it.IsAtEnd(); it.Advance()) { 1186 for (DictionaryValue::Iterator it(dict); !it.IsAtEnd(); it.Advance()) {
1187 ADD_FAILURE(); 1187 ADD_FAILURE();
1188 } 1188 }
1189 1189
1190 Value value1("value1"); 1190 Value value1("value1");
1191 dict.Set("key1", value1.CreateDeepCopy()); 1191 dict.Set("key1", MakeUnique<Value>(value1));
1192 bool seen1 = false; 1192 bool seen1 = false;
1193 for (DictionaryValue::Iterator it(dict); !it.IsAtEnd(); it.Advance()) { 1193 for (DictionaryValue::Iterator it(dict); !it.IsAtEnd(); it.Advance()) {
1194 EXPECT_FALSE(seen1); 1194 EXPECT_FALSE(seen1);
1195 EXPECT_EQ("key1", it.key()); 1195 EXPECT_EQ("key1", it.key());
1196 EXPECT_EQ(value1, it.value()); 1196 EXPECT_EQ(value1, it.value());
1197 seen1 = true; 1197 seen1 = true;
1198 } 1198 }
1199 EXPECT_TRUE(seen1); 1199 EXPECT_TRUE(seen1);
1200 1200
1201 Value value2("value2"); 1201 Value value2("value2");
1202 dict.Set("key2", value2.CreateDeepCopy()); 1202 dict.Set("key2", MakeUnique<Value>(value2));
1203 bool seen2 = seen1 = false; 1203 bool seen2 = seen1 = false;
1204 for (DictionaryValue::Iterator it(dict); !it.IsAtEnd(); it.Advance()) { 1204 for (DictionaryValue::Iterator it(dict); !it.IsAtEnd(); it.Advance()) {
1205 if (it.key() == "key1") { 1205 if (it.key() == "key1") {
1206 EXPECT_FALSE(seen1); 1206 EXPECT_FALSE(seen1);
1207 EXPECT_EQ(value1, it.value()); 1207 EXPECT_EQ(value1, it.value());
1208 seen1 = true; 1208 seen1 = true;
1209 } else if (it.key() == "key2") { 1209 } else if (it.key() == "key2") {
1210 EXPECT_FALSE(seen2); 1210 EXPECT_FALSE(seen2);
1211 EXPECT_EQ(value2, it.value()); 1211 EXPECT_EQ(value2, it.value());
1212 seen2 = true; 1212 seen2 = true;
(...skipping 12 matching lines...) Expand all
1225 ListValue main_list; 1225 ListValue main_list;
1226 1226
1227 Value bool_value(false); 1227 Value bool_value(false);
1228 Value int_value(1234); 1228 Value int_value(1234);
1229 Value double_value(12.34567); 1229 Value double_value(12.34567);
1230 Value string_value("foo"); 1230 Value string_value("foo");
1231 BinaryValue binary_value(Value::Type::BINARY); 1231 BinaryValue binary_value(Value::Type::BINARY);
1232 DictionaryValue dict_value; 1232 DictionaryValue dict_value;
1233 ListValue list_value; 1233 ListValue list_value;
1234 1234
1235 main_dict.Set("bool", bool_value.CreateDeepCopy()); 1235 main_dict.Set("bool", MakeUnique<Value>(bool_value));
1236 main_dict.Set("int", int_value.CreateDeepCopy()); 1236 main_dict.Set("int", MakeUnique<Value>(int_value));
1237 main_dict.Set("double", double_value.CreateDeepCopy()); 1237 main_dict.Set("double", MakeUnique<Value>(double_value));
1238 main_dict.Set("string", string_value.CreateDeepCopy()); 1238 main_dict.Set("string", MakeUnique<Value>(string_value));
1239 main_dict.Set("binary", binary_value.CreateDeepCopy()); 1239 main_dict.Set("binary", MakeUnique<Value>(binary_value));
1240 main_dict.Set("dict", dict_value.CreateDeepCopy()); 1240 main_dict.Set("dict", MakeUnique<Value>(dict_value));
1241 main_dict.Set("list", list_value.CreateDeepCopy()); 1241 main_dict.Set("list", MakeUnique<Value>(list_value));
1242 1242
1243 main_list.Append(bool_value.CreateDeepCopy()); 1243 main_list.Append(MakeUnique<Value>(bool_value));
1244 main_list.Append(int_value.CreateDeepCopy()); 1244 main_list.Append(MakeUnique<Value>(int_value));
1245 main_list.Append(double_value.CreateDeepCopy()); 1245 main_list.Append(MakeUnique<Value>(double_value));
1246 main_list.Append(string_value.CreateDeepCopy()); 1246 main_list.Append(MakeUnique<Value>(string_value));
1247 main_list.Append(binary_value.CreateDeepCopy()); 1247 main_list.Append(MakeUnique<Value>(binary_value));
1248 main_list.Append(dict_value.CreateDeepCopy()); 1248 main_list.Append(MakeUnique<Value>(dict_value));
1249 main_list.Append(list_value.CreateDeepCopy()); 1249 main_list.Append(MakeUnique<Value>(list_value));
1250 1250
1251 EXPECT_TRUE(main_dict.Get("bool", NULL)); 1251 EXPECT_TRUE(main_dict.Get("bool", NULL));
1252 EXPECT_TRUE(main_dict.Get("int", NULL)); 1252 EXPECT_TRUE(main_dict.Get("int", NULL));
1253 EXPECT_TRUE(main_dict.Get("double", NULL)); 1253 EXPECT_TRUE(main_dict.Get("double", NULL));
1254 EXPECT_TRUE(main_dict.Get("string", NULL)); 1254 EXPECT_TRUE(main_dict.Get("string", NULL));
1255 EXPECT_TRUE(main_dict.Get("binary", NULL)); 1255 EXPECT_TRUE(main_dict.Get("binary", NULL));
1256 EXPECT_TRUE(main_dict.Get("dict", NULL)); 1256 EXPECT_TRUE(main_dict.Get("dict", NULL));
1257 EXPECT_TRUE(main_dict.Get("list", NULL)); 1257 EXPECT_TRUE(main_dict.Get("list", NULL));
1258 EXPECT_FALSE(main_dict.Get("DNE", NULL)); 1258 EXPECT_FALSE(main_dict.Get("DNE", NULL));
1259 1259
(...skipping 237 matching lines...) Expand 10 before | Expand all | Expand 10 after
1497 EXPECT_FALSE(main_list.GetList(1, NULL)); 1497 EXPECT_FALSE(main_list.GetList(1, NULL));
1498 EXPECT_FALSE(main_list.GetList(2, NULL)); 1498 EXPECT_FALSE(main_list.GetList(2, NULL));
1499 EXPECT_FALSE(main_list.GetList(3, NULL)); 1499 EXPECT_FALSE(main_list.GetList(3, NULL));
1500 EXPECT_FALSE(main_list.GetList(4, NULL)); 1500 EXPECT_FALSE(main_list.GetList(4, NULL));
1501 EXPECT_FALSE(main_list.GetList(5, NULL)); 1501 EXPECT_FALSE(main_list.GetList(5, NULL));
1502 EXPECT_TRUE(main_list.GetList(6, NULL)); 1502 EXPECT_TRUE(main_list.GetList(6, NULL));
1503 EXPECT_FALSE(main_list.GetList(7, NULL)); 1503 EXPECT_FALSE(main_list.GetList(7, NULL));
1504 } 1504 }
1505 1505
1506 } // namespace base 1506 } // namespace base
OLDNEW
« base/values.cc ('K') | « base/values.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698