Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(117)

Side by Side Diff: base/values_unittest.cc

Issue 2689673002: Inline base::BinaryValue into base::Value (Closed)
Patch Set: Created 3 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 "base/values.h" 5 #include "base/values.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 8
9 #include <limits> 9 #include <limits>
10 #include <memory> 10 #include <memory>
11 #include <utility> 11 #include <utility>
12 #include <vector>
12 13
13 #include "base/memory/ptr_util.h" 14 #include "base/memory/ptr_util.h"
14 #include "base/strings/string16.h" 15 #include "base/strings/string16.h"
15 #include "base/strings/utf_string_conversions.h" 16 #include "base/strings/utf_string_conversions.h"
16 #include "testing/gtest/include/gtest/gtest.h" 17 #include "testing/gtest/include/gtest/gtest.h"
17 18
18 namespace base { 19 namespace base {
19 20
20 // Group of tests for the value constructors. 21 // Group of tests for the value constructors.
21 TEST(ValuesTest, ConstructBool) { 22 TEST(ValuesTest, ConstructBool) {
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
75 EXPECT_EQ("foobar", value.GetString()); 76 EXPECT_EQ("foobar", value.GetString());
76 } 77 }
77 78
78 TEST(ValuesTest, ConstructStringFromStringPiece) { 79 TEST(ValuesTest, ConstructStringFromStringPiece) {
79 StringPiece str = "foobar"; 80 StringPiece str = "foobar";
80 StringValue value(str); 81 StringValue value(str);
81 EXPECT_EQ(Value::Type::STRING, value.type()); 82 EXPECT_EQ(Value::Type::STRING, value.type());
82 EXPECT_EQ("foobar", value.GetString()); 83 EXPECT_EQ("foobar", value.GetString());
83 } 84 }
84 85
86 TEST(ValuesTest, ConstructBinary) {
87 BinaryValue value(std::vector<char>({0xF, 0x0, 0x0, 0xB, 0xA, 0x2}));
88 EXPECT_EQ(Value::Type::BINARY, value.type());
89 EXPECT_EQ(std::vector<char>({0xF, 0x0, 0x0, 0xB, 0xA, 0x2}), value.GetBlob());
90 }
91
85 // Group of tests for the copy constructors and copy-assigmnent. For equality 92 // Group of tests for the copy constructors and copy-assigmnent. For equality
86 // checks comparisons of the interesting fields are done instead of relying on 93 // checks comparisons of the interesting fields are done instead of relying on
87 // Equals being correct. 94 // Equals being correct.
88 TEST(ValuesTest, CopyBool) { 95 TEST(ValuesTest, CopyBool) {
89 FundamentalValue true_value(true); 96 FundamentalValue true_value(true);
90 FundamentalValue copied_true_value(true_value); 97 FundamentalValue copied_true_value(true_value);
91 EXPECT_EQ(true_value.type(), copied_true_value.type()); 98 EXPECT_EQ(true_value.type(), copied_true_value.type());
92 EXPECT_EQ(true_value.GetBool(), copied_true_value.GetBool()); 99 EXPECT_EQ(true_value.GetBool(), copied_true_value.GetBool());
93 100
94 FundamentalValue false_value(false); 101 FundamentalValue false_value(false);
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
139 EXPECT_EQ(value.type(), copied_value.type()); 146 EXPECT_EQ(value.type(), copied_value.type());
140 EXPECT_EQ(value.GetString(), copied_value.GetString()); 147 EXPECT_EQ(value.GetString(), copied_value.GetString());
141 148
142 Value blank; 149 Value blank;
143 150
144 blank = value; 151 blank = value;
145 EXPECT_EQ(value.type(), blank.type()); 152 EXPECT_EQ(value.type(), blank.type());
146 EXPECT_EQ(value.GetString(), blank.GetString()); 153 EXPECT_EQ(value.GetString(), blank.GetString());
147 } 154 }
148 155
156 TEST(ValuesTest, CopyBinary) {
157 BinaryValue value(std::vector<char>({0xF, 0x0, 0x0, 0xB, 0xA, 0x2}));
158 BinaryValue copied_value(value);
159 EXPECT_EQ(value.type(), copied_value.type());
160 EXPECT_EQ(value.GetBlob(), copied_value.GetBlob());
161
162 Value blank;
163
164 blank = value;
165 EXPECT_EQ(value.type(), blank.type());
166 EXPECT_EQ(value.GetBlob(), blank.GetBlob());
167 }
168
149 // Group of tests for the move constructors and move-assigmnent. 169 // Group of tests for the move constructors and move-assigmnent.
150 TEST(ValuesTest, MoveBool) { 170 TEST(ValuesTest, MoveBool) {
151 FundamentalValue true_value(true); 171 FundamentalValue true_value(true);
152 FundamentalValue moved_true_value(std::move(true_value)); 172 FundamentalValue moved_true_value(std::move(true_value));
153 EXPECT_EQ(Value::Type::BOOLEAN, moved_true_value.type()); 173 EXPECT_EQ(Value::Type::BOOLEAN, moved_true_value.type());
154 EXPECT_TRUE(moved_true_value.GetBool()); 174 EXPECT_TRUE(moved_true_value.GetBool());
155 175
156 FundamentalValue false_value(false); 176 FundamentalValue false_value(false);
157 FundamentalValue moved_false_value(std::move(false_value)); 177 FundamentalValue moved_false_value(std::move(false_value));
158 EXPECT_EQ(Value::Type::BOOLEAN, moved_false_value.type()); 178 EXPECT_EQ(Value::Type::BOOLEAN, moved_false_value.type());
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
201 EXPECT_EQ(Value::Type::STRING, moved_value.type()); 221 EXPECT_EQ(Value::Type::STRING, moved_value.type());
202 EXPECT_EQ("foobar", moved_value.GetString()); 222 EXPECT_EQ("foobar", moved_value.GetString());
203 223
204 Value blank; 224 Value blank;
205 225
206 blank = StringValue("foobar"); 226 blank = StringValue("foobar");
207 EXPECT_EQ(Value::Type::STRING, blank.type()); 227 EXPECT_EQ(Value::Type::STRING, blank.type());
208 EXPECT_EQ("foobar", blank.GetString()); 228 EXPECT_EQ("foobar", blank.GetString());
209 } 229 }
210 230
231 TEST(ValuesTest, MoveBinary) {
232 const std::vector<char> buffer = {0xF, 0x0, 0x0, 0xB, 0xA, 0x2};
233 BinaryValue value(buffer);
234 BinaryValue moved_value(std::move(value));
235 EXPECT_EQ(Value::Type::BINARY, moved_value.type());
236 EXPECT_EQ(buffer, moved_value.GetBlob());
237
238 Value blank;
239
240 blank = BinaryValue(buffer);
241 EXPECT_EQ(Value::Type::BINARY, blank.type());
242 EXPECT_EQ(buffer, blank.GetBlob());
243 }
244
211 TEST(ValuesTest, Basic) { 245 TEST(ValuesTest, Basic) {
212 // Test basic dictionary getting/setting 246 // Test basic dictionary getting/setting
213 DictionaryValue settings; 247 DictionaryValue settings;
214 std::string homepage = "http://google.com"; 248 std::string homepage = "http://google.com";
215 ASSERT_FALSE(settings.GetString("global.homepage", &homepage)); 249 ASSERT_FALSE(settings.GetString("global.homepage", &homepage));
216 ASSERT_EQ(std::string("http://google.com"), homepage); 250 ASSERT_EQ(std::string("http://google.com"), homepage);
217 251
218 ASSERT_FALSE(settings.Get("global", NULL)); 252 ASSERT_FALSE(settings.Get("global", NULL));
219 settings.SetBoolean("global", true); 253 settings.SetBoolean("global", true);
220 ASSERT_TRUE(settings.Get("global", NULL)); 254 ASSERT_TRUE(settings.Get("global", NULL));
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after
294 base::FundamentalValue sought_value(42); 328 base::FundamentalValue sought_value(42);
295 base::FundamentalValue not_found_value(false); 329 base::FundamentalValue not_found_value(false);
296 330
297 ASSERT_NE(mixed_list->end(), mixed_list->Find(sought_value)); 331 ASSERT_NE(mixed_list->end(), mixed_list->Find(sought_value));
298 ASSERT_TRUE((*mixed_list->Find(sought_value))->GetAsInteger(&int_value)); 332 ASSERT_TRUE((*mixed_list->Find(sought_value))->GetAsInteger(&int_value));
299 ASSERT_EQ(42, int_value); 333 ASSERT_EQ(42, int_value);
300 ASSERT_EQ(mixed_list->end(), mixed_list->Find(not_found_value)); 334 ASSERT_EQ(mixed_list->end(), mixed_list->Find(not_found_value));
301 } 335 }
302 336
303 TEST(ValuesTest, BinaryValue) { 337 TEST(ValuesTest, BinaryValue) {
304 // Default constructor creates a BinaryValue with a null buffer and size 0. 338 // Default constructor creates a BinaryValue with a buffer of size 0.
305 std::unique_ptr<BinaryValue> binary(new BinaryValue()); 339 auto binary = MakeUnique<Value>(Value::Type::BINARY);
306 ASSERT_TRUE(binary.get()); 340 ASSERT_TRUE(binary.get());
307 ASSERT_EQ(NULL, binary->GetBuffer());
308 ASSERT_EQ(0U, binary->GetSize()); 341 ASSERT_EQ(0U, binary->GetSize());
309 342
310 // Test the common case of a non-empty buffer 343 // Test the common case of a non-empty buffer
311 std::unique_ptr<char[]> buffer(new char[15]); 344 std::vector<char> buffer(15);
312 char* original_buffer = buffer.get(); 345 char* original_buffer = buffer.data();
313 binary.reset(new BinaryValue(std::move(buffer), 15)); 346 binary.reset(new BinaryValue(std::move(buffer)));
314 ASSERT_TRUE(binary.get()); 347 ASSERT_TRUE(binary.get());
315 ASSERT_TRUE(binary->GetBuffer()); 348 ASSERT_TRUE(binary->GetBuffer());
316 ASSERT_EQ(original_buffer, binary->GetBuffer()); 349 ASSERT_EQ(original_buffer, binary->GetBuffer());
317 ASSERT_EQ(15U, binary->GetSize()); 350 ASSERT_EQ(15U, binary->GetSize());
318 351
319 char stack_buffer[42]; 352 char stack_buffer[42];
320 memset(stack_buffer, '!', 42); 353 memset(stack_buffer, '!', 42);
321 binary = BinaryValue::CreateWithCopiedBuffer(stack_buffer, 42); 354 binary = BinaryValue::CreateWithCopiedBuffer(stack_buffer, 42);
322 ASSERT_TRUE(binary.get()); 355 ASSERT_TRUE(binary.get());
323 ASSERT_TRUE(binary->GetBuffer()); 356 ASSERT_TRUE(binary->GetBuffer());
(...skipping 277 matching lines...) Expand 10 before | Expand all | Expand 10 after
601 FundamentalValue* original_double = scoped_double.get(); 634 FundamentalValue* original_double = scoped_double.get();
602 original_dict.Set("double", std::move(scoped_double)); 635 original_dict.Set("double", std::move(scoped_double));
603 std::unique_ptr<StringValue> scoped_string(new StringValue("hello")); 636 std::unique_ptr<StringValue> scoped_string(new StringValue("hello"));
604 StringValue* original_string = scoped_string.get(); 637 StringValue* original_string = scoped_string.get();
605 original_dict.Set("string", std::move(scoped_string)); 638 original_dict.Set("string", std::move(scoped_string));
606 std::unique_ptr<StringValue> scoped_string16( 639 std::unique_ptr<StringValue> scoped_string16(
607 new StringValue(ASCIIToUTF16("hello16"))); 640 new StringValue(ASCIIToUTF16("hello16")));
608 StringValue* original_string16 = scoped_string16.get(); 641 StringValue* original_string16 = scoped_string16.get();
609 original_dict.Set("string16", std::move(scoped_string16)); 642 original_dict.Set("string16", std::move(scoped_string16));
610 643
611 std::unique_ptr<char[]> original_buffer(new char[42]); 644 std::vector<char> original_buffer(42, '!');
612 memset(original_buffer.get(), '!', 42);
613 std::unique_ptr<BinaryValue> scoped_binary( 645 std::unique_ptr<BinaryValue> scoped_binary(
614 new BinaryValue(std::move(original_buffer), 42)); 646 new BinaryValue(std::move(original_buffer)));
615 BinaryValue* original_binary = scoped_binary.get(); 647 BinaryValue* original_binary = scoped_binary.get();
616 original_dict.Set("binary", std::move(scoped_binary)); 648 original_dict.Set("binary", std::move(scoped_binary));
617 649
618 std::unique_ptr<ListValue> scoped_list(new ListValue()); 650 std::unique_ptr<ListValue> scoped_list(new ListValue());
619 Value* original_list = scoped_list.get(); 651 Value* original_list = scoped_list.get();
620 std::unique_ptr<FundamentalValue> scoped_list_element_0( 652 std::unique_ptr<FundamentalValue> scoped_list_element_0(
621 new FundamentalValue(0)); 653 new FundamentalValue(0));
622 Value* original_list_element_0 = scoped_list_element_0.get(); 654 Value* original_list_element_0 = scoped_list_element_0.get();
623 scoped_list->Append(std::move(scoped_list_element_0)); 655 scoped_list->Append(std::move(scoped_list_element_0));
624 std::unique_ptr<FundamentalValue> scoped_list_element_1( 656 std::unique_ptr<FundamentalValue> scoped_list_element_1(
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after
690 ASSERT_TRUE(copy_string16->GetAsString(&copy_string_value)); 722 ASSERT_TRUE(copy_string16->GetAsString(&copy_string_value));
691 ASSERT_TRUE(copy_string16->GetAsString(&copy_string16_value)); 723 ASSERT_TRUE(copy_string16->GetAsString(&copy_string16_value));
692 ASSERT_EQ(std::string("hello16"), copy_string_value); 724 ASSERT_EQ(std::string("hello16"), copy_string_value);
693 ASSERT_EQ(ASCIIToUTF16("hello16"), copy_string16_value); 725 ASSERT_EQ(ASCIIToUTF16("hello16"), copy_string16_value);
694 726
695 Value* copy_binary = NULL; 727 Value* copy_binary = NULL;
696 ASSERT_TRUE(copy_dict->Get("binary", &copy_binary)); 728 ASSERT_TRUE(copy_dict->Get("binary", &copy_binary));
697 ASSERT_TRUE(copy_binary); 729 ASSERT_TRUE(copy_binary);
698 ASSERT_NE(copy_binary, original_binary); 730 ASSERT_NE(copy_binary, original_binary);
699 ASSERT_TRUE(copy_binary->IsType(Value::Type::BINARY)); 731 ASSERT_TRUE(copy_binary->IsType(Value::Type::BINARY));
700 ASSERT_NE(original_binary->GetBuffer(), 732 ASSERT_NE(original_binary->GetBuffer(), copy_binary->GetBuffer());
701 static_cast<BinaryValue*>(copy_binary)->GetBuffer()); 733 ASSERT_EQ(original_binary->GetSize(), copy_binary->GetSize());
702 ASSERT_EQ(original_binary->GetSize(), 734 ASSERT_EQ(0, memcmp(original_binary->GetBuffer(), copy_binary->GetBuffer(),
703 static_cast<BinaryValue*>(copy_binary)->GetSize()); 735 original_binary->GetSize()));
704 ASSERT_EQ(0, memcmp(original_binary->GetBuffer(),
705 static_cast<BinaryValue*>(copy_binary)->GetBuffer(),
706 original_binary->GetSize()));
707 736
708 Value* copy_value = NULL; 737 Value* copy_value = NULL;
709 ASSERT_TRUE(copy_dict->Get("list", &copy_value)); 738 ASSERT_TRUE(copy_dict->Get("list", &copy_value));
710 ASSERT_TRUE(copy_value); 739 ASSERT_TRUE(copy_value);
711 ASSERT_NE(copy_value, original_list); 740 ASSERT_NE(copy_value, original_list);
712 ASSERT_TRUE(copy_value->IsType(Value::Type::LIST)); 741 ASSERT_TRUE(copy_value->IsType(Value::Type::LIST));
713 ListValue* copy_list = NULL; 742 ListValue* copy_list = NULL;
714 ASSERT_TRUE(copy_value->GetAsList(&copy_list)); 743 ASSERT_TRUE(copy_value->GetAsList(&copy_list));
715 ASSERT_TRUE(copy_list); 744 ASSERT_TRUE(copy_list);
716 ASSERT_EQ(2U, copy_list->GetSize()); 745 ASSERT_EQ(2U, copy_list->GetSize());
(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after
822 Value* original_double = scoped_double.get(); 851 Value* original_double = scoped_double.get();
823 original_dict.Set("double", std::move(scoped_double)); 852 original_dict.Set("double", std::move(scoped_double));
824 std::unique_ptr<StringValue> scoped_string(new StringValue("hello")); 853 std::unique_ptr<StringValue> scoped_string(new StringValue("hello"));
825 Value* original_string = scoped_string.get(); 854 Value* original_string = scoped_string.get();
826 original_dict.Set("string", std::move(scoped_string)); 855 original_dict.Set("string", std::move(scoped_string));
827 std::unique_ptr<StringValue> scoped_string16( 856 std::unique_ptr<StringValue> scoped_string16(
828 new StringValue(ASCIIToUTF16("hello16"))); 857 new StringValue(ASCIIToUTF16("hello16")));
829 Value* original_string16 = scoped_string16.get(); 858 Value* original_string16 = scoped_string16.get();
830 original_dict.Set("string16", std::move(scoped_string16)); 859 original_dict.Set("string16", std::move(scoped_string16));
831 860
832 std::unique_ptr<char[]> original_buffer(new char[42]); 861 std::vector<char> original_buffer(42, '!');
833 memset(original_buffer.get(), '!', 42);
834 std::unique_ptr<BinaryValue> scoped_binary( 862 std::unique_ptr<BinaryValue> scoped_binary(
835 new BinaryValue(std::move(original_buffer), 42)); 863 new BinaryValue(std::move(original_buffer)));
836 Value* original_binary = scoped_binary.get(); 864 Value* original_binary = scoped_binary.get();
837 original_dict.Set("binary", std::move(scoped_binary)); 865 original_dict.Set("binary", std::move(scoped_binary));
838 866
839 std::unique_ptr<ListValue> scoped_list(new ListValue()); 867 std::unique_ptr<ListValue> scoped_list(new ListValue());
840 Value* original_list = scoped_list.get(); 868 Value* original_list = scoped_list.get();
841 std::unique_ptr<FundamentalValue> scoped_list_element_0( 869 std::unique_ptr<FundamentalValue> scoped_list_element_0(
842 new FundamentalValue(0)); 870 new FundamentalValue(0));
843 scoped_list->Append(std::move(scoped_list_element_0)); 871 scoped_list->Append(std::move(scoped_list_element_0));
844 std::unique_ptr<FundamentalValue> scoped_list_element_1( 872 std::unique_ptr<FundamentalValue> scoped_list_element_1(
845 new FundamentalValue(1)); 873 new FundamentalValue(1));
(...skipping 216 matching lines...) Expand 10 before | Expand all | Expand 10 after
1062 // DictionaryValue/ListValue's Get*() methods should accept NULL as an out-value 1090 // DictionaryValue/ListValue's Get*() methods should accept NULL as an out-value
1063 // and still return true/false based on success. 1091 // and still return true/false based on success.
1064 TEST(ValuesTest, GetWithNullOutValue) { 1092 TEST(ValuesTest, GetWithNullOutValue) {
1065 DictionaryValue main_dict; 1093 DictionaryValue main_dict;
1066 ListValue main_list; 1094 ListValue main_list;
1067 1095
1068 FundamentalValue bool_value(false); 1096 FundamentalValue bool_value(false);
1069 FundamentalValue int_value(1234); 1097 FundamentalValue int_value(1234);
1070 FundamentalValue double_value(12.34567); 1098 FundamentalValue double_value(12.34567);
1071 StringValue string_value("foo"); 1099 StringValue string_value("foo");
1072 BinaryValue binary_value; 1100 BinaryValue binary_value(Value::Type::BINARY);
1073 DictionaryValue dict_value; 1101 DictionaryValue dict_value;
1074 ListValue list_value; 1102 ListValue list_value;
1075 1103
1076 main_dict.Set("bool", bool_value.CreateDeepCopy()); 1104 main_dict.Set("bool", bool_value.CreateDeepCopy());
1077 main_dict.Set("int", int_value.CreateDeepCopy()); 1105 main_dict.Set("int", int_value.CreateDeepCopy());
1078 main_dict.Set("double", double_value.CreateDeepCopy()); 1106 main_dict.Set("double", double_value.CreateDeepCopy());
1079 main_dict.Set("string", string_value.CreateDeepCopy()); 1107 main_dict.Set("string", string_value.CreateDeepCopy());
1080 main_dict.Set("binary", binary_value.CreateDeepCopy()); 1108 main_dict.Set("binary", binary_value.CreateDeepCopy());
1081 main_dict.Set("dict", dict_value.CreateDeepCopy()); 1109 main_dict.Set("dict", dict_value.CreateDeepCopy());
1082 main_dict.Set("list", list_value.CreateDeepCopy()); 1110 main_dict.Set("list", list_value.CreateDeepCopy());
(...skipping 255 matching lines...) Expand 10 before | Expand all | Expand 10 after
1338 EXPECT_FALSE(main_list.GetList(1, NULL)); 1366 EXPECT_FALSE(main_list.GetList(1, NULL));
1339 EXPECT_FALSE(main_list.GetList(2, NULL)); 1367 EXPECT_FALSE(main_list.GetList(2, NULL));
1340 EXPECT_FALSE(main_list.GetList(3, NULL)); 1368 EXPECT_FALSE(main_list.GetList(3, NULL));
1341 EXPECT_FALSE(main_list.GetList(4, NULL)); 1369 EXPECT_FALSE(main_list.GetList(4, NULL));
1342 EXPECT_FALSE(main_list.GetList(5, NULL)); 1370 EXPECT_FALSE(main_list.GetList(5, NULL));
1343 EXPECT_TRUE(main_list.GetList(6, NULL)); 1371 EXPECT_TRUE(main_list.GetList(6, NULL));
1344 EXPECT_FALSE(main_list.GetList(7, NULL)); 1372 EXPECT_FALSE(main_list.GetList(7, NULL));
1345 } 1373 }
1346 1374
1347 } // namespace base 1375 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698