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

Side by Side Diff: base/values_unittest.cc

Issue 2781983003: Deprecate Value::(Create)DeepCopy (Closed)
Patch Set: Braces 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
« no previous file with comments | « 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 682 matching lines...) Expand 10 before | Expand all | Expand 10 after
693 Value* original_list_element_1 = scoped_list_element_1.get(); 693 Value* original_list_element_1 = scoped_list_element_1.get();
694 scoped_list->Append(std::move(scoped_list_element_1)); 694 scoped_list->Append(std::move(scoped_list_element_1));
695 original_dict.Set("list", std::move(scoped_list)); 695 original_dict.Set("list", std::move(scoped_list));
696 696
697 std::unique_ptr<DictionaryValue> scoped_nested_dictionary( 697 std::unique_ptr<DictionaryValue> scoped_nested_dictionary(
698 new DictionaryValue()); 698 new DictionaryValue());
699 Value* original_nested_dictionary = scoped_nested_dictionary.get(); 699 Value* original_nested_dictionary = scoped_nested_dictionary.get();
700 scoped_nested_dictionary->SetString("key", "value"); 700 scoped_nested_dictionary->SetString("key", "value");
701 original_dict.Set("dictionary", std::move(scoped_nested_dictionary)); 701 original_dict.Set("dictionary", std::move(scoped_nested_dictionary));
702 702
703 std::unique_ptr<DictionaryValue> copy_dict = original_dict.CreateDeepCopy(); 703 auto copy_dict = MakeUnique<DictionaryValue>(original_dict);
704 ASSERT_TRUE(copy_dict.get()); 704 ASSERT_TRUE(copy_dict.get());
705 ASSERT_NE(copy_dict.get(), &original_dict); 705 ASSERT_NE(copy_dict.get(), &original_dict);
706 706
707 Value* copy_null = NULL; 707 Value* copy_null = NULL;
708 ASSERT_TRUE(copy_dict->Get("null", &copy_null)); 708 ASSERT_TRUE(copy_dict->Get("null", &copy_null));
709 ASSERT_TRUE(copy_null); 709 ASSERT_TRUE(copy_null);
710 ASSERT_NE(copy_null, original_null); 710 ASSERT_NE(copy_null, original_null);
711 ASSERT_TRUE(copy_null->IsType(Value::Type::NONE)); 711 ASSERT_TRUE(copy_null->IsType(Value::Type::NONE));
712 712
713 Value* copy_bool = NULL; 713 Value* copy_bool = NULL;
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after
816 EXPECT_NE(*null1, boolean); 816 EXPECT_NE(*null1, boolean);
817 817
818 DictionaryValue dv; 818 DictionaryValue dv;
819 dv.SetBoolean("a", false); 819 dv.SetBoolean("a", false);
820 dv.SetInteger("b", 2); 820 dv.SetInteger("b", 2);
821 dv.SetDouble("c", 2.5); 821 dv.SetDouble("c", 2.5);
822 dv.SetString("d1", "string"); 822 dv.SetString("d1", "string");
823 dv.SetString("d2", ASCIIToUTF16("http://google.com")); 823 dv.SetString("d2", ASCIIToUTF16("http://google.com"));
824 dv.Set("e", Value::CreateNullValue()); 824 dv.Set("e", Value::CreateNullValue());
825 825
826 std::unique_ptr<DictionaryValue> copy = dv.CreateDeepCopy(); 826 auto copy = MakeUnique<DictionaryValue>(dv);
827 EXPECT_EQ(dv, *copy); 827 EXPECT_EQ(dv, *copy);
828 828
829 std::unique_ptr<ListValue> list(new ListValue); 829 std::unique_ptr<ListValue> list(new ListValue);
830 ListValue* original_list = list.get(); 830 ListValue* original_list = list.get();
831 list->Append(Value::CreateNullValue()); 831 list->Append(Value::CreateNullValue());
832 list->Append(WrapUnique(new DictionaryValue)); 832 list->Append(WrapUnique(new DictionaryValue));
833 std::unique_ptr<Value> list_copy(list->CreateDeepCopy()); 833 auto list_copy = MakeUnique<Value>(*list);
834 834
835 dv.Set("f", std::move(list)); 835 dv.Set("f", std::move(list));
836 EXPECT_NE(dv, *copy); 836 EXPECT_NE(dv, *copy);
837 copy->Set("f", std::move(list_copy)); 837 copy->Set("f", std::move(list_copy));
838 EXPECT_EQ(dv, *copy); 838 EXPECT_EQ(dv, *copy);
839 839
840 original_list->Append(MakeUnique<Value>(true)); 840 original_list->Append(MakeUnique<Value>(true));
841 EXPECT_NE(dv, *copy); 841 EXPECT_NE(dv, *copy);
842 842
843 // Check if Equals detects differences in only the keys. 843 // Check if Equals detects differences in only the keys.
844 copy = dv.CreateDeepCopy(); 844 copy = MakeUnique<DictionaryValue>(dv);
845 EXPECT_EQ(dv, *copy); 845 EXPECT_EQ(dv, *copy);
846 copy->Remove("a", NULL); 846 copy->Remove("a", NULL);
847 copy->SetBoolean("aa", false); 847 copy->SetBoolean("aa", false);
848 EXPECT_NE(dv, *copy); 848 EXPECT_NE(dv, *copy);
849 } 849 }
850 850
851 TEST(ValuesTest, StaticEquals) { 851 TEST(ValuesTest, StaticEquals) {
852 std::unique_ptr<Value> null1(Value::CreateNullValue()); 852 std::unique_ptr<Value> null1(Value::CreateNullValue());
853 std::unique_ptr<Value> null2(Value::CreateNullValue()); 853 std::unique_ptr<Value> null2(Value::CreateNullValue());
854 EXPECT_TRUE(Value::Equals(null1.get(), null2.get())); 854 EXPECT_TRUE(Value::Equals(null1.get(), null2.get()));
(...skipping 164 matching lines...) Expand 10 before | Expand all | Expand 10 after
1019 original_dict.Set("binary", std::move(scoped_binary)); 1019 original_dict.Set("binary", std::move(scoped_binary));
1020 1020
1021 std::unique_ptr<ListValue> scoped_list(new ListValue()); 1021 std::unique_ptr<ListValue> scoped_list(new ListValue());
1022 Value* original_list = scoped_list.get(); 1022 Value* original_list = scoped_list.get();
1023 std::unique_ptr<Value> scoped_list_element_0(new Value(0)); 1023 std::unique_ptr<Value> scoped_list_element_0(new Value(0));
1024 scoped_list->Append(std::move(scoped_list_element_0)); 1024 scoped_list->Append(std::move(scoped_list_element_0));
1025 std::unique_ptr<Value> scoped_list_element_1(new Value(1)); 1025 std::unique_ptr<Value> scoped_list_element_1(new Value(1));
1026 scoped_list->Append(std::move(scoped_list_element_1)); 1026 scoped_list->Append(std::move(scoped_list_element_1));
1027 original_dict.Set("list", std::move(scoped_list)); 1027 original_dict.Set("list", std::move(scoped_list));
1028 1028
1029 std::unique_ptr<Value> copy_dict = original_dict.CreateDeepCopy(); 1029 auto copy_dict = MakeUnique<Value>(original_dict);
1030 std::unique_ptr<Value> copy_null = original_null->CreateDeepCopy(); 1030 auto copy_null = MakeUnique<Value>(*original_null);
1031 std::unique_ptr<Value> copy_bool = original_bool->CreateDeepCopy(); 1031 auto copy_bool = MakeUnique<Value>(*original_bool);
1032 std::unique_ptr<Value> copy_int = original_int->CreateDeepCopy(); 1032 auto copy_int = MakeUnique<Value>(*original_int);
1033 std::unique_ptr<Value> copy_double = original_double->CreateDeepCopy(); 1033 auto copy_double = MakeUnique<Value>(*original_double);
1034 std::unique_ptr<Value> copy_string = original_string->CreateDeepCopy(); 1034 auto copy_string = MakeUnique<Value>(*original_string);
1035 std::unique_ptr<Value> copy_string16 = original_string16->CreateDeepCopy(); 1035 auto copy_string16 = MakeUnique<Value>(*original_string16);
1036 std::unique_ptr<Value> copy_binary = original_binary->CreateDeepCopy(); 1036 auto copy_binary = MakeUnique<Value>(*original_binary);
1037 std::unique_ptr<Value> copy_list = original_list->CreateDeepCopy(); 1037 auto copy_list = MakeUnique<Value>(*original_list);
1038 1038
1039 EXPECT_EQ(original_dict, *copy_dict); 1039 EXPECT_EQ(original_dict, *copy_dict);
1040 EXPECT_EQ(*original_null, *copy_null); 1040 EXPECT_EQ(*original_null, *copy_null);
1041 EXPECT_EQ(*original_bool, *copy_bool); 1041 EXPECT_EQ(*original_bool, *copy_bool);
1042 EXPECT_EQ(*original_int, *copy_int); 1042 EXPECT_EQ(*original_int, *copy_int);
1043 EXPECT_EQ(*original_double, *copy_double); 1043 EXPECT_EQ(*original_double, *copy_double);
1044 EXPECT_EQ(*original_string, *copy_string); 1044 EXPECT_EQ(*original_string, *copy_string);
1045 EXPECT_EQ(*original_string16, *copy_string16); 1045 EXPECT_EQ(*original_string16, *copy_string16);
1046 EXPECT_EQ(*original_binary, *copy_binary); 1046 EXPECT_EQ(*original_binary, *copy_binary);
1047 EXPECT_EQ(*original_list, *copy_list); 1047 EXPECT_EQ(*original_list, *copy_list);
(...skipping 154 matching lines...) Expand 10 before | Expand all | Expand 10 after
1202 EXPECT_EQ("value", value); 1202 EXPECT_EQ("value", value);
1203 } 1203 }
1204 1204
1205 TEST(ValuesTest, DictionaryIterator) { 1205 TEST(ValuesTest, DictionaryIterator) {
1206 DictionaryValue dict; 1206 DictionaryValue dict;
1207 for (DictionaryValue::Iterator it(dict); !it.IsAtEnd(); it.Advance()) { 1207 for (DictionaryValue::Iterator it(dict); !it.IsAtEnd(); it.Advance()) {
1208 ADD_FAILURE(); 1208 ADD_FAILURE();
1209 } 1209 }
1210 1210
1211 Value value1("value1"); 1211 Value value1("value1");
1212 dict.Set("key1", value1.CreateDeepCopy()); 1212 dict.Set("key1", MakeUnique<Value>(value1));
1213 bool seen1 = false; 1213 bool seen1 = false;
1214 for (DictionaryValue::Iterator it(dict); !it.IsAtEnd(); it.Advance()) { 1214 for (DictionaryValue::Iterator it(dict); !it.IsAtEnd(); it.Advance()) {
1215 EXPECT_FALSE(seen1); 1215 EXPECT_FALSE(seen1);
1216 EXPECT_EQ("key1", it.key()); 1216 EXPECT_EQ("key1", it.key());
1217 EXPECT_EQ(value1, it.value()); 1217 EXPECT_EQ(value1, it.value());
1218 seen1 = true; 1218 seen1 = true;
1219 } 1219 }
1220 EXPECT_TRUE(seen1); 1220 EXPECT_TRUE(seen1);
1221 1221
1222 Value value2("value2"); 1222 Value value2("value2");
1223 dict.Set("key2", value2.CreateDeepCopy()); 1223 dict.Set("key2", MakeUnique<Value>(value2));
1224 bool seen2 = seen1 = false; 1224 bool seen2 = seen1 = false;
1225 for (DictionaryValue::Iterator it(dict); !it.IsAtEnd(); it.Advance()) { 1225 for (DictionaryValue::Iterator it(dict); !it.IsAtEnd(); it.Advance()) {
1226 if (it.key() == "key1") { 1226 if (it.key() == "key1") {
1227 EXPECT_FALSE(seen1); 1227 EXPECT_FALSE(seen1);
1228 EXPECT_EQ(value1, it.value()); 1228 EXPECT_EQ(value1, it.value());
1229 seen1 = true; 1229 seen1 = true;
1230 } else if (it.key() == "key2") { 1230 } else if (it.key() == "key2") {
1231 EXPECT_FALSE(seen2); 1231 EXPECT_FALSE(seen2);
1232 EXPECT_EQ(value2, it.value()); 1232 EXPECT_EQ(value2, it.value());
1233 seen2 = true; 1233 seen2 = true;
(...skipping 12 matching lines...) Expand all
1246 ListValue main_list; 1246 ListValue main_list;
1247 1247
1248 Value bool_value(false); 1248 Value bool_value(false);
1249 Value int_value(1234); 1249 Value int_value(1234);
1250 Value double_value(12.34567); 1250 Value double_value(12.34567);
1251 Value string_value("foo"); 1251 Value string_value("foo");
1252 BinaryValue binary_value(Value::Type::BINARY); 1252 BinaryValue binary_value(Value::Type::BINARY);
1253 DictionaryValue dict_value; 1253 DictionaryValue dict_value;
1254 ListValue list_value; 1254 ListValue list_value;
1255 1255
1256 main_dict.Set("bool", bool_value.CreateDeepCopy()); 1256 main_dict.Set("bool", MakeUnique<Value>(bool_value));
1257 main_dict.Set("int", int_value.CreateDeepCopy()); 1257 main_dict.Set("int", MakeUnique<Value>(int_value));
1258 main_dict.Set("double", double_value.CreateDeepCopy()); 1258 main_dict.Set("double", MakeUnique<Value>(double_value));
1259 main_dict.Set("string", string_value.CreateDeepCopy()); 1259 main_dict.Set("string", MakeUnique<Value>(string_value));
1260 main_dict.Set("binary", binary_value.CreateDeepCopy()); 1260 main_dict.Set("binary", MakeUnique<Value>(binary_value));
1261 main_dict.Set("dict", dict_value.CreateDeepCopy()); 1261 main_dict.Set("dict", MakeUnique<Value>(dict_value));
1262 main_dict.Set("list", list_value.CreateDeepCopy()); 1262 main_dict.Set("list", MakeUnique<Value>(list_value));
1263 1263
1264 main_list.Append(bool_value.CreateDeepCopy()); 1264 main_list.Append(MakeUnique<Value>(bool_value));
1265 main_list.Append(int_value.CreateDeepCopy()); 1265 main_list.Append(MakeUnique<Value>(int_value));
1266 main_list.Append(double_value.CreateDeepCopy()); 1266 main_list.Append(MakeUnique<Value>(double_value));
1267 main_list.Append(string_value.CreateDeepCopy()); 1267 main_list.Append(MakeUnique<Value>(string_value));
1268 main_list.Append(binary_value.CreateDeepCopy()); 1268 main_list.Append(MakeUnique<Value>(binary_value));
1269 main_list.Append(dict_value.CreateDeepCopy()); 1269 main_list.Append(MakeUnique<Value>(dict_value));
1270 main_list.Append(list_value.CreateDeepCopy()); 1270 main_list.Append(MakeUnique<Value>(list_value));
1271 1271
1272 EXPECT_TRUE(main_dict.Get("bool", NULL)); 1272 EXPECT_TRUE(main_dict.Get("bool", NULL));
1273 EXPECT_TRUE(main_dict.Get("int", NULL)); 1273 EXPECT_TRUE(main_dict.Get("int", NULL));
1274 EXPECT_TRUE(main_dict.Get("double", NULL)); 1274 EXPECT_TRUE(main_dict.Get("double", NULL));
1275 EXPECT_TRUE(main_dict.Get("string", NULL)); 1275 EXPECT_TRUE(main_dict.Get("string", NULL));
1276 EXPECT_TRUE(main_dict.Get("binary", NULL)); 1276 EXPECT_TRUE(main_dict.Get("binary", NULL));
1277 EXPECT_TRUE(main_dict.Get("dict", NULL)); 1277 EXPECT_TRUE(main_dict.Get("dict", NULL));
1278 EXPECT_TRUE(main_dict.Get("list", NULL)); 1278 EXPECT_TRUE(main_dict.Get("list", NULL));
1279 EXPECT_FALSE(main_dict.Get("DNE", NULL)); 1279 EXPECT_FALSE(main_dict.Get("DNE", NULL));
1280 1280
(...skipping 237 matching lines...) Expand 10 before | Expand all | Expand 10 after
1518 EXPECT_FALSE(main_list.GetList(1, NULL)); 1518 EXPECT_FALSE(main_list.GetList(1, NULL));
1519 EXPECT_FALSE(main_list.GetList(2, NULL)); 1519 EXPECT_FALSE(main_list.GetList(2, NULL));
1520 EXPECT_FALSE(main_list.GetList(3, NULL)); 1520 EXPECT_FALSE(main_list.GetList(3, NULL));
1521 EXPECT_FALSE(main_list.GetList(4, NULL)); 1521 EXPECT_FALSE(main_list.GetList(4, NULL));
1522 EXPECT_FALSE(main_list.GetList(5, NULL)); 1522 EXPECT_FALSE(main_list.GetList(5, NULL));
1523 EXPECT_TRUE(main_list.GetList(6, NULL)); 1523 EXPECT_TRUE(main_list.GetList(6, NULL));
1524 EXPECT_FALSE(main_list.GetList(7, NULL)); 1524 EXPECT_FALSE(main_list.GetList(7, NULL));
1525 } 1525 }
1526 1526
1527 } // namespace base 1527 } // namespace base
OLDNEW
« no previous file with comments | « base/values.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698