1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
|
// Copyright (c) 2010 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <limits>
#include "base/scoped_ptr.h"
#include "base/string16.h"
#include "base/utf_string_conversions.h"
#include "base/values.h"
#include "testing/gtest/include/gtest/gtest.h"
class ValuesTest: public testing::Test {
protected:
void CompareDictionariesAndCheckResult(
const DictionaryValue* dict1,
const DictionaryValue* dict2,
const char* expected_paths[],
size_t expected_paths_count) {
std::vector<std::string> differing_paths;
std::vector<std::string> expected_paths_vector(expected_paths,
expected_paths+expected_paths_count);
// All comparisons should be commutative, check dict1 against dict2
// and vice-versa.
dict1->GetDifferingPaths(dict2, &differing_paths);
ASSERT_EQ(expected_paths_count, differing_paths.size());
EXPECT_TRUE(equal(differing_paths.begin(), differing_paths.end(),
expected_paths_vector.begin()));
dict2->GetDifferingPaths(dict1, &differing_paths);
ASSERT_EQ(expected_paths_count, differing_paths.size());
EXPECT_TRUE(equal(differing_paths.begin(), differing_paths.end(),
expected_paths_vector.begin()));
}
};
TEST_F(ValuesTest, Basic) {
// Test basic dictionary getting/setting
DictionaryValue settings;
std::string homepage = "http://google.com";
ASSERT_FALSE(settings.GetString("global.homepage", &homepage));
ASSERT_EQ(std::string("http://google.com"), homepage);
ASSERT_FALSE(settings.Get("global", NULL));
settings.Set("global", Value::CreateBooleanValue(true));
ASSERT_TRUE(settings.Get("global", NULL));
settings.SetString("global.homepage", "http://scurvy.com");
ASSERT_TRUE(settings.Get("global", NULL));
homepage = "http://google.com";
ASSERT_TRUE(settings.GetString("global.homepage", &homepage));
ASSERT_EQ(std::string("http://scurvy.com"), homepage);
// Test storing a dictionary in a list.
ListValue* toolbar_bookmarks;
ASSERT_FALSE(
settings.GetList("global.toolbar.bookmarks", &toolbar_bookmarks));
toolbar_bookmarks = new ListValue;
settings.Set("global.toolbar.bookmarks", toolbar_bookmarks);
ASSERT_TRUE(settings.GetList("global.toolbar.bookmarks", &toolbar_bookmarks));
DictionaryValue* new_bookmark = new DictionaryValue;
new_bookmark->SetString("name", "Froogle");
new_bookmark->SetString("url", "http://froogle.com");
toolbar_bookmarks->Append(new_bookmark);
ListValue* bookmark_list;
ASSERT_TRUE(settings.GetList("global.toolbar.bookmarks", &bookmark_list));
DictionaryValue* bookmark;
ASSERT_EQ(1U, bookmark_list->GetSize());
ASSERT_TRUE(bookmark_list->GetDictionary(0, &bookmark));
std::string bookmark_name = "Unnamed";
ASSERT_TRUE(bookmark->GetString("name", &bookmark_name));
ASSERT_EQ(std::string("Froogle"), bookmark_name);
std::string bookmark_url;
ASSERT_TRUE(bookmark->GetString("url", &bookmark_url));
ASSERT_EQ(std::string("http://froogle.com"), bookmark_url);
}
TEST_F(ValuesTest, List) {
scoped_ptr<ListValue> mixed_list(new ListValue());
mixed_list->Set(0, Value::CreateBooleanValue(true));
mixed_list->Set(1, Value::CreateIntegerValue(42));
mixed_list->Set(2, Value::CreateRealValue(88.8));
mixed_list->Set(3, Value::CreateStringValue("foo"));
ASSERT_EQ(4u, mixed_list->GetSize());
Value *value = NULL;
bool bool_value = false;
int int_value = 0;
double double_value = 0.0;
std::string string_value;
ASSERT_FALSE(mixed_list->Get(4, &value));
ASSERT_FALSE(mixed_list->GetInteger(0, &int_value));
ASSERT_EQ(0, int_value);
ASSERT_FALSE(mixed_list->GetReal(1, &double_value));
ASSERT_EQ(0.0, double_value);
ASSERT_FALSE(mixed_list->GetString(2, &string_value));
ASSERT_EQ("", string_value);
ASSERT_FALSE(mixed_list->GetBoolean(3, &bool_value));
ASSERT_FALSE(bool_value);
ASSERT_TRUE(mixed_list->GetBoolean(0, &bool_value));
ASSERT_TRUE(bool_value);
ASSERT_TRUE(mixed_list->GetInteger(1, &int_value));
ASSERT_EQ(42, int_value);
ASSERT_TRUE(mixed_list->GetReal(2, &double_value));
ASSERT_EQ(88.8, double_value);
ASSERT_TRUE(mixed_list->GetString(3, &string_value));
ASSERT_EQ("foo", string_value);
}
TEST_F(ValuesTest, BinaryValue) {
char* buffer = NULL;
// Passing a null buffer pointer doesn't yield a BinaryValue
scoped_ptr<BinaryValue> binary(BinaryValue::Create(buffer, 0));
ASSERT_FALSE(binary.get());
// If you want to represent an empty binary value, use a zero-length buffer.
buffer = new char[1];
ASSERT_TRUE(buffer);
binary.reset(BinaryValue::Create(buffer, 0));
ASSERT_TRUE(binary.get());
ASSERT_TRUE(binary->GetBuffer());
ASSERT_EQ(buffer, binary->GetBuffer());
ASSERT_EQ(0U, binary->GetSize());
// Test the common case of a non-empty buffer
buffer = new char[15];
binary.reset(BinaryValue::Create(buffer, 15));
ASSERT_TRUE(binary.get());
ASSERT_TRUE(binary->GetBuffer());
ASSERT_EQ(buffer, binary->GetBuffer());
ASSERT_EQ(15U, binary->GetSize());
char stack_buffer[42];
memset(stack_buffer, '!', 42);
binary.reset(BinaryValue::CreateWithCopiedBuffer(stack_buffer, 42));
ASSERT_TRUE(binary.get());
ASSERT_TRUE(binary->GetBuffer());
ASSERT_NE(stack_buffer, binary->GetBuffer());
ASSERT_EQ(42U, binary->GetSize());
ASSERT_EQ(0, memcmp(stack_buffer, binary->GetBuffer(), binary->GetSize()));
}
TEST_F(ValuesTest, StringValue) {
// Test overloaded CreateStringValue.
scoped_ptr<Value> narrow_value(Value::CreateStringValue("narrow"));
ASSERT_TRUE(narrow_value.get());
ASSERT_TRUE(narrow_value->IsType(Value::TYPE_STRING));
scoped_ptr<Value> utf16_value(
Value::CreateStringValue(ASCIIToUTF16("utf16")));
ASSERT_TRUE(utf16_value.get());
ASSERT_TRUE(utf16_value->IsType(Value::TYPE_STRING));
// Test overloaded GetString.
std::string narrow = "http://google.com";
string16 utf16 = ASCIIToUTF16("http://google.com");
ASSERT_TRUE(narrow_value->GetAsString(&narrow));
ASSERT_TRUE(narrow_value->GetAsString(&utf16));
ASSERT_EQ(std::string("narrow"), narrow);
ASSERT_EQ(ASCIIToUTF16("narrow"), utf16);
ASSERT_TRUE(utf16_value->GetAsString(&narrow));
ASSERT_TRUE(utf16_value->GetAsString(&utf16));
ASSERT_EQ(std::string("utf16"), narrow);
ASSERT_EQ(ASCIIToUTF16("utf16"), utf16);
}
// This is a Value object that allows us to tell if it's been
// properly deleted by modifying the value of external flag on destruction.
class DeletionTestValue : public Value {
public:
explicit DeletionTestValue(bool* deletion_flag) : Value(TYPE_NULL) {
Init(deletion_flag); // Separate function so that we can use ASSERT_*
}
void Init(bool* deletion_flag) {
ASSERT_TRUE(deletion_flag);
deletion_flag_ = deletion_flag;
*deletion_flag_ = false;
}
~DeletionTestValue() {
*deletion_flag_ = true;
}
private:
bool* deletion_flag_;
};
TEST_F(ValuesTest, ListDeletion) {
bool deletion_flag = true;
{
ListValue list;
list.Append(new DeletionTestValue(&deletion_flag));
EXPECT_FALSE(deletion_flag);
}
EXPECT_TRUE(deletion_flag);
{
ListValue list;
list.Append(new DeletionTestValue(&deletion_flag));
EXPECT_FALSE(deletion_flag);
list.Clear();
EXPECT_TRUE(deletion_flag);
}
{
ListValue list;
list.Append(new DeletionTestValue(&deletion_flag));
EXPECT_FALSE(deletion_flag);
EXPECT_TRUE(list.Set(0, Value::CreateNullValue()));
EXPECT_TRUE(deletion_flag);
}
}
TEST_F(ValuesTest, ListRemoval) {
bool deletion_flag = true;
Value* removed_item = NULL;
{
ListValue list;
list.Append(new DeletionTestValue(&deletion_flag));
EXPECT_FALSE(deletion_flag);
EXPECT_EQ(1U, list.GetSize());
EXPECT_FALSE(list.Remove(std::numeric_limits<size_t>::max(),
&removed_item));
EXPECT_FALSE(list.Remove(1, &removed_item));
EXPECT_TRUE(list.Remove(0, &removed_item));
ASSERT_TRUE(removed_item);
EXPECT_EQ(0U, list.GetSize());
}
EXPECT_FALSE(deletion_flag);
delete removed_item;
removed_item = NULL;
EXPECT_TRUE(deletion_flag);
{
ListValue list;
list.Append(new DeletionTestValue(&deletion_flag));
EXPECT_FALSE(deletion_flag);
EXPECT_TRUE(list.Remove(0, NULL));
EXPECT_TRUE(deletion_flag);
EXPECT_EQ(0U, list.GetSize());
}
{
ListValue list;
DeletionTestValue* value = new DeletionTestValue(&deletion_flag);
list.Append(value);
EXPECT_FALSE(deletion_flag);
EXPECT_EQ(0, list.Remove(*value));
EXPECT_TRUE(deletion_flag);
EXPECT_EQ(0U, list.GetSize());
}
}
TEST_F(ValuesTest, DictionaryDeletion) {
std::string key = "test";
bool deletion_flag = true;
{
DictionaryValue dict;
dict.Set(key, new DeletionTestValue(&deletion_flag));
EXPECT_FALSE(deletion_flag);
}
EXPECT_TRUE(deletion_flag);
{
DictionaryValue dict;
dict.Set(key, new DeletionTestValue(&deletion_flag));
EXPECT_FALSE(deletion_flag);
dict.Clear();
EXPECT_TRUE(deletion_flag);
}
{
DictionaryValue dict;
dict.Set(key, new DeletionTestValue(&deletion_flag));
EXPECT_FALSE(deletion_flag);
dict.Set(key, Value::CreateNullValue());
EXPECT_TRUE(deletion_flag);
}
}
TEST_F(ValuesTest, DictionaryRemoval) {
std::string key = "test";
bool deletion_flag = true;
Value* removed_item = NULL;
{
DictionaryValue dict;
dict.Set(key, new DeletionTestValue(&deletion_flag));
EXPECT_FALSE(deletion_flag);
EXPECT_TRUE(dict.HasKey(key));
EXPECT_FALSE(dict.Remove("absent key", &removed_item));
EXPECT_TRUE(dict.Remove(key, &removed_item));
EXPECT_FALSE(dict.HasKey(key));
ASSERT_TRUE(removed_item);
}
EXPECT_FALSE(deletion_flag);
delete removed_item;
removed_item = NULL;
EXPECT_TRUE(deletion_flag);
{
DictionaryValue dict;
dict.Set(key, new DeletionTestValue(&deletion_flag));
EXPECT_FALSE(deletion_flag);
EXPECT_TRUE(dict.HasKey(key));
EXPECT_TRUE(dict.Remove(key, NULL));
EXPECT_TRUE(deletion_flag);
EXPECT_FALSE(dict.HasKey(key));
}
}
TEST_F(ValuesTest, DictionaryWithoutPathExpansion) {
DictionaryValue dict;
dict.Set("this.is.expanded", Value::CreateNullValue());
dict.SetWithoutPathExpansion("this.isnt.expanded", Value::CreateNullValue());
EXPECT_FALSE(dict.HasKey("this.is.expanded"));
EXPECT_TRUE(dict.HasKey("this"));
Value* value1;
EXPECT_TRUE(dict.Get("this", &value1));
DictionaryValue* value2;
ASSERT_TRUE(dict.GetDictionaryWithoutPathExpansion("this", &value2));
EXPECT_EQ(value1, value2);
EXPECT_EQ(1U, value2->size());
EXPECT_TRUE(dict.HasKey("this.isnt.expanded"));
Value* value3;
EXPECT_FALSE(dict.Get("this.isnt.expanded", &value3));
Value* value4;
ASSERT_TRUE(dict.GetWithoutPathExpansion("this.isnt.expanded", &value4));
EXPECT_EQ(Value::TYPE_NULL, value4->GetType());
}
TEST_F(ValuesTest, DeepCopy) {
DictionaryValue original_dict;
Value* original_null = Value::CreateNullValue();
original_dict.Set("null", original_null);
Value* original_bool = Value::CreateBooleanValue(true);
original_dict.Set("bool", original_bool);
Value* original_int = Value::CreateIntegerValue(42);
original_dict.Set("int", original_int);
Value* original_real = Value::CreateRealValue(3.14);
original_dict.Set("real", original_real);
Value* original_string = Value::CreateStringValue("hello");
original_dict.Set("string", original_string);
Value* original_string16 = Value::CreateStringValue(ASCIIToUTF16("hello16"));
original_dict.Set("string16", original_string16);
char* original_buffer = new char[42];
memset(original_buffer, '!', 42);
BinaryValue* original_binary = Value::CreateBinaryValue(original_buffer, 42);
original_dict.Set("binary", original_binary);
ListValue* original_list = new ListValue();
Value* original_list_element_0 = Value::CreateIntegerValue(0);
original_list->Append(original_list_element_0);
Value* original_list_element_1 = Value::CreateIntegerValue(1);
original_list->Append(original_list_element_1);
original_dict.Set("list", original_list);
scoped_ptr<DictionaryValue> copy_dict(
static_cast<DictionaryValue*>(original_dict.DeepCopy()));
ASSERT_TRUE(copy_dict.get());
ASSERT_NE(copy_dict.get(), &original_dict);
Value* copy_null = NULL;
ASSERT_TRUE(copy_dict->Get("null", ©_null));
ASSERT_TRUE(copy_null);
ASSERT_NE(copy_null, original_null);
ASSERT_TRUE(copy_null->IsType(Value::TYPE_NULL));
Value* copy_bool = NULL;
ASSERT_TRUE(copy_dict->Get("bool", ©_bool));
ASSERT_TRUE(copy_bool);
ASSERT_NE(copy_bool, original_bool);
ASSERT_TRUE(copy_bool->IsType(Value::TYPE_BOOLEAN));
bool copy_bool_value = false;
ASSERT_TRUE(copy_bool->GetAsBoolean(©_bool_value));
ASSERT_TRUE(copy_bool_value);
Value* copy_int = NULL;
ASSERT_TRUE(copy_dict->Get("int", ©_int));
ASSERT_TRUE(copy_int);
ASSERT_NE(copy_int, original_int);
ASSERT_TRUE(copy_int->IsType(Value::TYPE_INTEGER));
int copy_int_value = 0;
ASSERT_TRUE(copy_int->GetAsInteger(©_int_value));
ASSERT_EQ(42, copy_int_value);
Value* copy_real = NULL;
ASSERT_TRUE(copy_dict->Get("real", ©_real));
ASSERT_TRUE(copy_real);
ASSERT_NE(copy_real, original_real);
ASSERT_TRUE(copy_real->IsType(Value::TYPE_REAL));
double copy_real_value = 0;
ASSERT_TRUE(copy_real->GetAsReal(©_real_value));
ASSERT_EQ(3.14, copy_real_value);
Value* copy_string = NULL;
ASSERT_TRUE(copy_dict->Get("string", ©_string));
ASSERT_TRUE(copy_string);
ASSERT_NE(copy_string, original_string);
ASSERT_TRUE(copy_string->IsType(Value::TYPE_STRING));
std::string copy_string_value;
string16 copy_string16_value;
ASSERT_TRUE(copy_string->GetAsString(©_string_value));
ASSERT_TRUE(copy_string->GetAsString(©_string16_value));
ASSERT_EQ(std::string("hello"), copy_string_value);
ASSERT_EQ(ASCIIToUTF16("hello"), copy_string16_value);
Value* copy_string16 = NULL;
ASSERT_TRUE(copy_dict->Get("string16", ©_string16));
ASSERT_TRUE(copy_string16);
ASSERT_NE(copy_string16, original_string16);
ASSERT_TRUE(copy_string16->IsType(Value::TYPE_STRING));
ASSERT_TRUE(copy_string16->GetAsString(©_string_value));
ASSERT_TRUE(copy_string16->GetAsString(©_string16_value));
ASSERT_EQ(std::string("hello16"), copy_string_value);
ASSERT_EQ(ASCIIToUTF16("hello16"), copy_string16_value);
Value* copy_binary = NULL;
ASSERT_TRUE(copy_dict->Get("binary", ©_binary));
ASSERT_TRUE(copy_binary);
ASSERT_NE(copy_binary, original_binary);
ASSERT_TRUE(copy_binary->IsType(Value::TYPE_BINARY));
ASSERT_NE(original_binary->GetBuffer(),
static_cast<BinaryValue*>(copy_binary)->GetBuffer());
ASSERT_EQ(original_binary->GetSize(),
static_cast<BinaryValue*>(copy_binary)->GetSize());
ASSERT_EQ(0, memcmp(original_binary->GetBuffer(),
static_cast<BinaryValue*>(copy_binary)->GetBuffer(),
original_binary->GetSize()));
Value* copy_value = NULL;
ASSERT_TRUE(copy_dict->Get("list", ©_value));
ASSERT_TRUE(copy_value);
ASSERT_NE(copy_value, original_list);
ASSERT_TRUE(copy_value->IsType(Value::TYPE_LIST));
ListValue* copy_list = static_cast<ListValue*>(copy_value);
ASSERT_EQ(2U, copy_list->GetSize());
Value* copy_list_element_0;
ASSERT_TRUE(copy_list->Get(0, ©_list_element_0));
ASSERT_TRUE(copy_list_element_0);
ASSERT_NE(copy_list_element_0, original_list_element_0);
int copy_list_element_0_value;
ASSERT_TRUE(copy_list_element_0->GetAsInteger(©_list_element_0_value));
ASSERT_EQ(0, copy_list_element_0_value);
Value* copy_list_element_1;
ASSERT_TRUE(copy_list->Get(1, ©_list_element_1));
ASSERT_TRUE(copy_list_element_1);
ASSERT_NE(copy_list_element_1, original_list_element_1);
int copy_list_element_1_value;
ASSERT_TRUE(copy_list_element_1->GetAsInteger(©_list_element_1_value));
ASSERT_EQ(1, copy_list_element_1_value);
}
TEST_F(ValuesTest, Equals) {
Value* null1 = Value::CreateNullValue();
Value* null2 = Value::CreateNullValue();
EXPECT_NE(null1, null2);
EXPECT_TRUE(null1->Equals(null2));
Value* boolean = Value::CreateBooleanValue(false);
EXPECT_FALSE(null1->Equals(boolean));
delete null1;
delete null2;
delete boolean;
DictionaryValue dv;
dv.SetBoolean("a", false);
dv.SetInteger("b", 2);
dv.SetReal("c", 2.5);
dv.SetString("d1", "string");
dv.SetString("d2", ASCIIToUTF16("http://google.com"));
dv.Set("e", Value::CreateNullValue());
scoped_ptr<DictionaryValue> copy;
copy.reset(static_cast<DictionaryValue*>(dv.DeepCopy()));
EXPECT_TRUE(dv.Equals(copy.get()));
ListValue* list = new ListValue;
list->Append(Value::CreateNullValue());
list->Append(new DictionaryValue);
dv.Set("f", list);
EXPECT_FALSE(dv.Equals(copy.get()));
copy->Set("f", list->DeepCopy());
EXPECT_TRUE(dv.Equals(copy.get()));
list->Append(Value::CreateBooleanValue(true));
EXPECT_FALSE(dv.Equals(copy.get()));
// Check if Equals detects differences in only the keys.
copy.reset(static_cast<DictionaryValue*>(dv.DeepCopy()));
EXPECT_TRUE(dv.Equals(copy.get()));
copy->Remove("a", NULL);
copy->SetBoolean("aa", false);
EXPECT_FALSE(dv.Equals(copy.get()));
}
TEST_F(ValuesTest, RemoveEmptyChildren) {
scoped_ptr<DictionaryValue> root(new DictionaryValue);
// Remove empty lists and dictionaries.
root->Set("empty_dict", new DictionaryValue);
root->Set("empty_list", new ListValue);
root->SetWithoutPathExpansion("a.b.c.d.e", new DictionaryValue);
root.reset(root->DeepCopyWithoutEmptyChildren());
EXPECT_TRUE(root->empty());
// Make sure we don't prune too much.
root->SetBoolean("bool", true);
root->Set("empty_dict", new DictionaryValue);
root->SetString("empty_string", "");
root.reset(root->DeepCopyWithoutEmptyChildren());
EXPECT_EQ(2U, root->size());
// Should do nothing.
root.reset(root->DeepCopyWithoutEmptyChildren());
EXPECT_EQ(2U, root->size());
// Nested test cases. These should all reduce back to the bool and string
// set above.
{
root->Set("a.b.c.d.e", new DictionaryValue);
root.reset(root->DeepCopyWithoutEmptyChildren());
EXPECT_EQ(2U, root->size());
}
{
DictionaryValue* inner = new DictionaryValue;
root->Set("dict_with_emtpy_children", inner);
inner->Set("empty_dict", new DictionaryValue);
inner->Set("empty_list", new ListValue);
root.reset(root->DeepCopyWithoutEmptyChildren());
EXPECT_EQ(2U, root->size());
}
{
ListValue* inner = new ListValue;
root->Set("list_with_empty_children", inner);
inner->Append(new DictionaryValue);
inner->Append(new ListValue);
root.reset(root->DeepCopyWithoutEmptyChildren());
EXPECT_EQ(2U, root->size());
}
// Nested with siblings.
{
ListValue* inner = new ListValue;
root->Set("list_with_empty_children", inner);
inner->Append(new DictionaryValue);
inner->Append(new ListValue);
DictionaryValue* inner2 = new DictionaryValue;
root->Set("dict_with_empty_children", inner2);
inner2->Set("empty_dict", new DictionaryValue);
inner2->Set("empty_list", new ListValue);
root.reset(root->DeepCopyWithoutEmptyChildren());
EXPECT_EQ(2U, root->size());
}
// Make sure nested values don't get pruned.
{
ListValue* inner = new ListValue;
root->Set("list_with_empty_children", inner);
ListValue* inner2 = new ListValue;
inner->Append(new DictionaryValue);
inner->Append(inner2);
inner2->Append(Value::CreateStringValue("hello"));
root.reset(root->DeepCopyWithoutEmptyChildren());
EXPECT_EQ(3U, root->size());
EXPECT_TRUE(root->GetList("list_with_empty_children", &inner));
EXPECT_EQ(1U, inner->GetSize()); // Dictionary was pruned.
EXPECT_TRUE(inner->GetList(0, &inner2));
EXPECT_EQ(1U, inner2->GetSize());
}
}
TEST_F(ValuesTest, MergeDictionary) {
scoped_ptr<DictionaryValue> base(new DictionaryValue);
base->SetString("base_key", "base_key_value_base");
base->SetString("collide_key", "collide_key_value_base");
DictionaryValue* base_sub_dict = new DictionaryValue;
base_sub_dict->SetString("sub_base_key", "sub_base_key_value_base");
base_sub_dict->SetString("sub_collide_key", "sub_collide_key_value_base");
base->Set("sub_dict_key", base_sub_dict);
scoped_ptr<DictionaryValue> merge(new DictionaryValue);
merge->SetString("merge_key", "merge_key_value_merge");
merge->SetString("collide_key", "collide_key_value_merge");
DictionaryValue* merge_sub_dict = new DictionaryValue;
merge_sub_dict->SetString("sub_merge_key", "sub_merge_key_value_merge");
merge_sub_dict->SetString("sub_collide_key", "sub_collide_key_value_merge");
merge->Set("sub_dict_key", merge_sub_dict);
base->MergeDictionary(merge.get());
EXPECT_EQ(4U, base->size());
std::string base_key_value;
EXPECT_TRUE(base->GetString("base_key", &base_key_value));
EXPECT_EQ("base_key_value_base", base_key_value); // Base value preserved.
std::string collide_key_value;
EXPECT_TRUE(base->GetString("collide_key", &collide_key_value));
EXPECT_EQ("collide_key_value_merge", collide_key_value); // Replaced.
std::string merge_key_value;
EXPECT_TRUE(base->GetString("merge_key", &merge_key_value));
EXPECT_EQ("merge_key_value_merge", merge_key_value); // Merged in.
DictionaryValue* res_sub_dict;
EXPECT_TRUE(base->GetDictionary("sub_dict_key", &res_sub_dict));
EXPECT_EQ(3U, res_sub_dict->size());
std::string sub_base_key_value;
EXPECT_TRUE(res_sub_dict->GetString("sub_base_key", &sub_base_key_value));
EXPECT_EQ("sub_base_key_value_base", sub_base_key_value); // Preserved.
std::string sub_collide_key_value;
EXPECT_TRUE(res_sub_dict->GetString("sub_collide_key",
&sub_collide_key_value));
EXPECT_EQ("sub_collide_key_value_merge", sub_collide_key_value); // Replaced.
std::string sub_merge_key_value;
EXPECT_TRUE(res_sub_dict->GetString("sub_merge_key", &sub_merge_key_value));
EXPECT_EQ("sub_merge_key_value_merge", sub_merge_key_value); // Merged in.
}
TEST_F(ValuesTest, GetDifferingPaths) {
scoped_ptr<DictionaryValue> dict1(new DictionaryValue());
scoped_ptr<DictionaryValue> dict2(new DictionaryValue());
std::vector<std::string> differing_paths;
// Test comparing empty dictionaries.
dict1->GetDifferingPaths(dict2.get(), &differing_paths);
EXPECT_EQ(differing_paths.size(), 0UL);
// Compare an empty dictionary with various non-empty dictionaries.
static const char* expected_paths1[] = {
"segment1"
};
dict1->SetString("segment1", "value1");
CompareDictionariesAndCheckResult(dict1.get(), dict2.get(), expected_paths1,
arraysize(expected_paths1));
static const char* expected_paths2[] = {
"segment1",
"segment2",
"segment2.segment3"
};
dict1->SetString("segment2.segment3", "value2");
CompareDictionariesAndCheckResult(dict1.get(), dict2.get(), expected_paths2,
arraysize(expected_paths2));
static const char* expected_paths3[] = {
"segment1",
"segment2",
"segment2.segment3",
"segment4",
"segment4.segment5"
};
dict1->SetString("segment4.segment5", "value3");
CompareDictionariesAndCheckResult(dict1.get(), dict2.get(), expected_paths3,
arraysize(expected_paths3));
// Now various tests with two populated dictionaries.
static const char* expected_paths4[] = {
"segment1",
"segment2",
"segment2.segment3",
"segment4",
"segment4.segment5"
};
dict2->Set("segment2", new DictionaryValue());
CompareDictionariesAndCheckResult(dict1.get(), dict2.get(), expected_paths4,
arraysize(expected_paths4));
static const char* expected_paths5[] = {
"segment1",
"segment4",
"segment4.segment5"
};
dict2->SetString("segment2.segment3", "value2");
CompareDictionariesAndCheckResult(dict1.get(), dict2.get(), expected_paths5,
arraysize(expected_paths5));
dict2->SetBoolean("segment2.segment3", true);
CompareDictionariesAndCheckResult(dict1.get(), dict2.get(), expected_paths4,
arraysize(expected_paths4));
// Test two identical dictionaries.
dict2.reset(static_cast<DictionaryValue*>(dict1->DeepCopy()));
dict2->GetDifferingPaths(dict1.get(), &differing_paths);
EXPECT_EQ(differing_paths.size(), 0UL);
// Test a deep dictionary structure.
static const char* expected_paths6[] = {
"s1",
"s1.s2",
"s1.s2.s3",
"s1.s2.s3.s4",
"s1.s2.s3.s4.s5"
};
dict1.reset(new DictionaryValue());
dict2.reset(new DictionaryValue());
dict1->Set("s1.s2.s3.s4.s5", new DictionaryValue());
CompareDictionariesAndCheckResult(dict1.get(), dict2.get(), expected_paths6,
arraysize(expected_paths6));
// Make sure disjoint dictionaries generate the right differing path list.
static const char* expected_paths7[] = {
"a",
"b",
"c",
"d"
};
dict1.reset(new DictionaryValue());
dict1->SetBoolean("a", true);
dict1->SetBoolean("c", true);
dict2.reset(new DictionaryValue());
dict1->SetBoolean("b", true);
dict1->SetBoolean("d", true);
CompareDictionariesAndCheckResult(dict1.get(), dict2.get(), expected_paths7,
arraysize(expected_paths7));
// For code coverage completeness. Make sure that all branches
// that were not covered are executed.
static const char* expected_paths8[] = {
"s1",
"s1.s2"
};
dict1.reset(new DictionaryValue());
dict1->Set("s1.s2", new DictionaryValue());
dict2.reset(new DictionaryValue());
dict2->SetInteger("s1", 1);
CompareDictionariesAndCheckResult(dict1.get(), dict2.get(), expected_paths8,
arraysize(expected_paths8));
}
|