license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 1 | // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. |
| 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 | |
[email protected] | 836061b | 2008-08-13 14:57:51 | [diff] [blame] | 5 | #include <limits> |
| 6 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 7 | #include "base/values.h" |
[email protected] | f82fb495 | 2009-01-20 21:05:32 | [diff] [blame] | 8 | #include "base/scoped_ptr.h" |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 9 | #include "testing/gtest/include/gtest/gtest.h" |
| 10 | |
| 11 | class ValuesTest: public testing::Test { |
| 12 | }; |
| 13 | |
| 14 | TEST(ValuesTest, Basic) { |
| 15 | // Test basic dictionary getting/setting |
| 16 | DictionaryValue settings; |
[email protected] | 8e50b60 | 2009-03-03 22:59:43 | [diff] [blame] | 17 | std::wstring homepage = L"http://google.com"; |
| 18 | ASSERT_FALSE( |
| 19 | settings.GetString(L"global.homepage", &homepage)); |
| 20 | ASSERT_EQ(std::wstring(L"http://google.com"), homepage); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 21 | |
[email protected] | 8e50b60 | 2009-03-03 22:59:43 | [diff] [blame] | 22 | ASSERT_FALSE(settings.Get(L"global", NULL)); |
| 23 | ASSERT_TRUE(settings.Set(L"global", Value::CreateBooleanValue(true))); |
| 24 | ASSERT_TRUE(settings.Get(L"global", NULL)); |
| 25 | ASSERT_TRUE(settings.SetString(L"global.homepage", L"http://scurvy.com")); |
| 26 | ASSERT_TRUE(settings.Get(L"global", NULL)); |
| 27 | homepage = L"http://google.com"; |
| 28 | ASSERT_TRUE(settings.GetString(L"global.homepage", &homepage)); |
| 29 | ASSERT_EQ(std::wstring(L"http://scurvy.com"), homepage); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 30 | |
| 31 | // Test storing a dictionary in a list. |
| 32 | ListValue* toolbar_bookmarks; |
| 33 | ASSERT_FALSE( |
[email protected] | 8e50b60 | 2009-03-03 22:59:43 | [diff] [blame] | 34 | settings.GetList(L"global.toolbar.bookmarks", &toolbar_bookmarks)); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 35 | |
| 36 | toolbar_bookmarks = new ListValue; |
[email protected] | 8e50b60 | 2009-03-03 22:59:43 | [diff] [blame] | 37 | settings.Set(L"global.toolbar.bookmarks", toolbar_bookmarks); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 38 | ASSERT_TRUE( |
[email protected] | 8e50b60 | 2009-03-03 22:59:43 | [diff] [blame] | 39 | settings.GetList(L"global.toolbar.bookmarks", &toolbar_bookmarks)); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 40 | |
| 41 | DictionaryValue* new_bookmark = new DictionaryValue; |
[email protected] | 8e50b60 | 2009-03-03 22:59:43 | [diff] [blame] | 42 | new_bookmark->SetString(L"name", L"Froogle"); |
| 43 | new_bookmark->SetString(L"url", L"http://froogle.com"); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 44 | toolbar_bookmarks->Append(new_bookmark); |
| 45 | |
| 46 | ListValue* bookmark_list; |
[email protected] | 8e50b60 | 2009-03-03 22:59:43 | [diff] [blame] | 47 | ASSERT_TRUE(settings.GetList(L"global.toolbar.bookmarks", &bookmark_list)); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 48 | DictionaryValue* bookmark; |
[email protected] | f297d18 | 2008-08-21 16:24:51 | [diff] [blame] | 49 | ASSERT_EQ(1U, bookmark_list->GetSize()); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 50 | ASSERT_TRUE(bookmark_list->GetDictionary(0, &bookmark)); |
[email protected] | 8e50b60 | 2009-03-03 22:59:43 | [diff] [blame] | 51 | std::wstring bookmark_name = L"Unnamed"; |
| 52 | ASSERT_TRUE(bookmark->GetString(L"name", &bookmark_name)); |
| 53 | ASSERT_EQ(std::wstring(L"Froogle"), bookmark_name); |
| 54 | std::wstring bookmark_url; |
| 55 | ASSERT_TRUE(bookmark->GetString(L"url", &bookmark_url)); |
| 56 | ASSERT_EQ(std::wstring(L"http://froogle.com"), bookmark_url); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 57 | } |
| 58 | |
[email protected] | f82fb495 | 2009-01-20 21:05:32 | [diff] [blame] | 59 | TEST(ValuesTest, List) { |
| 60 | scoped_ptr<ListValue> mixed_list(new ListValue()); |
| 61 | mixed_list->Set(0, Value::CreateBooleanValue(true)); |
| 62 | mixed_list->Set(1, Value::CreateIntegerValue(42)); |
| 63 | mixed_list->Set(2, Value::CreateRealValue(88.8)); |
| 64 | mixed_list->Set(3, Value::CreateStringValue("foo")); |
| 65 | ASSERT_EQ(4u, mixed_list->GetSize()); |
[email protected] | 52a261f | 2009-03-03 15:01:12 | [diff] [blame] | 66 | |
[email protected] | f82fb495 | 2009-01-20 21:05:32 | [diff] [blame] | 67 | Value *value = NULL; |
| 68 | bool bool_value = false; |
| 69 | int int_value = 0; |
| 70 | double double_value = 0.0; |
| 71 | std::string string_value; |
| 72 | |
| 73 | ASSERT_FALSE(mixed_list->Get(4, &value)); |
| 74 | |
| 75 | ASSERT_FALSE(mixed_list->GetInteger(0, &int_value)); |
| 76 | ASSERT_EQ(0, int_value); |
| 77 | ASSERT_FALSE(mixed_list->GetReal(1, &double_value)); |
| 78 | ASSERT_EQ(0.0, double_value); |
| 79 | ASSERT_FALSE(mixed_list->GetString(2, &string_value)); |
| 80 | ASSERT_EQ("", string_value); |
| 81 | ASSERT_FALSE(mixed_list->GetBoolean(3, &bool_value)); |
| 82 | ASSERT_EQ(false, bool_value); |
| 83 | |
| 84 | ASSERT_TRUE(mixed_list->GetBoolean(0, &bool_value)); |
| 85 | ASSERT_EQ(true, bool_value); |
| 86 | ASSERT_TRUE(mixed_list->GetInteger(1, &int_value)); |
| 87 | ASSERT_EQ(42, int_value); |
| 88 | ASSERT_TRUE(mixed_list->GetReal(2, &double_value)); |
| 89 | ASSERT_EQ(88.8, double_value); |
| 90 | ASSERT_TRUE(mixed_list->GetString(3, &string_value)); |
| 91 | ASSERT_EQ("foo", string_value); |
| 92 | } |
| 93 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 94 | TEST(ValuesTest, BinaryValue) { |
| 95 | char* buffer = NULL; |
| 96 | // Passing a null buffer pointer doesn't yield a BinaryValue |
[email protected] | 0a9a0fc | 2009-03-24 23:37:14 | [diff] [blame] | 97 | scoped_ptr<BinaryValue> binary(BinaryValue::Create(buffer, 0)); |
| 98 | ASSERT_FALSE(binary.get()); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 99 | |
| 100 | // If you want to represent an empty binary value, use a zero-length buffer. |
[email protected] | cd67923 | 2008-08-13 02:39:51 | [diff] [blame] | 101 | buffer = new char[1]; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 102 | ASSERT_TRUE(buffer); |
[email protected] | 0a9a0fc | 2009-03-24 23:37:14 | [diff] [blame] | 103 | binary.reset(BinaryValue::Create(buffer, 0)); |
| 104 | ASSERT_TRUE(binary.get()); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 105 | ASSERT_TRUE(binary->GetBuffer()); |
| 106 | ASSERT_EQ(buffer, binary->GetBuffer()); |
[email protected] | f297d18 | 2008-08-21 16:24:51 | [diff] [blame] | 107 | ASSERT_EQ(0U, binary->GetSize()); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 108 | |
| 109 | // Test the common case of a non-empty buffer |
| 110 | buffer = new char[15]; |
[email protected] | 0a9a0fc | 2009-03-24 23:37:14 | [diff] [blame] | 111 | binary.reset(BinaryValue::Create(buffer, 15)); |
| 112 | ASSERT_TRUE(binary.get()); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 113 | ASSERT_TRUE(binary->GetBuffer()); |
| 114 | ASSERT_EQ(buffer, binary->GetBuffer()); |
[email protected] | f297d18 | 2008-08-21 16:24:51 | [diff] [blame] | 115 | ASSERT_EQ(15U, binary->GetSize()); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 116 | |
| 117 | char stack_buffer[42]; |
| 118 | memset(stack_buffer, '!', 42); |
[email protected] | 0a9a0fc | 2009-03-24 23:37:14 | [diff] [blame] | 119 | binary.reset(BinaryValue::CreateWithCopiedBuffer(stack_buffer, 42)); |
| 120 | ASSERT_TRUE(binary.get()); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 121 | ASSERT_TRUE(binary->GetBuffer()); |
| 122 | ASSERT_NE(stack_buffer, binary->GetBuffer()); |
[email protected] | f297d18 | 2008-08-21 16:24:51 | [diff] [blame] | 123 | ASSERT_EQ(42U, binary->GetSize()); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 124 | ASSERT_EQ(0, memcmp(stack_buffer, binary->GetBuffer(), binary->GetSize())); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 125 | } |
| 126 | |
[email protected] | 4cd5f6a | 2008-12-11 01:23:17 | [diff] [blame] | 127 | TEST(ValuesTest, StringValue) { |
| 128 | // Test overloaded CreateStringValue. |
[email protected] | 0a9a0fc | 2009-03-24 23:37:14 | [diff] [blame] | 129 | scoped_ptr<Value> narrow_value(Value::CreateStringValue("narrow")); |
| 130 | ASSERT_TRUE(narrow_value.get()); |
[email protected] | 4cd5f6a | 2008-12-11 01:23:17 | [diff] [blame] | 131 | ASSERT_TRUE(narrow_value->IsType(Value::TYPE_STRING)); |
[email protected] | 0a9a0fc | 2009-03-24 23:37:14 | [diff] [blame] | 132 | scoped_ptr<Value> wide_value(Value::CreateStringValue(L"wide")); |
| 133 | ASSERT_TRUE(wide_value.get()); |
[email protected] | 4cd5f6a | 2008-12-11 01:23:17 | [diff] [blame] | 134 | ASSERT_TRUE(wide_value->IsType(Value::TYPE_STRING)); |
| 135 | |
| 136 | // Test overloaded GetString. |
| 137 | std::string narrow = "http://google.com"; |
| 138 | std::wstring wide = L"http://google.com"; |
| 139 | ASSERT_TRUE(narrow_value->GetAsString(&narrow)); |
| 140 | ASSERT_TRUE(narrow_value->GetAsString(&wide)); |
| 141 | ASSERT_EQ(std::string("narrow"), narrow); |
| 142 | ASSERT_EQ(std::wstring(L"narrow"), wide); |
| 143 | ASSERT_TRUE(wide_value->GetAsString(&narrow)); |
| 144 | ASSERT_TRUE(wide_value->GetAsString(&wide)); |
| 145 | ASSERT_EQ(std::string("wide"), narrow); |
| 146 | ASSERT_EQ(std::wstring(L"wide"), wide); |
[email protected] | 4cd5f6a | 2008-12-11 01:23:17 | [diff] [blame] | 147 | } |
| 148 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 149 | // This is a Value object that allows us to tell if it's been |
| 150 | // properly deleted by modifying the value of external flag on destruction. |
| 151 | class DeletionTestValue : public Value { |
| 152 | public: |
| 153 | DeletionTestValue(bool* deletion_flag) : Value(TYPE_NULL) { |
| 154 | Init(deletion_flag); // Separate function so that we can use ASSERT_* |
| 155 | } |
| 156 | |
| 157 | void Init(bool* deletion_flag) { |
| 158 | ASSERT_TRUE(deletion_flag); |
| 159 | deletion_flag_ = deletion_flag; |
| 160 | *deletion_flag_ = false; |
| 161 | } |
| 162 | |
| 163 | ~DeletionTestValue() { |
| 164 | *deletion_flag_ = true; |
| 165 | } |
| 166 | |
| 167 | private: |
| 168 | bool* deletion_flag_; |
| 169 | }; |
| 170 | |
| 171 | TEST(ValuesTest, ListDeletion) { |
| 172 | bool deletion_flag = true; |
| 173 | |
| 174 | { |
| 175 | ListValue list; |
| 176 | list.Append(new DeletionTestValue(&deletion_flag)); |
| 177 | EXPECT_FALSE(deletion_flag); |
| 178 | } |
| 179 | EXPECT_TRUE(deletion_flag); |
| 180 | |
| 181 | { |
| 182 | ListValue list; |
| 183 | list.Append(new DeletionTestValue(&deletion_flag)); |
| 184 | EXPECT_FALSE(deletion_flag); |
| 185 | list.Clear(); |
| 186 | EXPECT_TRUE(deletion_flag); |
| 187 | } |
| 188 | |
| 189 | { |
| 190 | ListValue list; |
| 191 | list.Append(new DeletionTestValue(&deletion_flag)); |
| 192 | EXPECT_FALSE(deletion_flag); |
| 193 | EXPECT_TRUE(list.Set(0, Value::CreateNullValue())); |
| 194 | EXPECT_TRUE(deletion_flag); |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | TEST(ValuesTest, ListRemoval) { |
| 199 | bool deletion_flag = true; |
| 200 | Value* removed_item = NULL; |
| 201 | |
| 202 | { |
| 203 | ListValue list; |
| 204 | list.Append(new DeletionTestValue(&deletion_flag)); |
| 205 | EXPECT_FALSE(deletion_flag); |
[email protected] | f297d18 | 2008-08-21 16:24:51 | [diff] [blame] | 206 | EXPECT_EQ(1U, list.GetSize()); |
[email protected] | 836061b | 2008-08-13 14:57:51 | [diff] [blame] | 207 | EXPECT_FALSE(list.Remove(std::numeric_limits<size_t>::max(), |
| 208 | &removed_item)); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 209 | EXPECT_FALSE(list.Remove(1, &removed_item)); |
| 210 | EXPECT_TRUE(list.Remove(0, &removed_item)); |
| 211 | ASSERT_TRUE(removed_item); |
[email protected] | f297d18 | 2008-08-21 16:24:51 | [diff] [blame] | 212 | EXPECT_EQ(0U, list.GetSize()); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 213 | } |
| 214 | EXPECT_FALSE(deletion_flag); |
| 215 | delete removed_item; |
| 216 | removed_item = NULL; |
| 217 | EXPECT_TRUE(deletion_flag); |
| 218 | |
| 219 | { |
| 220 | ListValue list; |
| 221 | list.Append(new DeletionTestValue(&deletion_flag)); |
| 222 | EXPECT_FALSE(deletion_flag); |
| 223 | EXPECT_TRUE(list.Remove(0, NULL)); |
| 224 | EXPECT_TRUE(deletion_flag); |
[email protected] | f297d18 | 2008-08-21 16:24:51 | [diff] [blame] | 225 | EXPECT_EQ(0U, list.GetSize()); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 226 | } |
| 227 | } |
| 228 | |
| 229 | TEST(ValuesTest, DictionaryDeletion) { |
[email protected] | 8e50b60 | 2009-03-03 22:59:43 | [diff] [blame] | 230 | std::wstring key = L"test"; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 231 | bool deletion_flag = true; |
| 232 | |
| 233 | { |
| 234 | DictionaryValue dict; |
| 235 | dict.Set(key, new DeletionTestValue(&deletion_flag)); |
| 236 | EXPECT_FALSE(deletion_flag); |
| 237 | } |
| 238 | EXPECT_TRUE(deletion_flag); |
| 239 | |
| 240 | { |
| 241 | DictionaryValue dict; |
| 242 | dict.Set(key, new DeletionTestValue(&deletion_flag)); |
| 243 | EXPECT_FALSE(deletion_flag); |
| 244 | dict.Clear(); |
| 245 | EXPECT_TRUE(deletion_flag); |
| 246 | } |
| 247 | |
| 248 | { |
| 249 | DictionaryValue dict; |
| 250 | dict.Set(key, new DeletionTestValue(&deletion_flag)); |
| 251 | EXPECT_FALSE(deletion_flag); |
| 252 | dict.Set(key, Value::CreateNullValue()); |
| 253 | EXPECT_TRUE(deletion_flag); |
| 254 | } |
| 255 | } |
| 256 | |
| 257 | TEST(ValuesTest, DictionaryRemoval) { |
[email protected] | 8e50b60 | 2009-03-03 22:59:43 | [diff] [blame] | 258 | std::wstring key = L"test"; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 259 | bool deletion_flag = true; |
| 260 | Value* removed_item = NULL; |
| 261 | |
| 262 | { |
| 263 | DictionaryValue dict; |
| 264 | dict.Set(key, new DeletionTestValue(&deletion_flag)); |
| 265 | EXPECT_FALSE(deletion_flag); |
| 266 | EXPECT_TRUE(dict.HasKey(key)); |
[email protected] | 8e50b60 | 2009-03-03 22:59:43 | [diff] [blame] | 267 | EXPECT_FALSE(dict.Remove(L"absent key", &removed_item)); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 268 | EXPECT_TRUE(dict.Remove(key, &removed_item)); |
| 269 | EXPECT_FALSE(dict.HasKey(key)); |
| 270 | ASSERT_TRUE(removed_item); |
| 271 | } |
| 272 | EXPECT_FALSE(deletion_flag); |
| 273 | delete removed_item; |
| 274 | removed_item = NULL; |
| 275 | EXPECT_TRUE(deletion_flag); |
| 276 | |
| 277 | { |
| 278 | DictionaryValue dict; |
| 279 | dict.Set(key, new DeletionTestValue(&deletion_flag)); |
| 280 | EXPECT_FALSE(deletion_flag); |
| 281 | EXPECT_TRUE(dict.HasKey(key)); |
| 282 | EXPECT_TRUE(dict.Remove(key, NULL)); |
| 283 | EXPECT_TRUE(deletion_flag); |
| 284 | EXPECT_FALSE(dict.HasKey(key)); |
| 285 | } |
| 286 | } |
| 287 | |
| 288 | TEST(ValuesTest, DeepCopy) { |
| 289 | DictionaryValue original_dict; |
| 290 | Value* original_null = Value::CreateNullValue(); |
[email protected] | 8e50b60 | 2009-03-03 22:59:43 | [diff] [blame] | 291 | original_dict.Set(L"null", original_null); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 292 | Value* original_bool = Value::CreateBooleanValue(true); |
[email protected] | 8e50b60 | 2009-03-03 22:59:43 | [diff] [blame] | 293 | original_dict.Set(L"bool", original_bool); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 294 | Value* original_int = Value::CreateIntegerValue(42); |
[email protected] | 8e50b60 | 2009-03-03 22:59:43 | [diff] [blame] | 295 | original_dict.Set(L"int", original_int); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 296 | Value* original_real = Value::CreateRealValue(3.14); |
[email protected] | 8e50b60 | 2009-03-03 22:59:43 | [diff] [blame] | 297 | original_dict.Set(L"real", original_real); |
[email protected] | 4cd5f6a | 2008-12-11 01:23:17 | [diff] [blame] | 298 | Value* original_string = Value::CreateStringValue("hello"); |
[email protected] | 8e50b60 | 2009-03-03 22:59:43 | [diff] [blame] | 299 | original_dict.Set(L"string", original_string); |
[email protected] | 4cd5f6a | 2008-12-11 01:23:17 | [diff] [blame] | 300 | Value* original_wstring = Value::CreateStringValue(L"peek-a-boo"); |
[email protected] | 8e50b60 | 2009-03-03 22:59:43 | [diff] [blame] | 301 | original_dict.Set(L"wstring", original_wstring); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 302 | |
| 303 | char* original_buffer = new char[42]; |
| 304 | memset(original_buffer, '!', 42); |
| 305 | BinaryValue* original_binary = Value::CreateBinaryValue(original_buffer, 42); |
[email protected] | 8e50b60 | 2009-03-03 22:59:43 | [diff] [blame] | 306 | original_dict.Set(L"binary", original_binary); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 307 | |
| 308 | ListValue* original_list = new ListValue(); |
| 309 | Value* original_list_element_0 = Value::CreateIntegerValue(0); |
| 310 | original_list->Append(original_list_element_0); |
| 311 | Value* original_list_element_1 = Value::CreateIntegerValue(1); |
| 312 | original_list->Append(original_list_element_1); |
[email protected] | 8e50b60 | 2009-03-03 22:59:43 | [diff] [blame] | 313 | original_dict.Set(L"list", original_list); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 314 | |
| 315 | DictionaryValue* copy_dict = |
| 316 | static_cast<DictionaryValue*>(original_dict.DeepCopy()); |
| 317 | ASSERT_TRUE(copy_dict); |
| 318 | ASSERT_NE(copy_dict, &original_dict); |
| 319 | |
| 320 | Value* copy_null = NULL; |
[email protected] | 8e50b60 | 2009-03-03 22:59:43 | [diff] [blame] | 321 | ASSERT_TRUE(copy_dict->Get(L"null", ©_null)); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 322 | ASSERT_TRUE(copy_null); |
| 323 | ASSERT_NE(copy_null, original_null); |
| 324 | ASSERT_TRUE(copy_null->IsType(Value::TYPE_NULL)); |
| 325 | |
| 326 | Value* copy_bool = NULL; |
[email protected] | 8e50b60 | 2009-03-03 22:59:43 | [diff] [blame] | 327 | ASSERT_TRUE(copy_dict->Get(L"bool", ©_bool)); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 328 | ASSERT_TRUE(copy_bool); |
| 329 | ASSERT_NE(copy_bool, original_bool); |
| 330 | ASSERT_TRUE(copy_bool->IsType(Value::TYPE_BOOLEAN)); |
| 331 | bool copy_bool_value = false; |
| 332 | ASSERT_TRUE(copy_bool->GetAsBoolean(©_bool_value)); |
| 333 | ASSERT_TRUE(copy_bool_value); |
| 334 | |
| 335 | Value* copy_int = NULL; |
[email protected] | 8e50b60 | 2009-03-03 22:59:43 | [diff] [blame] | 336 | ASSERT_TRUE(copy_dict->Get(L"int", ©_int)); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 337 | ASSERT_TRUE(copy_int); |
| 338 | ASSERT_NE(copy_int, original_int); |
| 339 | ASSERT_TRUE(copy_int->IsType(Value::TYPE_INTEGER)); |
| 340 | int copy_int_value = 0; |
| 341 | ASSERT_TRUE(copy_int->GetAsInteger(©_int_value)); |
| 342 | ASSERT_EQ(42, copy_int_value); |
| 343 | |
| 344 | Value* copy_real = NULL; |
[email protected] | 8e50b60 | 2009-03-03 22:59:43 | [diff] [blame] | 345 | ASSERT_TRUE(copy_dict->Get(L"real", ©_real)); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 346 | ASSERT_TRUE(copy_real); |
| 347 | ASSERT_NE(copy_real, original_real); |
| 348 | ASSERT_TRUE(copy_real->IsType(Value::TYPE_REAL)); |
| 349 | double copy_real_value = 0; |
| 350 | ASSERT_TRUE(copy_real->GetAsReal(©_real_value)); |
| 351 | ASSERT_EQ(3.14, copy_real_value); |
| 352 | |
| 353 | Value* copy_string = NULL; |
[email protected] | 8e50b60 | 2009-03-03 22:59:43 | [diff] [blame] | 354 | ASSERT_TRUE(copy_dict->Get(L"string", ©_string)); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 355 | ASSERT_TRUE(copy_string); |
| 356 | ASSERT_NE(copy_string, original_string); |
| 357 | ASSERT_TRUE(copy_string->IsType(Value::TYPE_STRING)); |
[email protected] | 4cd5f6a | 2008-12-11 01:23:17 | [diff] [blame] | 358 | std::string copy_string_value; |
| 359 | std::wstring copy_wstring_value; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 360 | ASSERT_TRUE(copy_string->GetAsString(©_string_value)); |
[email protected] | 4cd5f6a | 2008-12-11 01:23:17 | [diff] [blame] | 361 | ASSERT_TRUE(copy_string->GetAsString(©_wstring_value)); |
| 362 | ASSERT_EQ(std::string("hello"), copy_string_value); |
| 363 | ASSERT_EQ(std::wstring(L"hello"), copy_wstring_value); |
| 364 | |
| 365 | Value* copy_wstring = NULL; |
[email protected] | 8e50b60 | 2009-03-03 22:59:43 | [diff] [blame] | 366 | ASSERT_TRUE(copy_dict->Get(L"wstring", ©_wstring)); |
[email protected] | 4cd5f6a | 2008-12-11 01:23:17 | [diff] [blame] | 367 | ASSERT_TRUE(copy_wstring); |
| 368 | ASSERT_NE(copy_wstring, original_wstring); |
[email protected] | 52a261f | 2009-03-03 15:01:12 | [diff] [blame] | 369 | ASSERT_TRUE(copy_wstring->IsType(Value::TYPE_STRING)); |
[email protected] | 4cd5f6a | 2008-12-11 01:23:17 | [diff] [blame] | 370 | ASSERT_TRUE(copy_wstring->GetAsString(©_string_value)); |
| 371 | ASSERT_TRUE(copy_wstring->GetAsString(©_wstring_value)); |
| 372 | ASSERT_EQ(std::string("peek-a-boo"), copy_string_value); |
| 373 | ASSERT_EQ(std::wstring(L"peek-a-boo"), copy_wstring_value); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 374 | |
| 375 | Value* copy_binary = NULL; |
[email protected] | 8e50b60 | 2009-03-03 22:59:43 | [diff] [blame] | 376 | ASSERT_TRUE(copy_dict->Get(L"binary", ©_binary)); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 377 | ASSERT_TRUE(copy_binary); |
| 378 | ASSERT_NE(copy_binary, original_binary); |
| 379 | ASSERT_TRUE(copy_binary->IsType(Value::TYPE_BINARY)); |
| 380 | ASSERT_NE(original_binary->GetBuffer(), |
| 381 | static_cast<BinaryValue*>(copy_binary)->GetBuffer()); |
| 382 | ASSERT_EQ(original_binary->GetSize(), |
| 383 | static_cast<BinaryValue*>(copy_binary)->GetSize()); |
| 384 | ASSERT_EQ(0, memcmp(original_binary->GetBuffer(), |
| 385 | static_cast<BinaryValue*>(copy_binary)->GetBuffer(), |
| 386 | original_binary->GetSize())); |
| 387 | |
| 388 | Value* copy_value = NULL; |
[email protected] | 8e50b60 | 2009-03-03 22:59:43 | [diff] [blame] | 389 | ASSERT_TRUE(copy_dict->Get(L"list", ©_value)); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 390 | ASSERT_TRUE(copy_value); |
| 391 | ASSERT_NE(copy_value, original_list); |
| 392 | ASSERT_TRUE(copy_value->IsType(Value::TYPE_LIST)); |
| 393 | ListValue* copy_list = static_cast<ListValue*>(copy_value); |
[email protected] | f297d18 | 2008-08-21 16:24:51 | [diff] [blame] | 394 | ASSERT_EQ(2U, copy_list->GetSize()); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 395 | |
| 396 | Value* copy_list_element_0; |
| 397 | ASSERT_TRUE(copy_list->Get(0, ©_list_element_0)); |
| 398 | ASSERT_TRUE(copy_list_element_0); |
| 399 | ASSERT_NE(copy_list_element_0, original_list_element_0); |
| 400 | int copy_list_element_0_value; |
| 401 | ASSERT_TRUE(copy_list_element_0->GetAsInteger(©_list_element_0_value)); |
| 402 | ASSERT_EQ(0, copy_list_element_0_value); |
| 403 | |
| 404 | Value* copy_list_element_1; |
| 405 | ASSERT_TRUE(copy_list->Get(1, ©_list_element_1)); |
| 406 | ASSERT_TRUE(copy_list_element_1); |
| 407 | ASSERT_NE(copy_list_element_1, original_list_element_1); |
| 408 | int copy_list_element_1_value; |
| 409 | ASSERT_TRUE(copy_list_element_1->GetAsInteger(©_list_element_1_value)); |
| 410 | ASSERT_EQ(1, copy_list_element_1_value); |
| 411 | |
| 412 | delete copy_dict; |
| 413 | } |
| 414 | |
| 415 | TEST(ValuesTest, Equals) { |
| 416 | Value* null1 = Value::CreateNullValue(); |
| 417 | Value* null2 = Value::CreateNullValue(); |
| 418 | EXPECT_NE(null1, null2); |
| 419 | EXPECT_TRUE(null1->Equals(null2)); |
| 420 | |
| 421 | Value* boolean = Value::CreateBooleanValue(false); |
| 422 | EXPECT_FALSE(null1->Equals(boolean)); |
| 423 | delete null1; |
| 424 | delete null2; |
| 425 | delete boolean; |
| 426 | |
| 427 | DictionaryValue dv; |
[email protected] | 8e50b60 | 2009-03-03 22:59:43 | [diff] [blame] | 428 | dv.SetBoolean(L"a", false); |
| 429 | dv.SetInteger(L"b", 2); |
| 430 | dv.SetReal(L"c", 2.5); |
| 431 | dv.SetString(L"d1", "string"); |
| 432 | dv.SetString(L"d2", L"string"); |
| 433 | dv.Set(L"e", Value::CreateNullValue()); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 434 | |
| 435 | DictionaryValue* copy = static_cast<DictionaryValue*>(dv.DeepCopy()); |
| 436 | EXPECT_TRUE(dv.Equals(copy)); |
| 437 | |
| 438 | ListValue* list = new ListValue; |
| 439 | list->Append(Value::CreateNullValue()); |
| 440 | list->Append(new DictionaryValue); |
[email protected] | 8e50b60 | 2009-03-03 22:59:43 | [diff] [blame] | 441 | dv.Set(L"f", list); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 442 | |
| 443 | EXPECT_FALSE(dv.Equals(copy)); |
[email protected] | 8e50b60 | 2009-03-03 22:59:43 | [diff] [blame] | 444 | copy->Set(L"f", list->DeepCopy()); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 445 | EXPECT_TRUE(dv.Equals(copy)); |
| 446 | |
| 447 | list->Append(Value::CreateBooleanValue(true)); |
| 448 | EXPECT_FALSE(dv.Equals(copy)); |
| 449 | delete copy; |
| 450 | } |