| OLD | NEW |
| 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include <limits> | 5 #include <limits> |
| 6 | 6 |
| 7 #include "base/values.h" | 7 #include "base/values.h" |
| 8 #include "testing/gtest/include/gtest/gtest.h" | 8 #include "testing/gtest/include/gtest/gtest.h" |
| 9 | 9 |
| 10 class ValuesTest: public testing::Test { | 10 class ValuesTest: public testing::Test { |
| (...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 86 memset(stack_buffer, '!', 42); | 86 memset(stack_buffer, '!', 42); |
| 87 binary = BinaryValue::CreateWithCopiedBuffer(stack_buffer, 42); | 87 binary = BinaryValue::CreateWithCopiedBuffer(stack_buffer, 42); |
| 88 ASSERT_TRUE(binary); | 88 ASSERT_TRUE(binary); |
| 89 ASSERT_TRUE(binary->GetBuffer()); | 89 ASSERT_TRUE(binary->GetBuffer()); |
| 90 ASSERT_NE(stack_buffer, binary->GetBuffer()); | 90 ASSERT_NE(stack_buffer, binary->GetBuffer()); |
| 91 ASSERT_EQ(42U, binary->GetSize()); | 91 ASSERT_EQ(42U, binary->GetSize()); |
| 92 ASSERT_EQ(0, memcmp(stack_buffer, binary->GetBuffer(), binary->GetSize())); | 92 ASSERT_EQ(0, memcmp(stack_buffer, binary->GetBuffer(), binary->GetSize())); |
| 93 delete binary; | 93 delete binary; |
| 94 } | 94 } |
| 95 | 95 |
| 96 TEST(ValuesTest, StringValue) { | |
| 97 // Test overloaded CreateStringValue. | |
| 98 Value* narrow_value = Value::CreateStringValue("narrow"); | |
| 99 ASSERT_TRUE(narrow_value); | |
| 100 ASSERT_TRUE(narrow_value->IsType(Value::TYPE_STRING)); | |
| 101 Value* wide_value = Value::CreateStringValue(L"wide"); | |
| 102 ASSERT_TRUE(wide_value); | |
| 103 ASSERT_TRUE(wide_value->IsType(Value::TYPE_STRING)); | |
| 104 | |
| 105 // Test overloaded GetString. | |
| 106 std::string narrow = "http://google.com"; | |
| 107 std::wstring wide = L"http://google.com"; | |
| 108 ASSERT_TRUE(narrow_value->GetAsString(&narrow)); | |
| 109 ASSERT_TRUE(narrow_value->GetAsString(&wide)); | |
| 110 ASSERT_EQ(std::string("narrow"), narrow); | |
| 111 ASSERT_EQ(std::wstring(L"narrow"), wide); | |
| 112 ASSERT_TRUE(wide_value->GetAsString(&narrow)); | |
| 113 ASSERT_TRUE(wide_value->GetAsString(&wide)); | |
| 114 ASSERT_EQ(std::string("wide"), narrow); | |
| 115 ASSERT_EQ(std::wstring(L"wide"), wide); | |
| 116 delete narrow_value; | |
| 117 delete wide_value; | |
| 118 } | |
| 119 | |
| 120 // This is a Value object that allows us to tell if it's been | 96 // This is a Value object that allows us to tell if it's been |
| 121 // properly deleted by modifying the value of external flag on destruction. | 97 // properly deleted by modifying the value of external flag on destruction. |
| 122 class DeletionTestValue : public Value { | 98 class DeletionTestValue : public Value { |
| 123 public: | 99 public: |
| 124 DeletionTestValue(bool* deletion_flag) : Value(TYPE_NULL) { | 100 DeletionTestValue(bool* deletion_flag) : Value(TYPE_NULL) { |
| 125 Init(deletion_flag); // Separate function so that we can use ASSERT_* | 101 Init(deletion_flag); // Separate function so that we can use ASSERT_* |
| 126 } | 102 } |
| 127 | 103 |
| 128 void Init(bool* deletion_flag) { | 104 void Init(bool* deletion_flag) { |
| 129 ASSERT_TRUE(deletion_flag); | 105 ASSERT_TRUE(deletion_flag); |
| (...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 259 TEST(ValuesTest, DeepCopy) { | 235 TEST(ValuesTest, DeepCopy) { |
| 260 DictionaryValue original_dict; | 236 DictionaryValue original_dict; |
| 261 Value* original_null = Value::CreateNullValue(); | 237 Value* original_null = Value::CreateNullValue(); |
| 262 original_dict.Set(L"null", original_null); | 238 original_dict.Set(L"null", original_null); |
| 263 Value* original_bool = Value::CreateBooleanValue(true); | 239 Value* original_bool = Value::CreateBooleanValue(true); |
| 264 original_dict.Set(L"bool", original_bool); | 240 original_dict.Set(L"bool", original_bool); |
| 265 Value* original_int = Value::CreateIntegerValue(42); | 241 Value* original_int = Value::CreateIntegerValue(42); |
| 266 original_dict.Set(L"int", original_int); | 242 original_dict.Set(L"int", original_int); |
| 267 Value* original_real = Value::CreateRealValue(3.14); | 243 Value* original_real = Value::CreateRealValue(3.14); |
| 268 original_dict.Set(L"real", original_real); | 244 original_dict.Set(L"real", original_real); |
| 269 Value* original_string = Value::CreateStringValue("hello"); | 245 Value* original_string = Value::CreateStringValue(L"peek-a-boo"); |
| 270 original_dict.Set(L"string", original_string); | 246 original_dict.Set(L"string", original_string); |
| 271 Value* original_wstring = Value::CreateStringValue(L"peek-a-boo"); | |
| 272 original_dict.Set(L"wstring", original_wstring); | |
| 273 | 247 |
| 274 char* original_buffer = new char[42]; | 248 char* original_buffer = new char[42]; |
| 275 memset(original_buffer, '!', 42); | 249 memset(original_buffer, '!', 42); |
| 276 BinaryValue* original_binary = Value::CreateBinaryValue(original_buffer, 42); | 250 BinaryValue* original_binary = Value::CreateBinaryValue(original_buffer, 42); |
| 277 original_dict.Set(L"binary", original_binary); | 251 original_dict.Set(L"binary", original_binary); |
| 278 | 252 |
| 279 ListValue* original_list = new ListValue(); | 253 ListValue* original_list = new ListValue(); |
| 280 Value* original_list_element_0 = Value::CreateIntegerValue(0); | 254 Value* original_list_element_0 = Value::CreateIntegerValue(0); |
| 281 original_list->Append(original_list_element_0); | 255 original_list->Append(original_list_element_0); |
| 282 Value* original_list_element_1 = Value::CreateIntegerValue(1); | 256 Value* original_list_element_1 = Value::CreateIntegerValue(1); |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 319 ASSERT_TRUE(copy_real->IsType(Value::TYPE_REAL)); | 293 ASSERT_TRUE(copy_real->IsType(Value::TYPE_REAL)); |
| 320 double copy_real_value = 0; | 294 double copy_real_value = 0; |
| 321 ASSERT_TRUE(copy_real->GetAsReal(©_real_value)); | 295 ASSERT_TRUE(copy_real->GetAsReal(©_real_value)); |
| 322 ASSERT_EQ(3.14, copy_real_value); | 296 ASSERT_EQ(3.14, copy_real_value); |
| 323 | 297 |
| 324 Value* copy_string = NULL; | 298 Value* copy_string = NULL; |
| 325 ASSERT_TRUE(copy_dict->Get(L"string", ©_string)); | 299 ASSERT_TRUE(copy_dict->Get(L"string", ©_string)); |
| 326 ASSERT_TRUE(copy_string); | 300 ASSERT_TRUE(copy_string); |
| 327 ASSERT_NE(copy_string, original_string); | 301 ASSERT_NE(copy_string, original_string); |
| 328 ASSERT_TRUE(copy_string->IsType(Value::TYPE_STRING)); | 302 ASSERT_TRUE(copy_string->IsType(Value::TYPE_STRING)); |
| 329 std::string copy_string_value; | 303 std::wstring copy_string_value; |
| 330 std::wstring copy_wstring_value; | |
| 331 ASSERT_TRUE(copy_string->GetAsString(©_string_value)); | 304 ASSERT_TRUE(copy_string->GetAsString(©_string_value)); |
| 332 ASSERT_TRUE(copy_string->GetAsString(©_wstring_value)); | 305 ASSERT_EQ(std::wstring(L"peek-a-boo"), copy_string_value); |
| 333 ASSERT_EQ(std::string("hello"), copy_string_value); | |
| 334 ASSERT_EQ(std::wstring(L"hello"), copy_wstring_value); | |
| 335 | |
| 336 Value* copy_wstring = NULL; | |
| 337 ASSERT_TRUE(copy_dict->Get(L"wstring", ©_wstring)); | |
| 338 ASSERT_TRUE(copy_wstring); | |
| 339 ASSERT_NE(copy_wstring, original_wstring); | |
| 340 ASSERT_TRUE(copy_wstring->IsType(Value::TYPE_STRING)); | |
| 341 ASSERT_TRUE(copy_wstring->GetAsString(©_string_value)); | |
| 342 ASSERT_TRUE(copy_wstring->GetAsString(©_wstring_value)); | |
| 343 ASSERT_EQ(std::string("peek-a-boo"), copy_string_value); | |
| 344 ASSERT_EQ(std::wstring(L"peek-a-boo"), copy_wstring_value); | |
| 345 | 306 |
| 346 Value* copy_binary = NULL; | 307 Value* copy_binary = NULL; |
| 347 ASSERT_TRUE(copy_dict->Get(L"binary", ©_binary)); | 308 ASSERT_TRUE(copy_dict->Get(L"binary", ©_binary)); |
| 348 ASSERT_TRUE(copy_binary); | 309 ASSERT_TRUE(copy_binary); |
| 349 ASSERT_NE(copy_binary, original_binary); | 310 ASSERT_NE(copy_binary, original_binary); |
| 350 ASSERT_TRUE(copy_binary->IsType(Value::TYPE_BINARY)); | 311 ASSERT_TRUE(copy_binary->IsType(Value::TYPE_BINARY)); |
| 351 ASSERT_NE(original_binary->GetBuffer(), | 312 ASSERT_NE(original_binary->GetBuffer(), |
| 352 static_cast<BinaryValue*>(copy_binary)->GetBuffer()); | 313 static_cast<BinaryValue*>(copy_binary)->GetBuffer()); |
| 353 ASSERT_EQ(original_binary->GetSize(), | 314 ASSERT_EQ(original_binary->GetSize(), |
| 354 static_cast<BinaryValue*>(copy_binary)->GetSize()); | 315 static_cast<BinaryValue*>(copy_binary)->GetSize()); |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 392 Value* boolean = Value::CreateBooleanValue(false); | 353 Value* boolean = Value::CreateBooleanValue(false); |
| 393 EXPECT_FALSE(null1->Equals(boolean)); | 354 EXPECT_FALSE(null1->Equals(boolean)); |
| 394 delete null1; | 355 delete null1; |
| 395 delete null2; | 356 delete null2; |
| 396 delete boolean; | 357 delete boolean; |
| 397 | 358 |
| 398 DictionaryValue dv; | 359 DictionaryValue dv; |
| 399 dv.SetBoolean(L"a", false); | 360 dv.SetBoolean(L"a", false); |
| 400 dv.SetInteger(L"b", 2); | 361 dv.SetInteger(L"b", 2); |
| 401 dv.SetReal(L"c", 2.5); | 362 dv.SetReal(L"c", 2.5); |
| 402 dv.SetString(L"d1", "string"); | 363 dv.SetString(L"d", L"string"); |
| 403 dv.SetString(L"d2", L"string"); | |
| 404 dv.Set(L"e", Value::CreateNullValue()); | 364 dv.Set(L"e", Value::CreateNullValue()); |
| 405 | 365 |
| 406 DictionaryValue* copy = static_cast<DictionaryValue*>(dv.DeepCopy()); | 366 DictionaryValue* copy = static_cast<DictionaryValue*>(dv.DeepCopy()); |
| 407 EXPECT_TRUE(dv.Equals(copy)); | 367 EXPECT_TRUE(dv.Equals(copy)); |
| 408 | 368 |
| 409 ListValue* list = new ListValue; | 369 ListValue* list = new ListValue; |
| 410 list->Append(Value::CreateNullValue()); | 370 list->Append(Value::CreateNullValue()); |
| 411 list->Append(new DictionaryValue); | 371 list->Append(new DictionaryValue); |
| 412 dv.Set(L"f", list); | 372 dv.Set(L"f", list); |
| 413 | 373 |
| 414 EXPECT_FALSE(dv.Equals(copy)); | 374 EXPECT_FALSE(dv.Equals(copy)); |
| 415 copy->Set(L"f", list->DeepCopy()); | 375 copy->Set(L"f", list->DeepCopy()); |
| 416 EXPECT_TRUE(dv.Equals(copy)); | 376 EXPECT_TRUE(dv.Equals(copy)); |
| 417 | 377 |
| 418 list->Append(Value::CreateBooleanValue(true)); | 378 list->Append(Value::CreateBooleanValue(true)); |
| 419 EXPECT_FALSE(dv.Equals(copy)); | 379 EXPECT_FALSE(dv.Equals(copy)); |
| 420 delete copy; | 380 delete copy; |
| 421 } | 381 } |
| 422 | 382 |
| OLD | NEW |