rsleevi@chromium.org | 4410618 | 2012-04-06 03:53:02 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 4 | |
dcheng | 093de9b | 2016-04-04 21:25:51 | [diff] [blame] | 5 | #include "base/values.h" |
| 6 | |
avi | 9b6f4293 | 2015-12-26 22:15:14 | [diff] [blame] | 7 | #include <stddef.h> |
| 8 | |
jdoerrie | 44efa9d | 2017-07-14 14:47:20 | [diff] [blame] | 9 | #include <functional> |
deanm@google.com | 836061b | 2008-08-13 14:57:51 | [diff] [blame] | 10 | #include <limits> |
dcheng | 093de9b | 2016-04-04 21:25:51 | [diff] [blame] | 11 | #include <memory> |
jdoerrie | 17e71cc | 2017-03-30 06:40:29 | [diff] [blame] | 12 | #include <string> |
| 13 | #include <type_traits> |
danakj | 0c8d4aa | 2015-11-25 05:29:58 | [diff] [blame] | 14 | #include <utility> |
jdoerrie | e03e80f | 2017-02-15 08:42:14 | [diff] [blame] | 15 | #include <vector> |
deanm@google.com | 836061b | 2008-08-13 14:57:51 | [diff] [blame] | 16 | |
jdoerrie | 44efa9d | 2017-07-14 14:47:20 | [diff] [blame] | 17 | #include "base/containers/adapters.h" |
dcheng | 093de9b | 2016-04-04 21:25:51 | [diff] [blame] | 18 | #include "base/memory/ptr_util.h" |
avi@chromium.org | c851cfd | 2013-06-10 20:11:14 | [diff] [blame] | 19 | #include "base/strings/string16.h" |
jdoerrie | 4634947 | 2017-08-02 02:20:32 | [diff] [blame] | 20 | #include "base/strings/string_piece.h" |
avi@chromium.org | a4ea1f1 | 2013-06-07 18:37:07 | [diff] [blame] | 21 | #include "base/strings/utf_string_conversions.h" |
jdoerrie | 44efa9d | 2017-07-14 14:47:20 | [diff] [blame] | 22 | #include "testing/gmock/include/gmock/gmock.h" |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 23 | #include "testing/gtest/include/gtest/gtest.h" |
| 24 | |
tfarina@chromium.org | 58b916e | 2011-06-28 22:56:33 | [diff] [blame] | 25 | namespace base { |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 26 | |
jdoerrie | 17e71cc | 2017-03-30 06:40:29 | [diff] [blame] | 27 | TEST(ValuesTest, TestNothrow) { |
jdoerrie | 9d47e9a | 2017-04-04 08:54:21 | [diff] [blame] | 28 | static_assert(std::is_nothrow_move_constructible<Value>::value, |
jdoerrie | 17e71cc | 2017-03-30 06:40:29 | [diff] [blame] | 29 | "IsNothrowMoveConstructible"); |
jdoerrie | 9d47e9a | 2017-04-04 08:54:21 | [diff] [blame] | 30 | static_assert(std::is_nothrow_default_constructible<Value>::value, |
jdoerrie | 17e71cc | 2017-03-30 06:40:29 | [diff] [blame] | 31 | "IsNothrowDefaultConstructible"); |
| 32 | static_assert(std::is_nothrow_constructible<Value, std::string&&>::value, |
| 33 | "IsNothrowMoveConstructibleFromString"); |
| 34 | static_assert( |
jdoerrie | 5f12b627 | 2017-04-18 10:22:41 | [diff] [blame] | 35 | std::is_nothrow_constructible<Value, Value::BlobStorage&&>::value, |
jdoerrie | 17e71cc | 2017-03-30 06:40:29 | [diff] [blame] | 36 | "IsNothrowMoveConstructibleFromBlob"); |
jdoerrie | 2b7d0fcd | 2017-04-19 07:15:38 | [diff] [blame] | 37 | static_assert( |
| 38 | std::is_nothrow_constructible<Value, Value::ListStorage&&>::value, |
| 39 | "IsNothrowMoveConstructibleFromList"); |
jdoerrie | 9d47e9a | 2017-04-04 08:54:21 | [diff] [blame] | 40 | static_assert(std::is_nothrow_move_assignable<Value>::value, |
| 41 | "IsNothrowMoveAssignable"); |
jdoerrie | 52939ed | 2017-04-26 18:19:42 | [diff] [blame] | 42 | static_assert( |
| 43 | std::is_nothrow_constructible<ListValue, Value::ListStorage&&>::value, |
| 44 | "ListIsNothrowMoveConstructibleFromList"); |
jdoerrie | 17e71cc | 2017-03-30 06:40:29 | [diff] [blame] | 45 | } |
| 46 | |
jdoerrie | 05eb316 | 2017-02-01 10:36:56 | [diff] [blame] | 47 | // Group of tests for the value constructors. |
| 48 | TEST(ValuesTest, ConstructBool) { |
jdoerrie | 23972357 | 2017-03-02 12:09:19 | [diff] [blame] | 49 | Value true_value(true); |
jdoerrie | 05eb316 | 2017-02-01 10:36:56 | [diff] [blame] | 50 | EXPECT_EQ(Value::Type::BOOLEAN, true_value.type()); |
| 51 | EXPECT_TRUE(true_value.GetBool()); |
| 52 | |
jdoerrie | 23972357 | 2017-03-02 12:09:19 | [diff] [blame] | 53 | Value false_value(false); |
jdoerrie | 05eb316 | 2017-02-01 10:36:56 | [diff] [blame] | 54 | EXPECT_EQ(Value::Type::BOOLEAN, false_value.type()); |
| 55 | EXPECT_FALSE(false_value.GetBool()); |
| 56 | } |
| 57 | |
| 58 | TEST(ValuesTest, ConstructInt) { |
jdoerrie | 23972357 | 2017-03-02 12:09:19 | [diff] [blame] | 59 | Value value(-37); |
jdoerrie | 05eb316 | 2017-02-01 10:36:56 | [diff] [blame] | 60 | EXPECT_EQ(Value::Type::INTEGER, value.type()); |
| 61 | EXPECT_EQ(-37, value.GetInt()); |
| 62 | } |
| 63 | |
| 64 | TEST(ValuesTest, ConstructDouble) { |
jdoerrie | 23972357 | 2017-03-02 12:09:19 | [diff] [blame] | 65 | Value value(-4.655); |
jdoerrie | 05eb316 | 2017-02-01 10:36:56 | [diff] [blame] | 66 | EXPECT_EQ(Value::Type::DOUBLE, value.type()); |
| 67 | EXPECT_EQ(-4.655, value.GetDouble()); |
| 68 | } |
| 69 | |
jdoerrie | f38f37b | 2017-02-01 14:38:32 | [diff] [blame] | 70 | TEST(ValuesTest, ConstructStringFromConstCharPtr) { |
| 71 | const char* str = "foobar"; |
jdoerrie | 122c4da | 2017-03-06 11:12:04 | [diff] [blame] | 72 | Value value(str); |
jdoerrie | f38f37b | 2017-02-01 14:38:32 | [diff] [blame] | 73 | EXPECT_EQ(Value::Type::STRING, value.type()); |
| 74 | EXPECT_EQ("foobar", value.GetString()); |
| 75 | } |
| 76 | |
jdoerrie | 9f90ad7 | 2017-09-11 17:23:26 | [diff] [blame] | 77 | TEST(ValuesTest, ConstructStringFromStringPiece) { |
jdoerrie | f38f37b | 2017-02-01 14:38:32 | [diff] [blame] | 78 | std::string str = "foobar"; |
jdoerrie | 9f90ad7 | 2017-09-11 17:23:26 | [diff] [blame] | 79 | Value value{StringPiece(str)}; |
jdoerrie | f38f37b | 2017-02-01 14:38:32 | [diff] [blame] | 80 | EXPECT_EQ(Value::Type::STRING, value.type()); |
| 81 | EXPECT_EQ("foobar", value.GetString()); |
| 82 | } |
| 83 | |
jdoerrie | 9f90ad7 | 2017-09-11 17:23:26 | [diff] [blame] | 84 | TEST(ValuesTest, ConstructStringFromStdStringRRef) { |
jdoerrie | f38f37b | 2017-02-01 14:38:32 | [diff] [blame] | 85 | std::string str = "foobar"; |
jdoerrie | 122c4da | 2017-03-06 11:12:04 | [diff] [blame] | 86 | Value value(std::move(str)); |
jdoerrie | f38f37b | 2017-02-01 14:38:32 | [diff] [blame] | 87 | EXPECT_EQ(Value::Type::STRING, value.type()); |
| 88 | EXPECT_EQ("foobar", value.GetString()); |
| 89 | } |
| 90 | |
| 91 | TEST(ValuesTest, ConstructStringFromConstChar16Ptr) { |
| 92 | string16 str = ASCIIToUTF16("foobar"); |
jdoerrie | 122c4da | 2017-03-06 11:12:04 | [diff] [blame] | 93 | Value value(str.c_str()); |
jdoerrie | f38f37b | 2017-02-01 14:38:32 | [diff] [blame] | 94 | EXPECT_EQ(Value::Type::STRING, value.type()); |
| 95 | EXPECT_EQ("foobar", value.GetString()); |
| 96 | } |
| 97 | |
jdoerrie | 9f90ad7 | 2017-09-11 17:23:26 | [diff] [blame] | 98 | TEST(ValuesTest, ConstructStringFromStringPiece16) { |
jdoerrie | f38f37b | 2017-02-01 14:38:32 | [diff] [blame] | 99 | string16 str = ASCIIToUTF16("foobar"); |
jdoerrie | 9f90ad7 | 2017-09-11 17:23:26 | [diff] [blame] | 100 | Value value{StringPiece16(str)}; |
jdoerrie | f38f37b | 2017-02-01 14:38:32 | [diff] [blame] | 101 | EXPECT_EQ(Value::Type::STRING, value.type()); |
| 102 | EXPECT_EQ("foobar", value.GetString()); |
| 103 | } |
| 104 | |
jdoerrie | e03e80f | 2017-02-15 08:42:14 | [diff] [blame] | 105 | TEST(ValuesTest, ConstructBinary) { |
jdoerrie | 5f12b627 | 2017-04-18 10:22:41 | [diff] [blame] | 106 | Value value(Value::BlobStorage({0xF, 0x0, 0x0, 0xB, 0xA, 0x2})); |
jdoerrie | e03e80f | 2017-02-15 08:42:14 | [diff] [blame] | 107 | EXPECT_EQ(Value::Type::BINARY, value.type()); |
jdoerrie | 5f12b627 | 2017-04-18 10:22:41 | [diff] [blame] | 108 | EXPECT_EQ(Value::BlobStorage({0xF, 0x0, 0x0, 0xB, 0xA, 0x2}), |
| 109 | value.GetBlob()); |
jdoerrie | e03e80f | 2017-02-15 08:42:14 | [diff] [blame] | 110 | } |
| 111 | |
jdoerrie | 8e94554 | 2017-02-17 13:54:49 | [diff] [blame] | 112 | TEST(ValuesTest, ConstructDict) { |
| 113 | DictionaryValue value; |
| 114 | EXPECT_EQ(Value::Type::DICTIONARY, value.type()); |
| 115 | } |
| 116 | |
jdoerrie | cc9f573 | 2017-08-23 14:12:30 | [diff] [blame] | 117 | TEST(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 | |
jdoerrie | 8e94554 | 2017-02-17 13:54:49 | [diff] [blame] | 136 | TEST(ValuesTest, ConstructList) { |
| 137 | ListValue value; |
| 138 | EXPECT_EQ(Value::Type::LIST, value.type()); |
| 139 | } |
| 140 | |
jdoerrie | 52939ed | 2017-04-26 18:19:42 | [diff] [blame] | 141 | TEST(ValuesTest, ConstructListFromStorage) { |
| 142 | Value::ListStorage storage; |
| 143 | storage.emplace_back("foo"); |
jdoerrie | 52939ed | 2017-04-26 18:19:42 | [diff] [blame] | 144 | { |
| 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 | |
jdoerrie | 05eb316 | 2017-02-01 10:36:56 | [diff] [blame] | 162 | // 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. |
| 165 | TEST(ValuesTest, CopyBool) { |
jdoerrie | 23972357 | 2017-03-02 12:09:19 | [diff] [blame] | 166 | Value true_value(true); |
jdoerrie | cc9f573 | 2017-08-23 14:12:30 | [diff] [blame] | 167 | Value copied_true_value(true_value.Clone()); |
jdoerrie | 05eb316 | 2017-02-01 10:36:56 | [diff] [blame] | 168 | EXPECT_EQ(true_value.type(), copied_true_value.type()); |
| 169 | EXPECT_EQ(true_value.GetBool(), copied_true_value.GetBool()); |
| 170 | |
jdoerrie | 23972357 | 2017-03-02 12:09:19 | [diff] [blame] | 171 | Value false_value(false); |
jdoerrie | cc9f573 | 2017-08-23 14:12:30 | [diff] [blame] | 172 | Value copied_false_value(false_value.Clone()); |
jdoerrie | 05eb316 | 2017-02-01 10:36:56 | [diff] [blame] | 173 | EXPECT_EQ(false_value.type(), copied_false_value.type()); |
| 174 | EXPECT_EQ(false_value.GetBool(), copied_false_value.GetBool()); |
| 175 | |
| 176 | Value blank; |
| 177 | |
jdoerrie | cc9f573 | 2017-08-23 14:12:30 | [diff] [blame] | 178 | blank = true_value.Clone(); |
jdoerrie | 05eb316 | 2017-02-01 10:36:56 | [diff] [blame] | 179 | EXPECT_EQ(true_value.type(), blank.type()); |
| 180 | EXPECT_EQ(true_value.GetBool(), blank.GetBool()); |
| 181 | |
jdoerrie | cc9f573 | 2017-08-23 14:12:30 | [diff] [blame] | 182 | blank = false_value.Clone(); |
jdoerrie | 05eb316 | 2017-02-01 10:36:56 | [diff] [blame] | 183 | EXPECT_EQ(false_value.type(), blank.type()); |
| 184 | EXPECT_EQ(false_value.GetBool(), blank.GetBool()); |
| 185 | } |
| 186 | |
| 187 | TEST(ValuesTest, CopyInt) { |
jdoerrie | 23972357 | 2017-03-02 12:09:19 | [diff] [blame] | 188 | Value value(74); |
jdoerrie | cc9f573 | 2017-08-23 14:12:30 | [diff] [blame] | 189 | Value copied_value(value.Clone()); |
jdoerrie | 05eb316 | 2017-02-01 10:36:56 | [diff] [blame] | 190 | EXPECT_EQ(value.type(), copied_value.type()); |
| 191 | EXPECT_EQ(value.GetInt(), copied_value.GetInt()); |
| 192 | |
| 193 | Value blank; |
| 194 | |
jdoerrie | cc9f573 | 2017-08-23 14:12:30 | [diff] [blame] | 195 | blank = value.Clone(); |
jdoerrie | 05eb316 | 2017-02-01 10:36:56 | [diff] [blame] | 196 | EXPECT_EQ(value.type(), blank.type()); |
| 197 | EXPECT_EQ(value.GetInt(), blank.GetInt()); |
| 198 | } |
| 199 | |
| 200 | TEST(ValuesTest, CopyDouble) { |
jdoerrie | 23972357 | 2017-03-02 12:09:19 | [diff] [blame] | 201 | Value value(74.896); |
jdoerrie | cc9f573 | 2017-08-23 14:12:30 | [diff] [blame] | 202 | Value copied_value(value.Clone()); |
jdoerrie | 05eb316 | 2017-02-01 10:36:56 | [diff] [blame] | 203 | EXPECT_EQ(value.type(), copied_value.type()); |
| 204 | EXPECT_EQ(value.GetDouble(), copied_value.GetDouble()); |
| 205 | |
| 206 | Value blank; |
| 207 | |
jdoerrie | cc9f573 | 2017-08-23 14:12:30 | [diff] [blame] | 208 | blank = value.Clone(); |
jdoerrie | 05eb316 | 2017-02-01 10:36:56 | [diff] [blame] | 209 | EXPECT_EQ(value.type(), blank.type()); |
| 210 | EXPECT_EQ(value.GetDouble(), blank.GetDouble()); |
| 211 | } |
| 212 | |
jdoerrie | f38f37b | 2017-02-01 14:38:32 | [diff] [blame] | 213 | TEST(ValuesTest, CopyString) { |
jdoerrie | 122c4da | 2017-03-06 11:12:04 | [diff] [blame] | 214 | Value value("foobar"); |
jdoerrie | cc9f573 | 2017-08-23 14:12:30 | [diff] [blame] | 215 | Value copied_value(value.Clone()); |
jdoerrie | f38f37b | 2017-02-01 14:38:32 | [diff] [blame] | 216 | EXPECT_EQ(value.type(), copied_value.type()); |
| 217 | EXPECT_EQ(value.GetString(), copied_value.GetString()); |
| 218 | |
| 219 | Value blank; |
| 220 | |
jdoerrie | cc9f573 | 2017-08-23 14:12:30 | [diff] [blame] | 221 | blank = value.Clone(); |
jdoerrie | f38f37b | 2017-02-01 14:38:32 | [diff] [blame] | 222 | EXPECT_EQ(value.type(), blank.type()); |
| 223 | EXPECT_EQ(value.GetString(), blank.GetString()); |
| 224 | } |
| 225 | |
jdoerrie | e03e80f | 2017-02-15 08:42:14 | [diff] [blame] | 226 | TEST(ValuesTest, CopyBinary) { |
jdoerrie | 5f12b627 | 2017-04-18 10:22:41 | [diff] [blame] | 227 | Value value(Value::BlobStorage({0xF, 0x0, 0x0, 0xB, 0xA, 0x2})); |
jdoerrie | cc9f573 | 2017-08-23 14:12:30 | [diff] [blame] | 228 | Value copied_value(value.Clone()); |
jdoerrie | e03e80f | 2017-02-15 08:42:14 | [diff] [blame] | 229 | EXPECT_EQ(value.type(), copied_value.type()); |
| 230 | EXPECT_EQ(value.GetBlob(), copied_value.GetBlob()); |
| 231 | |
| 232 | Value blank; |
| 233 | |
jdoerrie | cc9f573 | 2017-08-23 14:12:30 | [diff] [blame] | 234 | blank = value.Clone(); |
jdoerrie | e03e80f | 2017-02-15 08:42:14 | [diff] [blame] | 235 | EXPECT_EQ(value.type(), blank.type()); |
| 236 | EXPECT_EQ(value.GetBlob(), blank.GetBlob()); |
| 237 | } |
| 238 | |
jdoerrie | 8e94554 | 2017-02-17 13:54:49 | [diff] [blame] | 239 | TEST(ValuesTest, CopyDictionary) { |
jdoerrie | 44efa9d | 2017-07-14 14:47:20 | [diff] [blame] | 240 | Value::DictStorage storage; |
Jeremy Roman | 9532f25 | 2017-08-16 23:27:24 | [diff] [blame] | 241 | storage.emplace("Int", std::make_unique<Value>(123)); |
jdoerrie | 44efa9d | 2017-07-14 14:47:20 | [diff] [blame] | 242 | Value value(std::move(storage)); |
jdoerrie | 8e94554 | 2017-02-17 13:54:49 | [diff] [blame] | 243 | |
jdoerrie | cc9f573 | 2017-08-23 14:12:30 | [diff] [blame] | 244 | Value copied_value(value.Clone()); |
jdoerrie | 44efa9d | 2017-07-14 14:47:20 | [diff] [blame] | 245 | EXPECT_EQ(value, copied_value); |
jdoerrie | 8e94554 | 2017-02-17 13:54:49 | [diff] [blame] | 246 | |
jdoerrie | 44efa9d | 2017-07-14 14:47:20 | [diff] [blame] | 247 | Value blank; |
jdoerrie | cc9f573 | 2017-08-23 14:12:30 | [diff] [blame] | 248 | blank = value.Clone(); |
jdoerrie | 44efa9d | 2017-07-14 14:47:20 | [diff] [blame] | 249 | EXPECT_EQ(value, blank); |
jdoerrie | 8e94554 | 2017-02-17 13:54:49 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | TEST(ValuesTest, CopyList) { |
jdoerrie | cc9f573 | 2017-08-23 14:12:30 | [diff] [blame] | 253 | Value::ListStorage storage; |
| 254 | storage.emplace_back(123); |
| 255 | Value value(std::move(storage)); |
jdoerrie | 8e94554 | 2017-02-17 13:54:49 | [diff] [blame] | 256 | |
jdoerrie | cc9f573 | 2017-08-23 14:12:30 | [diff] [blame] | 257 | Value copied_value(value.Clone()); |
jdoerrie | 2b7d0fcd | 2017-04-19 07:15:38 | [diff] [blame] | 258 | EXPECT_EQ(value, copied_value); |
jdoerrie | 8e94554 | 2017-02-17 13:54:49 | [diff] [blame] | 259 | |
jdoerrie | 2b7d0fcd | 2017-04-19 07:15:38 | [diff] [blame] | 260 | Value blank; |
jdoerrie | cc9f573 | 2017-08-23 14:12:30 | [diff] [blame] | 261 | blank = value.Clone(); |
jdoerrie | 2b7d0fcd | 2017-04-19 07:15:38 | [diff] [blame] | 262 | EXPECT_EQ(value, blank); |
jdoerrie | 8e94554 | 2017-02-17 13:54:49 | [diff] [blame] | 263 | } |
| 264 | |
jdoerrie | 05eb316 | 2017-02-01 10:36:56 | [diff] [blame] | 265 | // Group of tests for the move constructors and move-assigmnent. |
| 266 | TEST(ValuesTest, MoveBool) { |
jdoerrie | 23972357 | 2017-03-02 12:09:19 | [diff] [blame] | 267 | Value true_value(true); |
| 268 | Value moved_true_value(std::move(true_value)); |
jdoerrie | 05eb316 | 2017-02-01 10:36:56 | [diff] [blame] | 269 | EXPECT_EQ(Value::Type::BOOLEAN, moved_true_value.type()); |
| 270 | EXPECT_TRUE(moved_true_value.GetBool()); |
| 271 | |
jdoerrie | 23972357 | 2017-03-02 12:09:19 | [diff] [blame] | 272 | Value false_value(false); |
| 273 | Value moved_false_value(std::move(false_value)); |
jdoerrie | 05eb316 | 2017-02-01 10:36:56 | [diff] [blame] | 274 | EXPECT_EQ(Value::Type::BOOLEAN, moved_false_value.type()); |
| 275 | EXPECT_FALSE(moved_false_value.GetBool()); |
| 276 | |
| 277 | Value blank; |
| 278 | |
jdoerrie | 23972357 | 2017-03-02 12:09:19 | [diff] [blame] | 279 | blank = Value(true); |
jdoerrie | 05eb316 | 2017-02-01 10:36:56 | [diff] [blame] | 280 | EXPECT_EQ(Value::Type::BOOLEAN, blank.type()); |
| 281 | EXPECT_TRUE(blank.GetBool()); |
| 282 | |
jdoerrie | 23972357 | 2017-03-02 12:09:19 | [diff] [blame] | 283 | blank = Value(false); |
jdoerrie | 05eb316 | 2017-02-01 10:36:56 | [diff] [blame] | 284 | EXPECT_EQ(Value::Type::BOOLEAN, blank.type()); |
| 285 | EXPECT_FALSE(blank.GetBool()); |
| 286 | } |
| 287 | |
| 288 | TEST(ValuesTest, MoveInt) { |
jdoerrie | 23972357 | 2017-03-02 12:09:19 | [diff] [blame] | 289 | Value value(74); |
| 290 | Value moved_value(std::move(value)); |
jdoerrie | 05eb316 | 2017-02-01 10:36:56 | [diff] [blame] | 291 | EXPECT_EQ(Value::Type::INTEGER, moved_value.type()); |
| 292 | EXPECT_EQ(74, moved_value.GetInt()); |
| 293 | |
| 294 | Value blank; |
| 295 | |
jdoerrie | 23972357 | 2017-03-02 12:09:19 | [diff] [blame] | 296 | blank = Value(47); |
jdoerrie | 05eb316 | 2017-02-01 10:36:56 | [diff] [blame] | 297 | EXPECT_EQ(Value::Type::INTEGER, blank.type()); |
| 298 | EXPECT_EQ(47, blank.GetInt()); |
| 299 | } |
| 300 | |
| 301 | TEST(ValuesTest, MoveDouble) { |
jdoerrie | 23972357 | 2017-03-02 12:09:19 | [diff] [blame] | 302 | Value value(74.896); |
| 303 | Value moved_value(std::move(value)); |
jdoerrie | 05eb316 | 2017-02-01 10:36:56 | [diff] [blame] | 304 | EXPECT_EQ(Value::Type::DOUBLE, moved_value.type()); |
| 305 | EXPECT_EQ(74.896, moved_value.GetDouble()); |
| 306 | |
| 307 | Value blank; |
| 308 | |
jdoerrie | 23972357 | 2017-03-02 12:09:19 | [diff] [blame] | 309 | blank = Value(654.38); |
jdoerrie | 05eb316 | 2017-02-01 10:36:56 | [diff] [blame] | 310 | EXPECT_EQ(Value::Type::DOUBLE, blank.type()); |
| 311 | EXPECT_EQ(654.38, blank.GetDouble()); |
| 312 | } |
| 313 | |
jdoerrie | f38f37b | 2017-02-01 14:38:32 | [diff] [blame] | 314 | TEST(ValuesTest, MoveString) { |
jdoerrie | 122c4da | 2017-03-06 11:12:04 | [diff] [blame] | 315 | Value value("foobar"); |
| 316 | Value moved_value(std::move(value)); |
jdoerrie | f38f37b | 2017-02-01 14:38:32 | [diff] [blame] | 317 | EXPECT_EQ(Value::Type::STRING, moved_value.type()); |
| 318 | EXPECT_EQ("foobar", moved_value.GetString()); |
| 319 | |
| 320 | Value blank; |
| 321 | |
jdoerrie | 122c4da | 2017-03-06 11:12:04 | [diff] [blame] | 322 | blank = Value("foobar"); |
jdoerrie | f38f37b | 2017-02-01 14:38:32 | [diff] [blame] | 323 | EXPECT_EQ(Value::Type::STRING, blank.type()); |
| 324 | EXPECT_EQ("foobar", blank.GetString()); |
| 325 | } |
| 326 | |
jdoerrie | e03e80f | 2017-02-15 08:42:14 | [diff] [blame] | 327 | TEST(ValuesTest, MoveBinary) { |
jdoerrie | 5f12b627 | 2017-04-18 10:22:41 | [diff] [blame] | 328 | const Value::BlobStorage buffer = {0xF, 0x0, 0x0, 0xB, 0xA, 0x2}; |
jdoerrie | 14b25da | 2017-04-11 07:45:50 | [diff] [blame] | 329 | Value value(buffer); |
| 330 | Value moved_value(std::move(value)); |
jdoerrie | e03e80f | 2017-02-15 08:42:14 | [diff] [blame] | 331 | EXPECT_EQ(Value::Type::BINARY, moved_value.type()); |
| 332 | EXPECT_EQ(buffer, moved_value.GetBlob()); |
| 333 | |
| 334 | Value blank; |
| 335 | |
jdoerrie | 14b25da | 2017-04-11 07:45:50 | [diff] [blame] | 336 | blank = Value(buffer); |
jdoerrie | e03e80f | 2017-02-15 08:42:14 | [diff] [blame] | 337 | EXPECT_EQ(Value::Type::BINARY, blank.type()); |
| 338 | EXPECT_EQ(buffer, blank.GetBlob()); |
| 339 | } |
| 340 | |
jdoerrie | 44efa9d | 2017-07-14 14:47:20 | [diff] [blame] | 341 | TEST(ValuesTest, MoveConstructDictionary) { |
| 342 | Value::DictStorage storage; |
Jeremy Roman | 9532f25 | 2017-08-16 23:27:24 | [diff] [blame] | 343 | storage.emplace("Int", std::make_unique<Value>(123)); |
jdoerrie | 8e94554 | 2017-02-17 13:54:49 | [diff] [blame] | 344 | |
jdoerrie | 44efa9d | 2017-07-14 14:47:20 | [diff] [blame] | 345 | Value value(std::move(storage)); |
| 346 | Value moved_value(std::move(value)); |
jdoerrie | 8e94554 | 2017-02-17 13:54:49 | [diff] [blame] | 347 | EXPECT_EQ(Value::Type::DICTIONARY, moved_value.type()); |
jdoerrie | 78ab7a2 | 2017-08-17 19:04:45 | [diff] [blame] | 348 | EXPECT_EQ(123, moved_value.FindKey("Int")->GetInt()); |
jdoerrie | 44efa9d | 2017-07-14 14:47:20 | [diff] [blame] | 349 | } |
| 350 | |
| 351 | TEST(ValuesTest, MoveAssignDictionary) { |
| 352 | Value::DictStorage storage; |
Jeremy Roman | 9532f25 | 2017-08-16 23:27:24 | [diff] [blame] | 353 | storage.emplace("Int", std::make_unique<Value>(123)); |
jdoerrie | 8e94554 | 2017-02-17 13:54:49 | [diff] [blame] | 354 | |
| 355 | Value blank; |
jdoerrie | 44efa9d | 2017-07-14 14:47:20 | [diff] [blame] | 356 | blank = Value(std::move(storage)); |
jdoerrie | 8e94554 | 2017-02-17 13:54:49 | [diff] [blame] | 357 | EXPECT_EQ(Value::Type::DICTIONARY, blank.type()); |
jdoerrie | 78ab7a2 | 2017-08-17 19:04:45 | [diff] [blame] | 358 | EXPECT_EQ(123, blank.FindKey("Int")->GetInt()); |
jdoerrie | 8e94554 | 2017-02-17 13:54:49 | [diff] [blame] | 359 | } |
| 360 | |
| 361 | TEST(ValuesTest, MoveList) { |
jdoerrie | cc9f573 | 2017-08-23 14:12:30 | [diff] [blame] | 362 | Value::ListStorage storage; |
| 363 | storage.emplace_back(123); |
| 364 | Value value(storage); |
jdoerrie | 2b7d0fcd | 2017-04-19 07:15:38 | [diff] [blame] | 365 | Value moved_value(std::move(value)); |
jdoerrie | 8e94554 | 2017-02-17 13:54:49 | [diff] [blame] | 366 | EXPECT_EQ(Value::Type::LIST, moved_value.type()); |
jdoerrie | 2b7d0fcd | 2017-04-19 07:15:38 | [diff] [blame] | 367 | EXPECT_EQ(123, moved_value.GetList().back().GetInt()); |
jdoerrie | 8e94554 | 2017-02-17 13:54:49 | [diff] [blame] | 368 | |
| 369 | Value blank; |
jdoerrie | cc9f573 | 2017-08-23 14:12:30 | [diff] [blame] | 370 | blank = Value(std::move(storage)); |
jdoerrie | 8e94554 | 2017-02-17 13:54:49 | [diff] [blame] | 371 | EXPECT_EQ(Value::Type::LIST, blank.type()); |
jdoerrie | 2b7d0fcd | 2017-04-19 07:15:38 | [diff] [blame] | 372 | EXPECT_EQ(123, blank.GetList().back().GetInt()); |
jdoerrie | 8e94554 | 2017-02-17 13:54:49 | [diff] [blame] | 373 | } |
| 374 | |
jdoerrie | 44efa9d | 2017-07-14 14:47:20 | [diff] [blame] | 375 | TEST(ValuesTest, FindKey) { |
| 376 | Value::DictStorage storage; |
Jeremy Roman | 9532f25 | 2017-08-16 23:27:24 | [diff] [blame] | 377 | storage.emplace("foo", std::make_unique<Value>("bar")); |
jdoerrie | 44efa9d | 2017-07-14 14:47:20 | [diff] [blame] | 378 | Value dict(std::move(storage)); |
jdoerrie | 78ab7a2 | 2017-08-17 19:04:45 | [diff] [blame] | 379 | EXPECT_NE(nullptr, dict.FindKey("foo")); |
| 380 | EXPECT_EQ(nullptr, dict.FindKey("baz")); |
jdoerrie | 8deeec2 | 2017-10-27 13:39:48 | [diff] [blame] | 381 | |
| 382 | // Single not found key. |
| 383 | bool found = dict.FindKey("notfound"); |
| 384 | EXPECT_FALSE(found); |
jdoerrie | 44efa9d | 2017-07-14 14:47:20 | [diff] [blame] | 385 | } |
| 386 | |
| 387 | TEST(ValuesTest, FindKeyChangeValue) { |
| 388 | Value::DictStorage storage; |
Jeremy Roman | 9532f25 | 2017-08-16 23:27:24 | [diff] [blame] | 389 | storage.emplace("foo", std::make_unique<Value>("bar")); |
jdoerrie | 44efa9d | 2017-07-14 14:47:20 | [diff] [blame] | 390 | Value dict(std::move(storage)); |
jdoerrie | 78ab7a2 | 2017-08-17 19:04:45 | [diff] [blame] | 391 | Value* found = dict.FindKey("foo"); |
| 392 | EXPECT_NE(nullptr, found); |
| 393 | EXPECT_EQ("bar", found->GetString()); |
jdoerrie | 44efa9d | 2017-07-14 14:47:20 | [diff] [blame] | 394 | |
jdoerrie | 78ab7a2 | 2017-08-17 19:04:45 | [diff] [blame] | 395 | *found = Value(123); |
| 396 | EXPECT_EQ(123, dict.FindKey("foo")->GetInt()); |
jdoerrie | 44efa9d | 2017-07-14 14:47:20 | [diff] [blame] | 397 | } |
| 398 | |
| 399 | TEST(ValuesTest, FindKeyConst) { |
| 400 | Value::DictStorage storage; |
Jeremy Roman | 9532f25 | 2017-08-16 23:27:24 | [diff] [blame] | 401 | storage.emplace("foo", std::make_unique<Value>("bar")); |
jdoerrie | 44efa9d | 2017-07-14 14:47:20 | [diff] [blame] | 402 | const Value dict(std::move(storage)); |
jdoerrie | 78ab7a2 | 2017-08-17 19:04:45 | [diff] [blame] | 403 | EXPECT_NE(nullptr, dict.FindKey("foo")); |
| 404 | EXPECT_EQ(nullptr, dict.FindKey("baz")); |
jdoerrie | 44efa9d | 2017-07-14 14:47:20 | [diff] [blame] | 405 | } |
| 406 | |
| 407 | TEST(ValuesTest, FindKeyOfType) { |
| 408 | Value::DictStorage storage; |
Jeremy Roman | 9532f25 | 2017-08-16 23:27:24 | [diff] [blame] | 409 | 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)); |
jdoerrie | 44efa9d | 2017-07-14 14:47:20 | [diff] [blame] | 417 | |
| 418 | Value dict(std::move(storage)); |
jdoerrie | 78ab7a2 | 2017-08-17 19:04:45 | [diff] [blame] | 419 | 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)); |
jdoerrie | 44efa9d | 2017-07-14 14:47:20 | [diff] [blame] | 427 | |
jdoerrie | 78ab7a2 | 2017-08-17 19:04:45 | [diff] [blame] | 428 | 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)); |
jdoerrie | 44efa9d | 2017-07-14 14:47:20 | [diff] [blame] | 436 | |
jdoerrie | 78ab7a2 | 2017-08-17 19:04:45 | [diff] [blame] | 437 | 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)); |
jdoerrie | 44efa9d | 2017-07-14 14:47:20 | [diff] [blame] | 445 | |
jdoerrie | 78ab7a2 | 2017-08-17 19:04:45 | [diff] [blame] | 446 | 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)); |
jdoerrie | 44efa9d | 2017-07-14 14:47:20 | [diff] [blame] | 454 | |
jdoerrie | 78ab7a2 | 2017-08-17 19:04:45 | [diff] [blame] | 455 | 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)); |
jdoerrie | 44efa9d | 2017-07-14 14:47:20 | [diff] [blame] | 463 | |
jdoerrie | 78ab7a2 | 2017-08-17 19:04:45 | [diff] [blame] | 464 | 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)); |
jdoerrie | 44efa9d | 2017-07-14 14:47:20 | [diff] [blame] | 472 | |
jdoerrie | 78ab7a2 | 2017-08-17 19:04:45 | [diff] [blame] | 473 | 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)); |
jdoerrie | 44efa9d | 2017-07-14 14:47:20 | [diff] [blame] | 481 | |
jdoerrie | 78ab7a2 | 2017-08-17 19:04:45 | [diff] [blame] | 482 | 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)); |
jdoerrie | 44efa9d | 2017-07-14 14:47:20 | [diff] [blame] | 490 | } |
| 491 | |
| 492 | TEST(ValuesTest, FindKeyOfTypeConst) { |
| 493 | Value::DictStorage storage; |
Jeremy Roman | 9532f25 | 2017-08-16 23:27:24 | [diff] [blame] | 494 | 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)); |
jdoerrie | 44efa9d | 2017-07-14 14:47:20 | [diff] [blame] | 502 | |
| 503 | const Value dict(std::move(storage)); |
jdoerrie | 78ab7a2 | 2017-08-17 19:04:45 | [diff] [blame] | 504 | 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)); |
jdoerrie | 44efa9d | 2017-07-14 14:47:20 | [diff] [blame] | 512 | |
jdoerrie | 78ab7a2 | 2017-08-17 19:04:45 | [diff] [blame] | 513 | 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)); |
jdoerrie | 44efa9d | 2017-07-14 14:47:20 | [diff] [blame] | 521 | |
jdoerrie | 78ab7a2 | 2017-08-17 19:04:45 | [diff] [blame] | 522 | 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)); |
jdoerrie | 44efa9d | 2017-07-14 14:47:20 | [diff] [blame] | 530 | |
jdoerrie | 78ab7a2 | 2017-08-17 19:04:45 | [diff] [blame] | 531 | 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)); |
jdoerrie | 44efa9d | 2017-07-14 14:47:20 | [diff] [blame] | 539 | |
jdoerrie | 78ab7a2 | 2017-08-17 19:04:45 | [diff] [blame] | 540 | 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)); |
jdoerrie | 44efa9d | 2017-07-14 14:47:20 | [diff] [blame] | 548 | |
jdoerrie | 78ab7a2 | 2017-08-17 19:04:45 | [diff] [blame] | 549 | 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)); |
jdoerrie | 44efa9d | 2017-07-14 14:47:20 | [diff] [blame] | 557 | |
jdoerrie | 78ab7a2 | 2017-08-17 19:04:45 | [diff] [blame] | 558 | 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)); |
jdoerrie | 44efa9d | 2017-07-14 14:47:20 | [diff] [blame] | 566 | |
jdoerrie | 78ab7a2 | 2017-08-17 19:04:45 | [diff] [blame] | 567 | 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)); |
jdoerrie | 44efa9d | 2017-07-14 14:47:20 | [diff] [blame] | 575 | } |
| 576 | |
| 577 | TEST(ValuesTest, SetKey) { |
| 578 | Value::DictStorage storage; |
Jeremy Roman | 9532f25 | 2017-08-16 23:27:24 | [diff] [blame] | 579 | 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)); |
jdoerrie | 44efa9d | 2017-07-14 14:47:20 | [diff] [blame] | 587 | |
| 588 | Value dict(Value::Type::DICTIONARY); |
jdoerrie | 4634947 | 2017-08-02 02:20:32 | [diff] [blame] | 589 | 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)); |
jdoerrie | 44efa9d | 2017-07-14 14:47:20 | [diff] [blame] | 594 | 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 Wilson | d16cf4ee | 2017-08-03 00:08:27 | [diff] [blame] | 601 | TEST(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). |
jdoerrie | 8deeec2 | 2017-10-27 13:39:48 | [diff] [blame] | 610 | Value* found = root.FindPath(std::vector<StringPiece>{}); |
Brett Wilson | d16cf4ee | 2017-08-03 00:08:27 | [diff] [blame] | 611 | EXPECT_EQ(&root, found); |
| 612 | |
Brett Wilson | d16cf4ee | 2017-08-03 00:08:27 | [diff] [blame] | 613 | // Double key, second not found. |
jdoerrie | cd02224 | 2017-08-23 08:38:27 | [diff] [blame] | 614 | found = root.FindPath(std::vector<StringPiece>{"foo", "notfound"}); |
Brett Wilson | d16cf4ee | 2017-08-03 00:08:27 | [diff] [blame] | 615 | EXPECT_FALSE(found); |
| 616 | |
| 617 | // Double key, found. |
jdoerrie | cd02224 | 2017-08-23 08:38:27 | [diff] [blame] | 618 | found = root.FindPath(std::vector<StringPiece>{"foo", "bar"}); |
Brett Wilson | d16cf4ee | 2017-08-03 00:08:27 | [diff] [blame] | 619 | EXPECT_TRUE(found); |
| 620 | EXPECT_TRUE(found->is_int()); |
| 621 | EXPECT_EQ(123, found->GetInt()); |
| 622 | } |
| 623 | |
| 624 | TEST(ValuesTest, SetPath) { |
| 625 | Value root(Value::Type::DICTIONARY); |
| 626 | |
jdoerrie | 8deeec2 | 2017-10-27 13:39:48 | [diff] [blame] | 627 | Value* inserted = root.SetPath({"one", "two"}, Value(123)); |
| 628 | Value* found = root.FindPathOfType({"one", "two"}, Value::Type::INTEGER); |
Brett Wilson | d16cf4ee | 2017-08-03 00:08:27 | [diff] [blame] | 629 | ASSERT_TRUE(found); |
| 630 | EXPECT_EQ(inserted, found); |
| 631 | EXPECT_EQ(123, found->GetInt()); |
| 632 | |
jdoerrie | cd02224 | 2017-08-23 08:38:27 | [diff] [blame] | 633 | inserted = root.SetPath(std::vector<StringPiece>{"foo", "bar"}, Value(123)); |
Brett Wilson | d16cf4ee | 2017-08-03 00:08:27 | [diff] [blame] | 634 | 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")); |
jdoerrie | cd02224 | 2017-08-23 08:38:27 | [diff] [blame] | 641 | found = root.FindPathOfType(std::vector<StringPiece>{"foo", "bar"}, |
| 642 | Value::Type::STRING); |
Brett Wilson | d16cf4ee | 2017-08-03 00:08:27 | [diff] [blame] | 643 | ASSERT_TRUE(found); |
| 644 | EXPECT_EQ("hello", found->GetString()); |
| 645 | |
| 646 | // Can't change existing non-dictionary keys to dictionaries. |
jdoerrie | cd02224 | 2017-08-23 08:38:27 | [diff] [blame] | 647 | found = |
| 648 | root.SetPath(std::vector<StringPiece>{"foo", "bar", "baz"}, Value(123)); |
Brett Wilson | d16cf4ee | 2017-08-03 00:08:27 | [diff] [blame] | 649 | EXPECT_FALSE(found); |
| 650 | } |
| 651 | |
jdoerrie | 6478316 | 2017-09-04 16:33:32 | [diff] [blame] | 652 | TEST(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 | |
| 666 | TEST(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. |
jdoerrie | 8deeec2 | 2017-10-27 13:39:48 | [diff] [blame] | 680 | EXPECT_FALSE(root.FindKey("one")); |
jdoerrie | 6478316 | 2017-09-04 16:33:32 | [diff] [blame] | 681 | |
| 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. |
jdoerrie | 8deeec2 | 2017-10-27 13:39:48 | [diff] [blame] | 687 | EXPECT_TRUE(root.FindKey("one")); |
jdoerrie | 6478316 | 2017-09-04 16:33:32 | [diff] [blame] | 688 | EXPECT_TRUE(root.FindPath({"one", "two"})); |
| 689 | EXPECT_TRUE(root.FindPath({"one", "two", "four"})); |
| 690 | } |
| 691 | |
tfarina@chromium.org | 58b916e | 2011-06-28 22:56:33 | [diff] [blame] | 692 | TEST(ValuesTest, Basic) { |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 693 | // Test basic dictionary getting/setting |
| 694 | DictionaryValue settings; |
viettrungluu@chromium.org | 9e4cda733 | 2010-07-31 04:56:14 | [diff] [blame] | 695 | 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)); |
estade | 7bc801fb | 2015-05-07 01:53:08 | [diff] [blame] | 700 | settings.SetBoolean("global", true); |
viettrungluu@chromium.org | 9e4cda733 | 2010-07-31 04:56:14 | [diff] [blame] | 701 | 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 | |
dcheng | 093de9b | 2016-04-04 21:25:51 | [diff] [blame] | 713 | std::unique_ptr<ListValue> new_toolbar_bookmarks(new ListValue); |
danakj | 0c8d4aa | 2015-11-25 05:29:58 | [diff] [blame] | 714 | settings.Set("global.toolbar.bookmarks", std::move(new_toolbar_bookmarks)); |
viettrungluu@chromium.org | 9e4cda733 | 2010-07-31 04:56:14 | [diff] [blame] | 715 | ASSERT_TRUE(settings.GetList("global.toolbar.bookmarks", &toolbar_bookmarks)); |
| 716 | |
dcheng | 093de9b | 2016-04-04 21:25:51 | [diff] [blame] | 717 | std::unique_ptr<DictionaryValue> new_bookmark(new DictionaryValue); |
viettrungluu@chromium.org | 9e4cda733 | 2010-07-31 04:56:14 | [diff] [blame] | 718 | new_bookmark->SetString("name", "Froogle"); |
| 719 | new_bookmark->SetString("url", "http://froogle.com"); |
danakj | 0c8d4aa | 2015-11-25 05:29:58 | [diff] [blame] | 720 | toolbar_bookmarks->Append(std::move(new_bookmark)); |
viettrungluu@chromium.org | 9e4cda733 | 2010-07-31 04:56:14 | [diff] [blame] | 721 | |
| 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.org | 58b916e | 2011-06-28 22:56:33 | [diff] [blame] | 735 | TEST(ValuesTest, List) { |
dcheng | 093de9b | 2016-04-04 21:25:51 | [diff] [blame] | 736 | std::unique_ptr<ListValue> mixed_list(new ListValue()); |
Jeremy Roman | 9532f25 | 2017-08-16 23:27:24 | [diff] [blame] | 737 | 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.org | f82fb495 | 2009-01-20 21:05:32 | [diff] [blame] | 741 | ASSERT_EQ(4u, mixed_list->GetSize()); |
maruel@chromium.org | 52a261f | 2009-03-03 15:01:12 | [diff] [blame] | 742 | |
Lei Zhang | 30895d5 | 2017-10-23 19:14:46 | [diff] [blame] | 743 | Value* value = nullptr; |
aa@chromium.org | f82fb495 | 2009-01-20 21:05:32 | [diff] [blame] | 744 | 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.com | d3accda | 2011-05-02 01:59:21 | [diff] [blame] | 753 | ASSERT_FALSE(mixed_list->GetBoolean(1, &bool_value)); |
| 754 | ASSERT_FALSE(bool_value); |
aa@chromium.org | f82fb495 | 2009-01-20 21:05:32 | [diff] [blame] | 755 | ASSERT_FALSE(mixed_list->GetString(2, &string_value)); |
| 756 | ASSERT_EQ("", string_value); |
yusukes@google.com | d3accda | 2011-05-02 01:59:21 | [diff] [blame] | 757 | ASSERT_FALSE(mixed_list->GetInteger(2, &int_value)); |
| 758 | ASSERT_EQ(0, int_value); |
aa@chromium.org | f82fb495 | 2009-01-20 21:05:32 | [diff] [blame] | 759 | ASSERT_FALSE(mixed_list->GetBoolean(3, &bool_value)); |
thakis@chromium.org | 74e3af7 | 2010-10-03 21:44:39 | [diff] [blame] | 760 | ASSERT_FALSE(bool_value); |
aa@chromium.org | f82fb495 | 2009-01-20 21:05:32 | [diff] [blame] | 761 | |
| 762 | ASSERT_TRUE(mixed_list->GetBoolean(0, &bool_value)); |
thakis@chromium.org | 74e3af7 | 2010-10-03 21:44:39 | [diff] [blame] | 763 | ASSERT_TRUE(bool_value); |
aa@chromium.org | f82fb495 | 2009-01-20 21:05:32 | [diff] [blame] | 764 | ASSERT_TRUE(mixed_list->GetInteger(1, &int_value)); |
| 765 | ASSERT_EQ(42, int_value); |
yusukes@google.com | d3accda | 2011-05-02 01:59:21 | [diff] [blame] | 766 | // 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.org | fb534c9 | 2011-02-01 01:02:07 | [diff] [blame] | 769 | ASSERT_TRUE(mixed_list->GetDouble(2, &double_value)); |
aa@chromium.org | f82fb495 | 2009-01-20 21:05:32 | [diff] [blame] | 770 | ASSERT_EQ(88.8, double_value); |
| 771 | ASSERT_TRUE(mixed_list->GetString(3, &string_value)); |
| 772 | ASSERT_EQ("foo", string_value); |
pastarmovj@chromium.org | 5fb3537 | 2011-09-19 15:23:10 | [diff] [blame] | 773 | |
| 774 | // Try searching in the mixed list. |
jdoerrie | 23972357 | 2017-03-02 12:09:19 | [diff] [blame] | 775 | base::Value sought_value(42); |
| 776 | base::Value not_found_value(false); |
pastarmovj@chromium.org | 5fb3537 | 2011-09-19 15:23:10 | [diff] [blame] | 777 | |
tfarina@chromium.org | 7e3ec42c | 2012-12-16 05:13:21 | [diff] [blame] | 778 | ASSERT_NE(mixed_list->end(), mixed_list->Find(sought_value)); |
jdoerrie | a5676c6 | 2017-04-11 18:09:14 | [diff] [blame] | 779 | ASSERT_TRUE((*mixed_list->Find(sought_value)).GetAsInteger(&int_value)); |
pastarmovj@chromium.org | 5fb3537 | 2011-09-19 15:23:10 | [diff] [blame] | 780 | ASSERT_EQ(42, int_value); |
tfarina@chromium.org | 7e3ec42c | 2012-12-16 05:13:21 | [diff] [blame] | 781 | ASSERT_EQ(mixed_list->end(), mixed_list->Find(not_found_value)); |
aa@chromium.org | f82fb495 | 2009-01-20 21:05:32 | [diff] [blame] | 782 | } |
| 783 | |
tfarina@chromium.org | 58b916e | 2011-06-28 22:56:33 | [diff] [blame] | 784 | TEST(ValuesTest, BinaryValue) { |
jdoerrie | e03e80f | 2017-02-15 08:42:14 | [diff] [blame] | 785 | // Default constructor creates a BinaryValue with a buffer of size 0. |
Jeremy Roman | 9532f25 | 2017-08-16 23:27:24 | [diff] [blame] | 786 | auto binary = std::make_unique<Value>(Value::Type::BINARY); |
patrick@chromium.org | 0a9a0fc | 2009-03-24 23:37:14 | [diff] [blame] | 787 | ASSERT_TRUE(binary.get()); |
jdoerrie | 5328aff | 2017-04-25 20:08:15 | [diff] [blame] | 788 | ASSERT_TRUE(binary->GetBlob().empty()); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 789 | |
| 790 | // Test the common case of a non-empty buffer |
jdoerrie | 5f12b627 | 2017-04-18 10:22:41 | [diff] [blame] | 791 | Value::BlobStorage buffer(15); |
jdoerrie | e03e80f | 2017-02-15 08:42:14 | [diff] [blame] | 792 | char* original_buffer = buffer.data(); |
jdoerrie | 14b25da | 2017-04-11 07:45:50 | [diff] [blame] | 793 | binary.reset(new Value(std::move(buffer))); |
patrick@chromium.org | 0a9a0fc | 2009-03-24 23:37:14 | [diff] [blame] | 794 | ASSERT_TRUE(binary.get()); |
jdoerrie | 5328aff | 2017-04-25 20:08:15 | [diff] [blame] | 795 | ASSERT_TRUE(binary->GetBlob().data()); |
| 796 | ASSERT_EQ(original_buffer, binary->GetBlob().data()); |
| 797 | ASSERT_EQ(15U, binary->GetBlob().size()); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 798 | |
| 799 | char stack_buffer[42]; |
| 800 | memset(stack_buffer, '!', 42); |
jdoerrie | 14b25da | 2017-04-11 07:45:50 | [diff] [blame] | 801 | binary = Value::CreateWithCopiedBuffer(stack_buffer, 42); |
patrick@chromium.org | 0a9a0fc | 2009-03-24 23:37:14 | [diff] [blame] | 802 | ASSERT_TRUE(binary.get()); |
jdoerrie | 5328aff | 2017-04-25 20:08:15 | [diff] [blame] | 803 | 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.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 808 | } |
| 809 | |
tfarina@chromium.org | 58b916e | 2011-06-28 22:56:33 | [diff] [blame] | 810 | TEST(ValuesTest, StringValue) { |
estade@chromium.org | b54e625 | 2014-01-30 10:32:41 | [diff] [blame] | 811 | // Test overloaded StringValue constructor. |
jdoerrie | 122c4da | 2017-03-06 11:12:04 | [diff] [blame] | 812 | std::unique_ptr<Value> narrow_value(new Value("narrow")); |
patrick@chromium.org | 0a9a0fc | 2009-03-24 23:37:14 | [diff] [blame] | 813 | ASSERT_TRUE(narrow_value.get()); |
jdoerrie | dc72ee94 | 2016-12-07 15:43:28 | [diff] [blame] | 814 | ASSERT_TRUE(narrow_value->IsType(Value::Type::STRING)); |
jdoerrie | 122c4da | 2017-03-06 11:12:04 | [diff] [blame] | 815 | std::unique_ptr<Value> utf16_value(new Value(ASCIIToUTF16("utf16"))); |
viettrungluu@chromium.org | e2e593d | 2010-08-03 15:42:58 | [diff] [blame] | 816 | ASSERT_TRUE(utf16_value.get()); |
jdoerrie | dc72ee94 | 2016-12-07 15:43:28 | [diff] [blame] | 817 | ASSERT_TRUE(utf16_value->IsType(Value::Type::STRING)); |
viettrungluu@chromium.org | e2e593d | 2010-08-03 15:42:58 | [diff] [blame] | 818 | |
estade@chromium.org | e552518 | 2014-02-11 22:57:34 | [diff] [blame] | 819 | // Test overloaded GetAsString. |
viettrungluu@chromium.org | e2e593d | 2010-08-03 15:42:58 | [diff] [blame] | 820 | std::string narrow = "http://google.com"; |
| 821 | string16 utf16 = ASCIIToUTF16("http://google.com"); |
jdoerrie | 122c4da | 2017-03-06 11:12:04 | [diff] [blame] | 822 | const Value* string_value = NULL; |
viettrungluu@chromium.org | e2e593d | 2010-08-03 15:42:58 | [diff] [blame] | 823 | ASSERT_TRUE(narrow_value->GetAsString(&narrow)); |
| 824 | ASSERT_TRUE(narrow_value->GetAsString(&utf16)); |
estade@chromium.org | e552518 | 2014-02-11 22:57:34 | [diff] [blame] | 825 | ASSERT_TRUE(narrow_value->GetAsString(&string_value)); |
viettrungluu@chromium.org | e2e593d | 2010-08-03 15:42:58 | [diff] [blame] | 826 | ASSERT_EQ(std::string("narrow"), narrow); |
| 827 | ASSERT_EQ(ASCIIToUTF16("narrow"), utf16); |
estade@chromium.org | e552518 | 2014-02-11 22:57:34 | [diff] [blame] | 828 | ASSERT_EQ(string_value->GetString(), narrow); |
viettrungluu@chromium.org | e2e593d | 2010-08-03 15:42:58 | [diff] [blame] | 829 | |
| 830 | ASSERT_TRUE(utf16_value->GetAsString(&narrow)); |
| 831 | ASSERT_TRUE(utf16_value->GetAsString(&utf16)); |
estade@chromium.org | e552518 | 2014-02-11 22:57:34 | [diff] [blame] | 832 | ASSERT_TRUE(utf16_value->GetAsString(&string_value)); |
viettrungluu@chromium.org | e2e593d | 2010-08-03 15:42:58 | [diff] [blame] | 833 | ASSERT_EQ(std::string("utf16"), narrow); |
| 834 | ASSERT_EQ(ASCIIToUTF16("utf16"), utf16); |
estade@chromium.org | e552518 | 2014-02-11 22:57:34 | [diff] [blame] | 835 | 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))); |
jdoerrie | 122c4da | 2017-03-06 11:12:04 | [diff] [blame] | 840 | ASSERT_TRUE(narrow_value->GetAsString(static_cast<const Value**>(NULL))); |
viettrungluu@chromium.org | e2e593d | 2010-08-03 15:42:58 | [diff] [blame] | 841 | } |
| 842 | |
tfarina@chromium.org | 58b916e | 2011-06-28 22:56:33 | [diff] [blame] | 843 | TEST(ValuesTest, ListDeletion) { |
jdoerrie | 8e94554 | 2017-02-17 13:54:49 | [diff] [blame] | 844 | ListValue list; |
Jeremy Roman | 9532f25 | 2017-08-16 23:27:24 | [diff] [blame] | 845 | list.Append(std::make_unique<Value>()); |
jdoerrie | 8e94554 | 2017-02-17 13:54:49 | [diff] [blame] | 846 | EXPECT_FALSE(list.empty()); |
| 847 | list.Clear(); |
| 848 | EXPECT_TRUE(list.empty()); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 849 | } |
| 850 | |
tfarina@chromium.org | 58b916e | 2011-06-28 22:56:33 | [diff] [blame] | 851 | TEST(ValuesTest, ListRemoval) { |
dcheng | 093de9b | 2016-04-04 21:25:51 | [diff] [blame] | 852 | std::unique_ptr<Value> removed_item; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 853 | |
| 854 | { |
| 855 | ListValue list; |
Jeremy Roman | 9532f25 | 2017-08-16 23:27:24 | [diff] [blame] | 856 | list.Append(std::make_unique<Value>()); |
mmentovai@google.com | f297d18 | 2008-08-21 16:24:51 | [diff] [blame] | 857 | EXPECT_EQ(1U, list.GetSize()); |
deanm@google.com | 836061b | 2008-08-13 14:57:51 | [diff] [blame] | 858 | EXPECT_FALSE(list.Remove(std::numeric_limits<size_t>::max(), |
| 859 | &removed_item)); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 860 | EXPECT_FALSE(list.Remove(1, &removed_item)); |
| 861 | EXPECT_TRUE(list.Remove(0, &removed_item)); |
| 862 | ASSERT_TRUE(removed_item); |
mmentovai@google.com | f297d18 | 2008-08-21 16:24:51 | [diff] [blame] | 863 | EXPECT_EQ(0U, list.GetSize()); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 864 | } |
bauerb@chromium.org | d814a88 | 2013-08-06 13:33:04 | [diff] [blame] | 865 | removed_item.reset(); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 866 | |
| 867 | { |
| 868 | ListValue list; |
Jeremy Roman | 9532f25 | 2017-08-16 23:27:24 | [diff] [blame] | 869 | list.Append(std::make_unique<Value>()); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 870 | EXPECT_TRUE(list.Remove(0, NULL)); |
mmentovai@google.com | f297d18 | 2008-08-21 16:24:51 | [diff] [blame] | 871 | EXPECT_EQ(0U, list.GetSize()); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 872 | } |
pkasting@chromium.org | 6832cbe | 2009-11-30 19:59:11 | [diff] [blame] | 873 | |
| 874 | { |
| 875 | ListValue list; |
Jeremy Roman | 9532f25 | 2017-08-16 23:27:24 | [diff] [blame] | 876 | auto value = std::make_unique<Value>(); |
jdoerrie | cc9f573 | 2017-08-23 14:12:30 | [diff] [blame] | 877 | Value original_value = value->Clone(); |
danakj | 0c8d4aa | 2015-11-25 05:29:58 | [diff] [blame] | 878 | list.Append(std::move(value)); |
tfarina@chromium.org | 4fc3c564 | 2011-08-13 17:34:31 | [diff] [blame] | 879 | size_t index = 0; |
jdoerrie | a5676c6 | 2017-04-11 18:09:14 | [diff] [blame] | 880 | list.Remove(original_value, &index); |
tfarina@chromium.org | 4fc3c564 | 2011-08-13 17:34:31 | [diff] [blame] | 881 | EXPECT_EQ(0U, index); |
pkasting@chromium.org | 6832cbe | 2009-11-30 19:59:11 | [diff] [blame] | 882 | EXPECT_EQ(0U, list.GetSize()); |
| 883 | } |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 884 | } |
| 885 | |
tfarina@chromium.org | 58b916e | 2011-06-28 22:56:33 | [diff] [blame] | 886 | TEST(ValuesTest, DictionaryDeletion) { |
viettrungluu@chromium.org | 9e4cda733 | 2010-07-31 04:56:14 | [diff] [blame] | 887 | std::string key = "test"; |
jdoerrie | 8e94554 | 2017-02-17 13:54:49 | [diff] [blame] | 888 | DictionaryValue dict; |
Jeremy Roman | 9532f25 | 2017-08-16 23:27:24 | [diff] [blame] | 889 | dict.Set(key, std::make_unique<Value>()); |
jdoerrie | 8e94554 | 2017-02-17 13:54:49 | [diff] [blame] | 890 | EXPECT_FALSE(dict.empty()); |
| 891 | dict.Clear(); |
| 892 | EXPECT_TRUE(dict.empty()); |
viettrungluu@chromium.org | 9e4cda733 | 2010-07-31 04:56:14 | [diff] [blame] | 893 | } |
| 894 | |
jdoerrie | b94e542 | 2017-04-28 21:52:58 | [diff] [blame] | 895 | TEST(ValuesTest, DictionarySetReturnsPointer) { |
| 896 | { |
| 897 | DictionaryValue dict; |
Jeremy Roman | 9532f25 | 2017-08-16 23:27:24 | [diff] [blame] | 898 | Value* blank_ptr = dict.Set("foo.bar", std::make_unique<base::Value>()); |
jdoerrie | b94e542 | 2017-04-28 21:52:58 | [diff] [blame] | 899 | EXPECT_EQ(Value::Type::NONE, blank_ptr->type()); |
| 900 | } |
| 901 | |
| 902 | { |
| 903 | DictionaryValue dict; |
| 904 | Value* blank_ptr = dict.SetWithoutPathExpansion( |
Jeremy Roman | 9532f25 | 2017-08-16 23:27:24 | [diff] [blame] | 905 | "foo.bar", std::make_unique<base::Value>()); |
jdoerrie | b94e542 | 2017-04-28 21:52:58 | [diff] [blame] | 906 | EXPECT_EQ(Value::Type::NONE, blank_ptr->type()); |
| 907 | } |
| 908 | |
| 909 | { |
| 910 | DictionaryValue dict; |
jdoerrie | b94e542 | 2017-04-28 21:52:58 | [diff] [blame] | 911 | 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; |
jdoerrie | b94e542 | 2017-04-28 21:52:58 | [diff] [blame] | 918 | 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; |
jdoerrie | b94e542 | 2017-04-28 21:52:58 | [diff] [blame] | 925 | 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; |
jdoerrie | b94e542 | 2017-04-28 21:52:58 | [diff] [blame] | 932 | 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; |
jdoerrie | b94e542 | 2017-04-28 21:52:58 | [diff] [blame] | 939 | DictionaryValue* dict_ptr = dict.SetDictionary( |
Jeremy Roman | 9532f25 | 2017-08-16 23:27:24 | [diff] [blame] | 940 | "foo.bar", std::make_unique<base::DictionaryValue>()); |
jdoerrie | b94e542 | 2017-04-28 21:52:58 | [diff] [blame] | 941 | EXPECT_EQ(Value::Type::DICTIONARY, dict_ptr->type()); |
| 942 | } |
| 943 | |
| 944 | { |
| 945 | DictionaryValue dict; |
| 946 | DictionaryValue* dict_ptr = dict.SetDictionaryWithoutPathExpansion( |
Jeremy Roman | 9532f25 | 2017-08-16 23:27:24 | [diff] [blame] | 947 | "foo.bar", std::make_unique<base::DictionaryValue>()); |
jdoerrie | b94e542 | 2017-04-28 21:52:58 | [diff] [blame] | 948 | EXPECT_EQ(Value::Type::DICTIONARY, dict_ptr->type()); |
| 949 | } |
| 950 | |
| 951 | { |
| 952 | DictionaryValue dict; |
| 953 | ListValue* list_ptr = |
Jeremy Roman | 9532f25 | 2017-08-16 23:27:24 | [diff] [blame] | 954 | dict.SetList("foo.bar", std::make_unique<base::ListValue>()); |
jdoerrie | b94e542 | 2017-04-28 21:52:58 | [diff] [blame] | 955 | EXPECT_EQ(Value::Type::LIST, list_ptr->type()); |
| 956 | } |
| 957 | |
| 958 | { |
| 959 | DictionaryValue dict; |
| 960 | ListValue* list_ptr = dict.SetListWithoutPathExpansion( |
Jeremy Roman | 9532f25 | 2017-08-16 23:27:24 | [diff] [blame] | 961 | "foo.bar", std::make_unique<base::ListValue>()); |
jdoerrie | b94e542 | 2017-04-28 21:52:58 | [diff] [blame] | 962 | EXPECT_EQ(Value::Type::LIST, list_ptr->type()); |
| 963 | } |
| 964 | } |
| 965 | |
tfarina@chromium.org | 58b916e | 2011-06-28 22:56:33 | [diff] [blame] | 966 | TEST(ValuesTest, DictionaryRemoval) { |
viettrungluu@chromium.org | 9e4cda733 | 2010-07-31 04:56:14 | [diff] [blame] | 967 | std::string key = "test"; |
dcheng | 093de9b | 2016-04-04 21:25:51 | [diff] [blame] | 968 | std::unique_ptr<Value> removed_item; |
viettrungluu@chromium.org | 9e4cda733 | 2010-07-31 04:56:14 | [diff] [blame] | 969 | |
| 970 | { |
| 971 | DictionaryValue dict; |
Jeremy Roman | 9532f25 | 2017-08-16 23:27:24 | [diff] [blame] | 972 | dict.Set(key, std::make_unique<Value>()); |
viettrungluu@chromium.org | 9e4cda733 | 2010-07-31 04:56:14 | [diff] [blame] | 973 | 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.org | 9e4cda733 | 2010-07-31 04:56:14 | [diff] [blame] | 979 | |
| 980 | { |
| 981 | DictionaryValue dict; |
Jeremy Roman | 9532f25 | 2017-08-16 23:27:24 | [diff] [blame] | 982 | dict.Set(key, std::make_unique<Value>()); |
viettrungluu@chromium.org | 9e4cda733 | 2010-07-31 04:56:14 | [diff] [blame] | 983 | EXPECT_TRUE(dict.HasKey(key)); |
| 984 | EXPECT_TRUE(dict.Remove(key, NULL)); |
viettrungluu@chromium.org | 9e4cda733 | 2010-07-31 04:56:14 | [diff] [blame] | 985 | EXPECT_FALSE(dict.HasKey(key)); |
| 986 | } |
| 987 | } |
| 988 | |
tfarina@chromium.org | 58b916e | 2011-06-28 22:56:33 | [diff] [blame] | 989 | TEST(ValuesTest, DictionaryWithoutPathExpansion) { |
pkasting@chromium.org | 4dad9ad8 | 2009-11-25 20:47:52 | [diff] [blame] | 990 | DictionaryValue dict; |
Jeremy Roman | 9532f25 | 2017-08-16 23:27:24 | [diff] [blame] | 991 | dict.Set("this.is.expanded", std::make_unique<Value>()); |
| 992 | dict.SetWithoutPathExpansion("this.isnt.expanded", std::make_unique<Value>()); |
estade | ca79848 | 2015-01-06 20:06:50 | [diff] [blame] | 993 | |
| 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)); |
jdoerrie | 76cee9c | 2017-10-06 22:42:42 | [diff] [blame] | 1008 | EXPECT_EQ(Value::Type::NONE, value4->type()); |
estade | ca79848 | 2015-01-06 20:06:50 | [diff] [blame] | 1009 | } |
| 1010 | |
| 1011 | // Tests the deprecated version of SetWithoutPathExpansion. |
| 1012 | // TODO(estade): remove. |
| 1013 | TEST(ValuesTest, DictionaryWithoutPathExpansionDeprecated) { |
| 1014 | DictionaryValue dict; |
Jeremy Roman | 9532f25 | 2017-08-16 23:27:24 | [diff] [blame] | 1015 | dict.Set("this.is.expanded", std::make_unique<Value>()); |
| 1016 | dict.SetWithoutPathExpansion("this.isnt.expanded", std::make_unique<Value>()); |
viettrungluu@chromium.org | 9e4cda733 | 2010-07-31 04:56:14 | [diff] [blame] | 1017 | |
| 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)); |
jdoerrie | 76cee9c | 2017-10-06 22:42:42 | [diff] [blame] | 1032 | EXPECT_EQ(Value::Type::NONE, value4->type()); |
viettrungluu@chromium.org | 9e4cda733 | 2010-07-31 04:56:14 | [diff] [blame] | 1033 | } |
| 1034 | |
gab@chromium.org | aa32833 | 2013-11-27 01:38:24 | [diff] [blame] | 1035 | TEST(ValuesTest, DictionaryRemovePath) { |
| 1036 | DictionaryValue dict; |
estade | ca79848 | 2015-01-06 20:06:50 | [diff] [blame] | 1037 | dict.SetInteger("a.long.way.down", 1); |
| 1038 | dict.SetBoolean("a.long.key.path", true); |
gab@chromium.org | aa32833 | 2013-11-27 01:38:24 | [diff] [blame] | 1039 | |
dcheng | 093de9b | 2016-04-04 21:25:51 | [diff] [blame] | 1040 | std::unique_ptr<Value> removed_item; |
gab@chromium.org | aa32833 | 2013-11-27 01:38:24 | [diff] [blame] | 1041 | EXPECT_TRUE(dict.RemovePath("a.long.way.down", &removed_item)); |
| 1042 | ASSERT_TRUE(removed_item); |
jdoerrie | dc72ee94 | 2016-12-07 15:43:28 | [diff] [blame] | 1043 | EXPECT_TRUE(removed_item->IsType(base::Value::Type::INTEGER)); |
gab@chromium.org | aa32833 | 2013-11-27 01:38:24 | [diff] [blame] | 1044 | 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); |
jdoerrie | dc72ee94 | 2016-12-07 15:43:28 | [diff] [blame] | 1056 | EXPECT_TRUE(removed_item->IsType(base::Value::Type::BOOLEAN)); |
gab@chromium.org | aa32833 | 2013-11-27 01:38:24 | [diff] [blame] | 1057 | EXPECT_TRUE(dict.empty()); |
| 1058 | } |
| 1059 | |
tfarina@chromium.org | 58b916e | 2011-06-28 22:56:33 | [diff] [blame] | 1060 | TEST(ValuesTest, DeepCopy) { |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 1061 | DictionaryValue original_dict; |
Jeremy Roman | 9532f25 | 2017-08-16 23:27:24 | [diff] [blame] | 1062 | 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.org | 9e4cda733 | 2010-07-31 04:56:14 | [diff] [blame] | 1071 | |
jdoerrie | 3a7275f | 2017-05-31 05:58:33 | [diff] [blame] | 1072 | Value* binary_weak = original_dict.Set( |
Jeremy Roman | 9532f25 | 2017-08-16 23:27:24 | [diff] [blame] | 1073 | "binary", std::make_unique<Value>(Value::BlobStorage(42, '!'))); |
viettrungluu@chromium.org | 9e4cda733 | 2010-07-31 04:56:14 | [diff] [blame] | 1074 | |
jdoerrie | cc9f573 | 2017-08-23 14:12:30 | [diff] [blame] | 1075 | 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))); |
jdoerrie | 3a7275f | 2017-05-31 05:58:33 | [diff] [blame] | 1080 | Value* list_element_0_weak = &list_weak->GetList()[0]; |
| 1081 | Value* list_element_1_weak = &list_weak->GetList()[1]; |
viettrungluu@chromium.org | 9e4cda733 | 2010-07-31 04:56:14 | [diff] [blame] | 1082 | |
Jeremy Roman | 9532f25 | 2017-08-16 23:27:24 | [diff] [blame] | 1083 | DictionaryValue* dict_weak = original_dict.SetDictionary( |
| 1084 | "dictionary", std::make_unique<DictionaryValue>()); |
jdoerrie | 3a7275f | 2017-05-31 05:58:33 | [diff] [blame] | 1085 | dict_weak->SetString("key", "value"); |
battre@chromium.org | 5cf906f8 | 2011-11-26 01:11:44 | [diff] [blame] | 1086 | |
jdoerrie | cc9f573 | 2017-08-23 14:12:30 | [diff] [blame] | 1087 | auto copy_dict = original_dict.CreateDeepCopy(); |
viettrungluu@chromium.org | 9e4cda733 | 2010-07-31 04:56:14 | [diff] [blame] | 1088 | 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", ©_null)); |
| 1093 | ASSERT_TRUE(copy_null); |
jdoerrie | 3a7275f | 2017-05-31 05:58:33 | [diff] [blame] | 1094 | ASSERT_NE(copy_null, null_weak); |
jdoerrie | dc72ee94 | 2016-12-07 15:43:28 | [diff] [blame] | 1095 | ASSERT_TRUE(copy_null->IsType(Value::Type::NONE)); |
viettrungluu@chromium.org | 9e4cda733 | 2010-07-31 04:56:14 | [diff] [blame] | 1096 | |
| 1097 | Value* copy_bool = NULL; |
| 1098 | ASSERT_TRUE(copy_dict->Get("bool", ©_bool)); |
| 1099 | ASSERT_TRUE(copy_bool); |
jdoerrie | 3a7275f | 2017-05-31 05:58:33 | [diff] [blame] | 1100 | ASSERT_NE(copy_bool, bool_weak); |
jdoerrie | dc72ee94 | 2016-12-07 15:43:28 | [diff] [blame] | 1101 | ASSERT_TRUE(copy_bool->IsType(Value::Type::BOOLEAN)); |
viettrungluu@chromium.org | 9e4cda733 | 2010-07-31 04:56:14 | [diff] [blame] | 1102 | bool copy_bool_value = false; |
| 1103 | ASSERT_TRUE(copy_bool->GetAsBoolean(©_bool_value)); |
| 1104 | ASSERT_TRUE(copy_bool_value); |
| 1105 | |
| 1106 | Value* copy_int = NULL; |
| 1107 | ASSERT_TRUE(copy_dict->Get("int", ©_int)); |
| 1108 | ASSERT_TRUE(copy_int); |
jdoerrie | 3a7275f | 2017-05-31 05:58:33 | [diff] [blame] | 1109 | ASSERT_NE(copy_int, int_weak); |
jdoerrie | dc72ee94 | 2016-12-07 15:43:28 | [diff] [blame] | 1110 | ASSERT_TRUE(copy_int->IsType(Value::Type::INTEGER)); |
viettrungluu@chromium.org | 9e4cda733 | 2010-07-31 04:56:14 | [diff] [blame] | 1111 | int copy_int_value = 0; |
| 1112 | ASSERT_TRUE(copy_int->GetAsInteger(©_int_value)); |
| 1113 | ASSERT_EQ(42, copy_int_value); |
| 1114 | |
arv@chromium.org | fb534c9 | 2011-02-01 01:02:07 | [diff] [blame] | 1115 | Value* copy_double = NULL; |
| 1116 | ASSERT_TRUE(copy_dict->Get("double", ©_double)); |
| 1117 | ASSERT_TRUE(copy_double); |
jdoerrie | 3a7275f | 2017-05-31 05:58:33 | [diff] [blame] | 1118 | ASSERT_NE(copy_double, double_weak); |
jdoerrie | dc72ee94 | 2016-12-07 15:43:28 | [diff] [blame] | 1119 | ASSERT_TRUE(copy_double->IsType(Value::Type::DOUBLE)); |
arv@chromium.org | fb534c9 | 2011-02-01 01:02:07 | [diff] [blame] | 1120 | double copy_double_value = 0; |
| 1121 | ASSERT_TRUE(copy_double->GetAsDouble(©_double_value)); |
| 1122 | ASSERT_EQ(3.14, copy_double_value); |
viettrungluu@chromium.org | 9e4cda733 | 2010-07-31 04:56:14 | [diff] [blame] | 1123 | |
| 1124 | Value* copy_string = NULL; |
| 1125 | ASSERT_TRUE(copy_dict->Get("string", ©_string)); |
| 1126 | ASSERT_TRUE(copy_string); |
jdoerrie | 3a7275f | 2017-05-31 05:58:33 | [diff] [blame] | 1127 | ASSERT_NE(copy_string, string_weak); |
jdoerrie | dc72ee94 | 2016-12-07 15:43:28 | [diff] [blame] | 1128 | ASSERT_TRUE(copy_string->IsType(Value::Type::STRING)); |
viettrungluu@chromium.org | 9e4cda733 | 2010-07-31 04:56:14 | [diff] [blame] | 1129 | std::string copy_string_value; |
viettrungluu@chromium.org | e2e593d | 2010-08-03 15:42:58 | [diff] [blame] | 1130 | string16 copy_string16_value; |
viettrungluu@chromium.org | 9e4cda733 | 2010-07-31 04:56:14 | [diff] [blame] | 1131 | ASSERT_TRUE(copy_string->GetAsString(©_string_value)); |
viettrungluu@chromium.org | e2e593d | 2010-08-03 15:42:58 | [diff] [blame] | 1132 | ASSERT_TRUE(copy_string->GetAsString(©_string16_value)); |
viettrungluu@chromium.org | 9e4cda733 | 2010-07-31 04:56:14 | [diff] [blame] | 1133 | ASSERT_EQ(std::string("hello"), copy_string_value); |
viettrungluu@chromium.org | e2e593d | 2010-08-03 15:42:58 | [diff] [blame] | 1134 | ASSERT_EQ(ASCIIToUTF16("hello"), copy_string16_value); |
viettrungluu@chromium.org | 9e4cda733 | 2010-07-31 04:56:14 | [diff] [blame] | 1135 | |
viettrungluu@chromium.org | e2e593d | 2010-08-03 15:42:58 | [diff] [blame] | 1136 | Value* copy_string16 = NULL; |
| 1137 | ASSERT_TRUE(copy_dict->Get("string16", ©_string16)); |
| 1138 | ASSERT_TRUE(copy_string16); |
jdoerrie | 3a7275f | 2017-05-31 05:58:33 | [diff] [blame] | 1139 | ASSERT_NE(copy_string16, string16_weak); |
jdoerrie | dc72ee94 | 2016-12-07 15:43:28 | [diff] [blame] | 1140 | ASSERT_TRUE(copy_string16->IsType(Value::Type::STRING)); |
viettrungluu@chromium.org | e2e593d | 2010-08-03 15:42:58 | [diff] [blame] | 1141 | ASSERT_TRUE(copy_string16->GetAsString(©_string_value)); |
| 1142 | ASSERT_TRUE(copy_string16->GetAsString(©_string16_value)); |
viettrungluu@chromium.org | 9e4cda733 | 2010-07-31 04:56:14 | [diff] [blame] | 1143 | ASSERT_EQ(std::string("hello16"), copy_string_value); |
viettrungluu@chromium.org | e2e593d | 2010-08-03 15:42:58 | [diff] [blame] | 1144 | ASSERT_EQ(ASCIIToUTF16("hello16"), copy_string16_value); |
viettrungluu@chromium.org | 9e4cda733 | 2010-07-31 04:56:14 | [diff] [blame] | 1145 | |
| 1146 | Value* copy_binary = NULL; |
| 1147 | ASSERT_TRUE(copy_dict->Get("binary", ©_binary)); |
| 1148 | ASSERT_TRUE(copy_binary); |
jdoerrie | 3a7275f | 2017-05-31 05:58:33 | [diff] [blame] | 1149 | ASSERT_NE(copy_binary, binary_weak); |
jdoerrie | dc72ee94 | 2016-12-07 15:43:28 | [diff] [blame] | 1150 | ASSERT_TRUE(copy_binary->IsType(Value::Type::BINARY)); |
jdoerrie | 3a7275f | 2017-05-31 05:58:33 | [diff] [blame] | 1151 | ASSERT_NE(binary_weak->GetBlob().data(), copy_binary->GetBlob().data()); |
| 1152 | ASSERT_EQ(binary_weak->GetBlob(), copy_binary->GetBlob()); |
viettrungluu@chromium.org | 9e4cda733 | 2010-07-31 04:56:14 | [diff] [blame] | 1153 | |
| 1154 | Value* copy_value = NULL; |
| 1155 | ASSERT_TRUE(copy_dict->Get("list", ©_value)); |
| 1156 | ASSERT_TRUE(copy_value); |
jdoerrie | 3a7275f | 2017-05-31 05:58:33 | [diff] [blame] | 1157 | ASSERT_NE(copy_value, list_weak); |
jdoerrie | dc72ee94 | 2016-12-07 15:43:28 | [diff] [blame] | 1158 | ASSERT_TRUE(copy_value->IsType(Value::Type::LIST)); |
battre@chromium.org | 5cf906f8 | 2011-11-26 01:11:44 | [diff] [blame] | 1159 | ListValue* copy_list = NULL; |
| 1160 | ASSERT_TRUE(copy_value->GetAsList(©_list)); |
| 1161 | ASSERT_TRUE(copy_list); |
viettrungluu@chromium.org | 9e4cda733 | 2010-07-31 04:56:14 | [diff] [blame] | 1162 | ASSERT_EQ(2U, copy_list->GetSize()); |
| 1163 | |
| 1164 | Value* copy_list_element_0; |
| 1165 | ASSERT_TRUE(copy_list->Get(0, ©_list_element_0)); |
| 1166 | ASSERT_TRUE(copy_list_element_0); |
jdoerrie | 3a7275f | 2017-05-31 05:58:33 | [diff] [blame] | 1167 | ASSERT_NE(copy_list_element_0, list_element_0_weak); |
viettrungluu@chromium.org | 9e4cda733 | 2010-07-31 04:56:14 | [diff] [blame] | 1168 | int copy_list_element_0_value; |
| 1169 | ASSERT_TRUE(copy_list_element_0->GetAsInteger(©_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, ©_list_element_1)); |
| 1174 | ASSERT_TRUE(copy_list_element_1); |
jdoerrie | 3a7275f | 2017-05-31 05:58:33 | [diff] [blame] | 1175 | ASSERT_NE(copy_list_element_1, list_element_1_weak); |
viettrungluu@chromium.org | 9e4cda733 | 2010-07-31 04:56:14 | [diff] [blame] | 1176 | int copy_list_element_1_value; |
| 1177 | ASSERT_TRUE(copy_list_element_1->GetAsInteger(©_list_element_1_value)); |
| 1178 | ASSERT_EQ(1, copy_list_element_1_value); |
battre@chromium.org | 5cf906f8 | 2011-11-26 01:11:44 | [diff] [blame] | 1179 | |
| 1180 | copy_value = NULL; |
| 1181 | ASSERT_TRUE(copy_dict->Get("dictionary", ©_value)); |
| 1182 | ASSERT_TRUE(copy_value); |
jdoerrie | 3a7275f | 2017-05-31 05:58:33 | [diff] [blame] | 1183 | ASSERT_NE(copy_value, dict_weak); |
jdoerrie | dc72ee94 | 2016-12-07 15:43:28 | [diff] [blame] | 1184 | ASSERT_TRUE(copy_value->IsType(Value::Type::DICTIONARY)); |
battre@chromium.org | 5cf906f8 | 2011-11-26 01:11:44 | [diff] [blame] | 1185 | DictionaryValue* copy_nested_dictionary = NULL; |
| 1186 | ASSERT_TRUE(copy_value->GetAsDictionary(©_nested_dictionary)); |
| 1187 | ASSERT_TRUE(copy_nested_dictionary); |
| 1188 | EXPECT_TRUE(copy_nested_dictionary->HasKey("key")); |
viettrungluu@chromium.org | 9e4cda733 | 2010-07-31 04:56:14 | [diff] [blame] | 1189 | } |
| 1190 | |
tfarina@chromium.org | 58b916e | 2011-06-28 22:56:33 | [diff] [blame] | 1191 | TEST(ValuesTest, Equals) { |
Jeremy Roman | 9532f25 | 2017-08-16 23:27:24 | [diff] [blame] | 1192 | auto null1 = std::make_unique<Value>(); |
| 1193 | auto null2 = std::make_unique<Value>(); |
danakj | 8dba5a5 | 2015-05-11 21:24:09 | [diff] [blame] | 1194 | EXPECT_NE(null1.get(), null2.get()); |
jdoerrie | 5c1cee11 | 2017-03-28 17:52:00 | [diff] [blame] | 1195 | EXPECT_EQ(*null1, *null2); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 1196 | |
jdoerrie | 23972357 | 2017-03-02 12:09:19 | [diff] [blame] | 1197 | Value boolean(false); |
jdoerrie | 5c1cee11 | 2017-03-28 17:52:00 | [diff] [blame] | 1198 | EXPECT_NE(*null1, boolean); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 1199 | |
| 1200 | DictionaryValue dv; |
viettrungluu@chromium.org | 9e4cda733 | 2010-07-31 04:56:14 | [diff] [blame] | 1201 | dv.SetBoolean("a", false); |
| 1202 | dv.SetInteger("b", 2); |
arv@chromium.org | fb534c9 | 2011-02-01 01:02:07 | [diff] [blame] | 1203 | dv.SetDouble("c", 2.5); |
viettrungluu@chromium.org | 9e4cda733 | 2010-07-31 04:56:14 | [diff] [blame] | 1204 | dv.SetString("d1", "string"); |
viettrungluu@chromium.org | ff4c1d8 | 2010-08-04 16:58:12 | [diff] [blame] | 1205 | dv.SetString("d2", ASCIIToUTF16("http://google.com")); |
Jeremy Roman | 9532f25 | 2017-08-16 23:27:24 | [diff] [blame] | 1206 | dv.Set("e", std::make_unique<Value>()); |
viettrungluu@chromium.org | 9e4cda733 | 2010-07-31 04:56:14 | [diff] [blame] | 1207 | |
jdoerrie | cc9f573 | 2017-08-23 14:12:30 | [diff] [blame] | 1208 | auto copy = dv.CreateDeepCopy(); |
jdoerrie | 5c1cee11 | 2017-03-28 17:52:00 | [diff] [blame] | 1209 | EXPECT_EQ(dv, *copy); |
viettrungluu@chromium.org | 9e4cda733 | 2010-07-31 04:56:14 | [diff] [blame] | 1210 | |
dcheng | 093de9b | 2016-04-04 21:25:51 | [diff] [blame] | 1211 | std::unique_ptr<ListValue> list(new ListValue); |
Jeremy Roman | 9532f25 | 2017-08-16 23:27:24 | [diff] [blame] | 1212 | list->Append(std::make_unique<Value>()); |
dcheng | 093de9b | 2016-04-04 21:25:51 | [diff] [blame] | 1213 | list->Append(WrapUnique(new DictionaryValue)); |
jdoerrie | cc9f573 | 2017-08-23 14:12:30 | [diff] [blame] | 1214 | auto list_copy = std::make_unique<Value>(list->Clone()); |
viettrungluu@chromium.org | 9e4cda733 | 2010-07-31 04:56:14 | [diff] [blame] | 1215 | |
jdoerrie | 3a7275f | 2017-05-31 05:58:33 | [diff] [blame] | 1216 | ListValue* list_weak = dv.SetList("f", std::move(list)); |
jdoerrie | 5c1cee11 | 2017-03-28 17:52:00 | [diff] [blame] | 1217 | EXPECT_NE(dv, *copy); |
danakj | 0c8d4aa | 2015-11-25 05:29:58 | [diff] [blame] | 1218 | copy->Set("f", std::move(list_copy)); |
jdoerrie | 5c1cee11 | 2017-03-28 17:52:00 | [diff] [blame] | 1219 | EXPECT_EQ(dv, *copy); |
viettrungluu@chromium.org | 9e4cda733 | 2010-07-31 04:56:14 | [diff] [blame] | 1220 | |
Jeremy Roman | 9532f25 | 2017-08-16 23:27:24 | [diff] [blame] | 1221 | list_weak->Append(std::make_unique<Value>(true)); |
jdoerrie | 5c1cee11 | 2017-03-28 17:52:00 | [diff] [blame] | 1222 | EXPECT_NE(dv, *copy); |
mnissler@chromium.org | dc1f244 | 2010-08-18 16:23:40 | [diff] [blame] | 1223 | |
| 1224 | // Check if Equals detects differences in only the keys. |
jdoerrie | cc9f573 | 2017-08-23 14:12:30 | [diff] [blame] | 1225 | copy = dv.CreateDeepCopy(); |
jdoerrie | 5c1cee11 | 2017-03-28 17:52:00 | [diff] [blame] | 1226 | EXPECT_EQ(dv, *copy); |
mnissler@chromium.org | dc1f244 | 2010-08-18 16:23:40 | [diff] [blame] | 1227 | copy->Remove("a", NULL); |
| 1228 | copy->SetBoolean("aa", false); |
jdoerrie | 5c1cee11 | 2017-03-28 17:52:00 | [diff] [blame] | 1229 | EXPECT_NE(dv, *copy); |
viettrungluu@chromium.org | 9e4cda733 | 2010-07-31 04:56:14 | [diff] [blame] | 1230 | } |
| 1231 | |
jdoerrie | 5c1cee11 | 2017-03-28 17:52:00 | [diff] [blame] | 1232 | TEST(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. |
jdoerrie | 5f12b627 | 2017-04-18 10:22:41 | [diff] [blame] | 1284 | Value binary1(Value::BlobStorage{0x01}); |
| 1285 | Value binary2(Value::BlobStorage{0x02}); |
jdoerrie | 5c1cee11 | 2017-03-28 17:52:00 | [diff] [blame] | 1286 | 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. |
jdoerrie | cc9f573 | 2017-08-23 14:12:30 | [diff] [blame] | 1338 | 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)); |
jdoerrie | 5c1cee11 | 2017-03-28 17:52:00 | [diff] [blame] | 1347 | 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.org | 58b916e | 2011-06-28 22:56:33 | [diff] [blame] | 1359 | TEST(ValuesTest, DeepCopyCovariantReturnTypes) { |
akalin@chromium.org | 16f47e0 | 2011-01-18 02:16:59 | [diff] [blame] | 1360 | DictionaryValue original_dict; |
jdoerrie | cc9f573 | 2017-08-23 14:12:30 | [diff] [blame] | 1361 | 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.org | 16f47e0 | 2011-01-18 02:16:59 | [diff] [blame] | 1370 | |
jdoerrie | cc9f573 | 2017-08-23 14:12:30 | [diff] [blame] | 1371 | 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.org | 16f47e0 | 2011-01-18 02:16:59 | [diff] [blame] | 1375 | |
jdoerrie | cc9f573 | 2017-08-23 14:12:30 | [diff] [blame] | 1376 | 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.org | 16f47e0 | 2011-01-18 02:16:59 | [diff] [blame] | 1385 | |
jdoerrie | 5c1cee11 | 2017-03-28 17:52:00 | [diff] [blame] | 1386 | EXPECT_EQ(original_dict, *copy_dict); |
jdoerrie | 3a7275f | 2017-05-31 05:58:33 | [diff] [blame] | 1387 | 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.org | 16f47e0 | 2011-01-18 02:16:59 | [diff] [blame] | 1395 | } |
| 1396 | |
tfarina@chromium.org | 58b916e | 2011-06-28 22:56:33 | [diff] [blame] | 1397 | TEST(ValuesTest, RemoveEmptyChildren) { |
Jeremy Roman | 9532f25 | 2017-08-16 23:27:24 | [diff] [blame] | 1398 | auto root = std::make_unique<DictionaryValue>(); |
tony@chromium.org | ec330b5 | 2009-12-02 00:20:32 | [diff] [blame] | 1399 | // Remove empty lists and dictionaries. |
Jeremy Roman | 9532f25 | 2017-08-16 23:27:24 | [diff] [blame] | 1400 | 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>()); |
estade | 6e04d50 | 2015-05-23 02:42:09 | [diff] [blame] | 1404 | root = root->DeepCopyWithoutEmptyChildren(); |
viettrungluu@chromium.org | 9e4cda733 | 2010-07-31 04:56:14 | [diff] [blame] | 1405 | EXPECT_TRUE(root->empty()); |
| 1406 | |
| 1407 | // Make sure we don't prune too much. |
| 1408 | root->SetBoolean("bool", true); |
Jeremy Roman | 9532f25 | 2017-08-16 23:27:24 | [diff] [blame] | 1409 | root->Set("empty_dict", std::make_unique<DictionaryValue>()); |
dcheng@chromium.org | 007b3f8 | 2013-04-09 08:46:45 | [diff] [blame] | 1410 | root->SetString("empty_string", std::string()); |
estade | 6e04d50 | 2015-05-23 02:42:09 | [diff] [blame] | 1411 | root = root->DeepCopyWithoutEmptyChildren(); |
viettrungluu@chromium.org | 9e4cda733 | 2010-07-31 04:56:14 | [diff] [blame] | 1412 | EXPECT_EQ(2U, root->size()); |
| 1413 | |
| 1414 | // Should do nothing. |
estade | 6e04d50 | 2015-05-23 02:42:09 | [diff] [blame] | 1415 | root = root->DeepCopyWithoutEmptyChildren(); |
viettrungluu@chromium.org | 9e4cda733 | 2010-07-31 04:56:14 | [diff] [blame] | 1416 | 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 Roman | 9532f25 | 2017-08-16 23:27:24 | [diff] [blame] | 1421 | root->Set("a.b.c.d.e", std::make_unique<DictionaryValue>()); |
estade | 6e04d50 | 2015-05-23 02:42:09 | [diff] [blame] | 1422 | root = root->DeepCopyWithoutEmptyChildren(); |
viettrungluu@chromium.org | 9e4cda733 | 2010-07-31 04:56:14 | [diff] [blame] | 1423 | EXPECT_EQ(2U, root->size()); |
| 1424 | } |
| 1425 | { |
Jeremy Roman | 9532f25 | 2017-08-16 23:27:24 | [diff] [blame] | 1426 | auto inner = std::make_unique<DictionaryValue>(); |
| 1427 | inner->Set("empty_dict", std::make_unique<DictionaryValue>()); |
| 1428 | inner->Set("empty_list", std::make_unique<ListValue>()); |
danakj | 0c8d4aa | 2015-11-25 05:29:58 | [diff] [blame] | 1429 | root->Set("dict_with_empty_children", std::move(inner)); |
estade | 6e04d50 | 2015-05-23 02:42:09 | [diff] [blame] | 1430 | root = root->DeepCopyWithoutEmptyChildren(); |
viettrungluu@chromium.org | 9e4cda733 | 2010-07-31 04:56:14 | [diff] [blame] | 1431 | EXPECT_EQ(2U, root->size()); |
| 1432 | } |
| 1433 | { |
Jeremy Roman | 9532f25 | 2017-08-16 23:27:24 | [diff] [blame] | 1434 | auto inner = std::make_unique<ListValue>(); |
| 1435 | inner->Append(std::make_unique<DictionaryValue>()); |
| 1436 | inner->Append(std::make_unique<ListValue>()); |
danakj | 0c8d4aa | 2015-11-25 05:29:58 | [diff] [blame] | 1437 | root->Set("list_with_empty_children", std::move(inner)); |
estade | 6e04d50 | 2015-05-23 02:42:09 | [diff] [blame] | 1438 | root = root->DeepCopyWithoutEmptyChildren(); |
viettrungluu@chromium.org | 9e4cda733 | 2010-07-31 04:56:14 | [diff] [blame] | 1439 | EXPECT_EQ(2U, root->size()); |
| 1440 | } |
| 1441 | |
| 1442 | // Nested with siblings. |
| 1443 | { |
Jeremy Roman | 9532f25 | 2017-08-16 23:27:24 | [diff] [blame] | 1444 | auto inner = std::make_unique<ListValue>(); |
| 1445 | inner->Append(std::make_unique<DictionaryValue>()); |
| 1446 | inner->Append(std::make_unique<ListValue>()); |
danakj | 0c8d4aa | 2015-11-25 05:29:58 | [diff] [blame] | 1447 | root->Set("list_with_empty_children", std::move(inner)); |
Jeremy Roman | 9532f25 | 2017-08-16 23:27:24 | [diff] [blame] | 1448 | auto inner2 = std::make_unique<DictionaryValue>(); |
| 1449 | inner2->Set("empty_dict", std::make_unique<DictionaryValue>()); |
| 1450 | inner2->Set("empty_list", std::make_unique<ListValue>()); |
danakj | 0c8d4aa | 2015-11-25 05:29:58 | [diff] [blame] | 1451 | root->Set("dict_with_empty_children", std::move(inner2)); |
estade | 6e04d50 | 2015-05-23 02:42:09 | [diff] [blame] | 1452 | root = root->DeepCopyWithoutEmptyChildren(); |
viettrungluu@chromium.org | 9e4cda733 | 2010-07-31 04:56:14 | [diff] [blame] | 1453 | EXPECT_EQ(2U, root->size()); |
| 1454 | } |
| 1455 | |
| 1456 | // Make sure nested values don't get pruned. |
| 1457 | { |
Jeremy Roman | 9532f25 | 2017-08-16 23:27:24 | [diff] [blame] | 1458 | 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>()); |
danakj | 0c8d4aa | 2015-11-25 05:29:58 | [diff] [blame] | 1462 | inner->Append(std::move(inner2)); |
| 1463 | root->Set("list_with_empty_children", std::move(inner)); |
estade | 6e04d50 | 2015-05-23 02:42:09 | [diff] [blame] | 1464 | root = root->DeepCopyWithoutEmptyChildren(); |
viettrungluu@chromium.org | 9e4cda733 | 2010-07-31 04:56:14 | [diff] [blame] | 1465 | EXPECT_EQ(3U, root->size()); |
estade | 7bc801fb | 2015-05-07 01:53:08 | [diff] [blame] | 1466 | |
| 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.org | 9e4cda733 | 2010-07-31 04:56:14 | [diff] [blame] | 1472 | } |
| 1473 | } |
| 1474 | |
tfarina@chromium.org | 58b916e | 2011-06-28 22:56:33 | [diff] [blame] | 1475 | TEST(ValuesTest, MergeDictionary) { |
dcheng | 093de9b | 2016-04-04 21:25:51 | [diff] [blame] | 1476 | std::unique_ptr<DictionaryValue> base(new DictionaryValue); |
viettrungluu@chromium.org | 9e4cda733 | 2010-07-31 04:56:14 | [diff] [blame] | 1477 | base->SetString("base_key", "base_key_value_base"); |
| 1478 | base->SetString("collide_key", "collide_key_value_base"); |
dcheng | 093de9b | 2016-04-04 21:25:51 | [diff] [blame] | 1479 | std::unique_ptr<DictionaryValue> base_sub_dict(new DictionaryValue); |
viettrungluu@chromium.org | 9e4cda733 | 2010-07-31 04:56:14 | [diff] [blame] | 1480 | 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"); |
danakj | 0c8d4aa | 2015-11-25 05:29:58 | [diff] [blame] | 1482 | base->Set("sub_dict_key", std::move(base_sub_dict)); |
viettrungluu@chromium.org | 9e4cda733 | 2010-07-31 04:56:14 | [diff] [blame] | 1483 | |
dcheng | 093de9b | 2016-04-04 21:25:51 | [diff] [blame] | 1484 | std::unique_ptr<DictionaryValue> merge(new DictionaryValue); |
viettrungluu@chromium.org | 9e4cda733 | 2010-07-31 04:56:14 | [diff] [blame] | 1485 | merge->SetString("merge_key", "merge_key_value_merge"); |
| 1486 | merge->SetString("collide_key", "collide_key_value_merge"); |
dcheng | 093de9b | 2016-04-04 21:25:51 | [diff] [blame] | 1487 | std::unique_ptr<DictionaryValue> merge_sub_dict(new DictionaryValue); |
viettrungluu@chromium.org | 9e4cda733 | 2010-07-31 04:56:14 | [diff] [blame] | 1488 | 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"); |
danakj | 0c8d4aa | 2015-11-25 05:29:58 | [diff] [blame] | 1490 | merge->Set("sub_dict_key", std::move(merge_sub_dict)); |
viettrungluu@chromium.org | 9e4cda733 | 2010-07-31 04:56:14 | [diff] [blame] | 1491 | |
| 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.org | 58b916e | 2011-06-28 22:56:33 | [diff] [blame] | 1519 | |
jhawkins@chromium.org | 1350256 | 2012-05-09 21:54:27 | [diff] [blame] | 1520 | TEST(ValuesTest, MergeDictionaryDeepCopy) { |
dcheng | 093de9b | 2016-04-04 21:25:51 | [diff] [blame] | 1521 | std::unique_ptr<DictionaryValue> child(new DictionaryValue); |
danakj | 8dba5a5 | 2015-05-11 21:24:09 | [diff] [blame] | 1522 | DictionaryValue* original_child = child.get(); |
jhawkins@chromium.org | 1350256 | 2012-05-09 21:54:27 | [diff] [blame] | 1523 | 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 | |
dcheng | 093de9b | 2016-04-04 21:25:51 | [diff] [blame] | 1530 | std::unique_ptr<DictionaryValue> base(new DictionaryValue); |
danakj | 0c8d4aa | 2015-11-25 05:29:58 | [diff] [blame] | 1531 | base->Set("dict", std::move(child)); |
jhawkins@chromium.org | 1350256 | 2012-05-09 21:54:27 | [diff] [blame] | 1532 | EXPECT_EQ(1U, base->size()); |
| 1533 | |
| 1534 | DictionaryValue* ptr; |
| 1535 | EXPECT_TRUE(base->GetDictionary("dict", &ptr)); |
danakj | 8dba5a5 | 2015-05-11 21:24:09 | [diff] [blame] | 1536 | EXPECT_EQ(original_child, ptr); |
jhawkins@chromium.org | 1350256 | 2012-05-09 21:54:27 | [diff] [blame] | 1537 | |
dcheng | 093de9b | 2016-04-04 21:25:51 | [diff] [blame] | 1538 | std::unique_ptr<DictionaryValue> merged(new DictionaryValue); |
jhawkins@chromium.org | 1350256 | 2012-05-09 21:54:27 | [diff] [blame] | 1539 | merged->MergeDictionary(base.get()); |
| 1540 | EXPECT_EQ(1U, merged->size()); |
| 1541 | EXPECT_TRUE(merged->GetDictionary("dict", &ptr)); |
danakj | 8dba5a5 | 2015-05-11 21:24:09 | [diff] [blame] | 1542 | EXPECT_NE(original_child, ptr); |
jhawkins@chromium.org | 1350256 | 2012-05-09 21:54:27 | [diff] [blame] | 1543 | EXPECT_TRUE(ptr->GetString("test", &value)); |
| 1544 | EXPECT_EQ("value", value); |
| 1545 | |
danakj | 8dba5a5 | 2015-05-11 21:24:09 | [diff] [blame] | 1546 | original_child->SetString("test", "overwrite"); |
jhawkins@chromium.org | 1350256 | 2012-05-09 21:54:27 | [diff] [blame] | 1547 | base.reset(); |
| 1548 | EXPECT_TRUE(ptr->GetString("test", &value)); |
| 1549 | EXPECT_EQ("value", value); |
| 1550 | } |
| 1551 | |
kalman@chromium.org | 32c0e00 | 2011-11-08 21:26:41 | [diff] [blame] | 1552 | TEST(ValuesTest, DictionaryIterator) { |
| 1553 | DictionaryValue dict; |
pneubeck@chromium.org | ee6ba60f | 2013-04-12 09:25:23 | [diff] [blame] | 1554 | for (DictionaryValue::Iterator it(dict); !it.IsAtEnd(); it.Advance()) { |
kalman@chromium.org | 32c0e00 | 2011-11-08 21:26:41 | [diff] [blame] | 1555 | ADD_FAILURE(); |
| 1556 | } |
| 1557 | |
jdoerrie | 122c4da | 2017-03-06 11:12:04 | [diff] [blame] | 1558 | Value value1("value1"); |
jdoerrie | cc9f573 | 2017-08-23 14:12:30 | [diff] [blame] | 1559 | dict.SetKey("key1", value1.Clone()); |
kalman@chromium.org | 32c0e00 | 2011-11-08 21:26:41 | [diff] [blame] | 1560 | bool seen1 = false; |
pneubeck@chromium.org | ee6ba60f | 2013-04-12 09:25:23 | [diff] [blame] | 1561 | for (DictionaryValue::Iterator it(dict); !it.IsAtEnd(); it.Advance()) { |
kalman@chromium.org | 32c0e00 | 2011-11-08 21:26:41 | [diff] [blame] | 1562 | EXPECT_FALSE(seen1); |
| 1563 | EXPECT_EQ("key1", it.key()); |
jdoerrie | 5c1cee11 | 2017-03-28 17:52:00 | [diff] [blame] | 1564 | EXPECT_EQ(value1, it.value()); |
kalman@chromium.org | 32c0e00 | 2011-11-08 21:26:41 | [diff] [blame] | 1565 | seen1 = true; |
| 1566 | } |
| 1567 | EXPECT_TRUE(seen1); |
| 1568 | |
jdoerrie | 122c4da | 2017-03-06 11:12:04 | [diff] [blame] | 1569 | Value value2("value2"); |
jdoerrie | cc9f573 | 2017-08-23 14:12:30 | [diff] [blame] | 1570 | dict.SetKey("key2", value2.Clone()); |
kalman@chromium.org | 32c0e00 | 2011-11-08 21:26:41 | [diff] [blame] | 1571 | bool seen2 = seen1 = false; |
pneubeck@chromium.org | ee6ba60f | 2013-04-12 09:25:23 | [diff] [blame] | 1572 | for (DictionaryValue::Iterator it(dict); !it.IsAtEnd(); it.Advance()) { |
kalman@chromium.org | 32c0e00 | 2011-11-08 21:26:41 | [diff] [blame] | 1573 | if (it.key() == "key1") { |
| 1574 | EXPECT_FALSE(seen1); |
jdoerrie | 5c1cee11 | 2017-03-28 17:52:00 | [diff] [blame] | 1575 | EXPECT_EQ(value1, it.value()); |
kalman@chromium.org | 32c0e00 | 2011-11-08 21:26:41 | [diff] [blame] | 1576 | seen1 = true; |
| 1577 | } else if (it.key() == "key2") { |
| 1578 | EXPECT_FALSE(seen2); |
jdoerrie | 5c1cee11 | 2017-03-28 17:52:00 | [diff] [blame] | 1579 | EXPECT_EQ(value2, it.value()); |
kalman@chromium.org | 32c0e00 | 2011-11-08 21:26:41 | [diff] [blame] | 1580 | seen2 = true; |
| 1581 | } else { |
| 1582 | ADD_FAILURE(); |
| 1583 | } |
| 1584 | } |
| 1585 | EXPECT_TRUE(seen1); |
| 1586 | EXPECT_TRUE(seen2); |
| 1587 | } |
| 1588 | |
Johan Tibell | 71bba86c | 2017-05-17 05:21:12 | [diff] [blame] | 1589 | TEST(ValuesTest, StdDictionaryIterator) { |
| 1590 | DictionaryValue dict; |
| 1591 | for (auto it = dict.begin(); it != dict.end(); ++it) { |
| 1592 | ADD_FAILURE(); |
| 1593 | } |
| 1594 | |
| 1595 | Value value1("value1"); |
jdoerrie | cc9f573 | 2017-08-23 14:12:30 | [diff] [blame] | 1596 | dict.SetKey("key1", value1.Clone()); |
Johan Tibell | 71bba86c | 2017-05-17 05:21:12 | [diff] [blame] | 1597 | 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"); |
jdoerrie | cc9f573 | 2017-08-23 14:12:30 | [diff] [blame] | 1607 | dict.SetKey("key2", value2.Clone()); |
Johan Tibell | 71bba86c | 2017-05-17 05:21:12 | [diff] [blame] | 1608 | 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.org | 78c03a4 | 2014-03-09 07:13:23 | [diff] [blame] | 1626 | // DictionaryValue/ListValue's Get*() methods should accept NULL as an out-value |
| 1627 | // and still return true/false based on success. |
| 1628 | TEST(ValuesTest, GetWithNullOutValue) { |
| 1629 | DictionaryValue main_dict; |
| 1630 | ListValue main_list; |
| 1631 | |
jdoerrie | 23972357 | 2017-03-02 12:09:19 | [diff] [blame] | 1632 | Value bool_value(false); |
| 1633 | Value int_value(1234); |
| 1634 | Value double_value(12.34567); |
jdoerrie | 122c4da | 2017-03-06 11:12:04 | [diff] [blame] | 1635 | Value string_value("foo"); |
jdoerrie | 14b25da | 2017-04-11 07:45:50 | [diff] [blame] | 1636 | Value binary_value(Value::Type::BINARY); |
gab@chromium.org | 78c03a4 | 2014-03-09 07:13:23 | [diff] [blame] | 1637 | DictionaryValue dict_value; |
| 1638 | ListValue list_value; |
| 1639 | |
jdoerrie | cc9f573 | 2017-08-23 14:12:30 | [diff] [blame] | 1640 | 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.org | 78c03a4 | 2014-03-09 07:13:23 | [diff] [blame] | 1647 | |
jdoerrie | cc9f573 | 2017-08-23 14:12:30 | [diff] [blame] | 1648 | 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.org | 78c03a4 | 2014-03-09 07:13:23 | [diff] [blame] | 1655 | |
| 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 | |
dyaroshev | ed2ec7d0 | 2017-04-06 12:19:04 | [diff] [blame] | 1911 | TEST(ValuesTest, SelfSwap) { |
| 1912 | base::Value test(1); |
mkwst | cd8067b | 2017-04-11 06:52:21 | [diff] [blame] | 1913 | std::swap(test, test); |
Lei Zhang | 30895d5 | 2017-10-23 19:14:46 | [diff] [blame] | 1914 | EXPECT_EQ(1, test.GetInt()); |
| 1915 | } |
| 1916 | |
| 1917 | TEST(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); |
dyaroshev | ed2ec7d0 | 2017-04-06 12:19:04 | [diff] [blame] | 1929 | } |
| 1930 | |
tfarina@chromium.org | 58b916e | 2011-06-28 22:56:33 | [diff] [blame] | 1931 | } // namespace base |