[go: nahoru, domu]

blob: 1244d86e94ddcda31cc54a4ed40ba6b735909b9c [file] [log] [blame]
rsleevi@chromium.org44106182012-04-06 03:53:021// Copyright (c) 2012 The Chromium Authors. All rights reserved.
license.botbf09a502008-08-24 00:55:552// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commitd7cae122008-07-26 21:49:384
dcheng093de9b2016-04-04 21:25:515#include "base/values.h"
6
avi9b6f42932015-12-26 22:15:147#include <stddef.h>
8
jdoerrie44efa9d2017-07-14 14:47:209#include <functional>
deanm@google.com836061b2008-08-13 14:57:5110#include <limits>
dcheng093de9b2016-04-04 21:25:5111#include <memory>
jdoerrie17e71cc2017-03-30 06:40:2912#include <string>
13#include <type_traits>
danakj0c8d4aa2015-11-25 05:29:5814#include <utility>
jdoerriee03e80f2017-02-15 08:42:1415#include <vector>
deanm@google.com836061b2008-08-13 14:57:5116
jdoerrie44efa9d2017-07-14 14:47:2017#include "base/containers/adapters.h"
dcheng093de9b2016-04-04 21:25:5118#include "base/memory/ptr_util.h"
avi@chromium.orgc851cfd2013-06-10 20:11:1419#include "base/strings/string16.h"
jdoerrie46349472017-08-02 02:20:3220#include "base/strings/string_piece.h"
avi@chromium.orga4ea1f12013-06-07 18:37:0721#include "base/strings/utf_string_conversions.h"
jdoerrie44efa9d2017-07-14 14:47:2022#include "testing/gmock/include/gmock/gmock.h"
initial.commitd7cae122008-07-26 21:49:3823#include "testing/gtest/include/gtest/gtest.h"
24
tfarina@chromium.org58b916e2011-06-28 22:56:3325namespace base {
initial.commitd7cae122008-07-26 21:49:3826
jdoerrie17e71cc2017-03-30 06:40:2927TEST(ValuesTest, TestNothrow) {
jdoerrie9d47e9a2017-04-04 08:54:2128 static_assert(std::is_nothrow_move_constructible<Value>::value,
jdoerrie17e71cc2017-03-30 06:40:2929 "IsNothrowMoveConstructible");
jdoerrie9d47e9a2017-04-04 08:54:2130 static_assert(std::is_nothrow_default_constructible<Value>::value,
jdoerrie17e71cc2017-03-30 06:40:2931 "IsNothrowDefaultConstructible");
32 static_assert(std::is_nothrow_constructible<Value, std::string&&>::value,
33 "IsNothrowMoveConstructibleFromString");
34 static_assert(
jdoerrie5f12b6272017-04-18 10:22:4135 std::is_nothrow_constructible<Value, Value::BlobStorage&&>::value,
jdoerrie17e71cc2017-03-30 06:40:2936 "IsNothrowMoveConstructibleFromBlob");
jdoerrie2b7d0fcd2017-04-19 07:15:3837 static_assert(
38 std::is_nothrow_constructible<Value, Value::ListStorage&&>::value,
39 "IsNothrowMoveConstructibleFromList");
jdoerrie9d47e9a2017-04-04 08:54:2140 static_assert(std::is_nothrow_move_assignable<Value>::value,
41 "IsNothrowMoveAssignable");
jdoerrie52939ed2017-04-26 18:19:4242 static_assert(
43 std::is_nothrow_constructible<ListValue, Value::ListStorage&&>::value,
44 "ListIsNothrowMoveConstructibleFromList");
jdoerrie17e71cc2017-03-30 06:40:2945}
46
jdoerrie05eb3162017-02-01 10:36:5647// Group of tests for the value constructors.
48TEST(ValuesTest, ConstructBool) {
jdoerrie239723572017-03-02 12:09:1949 Value true_value(true);
jdoerrie05eb3162017-02-01 10:36:5650 EXPECT_EQ(Value::Type::BOOLEAN, true_value.type());
51 EXPECT_TRUE(true_value.GetBool());
52
jdoerrie239723572017-03-02 12:09:1953 Value false_value(false);
jdoerrie05eb3162017-02-01 10:36:5654 EXPECT_EQ(Value::Type::BOOLEAN, false_value.type());
55 EXPECT_FALSE(false_value.GetBool());
56}
57
58TEST(ValuesTest, ConstructInt) {
jdoerrie239723572017-03-02 12:09:1959 Value value(-37);
jdoerrie05eb3162017-02-01 10:36:5660 EXPECT_EQ(Value::Type::INTEGER, value.type());
61 EXPECT_EQ(-37, value.GetInt());
62}
63
64TEST(ValuesTest, ConstructDouble) {
jdoerrie239723572017-03-02 12:09:1965 Value value(-4.655);
jdoerrie05eb3162017-02-01 10:36:5666 EXPECT_EQ(Value::Type::DOUBLE, value.type());
67 EXPECT_EQ(-4.655, value.GetDouble());
68}
69
jdoerrief38f37b2017-02-01 14:38:3270TEST(ValuesTest, ConstructStringFromConstCharPtr) {
71 const char* str = "foobar";
jdoerrie122c4da2017-03-06 11:12:0472 Value value(str);
jdoerrief38f37b2017-02-01 14:38:3273 EXPECT_EQ(Value::Type::STRING, value.type());
74 EXPECT_EQ("foobar", value.GetString());
75}
76
jdoerrie9f90ad72017-09-11 17:23:2677TEST(ValuesTest, ConstructStringFromStringPiece) {
jdoerrief38f37b2017-02-01 14:38:3278 std::string str = "foobar";
jdoerrie9f90ad72017-09-11 17:23:2679 Value value{StringPiece(str)};
jdoerrief38f37b2017-02-01 14:38:3280 EXPECT_EQ(Value::Type::STRING, value.type());
81 EXPECT_EQ("foobar", value.GetString());
82}
83
jdoerrie9f90ad72017-09-11 17:23:2684TEST(ValuesTest, ConstructStringFromStdStringRRef) {
jdoerrief38f37b2017-02-01 14:38:3285 std::string str = "foobar";
jdoerrie122c4da2017-03-06 11:12:0486 Value value(std::move(str));
jdoerrief38f37b2017-02-01 14:38:3287 EXPECT_EQ(Value::Type::STRING, value.type());
88 EXPECT_EQ("foobar", value.GetString());
89}
90
91TEST(ValuesTest, ConstructStringFromConstChar16Ptr) {
92 string16 str = ASCIIToUTF16("foobar");
jdoerrie122c4da2017-03-06 11:12:0493 Value value(str.c_str());
jdoerrief38f37b2017-02-01 14:38:3294 EXPECT_EQ(Value::Type::STRING, value.type());
95 EXPECT_EQ("foobar", value.GetString());
96}
97
jdoerrie9f90ad72017-09-11 17:23:2698TEST(ValuesTest, ConstructStringFromStringPiece16) {
jdoerrief38f37b2017-02-01 14:38:3299 string16 str = ASCIIToUTF16("foobar");
jdoerrie9f90ad72017-09-11 17:23:26100 Value value{StringPiece16(str)};
jdoerrief38f37b2017-02-01 14:38:32101 EXPECT_EQ(Value::Type::STRING, value.type());
102 EXPECT_EQ("foobar", value.GetString());
103}
104
jdoerriee03e80f2017-02-15 08:42:14105TEST(ValuesTest, ConstructBinary) {
jdoerrie5f12b6272017-04-18 10:22:41106 Value value(Value::BlobStorage({0xF, 0x0, 0x0, 0xB, 0xA, 0x2}));
jdoerriee03e80f2017-02-15 08:42:14107 EXPECT_EQ(Value::Type::BINARY, value.type());
jdoerrie5f12b6272017-04-18 10:22:41108 EXPECT_EQ(Value::BlobStorage({0xF, 0x0, 0x0, 0xB, 0xA, 0x2}),
109 value.GetBlob());
jdoerriee03e80f2017-02-15 08:42:14110}
111
jdoerrie8e945542017-02-17 13:54:49112TEST(ValuesTest, ConstructDict) {
113 DictionaryValue value;
114 EXPECT_EQ(Value::Type::DICTIONARY, value.type());
115}
116
jdoerriecc9f5732017-08-23 14:12:30117TEST(ValuesTest, ConstructDictFromStorage) {
118 Value::DictStorage storage;
119 storage.emplace("foo", std::make_unique<Value>("bar"));
120 {
121 DictionaryValue value(storage);
122 EXPECT_EQ(Value::Type::DICTIONARY, value.type());
123 EXPECT_EQ(Value::Type::STRING, value.FindKey("foo")->type());
124 EXPECT_EQ("bar", value.FindKey("foo")->GetString());
125 }
126
127 *storage["foo"] = base::Value("baz");
128 {
129 DictionaryValue value(std::move(storage));
130 EXPECT_EQ(Value::Type::DICTIONARY, value.type());
131 EXPECT_EQ(Value::Type::STRING, value.FindKey("foo")->type());
132 EXPECT_EQ("baz", value.FindKey("foo")->GetString());
133 }
134}
135
jdoerrie8e945542017-02-17 13:54:49136TEST(ValuesTest, ConstructList) {
137 ListValue value;
138 EXPECT_EQ(Value::Type::LIST, value.type());
139}
140
jdoerrie52939ed2017-04-26 18:19:42141TEST(ValuesTest, ConstructListFromStorage) {
142 Value::ListStorage storage;
143 storage.emplace_back("foo");
jdoerrie52939ed2017-04-26 18:19:42144 {
145 ListValue value(storage);
146 EXPECT_EQ(Value::Type::LIST, value.type());
147 EXPECT_EQ(1u, value.GetList().size());
148 EXPECT_EQ(Value::Type::STRING, value.GetList()[0].type());
149 EXPECT_EQ("foo", value.GetList()[0].GetString());
150 }
151
152 storage.back() = base::Value("bar");
153 {
154 ListValue value(std::move(storage));
155 EXPECT_EQ(Value::Type::LIST, value.type());
156 EXPECT_EQ(1u, value.GetList().size());
157 EXPECT_EQ(Value::Type::STRING, value.GetList()[0].type());
158 EXPECT_EQ("bar", value.GetList()[0].GetString());
159 }
160}
161
jdoerrie05eb3162017-02-01 10:36:56162// Group of tests for the copy constructors and copy-assigmnent. For equality
163// checks comparisons of the interesting fields are done instead of relying on
164// Equals being correct.
165TEST(ValuesTest, CopyBool) {
jdoerrie239723572017-03-02 12:09:19166 Value true_value(true);
jdoerriecc9f5732017-08-23 14:12:30167 Value copied_true_value(true_value.Clone());
jdoerrie05eb3162017-02-01 10:36:56168 EXPECT_EQ(true_value.type(), copied_true_value.type());
169 EXPECT_EQ(true_value.GetBool(), copied_true_value.GetBool());
170
jdoerrie239723572017-03-02 12:09:19171 Value false_value(false);
jdoerriecc9f5732017-08-23 14:12:30172 Value copied_false_value(false_value.Clone());
jdoerrie05eb3162017-02-01 10:36:56173 EXPECT_EQ(false_value.type(), copied_false_value.type());
174 EXPECT_EQ(false_value.GetBool(), copied_false_value.GetBool());
175
176 Value blank;
177
jdoerriecc9f5732017-08-23 14:12:30178 blank = true_value.Clone();
jdoerrie05eb3162017-02-01 10:36:56179 EXPECT_EQ(true_value.type(), blank.type());
180 EXPECT_EQ(true_value.GetBool(), blank.GetBool());
181
jdoerriecc9f5732017-08-23 14:12:30182 blank = false_value.Clone();
jdoerrie05eb3162017-02-01 10:36:56183 EXPECT_EQ(false_value.type(), blank.type());
184 EXPECT_EQ(false_value.GetBool(), blank.GetBool());
185}
186
187TEST(ValuesTest, CopyInt) {
jdoerrie239723572017-03-02 12:09:19188 Value value(74);
jdoerriecc9f5732017-08-23 14:12:30189 Value copied_value(value.Clone());
jdoerrie05eb3162017-02-01 10:36:56190 EXPECT_EQ(value.type(), copied_value.type());
191 EXPECT_EQ(value.GetInt(), copied_value.GetInt());
192
193 Value blank;
194
jdoerriecc9f5732017-08-23 14:12:30195 blank = value.Clone();
jdoerrie05eb3162017-02-01 10:36:56196 EXPECT_EQ(value.type(), blank.type());
197 EXPECT_EQ(value.GetInt(), blank.GetInt());
198}
199
200TEST(ValuesTest, CopyDouble) {
jdoerrie239723572017-03-02 12:09:19201 Value value(74.896);
jdoerriecc9f5732017-08-23 14:12:30202 Value copied_value(value.Clone());
jdoerrie05eb3162017-02-01 10:36:56203 EXPECT_EQ(value.type(), copied_value.type());
204 EXPECT_EQ(value.GetDouble(), copied_value.GetDouble());
205
206 Value blank;
207
jdoerriecc9f5732017-08-23 14:12:30208 blank = value.Clone();
jdoerrie05eb3162017-02-01 10:36:56209 EXPECT_EQ(value.type(), blank.type());
210 EXPECT_EQ(value.GetDouble(), blank.GetDouble());
211}
212
jdoerrief38f37b2017-02-01 14:38:32213TEST(ValuesTest, CopyString) {
jdoerrie122c4da2017-03-06 11:12:04214 Value value("foobar");
jdoerriecc9f5732017-08-23 14:12:30215 Value copied_value(value.Clone());
jdoerrief38f37b2017-02-01 14:38:32216 EXPECT_EQ(value.type(), copied_value.type());
217 EXPECT_EQ(value.GetString(), copied_value.GetString());
218
219 Value blank;
220
jdoerriecc9f5732017-08-23 14:12:30221 blank = value.Clone();
jdoerrief38f37b2017-02-01 14:38:32222 EXPECT_EQ(value.type(), blank.type());
223 EXPECT_EQ(value.GetString(), blank.GetString());
224}
225
jdoerriee03e80f2017-02-15 08:42:14226TEST(ValuesTest, CopyBinary) {
jdoerrie5f12b6272017-04-18 10:22:41227 Value value(Value::BlobStorage({0xF, 0x0, 0x0, 0xB, 0xA, 0x2}));
jdoerriecc9f5732017-08-23 14:12:30228 Value copied_value(value.Clone());
jdoerriee03e80f2017-02-15 08:42:14229 EXPECT_EQ(value.type(), copied_value.type());
230 EXPECT_EQ(value.GetBlob(), copied_value.GetBlob());
231
232 Value blank;
233
jdoerriecc9f5732017-08-23 14:12:30234 blank = value.Clone();
jdoerriee03e80f2017-02-15 08:42:14235 EXPECT_EQ(value.type(), blank.type());
236 EXPECT_EQ(value.GetBlob(), blank.GetBlob());
237}
238
jdoerrie8e945542017-02-17 13:54:49239TEST(ValuesTest, CopyDictionary) {
jdoerrie44efa9d2017-07-14 14:47:20240 Value::DictStorage storage;
Jeremy Roman9532f252017-08-16 23:27:24241 storage.emplace("Int", std::make_unique<Value>(123));
jdoerrie44efa9d2017-07-14 14:47:20242 Value value(std::move(storage));
jdoerrie8e945542017-02-17 13:54:49243
jdoerriecc9f5732017-08-23 14:12:30244 Value copied_value(value.Clone());
jdoerrie44efa9d2017-07-14 14:47:20245 EXPECT_EQ(value, copied_value);
jdoerrie8e945542017-02-17 13:54:49246
jdoerrie44efa9d2017-07-14 14:47:20247 Value blank;
jdoerriecc9f5732017-08-23 14:12:30248 blank = value.Clone();
jdoerrie44efa9d2017-07-14 14:47:20249 EXPECT_EQ(value, blank);
jdoerrie8e945542017-02-17 13:54:49250}
251
252TEST(ValuesTest, CopyList) {
jdoerriecc9f5732017-08-23 14:12:30253 Value::ListStorage storage;
254 storage.emplace_back(123);
255 Value value(std::move(storage));
jdoerrie8e945542017-02-17 13:54:49256
jdoerriecc9f5732017-08-23 14:12:30257 Value copied_value(value.Clone());
jdoerrie2b7d0fcd2017-04-19 07:15:38258 EXPECT_EQ(value, copied_value);
jdoerrie8e945542017-02-17 13:54:49259
jdoerrie2b7d0fcd2017-04-19 07:15:38260 Value blank;
jdoerriecc9f5732017-08-23 14:12:30261 blank = value.Clone();
jdoerrie2b7d0fcd2017-04-19 07:15:38262 EXPECT_EQ(value, blank);
jdoerrie8e945542017-02-17 13:54:49263}
264
jdoerrie05eb3162017-02-01 10:36:56265// Group of tests for the move constructors and move-assigmnent.
266TEST(ValuesTest, MoveBool) {
jdoerrie239723572017-03-02 12:09:19267 Value true_value(true);
268 Value moved_true_value(std::move(true_value));
jdoerrie05eb3162017-02-01 10:36:56269 EXPECT_EQ(Value::Type::BOOLEAN, moved_true_value.type());
270 EXPECT_TRUE(moved_true_value.GetBool());
271
jdoerrie239723572017-03-02 12:09:19272 Value false_value(false);
273 Value moved_false_value(std::move(false_value));
jdoerrie05eb3162017-02-01 10:36:56274 EXPECT_EQ(Value::Type::BOOLEAN, moved_false_value.type());
275 EXPECT_FALSE(moved_false_value.GetBool());
276
277 Value blank;
278
jdoerrie239723572017-03-02 12:09:19279 blank = Value(true);
jdoerrie05eb3162017-02-01 10:36:56280 EXPECT_EQ(Value::Type::BOOLEAN, blank.type());
281 EXPECT_TRUE(blank.GetBool());
282
jdoerrie239723572017-03-02 12:09:19283 blank = Value(false);
jdoerrie05eb3162017-02-01 10:36:56284 EXPECT_EQ(Value::Type::BOOLEAN, blank.type());
285 EXPECT_FALSE(blank.GetBool());
286}
287
288TEST(ValuesTest, MoveInt) {
jdoerrie239723572017-03-02 12:09:19289 Value value(74);
290 Value moved_value(std::move(value));
jdoerrie05eb3162017-02-01 10:36:56291 EXPECT_EQ(Value::Type::INTEGER, moved_value.type());
292 EXPECT_EQ(74, moved_value.GetInt());
293
294 Value blank;
295
jdoerrie239723572017-03-02 12:09:19296 blank = Value(47);
jdoerrie05eb3162017-02-01 10:36:56297 EXPECT_EQ(Value::Type::INTEGER, blank.type());
298 EXPECT_EQ(47, blank.GetInt());
299}
300
301TEST(ValuesTest, MoveDouble) {
jdoerrie239723572017-03-02 12:09:19302 Value value(74.896);
303 Value moved_value(std::move(value));
jdoerrie05eb3162017-02-01 10:36:56304 EXPECT_EQ(Value::Type::DOUBLE, moved_value.type());
305 EXPECT_EQ(74.896, moved_value.GetDouble());
306
307 Value blank;
308
jdoerrie239723572017-03-02 12:09:19309 blank = Value(654.38);
jdoerrie05eb3162017-02-01 10:36:56310 EXPECT_EQ(Value::Type::DOUBLE, blank.type());
311 EXPECT_EQ(654.38, blank.GetDouble());
312}
313
jdoerrief38f37b2017-02-01 14:38:32314TEST(ValuesTest, MoveString) {
jdoerrie122c4da2017-03-06 11:12:04315 Value value("foobar");
316 Value moved_value(std::move(value));
jdoerrief38f37b2017-02-01 14:38:32317 EXPECT_EQ(Value::Type::STRING, moved_value.type());
318 EXPECT_EQ("foobar", moved_value.GetString());
319
320 Value blank;
321
jdoerrie122c4da2017-03-06 11:12:04322 blank = Value("foobar");
jdoerrief38f37b2017-02-01 14:38:32323 EXPECT_EQ(Value::Type::STRING, blank.type());
324 EXPECT_EQ("foobar", blank.GetString());
325}
326
jdoerriee03e80f2017-02-15 08:42:14327TEST(ValuesTest, MoveBinary) {
jdoerrie5f12b6272017-04-18 10:22:41328 const Value::BlobStorage buffer = {0xF, 0x0, 0x0, 0xB, 0xA, 0x2};
jdoerrie14b25da2017-04-11 07:45:50329 Value value(buffer);
330 Value moved_value(std::move(value));
jdoerriee03e80f2017-02-15 08:42:14331 EXPECT_EQ(Value::Type::BINARY, moved_value.type());
332 EXPECT_EQ(buffer, moved_value.GetBlob());
333
334 Value blank;
335
jdoerrie14b25da2017-04-11 07:45:50336 blank = Value(buffer);
jdoerriee03e80f2017-02-15 08:42:14337 EXPECT_EQ(Value::Type::BINARY, blank.type());
338 EXPECT_EQ(buffer, blank.GetBlob());
339}
340
jdoerrie44efa9d2017-07-14 14:47:20341TEST(ValuesTest, MoveConstructDictionary) {
342 Value::DictStorage storage;
Jeremy Roman9532f252017-08-16 23:27:24343 storage.emplace("Int", std::make_unique<Value>(123));
jdoerrie8e945542017-02-17 13:54:49344
jdoerrie44efa9d2017-07-14 14:47:20345 Value value(std::move(storage));
346 Value moved_value(std::move(value));
jdoerrie8e945542017-02-17 13:54:49347 EXPECT_EQ(Value::Type::DICTIONARY, moved_value.type());
jdoerrie78ab7a22017-08-17 19:04:45348 EXPECT_EQ(123, moved_value.FindKey("Int")->GetInt());
jdoerrie44efa9d2017-07-14 14:47:20349}
350
351TEST(ValuesTest, MoveAssignDictionary) {
352 Value::DictStorage storage;
Jeremy Roman9532f252017-08-16 23:27:24353 storage.emplace("Int", std::make_unique<Value>(123));
jdoerrie8e945542017-02-17 13:54:49354
355 Value blank;
jdoerrie44efa9d2017-07-14 14:47:20356 blank = Value(std::move(storage));
jdoerrie8e945542017-02-17 13:54:49357 EXPECT_EQ(Value::Type::DICTIONARY, blank.type());
jdoerrie78ab7a22017-08-17 19:04:45358 EXPECT_EQ(123, blank.FindKey("Int")->GetInt());
jdoerrie8e945542017-02-17 13:54:49359}
360
361TEST(ValuesTest, MoveList) {
jdoerriecc9f5732017-08-23 14:12:30362 Value::ListStorage storage;
363 storage.emplace_back(123);
364 Value value(storage);
jdoerrie2b7d0fcd2017-04-19 07:15:38365 Value moved_value(std::move(value));
jdoerrie8e945542017-02-17 13:54:49366 EXPECT_EQ(Value::Type::LIST, moved_value.type());
jdoerrie2b7d0fcd2017-04-19 07:15:38367 EXPECT_EQ(123, moved_value.GetList().back().GetInt());
jdoerrie8e945542017-02-17 13:54:49368
369 Value blank;
jdoerriecc9f5732017-08-23 14:12:30370 blank = Value(std::move(storage));
jdoerrie8e945542017-02-17 13:54:49371 EXPECT_EQ(Value::Type::LIST, blank.type());
jdoerrie2b7d0fcd2017-04-19 07:15:38372 EXPECT_EQ(123, blank.GetList().back().GetInt());
jdoerrie8e945542017-02-17 13:54:49373}
374
jdoerrie44efa9d2017-07-14 14:47:20375TEST(ValuesTest, FindKey) {
376 Value::DictStorage storage;
Jeremy Roman9532f252017-08-16 23:27:24377 storage.emplace("foo", std::make_unique<Value>("bar"));
jdoerrie44efa9d2017-07-14 14:47:20378 Value dict(std::move(storage));
jdoerrie78ab7a22017-08-17 19:04:45379 EXPECT_NE(nullptr, dict.FindKey("foo"));
380 EXPECT_EQ(nullptr, dict.FindKey("baz"));
jdoerrie8deeec22017-10-27 13:39:48381
382 // Single not found key.
383 bool found = dict.FindKey("notfound");
384 EXPECT_FALSE(found);
jdoerrie44efa9d2017-07-14 14:47:20385}
386
387TEST(ValuesTest, FindKeyChangeValue) {
388 Value::DictStorage storage;
Jeremy Roman9532f252017-08-16 23:27:24389 storage.emplace("foo", std::make_unique<Value>("bar"));
jdoerrie44efa9d2017-07-14 14:47:20390 Value dict(std::move(storage));
jdoerrie78ab7a22017-08-17 19:04:45391 Value* found = dict.FindKey("foo");
392 EXPECT_NE(nullptr, found);
393 EXPECT_EQ("bar", found->GetString());
jdoerrie44efa9d2017-07-14 14:47:20394
jdoerrie78ab7a22017-08-17 19:04:45395 *found = Value(123);
396 EXPECT_EQ(123, dict.FindKey("foo")->GetInt());
jdoerrie44efa9d2017-07-14 14:47:20397}
398
399TEST(ValuesTest, FindKeyConst) {
400 Value::DictStorage storage;
Jeremy Roman9532f252017-08-16 23:27:24401 storage.emplace("foo", std::make_unique<Value>("bar"));
jdoerrie44efa9d2017-07-14 14:47:20402 const Value dict(std::move(storage));
jdoerrie78ab7a22017-08-17 19:04:45403 EXPECT_NE(nullptr, dict.FindKey("foo"));
404 EXPECT_EQ(nullptr, dict.FindKey("baz"));
jdoerrie44efa9d2017-07-14 14:47:20405}
406
407TEST(ValuesTest, FindKeyOfType) {
408 Value::DictStorage storage;
Jeremy Roman9532f252017-08-16 23:27:24409 storage.emplace("null", std::make_unique<Value>(Value::Type::NONE));
410 storage.emplace("bool", std::make_unique<Value>(Value::Type::BOOLEAN));
411 storage.emplace("int", std::make_unique<Value>(Value::Type::INTEGER));
412 storage.emplace("double", std::make_unique<Value>(Value::Type::DOUBLE));
413 storage.emplace("string", std::make_unique<Value>(Value::Type::STRING));
414 storage.emplace("blob", std::make_unique<Value>(Value::Type::BINARY));
415 storage.emplace("list", std::make_unique<Value>(Value::Type::LIST));
416 storage.emplace("dict", std::make_unique<Value>(Value::Type::DICTIONARY));
jdoerrie44efa9d2017-07-14 14:47:20417
418 Value dict(std::move(storage));
jdoerrie78ab7a22017-08-17 19:04:45419 EXPECT_NE(nullptr, dict.FindKeyOfType("null", Value::Type::NONE));
420 EXPECT_EQ(nullptr, dict.FindKeyOfType("null", Value::Type::BOOLEAN));
421 EXPECT_EQ(nullptr, dict.FindKeyOfType("null", Value::Type::INTEGER));
422 EXPECT_EQ(nullptr, dict.FindKeyOfType("null", Value::Type::DOUBLE));
423 EXPECT_EQ(nullptr, dict.FindKeyOfType("null", Value::Type::STRING));
424 EXPECT_EQ(nullptr, dict.FindKeyOfType("null", Value::Type::BINARY));
425 EXPECT_EQ(nullptr, dict.FindKeyOfType("null", Value::Type::LIST));
426 EXPECT_EQ(nullptr, dict.FindKeyOfType("null", Value::Type::DICTIONARY));
jdoerrie44efa9d2017-07-14 14:47:20427
jdoerrie78ab7a22017-08-17 19:04:45428 EXPECT_EQ(nullptr, dict.FindKeyOfType("bool", Value::Type::NONE));
429 EXPECT_NE(nullptr, dict.FindKeyOfType("bool", Value::Type::BOOLEAN));
430 EXPECT_EQ(nullptr, dict.FindKeyOfType("bool", Value::Type::INTEGER));
431 EXPECT_EQ(nullptr, dict.FindKeyOfType("bool", Value::Type::DOUBLE));
432 EXPECT_EQ(nullptr, dict.FindKeyOfType("bool", Value::Type::STRING));
433 EXPECT_EQ(nullptr, dict.FindKeyOfType("bool", Value::Type::BINARY));
434 EXPECT_EQ(nullptr, dict.FindKeyOfType("bool", Value::Type::LIST));
435 EXPECT_EQ(nullptr, dict.FindKeyOfType("bool", Value::Type::DICTIONARY));
jdoerrie44efa9d2017-07-14 14:47:20436
jdoerrie78ab7a22017-08-17 19:04:45437 EXPECT_EQ(nullptr, dict.FindKeyOfType("int", Value::Type::NONE));
438 EXPECT_EQ(nullptr, dict.FindKeyOfType("int", Value::Type::BOOLEAN));
439 EXPECT_NE(nullptr, dict.FindKeyOfType("int", Value::Type::INTEGER));
440 EXPECT_EQ(nullptr, dict.FindKeyOfType("int", Value::Type::DOUBLE));
441 EXPECT_EQ(nullptr, dict.FindKeyOfType("int", Value::Type::STRING));
442 EXPECT_EQ(nullptr, dict.FindKeyOfType("int", Value::Type::BINARY));
443 EXPECT_EQ(nullptr, dict.FindKeyOfType("int", Value::Type::LIST));
444 EXPECT_EQ(nullptr, dict.FindKeyOfType("int", Value::Type::DICTIONARY));
jdoerrie44efa9d2017-07-14 14:47:20445
jdoerrie78ab7a22017-08-17 19:04:45446 EXPECT_EQ(nullptr, dict.FindKeyOfType("double", Value::Type::NONE));
447 EXPECT_EQ(nullptr, dict.FindKeyOfType("double", Value::Type::BOOLEAN));
448 EXPECT_EQ(nullptr, dict.FindKeyOfType("double", Value::Type::INTEGER));
449 EXPECT_NE(nullptr, dict.FindKeyOfType("double", Value::Type::DOUBLE));
450 EXPECT_EQ(nullptr, dict.FindKeyOfType("double", Value::Type::STRING));
451 EXPECT_EQ(nullptr, dict.FindKeyOfType("double", Value::Type::BINARY));
452 EXPECT_EQ(nullptr, dict.FindKeyOfType("double", Value::Type::LIST));
453 EXPECT_EQ(nullptr, dict.FindKeyOfType("double", Value::Type::DICTIONARY));
jdoerrie44efa9d2017-07-14 14:47:20454
jdoerrie78ab7a22017-08-17 19:04:45455 EXPECT_EQ(nullptr, dict.FindKeyOfType("string", Value::Type::NONE));
456 EXPECT_EQ(nullptr, dict.FindKeyOfType("string", Value::Type::BOOLEAN));
457 EXPECT_EQ(nullptr, dict.FindKeyOfType("string", Value::Type::INTEGER));
458 EXPECT_EQ(nullptr, dict.FindKeyOfType("string", Value::Type::DOUBLE));
459 EXPECT_NE(nullptr, dict.FindKeyOfType("string", Value::Type::STRING));
460 EXPECT_EQ(nullptr, dict.FindKeyOfType("string", Value::Type::BINARY));
461 EXPECT_EQ(nullptr, dict.FindKeyOfType("string", Value::Type::LIST));
462 EXPECT_EQ(nullptr, dict.FindKeyOfType("string", Value::Type::DICTIONARY));
jdoerrie44efa9d2017-07-14 14:47:20463
jdoerrie78ab7a22017-08-17 19:04:45464 EXPECT_EQ(nullptr, dict.FindKeyOfType("blob", Value::Type::NONE));
465 EXPECT_EQ(nullptr, dict.FindKeyOfType("blob", Value::Type::BOOLEAN));
466 EXPECT_EQ(nullptr, dict.FindKeyOfType("blob", Value::Type::INTEGER));
467 EXPECT_EQ(nullptr, dict.FindKeyOfType("blob", Value::Type::DOUBLE));
468 EXPECT_EQ(nullptr, dict.FindKeyOfType("blob", Value::Type::STRING));
469 EXPECT_NE(nullptr, dict.FindKeyOfType("blob", Value::Type::BINARY));
470 EXPECT_EQ(nullptr, dict.FindKeyOfType("blob", Value::Type::LIST));
471 EXPECT_EQ(nullptr, dict.FindKeyOfType("blob", Value::Type::DICTIONARY));
jdoerrie44efa9d2017-07-14 14:47:20472
jdoerrie78ab7a22017-08-17 19:04:45473 EXPECT_EQ(nullptr, dict.FindKeyOfType("list", Value::Type::NONE));
474 EXPECT_EQ(nullptr, dict.FindKeyOfType("list", Value::Type::BOOLEAN));
475 EXPECT_EQ(nullptr, dict.FindKeyOfType("list", Value::Type::INTEGER));
476 EXPECT_EQ(nullptr, dict.FindKeyOfType("list", Value::Type::DOUBLE));
477 EXPECT_EQ(nullptr, dict.FindKeyOfType("list", Value::Type::STRING));
478 EXPECT_EQ(nullptr, dict.FindKeyOfType("list", Value::Type::BINARY));
479 EXPECT_NE(nullptr, dict.FindKeyOfType("list", Value::Type::LIST));
480 EXPECT_EQ(nullptr, dict.FindKeyOfType("list", Value::Type::DICTIONARY));
jdoerrie44efa9d2017-07-14 14:47:20481
jdoerrie78ab7a22017-08-17 19:04:45482 EXPECT_EQ(nullptr, dict.FindKeyOfType("dict", Value::Type::NONE));
483 EXPECT_EQ(nullptr, dict.FindKeyOfType("dict", Value::Type::BOOLEAN));
484 EXPECT_EQ(nullptr, dict.FindKeyOfType("dict", Value::Type::INTEGER));
485 EXPECT_EQ(nullptr, dict.FindKeyOfType("dict", Value::Type::DOUBLE));
486 EXPECT_EQ(nullptr, dict.FindKeyOfType("dict", Value::Type::STRING));
487 EXPECT_EQ(nullptr, dict.FindKeyOfType("dict", Value::Type::BINARY));
488 EXPECT_EQ(nullptr, dict.FindKeyOfType("dict", Value::Type::LIST));
489 EXPECT_NE(nullptr, dict.FindKeyOfType("dict", Value::Type::DICTIONARY));
jdoerrie44efa9d2017-07-14 14:47:20490}
491
492TEST(ValuesTest, FindKeyOfTypeConst) {
493 Value::DictStorage storage;
Jeremy Roman9532f252017-08-16 23:27:24494 storage.emplace("null", std::make_unique<Value>(Value::Type::NONE));
495 storage.emplace("bool", std::make_unique<Value>(Value::Type::BOOLEAN));
496 storage.emplace("int", std::make_unique<Value>(Value::Type::INTEGER));
497 storage.emplace("double", std::make_unique<Value>(Value::Type::DOUBLE));
498 storage.emplace("string", std::make_unique<Value>(Value::Type::STRING));
499 storage.emplace("blob", std::make_unique<Value>(Value::Type::BINARY));
500 storage.emplace("list", std::make_unique<Value>(Value::Type::LIST));
501 storage.emplace("dict", std::make_unique<Value>(Value::Type::DICTIONARY));
jdoerrie44efa9d2017-07-14 14:47:20502
503 const Value dict(std::move(storage));
jdoerrie78ab7a22017-08-17 19:04:45504 EXPECT_NE(nullptr, dict.FindKeyOfType("null", Value::Type::NONE));
505 EXPECT_EQ(nullptr, dict.FindKeyOfType("null", Value::Type::BOOLEAN));
506 EXPECT_EQ(nullptr, dict.FindKeyOfType("null", Value::Type::INTEGER));
507 EXPECT_EQ(nullptr, dict.FindKeyOfType("null", Value::Type::DOUBLE));
508 EXPECT_EQ(nullptr, dict.FindKeyOfType("null", Value::Type::STRING));
509 EXPECT_EQ(nullptr, dict.FindKeyOfType("null", Value::Type::BINARY));
510 EXPECT_EQ(nullptr, dict.FindKeyOfType("null", Value::Type::LIST));
511 EXPECT_EQ(nullptr, dict.FindKeyOfType("null", Value::Type::DICTIONARY));
jdoerrie44efa9d2017-07-14 14:47:20512
jdoerrie78ab7a22017-08-17 19:04:45513 EXPECT_EQ(nullptr, dict.FindKeyOfType("bool", Value::Type::NONE));
514 EXPECT_NE(nullptr, dict.FindKeyOfType("bool", Value::Type::BOOLEAN));
515 EXPECT_EQ(nullptr, dict.FindKeyOfType("bool", Value::Type::INTEGER));
516 EXPECT_EQ(nullptr, dict.FindKeyOfType("bool", Value::Type::DOUBLE));
517 EXPECT_EQ(nullptr, dict.FindKeyOfType("bool", Value::Type::STRING));
518 EXPECT_EQ(nullptr, dict.FindKeyOfType("bool", Value::Type::BINARY));
519 EXPECT_EQ(nullptr, dict.FindKeyOfType("bool", Value::Type::LIST));
520 EXPECT_EQ(nullptr, dict.FindKeyOfType("bool", Value::Type::DICTIONARY));
jdoerrie44efa9d2017-07-14 14:47:20521
jdoerrie78ab7a22017-08-17 19:04:45522 EXPECT_EQ(nullptr, dict.FindKeyOfType("int", Value::Type::NONE));
523 EXPECT_EQ(nullptr, dict.FindKeyOfType("int", Value::Type::BOOLEAN));
524 EXPECT_NE(nullptr, dict.FindKeyOfType("int", Value::Type::INTEGER));
525 EXPECT_EQ(nullptr, dict.FindKeyOfType("int", Value::Type::DOUBLE));
526 EXPECT_EQ(nullptr, dict.FindKeyOfType("int", Value::Type::STRING));
527 EXPECT_EQ(nullptr, dict.FindKeyOfType("int", Value::Type::BINARY));
528 EXPECT_EQ(nullptr, dict.FindKeyOfType("int", Value::Type::LIST));
529 EXPECT_EQ(nullptr, dict.FindKeyOfType("int", Value::Type::DICTIONARY));
jdoerrie44efa9d2017-07-14 14:47:20530
jdoerrie78ab7a22017-08-17 19:04:45531 EXPECT_EQ(nullptr, dict.FindKeyOfType("double", Value::Type::NONE));
532 EXPECT_EQ(nullptr, dict.FindKeyOfType("double", Value::Type::BOOLEAN));
533 EXPECT_EQ(nullptr, dict.FindKeyOfType("double", Value::Type::INTEGER));
534 EXPECT_NE(nullptr, dict.FindKeyOfType("double", Value::Type::DOUBLE));
535 EXPECT_EQ(nullptr, dict.FindKeyOfType("double", Value::Type::STRING));
536 EXPECT_EQ(nullptr, dict.FindKeyOfType("double", Value::Type::BINARY));
537 EXPECT_EQ(nullptr, dict.FindKeyOfType("double", Value::Type::LIST));
538 EXPECT_EQ(nullptr, dict.FindKeyOfType("double", Value::Type::DICTIONARY));
jdoerrie44efa9d2017-07-14 14:47:20539
jdoerrie78ab7a22017-08-17 19:04:45540 EXPECT_EQ(nullptr, dict.FindKeyOfType("string", Value::Type::NONE));
541 EXPECT_EQ(nullptr, dict.FindKeyOfType("string", Value::Type::BOOLEAN));
542 EXPECT_EQ(nullptr, dict.FindKeyOfType("string", Value::Type::INTEGER));
543 EXPECT_EQ(nullptr, dict.FindKeyOfType("string", Value::Type::DOUBLE));
544 EXPECT_NE(nullptr, dict.FindKeyOfType("string", Value::Type::STRING));
545 EXPECT_EQ(nullptr, dict.FindKeyOfType("string", Value::Type::BINARY));
546 EXPECT_EQ(nullptr, dict.FindKeyOfType("string", Value::Type::LIST));
547 EXPECT_EQ(nullptr, dict.FindKeyOfType("string", Value::Type::DICTIONARY));
jdoerrie44efa9d2017-07-14 14:47:20548
jdoerrie78ab7a22017-08-17 19:04:45549 EXPECT_EQ(nullptr, dict.FindKeyOfType("blob", Value::Type::NONE));
550 EXPECT_EQ(nullptr, dict.FindKeyOfType("blob", Value::Type::BOOLEAN));
551 EXPECT_EQ(nullptr, dict.FindKeyOfType("blob", Value::Type::INTEGER));
552 EXPECT_EQ(nullptr, dict.FindKeyOfType("blob", Value::Type::DOUBLE));
553 EXPECT_EQ(nullptr, dict.FindKeyOfType("blob", Value::Type::STRING));
554 EXPECT_NE(nullptr, dict.FindKeyOfType("blob", Value::Type::BINARY));
555 EXPECT_EQ(nullptr, dict.FindKeyOfType("blob", Value::Type::LIST));
556 EXPECT_EQ(nullptr, dict.FindKeyOfType("blob", Value::Type::DICTIONARY));
jdoerrie44efa9d2017-07-14 14:47:20557
jdoerrie78ab7a22017-08-17 19:04:45558 EXPECT_EQ(nullptr, dict.FindKeyOfType("list", Value::Type::NONE));
559 EXPECT_EQ(nullptr, dict.FindKeyOfType("list", Value::Type::BOOLEAN));
560 EXPECT_EQ(nullptr, dict.FindKeyOfType("list", Value::Type::INTEGER));
561 EXPECT_EQ(nullptr, dict.FindKeyOfType("list", Value::Type::DOUBLE));
562 EXPECT_EQ(nullptr, dict.FindKeyOfType("list", Value::Type::STRING));
563 EXPECT_EQ(nullptr, dict.FindKeyOfType("list", Value::Type::BINARY));
564 EXPECT_NE(nullptr, dict.FindKeyOfType("list", Value::Type::LIST));
565 EXPECT_EQ(nullptr, dict.FindKeyOfType("list", Value::Type::DICTIONARY));
jdoerrie44efa9d2017-07-14 14:47:20566
jdoerrie78ab7a22017-08-17 19:04:45567 EXPECT_EQ(nullptr, dict.FindKeyOfType("dict", Value::Type::NONE));
568 EXPECT_EQ(nullptr, dict.FindKeyOfType("dict", Value::Type::BOOLEAN));
569 EXPECT_EQ(nullptr, dict.FindKeyOfType("dict", Value::Type::INTEGER));
570 EXPECT_EQ(nullptr, dict.FindKeyOfType("dict", Value::Type::DOUBLE));
571 EXPECT_EQ(nullptr, dict.FindKeyOfType("dict", Value::Type::STRING));
572 EXPECT_EQ(nullptr, dict.FindKeyOfType("dict", Value::Type::BINARY));
573 EXPECT_EQ(nullptr, dict.FindKeyOfType("dict", Value::Type::LIST));
574 EXPECT_NE(nullptr, dict.FindKeyOfType("dict", Value::Type::DICTIONARY));
jdoerrie44efa9d2017-07-14 14:47:20575}
576
577TEST(ValuesTest, SetKey) {
578 Value::DictStorage storage;
Jeremy Roman9532f252017-08-16 23:27:24579 storage.emplace("null", std::make_unique<Value>(Value::Type::NONE));
580 storage.emplace("bool", std::make_unique<Value>(Value::Type::BOOLEAN));
581 storage.emplace("int", std::make_unique<Value>(Value::Type::INTEGER));
582 storage.emplace("double", std::make_unique<Value>(Value::Type::DOUBLE));
583 storage.emplace("string", std::make_unique<Value>(Value::Type::STRING));
584 storage.emplace("blob", std::make_unique<Value>(Value::Type::BINARY));
585 storage.emplace("list", std::make_unique<Value>(Value::Type::LIST));
586 storage.emplace("dict", std::make_unique<Value>(Value::Type::DICTIONARY));
jdoerrie44efa9d2017-07-14 14:47:20587
588 Value dict(Value::Type::DICTIONARY);
jdoerrie46349472017-08-02 02:20:32589 dict.SetKey(StringPiece("null"), Value(Value::Type::NONE));
590 dict.SetKey(StringPiece("bool"), Value(Value::Type::BOOLEAN));
591 dict.SetKey(std::string("int"), Value(Value::Type::INTEGER));
592 dict.SetKey(std::string("double"), Value(Value::Type::DOUBLE));
593 dict.SetKey(std::string("string"), Value(Value::Type::STRING));
jdoerrie44efa9d2017-07-14 14:47:20594 dict.SetKey("blob", Value(Value::Type::BINARY));
595 dict.SetKey("list", Value(Value::Type::LIST));
596 dict.SetKey("dict", Value(Value::Type::DICTIONARY));
597
598 EXPECT_EQ(Value(std::move(storage)), dict);
599}
600
Brett Wilsond16cf4ee2017-08-03 00:08:27601TEST(ValuesTest, FindPath) {
602 // Construct a dictionary path {root}.foo.bar = 123
603 Value foo(Value::Type::DICTIONARY);
604 foo.SetKey("bar", Value(123));
605
606 Value root(Value::Type::DICTIONARY);
607 root.SetKey("foo", std::move(foo));
608
609 // No key (stupid but well-defined and takes work to prevent).
jdoerrie8deeec22017-10-27 13:39:48610 Value* found = root.FindPath(std::vector<StringPiece>{});
Brett Wilsond16cf4ee2017-08-03 00:08:27611 EXPECT_EQ(&root, found);
612
Brett Wilsond16cf4ee2017-08-03 00:08:27613 // Double key, second not found.
jdoerriecd022242017-08-23 08:38:27614 found = root.FindPath(std::vector<StringPiece>{"foo", "notfound"});
Brett Wilsond16cf4ee2017-08-03 00:08:27615 EXPECT_FALSE(found);
616
617 // Double key, found.
jdoerriecd022242017-08-23 08:38:27618 found = root.FindPath(std::vector<StringPiece>{"foo", "bar"});
Brett Wilsond16cf4ee2017-08-03 00:08:27619 EXPECT_TRUE(found);
620 EXPECT_TRUE(found->is_int());
621 EXPECT_EQ(123, found->GetInt());
622}
623
624TEST(ValuesTest, SetPath) {
625 Value root(Value::Type::DICTIONARY);
626
jdoerrie8deeec22017-10-27 13:39:48627 Value* inserted = root.SetPath({"one", "two"}, Value(123));
628 Value* found = root.FindPathOfType({"one", "two"}, Value::Type::INTEGER);
Brett Wilsond16cf4ee2017-08-03 00:08:27629 ASSERT_TRUE(found);
630 EXPECT_EQ(inserted, found);
631 EXPECT_EQ(123, found->GetInt());
632
jdoerriecd022242017-08-23 08:38:27633 inserted = root.SetPath(std::vector<StringPiece>{"foo", "bar"}, Value(123));
Brett Wilsond16cf4ee2017-08-03 00:08:27634 found = root.FindPathOfType({"foo", "bar"}, Value::Type::INTEGER);
635 ASSERT_TRUE(found);
636 EXPECT_EQ(inserted, found);
637 EXPECT_EQ(123, found->GetInt());
638
639 // Overwrite with a different value.
640 root.SetPath({"foo", "bar"}, Value("hello"));
jdoerriecd022242017-08-23 08:38:27641 found = root.FindPathOfType(std::vector<StringPiece>{"foo", "bar"},
642 Value::Type::STRING);
Brett Wilsond16cf4ee2017-08-03 00:08:27643 ASSERT_TRUE(found);
644 EXPECT_EQ("hello", found->GetString());
645
646 // Can't change existing non-dictionary keys to dictionaries.
jdoerriecd022242017-08-23 08:38:27647 found =
648 root.SetPath(std::vector<StringPiece>{"foo", "bar", "baz"}, Value(123));
Brett Wilsond16cf4ee2017-08-03 00:08:27649 EXPECT_FALSE(found);
650}
651
jdoerrie64783162017-09-04 16:33:32652TEST(ValuesTest, RemoveKey) {
653 Value root(Value::Type::DICTIONARY);
654 root.SetKey("one", Value(123));
655
656 // Removal of missing key should fail.
657 EXPECT_FALSE(root.RemoveKey("two"));
658
659 // Removal of existing key should succeed.
660 EXPECT_TRUE(root.RemoveKey("one"));
661
662 // Second removal of previously existing key should fail.
663 EXPECT_FALSE(root.RemoveKey("one"));
664}
665
666TEST(ValuesTest, RemovePath) {
667 Value root(Value::Type::DICTIONARY);
668 root.SetPath({"one", "two", "three"}, Value(123));
669
670 // Removal of missing key should fail.
671 EXPECT_FALSE(root.RemovePath({"one", "two", "four"}));
672
673 // Removal of existing key should succeed.
674 EXPECT_TRUE(root.RemovePath({"one", "two", "three"}));
675
676 // Second removal of previously existing key should fail.
677 EXPECT_FALSE(root.RemovePath({"one", "two", "three"}));
678
679 // Intermediate empty dictionaries should be cleared.
jdoerrie8deeec22017-10-27 13:39:48680 EXPECT_FALSE(root.FindKey("one"));
jdoerrie64783162017-09-04 16:33:32681
682 root.SetPath({"one", "two", "three"}, Value(123));
683 root.SetPath({"one", "two", "four"}, Value(124));
684
685 EXPECT_TRUE(root.RemovePath(std::vector<StringPiece>{"one", "two", "three"}));
686 // Intermediate non-empty dictionaries should be kept.
jdoerrie8deeec22017-10-27 13:39:48687 EXPECT_TRUE(root.FindKey("one"));
jdoerrie64783162017-09-04 16:33:32688 EXPECT_TRUE(root.FindPath({"one", "two"}));
689 EXPECT_TRUE(root.FindPath({"one", "two", "four"}));
690}
691
tfarina@chromium.org58b916e2011-06-28 22:56:33692TEST(ValuesTest, Basic) {
initial.commitd7cae122008-07-26 21:49:38693 // Test basic dictionary getting/setting
694 DictionaryValue settings;
viettrungluu@chromium.org9e4cda7332010-07-31 04:56:14695 std::string homepage = "http://google.com";
696 ASSERT_FALSE(settings.GetString("global.homepage", &homepage));
697 ASSERT_EQ(std::string("http://google.com"), homepage);
698
699 ASSERT_FALSE(settings.Get("global", NULL));
estade7bc801fb2015-05-07 01:53:08700 settings.SetBoolean("global", true);
viettrungluu@chromium.org9e4cda7332010-07-31 04:56:14701 ASSERT_TRUE(settings.Get("global", NULL));
702 settings.SetString("global.homepage", "http://scurvy.com");
703 ASSERT_TRUE(settings.Get("global", NULL));
704 homepage = "http://google.com";
705 ASSERT_TRUE(settings.GetString("global.homepage", &homepage));
706 ASSERT_EQ(std::string("http://scurvy.com"), homepage);
707
708 // Test storing a dictionary in a list.
709 ListValue* toolbar_bookmarks;
710 ASSERT_FALSE(
711 settings.GetList("global.toolbar.bookmarks", &toolbar_bookmarks));
712
dcheng093de9b2016-04-04 21:25:51713 std::unique_ptr<ListValue> new_toolbar_bookmarks(new ListValue);
danakj0c8d4aa2015-11-25 05:29:58714 settings.Set("global.toolbar.bookmarks", std::move(new_toolbar_bookmarks));
viettrungluu@chromium.org9e4cda7332010-07-31 04:56:14715 ASSERT_TRUE(settings.GetList("global.toolbar.bookmarks", &toolbar_bookmarks));
716
dcheng093de9b2016-04-04 21:25:51717 std::unique_ptr<DictionaryValue> new_bookmark(new DictionaryValue);
viettrungluu@chromium.org9e4cda7332010-07-31 04:56:14718 new_bookmark->SetString("name", "Froogle");
719 new_bookmark->SetString("url", "http://froogle.com");
danakj0c8d4aa2015-11-25 05:29:58720 toolbar_bookmarks->Append(std::move(new_bookmark));
viettrungluu@chromium.org9e4cda7332010-07-31 04:56:14721
722 ListValue* bookmark_list;
723 ASSERT_TRUE(settings.GetList("global.toolbar.bookmarks", &bookmark_list));
724 DictionaryValue* bookmark;
725 ASSERT_EQ(1U, bookmark_list->GetSize());
726 ASSERT_TRUE(bookmark_list->GetDictionary(0, &bookmark));
727 std::string bookmark_name = "Unnamed";
728 ASSERT_TRUE(bookmark->GetString("name", &bookmark_name));
729 ASSERT_EQ(std::string("Froogle"), bookmark_name);
730 std::string bookmark_url;
731 ASSERT_TRUE(bookmark->GetString("url", &bookmark_url));
732 ASSERT_EQ(std::string("http://froogle.com"), bookmark_url);
733}
734
tfarina@chromium.org58b916e2011-06-28 22:56:33735TEST(ValuesTest, List) {
dcheng093de9b2016-04-04 21:25:51736 std::unique_ptr<ListValue> mixed_list(new ListValue());
Jeremy Roman9532f252017-08-16 23:27:24737 mixed_list->Set(0, std::make_unique<Value>(true));
738 mixed_list->Set(1, std::make_unique<Value>(42));
739 mixed_list->Set(2, std::make_unique<Value>(88.8));
740 mixed_list->Set(3, std::make_unique<Value>("foo"));
aa@chromium.orgf82fb4952009-01-20 21:05:32741 ASSERT_EQ(4u, mixed_list->GetSize());
maruel@chromium.org52a261f2009-03-03 15:01:12742
Lei Zhang30895d52017-10-23 19:14:46743 Value* value = nullptr;
aa@chromium.orgf82fb4952009-01-20 21:05:32744 bool bool_value = false;
745 int int_value = 0;
746 double double_value = 0.0;
747 std::string string_value;
748
749 ASSERT_FALSE(mixed_list->Get(4, &value));
750
751 ASSERT_FALSE(mixed_list->GetInteger(0, &int_value));
752 ASSERT_EQ(0, int_value);
yusukes@google.comd3accda2011-05-02 01:59:21753 ASSERT_FALSE(mixed_list->GetBoolean(1, &bool_value));
754 ASSERT_FALSE(bool_value);
aa@chromium.orgf82fb4952009-01-20 21:05:32755 ASSERT_FALSE(mixed_list->GetString(2, &string_value));
756 ASSERT_EQ("", string_value);
yusukes@google.comd3accda2011-05-02 01:59:21757 ASSERT_FALSE(mixed_list->GetInteger(2, &int_value));
758 ASSERT_EQ(0, int_value);
aa@chromium.orgf82fb4952009-01-20 21:05:32759 ASSERT_FALSE(mixed_list->GetBoolean(3, &bool_value));
thakis@chromium.org74e3af72010-10-03 21:44:39760 ASSERT_FALSE(bool_value);
aa@chromium.orgf82fb4952009-01-20 21:05:32761
762 ASSERT_TRUE(mixed_list->GetBoolean(0, &bool_value));
thakis@chromium.org74e3af72010-10-03 21:44:39763 ASSERT_TRUE(bool_value);
aa@chromium.orgf82fb4952009-01-20 21:05:32764 ASSERT_TRUE(mixed_list->GetInteger(1, &int_value));
765 ASSERT_EQ(42, int_value);
yusukes@google.comd3accda2011-05-02 01:59:21766 // implicit conversion from Integer to Double should be possible.
767 ASSERT_TRUE(mixed_list->GetDouble(1, &double_value));
768 ASSERT_EQ(42, double_value);
arv@chromium.orgfb534c92011-02-01 01:02:07769 ASSERT_TRUE(mixed_list->GetDouble(2, &double_value));
aa@chromium.orgf82fb4952009-01-20 21:05:32770 ASSERT_EQ(88.8, double_value);
771 ASSERT_TRUE(mixed_list->GetString(3, &string_value));
772 ASSERT_EQ("foo", string_value);
pastarmovj@chromium.org5fb35372011-09-19 15:23:10773
774 // Try searching in the mixed list.
jdoerrie239723572017-03-02 12:09:19775 base::Value sought_value(42);
776 base::Value not_found_value(false);
pastarmovj@chromium.org5fb35372011-09-19 15:23:10777
tfarina@chromium.org7e3ec42c2012-12-16 05:13:21778 ASSERT_NE(mixed_list->end(), mixed_list->Find(sought_value));
jdoerriea5676c62017-04-11 18:09:14779 ASSERT_TRUE((*mixed_list->Find(sought_value)).GetAsInteger(&int_value));
pastarmovj@chromium.org5fb35372011-09-19 15:23:10780 ASSERT_EQ(42, int_value);
tfarina@chromium.org7e3ec42c2012-12-16 05:13:21781 ASSERT_EQ(mixed_list->end(), mixed_list->Find(not_found_value));
aa@chromium.orgf82fb4952009-01-20 21:05:32782}
783
tfarina@chromium.org58b916e2011-06-28 22:56:33784TEST(ValuesTest, BinaryValue) {
jdoerriee03e80f2017-02-15 08:42:14785 // Default constructor creates a BinaryValue with a buffer of size 0.
Jeremy Roman9532f252017-08-16 23:27:24786 auto binary = std::make_unique<Value>(Value::Type::BINARY);
patrick@chromium.org0a9a0fc2009-03-24 23:37:14787 ASSERT_TRUE(binary.get());
jdoerrie5328aff2017-04-25 20:08:15788 ASSERT_TRUE(binary->GetBlob().empty());
initial.commitd7cae122008-07-26 21:49:38789
790 // Test the common case of a non-empty buffer
jdoerrie5f12b6272017-04-18 10:22:41791 Value::BlobStorage buffer(15);
jdoerriee03e80f2017-02-15 08:42:14792 char* original_buffer = buffer.data();
jdoerrie14b25da2017-04-11 07:45:50793 binary.reset(new Value(std::move(buffer)));
patrick@chromium.org0a9a0fc2009-03-24 23:37:14794 ASSERT_TRUE(binary.get());
jdoerrie5328aff2017-04-25 20:08:15795 ASSERT_TRUE(binary->GetBlob().data());
796 ASSERT_EQ(original_buffer, binary->GetBlob().data());
797 ASSERT_EQ(15U, binary->GetBlob().size());
initial.commitd7cae122008-07-26 21:49:38798
799 char stack_buffer[42];
800 memset(stack_buffer, '!', 42);
jdoerrie14b25da2017-04-11 07:45:50801 binary = Value::CreateWithCopiedBuffer(stack_buffer, 42);
patrick@chromium.org0a9a0fc2009-03-24 23:37:14802 ASSERT_TRUE(binary.get());
jdoerrie5328aff2017-04-25 20:08:15803 ASSERT_TRUE(binary->GetBlob().data());
804 ASSERT_NE(stack_buffer, binary->GetBlob().data());
805 ASSERT_EQ(42U, binary->GetBlob().size());
806 ASSERT_EQ(0, memcmp(stack_buffer, binary->GetBlob().data(),
807 binary->GetBlob().size()));
initial.commitd7cae122008-07-26 21:49:38808}
809
tfarina@chromium.org58b916e2011-06-28 22:56:33810TEST(ValuesTest, StringValue) {
estade@chromium.orgb54e6252014-01-30 10:32:41811 // Test overloaded StringValue constructor.
jdoerrie122c4da2017-03-06 11:12:04812 std::unique_ptr<Value> narrow_value(new Value("narrow"));
patrick@chromium.org0a9a0fc2009-03-24 23:37:14813 ASSERT_TRUE(narrow_value.get());
jdoerriedc72ee942016-12-07 15:43:28814 ASSERT_TRUE(narrow_value->IsType(Value::Type::STRING));
jdoerrie122c4da2017-03-06 11:12:04815 std::unique_ptr<Value> utf16_value(new Value(ASCIIToUTF16("utf16")));
viettrungluu@chromium.orge2e593d2010-08-03 15:42:58816 ASSERT_TRUE(utf16_value.get());
jdoerriedc72ee942016-12-07 15:43:28817 ASSERT_TRUE(utf16_value->IsType(Value::Type::STRING));
viettrungluu@chromium.orge2e593d2010-08-03 15:42:58818
estade@chromium.orge5525182014-02-11 22:57:34819 // Test overloaded GetAsString.
viettrungluu@chromium.orge2e593d2010-08-03 15:42:58820 std::string narrow = "http://google.com";
821 string16 utf16 = ASCIIToUTF16("http://google.com");
jdoerrie122c4da2017-03-06 11:12:04822 const Value* string_value = NULL;
viettrungluu@chromium.orge2e593d2010-08-03 15:42:58823 ASSERT_TRUE(narrow_value->GetAsString(&narrow));
824 ASSERT_TRUE(narrow_value->GetAsString(&utf16));
estade@chromium.orge5525182014-02-11 22:57:34825 ASSERT_TRUE(narrow_value->GetAsString(&string_value));
viettrungluu@chromium.orge2e593d2010-08-03 15:42:58826 ASSERT_EQ(std::string("narrow"), narrow);
827 ASSERT_EQ(ASCIIToUTF16("narrow"), utf16);
estade@chromium.orge5525182014-02-11 22:57:34828 ASSERT_EQ(string_value->GetString(), narrow);
viettrungluu@chromium.orge2e593d2010-08-03 15:42:58829
830 ASSERT_TRUE(utf16_value->GetAsString(&narrow));
831 ASSERT_TRUE(utf16_value->GetAsString(&utf16));
estade@chromium.orge5525182014-02-11 22:57:34832 ASSERT_TRUE(utf16_value->GetAsString(&string_value));
viettrungluu@chromium.orge2e593d2010-08-03 15:42:58833 ASSERT_EQ(std::string("utf16"), narrow);
834 ASSERT_EQ(ASCIIToUTF16("utf16"), utf16);
estade@chromium.orge5525182014-02-11 22:57:34835 ASSERT_EQ(string_value->GetString(), narrow);
836
837 // Don't choke on NULL values.
838 ASSERT_TRUE(narrow_value->GetAsString(static_cast<string16*>(NULL)));
839 ASSERT_TRUE(narrow_value->GetAsString(static_cast<std::string*>(NULL)));
jdoerrie122c4da2017-03-06 11:12:04840 ASSERT_TRUE(narrow_value->GetAsString(static_cast<const Value**>(NULL)));
viettrungluu@chromium.orge2e593d2010-08-03 15:42:58841}
842
tfarina@chromium.org58b916e2011-06-28 22:56:33843TEST(ValuesTest, ListDeletion) {
jdoerrie8e945542017-02-17 13:54:49844 ListValue list;
Jeremy Roman9532f252017-08-16 23:27:24845 list.Append(std::make_unique<Value>());
jdoerrie8e945542017-02-17 13:54:49846 EXPECT_FALSE(list.empty());
847 list.Clear();
848 EXPECT_TRUE(list.empty());
initial.commitd7cae122008-07-26 21:49:38849}
850
tfarina@chromium.org58b916e2011-06-28 22:56:33851TEST(ValuesTest, ListRemoval) {
dcheng093de9b2016-04-04 21:25:51852 std::unique_ptr<Value> removed_item;
initial.commitd7cae122008-07-26 21:49:38853
854 {
855 ListValue list;
Jeremy Roman9532f252017-08-16 23:27:24856 list.Append(std::make_unique<Value>());
mmentovai@google.comf297d182008-08-21 16:24:51857 EXPECT_EQ(1U, list.GetSize());
deanm@google.com836061b2008-08-13 14:57:51858 EXPECT_FALSE(list.Remove(std::numeric_limits<size_t>::max(),
859 &removed_item));
initial.commitd7cae122008-07-26 21:49:38860 EXPECT_FALSE(list.Remove(1, &removed_item));
861 EXPECT_TRUE(list.Remove(0, &removed_item));
862 ASSERT_TRUE(removed_item);
mmentovai@google.comf297d182008-08-21 16:24:51863 EXPECT_EQ(0U, list.GetSize());
initial.commitd7cae122008-07-26 21:49:38864 }
bauerb@chromium.orgd814a882013-08-06 13:33:04865 removed_item.reset();
initial.commitd7cae122008-07-26 21:49:38866
867 {
868 ListValue list;
Jeremy Roman9532f252017-08-16 23:27:24869 list.Append(std::make_unique<Value>());
initial.commitd7cae122008-07-26 21:49:38870 EXPECT_TRUE(list.Remove(0, NULL));
mmentovai@google.comf297d182008-08-21 16:24:51871 EXPECT_EQ(0U, list.GetSize());
initial.commitd7cae122008-07-26 21:49:38872 }
pkasting@chromium.org6832cbe2009-11-30 19:59:11873
874 {
875 ListValue list;
Jeremy Roman9532f252017-08-16 23:27:24876 auto value = std::make_unique<Value>();
jdoerriecc9f5732017-08-23 14:12:30877 Value original_value = value->Clone();
danakj0c8d4aa2015-11-25 05:29:58878 list.Append(std::move(value));
tfarina@chromium.org4fc3c5642011-08-13 17:34:31879 size_t index = 0;
jdoerriea5676c62017-04-11 18:09:14880 list.Remove(original_value, &index);
tfarina@chromium.org4fc3c5642011-08-13 17:34:31881 EXPECT_EQ(0U, index);
pkasting@chromium.org6832cbe2009-11-30 19:59:11882 EXPECT_EQ(0U, list.GetSize());
883 }
initial.commitd7cae122008-07-26 21:49:38884}
885
tfarina@chromium.org58b916e2011-06-28 22:56:33886TEST(ValuesTest, DictionaryDeletion) {
viettrungluu@chromium.org9e4cda7332010-07-31 04:56:14887 std::string key = "test";
jdoerrie8e945542017-02-17 13:54:49888 DictionaryValue dict;
Jeremy Roman9532f252017-08-16 23:27:24889 dict.Set(key, std::make_unique<Value>());
jdoerrie8e945542017-02-17 13:54:49890 EXPECT_FALSE(dict.empty());
891 dict.Clear();
892 EXPECT_TRUE(dict.empty());
viettrungluu@chromium.org9e4cda7332010-07-31 04:56:14893}
894
jdoerrieb94e5422017-04-28 21:52:58895TEST(ValuesTest, DictionarySetReturnsPointer) {
896 {
897 DictionaryValue dict;
Jeremy Roman9532f252017-08-16 23:27:24898 Value* blank_ptr = dict.Set("foo.bar", std::make_unique<base::Value>());
jdoerrieb94e5422017-04-28 21:52:58899 EXPECT_EQ(Value::Type::NONE, blank_ptr->type());
900 }
901
902 {
903 DictionaryValue dict;
904 Value* blank_ptr = dict.SetWithoutPathExpansion(
Jeremy Roman9532f252017-08-16 23:27:24905 "foo.bar", std::make_unique<base::Value>());
jdoerrieb94e5422017-04-28 21:52:58906 EXPECT_EQ(Value::Type::NONE, blank_ptr->type());
907 }
908
909 {
910 DictionaryValue dict;
jdoerrieb94e5422017-04-28 21:52:58911 Value* int_ptr = dict.SetInteger("foo.bar", 42);
912 EXPECT_EQ(Value::Type::INTEGER, int_ptr->type());
913 EXPECT_EQ(42, int_ptr->GetInt());
914 }
915
916 {
917 DictionaryValue dict;
jdoerrieb94e5422017-04-28 21:52:58918 Value* double_ptr = dict.SetDouble("foo.bar", 3.142);
919 EXPECT_EQ(Value::Type::DOUBLE, double_ptr->type());
920 EXPECT_EQ(3.142, double_ptr->GetDouble());
921 }
922
923 {
924 DictionaryValue dict;
jdoerrieb94e5422017-04-28 21:52:58925 Value* string_ptr = dict.SetString("foo.bar", "foo");
926 EXPECT_EQ(Value::Type::STRING, string_ptr->type());
927 EXPECT_EQ("foo", string_ptr->GetString());
928 }
929
930 {
931 DictionaryValue dict;
jdoerrieb94e5422017-04-28 21:52:58932 Value* string16_ptr = dict.SetString("foo.bar", ASCIIToUTF16("baz"));
933 EXPECT_EQ(Value::Type::STRING, string16_ptr->type());
934 EXPECT_EQ("baz", string16_ptr->GetString());
935 }
936
937 {
938 DictionaryValue dict;
jdoerrieb94e5422017-04-28 21:52:58939 DictionaryValue* dict_ptr = dict.SetDictionary(
Jeremy Roman9532f252017-08-16 23:27:24940 "foo.bar", std::make_unique<base::DictionaryValue>());
jdoerrieb94e5422017-04-28 21:52:58941 EXPECT_EQ(Value::Type::DICTIONARY, dict_ptr->type());
942 }
943
944 {
945 DictionaryValue dict;
946 DictionaryValue* dict_ptr = dict.SetDictionaryWithoutPathExpansion(
Jeremy Roman9532f252017-08-16 23:27:24947 "foo.bar", std::make_unique<base::DictionaryValue>());
jdoerrieb94e5422017-04-28 21:52:58948 EXPECT_EQ(Value::Type::DICTIONARY, dict_ptr->type());
949 }
950
951 {
952 DictionaryValue dict;
953 ListValue* list_ptr =
Jeremy Roman9532f252017-08-16 23:27:24954 dict.SetList("foo.bar", std::make_unique<base::ListValue>());
jdoerrieb94e5422017-04-28 21:52:58955 EXPECT_EQ(Value::Type::LIST, list_ptr->type());
956 }
957
958 {
959 DictionaryValue dict;
960 ListValue* list_ptr = dict.SetListWithoutPathExpansion(
Jeremy Roman9532f252017-08-16 23:27:24961 "foo.bar", std::make_unique<base::ListValue>());
jdoerrieb94e5422017-04-28 21:52:58962 EXPECT_EQ(Value::Type::LIST, list_ptr->type());
963 }
964}
965
tfarina@chromium.org58b916e2011-06-28 22:56:33966TEST(ValuesTest, DictionaryRemoval) {
viettrungluu@chromium.org9e4cda7332010-07-31 04:56:14967 std::string key = "test";
dcheng093de9b2016-04-04 21:25:51968 std::unique_ptr<Value> removed_item;
viettrungluu@chromium.org9e4cda7332010-07-31 04:56:14969
970 {
971 DictionaryValue dict;
Jeremy Roman9532f252017-08-16 23:27:24972 dict.Set(key, std::make_unique<Value>());
viettrungluu@chromium.org9e4cda7332010-07-31 04:56:14973 EXPECT_TRUE(dict.HasKey(key));
974 EXPECT_FALSE(dict.Remove("absent key", &removed_item));
975 EXPECT_TRUE(dict.Remove(key, &removed_item));
976 EXPECT_FALSE(dict.HasKey(key));
977 ASSERT_TRUE(removed_item);
978 }
viettrungluu@chromium.org9e4cda7332010-07-31 04:56:14979
980 {
981 DictionaryValue dict;
Jeremy Roman9532f252017-08-16 23:27:24982 dict.Set(key, std::make_unique<Value>());
viettrungluu@chromium.org9e4cda7332010-07-31 04:56:14983 EXPECT_TRUE(dict.HasKey(key));
984 EXPECT_TRUE(dict.Remove(key, NULL));
viettrungluu@chromium.org9e4cda7332010-07-31 04:56:14985 EXPECT_FALSE(dict.HasKey(key));
986 }
987}
988
tfarina@chromium.org58b916e2011-06-28 22:56:33989TEST(ValuesTest, DictionaryWithoutPathExpansion) {
pkasting@chromium.org4dad9ad82009-11-25 20:47:52990 DictionaryValue dict;
Jeremy Roman9532f252017-08-16 23:27:24991 dict.Set("this.is.expanded", std::make_unique<Value>());
992 dict.SetWithoutPathExpansion("this.isnt.expanded", std::make_unique<Value>());
estadeca798482015-01-06 20:06:50993
994 EXPECT_FALSE(dict.HasKey("this.is.expanded"));
995 EXPECT_TRUE(dict.HasKey("this"));
996 Value* value1;
997 EXPECT_TRUE(dict.Get("this", &value1));
998 DictionaryValue* value2;
999 ASSERT_TRUE(dict.GetDictionaryWithoutPathExpansion("this", &value2));
1000 EXPECT_EQ(value1, value2);
1001 EXPECT_EQ(1U, value2->size());
1002
1003 EXPECT_TRUE(dict.HasKey("this.isnt.expanded"));
1004 Value* value3;
1005 EXPECT_FALSE(dict.Get("this.isnt.expanded", &value3));
1006 Value* value4;
1007 ASSERT_TRUE(dict.GetWithoutPathExpansion("this.isnt.expanded", &value4));
jdoerrie76cee9c2017-10-06 22:42:421008 EXPECT_EQ(Value::Type::NONE, value4->type());
estadeca798482015-01-06 20:06:501009}
1010
1011// Tests the deprecated version of SetWithoutPathExpansion.
1012// TODO(estade): remove.
1013TEST(ValuesTest, DictionaryWithoutPathExpansionDeprecated) {
1014 DictionaryValue dict;
Jeremy Roman9532f252017-08-16 23:27:241015 dict.Set("this.is.expanded", std::make_unique<Value>());
1016 dict.SetWithoutPathExpansion("this.isnt.expanded", std::make_unique<Value>());
viettrungluu@chromium.org9e4cda7332010-07-31 04:56:141017
1018 EXPECT_FALSE(dict.HasKey("this.is.expanded"));
1019 EXPECT_TRUE(dict.HasKey("this"));
1020 Value* value1;
1021 EXPECT_TRUE(dict.Get("this", &value1));
1022 DictionaryValue* value2;
1023 ASSERT_TRUE(dict.GetDictionaryWithoutPathExpansion("this", &value2));
1024 EXPECT_EQ(value1, value2);
1025 EXPECT_EQ(1U, value2->size());
1026
1027 EXPECT_TRUE(dict.HasKey("this.isnt.expanded"));
1028 Value* value3;
1029 EXPECT_FALSE(dict.Get("this.isnt.expanded", &value3));
1030 Value* value4;
1031 ASSERT_TRUE(dict.GetWithoutPathExpansion("this.isnt.expanded", &value4));
jdoerrie76cee9c2017-10-06 22:42:421032 EXPECT_EQ(Value::Type::NONE, value4->type());
viettrungluu@chromium.org9e4cda7332010-07-31 04:56:141033}
1034
gab@chromium.orgaa328332013-11-27 01:38:241035TEST(ValuesTest, DictionaryRemovePath) {
1036 DictionaryValue dict;
estadeca798482015-01-06 20:06:501037 dict.SetInteger("a.long.way.down", 1);
1038 dict.SetBoolean("a.long.key.path", true);
gab@chromium.orgaa328332013-11-27 01:38:241039
dcheng093de9b2016-04-04 21:25:511040 std::unique_ptr<Value> removed_item;
gab@chromium.orgaa328332013-11-27 01:38:241041 EXPECT_TRUE(dict.RemovePath("a.long.way.down", &removed_item));
1042 ASSERT_TRUE(removed_item);
jdoerriedc72ee942016-12-07 15:43:281043 EXPECT_TRUE(removed_item->IsType(base::Value::Type::INTEGER));
gab@chromium.orgaa328332013-11-27 01:38:241044 EXPECT_FALSE(dict.HasKey("a.long.way.down"));
1045 EXPECT_FALSE(dict.HasKey("a.long.way"));
1046 EXPECT_TRUE(dict.Get("a.long.key.path", NULL));
1047
1048 removed_item.reset();
1049 EXPECT_FALSE(dict.RemovePath("a.long.way.down", &removed_item));
1050 EXPECT_FALSE(removed_item);
1051 EXPECT_TRUE(dict.Get("a.long.key.path", NULL));
1052
1053 removed_item.reset();
1054 EXPECT_TRUE(dict.RemovePath("a.long.key.path", &removed_item));
1055 ASSERT_TRUE(removed_item);
jdoerriedc72ee942016-12-07 15:43:281056 EXPECT_TRUE(removed_item->IsType(base::Value::Type::BOOLEAN));
gab@chromium.orgaa328332013-11-27 01:38:241057 EXPECT_TRUE(dict.empty());
1058}
1059
tfarina@chromium.org58b916e2011-06-28 22:56:331060TEST(ValuesTest, DeepCopy) {
initial.commitd7cae122008-07-26 21:49:381061 DictionaryValue original_dict;
Jeremy Roman9532f252017-08-16 23:27:241062 Value* null_weak = original_dict.Set("null", std::make_unique<Value>());
1063 Value* bool_weak = original_dict.Set("bool", std::make_unique<Value>(true));
1064 Value* int_weak = original_dict.Set("int", std::make_unique<Value>(42));
1065 Value* double_weak =
1066 original_dict.Set("double", std::make_unique<Value>(3.14));
1067 Value* string_weak =
1068 original_dict.Set("string", std::make_unique<Value>("hello"));
1069 Value* string16_weak = original_dict.Set(
1070 "string16", std::make_unique<Value>(ASCIIToUTF16("hello16")));
viettrungluu@chromium.org9e4cda7332010-07-31 04:56:141071
jdoerrie3a7275f2017-05-31 05:58:331072 Value* binary_weak = original_dict.Set(
Jeremy Roman9532f252017-08-16 23:27:241073 "binary", std::make_unique<Value>(Value::BlobStorage(42, '!')));
viettrungluu@chromium.org9e4cda7332010-07-31 04:56:141074
jdoerriecc9f5732017-08-23 14:12:301075 Value::ListStorage storage;
1076 storage.emplace_back(0);
1077 storage.emplace_back(1);
1078 Value* list_weak =
1079 original_dict.Set("list", std::make_unique<Value>(std::move(storage)));
jdoerrie3a7275f2017-05-31 05:58:331080 Value* list_element_0_weak = &list_weak->GetList()[0];
1081 Value* list_element_1_weak = &list_weak->GetList()[1];
viettrungluu@chromium.org9e4cda7332010-07-31 04:56:141082
Jeremy Roman9532f252017-08-16 23:27:241083 DictionaryValue* dict_weak = original_dict.SetDictionary(
1084 "dictionary", std::make_unique<DictionaryValue>());
jdoerrie3a7275f2017-05-31 05:58:331085 dict_weak->SetString("key", "value");
battre@chromium.org5cf906f82011-11-26 01:11:441086
jdoerriecc9f5732017-08-23 14:12:301087 auto copy_dict = original_dict.CreateDeepCopy();
viettrungluu@chromium.org9e4cda7332010-07-31 04:56:141088 ASSERT_TRUE(copy_dict.get());
1089 ASSERT_NE(copy_dict.get(), &original_dict);
1090
1091 Value* copy_null = NULL;
1092 ASSERT_TRUE(copy_dict->Get("null", &copy_null));
1093 ASSERT_TRUE(copy_null);
jdoerrie3a7275f2017-05-31 05:58:331094 ASSERT_NE(copy_null, null_weak);
jdoerriedc72ee942016-12-07 15:43:281095 ASSERT_TRUE(copy_null->IsType(Value::Type::NONE));
viettrungluu@chromium.org9e4cda7332010-07-31 04:56:141096
1097 Value* copy_bool = NULL;
1098 ASSERT_TRUE(copy_dict->Get("bool", &copy_bool));
1099 ASSERT_TRUE(copy_bool);
jdoerrie3a7275f2017-05-31 05:58:331100 ASSERT_NE(copy_bool, bool_weak);
jdoerriedc72ee942016-12-07 15:43:281101 ASSERT_TRUE(copy_bool->IsType(Value::Type::BOOLEAN));
viettrungluu@chromium.org9e4cda7332010-07-31 04:56:141102 bool copy_bool_value = false;
1103 ASSERT_TRUE(copy_bool->GetAsBoolean(&copy_bool_value));
1104 ASSERT_TRUE(copy_bool_value);
1105
1106 Value* copy_int = NULL;
1107 ASSERT_TRUE(copy_dict->Get("int", &copy_int));
1108 ASSERT_TRUE(copy_int);
jdoerrie3a7275f2017-05-31 05:58:331109 ASSERT_NE(copy_int, int_weak);
jdoerriedc72ee942016-12-07 15:43:281110 ASSERT_TRUE(copy_int->IsType(Value::Type::INTEGER));
viettrungluu@chromium.org9e4cda7332010-07-31 04:56:141111 int copy_int_value = 0;
1112 ASSERT_TRUE(copy_int->GetAsInteger(&copy_int_value));
1113 ASSERT_EQ(42, copy_int_value);
1114
arv@chromium.orgfb534c92011-02-01 01:02:071115 Value* copy_double = NULL;
1116 ASSERT_TRUE(copy_dict->Get("double", &copy_double));
1117 ASSERT_TRUE(copy_double);
jdoerrie3a7275f2017-05-31 05:58:331118 ASSERT_NE(copy_double, double_weak);
jdoerriedc72ee942016-12-07 15:43:281119 ASSERT_TRUE(copy_double->IsType(Value::Type::DOUBLE));
arv@chromium.orgfb534c92011-02-01 01:02:071120 double copy_double_value = 0;
1121 ASSERT_TRUE(copy_double->GetAsDouble(&copy_double_value));
1122 ASSERT_EQ(3.14, copy_double_value);
viettrungluu@chromium.org9e4cda7332010-07-31 04:56:141123
1124 Value* copy_string = NULL;
1125 ASSERT_TRUE(copy_dict->Get("string", &copy_string));
1126 ASSERT_TRUE(copy_string);
jdoerrie3a7275f2017-05-31 05:58:331127 ASSERT_NE(copy_string, string_weak);
jdoerriedc72ee942016-12-07 15:43:281128 ASSERT_TRUE(copy_string->IsType(Value::Type::STRING));
viettrungluu@chromium.org9e4cda7332010-07-31 04:56:141129 std::string copy_string_value;
viettrungluu@chromium.orge2e593d2010-08-03 15:42:581130 string16 copy_string16_value;
viettrungluu@chromium.org9e4cda7332010-07-31 04:56:141131 ASSERT_TRUE(copy_string->GetAsString(&copy_string_value));
viettrungluu@chromium.orge2e593d2010-08-03 15:42:581132 ASSERT_TRUE(copy_string->GetAsString(&copy_string16_value));
viettrungluu@chromium.org9e4cda7332010-07-31 04:56:141133 ASSERT_EQ(std::string("hello"), copy_string_value);
viettrungluu@chromium.orge2e593d2010-08-03 15:42:581134 ASSERT_EQ(ASCIIToUTF16("hello"), copy_string16_value);
viettrungluu@chromium.org9e4cda7332010-07-31 04:56:141135
viettrungluu@chromium.orge2e593d2010-08-03 15:42:581136 Value* copy_string16 = NULL;
1137 ASSERT_TRUE(copy_dict->Get("string16", &copy_string16));
1138 ASSERT_TRUE(copy_string16);
jdoerrie3a7275f2017-05-31 05:58:331139 ASSERT_NE(copy_string16, string16_weak);
jdoerriedc72ee942016-12-07 15:43:281140 ASSERT_TRUE(copy_string16->IsType(Value::Type::STRING));
viettrungluu@chromium.orge2e593d2010-08-03 15:42:581141 ASSERT_TRUE(copy_string16->GetAsString(&copy_string_value));
1142 ASSERT_TRUE(copy_string16->GetAsString(&copy_string16_value));
viettrungluu@chromium.org9e4cda7332010-07-31 04:56:141143 ASSERT_EQ(std::string("hello16"), copy_string_value);
viettrungluu@chromium.orge2e593d2010-08-03 15:42:581144 ASSERT_EQ(ASCIIToUTF16("hello16"), copy_string16_value);
viettrungluu@chromium.org9e4cda7332010-07-31 04:56:141145
1146 Value* copy_binary = NULL;
1147 ASSERT_TRUE(copy_dict->Get("binary", &copy_binary));
1148 ASSERT_TRUE(copy_binary);
jdoerrie3a7275f2017-05-31 05:58:331149 ASSERT_NE(copy_binary, binary_weak);
jdoerriedc72ee942016-12-07 15:43:281150 ASSERT_TRUE(copy_binary->IsType(Value::Type::BINARY));
jdoerrie3a7275f2017-05-31 05:58:331151 ASSERT_NE(binary_weak->GetBlob().data(), copy_binary->GetBlob().data());
1152 ASSERT_EQ(binary_weak->GetBlob(), copy_binary->GetBlob());
viettrungluu@chromium.org9e4cda7332010-07-31 04:56:141153
1154 Value* copy_value = NULL;
1155 ASSERT_TRUE(copy_dict->Get("list", &copy_value));
1156 ASSERT_TRUE(copy_value);
jdoerrie3a7275f2017-05-31 05:58:331157 ASSERT_NE(copy_value, list_weak);
jdoerriedc72ee942016-12-07 15:43:281158 ASSERT_TRUE(copy_value->IsType(Value::Type::LIST));
battre@chromium.org5cf906f82011-11-26 01:11:441159 ListValue* copy_list = NULL;
1160 ASSERT_TRUE(copy_value->GetAsList(&copy_list));
1161 ASSERT_TRUE(copy_list);
viettrungluu@chromium.org9e4cda7332010-07-31 04:56:141162 ASSERT_EQ(2U, copy_list->GetSize());
1163
1164 Value* copy_list_element_0;
1165 ASSERT_TRUE(copy_list->Get(0, &copy_list_element_0));
1166 ASSERT_TRUE(copy_list_element_0);
jdoerrie3a7275f2017-05-31 05:58:331167 ASSERT_NE(copy_list_element_0, list_element_0_weak);
viettrungluu@chromium.org9e4cda7332010-07-31 04:56:141168 int copy_list_element_0_value;
1169 ASSERT_TRUE(copy_list_element_0->GetAsInteger(&copy_list_element_0_value));
1170 ASSERT_EQ(0, copy_list_element_0_value);
1171
1172 Value* copy_list_element_1;
1173 ASSERT_TRUE(copy_list->Get(1, &copy_list_element_1));
1174 ASSERT_TRUE(copy_list_element_1);
jdoerrie3a7275f2017-05-31 05:58:331175 ASSERT_NE(copy_list_element_1, list_element_1_weak);
viettrungluu@chromium.org9e4cda7332010-07-31 04:56:141176 int copy_list_element_1_value;
1177 ASSERT_TRUE(copy_list_element_1->GetAsInteger(&copy_list_element_1_value));
1178 ASSERT_EQ(1, copy_list_element_1_value);
battre@chromium.org5cf906f82011-11-26 01:11:441179
1180 copy_value = NULL;
1181 ASSERT_TRUE(copy_dict->Get("dictionary", &copy_value));
1182 ASSERT_TRUE(copy_value);
jdoerrie3a7275f2017-05-31 05:58:331183 ASSERT_NE(copy_value, dict_weak);
jdoerriedc72ee942016-12-07 15:43:281184 ASSERT_TRUE(copy_value->IsType(Value::Type::DICTIONARY));
battre@chromium.org5cf906f82011-11-26 01:11:441185 DictionaryValue* copy_nested_dictionary = NULL;
1186 ASSERT_TRUE(copy_value->GetAsDictionary(&copy_nested_dictionary));
1187 ASSERT_TRUE(copy_nested_dictionary);
1188 EXPECT_TRUE(copy_nested_dictionary->HasKey("key"));
viettrungluu@chromium.org9e4cda7332010-07-31 04:56:141189}
1190
tfarina@chromium.org58b916e2011-06-28 22:56:331191TEST(ValuesTest, Equals) {
Jeremy Roman9532f252017-08-16 23:27:241192 auto null1 = std::make_unique<Value>();
1193 auto null2 = std::make_unique<Value>();
danakj8dba5a52015-05-11 21:24:091194 EXPECT_NE(null1.get(), null2.get());
jdoerrie5c1cee112017-03-28 17:52:001195 EXPECT_EQ(*null1, *null2);
initial.commitd7cae122008-07-26 21:49:381196
jdoerrie239723572017-03-02 12:09:191197 Value boolean(false);
jdoerrie5c1cee112017-03-28 17:52:001198 EXPECT_NE(*null1, boolean);
initial.commitd7cae122008-07-26 21:49:381199
1200 DictionaryValue dv;
viettrungluu@chromium.org9e4cda7332010-07-31 04:56:141201 dv.SetBoolean("a", false);
1202 dv.SetInteger("b", 2);
arv@chromium.orgfb534c92011-02-01 01:02:071203 dv.SetDouble("c", 2.5);
viettrungluu@chromium.org9e4cda7332010-07-31 04:56:141204 dv.SetString("d1", "string");
viettrungluu@chromium.orgff4c1d82010-08-04 16:58:121205 dv.SetString("d2", ASCIIToUTF16("http://google.com"));
Jeremy Roman9532f252017-08-16 23:27:241206 dv.Set("e", std::make_unique<Value>());
viettrungluu@chromium.org9e4cda7332010-07-31 04:56:141207
jdoerriecc9f5732017-08-23 14:12:301208 auto copy = dv.CreateDeepCopy();
jdoerrie5c1cee112017-03-28 17:52:001209 EXPECT_EQ(dv, *copy);
viettrungluu@chromium.org9e4cda7332010-07-31 04:56:141210
dcheng093de9b2016-04-04 21:25:511211 std::unique_ptr<ListValue> list(new ListValue);
Jeremy Roman9532f252017-08-16 23:27:241212 list->Append(std::make_unique<Value>());
dcheng093de9b2016-04-04 21:25:511213 list->Append(WrapUnique(new DictionaryValue));
jdoerriecc9f5732017-08-23 14:12:301214 auto list_copy = std::make_unique<Value>(list->Clone());
viettrungluu@chromium.org9e4cda7332010-07-31 04:56:141215
jdoerrie3a7275f2017-05-31 05:58:331216 ListValue* list_weak = dv.SetList("f", std::move(list));
jdoerrie5c1cee112017-03-28 17:52:001217 EXPECT_NE(dv, *copy);
danakj0c8d4aa2015-11-25 05:29:581218 copy->Set("f", std::move(list_copy));
jdoerrie5c1cee112017-03-28 17:52:001219 EXPECT_EQ(dv, *copy);
viettrungluu@chromium.org9e4cda7332010-07-31 04:56:141220
Jeremy Roman9532f252017-08-16 23:27:241221 list_weak->Append(std::make_unique<Value>(true));
jdoerrie5c1cee112017-03-28 17:52:001222 EXPECT_NE(dv, *copy);
mnissler@chromium.orgdc1f2442010-08-18 16:23:401223
1224 // Check if Equals detects differences in only the keys.
jdoerriecc9f5732017-08-23 14:12:301225 copy = dv.CreateDeepCopy();
jdoerrie5c1cee112017-03-28 17:52:001226 EXPECT_EQ(dv, *copy);
mnissler@chromium.orgdc1f2442010-08-18 16:23:401227 copy->Remove("a", NULL);
1228 copy->SetBoolean("aa", false);
jdoerrie5c1cee112017-03-28 17:52:001229 EXPECT_NE(dv, *copy);
viettrungluu@chromium.org9e4cda7332010-07-31 04:56:141230}
1231
jdoerrie5c1cee112017-03-28 17:52:001232TEST(ValuesTest, Comparisons) {
1233 // Test None Values.
1234 Value null1;
1235 Value null2;
1236 EXPECT_EQ(null1, null2);
1237 EXPECT_FALSE(null1 != null2);
1238 EXPECT_FALSE(null1 < null2);
1239 EXPECT_FALSE(null1 > null2);
1240 EXPECT_LE(null1, null2);
1241 EXPECT_GE(null1, null2);
1242
1243 // Test Bool Values.
1244 Value bool1(false);
1245 Value bool2(true);
1246 EXPECT_FALSE(bool1 == bool2);
1247 EXPECT_NE(bool1, bool2);
1248 EXPECT_LT(bool1, bool2);
1249 EXPECT_FALSE(bool1 > bool2);
1250 EXPECT_LE(bool1, bool2);
1251 EXPECT_FALSE(bool1 >= bool2);
1252
1253 // Test Int Values.
1254 Value int1(1);
1255 Value int2(2);
1256 EXPECT_FALSE(int1 == int2);
1257 EXPECT_NE(int1, int2);
1258 EXPECT_LT(int1, int2);
1259 EXPECT_FALSE(int1 > int2);
1260 EXPECT_LE(int1, int2);
1261 EXPECT_FALSE(int1 >= int2);
1262
1263 // Test Double Values.
1264 Value double1(1.0);
1265 Value double2(2.0);
1266 EXPECT_FALSE(double1 == double2);
1267 EXPECT_NE(double1, double2);
1268 EXPECT_LT(double1, double2);
1269 EXPECT_FALSE(double1 > double2);
1270 EXPECT_LE(double1, double2);
1271 EXPECT_FALSE(double1 >= double2);
1272
1273 // Test String Values.
1274 Value string1("1");
1275 Value string2("2");
1276 EXPECT_FALSE(string1 == string2);
1277 EXPECT_NE(string1, string2);
1278 EXPECT_LT(string1, string2);
1279 EXPECT_FALSE(string1 > string2);
1280 EXPECT_LE(string1, string2);
1281 EXPECT_FALSE(string1 >= string2);
1282
1283 // Test Binary Values.
jdoerrie5f12b6272017-04-18 10:22:411284 Value binary1(Value::BlobStorage{0x01});
1285 Value binary2(Value::BlobStorage{0x02});
jdoerrie5c1cee112017-03-28 17:52:001286 EXPECT_FALSE(binary1 == binary2);
1287 EXPECT_NE(binary1, binary2);
1288 EXPECT_LT(binary1, binary2);
1289 EXPECT_FALSE(binary1 > binary2);
1290 EXPECT_LE(binary1, binary2);
1291 EXPECT_FALSE(binary1 >= binary2);
1292
1293 // Test Empty List Values.
1294 ListValue null_list1;
1295 ListValue null_list2;
1296 EXPECT_EQ(null_list1, null_list2);
1297 EXPECT_FALSE(null_list1 != null_list2);
1298 EXPECT_FALSE(null_list1 < null_list2);
1299 EXPECT_FALSE(null_list1 > null_list2);
1300 EXPECT_LE(null_list1, null_list2);
1301 EXPECT_GE(null_list1, null_list2);
1302
1303 // Test Non Empty List Values.
1304 ListValue int_list1;
1305 ListValue int_list2;
1306 int_list1.AppendInteger(1);
1307 int_list2.AppendInteger(2);
1308 EXPECT_FALSE(int_list1 == int_list2);
1309 EXPECT_NE(int_list1, int_list2);
1310 EXPECT_LT(int_list1, int_list2);
1311 EXPECT_FALSE(int_list1 > int_list2);
1312 EXPECT_LE(int_list1, int_list2);
1313 EXPECT_FALSE(int_list1 >= int_list2);
1314
1315 // Test Empty Dict Values.
1316 DictionaryValue null_dict1;
1317 DictionaryValue null_dict2;
1318 EXPECT_EQ(null_dict1, null_dict2);
1319 EXPECT_FALSE(null_dict1 != null_dict2);
1320 EXPECT_FALSE(null_dict1 < null_dict2);
1321 EXPECT_FALSE(null_dict1 > null_dict2);
1322 EXPECT_LE(null_dict1, null_dict2);
1323 EXPECT_GE(null_dict1, null_dict2);
1324
1325 // Test Non Empty Dict Values.
1326 DictionaryValue int_dict1;
1327 DictionaryValue int_dict2;
1328 int_dict1.SetInteger("key", 1);
1329 int_dict2.SetInteger("key", 2);
1330 EXPECT_FALSE(int_dict1 == int_dict2);
1331 EXPECT_NE(int_dict1, int_dict2);
1332 EXPECT_LT(int_dict1, int_dict2);
1333 EXPECT_FALSE(int_dict1 > int_dict2);
1334 EXPECT_LE(int_dict1, int_dict2);
1335 EXPECT_FALSE(int_dict1 >= int_dict2);
1336
1337 // Test Values of different types.
jdoerriecc9f5732017-08-23 14:12:301338 std::vector<Value> values;
1339 values.emplace_back(std::move(null1));
1340 values.emplace_back(std::move(bool1));
1341 values.emplace_back(std::move(int1));
1342 values.emplace_back(std::move(double1));
1343 values.emplace_back(std::move(string1));
1344 values.emplace_back(std::move(binary1));
1345 values.emplace_back(std::move(int_dict1));
1346 values.emplace_back(std::move(int_list1));
jdoerrie5c1cee112017-03-28 17:52:001347 for (size_t i = 0; i < values.size(); ++i) {
1348 for (size_t j = i + 1; j < values.size(); ++j) {
1349 EXPECT_FALSE(values[i] == values[j]);
1350 EXPECT_NE(values[i], values[j]);
1351 EXPECT_LT(values[i], values[j]);
1352 EXPECT_FALSE(values[i] > values[j]);
1353 EXPECT_LE(values[i], values[j]);
1354 EXPECT_FALSE(values[i] >= values[j]);
1355 }
1356 }
1357}
1358
tfarina@chromium.org58b916e2011-06-28 22:56:331359TEST(ValuesTest, DeepCopyCovariantReturnTypes) {
akalin@chromium.org16f47e02011-01-18 02:16:591360 DictionaryValue original_dict;
jdoerriecc9f5732017-08-23 14:12:301361 Value* null_weak = original_dict.SetKey("null", Value());
1362 Value* bool_weak = original_dict.SetKey("bool", Value(true));
1363 Value* int_weak = original_dict.SetKey("int", Value(42));
1364 Value* double_weak = original_dict.SetKey("double", Value(3.14));
1365 Value* string_weak = original_dict.SetKey("string", Value("hello"));
1366 Value* string16_weak =
1367 original_dict.SetKey("string16", Value(ASCIIToUTF16("hello16")));
1368 Value* binary_weak =
1369 original_dict.SetKey("binary", Value(Value::BlobStorage(42, '!')));
akalin@chromium.org16f47e02011-01-18 02:16:591370
jdoerriecc9f5732017-08-23 14:12:301371 Value::ListStorage storage;
1372 storage.emplace_back(0);
1373 storage.emplace_back(1);
1374 Value* list_weak = original_dict.SetKey("list", Value(std::move(storage)));
akalin@chromium.org16f47e02011-01-18 02:16:591375
jdoerriecc9f5732017-08-23 14:12:301376 auto copy_dict = std::make_unique<Value>(original_dict.Clone());
1377 auto copy_null = std::make_unique<Value>(null_weak->Clone());
1378 auto copy_bool = std::make_unique<Value>(bool_weak->Clone());
1379 auto copy_int = std::make_unique<Value>(int_weak->Clone());
1380 auto copy_double = std::make_unique<Value>(double_weak->Clone());
1381 auto copy_string = std::make_unique<Value>(string_weak->Clone());
1382 auto copy_string16 = std::make_unique<Value>(string16_weak->Clone());
1383 auto copy_binary = std::make_unique<Value>(binary_weak->Clone());
1384 auto copy_list = std::make_unique<Value>(list_weak->Clone());
akalin@chromium.org16f47e02011-01-18 02:16:591385
jdoerrie5c1cee112017-03-28 17:52:001386 EXPECT_EQ(original_dict, *copy_dict);
jdoerrie3a7275f2017-05-31 05:58:331387 EXPECT_EQ(*null_weak, *copy_null);
1388 EXPECT_EQ(*bool_weak, *copy_bool);
1389 EXPECT_EQ(*int_weak, *copy_int);
1390 EXPECT_EQ(*double_weak, *copy_double);
1391 EXPECT_EQ(*string_weak, *copy_string);
1392 EXPECT_EQ(*string16_weak, *copy_string16);
1393 EXPECT_EQ(*binary_weak, *copy_binary);
1394 EXPECT_EQ(*list_weak, *copy_list);
akalin@chromium.org16f47e02011-01-18 02:16:591395}
1396
tfarina@chromium.org58b916e2011-06-28 22:56:331397TEST(ValuesTest, RemoveEmptyChildren) {
Jeremy Roman9532f252017-08-16 23:27:241398 auto root = std::make_unique<DictionaryValue>();
tony@chromium.orgec330b52009-12-02 00:20:321399 // Remove empty lists and dictionaries.
Jeremy Roman9532f252017-08-16 23:27:241400 root->Set("empty_dict", std::make_unique<DictionaryValue>());
1401 root->Set("empty_list", std::make_unique<ListValue>());
1402 root->SetWithoutPathExpansion("a.b.c.d.e",
1403 std::make_unique<DictionaryValue>());
estade6e04d502015-05-23 02:42:091404 root = root->DeepCopyWithoutEmptyChildren();
viettrungluu@chromium.org9e4cda7332010-07-31 04:56:141405 EXPECT_TRUE(root->empty());
1406
1407 // Make sure we don't prune too much.
1408 root->SetBoolean("bool", true);
Jeremy Roman9532f252017-08-16 23:27:241409 root->Set("empty_dict", std::make_unique<DictionaryValue>());
dcheng@chromium.org007b3f82013-04-09 08:46:451410 root->SetString("empty_string", std::string());
estade6e04d502015-05-23 02:42:091411 root = root->DeepCopyWithoutEmptyChildren();
viettrungluu@chromium.org9e4cda7332010-07-31 04:56:141412 EXPECT_EQ(2U, root->size());
1413
1414 // Should do nothing.
estade6e04d502015-05-23 02:42:091415 root = root->DeepCopyWithoutEmptyChildren();
viettrungluu@chromium.org9e4cda7332010-07-31 04:56:141416 EXPECT_EQ(2U, root->size());
1417
1418 // Nested test cases. These should all reduce back to the bool and string
1419 // set above.
1420 {
Jeremy Roman9532f252017-08-16 23:27:241421 root->Set("a.b.c.d.e", std::make_unique<DictionaryValue>());
estade6e04d502015-05-23 02:42:091422 root = root->DeepCopyWithoutEmptyChildren();
viettrungluu@chromium.org9e4cda7332010-07-31 04:56:141423 EXPECT_EQ(2U, root->size());
1424 }
1425 {
Jeremy Roman9532f252017-08-16 23:27:241426 auto inner = std::make_unique<DictionaryValue>();
1427 inner->Set("empty_dict", std::make_unique<DictionaryValue>());
1428 inner->Set("empty_list", std::make_unique<ListValue>());
danakj0c8d4aa2015-11-25 05:29:581429 root->Set("dict_with_empty_children", std::move(inner));
estade6e04d502015-05-23 02:42:091430 root = root->DeepCopyWithoutEmptyChildren();
viettrungluu@chromium.org9e4cda7332010-07-31 04:56:141431 EXPECT_EQ(2U, root->size());
1432 }
1433 {
Jeremy Roman9532f252017-08-16 23:27:241434 auto inner = std::make_unique<ListValue>();
1435 inner->Append(std::make_unique<DictionaryValue>());
1436 inner->Append(std::make_unique<ListValue>());
danakj0c8d4aa2015-11-25 05:29:581437 root->Set("list_with_empty_children", std::move(inner));
estade6e04d502015-05-23 02:42:091438 root = root->DeepCopyWithoutEmptyChildren();
viettrungluu@chromium.org9e4cda7332010-07-31 04:56:141439 EXPECT_EQ(2U, root->size());
1440 }
1441
1442 // Nested with siblings.
1443 {
Jeremy Roman9532f252017-08-16 23:27:241444 auto inner = std::make_unique<ListValue>();
1445 inner->Append(std::make_unique<DictionaryValue>());
1446 inner->Append(std::make_unique<ListValue>());
danakj0c8d4aa2015-11-25 05:29:581447 root->Set("list_with_empty_children", std::move(inner));
Jeremy Roman9532f252017-08-16 23:27:241448 auto inner2 = std::make_unique<DictionaryValue>();
1449 inner2->Set("empty_dict", std::make_unique<DictionaryValue>());
1450 inner2->Set("empty_list", std::make_unique<ListValue>());
danakj0c8d4aa2015-11-25 05:29:581451 root->Set("dict_with_empty_children", std::move(inner2));
estade6e04d502015-05-23 02:42:091452 root = root->DeepCopyWithoutEmptyChildren();
viettrungluu@chromium.org9e4cda7332010-07-31 04:56:141453 EXPECT_EQ(2U, root->size());
1454 }
1455
1456 // Make sure nested values don't get pruned.
1457 {
Jeremy Roman9532f252017-08-16 23:27:241458 auto inner = std::make_unique<ListValue>();
1459 auto inner2 = std::make_unique<ListValue>();
1460 inner2->Append(std::make_unique<Value>("hello"));
1461 inner->Append(std::make_unique<DictionaryValue>());
danakj0c8d4aa2015-11-25 05:29:581462 inner->Append(std::move(inner2));
1463 root->Set("list_with_empty_children", std::move(inner));
estade6e04d502015-05-23 02:42:091464 root = root->DeepCopyWithoutEmptyChildren();
viettrungluu@chromium.org9e4cda7332010-07-31 04:56:141465 EXPECT_EQ(3U, root->size());
estade7bc801fb2015-05-07 01:53:081466
1467 ListValue* inner_value, *inner_value2;
1468 EXPECT_TRUE(root->GetList("list_with_empty_children", &inner_value));
1469 EXPECT_EQ(1U, inner_value->GetSize()); // Dictionary was pruned.
1470 EXPECT_TRUE(inner_value->GetList(0, &inner_value2));
1471 EXPECT_EQ(1U, inner_value2->GetSize());
viettrungluu@chromium.org9e4cda7332010-07-31 04:56:141472 }
1473}
1474
tfarina@chromium.org58b916e2011-06-28 22:56:331475TEST(ValuesTest, MergeDictionary) {
dcheng093de9b2016-04-04 21:25:511476 std::unique_ptr<DictionaryValue> base(new DictionaryValue);
viettrungluu@chromium.org9e4cda7332010-07-31 04:56:141477 base->SetString("base_key", "base_key_value_base");
1478 base->SetString("collide_key", "collide_key_value_base");
dcheng093de9b2016-04-04 21:25:511479 std::unique_ptr<DictionaryValue> base_sub_dict(new DictionaryValue);
viettrungluu@chromium.org9e4cda7332010-07-31 04:56:141480 base_sub_dict->SetString("sub_base_key", "sub_base_key_value_base");
1481 base_sub_dict->SetString("sub_collide_key", "sub_collide_key_value_base");
danakj0c8d4aa2015-11-25 05:29:581482 base->Set("sub_dict_key", std::move(base_sub_dict));
viettrungluu@chromium.org9e4cda7332010-07-31 04:56:141483
dcheng093de9b2016-04-04 21:25:511484 std::unique_ptr<DictionaryValue> merge(new DictionaryValue);
viettrungluu@chromium.org9e4cda7332010-07-31 04:56:141485 merge->SetString("merge_key", "merge_key_value_merge");
1486 merge->SetString("collide_key", "collide_key_value_merge");
dcheng093de9b2016-04-04 21:25:511487 std::unique_ptr<DictionaryValue> merge_sub_dict(new DictionaryValue);
viettrungluu@chromium.org9e4cda7332010-07-31 04:56:141488 merge_sub_dict->SetString("sub_merge_key", "sub_merge_key_value_merge");
1489 merge_sub_dict->SetString("sub_collide_key", "sub_collide_key_value_merge");
danakj0c8d4aa2015-11-25 05:29:581490 merge->Set("sub_dict_key", std::move(merge_sub_dict));
viettrungluu@chromium.org9e4cda7332010-07-31 04:56:141491
1492 base->MergeDictionary(merge.get());
1493
1494 EXPECT_EQ(4U, base->size());
1495 std::string base_key_value;
1496 EXPECT_TRUE(base->GetString("base_key", &base_key_value));
1497 EXPECT_EQ("base_key_value_base", base_key_value); // Base value preserved.
1498 std::string collide_key_value;
1499 EXPECT_TRUE(base->GetString("collide_key", &collide_key_value));
1500 EXPECT_EQ("collide_key_value_merge", collide_key_value); // Replaced.
1501 std::string merge_key_value;
1502 EXPECT_TRUE(base->GetString("merge_key", &merge_key_value));
1503 EXPECT_EQ("merge_key_value_merge", merge_key_value); // Merged in.
1504
1505 DictionaryValue* res_sub_dict;
1506 EXPECT_TRUE(base->GetDictionary("sub_dict_key", &res_sub_dict));
1507 EXPECT_EQ(3U, res_sub_dict->size());
1508 std::string sub_base_key_value;
1509 EXPECT_TRUE(res_sub_dict->GetString("sub_base_key", &sub_base_key_value));
1510 EXPECT_EQ("sub_base_key_value_base", sub_base_key_value); // Preserved.
1511 std::string sub_collide_key_value;
1512 EXPECT_TRUE(res_sub_dict->GetString("sub_collide_key",
1513 &sub_collide_key_value));
1514 EXPECT_EQ("sub_collide_key_value_merge", sub_collide_key_value); // Replaced.
1515 std::string sub_merge_key_value;
1516 EXPECT_TRUE(res_sub_dict->GetString("sub_merge_key", &sub_merge_key_value));
1517 EXPECT_EQ("sub_merge_key_value_merge", sub_merge_key_value); // Merged in.
1518}
tfarina@chromium.org58b916e2011-06-28 22:56:331519
jhawkins@chromium.org13502562012-05-09 21:54:271520TEST(ValuesTest, MergeDictionaryDeepCopy) {
dcheng093de9b2016-04-04 21:25:511521 std::unique_ptr<DictionaryValue> child(new DictionaryValue);
danakj8dba5a52015-05-11 21:24:091522 DictionaryValue* original_child = child.get();
jhawkins@chromium.org13502562012-05-09 21:54:271523 child->SetString("test", "value");
1524 EXPECT_EQ(1U, child->size());
1525
1526 std::string value;
1527 EXPECT_TRUE(child->GetString("test", &value));
1528 EXPECT_EQ("value", value);
1529
dcheng093de9b2016-04-04 21:25:511530 std::unique_ptr<DictionaryValue> base(new DictionaryValue);
danakj0c8d4aa2015-11-25 05:29:581531 base->Set("dict", std::move(child));
jhawkins@chromium.org13502562012-05-09 21:54:271532 EXPECT_EQ(1U, base->size());
1533
1534 DictionaryValue* ptr;
1535 EXPECT_TRUE(base->GetDictionary("dict", &ptr));
danakj8dba5a52015-05-11 21:24:091536 EXPECT_EQ(original_child, ptr);
jhawkins@chromium.org13502562012-05-09 21:54:271537
dcheng093de9b2016-04-04 21:25:511538 std::unique_ptr<DictionaryValue> merged(new DictionaryValue);
jhawkins@chromium.org13502562012-05-09 21:54:271539 merged->MergeDictionary(base.get());
1540 EXPECT_EQ(1U, merged->size());
1541 EXPECT_TRUE(merged->GetDictionary("dict", &ptr));
danakj8dba5a52015-05-11 21:24:091542 EXPECT_NE(original_child, ptr);
jhawkins@chromium.org13502562012-05-09 21:54:271543 EXPECT_TRUE(ptr->GetString("test", &value));
1544 EXPECT_EQ("value", value);
1545
danakj8dba5a52015-05-11 21:24:091546 original_child->SetString("test", "overwrite");
jhawkins@chromium.org13502562012-05-09 21:54:271547 base.reset();
1548 EXPECT_TRUE(ptr->GetString("test", &value));
1549 EXPECT_EQ("value", value);
1550}
1551
kalman@chromium.org32c0e002011-11-08 21:26:411552TEST(ValuesTest, DictionaryIterator) {
1553 DictionaryValue dict;
pneubeck@chromium.orgee6ba60f2013-04-12 09:25:231554 for (DictionaryValue::Iterator it(dict); !it.IsAtEnd(); it.Advance()) {
kalman@chromium.org32c0e002011-11-08 21:26:411555 ADD_FAILURE();
1556 }
1557
jdoerrie122c4da2017-03-06 11:12:041558 Value value1("value1");
jdoerriecc9f5732017-08-23 14:12:301559 dict.SetKey("key1", value1.Clone());
kalman@chromium.org32c0e002011-11-08 21:26:411560 bool seen1 = false;
pneubeck@chromium.orgee6ba60f2013-04-12 09:25:231561 for (DictionaryValue::Iterator it(dict); !it.IsAtEnd(); it.Advance()) {
kalman@chromium.org32c0e002011-11-08 21:26:411562 EXPECT_FALSE(seen1);
1563 EXPECT_EQ("key1", it.key());
jdoerrie5c1cee112017-03-28 17:52:001564 EXPECT_EQ(value1, it.value());
kalman@chromium.org32c0e002011-11-08 21:26:411565 seen1 = true;
1566 }
1567 EXPECT_TRUE(seen1);
1568
jdoerrie122c4da2017-03-06 11:12:041569 Value value2("value2");
jdoerriecc9f5732017-08-23 14:12:301570 dict.SetKey("key2", value2.Clone());
kalman@chromium.org32c0e002011-11-08 21:26:411571 bool seen2 = seen1 = false;
pneubeck@chromium.orgee6ba60f2013-04-12 09:25:231572 for (DictionaryValue::Iterator it(dict); !it.IsAtEnd(); it.Advance()) {
kalman@chromium.org32c0e002011-11-08 21:26:411573 if (it.key() == "key1") {
1574 EXPECT_FALSE(seen1);
jdoerrie5c1cee112017-03-28 17:52:001575 EXPECT_EQ(value1, it.value());
kalman@chromium.org32c0e002011-11-08 21:26:411576 seen1 = true;
1577 } else if (it.key() == "key2") {
1578 EXPECT_FALSE(seen2);
jdoerrie5c1cee112017-03-28 17:52:001579 EXPECT_EQ(value2, it.value());
kalman@chromium.org32c0e002011-11-08 21:26:411580 seen2 = true;
1581 } else {
1582 ADD_FAILURE();
1583 }
1584 }
1585 EXPECT_TRUE(seen1);
1586 EXPECT_TRUE(seen2);
1587}
1588
Johan Tibell71bba86c2017-05-17 05:21:121589TEST(ValuesTest, StdDictionaryIterator) {
1590 DictionaryValue dict;
1591 for (auto it = dict.begin(); it != dict.end(); ++it) {
1592 ADD_FAILURE();
1593 }
1594
1595 Value value1("value1");
jdoerriecc9f5732017-08-23 14:12:301596 dict.SetKey("key1", value1.Clone());
Johan Tibell71bba86c2017-05-17 05:21:121597 bool seen1 = false;
1598 for (const auto& it : dict) {
1599 EXPECT_FALSE(seen1);
1600 EXPECT_EQ("key1", it.first);
1601 EXPECT_EQ(value1, *it.second);
1602 seen1 = true;
1603 }
1604 EXPECT_TRUE(seen1);
1605
1606 Value value2("value2");
jdoerriecc9f5732017-08-23 14:12:301607 dict.SetKey("key2", value2.Clone());
Johan Tibell71bba86c2017-05-17 05:21:121608 bool seen2 = seen1 = false;
1609 for (const auto& it : dict) {
1610 if (it.first == "key1") {
1611 EXPECT_FALSE(seen1);
1612 EXPECT_EQ(value1, *it.second);
1613 seen1 = true;
1614 } else if (it.first == "key2") {
1615 EXPECT_FALSE(seen2);
1616 EXPECT_EQ(value2, *it.second);
1617 seen2 = true;
1618 } else {
1619 ADD_FAILURE();
1620 }
1621 }
1622 EXPECT_TRUE(seen1);
1623 EXPECT_TRUE(seen2);
1624}
1625
gab@chromium.org78c03a42014-03-09 07:13:231626// DictionaryValue/ListValue's Get*() methods should accept NULL as an out-value
1627// and still return true/false based on success.
1628TEST(ValuesTest, GetWithNullOutValue) {
1629 DictionaryValue main_dict;
1630 ListValue main_list;
1631
jdoerrie239723572017-03-02 12:09:191632 Value bool_value(false);
1633 Value int_value(1234);
1634 Value double_value(12.34567);
jdoerrie122c4da2017-03-06 11:12:041635 Value string_value("foo");
jdoerrie14b25da2017-04-11 07:45:501636 Value binary_value(Value::Type::BINARY);
gab@chromium.org78c03a42014-03-09 07:13:231637 DictionaryValue dict_value;
1638 ListValue list_value;
1639
jdoerriecc9f5732017-08-23 14:12:301640 main_dict.SetKey("bool", bool_value.Clone());
1641 main_dict.SetKey("int", int_value.Clone());
1642 main_dict.SetKey("double", double_value.Clone());
1643 main_dict.SetKey("string", string_value.Clone());
1644 main_dict.SetKey("binary", binary_value.Clone());
1645 main_dict.SetKey("dict", dict_value.Clone());
1646 main_dict.SetKey("list", list_value.Clone());
gab@chromium.org78c03a42014-03-09 07:13:231647
jdoerriecc9f5732017-08-23 14:12:301648 main_list.Append(std::make_unique<Value>(bool_value.Clone()));
1649 main_list.Append(std::make_unique<Value>(int_value.Clone()));
1650 main_list.Append(std::make_unique<Value>(double_value.Clone()));
1651 main_list.Append(std::make_unique<Value>(string_value.Clone()));
1652 main_list.Append(std::make_unique<Value>(binary_value.Clone()));
1653 main_list.Append(std::make_unique<Value>(dict_value.Clone()));
1654 main_list.Append(std::make_unique<Value>(list_value.Clone()));
gab@chromium.org78c03a42014-03-09 07:13:231655
1656 EXPECT_TRUE(main_dict.Get("bool", NULL));
1657 EXPECT_TRUE(main_dict.Get("int", NULL));
1658 EXPECT_TRUE(main_dict.Get("double", NULL));
1659 EXPECT_TRUE(main_dict.Get("string", NULL));
1660 EXPECT_TRUE(main_dict.Get("binary", NULL));
1661 EXPECT_TRUE(main_dict.Get("dict", NULL));
1662 EXPECT_TRUE(main_dict.Get("list", NULL));
1663 EXPECT_FALSE(main_dict.Get("DNE", NULL));
1664
1665 EXPECT_TRUE(main_dict.GetBoolean("bool", NULL));
1666 EXPECT_FALSE(main_dict.GetBoolean("int", NULL));
1667 EXPECT_FALSE(main_dict.GetBoolean("double", NULL));
1668 EXPECT_FALSE(main_dict.GetBoolean("string", NULL));
1669 EXPECT_FALSE(main_dict.GetBoolean("binary", NULL));
1670 EXPECT_FALSE(main_dict.GetBoolean("dict", NULL));
1671 EXPECT_FALSE(main_dict.GetBoolean("list", NULL));
1672 EXPECT_FALSE(main_dict.GetBoolean("DNE", NULL));
1673
1674 EXPECT_FALSE(main_dict.GetInteger("bool", NULL));
1675 EXPECT_TRUE(main_dict.GetInteger("int", NULL));
1676 EXPECT_FALSE(main_dict.GetInteger("double", NULL));
1677 EXPECT_FALSE(main_dict.GetInteger("string", NULL));
1678 EXPECT_FALSE(main_dict.GetInteger("binary", NULL));
1679 EXPECT_FALSE(main_dict.GetInteger("dict", NULL));
1680 EXPECT_FALSE(main_dict.GetInteger("list", NULL));
1681 EXPECT_FALSE(main_dict.GetInteger("DNE", NULL));
1682
1683 // Both int and double values can be obtained from GetDouble.
1684 EXPECT_FALSE(main_dict.GetDouble("bool", NULL));
1685 EXPECT_TRUE(main_dict.GetDouble("int", NULL));
1686 EXPECT_TRUE(main_dict.GetDouble("double", NULL));
1687 EXPECT_FALSE(main_dict.GetDouble("string", NULL));
1688 EXPECT_FALSE(main_dict.GetDouble("binary", NULL));
1689 EXPECT_FALSE(main_dict.GetDouble("dict", NULL));
1690 EXPECT_FALSE(main_dict.GetDouble("list", NULL));
1691 EXPECT_FALSE(main_dict.GetDouble("DNE", NULL));
1692
1693 EXPECT_FALSE(main_dict.GetString("bool", static_cast<std::string*>(NULL)));
1694 EXPECT_FALSE(main_dict.GetString("int", static_cast<std::string*>(NULL)));
1695 EXPECT_FALSE(main_dict.GetString("double", static_cast<std::string*>(NULL)));
1696 EXPECT_TRUE(main_dict.GetString("string", static_cast<std::string*>(NULL)));
1697 EXPECT_FALSE(main_dict.GetString("binary", static_cast<std::string*>(NULL)));
1698 EXPECT_FALSE(main_dict.GetString("dict", static_cast<std::string*>(NULL)));
1699 EXPECT_FALSE(main_dict.GetString("list", static_cast<std::string*>(NULL)));
1700 EXPECT_FALSE(main_dict.GetString("DNE", static_cast<std::string*>(NULL)));
1701
1702 EXPECT_FALSE(main_dict.GetString("bool", static_cast<string16*>(NULL)));
1703 EXPECT_FALSE(main_dict.GetString("int", static_cast<string16*>(NULL)));
1704 EXPECT_FALSE(main_dict.GetString("double", static_cast<string16*>(NULL)));
1705 EXPECT_TRUE(main_dict.GetString("string", static_cast<string16*>(NULL)));
1706 EXPECT_FALSE(main_dict.GetString("binary", static_cast<string16*>(NULL)));
1707 EXPECT_FALSE(main_dict.GetString("dict", static_cast<string16*>(NULL)));
1708 EXPECT_FALSE(main_dict.GetString("list", static_cast<string16*>(NULL)));
1709 EXPECT_FALSE(main_dict.GetString("DNE", static_cast<string16*>(NULL)));
1710
1711 EXPECT_FALSE(main_dict.GetBinary("bool", NULL));
1712 EXPECT_FALSE(main_dict.GetBinary("int", NULL));
1713 EXPECT_FALSE(main_dict.GetBinary("double", NULL));
1714 EXPECT_FALSE(main_dict.GetBinary("string", NULL));
1715 EXPECT_TRUE(main_dict.GetBinary("binary", NULL));
1716 EXPECT_FALSE(main_dict.GetBinary("dict", NULL));
1717 EXPECT_FALSE(main_dict.GetBinary("list", NULL));
1718 EXPECT_FALSE(main_dict.GetBinary("DNE", NULL));
1719
1720 EXPECT_FALSE(main_dict.GetDictionary("bool", NULL));
1721 EXPECT_FALSE(main_dict.GetDictionary("int", NULL));
1722 EXPECT_FALSE(main_dict.GetDictionary("double", NULL));
1723 EXPECT_FALSE(main_dict.GetDictionary("string", NULL));
1724 EXPECT_FALSE(main_dict.GetDictionary("binary", NULL));
1725 EXPECT_TRUE(main_dict.GetDictionary("dict", NULL));
1726 EXPECT_FALSE(main_dict.GetDictionary("list", NULL));
1727 EXPECT_FALSE(main_dict.GetDictionary("DNE", NULL));
1728
1729 EXPECT_FALSE(main_dict.GetList("bool", NULL));
1730 EXPECT_FALSE(main_dict.GetList("int", NULL));
1731 EXPECT_FALSE(main_dict.GetList("double", NULL));
1732 EXPECT_FALSE(main_dict.GetList("string", NULL));
1733 EXPECT_FALSE(main_dict.GetList("binary", NULL));
1734 EXPECT_FALSE(main_dict.GetList("dict", NULL));
1735 EXPECT_TRUE(main_dict.GetList("list", NULL));
1736 EXPECT_FALSE(main_dict.GetList("DNE", NULL));
1737
1738 EXPECT_TRUE(main_dict.GetWithoutPathExpansion("bool", NULL));
1739 EXPECT_TRUE(main_dict.GetWithoutPathExpansion("int", NULL));
1740 EXPECT_TRUE(main_dict.GetWithoutPathExpansion("double", NULL));
1741 EXPECT_TRUE(main_dict.GetWithoutPathExpansion("string", NULL));
1742 EXPECT_TRUE(main_dict.GetWithoutPathExpansion("binary", NULL));
1743 EXPECT_TRUE(main_dict.GetWithoutPathExpansion("dict", NULL));
1744 EXPECT_TRUE(main_dict.GetWithoutPathExpansion("list", NULL));
1745 EXPECT_FALSE(main_dict.GetWithoutPathExpansion("DNE", NULL));
1746
1747 EXPECT_TRUE(main_dict.GetBooleanWithoutPathExpansion("bool", NULL));
1748 EXPECT_FALSE(main_dict.GetBooleanWithoutPathExpansion("int", NULL));
1749 EXPECT_FALSE(main_dict.GetBooleanWithoutPathExpansion("double", NULL));
1750 EXPECT_FALSE(main_dict.GetBooleanWithoutPathExpansion("string", NULL));
1751 EXPECT_FALSE(main_dict.GetBooleanWithoutPathExpansion("binary", NULL));
1752 EXPECT_FALSE(main_dict.GetBooleanWithoutPathExpansion("dict", NULL));
1753 EXPECT_FALSE(main_dict.GetBooleanWithoutPathExpansion("list", NULL));
1754 EXPECT_FALSE(main_dict.GetBooleanWithoutPathExpansion("DNE", NULL));
1755
1756 EXPECT_FALSE(main_dict.GetIntegerWithoutPathExpansion("bool", NULL));
1757 EXPECT_TRUE(main_dict.GetIntegerWithoutPathExpansion("int", NULL));
1758 EXPECT_FALSE(main_dict.GetIntegerWithoutPathExpansion("double", NULL));
1759 EXPECT_FALSE(main_dict.GetIntegerWithoutPathExpansion("string", NULL));
1760 EXPECT_FALSE(main_dict.GetIntegerWithoutPathExpansion("binary", NULL));
1761 EXPECT_FALSE(main_dict.GetIntegerWithoutPathExpansion("dict", NULL));
1762 EXPECT_FALSE(main_dict.GetIntegerWithoutPathExpansion("list", NULL));
1763 EXPECT_FALSE(main_dict.GetIntegerWithoutPathExpansion("DNE", NULL));
1764
1765 EXPECT_FALSE(main_dict.GetDoubleWithoutPathExpansion("bool", NULL));
1766 EXPECT_TRUE(main_dict.GetDoubleWithoutPathExpansion("int", NULL));
1767 EXPECT_TRUE(main_dict.GetDoubleWithoutPathExpansion("double", NULL));
1768 EXPECT_FALSE(main_dict.GetDoubleWithoutPathExpansion("string", NULL));
1769 EXPECT_FALSE(main_dict.GetDoubleWithoutPathExpansion("binary", NULL));
1770 EXPECT_FALSE(main_dict.GetDoubleWithoutPathExpansion("dict", NULL));
1771 EXPECT_FALSE(main_dict.GetDoubleWithoutPathExpansion("list", NULL));
1772 EXPECT_FALSE(main_dict.GetDoubleWithoutPathExpansion("DNE", NULL));
1773
1774 EXPECT_FALSE(main_dict.GetStringWithoutPathExpansion(
1775 "bool", static_cast<std::string*>(NULL)));
1776 EXPECT_FALSE(main_dict.GetStringWithoutPathExpansion(
1777 "int", static_cast<std::string*>(NULL)));
1778 EXPECT_FALSE(main_dict.GetStringWithoutPathExpansion(
1779 "double", static_cast<std::string*>(NULL)));
1780 EXPECT_TRUE(main_dict.GetStringWithoutPathExpansion(
1781 "string", static_cast<std::string*>(NULL)));
1782 EXPECT_FALSE(main_dict.GetStringWithoutPathExpansion(
1783 "binary", static_cast<std::string*>(NULL)));
1784 EXPECT_FALSE(main_dict.GetStringWithoutPathExpansion(
1785 "dict", static_cast<std::string*>(NULL)));
1786 EXPECT_FALSE(main_dict.GetStringWithoutPathExpansion(
1787 "list", static_cast<std::string*>(NULL)));
1788 EXPECT_FALSE(main_dict.GetStringWithoutPathExpansion(
1789 "DNE", static_cast<std::string*>(NULL)));
1790
1791 EXPECT_FALSE(main_dict.GetStringWithoutPathExpansion(
1792 "bool", static_cast<string16*>(NULL)));
1793 EXPECT_FALSE(main_dict.GetStringWithoutPathExpansion(
1794 "int", static_cast<string16*>(NULL)));
1795 EXPECT_FALSE(main_dict.GetStringWithoutPathExpansion(
1796 "double", static_cast<string16*>(NULL)));
1797 EXPECT_TRUE(main_dict.GetStringWithoutPathExpansion(
1798 "string", static_cast<string16*>(NULL)));
1799 EXPECT_FALSE(main_dict.GetStringWithoutPathExpansion(
1800 "binary", static_cast<string16*>(NULL)));
1801 EXPECT_FALSE(main_dict.GetStringWithoutPathExpansion(
1802 "dict", static_cast<string16*>(NULL)));
1803 EXPECT_FALSE(main_dict.GetStringWithoutPathExpansion(
1804 "list", static_cast<string16*>(NULL)));
1805 EXPECT_FALSE(main_dict.GetStringWithoutPathExpansion(
1806 "DNE", static_cast<string16*>(NULL)));
1807
1808 // There is no GetBinaryWithoutPathExpansion for some reason, but if there
1809 // were it should be tested here...
1810
1811 EXPECT_FALSE(main_dict.GetDictionaryWithoutPathExpansion("bool", NULL));
1812 EXPECT_FALSE(main_dict.GetDictionaryWithoutPathExpansion("int", NULL));
1813 EXPECT_FALSE(main_dict.GetDictionaryWithoutPathExpansion("double", NULL));
1814 EXPECT_FALSE(main_dict.GetDictionaryWithoutPathExpansion("string", NULL));
1815 EXPECT_FALSE(main_dict.GetDictionaryWithoutPathExpansion("binary", NULL));
1816 EXPECT_TRUE(main_dict.GetDictionaryWithoutPathExpansion("dict", NULL));
1817 EXPECT_FALSE(main_dict.GetDictionaryWithoutPathExpansion("list", NULL));
1818 EXPECT_FALSE(main_dict.GetDictionaryWithoutPathExpansion("DNE", NULL));
1819
1820 EXPECT_FALSE(main_dict.GetListWithoutPathExpansion("bool", NULL));
1821 EXPECT_FALSE(main_dict.GetListWithoutPathExpansion("int", NULL));
1822 EXPECT_FALSE(main_dict.GetListWithoutPathExpansion("double", NULL));
1823 EXPECT_FALSE(main_dict.GetListWithoutPathExpansion("string", NULL));
1824 EXPECT_FALSE(main_dict.GetListWithoutPathExpansion("binary", NULL));
1825 EXPECT_FALSE(main_dict.GetListWithoutPathExpansion("dict", NULL));
1826 EXPECT_TRUE(main_dict.GetListWithoutPathExpansion("list", NULL));
1827 EXPECT_FALSE(main_dict.GetListWithoutPathExpansion("DNE", NULL));
1828
1829 EXPECT_TRUE(main_list.Get(0, NULL));
1830 EXPECT_TRUE(main_list.Get(1, NULL));
1831 EXPECT_TRUE(main_list.Get(2, NULL));
1832 EXPECT_TRUE(main_list.Get(3, NULL));
1833 EXPECT_TRUE(main_list.Get(4, NULL));
1834 EXPECT_TRUE(main_list.Get(5, NULL));
1835 EXPECT_TRUE(main_list.Get(6, NULL));
1836 EXPECT_FALSE(main_list.Get(7, NULL));
1837
1838 EXPECT_TRUE(main_list.GetBoolean(0, NULL));
1839 EXPECT_FALSE(main_list.GetBoolean(1, NULL));
1840 EXPECT_FALSE(main_list.GetBoolean(2, NULL));
1841 EXPECT_FALSE(main_list.GetBoolean(3, NULL));
1842 EXPECT_FALSE(main_list.GetBoolean(4, NULL));
1843 EXPECT_FALSE(main_list.GetBoolean(5, NULL));
1844 EXPECT_FALSE(main_list.GetBoolean(6, NULL));
1845 EXPECT_FALSE(main_list.GetBoolean(7, NULL));
1846
1847 EXPECT_FALSE(main_list.GetInteger(0, NULL));
1848 EXPECT_TRUE(main_list.GetInteger(1, NULL));
1849 EXPECT_FALSE(main_list.GetInteger(2, NULL));
1850 EXPECT_FALSE(main_list.GetInteger(3, NULL));
1851 EXPECT_FALSE(main_list.GetInteger(4, NULL));
1852 EXPECT_FALSE(main_list.GetInteger(5, NULL));
1853 EXPECT_FALSE(main_list.GetInteger(6, NULL));
1854 EXPECT_FALSE(main_list.GetInteger(7, NULL));
1855
1856 EXPECT_FALSE(main_list.GetDouble(0, NULL));
1857 EXPECT_TRUE(main_list.GetDouble(1, NULL));
1858 EXPECT_TRUE(main_list.GetDouble(2, NULL));
1859 EXPECT_FALSE(main_list.GetDouble(3, NULL));
1860 EXPECT_FALSE(main_list.GetDouble(4, NULL));
1861 EXPECT_FALSE(main_list.GetDouble(5, NULL));
1862 EXPECT_FALSE(main_list.GetDouble(6, NULL));
1863 EXPECT_FALSE(main_list.GetDouble(7, NULL));
1864
1865 EXPECT_FALSE(main_list.GetString(0, static_cast<std::string*>(NULL)));
1866 EXPECT_FALSE(main_list.GetString(1, static_cast<std::string*>(NULL)));
1867 EXPECT_FALSE(main_list.GetString(2, static_cast<std::string*>(NULL)));
1868 EXPECT_TRUE(main_list.GetString(3, static_cast<std::string*>(NULL)));
1869 EXPECT_FALSE(main_list.GetString(4, static_cast<std::string*>(NULL)));
1870 EXPECT_FALSE(main_list.GetString(5, static_cast<std::string*>(NULL)));
1871 EXPECT_FALSE(main_list.GetString(6, static_cast<std::string*>(NULL)));
1872 EXPECT_FALSE(main_list.GetString(7, static_cast<std::string*>(NULL)));
1873
1874 EXPECT_FALSE(main_list.GetString(0, static_cast<string16*>(NULL)));
1875 EXPECT_FALSE(main_list.GetString(1, static_cast<string16*>(NULL)));
1876 EXPECT_FALSE(main_list.GetString(2, static_cast<string16*>(NULL)));
1877 EXPECT_TRUE(main_list.GetString(3, static_cast<string16*>(NULL)));
1878 EXPECT_FALSE(main_list.GetString(4, static_cast<string16*>(NULL)));
1879 EXPECT_FALSE(main_list.GetString(5, static_cast<string16*>(NULL)));
1880 EXPECT_FALSE(main_list.GetString(6, static_cast<string16*>(NULL)));
1881 EXPECT_FALSE(main_list.GetString(7, static_cast<string16*>(NULL)));
1882
1883 EXPECT_FALSE(main_list.GetBinary(0, NULL));
1884 EXPECT_FALSE(main_list.GetBinary(1, NULL));
1885 EXPECT_FALSE(main_list.GetBinary(2, NULL));
1886 EXPECT_FALSE(main_list.GetBinary(3, NULL));
1887 EXPECT_TRUE(main_list.GetBinary(4, NULL));
1888 EXPECT_FALSE(main_list.GetBinary(5, NULL));
1889 EXPECT_FALSE(main_list.GetBinary(6, NULL));
1890 EXPECT_FALSE(main_list.GetBinary(7, NULL));
1891
1892 EXPECT_FALSE(main_list.GetDictionary(0, NULL));
1893 EXPECT_FALSE(main_list.GetDictionary(1, NULL));
1894 EXPECT_FALSE(main_list.GetDictionary(2, NULL));
1895 EXPECT_FALSE(main_list.GetDictionary(3, NULL));
1896 EXPECT_FALSE(main_list.GetDictionary(4, NULL));
1897 EXPECT_TRUE(main_list.GetDictionary(5, NULL));
1898 EXPECT_FALSE(main_list.GetDictionary(6, NULL));
1899 EXPECT_FALSE(main_list.GetDictionary(7, NULL));
1900
1901 EXPECT_FALSE(main_list.GetList(0, NULL));
1902 EXPECT_FALSE(main_list.GetList(1, NULL));
1903 EXPECT_FALSE(main_list.GetList(2, NULL));
1904 EXPECT_FALSE(main_list.GetList(3, NULL));
1905 EXPECT_FALSE(main_list.GetList(4, NULL));
1906 EXPECT_FALSE(main_list.GetList(5, NULL));
1907 EXPECT_TRUE(main_list.GetList(6, NULL));
1908 EXPECT_FALSE(main_list.GetList(7, NULL));
1909}
1910
dyarosheved2ec7d02017-04-06 12:19:041911TEST(ValuesTest, SelfSwap) {
1912 base::Value test(1);
mkwstcd8067b2017-04-11 06:52:211913 std::swap(test, test);
Lei Zhang30895d52017-10-23 19:14:461914 EXPECT_EQ(1, test.GetInt());
1915}
1916
1917TEST(ValuesTest, FromToUniquePtrValue) {
1918 std::unique_ptr<DictionaryValue> dict = std::make_unique<DictionaryValue>();
1919 dict->SetString("name", "Froogle");
1920 dict->SetString("url", "http://froogle.com");
1921 Value dict_copy = dict->Clone();
1922
1923 Value dict_converted = Value::FromUniquePtrValue(std::move(dict));
1924 EXPECT_EQ(dict_copy, dict_converted);
1925
1926 std::unique_ptr<Value> val =
1927 Value::ToUniquePtrValue(std::move(dict_converted));
1928 EXPECT_EQ(dict_copy, *val);
dyarosheved2ec7d02017-04-06 12:19:041929}
1930
tfarina@chromium.org58b916e2011-06-28 22:56:331931} // namespace base