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

Side by Side Diff: base/values_unittest.cc

Issue 7647026: base: Add three helper functions to Values API. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: fix a typo Ceate -> Create Created 9 years, 4 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 | Annotate | Revision Log
« no previous file with comments | « base/values.cc ('k') | chrome/browser/automation/automation_provider_observers.cc » ('j') | 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) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 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 <limits> 5 #include <limits>
6 6
7 #include "base/memory/scoped_ptr.h" 7 #include "base/memory/scoped_ptr.h"
8 #include "base/string16.h" 8 #include "base/string16.h"
9 #include "base/utf_string_conversions.h" 9 #include "base/utf_string_conversions.h"
10 #include "base/values.h" 10 #include "base/values.h"
11 #include "testing/gtest/include/gtest/gtest.h" 11 #include "testing/gtest/include/gtest/gtest.h"
12 12
13 namespace base { 13 namespace base {
14 14
15 TEST(ValuesTest, Basic) { 15 TEST(ValuesTest, Basic) {
16 // Test basic dictionary getting/setting 16 // Test basic dictionary getting/setting
17 DictionaryValue settings; 17 DictionaryValue settings;
18 std::string homepage = "http://google.com"; 18 std::string homepage = "http://google.com";
19 ASSERT_FALSE(settings.GetString("global.homepage", &homepage)); 19 ASSERT_FALSE(settings.GetString("global.homepage", &homepage));
20 ASSERT_EQ(std::string("http://google.com"), homepage); 20 ASSERT_EQ(std::string("http://google.com"), homepage);
21 21
22 ASSERT_FALSE(settings.Get("global", NULL)); 22 ASSERT_FALSE(settings.Get("global", NULL));
23 settings.Set("global", Value::CreateBooleanValue(true)); 23 settings.Set("global", TrueValue());
24 ASSERT_TRUE(settings.Get("global", NULL)); 24 ASSERT_TRUE(settings.Get("global", NULL));
25 settings.SetString("global.homepage", "http://scurvy.com"); 25 settings.SetString("global.homepage", "http://scurvy.com");
26 ASSERT_TRUE(settings.Get("global", NULL)); 26 ASSERT_TRUE(settings.Get("global", NULL));
27 homepage = "http://google.com"; 27 homepage = "http://google.com";
28 ASSERT_TRUE(settings.GetString("global.homepage", &homepage)); 28 ASSERT_TRUE(settings.GetString("global.homepage", &homepage));
29 ASSERT_EQ(std::string("http://scurvy.com"), homepage); 29 ASSERT_EQ(std::string("http://scurvy.com"), homepage);
30 30
31 // Test storing a dictionary in a list. 31 // Test storing a dictionary in a list.
32 ListValue* toolbar_bookmarks; 32 ListValue* toolbar_bookmarks;
33 ASSERT_FALSE( 33 ASSERT_FALSE(
(...skipping 16 matching lines...) Expand all
50 std::string bookmark_name = "Unnamed"; 50 std::string bookmark_name = "Unnamed";
51 ASSERT_TRUE(bookmark->GetString("name", &bookmark_name)); 51 ASSERT_TRUE(bookmark->GetString("name", &bookmark_name));
52 ASSERT_EQ(std::string("Froogle"), bookmark_name); 52 ASSERT_EQ(std::string("Froogle"), bookmark_name);
53 std::string bookmark_url; 53 std::string bookmark_url;
54 ASSERT_TRUE(bookmark->GetString("url", &bookmark_url)); 54 ASSERT_TRUE(bookmark->GetString("url", &bookmark_url));
55 ASSERT_EQ(std::string("http://froogle.com"), bookmark_url); 55 ASSERT_EQ(std::string("http://froogle.com"), bookmark_url);
56 } 56 }
57 57
58 TEST(ValuesTest, List) { 58 TEST(ValuesTest, List) {
59 scoped_ptr<ListValue> mixed_list(new ListValue()); 59 scoped_ptr<ListValue> mixed_list(new ListValue());
60 mixed_list->Set(0, Value::CreateBooleanValue(true)); 60 mixed_list->Set(0, TrueValue());
61 mixed_list->Set(1, Value::CreateIntegerValue(42)); 61 mixed_list->Set(1, Value::CreateIntegerValue(42));
62 mixed_list->Set(2, Value::CreateDoubleValue(88.8)); 62 mixed_list->Set(2, Value::CreateDoubleValue(88.8));
63 mixed_list->Set(3, Value::CreateStringValue("foo")); 63 mixed_list->Set(3, Value::CreateStringValue("foo"));
64 ASSERT_EQ(4u, mixed_list->GetSize()); 64 ASSERT_EQ(4u, mixed_list->GetSize());
65 65
66 Value *value = NULL; 66 Value *value = NULL;
67 bool bool_value = false; 67 bool bool_value = false;
68 int int_value = 0; 68 int int_value = 0;
69 double double_value = 0.0; 69 double double_value = 0.0;
70 std::string string_value; 70 std::string string_value;
(...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after
189 list.Append(new DeletionTestValue(&deletion_flag)); 189 list.Append(new DeletionTestValue(&deletion_flag));
190 EXPECT_FALSE(deletion_flag); 190 EXPECT_FALSE(deletion_flag);
191 list.Clear(); 191 list.Clear();
192 EXPECT_TRUE(deletion_flag); 192 EXPECT_TRUE(deletion_flag);
193 } 193 }
194 194
195 { 195 {
196 ListValue list; 196 ListValue list;
197 list.Append(new DeletionTestValue(&deletion_flag)); 197 list.Append(new DeletionTestValue(&deletion_flag));
198 EXPECT_FALSE(deletion_flag); 198 EXPECT_FALSE(deletion_flag);
199 EXPECT_TRUE(list.Set(0, Value::CreateNullValue())); 199 EXPECT_TRUE(list.Set(0, NullValue()));
200 EXPECT_TRUE(deletion_flag); 200 EXPECT_TRUE(deletion_flag);
201 } 201 }
202 } 202 }
203 203
204 TEST(ValuesTest, ListRemoval) { 204 TEST(ValuesTest, ListRemoval) {
205 bool deletion_flag = true; 205 bool deletion_flag = true;
206 Value* removed_item = NULL; 206 Value* removed_item = NULL;
207 207
208 { 208 {
209 ListValue list; 209 ListValue list;
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
260 dict.Set(key, new DeletionTestValue(&deletion_flag)); 260 dict.Set(key, new DeletionTestValue(&deletion_flag));
261 EXPECT_FALSE(deletion_flag); 261 EXPECT_FALSE(deletion_flag);
262 dict.Clear(); 262 dict.Clear();
263 EXPECT_TRUE(deletion_flag); 263 EXPECT_TRUE(deletion_flag);
264 } 264 }
265 265
266 { 266 {
267 DictionaryValue dict; 267 DictionaryValue dict;
268 dict.Set(key, new DeletionTestValue(&deletion_flag)); 268 dict.Set(key, new DeletionTestValue(&deletion_flag));
269 EXPECT_FALSE(deletion_flag); 269 EXPECT_FALSE(deletion_flag);
270 dict.Set(key, Value::CreateNullValue()); 270 dict.Set(key, NullValue());
271 EXPECT_TRUE(deletion_flag); 271 EXPECT_TRUE(deletion_flag);
272 } 272 }
273 } 273 }
274 274
275 TEST(ValuesTest, DictionaryRemoval) { 275 TEST(ValuesTest, DictionaryRemoval) {
276 std::string key = "test"; 276 std::string key = "test";
277 bool deletion_flag = true; 277 bool deletion_flag = true;
278 Value* removed_item = NULL; 278 Value* removed_item = NULL;
279 279
280 { 280 {
(...skipping 17 matching lines...) Expand all
298 EXPECT_FALSE(deletion_flag); 298 EXPECT_FALSE(deletion_flag);
299 EXPECT_TRUE(dict.HasKey(key)); 299 EXPECT_TRUE(dict.HasKey(key));
300 EXPECT_TRUE(dict.Remove(key, NULL)); 300 EXPECT_TRUE(dict.Remove(key, NULL));
301 EXPECT_TRUE(deletion_flag); 301 EXPECT_TRUE(deletion_flag);
302 EXPECT_FALSE(dict.HasKey(key)); 302 EXPECT_FALSE(dict.HasKey(key));
303 } 303 }
304 } 304 }
305 305
306 TEST(ValuesTest, DictionaryWithoutPathExpansion) { 306 TEST(ValuesTest, DictionaryWithoutPathExpansion) {
307 DictionaryValue dict; 307 DictionaryValue dict;
308 dict.Set("this.is.expanded", Value::CreateNullValue()); 308 dict.Set("this.is.expanded", NullValue());
309 dict.SetWithoutPathExpansion("this.isnt.expanded", Value::CreateNullValue()); 309 dict.SetWithoutPathExpansion("this.isnt.expanded", NullValue());
310 310
311 EXPECT_FALSE(dict.HasKey("this.is.expanded")); 311 EXPECT_FALSE(dict.HasKey("this.is.expanded"));
312 EXPECT_TRUE(dict.HasKey("this")); 312 EXPECT_TRUE(dict.HasKey("this"));
313 Value* value1; 313 Value* value1;
314 EXPECT_TRUE(dict.Get("this", &value1)); 314 EXPECT_TRUE(dict.Get("this", &value1));
315 DictionaryValue* value2; 315 DictionaryValue* value2;
316 ASSERT_TRUE(dict.GetDictionaryWithoutPathExpansion("this", &value2)); 316 ASSERT_TRUE(dict.GetDictionaryWithoutPathExpansion("this", &value2));
317 EXPECT_EQ(value1, value2); 317 EXPECT_EQ(value1, value2);
318 EXPECT_EQ(1U, value2->size()); 318 EXPECT_EQ(1U, value2->size());
319 319
320 EXPECT_TRUE(dict.HasKey("this.isnt.expanded")); 320 EXPECT_TRUE(dict.HasKey("this.isnt.expanded"));
321 Value* value3; 321 Value* value3;
322 EXPECT_FALSE(dict.Get("this.isnt.expanded", &value3)); 322 EXPECT_FALSE(dict.Get("this.isnt.expanded", &value3));
323 Value* value4; 323 Value* value4;
324 ASSERT_TRUE(dict.GetWithoutPathExpansion("this.isnt.expanded", &value4)); 324 ASSERT_TRUE(dict.GetWithoutPathExpansion("this.isnt.expanded", &value4));
325 EXPECT_EQ(Value::TYPE_NULL, value4->GetType()); 325 EXPECT_EQ(Value::TYPE_NULL, value4->GetType());
326 } 326 }
327 327
328 TEST(ValuesTest, DeepCopy) { 328 TEST(ValuesTest, DeepCopy) {
329 DictionaryValue original_dict; 329 DictionaryValue original_dict;
330 Value* original_null = Value::CreateNullValue(); 330 Value* original_null = NullValue();
331 original_dict.Set("null", original_null); 331 original_dict.Set("null", original_null);
332 FundamentalValue* original_bool = Value::CreateBooleanValue(true); 332 FundamentalValue* original_bool = TrueValue();
333 original_dict.Set("bool", original_bool); 333 original_dict.Set("bool", original_bool);
334 FundamentalValue* original_int = Value::CreateIntegerValue(42); 334 FundamentalValue* original_int = Value::CreateIntegerValue(42);
335 original_dict.Set("int", original_int); 335 original_dict.Set("int", original_int);
336 FundamentalValue* original_double = Value::CreateDoubleValue(3.14); 336 FundamentalValue* original_double = Value::CreateDoubleValue(3.14);
337 original_dict.Set("double", original_double); 337 original_dict.Set("double", original_double);
338 StringValue* original_string = Value::CreateStringValue("hello"); 338 StringValue* original_string = Value::CreateStringValue("hello");
339 original_dict.Set("string", original_string); 339 original_dict.Set("string", original_string);
340 StringValue* original_string16 = 340 StringValue* original_string16 =
341 Value::CreateStringValue(ASCIIToUTF16("hello16")); 341 Value::CreateStringValue(ASCIIToUTF16("hello16"));
342 original_dict.Set("string16", original_string16); 342 original_dict.Set("string16", original_string16);
(...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after
444 Value* copy_list_element_1; 444 Value* copy_list_element_1;
445 ASSERT_TRUE(copy_list->Get(1, &copy_list_element_1)); 445 ASSERT_TRUE(copy_list->Get(1, &copy_list_element_1));
446 ASSERT_TRUE(copy_list_element_1); 446 ASSERT_TRUE(copy_list_element_1);
447 ASSERT_NE(copy_list_element_1, original_list_element_1); 447 ASSERT_NE(copy_list_element_1, original_list_element_1);
448 int copy_list_element_1_value; 448 int copy_list_element_1_value;
449 ASSERT_TRUE(copy_list_element_1->GetAsInteger(&copy_list_element_1_value)); 449 ASSERT_TRUE(copy_list_element_1->GetAsInteger(&copy_list_element_1_value));
450 ASSERT_EQ(1, copy_list_element_1_value); 450 ASSERT_EQ(1, copy_list_element_1_value);
451 } 451 }
452 452
453 TEST(ValuesTest, Equals) { 453 TEST(ValuesTest, Equals) {
454 Value* null1 = Value::CreateNullValue(); 454 Value* null1 = NullValue();
455 Value* null2 = Value::CreateNullValue(); 455 Value* null2 = NullValue();
456 EXPECT_NE(null1, null2); 456 EXPECT_NE(null1, null2);
457 EXPECT_TRUE(null1->Equals(null2)); 457 EXPECT_TRUE(null1->Equals(null2));
458 458
459 Value* boolean = Value::CreateBooleanValue(false); 459 Value* boolean = FalseValue();
460 EXPECT_FALSE(null1->Equals(boolean)); 460 EXPECT_FALSE(null1->Equals(boolean));
461 delete null1; 461 delete null1;
462 delete null2; 462 delete null2;
463 delete boolean; 463 delete boolean;
464 464
465 DictionaryValue dv; 465 DictionaryValue dv;
466 dv.SetBoolean("a", false); 466 dv.SetBoolean("a", false);
467 dv.SetInteger("b", 2); 467 dv.SetInteger("b", 2);
468 dv.SetDouble("c", 2.5); 468 dv.SetDouble("c", 2.5);
469 dv.SetString("d1", "string"); 469 dv.SetString("d1", "string");
470 dv.SetString("d2", ASCIIToUTF16("http://google.com")); 470 dv.SetString("d2", ASCIIToUTF16("http://google.com"));
471 dv.Set("e", Value::CreateNullValue()); 471 dv.Set("e", NullValue());
472 472
473 scoped_ptr<DictionaryValue> copy; 473 scoped_ptr<DictionaryValue> copy;
474 copy.reset(dv.DeepCopy()); 474 copy.reset(dv.DeepCopy());
475 EXPECT_TRUE(dv.Equals(copy.get())); 475 EXPECT_TRUE(dv.Equals(copy.get()));
476 476
477 ListValue* list = new ListValue; 477 ListValue* list = new ListValue;
478 list->Append(Value::CreateNullValue()); 478 list->Append(NullValue());
479 list->Append(new DictionaryValue); 479 list->Append(new DictionaryValue);
480 dv.Set("f", list); 480 dv.Set("f", list);
481 481
482 EXPECT_FALSE(dv.Equals(copy.get())); 482 EXPECT_FALSE(dv.Equals(copy.get()));
483 copy->Set("f", list->DeepCopy()); 483 copy->Set("f", list->DeepCopy());
484 EXPECT_TRUE(dv.Equals(copy.get())); 484 EXPECT_TRUE(dv.Equals(copy.get()));
485 485
486 list->Append(Value::CreateBooleanValue(true)); 486 list->Append(TrueValue());
487 EXPECT_FALSE(dv.Equals(copy.get())); 487 EXPECT_FALSE(dv.Equals(copy.get()));
488 488
489 // Check if Equals detects differences in only the keys. 489 // Check if Equals detects differences in only the keys.
490 copy.reset(dv.DeepCopy()); 490 copy.reset(dv.DeepCopy());
491 EXPECT_TRUE(dv.Equals(copy.get())); 491 EXPECT_TRUE(dv.Equals(copy.get()));
492 copy->Remove("a", NULL); 492 copy->Remove("a", NULL);
493 copy->SetBoolean("aa", false); 493 copy->SetBoolean("aa", false);
494 EXPECT_FALSE(dv.Equals(copy.get())); 494 EXPECT_FALSE(dv.Equals(copy.get()));
495 } 495 }
496 496
497 TEST(ValuesTest, StaticEquals) { 497 TEST(ValuesTest, StaticEquals) {
498 scoped_ptr<Value> null1(Value::CreateNullValue()); 498 scoped_ptr<Value> null1(NullValue());
499 scoped_ptr<Value> null2(Value::CreateNullValue()); 499 scoped_ptr<Value> null2(NullValue());
500 EXPECT_TRUE(Value::Equals(null1.get(), null2.get())); 500 EXPECT_TRUE(Value::Equals(null1.get(), null2.get()));
501 EXPECT_TRUE(Value::Equals(NULL, NULL)); 501 EXPECT_TRUE(Value::Equals(NULL, NULL));
502 502
503 scoped_ptr<Value> i42(Value::CreateIntegerValue(42)); 503 scoped_ptr<Value> i42(Value::CreateIntegerValue(42));
504 scoped_ptr<Value> j42(Value::CreateIntegerValue(42)); 504 scoped_ptr<Value> j42(Value::CreateIntegerValue(42));
505 scoped_ptr<Value> i17(Value::CreateIntegerValue(17)); 505 scoped_ptr<Value> i17(Value::CreateIntegerValue(17));
506 EXPECT_TRUE(Value::Equals(i42.get(), i42.get())); 506 EXPECT_TRUE(Value::Equals(i42.get(), i42.get()));
507 EXPECT_TRUE(Value::Equals(j42.get(), i42.get())); 507 EXPECT_TRUE(Value::Equals(j42.get(), i42.get()));
508 EXPECT_TRUE(Value::Equals(i42.get(), j42.get())); 508 EXPECT_TRUE(Value::Equals(i42.get(), j42.get()));
509 EXPECT_FALSE(Value::Equals(i42.get(), i17.get())); 509 EXPECT_FALSE(Value::Equals(i42.get(), i17.get()));
510 EXPECT_FALSE(Value::Equals(i42.get(), NULL)); 510 EXPECT_FALSE(Value::Equals(i42.get(), NULL));
511 EXPECT_FALSE(Value::Equals(NULL, i42.get())); 511 EXPECT_FALSE(Value::Equals(NULL, i42.get()));
512 512
513 // NULL and Value::CreateNullValue() are intentionally different: We need 513 // NULL and NullValue() are intentionally different: We need
514 // support for NULL as a return value for "undefined" without caring for 514 // support for NULL as a return value for "undefined" without caring for
515 // ownership of the pointer. 515 // ownership of the pointer.
516 EXPECT_FALSE(Value::Equals(null1.get(), NULL)); 516 EXPECT_FALSE(Value::Equals(null1.get(), NULL));
517 EXPECT_FALSE(Value::Equals(NULL, null1.get())); 517 EXPECT_FALSE(Value::Equals(NULL, null1.get()));
518 } 518 }
519 519
520 TEST(ValuesTest, DeepCopyCovariantReturnTypes) { 520 TEST(ValuesTest, DeepCopyCovariantReturnTypes) {
521 DictionaryValue original_dict; 521 DictionaryValue original_dict;
522 Value* original_null = Value::CreateNullValue(); 522 Value* original_null = NullValue();
523 original_dict.Set("null", original_null); 523 original_dict.Set("null", original_null);
524 FundamentalValue* original_bool = Value::CreateBooleanValue(true); 524 FundamentalValue* original_bool = TrueValue();
525 original_dict.Set("bool", original_bool); 525 original_dict.Set("bool", original_bool);
526 FundamentalValue* original_int = Value::CreateIntegerValue(42); 526 FundamentalValue* original_int = Value::CreateIntegerValue(42);
527 original_dict.Set("int", original_int); 527 original_dict.Set("int", original_int);
528 FundamentalValue* original_double = Value::CreateDoubleValue(3.14); 528 FundamentalValue* original_double = Value::CreateDoubleValue(3.14);
529 original_dict.Set("double", original_double); 529 original_dict.Set("double", original_double);
530 StringValue* original_string = Value::CreateStringValue("hello"); 530 StringValue* original_string = Value::CreateStringValue("hello");
531 original_dict.Set("string", original_string); 531 original_dict.Set("string", original_string);
532 StringValue* original_string16 = 532 StringValue* original_string16 =
533 Value::CreateStringValue(ASCIIToUTF16("hello16")); 533 Value::CreateStringValue(ASCIIToUTF16("hello16"));
534 original_dict.Set("string16", original_string16); 534 original_dict.Set("string16", original_string16);
(...skipping 152 matching lines...) Expand 10 before | Expand all | Expand 10 after
687 std::string sub_collide_key_value; 687 std::string sub_collide_key_value;
688 EXPECT_TRUE(res_sub_dict->GetString("sub_collide_key", 688 EXPECT_TRUE(res_sub_dict->GetString("sub_collide_key",
689 &sub_collide_key_value)); 689 &sub_collide_key_value));
690 EXPECT_EQ("sub_collide_key_value_merge", sub_collide_key_value); // Replaced. 690 EXPECT_EQ("sub_collide_key_value_merge", sub_collide_key_value); // Replaced.
691 std::string sub_merge_key_value; 691 std::string sub_merge_key_value;
692 EXPECT_TRUE(res_sub_dict->GetString("sub_merge_key", &sub_merge_key_value)); 692 EXPECT_TRUE(res_sub_dict->GetString("sub_merge_key", &sub_merge_key_value));
693 EXPECT_EQ("sub_merge_key_value_merge", sub_merge_key_value); // Merged in. 693 EXPECT_EQ("sub_merge_key_value_merge", sub_merge_key_value); // Merged in.
694 } 694 }
695 695
696 } // namespace base 696 } // namespace base
OLDNEW
« no previous file with comments | « base/values.cc ('k') | chrome/browser/automation/automation_provider_observers.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698