Chromium Code Reviews| 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 381 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 392 ASSERT_EQ(ASCIIToUTF16("utf16"), utf16); | 392 ASSERT_EQ(ASCIIToUTF16("utf16"), utf16); |
| 393 ASSERT_EQ(string_value->GetString(), narrow); | 393 ASSERT_EQ(string_value->GetString(), narrow); |
| 394 | 394 |
| 395 // Don't choke on NULL values. | 395 // Don't choke on NULL values. |
| 396 ASSERT_TRUE(narrow_value->GetAsString(static_cast<string16*>(NULL))); | 396 ASSERT_TRUE(narrow_value->GetAsString(static_cast<string16*>(NULL))); |
| 397 ASSERT_TRUE(narrow_value->GetAsString(static_cast<std::string*>(NULL))); | 397 ASSERT_TRUE(narrow_value->GetAsString(static_cast<std::string*>(NULL))); |
| 398 ASSERT_TRUE(narrow_value->GetAsString( | 398 ASSERT_TRUE(narrow_value->GetAsString( |
| 399 static_cast<const StringValue**>(NULL))); | 399 static_cast<const StringValue**>(NULL))); |
| 400 } | 400 } |
| 401 | 401 |
| 402 // This is a Value object that allows us to tell if it's been | |
| 403 // properly deleted by modifying the value of external flag on destruction. | |
| 404 class DeletionTestValue : public Value { | |
| 405 public: | |
| 406 explicit DeletionTestValue(bool* deletion_flag) : Value(Type::NONE) { | |
| 407 Init(deletion_flag); // Separate function so that we can use ASSERT_* | |
| 408 } | |
| 409 | |
| 410 void Init(bool* deletion_flag) { | |
| 411 ASSERT_TRUE(deletion_flag); | |
| 412 deletion_flag_ = deletion_flag; | |
| 413 *deletion_flag_ = false; | |
| 414 } | |
| 415 | |
| 416 ~DeletionTestValue() override { *deletion_flag_ = true; } | |
| 417 | |
| 418 private: | |
| 419 bool* deletion_flag_; | |
| 420 }; | |
|
jdoerrie
2017/02/10 15:17:38
I had to get rid of this class since Value's const
jdoerrie
2017/02/10 18:45:44
The tests are currently failing, because there is
brettw
2017/02/11 00:24:04
I think doing so is OK.
jdoerrie
2017/02/14 17:06:56
Done.
| |
| 421 | |
| 422 TEST(ValuesTest, ListDeletion) { | 402 TEST(ValuesTest, ListDeletion) { |
| 423 bool deletion_flag = true; | 403 ListValue list; |
| 424 | 404 list.Append(MakeUnique<Value>()); |
| 425 { | 405 EXPECT_FALSE(list.empty()); |
| 426 ListValue list; | 406 list.Clear(); |
| 427 list.Append(MakeUnique<DeletionTestValue>(&deletion_flag)); | 407 EXPECT_TRUE(list.empty()); |
| 428 EXPECT_FALSE(deletion_flag); | |
| 429 } | |
| 430 EXPECT_TRUE(deletion_flag); | |
| 431 | |
| 432 { | |
| 433 ListValue list; | |
| 434 list.Append(MakeUnique<DeletionTestValue>(&deletion_flag)); | |
| 435 EXPECT_FALSE(deletion_flag); | |
| 436 list.Clear(); | |
| 437 EXPECT_TRUE(deletion_flag); | |
| 438 } | |
| 439 | |
| 440 { | |
| 441 ListValue list; | |
| 442 list.Append(MakeUnique<DeletionTestValue>(&deletion_flag)); | |
| 443 EXPECT_FALSE(deletion_flag); | |
| 444 EXPECT_TRUE(list.Set(0, Value::CreateNullValue())); | |
| 445 EXPECT_TRUE(deletion_flag); | |
| 446 } | |
| 447 } | 408 } |
| 448 | 409 |
| 449 TEST(ValuesTest, ListRemoval) { | 410 TEST(ValuesTest, ListRemoval) { |
| 450 bool deletion_flag = true; | |
| 451 std::unique_ptr<Value> removed_item; | 411 std::unique_ptr<Value> removed_item; |
| 452 | 412 |
| 453 { | 413 { |
| 454 ListValue list; | 414 ListValue list; |
| 455 list.Append(MakeUnique<DeletionTestValue>(&deletion_flag)); | 415 list.Append(MakeUnique<Value>()); |
| 456 EXPECT_FALSE(deletion_flag); | |
| 457 EXPECT_EQ(1U, list.GetSize()); | 416 EXPECT_EQ(1U, list.GetSize()); |
| 458 EXPECT_FALSE(list.Remove(std::numeric_limits<size_t>::max(), | 417 EXPECT_FALSE(list.Remove(std::numeric_limits<size_t>::max(), |
| 459 &removed_item)); | 418 &removed_item)); |
| 460 EXPECT_FALSE(list.Remove(1, &removed_item)); | 419 EXPECT_FALSE(list.Remove(1, &removed_item)); |
| 461 EXPECT_TRUE(list.Remove(0, &removed_item)); | 420 EXPECT_TRUE(list.Remove(0, &removed_item)); |
| 462 ASSERT_TRUE(removed_item); | 421 ASSERT_TRUE(removed_item); |
| 463 EXPECT_EQ(0U, list.GetSize()); | 422 EXPECT_EQ(0U, list.GetSize()); |
| 464 } | 423 } |
| 465 EXPECT_FALSE(deletion_flag); | |
| 466 removed_item.reset(); | 424 removed_item.reset(); |
| 467 EXPECT_TRUE(deletion_flag); | |
| 468 | 425 |
| 469 { | 426 { |
| 470 ListValue list; | 427 ListValue list; |
| 471 list.Append(MakeUnique<DeletionTestValue>(&deletion_flag)); | 428 list.Append(MakeUnique<Value>()); |
| 472 EXPECT_FALSE(deletion_flag); | |
| 473 EXPECT_TRUE(list.Remove(0, NULL)); | 429 EXPECT_TRUE(list.Remove(0, NULL)); |
| 474 EXPECT_TRUE(deletion_flag); | |
| 475 EXPECT_EQ(0U, list.GetSize()); | 430 EXPECT_EQ(0U, list.GetSize()); |
| 476 } | 431 } |
| 477 | 432 |
| 478 { | 433 { |
| 479 ListValue list; | 434 ListValue list; |
| 480 std::unique_ptr<DeletionTestValue> value( | 435 auto value = MakeUnique<Value>(); |
| 481 new DeletionTestValue(&deletion_flag)); | 436 Value* original_value = value.get(); |
| 482 DeletionTestValue* original_value = value.get(); | |
| 483 list.Append(std::move(value)); | 437 list.Append(std::move(value)); |
| 484 EXPECT_FALSE(deletion_flag); | |
| 485 size_t index = 0; | 438 size_t index = 0; |
| 486 list.Remove(*original_value, &index); | 439 list.Remove(*original_value, &index); |
| 487 EXPECT_EQ(0U, index); | 440 EXPECT_EQ(0U, index); |
| 488 EXPECT_TRUE(deletion_flag); | |
| 489 EXPECT_EQ(0U, list.GetSize()); | 441 EXPECT_EQ(0U, list.GetSize()); |
| 490 } | 442 } |
| 491 } | 443 } |
| 492 | 444 |
| 493 TEST(ValuesTest, DictionaryDeletion) { | 445 TEST(ValuesTest, DictionaryDeletion) { |
| 494 std::string key = "test"; | 446 std::string key = "test"; |
| 495 bool deletion_flag = true; | 447 DictionaryValue dict; |
| 496 | 448 dict.Set(key, MakeUnique<Value>()); |
| 497 { | 449 EXPECT_FALSE(dict.empty()); |
| 498 DictionaryValue dict; | 450 dict.Clear(); |
| 499 dict.Set(key, MakeUnique<DeletionTestValue>(&deletion_flag)); | 451 EXPECT_TRUE(dict.empty()); |
| 500 EXPECT_FALSE(deletion_flag); | |
| 501 } | |
| 502 EXPECT_TRUE(deletion_flag); | |
| 503 | |
| 504 { | |
| 505 DictionaryValue dict; | |
| 506 dict.Set(key, MakeUnique<DeletionTestValue>(&deletion_flag)); | |
| 507 EXPECT_FALSE(deletion_flag); | |
| 508 dict.Clear(); | |
| 509 EXPECT_TRUE(deletion_flag); | |
| 510 } | |
| 511 | |
| 512 { | |
| 513 DictionaryValue dict; | |
| 514 dict.Set(key, MakeUnique<DeletionTestValue>(&deletion_flag)); | |
| 515 EXPECT_FALSE(deletion_flag); | |
| 516 dict.Set(key, Value::CreateNullValue()); | |
| 517 EXPECT_TRUE(deletion_flag); | |
| 518 } | |
| 519 } | 452 } |
| 520 | 453 |
| 521 TEST(ValuesTest, DictionaryRemoval) { | 454 TEST(ValuesTest, DictionaryRemoval) { |
| 522 std::string key = "test"; | 455 std::string key = "test"; |
| 523 bool deletion_flag = true; | |
| 524 std::unique_ptr<Value> removed_item; | 456 std::unique_ptr<Value> removed_item; |
| 525 | 457 |
| 526 { | 458 { |
| 527 DictionaryValue dict; | 459 DictionaryValue dict; |
| 528 dict.Set(key, MakeUnique<DeletionTestValue>(&deletion_flag)); | 460 dict.Set(key, MakeUnique<Value>()); |
| 529 EXPECT_FALSE(deletion_flag); | |
| 530 EXPECT_TRUE(dict.HasKey(key)); | 461 EXPECT_TRUE(dict.HasKey(key)); |
| 531 EXPECT_FALSE(dict.Remove("absent key", &removed_item)); | 462 EXPECT_FALSE(dict.Remove("absent key", &removed_item)); |
| 532 EXPECT_TRUE(dict.Remove(key, &removed_item)); | 463 EXPECT_TRUE(dict.Remove(key, &removed_item)); |
| 533 EXPECT_FALSE(dict.HasKey(key)); | 464 EXPECT_FALSE(dict.HasKey(key)); |
| 534 ASSERT_TRUE(removed_item); | 465 ASSERT_TRUE(removed_item); |
| 535 } | 466 } |
| 536 EXPECT_FALSE(deletion_flag); | |
| 537 removed_item.reset(); | |
| 538 EXPECT_TRUE(deletion_flag); | |
| 539 | 467 |
| 540 { | 468 { |
| 541 DictionaryValue dict; | 469 DictionaryValue dict; |
| 542 dict.Set(key, MakeUnique<DeletionTestValue>(&deletion_flag)); | 470 dict.Set(key, MakeUnique<Value>()); |
| 543 EXPECT_FALSE(deletion_flag); | |
| 544 EXPECT_TRUE(dict.HasKey(key)); | 471 EXPECT_TRUE(dict.HasKey(key)); |
| 545 EXPECT_TRUE(dict.Remove(key, NULL)); | 472 EXPECT_TRUE(dict.Remove(key, NULL)); |
| 546 EXPECT_TRUE(deletion_flag); | |
| 547 EXPECT_FALSE(dict.HasKey(key)); | 473 EXPECT_FALSE(dict.HasKey(key)); |
| 548 } | 474 } |
| 549 } | 475 } |
| 550 | 476 |
| 551 TEST(ValuesTest, DictionaryWithoutPathExpansion) { | 477 TEST(ValuesTest, DictionaryWithoutPathExpansion) { |
| 552 DictionaryValue dict; | 478 DictionaryValue dict; |
| 553 dict.Set("this.is.expanded", Value::CreateNullValue()); | 479 dict.Set("this.is.expanded", Value::CreateNullValue()); |
| 554 dict.SetWithoutPathExpansion("this.isnt.expanded", Value::CreateNullValue()); | 480 dict.SetWithoutPathExpansion("this.isnt.expanded", Value::CreateNullValue()); |
| 555 | 481 |
| 556 EXPECT_FALSE(dict.HasKey("this.is.expanded")); | 482 EXPECT_FALSE(dict.HasKey("this.is.expanded")); |
| (...skipping 809 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1366 EXPECT_FALSE(main_list.GetList(1, NULL)); | 1292 EXPECT_FALSE(main_list.GetList(1, NULL)); |
| 1367 EXPECT_FALSE(main_list.GetList(2, NULL)); | 1293 EXPECT_FALSE(main_list.GetList(2, NULL)); |
| 1368 EXPECT_FALSE(main_list.GetList(3, NULL)); | 1294 EXPECT_FALSE(main_list.GetList(3, NULL)); |
| 1369 EXPECT_FALSE(main_list.GetList(4, NULL)); | 1295 EXPECT_FALSE(main_list.GetList(4, NULL)); |
| 1370 EXPECT_FALSE(main_list.GetList(5, NULL)); | 1296 EXPECT_FALSE(main_list.GetList(5, NULL)); |
| 1371 EXPECT_TRUE(main_list.GetList(6, NULL)); | 1297 EXPECT_TRUE(main_list.GetList(6, NULL)); |
| 1372 EXPECT_FALSE(main_list.GetList(7, NULL)); | 1298 EXPECT_FALSE(main_list.GetList(7, NULL)); |
| 1373 } | 1299 } |
| 1374 | 1300 |
| 1375 } // namespace base | 1301 } // namespace base |
| OLD | NEW |