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

Side by Side Diff: base/values_unittest.cc

Issue 2258713003: Re-write many calls to WrapUnique() with MakeUnique() (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Rebase Created 4 years, 3 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/trace_event/trace_event_argument_unittest.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 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
55 std::string bookmark_name = "Unnamed"; 55 std::string bookmark_name = "Unnamed";
56 ASSERT_TRUE(bookmark->GetString("name", &bookmark_name)); 56 ASSERT_TRUE(bookmark->GetString("name", &bookmark_name));
57 ASSERT_EQ(std::string("Froogle"), bookmark_name); 57 ASSERT_EQ(std::string("Froogle"), bookmark_name);
58 std::string bookmark_url; 58 std::string bookmark_url;
59 ASSERT_TRUE(bookmark->GetString("url", &bookmark_url)); 59 ASSERT_TRUE(bookmark->GetString("url", &bookmark_url));
60 ASSERT_EQ(std::string("http://froogle.com"), bookmark_url); 60 ASSERT_EQ(std::string("http://froogle.com"), bookmark_url);
61 } 61 }
62 62
63 TEST(ValuesTest, List) { 63 TEST(ValuesTest, List) {
64 std::unique_ptr<ListValue> mixed_list(new ListValue()); 64 std::unique_ptr<ListValue> mixed_list(new ListValue());
65 mixed_list->Set(0, WrapUnique(new FundamentalValue(true))); 65 mixed_list->Set(0, MakeUnique<FundamentalValue>(true));
66 mixed_list->Set(1, WrapUnique(new FundamentalValue(42))); 66 mixed_list->Set(1, MakeUnique<FundamentalValue>(42));
67 mixed_list->Set(2, WrapUnique(new FundamentalValue(88.8))); 67 mixed_list->Set(2, MakeUnique<FundamentalValue>(88.8));
68 mixed_list->Set(3, WrapUnique(new StringValue("foo"))); 68 mixed_list->Set(3, MakeUnique<StringValue>("foo"));
69 ASSERT_EQ(4u, mixed_list->GetSize()); 69 ASSERT_EQ(4u, mixed_list->GetSize());
70 70
71 Value *value = NULL; 71 Value *value = NULL;
72 bool bool_value = false; 72 bool bool_value = false;
73 int int_value = 0; 73 int int_value = 0;
74 double double_value = 0.0; 74 double double_value = 0.0;
75 std::string string_value; 75 std::string string_value;
76 76
77 ASSERT_FALSE(mixed_list->Get(4, &value)); 77 ASSERT_FALSE(mixed_list->Get(4, &value));
78 78
(...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after
193 193
194 private: 194 private:
195 bool* deletion_flag_; 195 bool* deletion_flag_;
196 }; 196 };
197 197
198 TEST(ValuesTest, ListDeletion) { 198 TEST(ValuesTest, ListDeletion) {
199 bool deletion_flag = true; 199 bool deletion_flag = true;
200 200
201 { 201 {
202 ListValue list; 202 ListValue list;
203 list.Append(WrapUnique(new DeletionTestValue(&deletion_flag))); 203 list.Append(MakeUnique<DeletionTestValue>(&deletion_flag));
204 EXPECT_FALSE(deletion_flag); 204 EXPECT_FALSE(deletion_flag);
205 } 205 }
206 EXPECT_TRUE(deletion_flag); 206 EXPECT_TRUE(deletion_flag);
207 207
208 { 208 {
209 ListValue list; 209 ListValue list;
210 list.Append(WrapUnique(new DeletionTestValue(&deletion_flag))); 210 list.Append(MakeUnique<DeletionTestValue>(&deletion_flag));
211 EXPECT_FALSE(deletion_flag); 211 EXPECT_FALSE(deletion_flag);
212 list.Clear(); 212 list.Clear();
213 EXPECT_TRUE(deletion_flag); 213 EXPECT_TRUE(deletion_flag);
214 } 214 }
215 215
216 { 216 {
217 ListValue list; 217 ListValue list;
218 list.Append(WrapUnique(new DeletionTestValue(&deletion_flag))); 218 list.Append(MakeUnique<DeletionTestValue>(&deletion_flag));
219 EXPECT_FALSE(deletion_flag); 219 EXPECT_FALSE(deletion_flag);
220 EXPECT_TRUE(list.Set(0, Value::CreateNullValue())); 220 EXPECT_TRUE(list.Set(0, Value::CreateNullValue()));
221 EXPECT_TRUE(deletion_flag); 221 EXPECT_TRUE(deletion_flag);
222 } 222 }
223 } 223 }
224 224
225 TEST(ValuesTest, ListRemoval) { 225 TEST(ValuesTest, ListRemoval) {
226 bool deletion_flag = true; 226 bool deletion_flag = true;
227 std::unique_ptr<Value> removed_item; 227 std::unique_ptr<Value> removed_item;
228 228
229 { 229 {
230 ListValue list; 230 ListValue list;
231 list.Append(WrapUnique(new DeletionTestValue(&deletion_flag))); 231 list.Append(MakeUnique<DeletionTestValue>(&deletion_flag));
232 EXPECT_FALSE(deletion_flag); 232 EXPECT_FALSE(deletion_flag);
233 EXPECT_EQ(1U, list.GetSize()); 233 EXPECT_EQ(1U, list.GetSize());
234 EXPECT_FALSE(list.Remove(std::numeric_limits<size_t>::max(), 234 EXPECT_FALSE(list.Remove(std::numeric_limits<size_t>::max(),
235 &removed_item)); 235 &removed_item));
236 EXPECT_FALSE(list.Remove(1, &removed_item)); 236 EXPECT_FALSE(list.Remove(1, &removed_item));
237 EXPECT_TRUE(list.Remove(0, &removed_item)); 237 EXPECT_TRUE(list.Remove(0, &removed_item));
238 ASSERT_TRUE(removed_item); 238 ASSERT_TRUE(removed_item);
239 EXPECT_EQ(0U, list.GetSize()); 239 EXPECT_EQ(0U, list.GetSize());
240 } 240 }
241 EXPECT_FALSE(deletion_flag); 241 EXPECT_FALSE(deletion_flag);
242 removed_item.reset(); 242 removed_item.reset();
243 EXPECT_TRUE(deletion_flag); 243 EXPECT_TRUE(deletion_flag);
244 244
245 { 245 {
246 ListValue list; 246 ListValue list;
247 list.Append(WrapUnique(new DeletionTestValue(&deletion_flag))); 247 list.Append(MakeUnique<DeletionTestValue>(&deletion_flag));
248 EXPECT_FALSE(deletion_flag); 248 EXPECT_FALSE(deletion_flag);
249 EXPECT_TRUE(list.Remove(0, NULL)); 249 EXPECT_TRUE(list.Remove(0, NULL));
250 EXPECT_TRUE(deletion_flag); 250 EXPECT_TRUE(deletion_flag);
251 EXPECT_EQ(0U, list.GetSize()); 251 EXPECT_EQ(0U, list.GetSize());
252 } 252 }
253 253
254 { 254 {
255 ListValue list; 255 ListValue list;
256 std::unique_ptr<DeletionTestValue> value( 256 std::unique_ptr<DeletionTestValue> value(
257 new DeletionTestValue(&deletion_flag)); 257 new DeletionTestValue(&deletion_flag));
258 DeletionTestValue* original_value = value.get(); 258 DeletionTestValue* original_value = value.get();
259 list.Append(std::move(value)); 259 list.Append(std::move(value));
260 EXPECT_FALSE(deletion_flag); 260 EXPECT_FALSE(deletion_flag);
261 size_t index = 0; 261 size_t index = 0;
262 list.Remove(*original_value, &index); 262 list.Remove(*original_value, &index);
263 EXPECT_EQ(0U, index); 263 EXPECT_EQ(0U, index);
264 EXPECT_TRUE(deletion_flag); 264 EXPECT_TRUE(deletion_flag);
265 EXPECT_EQ(0U, list.GetSize()); 265 EXPECT_EQ(0U, list.GetSize());
266 } 266 }
267 } 267 }
268 268
269 TEST(ValuesTest, DictionaryDeletion) { 269 TEST(ValuesTest, DictionaryDeletion) {
270 std::string key = "test"; 270 std::string key = "test";
271 bool deletion_flag = true; 271 bool deletion_flag = true;
272 272
273 { 273 {
274 DictionaryValue dict; 274 DictionaryValue dict;
275 dict.Set(key, WrapUnique(new DeletionTestValue(&deletion_flag))); 275 dict.Set(key, MakeUnique<DeletionTestValue>(&deletion_flag));
276 EXPECT_FALSE(deletion_flag); 276 EXPECT_FALSE(deletion_flag);
277 } 277 }
278 EXPECT_TRUE(deletion_flag); 278 EXPECT_TRUE(deletion_flag);
279 279
280 { 280 {
281 DictionaryValue dict; 281 DictionaryValue dict;
282 dict.Set(key, WrapUnique(new DeletionTestValue(&deletion_flag))); 282 dict.Set(key, MakeUnique<DeletionTestValue>(&deletion_flag));
283 EXPECT_FALSE(deletion_flag); 283 EXPECT_FALSE(deletion_flag);
284 dict.Clear(); 284 dict.Clear();
285 EXPECT_TRUE(deletion_flag); 285 EXPECT_TRUE(deletion_flag);
286 } 286 }
287 287
288 { 288 {
289 DictionaryValue dict; 289 DictionaryValue dict;
290 dict.Set(key, WrapUnique(new DeletionTestValue(&deletion_flag))); 290 dict.Set(key, MakeUnique<DeletionTestValue>(&deletion_flag));
291 EXPECT_FALSE(deletion_flag); 291 EXPECT_FALSE(deletion_flag);
292 dict.Set(key, Value::CreateNullValue()); 292 dict.Set(key, Value::CreateNullValue());
293 EXPECT_TRUE(deletion_flag); 293 EXPECT_TRUE(deletion_flag);
294 } 294 }
295 } 295 }
296 296
297 TEST(ValuesTest, DictionaryRemoval) { 297 TEST(ValuesTest, DictionaryRemoval) {
298 std::string key = "test"; 298 std::string key = "test";
299 bool deletion_flag = true; 299 bool deletion_flag = true;
300 std::unique_ptr<Value> removed_item; 300 std::unique_ptr<Value> removed_item;
301 301
302 { 302 {
303 DictionaryValue dict; 303 DictionaryValue dict;
304 dict.Set(key, WrapUnique(new DeletionTestValue(&deletion_flag))); 304 dict.Set(key, MakeUnique<DeletionTestValue>(&deletion_flag));
305 EXPECT_FALSE(deletion_flag); 305 EXPECT_FALSE(deletion_flag);
306 EXPECT_TRUE(dict.HasKey(key)); 306 EXPECT_TRUE(dict.HasKey(key));
307 EXPECT_FALSE(dict.Remove("absent key", &removed_item)); 307 EXPECT_FALSE(dict.Remove("absent key", &removed_item));
308 EXPECT_TRUE(dict.Remove(key, &removed_item)); 308 EXPECT_TRUE(dict.Remove(key, &removed_item));
309 EXPECT_FALSE(dict.HasKey(key)); 309 EXPECT_FALSE(dict.HasKey(key));
310 ASSERT_TRUE(removed_item); 310 ASSERT_TRUE(removed_item);
311 } 311 }
312 EXPECT_FALSE(deletion_flag); 312 EXPECT_FALSE(deletion_flag);
313 removed_item.reset(); 313 removed_item.reset();
314 EXPECT_TRUE(deletion_flag); 314 EXPECT_TRUE(deletion_flag);
315 315
316 { 316 {
317 DictionaryValue dict; 317 DictionaryValue dict;
318 dict.Set(key, WrapUnique(new DeletionTestValue(&deletion_flag))); 318 dict.Set(key, MakeUnique<DeletionTestValue>(&deletion_flag));
319 EXPECT_FALSE(deletion_flag); 319 EXPECT_FALSE(deletion_flag);
320 EXPECT_TRUE(dict.HasKey(key)); 320 EXPECT_TRUE(dict.HasKey(key));
321 EXPECT_TRUE(dict.Remove(key, NULL)); 321 EXPECT_TRUE(dict.Remove(key, NULL));
322 EXPECT_TRUE(deletion_flag); 322 EXPECT_TRUE(deletion_flag);
323 EXPECT_FALSE(dict.HasKey(key)); 323 EXPECT_FALSE(dict.HasKey(key));
324 } 324 }
325 } 325 }
326 326
327 TEST(ValuesTest, DictionaryWithoutPathExpansion) { 327 TEST(ValuesTest, DictionaryWithoutPathExpansion) {
328 DictionaryValue dict; 328 DictionaryValue dict;
(...skipping 246 matching lines...) Expand 10 before | Expand all | Expand 10 after
575 ListValue* original_list = list.get(); 575 ListValue* original_list = list.get();
576 list->Append(Value::CreateNullValue()); 576 list->Append(Value::CreateNullValue());
577 list->Append(WrapUnique(new DictionaryValue)); 577 list->Append(WrapUnique(new DictionaryValue));
578 std::unique_ptr<Value> list_copy(list->CreateDeepCopy()); 578 std::unique_ptr<Value> list_copy(list->CreateDeepCopy());
579 579
580 dv.Set("f", std::move(list)); 580 dv.Set("f", std::move(list));
581 EXPECT_FALSE(dv.Equals(copy.get())); 581 EXPECT_FALSE(dv.Equals(copy.get()));
582 copy->Set("f", std::move(list_copy)); 582 copy->Set("f", std::move(list_copy));
583 EXPECT_TRUE(dv.Equals(copy.get())); 583 EXPECT_TRUE(dv.Equals(copy.get()));
584 584
585 original_list->Append(WrapUnique(new FundamentalValue(true))); 585 original_list->Append(MakeUnique<FundamentalValue>(true));
586 EXPECT_FALSE(dv.Equals(copy.get())); 586 EXPECT_FALSE(dv.Equals(copy.get()));
587 587
588 // Check if Equals detects differences in only the keys. 588 // Check if Equals detects differences in only the keys.
589 copy = dv.CreateDeepCopy(); 589 copy = dv.CreateDeepCopy();
590 EXPECT_TRUE(dv.Equals(copy.get())); 590 EXPECT_TRUE(dv.Equals(copy.get()));
591 copy->Remove("a", NULL); 591 copy->Remove("a", NULL);
592 copy->SetBoolean("aa", false); 592 copy->SetBoolean("aa", false);
593 EXPECT_FALSE(dv.Equals(copy.get())); 593 EXPECT_FALSE(dv.Equals(copy.get()));
594 } 594 }
595 595
(...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after
732 inner2->Set("empty_list", WrapUnique(new ListValue)); 732 inner2->Set("empty_list", WrapUnique(new ListValue));
733 root->Set("dict_with_empty_children", std::move(inner2)); 733 root->Set("dict_with_empty_children", std::move(inner2));
734 root = root->DeepCopyWithoutEmptyChildren(); 734 root = root->DeepCopyWithoutEmptyChildren();
735 EXPECT_EQ(2U, root->size()); 735 EXPECT_EQ(2U, root->size());
736 } 736 }
737 737
738 // Make sure nested values don't get pruned. 738 // Make sure nested values don't get pruned.
739 { 739 {
740 std::unique_ptr<ListValue> inner(new ListValue); 740 std::unique_ptr<ListValue> inner(new ListValue);
741 std::unique_ptr<ListValue> inner2(new ListValue); 741 std::unique_ptr<ListValue> inner2(new ListValue);
742 inner2->Append(WrapUnique(new StringValue("hello"))); 742 inner2->Append(MakeUnique<StringValue>("hello"));
743 inner->Append(WrapUnique(new DictionaryValue)); 743 inner->Append(WrapUnique(new DictionaryValue));
744 inner->Append(std::move(inner2)); 744 inner->Append(std::move(inner2));
745 root->Set("list_with_empty_children", std::move(inner)); 745 root->Set("list_with_empty_children", std::move(inner));
746 root = root->DeepCopyWithoutEmptyChildren(); 746 root = root->DeepCopyWithoutEmptyChildren();
747 EXPECT_EQ(3U, root->size()); 747 EXPECT_EQ(3U, root->size());
748 748
749 ListValue* inner_value, *inner_value2; 749 ListValue* inner_value, *inner_value2;
750 EXPECT_TRUE(root->GetList("list_with_empty_children", &inner_value)); 750 EXPECT_TRUE(root->GetList("list_with_empty_children", &inner_value));
751 EXPECT_EQ(1U, inner_value->GetSize()); // Dictionary was pruned. 751 EXPECT_EQ(1U, inner_value->GetSize()); // Dictionary was pruned.
752 EXPECT_TRUE(inner_value->GetList(0, &inner_value2)); 752 EXPECT_TRUE(inner_value->GetList(0, &inner_value2));
(...skipping 394 matching lines...) Expand 10 before | Expand all | Expand 10 after
1147 EXPECT_FALSE(main_list.GetList(1, NULL)); 1147 EXPECT_FALSE(main_list.GetList(1, NULL));
1148 EXPECT_FALSE(main_list.GetList(2, NULL)); 1148 EXPECT_FALSE(main_list.GetList(2, NULL));
1149 EXPECT_FALSE(main_list.GetList(3, NULL)); 1149 EXPECT_FALSE(main_list.GetList(3, NULL));
1150 EXPECT_FALSE(main_list.GetList(4, NULL)); 1150 EXPECT_FALSE(main_list.GetList(4, NULL));
1151 EXPECT_FALSE(main_list.GetList(5, NULL)); 1151 EXPECT_FALSE(main_list.GetList(5, NULL));
1152 EXPECT_TRUE(main_list.GetList(6, NULL)); 1152 EXPECT_TRUE(main_list.GetList(6, NULL));
1153 EXPECT_FALSE(main_list.GetList(7, NULL)); 1153 EXPECT_FALSE(main_list.GetList(7, NULL));
1154 } 1154 }
1155 1155
1156 } // namespace base 1156 } // namespace base
OLDNEW
« no previous file with comments | « base/trace_event/trace_event_argument_unittest.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698