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

Side by Side Diff: base/values_unittest.cc

Issue 2666093002: Remove base::FundamentalValue (Closed)
Patch Set: Rebase 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
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>
11 #include <utility> 11 #include <utility>
12 #include <vector> 12 #include <vector>
13 13
14 #include "base/memory/ptr_util.h" 14 #include "base/memory/ptr_util.h"
15 #include "base/strings/string16.h" 15 #include "base/strings/string16.h"
16 #include "base/strings/utf_string_conversions.h" 16 #include "base/strings/utf_string_conversions.h"
17 #include "testing/gtest/include/gtest/gtest.h" 17 #include "testing/gtest/include/gtest/gtest.h"
18 18
19 namespace base { 19 namespace base {
20 20
21 // Group of tests for the value constructors. 21 // Group of tests for the value constructors.
22 TEST(ValuesTest, ConstructBool) { 22 TEST(ValuesTest, ConstructBool) {
23 FundamentalValue true_value(true); 23 Value true_value(true);
24 EXPECT_EQ(Value::Type::BOOLEAN, true_value.type()); 24 EXPECT_EQ(Value::Type::BOOLEAN, true_value.type());
25 EXPECT_TRUE(true_value.GetBool()); 25 EXPECT_TRUE(true_value.GetBool());
26 26
27 FundamentalValue false_value(false); 27 Value false_value(false);
28 EXPECT_EQ(Value::Type::BOOLEAN, false_value.type()); 28 EXPECT_EQ(Value::Type::BOOLEAN, false_value.type());
29 EXPECT_FALSE(false_value.GetBool()); 29 EXPECT_FALSE(false_value.GetBool());
30 } 30 }
31 31
32 TEST(ValuesTest, ConstructInt) { 32 TEST(ValuesTest, ConstructInt) {
33 FundamentalValue value(-37); 33 Value value(-37);
34 EXPECT_EQ(Value::Type::INTEGER, value.type()); 34 EXPECT_EQ(Value::Type::INTEGER, value.type());
35 EXPECT_EQ(-37, value.GetInt()); 35 EXPECT_EQ(-37, value.GetInt());
36 } 36 }
37 37
38 TEST(ValuesTest, ConstructDouble) { 38 TEST(ValuesTest, ConstructDouble) {
39 FundamentalValue value(-4.655); 39 Value value(-4.655);
40 EXPECT_EQ(Value::Type::DOUBLE, value.type()); 40 EXPECT_EQ(Value::Type::DOUBLE, value.type());
41 EXPECT_EQ(-4.655, value.GetDouble()); 41 EXPECT_EQ(-4.655, value.GetDouble());
42 } 42 }
43 43
44 TEST(ValuesTest, ConstructStringFromConstCharPtr) { 44 TEST(ValuesTest, ConstructStringFromConstCharPtr) {
45 const char* str = "foobar"; 45 const char* str = "foobar";
46 StringValue value(str); 46 StringValue value(str);
47 EXPECT_EQ(Value::Type::STRING, value.type()); 47 EXPECT_EQ(Value::Type::STRING, value.type());
48 EXPECT_EQ("foobar", value.GetString()); 48 EXPECT_EQ("foobar", value.GetString());
49 } 49 }
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
96 96
97 TEST(ValuesTest, ConstructList) { 97 TEST(ValuesTest, ConstructList) {
98 ListValue value; 98 ListValue value;
99 EXPECT_EQ(Value::Type::LIST, value.type()); 99 EXPECT_EQ(Value::Type::LIST, value.type());
100 } 100 }
101 101
102 // Group of tests for the copy constructors and copy-assigmnent. For equality 102 // Group of tests for the copy constructors and copy-assigmnent. For equality
103 // checks comparisons of the interesting fields are done instead of relying on 103 // checks comparisons of the interesting fields are done instead of relying on
104 // Equals being correct. 104 // Equals being correct.
105 TEST(ValuesTest, CopyBool) { 105 TEST(ValuesTest, CopyBool) {
106 FundamentalValue true_value(true); 106 Value true_value(true);
107 FundamentalValue copied_true_value(true_value); 107 Value copied_true_value(true_value);
108 EXPECT_EQ(true_value.type(), copied_true_value.type()); 108 EXPECT_EQ(true_value.type(), copied_true_value.type());
109 EXPECT_EQ(true_value.GetBool(), copied_true_value.GetBool()); 109 EXPECT_EQ(true_value.GetBool(), copied_true_value.GetBool());
110 110
111 FundamentalValue false_value(false); 111 Value false_value(false);
112 FundamentalValue copied_false_value(false_value); 112 Value copied_false_value(false_value);
113 EXPECT_EQ(false_value.type(), copied_false_value.type()); 113 EXPECT_EQ(false_value.type(), copied_false_value.type());
114 EXPECT_EQ(false_value.GetBool(), copied_false_value.GetBool()); 114 EXPECT_EQ(false_value.GetBool(), copied_false_value.GetBool());
115 115
116 Value blank; 116 Value blank;
117 117
118 blank = true_value; 118 blank = true_value;
119 EXPECT_EQ(true_value.type(), blank.type()); 119 EXPECT_EQ(true_value.type(), blank.type());
120 EXPECT_EQ(true_value.GetBool(), blank.GetBool()); 120 EXPECT_EQ(true_value.GetBool(), blank.GetBool());
121 121
122 blank = false_value; 122 blank = false_value;
123 EXPECT_EQ(false_value.type(), blank.type()); 123 EXPECT_EQ(false_value.type(), blank.type());
124 EXPECT_EQ(false_value.GetBool(), blank.GetBool()); 124 EXPECT_EQ(false_value.GetBool(), blank.GetBool());
125 } 125 }
126 126
127 TEST(ValuesTest, CopyInt) { 127 TEST(ValuesTest, CopyInt) {
128 FundamentalValue value(74); 128 Value value(74);
129 FundamentalValue copied_value(value); 129 Value copied_value(value);
130 EXPECT_EQ(value.type(), copied_value.type()); 130 EXPECT_EQ(value.type(), copied_value.type());
131 EXPECT_EQ(value.GetInt(), copied_value.GetInt()); 131 EXPECT_EQ(value.GetInt(), copied_value.GetInt());
132 132
133 Value blank; 133 Value blank;
134 134
135 blank = value; 135 blank = value;
136 EXPECT_EQ(value.type(), blank.type()); 136 EXPECT_EQ(value.type(), blank.type());
137 EXPECT_EQ(value.GetInt(), blank.GetInt()); 137 EXPECT_EQ(value.GetInt(), blank.GetInt());
138 } 138 }
139 139
140 TEST(ValuesTest, CopyDouble) { 140 TEST(ValuesTest, CopyDouble) {
141 FundamentalValue value(74.896); 141 Value value(74.896);
142 FundamentalValue copied_value(value); 142 Value copied_value(value);
143 EXPECT_EQ(value.type(), copied_value.type()); 143 EXPECT_EQ(value.type(), copied_value.type());
144 EXPECT_EQ(value.GetDouble(), copied_value.GetDouble()); 144 EXPECT_EQ(value.GetDouble(), copied_value.GetDouble());
145 145
146 Value blank; 146 Value blank;
147 147
148 blank = value; 148 blank = value;
149 EXPECT_EQ(value.type(), blank.type()); 149 EXPECT_EQ(value.type(), blank.type());
150 EXPECT_EQ(value.GetDouble(), blank.GetDouble()); 150 EXPECT_EQ(value.GetDouble(), blank.GetDouble());
151 } 151 }
152 152
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after
215 215
216 *blank = value; 216 *blank = value;
217 EXPECT_EQ(Value::Type::LIST, blank->type()); 217 EXPECT_EQ(Value::Type::LIST, blank->type());
218 218
219 static_cast<ListValue*>(blank.get())->GetInteger(0, &copy); 219 static_cast<ListValue*>(blank.get())->GetInteger(0, &copy);
220 EXPECT_EQ(123, copy); 220 EXPECT_EQ(123, copy);
221 } 221 }
222 222
223 // Group of tests for the move constructors and move-assigmnent. 223 // Group of tests for the move constructors and move-assigmnent.
224 TEST(ValuesTest, MoveBool) { 224 TEST(ValuesTest, MoveBool) {
225 FundamentalValue true_value(true); 225 Value true_value(true);
226 FundamentalValue moved_true_value(std::move(true_value)); 226 Value moved_true_value(std::move(true_value));
227 EXPECT_EQ(Value::Type::BOOLEAN, moved_true_value.type()); 227 EXPECT_EQ(Value::Type::BOOLEAN, moved_true_value.type());
228 EXPECT_TRUE(moved_true_value.GetBool()); 228 EXPECT_TRUE(moved_true_value.GetBool());
229 229
230 FundamentalValue false_value(false); 230 Value false_value(false);
231 FundamentalValue moved_false_value(std::move(false_value)); 231 Value moved_false_value(std::move(false_value));
232 EXPECT_EQ(Value::Type::BOOLEAN, moved_false_value.type()); 232 EXPECT_EQ(Value::Type::BOOLEAN, moved_false_value.type());
233 EXPECT_FALSE(moved_false_value.GetBool()); 233 EXPECT_FALSE(moved_false_value.GetBool());
234 234
235 Value blank; 235 Value blank;
236 236
237 blank = FundamentalValue(true); 237 blank = Value(true);
238 EXPECT_EQ(Value::Type::BOOLEAN, blank.type()); 238 EXPECT_EQ(Value::Type::BOOLEAN, blank.type());
239 EXPECT_TRUE(blank.GetBool()); 239 EXPECT_TRUE(blank.GetBool());
240 240
241 blank = FundamentalValue(false); 241 blank = Value(false);
242 EXPECT_EQ(Value::Type::BOOLEAN, blank.type()); 242 EXPECT_EQ(Value::Type::BOOLEAN, blank.type());
243 EXPECT_FALSE(blank.GetBool()); 243 EXPECT_FALSE(blank.GetBool());
244 } 244 }
245 245
246 TEST(ValuesTest, MoveInt) { 246 TEST(ValuesTest, MoveInt) {
247 FundamentalValue value(74); 247 Value value(74);
248 FundamentalValue moved_value(std::move(value)); 248 Value moved_value(std::move(value));
249 EXPECT_EQ(Value::Type::INTEGER, moved_value.type()); 249 EXPECT_EQ(Value::Type::INTEGER, moved_value.type());
250 EXPECT_EQ(74, moved_value.GetInt()); 250 EXPECT_EQ(74, moved_value.GetInt());
251 251
252 Value blank; 252 Value blank;
253 253
254 blank = FundamentalValue(47); 254 blank = Value(47);
255 EXPECT_EQ(Value::Type::INTEGER, blank.type()); 255 EXPECT_EQ(Value::Type::INTEGER, blank.type());
256 EXPECT_EQ(47, blank.GetInt()); 256 EXPECT_EQ(47, blank.GetInt());
257 } 257 }
258 258
259 TEST(ValuesTest, MoveDouble) { 259 TEST(ValuesTest, MoveDouble) {
260 FundamentalValue value(74.896); 260 Value value(74.896);
261 FundamentalValue moved_value(std::move(value)); 261 Value moved_value(std::move(value));
262 EXPECT_EQ(Value::Type::DOUBLE, moved_value.type()); 262 EXPECT_EQ(Value::Type::DOUBLE, moved_value.type());
263 EXPECT_EQ(74.896, moved_value.GetDouble()); 263 EXPECT_EQ(74.896, moved_value.GetDouble());
264 264
265 Value blank; 265 Value blank;
266 266
267 blank = FundamentalValue(654.38); 267 blank = Value(654.38);
268 EXPECT_EQ(Value::Type::DOUBLE, blank.type()); 268 EXPECT_EQ(Value::Type::DOUBLE, blank.type());
269 EXPECT_EQ(654.38, blank.GetDouble()); 269 EXPECT_EQ(654.38, blank.GetDouble());
270 } 270 }
271 271
272 TEST(ValuesTest, MoveString) { 272 TEST(ValuesTest, MoveString) {
273 StringValue value("foobar"); 273 StringValue value("foobar");
274 StringValue moved_value(std::move(value)); 274 StringValue moved_value(std::move(value));
275 EXPECT_EQ(Value::Type::STRING, moved_value.type()); 275 EXPECT_EQ(Value::Type::STRING, moved_value.type());
276 EXPECT_EQ("foobar", moved_value.GetString()); 276 EXPECT_EQ("foobar", moved_value.GetString());
277 277
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after
372 std::string bookmark_name = "Unnamed"; 372 std::string bookmark_name = "Unnamed";
373 ASSERT_TRUE(bookmark->GetString("name", &bookmark_name)); 373 ASSERT_TRUE(bookmark->GetString("name", &bookmark_name));
374 ASSERT_EQ(std::string("Froogle"), bookmark_name); 374 ASSERT_EQ(std::string("Froogle"), bookmark_name);
375 std::string bookmark_url; 375 std::string bookmark_url;
376 ASSERT_TRUE(bookmark->GetString("url", &bookmark_url)); 376 ASSERT_TRUE(bookmark->GetString("url", &bookmark_url));
377 ASSERT_EQ(std::string("http://froogle.com"), bookmark_url); 377 ASSERT_EQ(std::string("http://froogle.com"), bookmark_url);
378 } 378 }
379 379
380 TEST(ValuesTest, List) { 380 TEST(ValuesTest, List) {
381 std::unique_ptr<ListValue> mixed_list(new ListValue()); 381 std::unique_ptr<ListValue> mixed_list(new ListValue());
382 mixed_list->Set(0, MakeUnique<FundamentalValue>(true)); 382 mixed_list->Set(0, MakeUnique<Value>(true));
383 mixed_list->Set(1, MakeUnique<FundamentalValue>(42)); 383 mixed_list->Set(1, MakeUnique<Value>(42));
384 mixed_list->Set(2, MakeUnique<FundamentalValue>(88.8)); 384 mixed_list->Set(2, MakeUnique<Value>(88.8));
385 mixed_list->Set(3, MakeUnique<StringValue>("foo")); 385 mixed_list->Set(3, MakeUnique<StringValue>("foo"));
386 ASSERT_EQ(4u, mixed_list->GetSize()); 386 ASSERT_EQ(4u, mixed_list->GetSize());
387 387
388 Value *value = NULL; 388 Value *value = NULL;
389 bool bool_value = false; 389 bool bool_value = false;
390 int int_value = 0; 390 int int_value = 0;
391 double double_value = 0.0; 391 double double_value = 0.0;
392 std::string string_value; 392 std::string string_value;
393 393
394 ASSERT_FALSE(mixed_list->Get(4, &value)); 394 ASSERT_FALSE(mixed_list->Get(4, &value));
(...skipping 15 matching lines...) Expand all
410 ASSERT_EQ(42, int_value); 410 ASSERT_EQ(42, int_value);
411 // implicit conversion from Integer to Double should be possible. 411 // implicit conversion from Integer to Double should be possible.
412 ASSERT_TRUE(mixed_list->GetDouble(1, &double_value)); 412 ASSERT_TRUE(mixed_list->GetDouble(1, &double_value));
413 ASSERT_EQ(42, double_value); 413 ASSERT_EQ(42, double_value);
414 ASSERT_TRUE(mixed_list->GetDouble(2, &double_value)); 414 ASSERT_TRUE(mixed_list->GetDouble(2, &double_value));
415 ASSERT_EQ(88.8, double_value); 415 ASSERT_EQ(88.8, double_value);
416 ASSERT_TRUE(mixed_list->GetString(3, &string_value)); 416 ASSERT_TRUE(mixed_list->GetString(3, &string_value));
417 ASSERT_EQ("foo", string_value); 417 ASSERT_EQ("foo", string_value);
418 418
419 // Try searching in the mixed list. 419 // Try searching in the mixed list.
420 base::FundamentalValue sought_value(42); 420 base::Value sought_value(42);
421 base::FundamentalValue not_found_value(false); 421 base::Value not_found_value(false);
422 422
423 ASSERT_NE(mixed_list->end(), mixed_list->Find(sought_value)); 423 ASSERT_NE(mixed_list->end(), mixed_list->Find(sought_value));
424 ASSERT_TRUE((*mixed_list->Find(sought_value))->GetAsInteger(&int_value)); 424 ASSERT_TRUE((*mixed_list->Find(sought_value))->GetAsInteger(&int_value));
425 ASSERT_EQ(42, int_value); 425 ASSERT_EQ(42, int_value);
426 ASSERT_EQ(mixed_list->end(), mixed_list->Find(not_found_value)); 426 ASSERT_EQ(mixed_list->end(), mixed_list->Find(not_found_value));
427 } 427 }
428 428
429 TEST(ValuesTest, BinaryValue) { 429 TEST(ValuesTest, BinaryValue) {
430 // Default constructor creates a BinaryValue with a buffer of size 0. 430 // Default constructor creates a BinaryValue with a buffer of size 0.
431 auto binary = MakeUnique<Value>(Value::Type::BINARY); 431 auto binary = MakeUnique<Value>(Value::Type::BINARY);
(...skipping 203 matching lines...) Expand 10 before | Expand all | Expand 10 after
635 ASSERT_TRUE(removed_item); 635 ASSERT_TRUE(removed_item);
636 EXPECT_TRUE(removed_item->IsType(base::Value::Type::BOOLEAN)); 636 EXPECT_TRUE(removed_item->IsType(base::Value::Type::BOOLEAN));
637 EXPECT_TRUE(dict.empty()); 637 EXPECT_TRUE(dict.empty());
638 } 638 }
639 639
640 TEST(ValuesTest, DeepCopy) { 640 TEST(ValuesTest, DeepCopy) {
641 DictionaryValue original_dict; 641 DictionaryValue original_dict;
642 std::unique_ptr<Value> scoped_null = Value::CreateNullValue(); 642 std::unique_ptr<Value> scoped_null = Value::CreateNullValue();
643 Value* original_null = scoped_null.get(); 643 Value* original_null = scoped_null.get();
644 original_dict.Set("null", std::move(scoped_null)); 644 original_dict.Set("null", std::move(scoped_null));
645 std::unique_ptr<FundamentalValue> scoped_bool(new FundamentalValue(true)); 645 std::unique_ptr<Value> scoped_bool(new Value(true));
646 FundamentalValue* original_bool = scoped_bool.get(); 646 Value* original_bool = scoped_bool.get();
647 original_dict.Set("bool", std::move(scoped_bool)); 647 original_dict.Set("bool", std::move(scoped_bool));
648 std::unique_ptr<FundamentalValue> scoped_int(new FundamentalValue(42)); 648 std::unique_ptr<Value> scoped_int(new Value(42));
649 FundamentalValue* original_int = scoped_int.get(); 649 Value* original_int = scoped_int.get();
650 original_dict.Set("int", std::move(scoped_int)); 650 original_dict.Set("int", std::move(scoped_int));
651 std::unique_ptr<FundamentalValue> scoped_double(new FundamentalValue(3.14)); 651 std::unique_ptr<Value> scoped_double(new Value(3.14));
652 FundamentalValue* original_double = scoped_double.get(); 652 Value* original_double = scoped_double.get();
653 original_dict.Set("double", std::move(scoped_double)); 653 original_dict.Set("double", std::move(scoped_double));
654 std::unique_ptr<StringValue> scoped_string(new StringValue("hello")); 654 std::unique_ptr<StringValue> scoped_string(new StringValue("hello"));
655 StringValue* original_string = scoped_string.get(); 655 StringValue* original_string = scoped_string.get();
656 original_dict.Set("string", std::move(scoped_string)); 656 original_dict.Set("string", std::move(scoped_string));
657 std::unique_ptr<StringValue> scoped_string16( 657 std::unique_ptr<StringValue> scoped_string16(
658 new StringValue(ASCIIToUTF16("hello16"))); 658 new StringValue(ASCIIToUTF16("hello16")));
659 StringValue* original_string16 = scoped_string16.get(); 659 StringValue* original_string16 = scoped_string16.get();
660 original_dict.Set("string16", std::move(scoped_string16)); 660 original_dict.Set("string16", std::move(scoped_string16));
661 661
662 std::vector<char> original_buffer(42, '!'); 662 std::vector<char> original_buffer(42, '!');
663 std::unique_ptr<BinaryValue> scoped_binary( 663 std::unique_ptr<BinaryValue> scoped_binary(
664 new BinaryValue(std::move(original_buffer))); 664 new BinaryValue(std::move(original_buffer)));
665 BinaryValue* original_binary = scoped_binary.get(); 665 BinaryValue* original_binary = scoped_binary.get();
666 original_dict.Set("binary", std::move(scoped_binary)); 666 original_dict.Set("binary", std::move(scoped_binary));
667 667
668 std::unique_ptr<ListValue> scoped_list(new ListValue()); 668 std::unique_ptr<ListValue> scoped_list(new ListValue());
669 Value* original_list = scoped_list.get(); 669 Value* original_list = scoped_list.get();
670 std::unique_ptr<FundamentalValue> scoped_list_element_0( 670 std::unique_ptr<Value> scoped_list_element_0(new Value(0));
671 new FundamentalValue(0));
672 Value* original_list_element_0 = scoped_list_element_0.get(); 671 Value* original_list_element_0 = scoped_list_element_0.get();
673 scoped_list->Append(std::move(scoped_list_element_0)); 672 scoped_list->Append(std::move(scoped_list_element_0));
674 std::unique_ptr<FundamentalValue> scoped_list_element_1( 673 std::unique_ptr<Value> scoped_list_element_1(new Value(1));
675 new FundamentalValue(1));
676 Value* original_list_element_1 = scoped_list_element_1.get(); 674 Value* original_list_element_1 = scoped_list_element_1.get();
677 scoped_list->Append(std::move(scoped_list_element_1)); 675 scoped_list->Append(std::move(scoped_list_element_1));
678 original_dict.Set("list", std::move(scoped_list)); 676 original_dict.Set("list", std::move(scoped_list));
679 677
680 std::unique_ptr<DictionaryValue> scoped_nested_dictionary( 678 std::unique_ptr<DictionaryValue> scoped_nested_dictionary(
681 new DictionaryValue()); 679 new DictionaryValue());
682 Value* original_nested_dictionary = scoped_nested_dictionary.get(); 680 Value* original_nested_dictionary = scoped_nested_dictionary.get();
683 scoped_nested_dictionary->SetString("key", "value"); 681 scoped_nested_dictionary->SetString("key", "value");
684 original_dict.Set("dictionary", std::move(scoped_nested_dictionary)); 682 original_dict.Set("dictionary", std::move(scoped_nested_dictionary));
685 683
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after
788 ASSERT_TRUE(copy_nested_dictionary); 786 ASSERT_TRUE(copy_nested_dictionary);
789 EXPECT_TRUE(copy_nested_dictionary->HasKey("key")); 787 EXPECT_TRUE(copy_nested_dictionary->HasKey("key"));
790 } 788 }
791 789
792 TEST(ValuesTest, Equals) { 790 TEST(ValuesTest, Equals) {
793 std::unique_ptr<Value> null1(Value::CreateNullValue()); 791 std::unique_ptr<Value> null1(Value::CreateNullValue());
794 std::unique_ptr<Value> null2(Value::CreateNullValue()); 792 std::unique_ptr<Value> null2(Value::CreateNullValue());
795 EXPECT_NE(null1.get(), null2.get()); 793 EXPECT_NE(null1.get(), null2.get());
796 EXPECT_TRUE(null1->Equals(null2.get())); 794 EXPECT_TRUE(null1->Equals(null2.get()));
797 795
798 FundamentalValue boolean(false); 796 Value boolean(false);
799 EXPECT_FALSE(null1->Equals(&boolean)); 797 EXPECT_FALSE(null1->Equals(&boolean));
800 798
801 DictionaryValue dv; 799 DictionaryValue dv;
802 dv.SetBoolean("a", false); 800 dv.SetBoolean("a", false);
803 dv.SetInteger("b", 2); 801 dv.SetInteger("b", 2);
804 dv.SetDouble("c", 2.5); 802 dv.SetDouble("c", 2.5);
805 dv.SetString("d1", "string"); 803 dv.SetString("d1", "string");
806 dv.SetString("d2", ASCIIToUTF16("http://google.com")); 804 dv.SetString("d2", ASCIIToUTF16("http://google.com"));
807 dv.Set("e", Value::CreateNullValue()); 805 dv.Set("e", Value::CreateNullValue());
808 806
809 std::unique_ptr<DictionaryValue> copy = dv.CreateDeepCopy(); 807 std::unique_ptr<DictionaryValue> copy = dv.CreateDeepCopy();
810 EXPECT_TRUE(dv.Equals(copy.get())); 808 EXPECT_TRUE(dv.Equals(copy.get()));
811 809
812 std::unique_ptr<ListValue> list(new ListValue); 810 std::unique_ptr<ListValue> list(new ListValue);
813 ListValue* original_list = list.get(); 811 ListValue* original_list = list.get();
814 list->Append(Value::CreateNullValue()); 812 list->Append(Value::CreateNullValue());
815 list->Append(WrapUnique(new DictionaryValue)); 813 list->Append(WrapUnique(new DictionaryValue));
816 std::unique_ptr<Value> list_copy(list->CreateDeepCopy()); 814 std::unique_ptr<Value> list_copy(list->CreateDeepCopy());
817 815
818 dv.Set("f", std::move(list)); 816 dv.Set("f", std::move(list));
819 EXPECT_FALSE(dv.Equals(copy.get())); 817 EXPECT_FALSE(dv.Equals(copy.get()));
820 copy->Set("f", std::move(list_copy)); 818 copy->Set("f", std::move(list_copy));
821 EXPECT_TRUE(dv.Equals(copy.get())); 819 EXPECT_TRUE(dv.Equals(copy.get()));
822 820
823 original_list->Append(MakeUnique<FundamentalValue>(true)); 821 original_list->Append(MakeUnique<Value>(true));
824 EXPECT_FALSE(dv.Equals(copy.get())); 822 EXPECT_FALSE(dv.Equals(copy.get()));
825 823
826 // Check if Equals detects differences in only the keys. 824 // Check if Equals detects differences in only the keys.
827 copy = dv.CreateDeepCopy(); 825 copy = dv.CreateDeepCopy();
828 EXPECT_TRUE(dv.Equals(copy.get())); 826 EXPECT_TRUE(dv.Equals(copy.get()));
829 copy->Remove("a", NULL); 827 copy->Remove("a", NULL);
830 copy->SetBoolean("aa", false); 828 copy->SetBoolean("aa", false);
831 EXPECT_FALSE(dv.Equals(copy.get())); 829 EXPECT_FALSE(dv.Equals(copy.get()));
832 } 830 }
833 831
834 TEST(ValuesTest, StaticEquals) { 832 TEST(ValuesTest, StaticEquals) {
835 std::unique_ptr<Value> null1(Value::CreateNullValue()); 833 std::unique_ptr<Value> null1(Value::CreateNullValue());
836 std::unique_ptr<Value> null2(Value::CreateNullValue()); 834 std::unique_ptr<Value> null2(Value::CreateNullValue());
837 EXPECT_TRUE(Value::Equals(null1.get(), null2.get())); 835 EXPECT_TRUE(Value::Equals(null1.get(), null2.get()));
838 EXPECT_TRUE(Value::Equals(NULL, NULL)); 836 EXPECT_TRUE(Value::Equals(NULL, NULL));
839 837
840 std::unique_ptr<Value> i42(new FundamentalValue(42)); 838 std::unique_ptr<Value> i42(new Value(42));
841 std::unique_ptr<Value> j42(new FundamentalValue(42)); 839 std::unique_ptr<Value> j42(new Value(42));
842 std::unique_ptr<Value> i17(new FundamentalValue(17)); 840 std::unique_ptr<Value> i17(new Value(17));
843 EXPECT_TRUE(Value::Equals(i42.get(), i42.get())); 841 EXPECT_TRUE(Value::Equals(i42.get(), i42.get()));
844 EXPECT_TRUE(Value::Equals(j42.get(), i42.get())); 842 EXPECT_TRUE(Value::Equals(j42.get(), i42.get()));
845 EXPECT_TRUE(Value::Equals(i42.get(), j42.get())); 843 EXPECT_TRUE(Value::Equals(i42.get(), j42.get()));
846 EXPECT_FALSE(Value::Equals(i42.get(), i17.get())); 844 EXPECT_FALSE(Value::Equals(i42.get(), i17.get()));
847 EXPECT_FALSE(Value::Equals(i42.get(), NULL)); 845 EXPECT_FALSE(Value::Equals(i42.get(), NULL));
848 EXPECT_FALSE(Value::Equals(NULL, i42.get())); 846 EXPECT_FALSE(Value::Equals(NULL, i42.get()));
849 847
850 // NULL and Value::CreateNullValue() are intentionally different: We need 848 // NULL and Value::CreateNullValue() are intentionally different: We need
851 // support for NULL as a return value for "undefined" without caring for 849 // support for NULL as a return value for "undefined" without caring for
852 // ownership of the pointer. 850 // ownership of the pointer.
853 EXPECT_FALSE(Value::Equals(null1.get(), NULL)); 851 EXPECT_FALSE(Value::Equals(null1.get(), NULL));
854 EXPECT_FALSE(Value::Equals(NULL, null1.get())); 852 EXPECT_FALSE(Value::Equals(NULL, null1.get()));
855 } 853 }
856 854
857 TEST(ValuesTest, DeepCopyCovariantReturnTypes) { 855 TEST(ValuesTest, DeepCopyCovariantReturnTypes) {
858 DictionaryValue original_dict; 856 DictionaryValue original_dict;
859 std::unique_ptr<Value> scoped_null(Value::CreateNullValue()); 857 std::unique_ptr<Value> scoped_null(Value::CreateNullValue());
860 Value* original_null = scoped_null.get(); 858 Value* original_null = scoped_null.get();
861 original_dict.Set("null", std::move(scoped_null)); 859 original_dict.Set("null", std::move(scoped_null));
862 std::unique_ptr<FundamentalValue> scoped_bool(new FundamentalValue(true)); 860 std::unique_ptr<Value> scoped_bool(new Value(true));
863 Value* original_bool = scoped_bool.get(); 861 Value* original_bool = scoped_bool.get();
864 original_dict.Set("bool", std::move(scoped_bool)); 862 original_dict.Set("bool", std::move(scoped_bool));
865 std::unique_ptr<FundamentalValue> scoped_int(new FundamentalValue(42)); 863 std::unique_ptr<Value> scoped_int(new Value(42));
866 Value* original_int = scoped_int.get(); 864 Value* original_int = scoped_int.get();
867 original_dict.Set("int", std::move(scoped_int)); 865 original_dict.Set("int", std::move(scoped_int));
868 std::unique_ptr<FundamentalValue> scoped_double(new FundamentalValue(3.14)); 866 std::unique_ptr<Value> scoped_double(new Value(3.14));
869 Value* original_double = scoped_double.get(); 867 Value* original_double = scoped_double.get();
870 original_dict.Set("double", std::move(scoped_double)); 868 original_dict.Set("double", std::move(scoped_double));
871 std::unique_ptr<StringValue> scoped_string(new StringValue("hello")); 869 std::unique_ptr<StringValue> scoped_string(new StringValue("hello"));
872 Value* original_string = scoped_string.get(); 870 Value* original_string = scoped_string.get();
873 original_dict.Set("string", std::move(scoped_string)); 871 original_dict.Set("string", std::move(scoped_string));
874 std::unique_ptr<StringValue> scoped_string16( 872 std::unique_ptr<StringValue> scoped_string16(
875 new StringValue(ASCIIToUTF16("hello16"))); 873 new StringValue(ASCIIToUTF16("hello16")));
876 Value* original_string16 = scoped_string16.get(); 874 Value* original_string16 = scoped_string16.get();
877 original_dict.Set("string16", std::move(scoped_string16)); 875 original_dict.Set("string16", std::move(scoped_string16));
878 876
879 std::vector<char> original_buffer(42, '!'); 877 std::vector<char> original_buffer(42, '!');
880 std::unique_ptr<BinaryValue> scoped_binary( 878 std::unique_ptr<BinaryValue> scoped_binary(
881 new BinaryValue(std::move(original_buffer))); 879 new BinaryValue(std::move(original_buffer)));
882 Value* original_binary = scoped_binary.get(); 880 Value* original_binary = scoped_binary.get();
883 original_dict.Set("binary", std::move(scoped_binary)); 881 original_dict.Set("binary", std::move(scoped_binary));
884 882
885 std::unique_ptr<ListValue> scoped_list(new ListValue()); 883 std::unique_ptr<ListValue> scoped_list(new ListValue());
886 Value* original_list = scoped_list.get(); 884 Value* original_list = scoped_list.get();
887 std::unique_ptr<FundamentalValue> scoped_list_element_0( 885 std::unique_ptr<Value> scoped_list_element_0(new Value(0));
888 new FundamentalValue(0));
889 scoped_list->Append(std::move(scoped_list_element_0)); 886 scoped_list->Append(std::move(scoped_list_element_0));
890 std::unique_ptr<FundamentalValue> scoped_list_element_1( 887 std::unique_ptr<Value> scoped_list_element_1(new Value(1));
891 new FundamentalValue(1));
892 scoped_list->Append(std::move(scoped_list_element_1)); 888 scoped_list->Append(std::move(scoped_list_element_1));
893 original_dict.Set("list", std::move(scoped_list)); 889 original_dict.Set("list", std::move(scoped_list));
894 890
895 std::unique_ptr<Value> copy_dict = original_dict.CreateDeepCopy(); 891 std::unique_ptr<Value> copy_dict = original_dict.CreateDeepCopy();
896 std::unique_ptr<Value> copy_null = original_null->CreateDeepCopy(); 892 std::unique_ptr<Value> copy_null = original_null->CreateDeepCopy();
897 std::unique_ptr<Value> copy_bool = original_bool->CreateDeepCopy(); 893 std::unique_ptr<Value> copy_bool = original_bool->CreateDeepCopy();
898 std::unique_ptr<Value> copy_int = original_int->CreateDeepCopy(); 894 std::unique_ptr<Value> copy_int = original_int->CreateDeepCopy();
899 std::unique_ptr<Value> copy_double = original_double->CreateDeepCopy(); 895 std::unique_ptr<Value> copy_double = original_double->CreateDeepCopy();
900 std::unique_ptr<Value> copy_string = original_string->CreateDeepCopy(); 896 std::unique_ptr<Value> copy_string = original_string->CreateDeepCopy();
901 std::unique_ptr<Value> copy_string16 = original_string16->CreateDeepCopy(); 897 std::unique_ptr<Value> copy_string16 = original_string16->CreateDeepCopy();
(...skipping 202 matching lines...) Expand 10 before | Expand all | Expand 10 after
1104 EXPECT_TRUE(seen1); 1100 EXPECT_TRUE(seen1);
1105 EXPECT_TRUE(seen2); 1101 EXPECT_TRUE(seen2);
1106 } 1102 }
1107 1103
1108 // DictionaryValue/ListValue's Get*() methods should accept NULL as an out-value 1104 // DictionaryValue/ListValue's Get*() methods should accept NULL as an out-value
1109 // and still return true/false based on success. 1105 // and still return true/false based on success.
1110 TEST(ValuesTest, GetWithNullOutValue) { 1106 TEST(ValuesTest, GetWithNullOutValue) {
1111 DictionaryValue main_dict; 1107 DictionaryValue main_dict;
1112 ListValue main_list; 1108 ListValue main_list;
1113 1109
1114 FundamentalValue bool_value(false); 1110 Value bool_value(false);
1115 FundamentalValue int_value(1234); 1111 Value int_value(1234);
1116 FundamentalValue double_value(12.34567); 1112 Value double_value(12.34567);
1117 StringValue string_value("foo"); 1113 StringValue string_value("foo");
1118 BinaryValue binary_value(Value::Type::BINARY); 1114 BinaryValue binary_value(Value::Type::BINARY);
1119 DictionaryValue dict_value; 1115 DictionaryValue dict_value;
1120 ListValue list_value; 1116 ListValue list_value;
1121 1117
1122 main_dict.Set("bool", bool_value.CreateDeepCopy()); 1118 main_dict.Set("bool", bool_value.CreateDeepCopy());
1123 main_dict.Set("int", int_value.CreateDeepCopy()); 1119 main_dict.Set("int", int_value.CreateDeepCopy());
1124 main_dict.Set("double", double_value.CreateDeepCopy()); 1120 main_dict.Set("double", double_value.CreateDeepCopy());
1125 main_dict.Set("string", string_value.CreateDeepCopy()); 1121 main_dict.Set("string", string_value.CreateDeepCopy());
1126 main_dict.Set("binary", binary_value.CreateDeepCopy()); 1122 main_dict.Set("binary", binary_value.CreateDeepCopy());
(...skipping 257 matching lines...) Expand 10 before | Expand all | Expand 10 after
1384 EXPECT_FALSE(main_list.GetList(1, NULL)); 1380 EXPECT_FALSE(main_list.GetList(1, NULL));
1385 EXPECT_FALSE(main_list.GetList(2, NULL)); 1381 EXPECT_FALSE(main_list.GetList(2, NULL));
1386 EXPECT_FALSE(main_list.GetList(3, NULL)); 1382 EXPECT_FALSE(main_list.GetList(3, NULL));
1387 EXPECT_FALSE(main_list.GetList(4, NULL)); 1383 EXPECT_FALSE(main_list.GetList(4, NULL));
1388 EXPECT_FALSE(main_list.GetList(5, NULL)); 1384 EXPECT_FALSE(main_list.GetList(5, NULL));
1389 EXPECT_TRUE(main_list.GetList(6, NULL)); 1385 EXPECT_TRUE(main_list.GetList(6, NULL));
1390 EXPECT_FALSE(main_list.GetList(7, NULL)); 1386 EXPECT_FALSE(main_list.GetList(7, NULL));
1391 } 1387 }
1392 1388
1393 } // namespace base 1389 } // namespace base
OLDNEW
« no previous file with comments | « base/values.cc ('k') | chrome/browser/android/search_geolocation/search_geolocation_service_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698