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

Side by Side Diff: tools/json_schema_compiler/test/arrays_unittest.cc

Issue 2911033002: Remove raw base::DictionaryValue::Set (Closed)
Patch Set: Proper Windows Fix Created 3 years, 6 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 "tools/json_schema_compiler/test/arrays.h" 5 #include "tools/json_schema_compiler/test/arrays.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 8
9 #include <memory> 9 #include <memory>
10 #include <utility> 10 #include <utility>
11 11
12 #include "base/macros.h" 12 #include "base/macros.h"
13 #include "base/memory/ptr_util.h"
14 #include "base/values.h"
13 #include "testing/gtest/include/gtest/gtest.h" 15 #include "testing/gtest/include/gtest/gtest.h"
14 #include "tools/json_schema_compiler/test/enums.h" 16 #include "tools/json_schema_compiler/test/enums.h"
15 17
16 using namespace test::api::arrays; 18 using namespace test::api::arrays;
17 19
18 namespace { 20 namespace {
19 21
20 // TODO(calamity): Change to AppendString etc once kalman's patch goes through 22 // TODO(calamity): Change to AppendString etc once kalman's patch goes through
21 static std::unique_ptr<base::DictionaryValue> CreateBasicArrayTypeDictionary() { 23 static std::unique_ptr<base::DictionaryValue> CreateBasicArrayTypeDictionary() {
22 std::unique_ptr<base::DictionaryValue> value(new base::DictionaryValue()); 24 auto value = base::MakeUnique<base::DictionaryValue>();
23 base::ListValue* strings_value = new base::ListValue(); 25 auto strings_value = base::MakeUnique<base::ListValue>();
24 strings_value->AppendString("a"); 26 strings_value->AppendString("a");
25 strings_value->AppendString("b"); 27 strings_value->AppendString("b");
26 strings_value->AppendString("c"); 28 strings_value->AppendString("c");
27 strings_value->AppendString("it's easy as"); 29 strings_value->AppendString("it's easy as");
28 base::ListValue* integers_value = new base::ListValue(); 30 auto integers_value = base::MakeUnique<base::ListValue>();
29 integers_value->AppendInteger(1); 31 integers_value->AppendInteger(1);
30 integers_value->AppendInteger(2); 32 integers_value->AppendInteger(2);
31 integers_value->AppendInteger(3); 33 integers_value->AppendInteger(3);
32 base::ListValue* booleans_value = new base::ListValue(); 34 auto booleans_value = base::MakeUnique<base::ListValue>();
33 booleans_value->AppendBoolean(false); 35 booleans_value->AppendBoolean(false);
34 booleans_value->AppendBoolean(true); 36 booleans_value->AppendBoolean(true);
35 base::ListValue* numbers_value = new base::ListValue(); 37 auto numbers_value = base::MakeUnique<base::ListValue>();
36 numbers_value->AppendDouble(6.1); 38 numbers_value->AppendDouble(6.1);
37 value->Set("numbers", numbers_value); 39 value->Set("numbers", std::move(numbers_value));
38 value->Set("booleans", booleans_value); 40 value->Set("booleans", std::move(booleans_value));
39 value->Set("strings", strings_value); 41 value->Set("strings", std::move(strings_value));
40 value->Set("integers", integers_value); 42 value->Set("integers", std::move(integers_value));
41 return value; 43 return value;
42 } 44 }
43 45
44 std::unique_ptr<base::DictionaryValue> CreateItemValue(int val) { 46 std::unique_ptr<base::DictionaryValue> CreateItemValue(int val) {
45 std::unique_ptr<base::DictionaryValue> value(new base::DictionaryValue()); 47 auto value = base::MakeUnique<base::DictionaryValue>();
46 value->Set("val", new base::Value(val)); 48 value->SetInteger("val", val);
47 return value; 49 return value;
48 } 50 }
49 51
50 } // namespace 52 } // namespace
51 53
52 TEST(JsonSchemaCompilerArrayTest, BasicArrayType) { 54 TEST(JsonSchemaCompilerArrayTest, BasicArrayType) {
53 { 55 {
54 std::unique_ptr<base::DictionaryValue> value = 56 std::unique_ptr<base::DictionaryValue> value =
55 CreateBasicArrayTypeDictionary(); 57 CreateBasicArrayTypeDictionary();
56 std::unique_ptr<BasicArrayType> basic_array_type(new BasicArrayType()); 58 std::unique_ptr<BasicArrayType> basic_array_type(new BasicArrayType());
57 ASSERT_TRUE(BasicArrayType::Populate(*value, basic_array_type.get())); 59 ASSERT_TRUE(BasicArrayType::Populate(*value, basic_array_type.get()));
58 EXPECT_TRUE(value->Equals(basic_array_type->ToValue().get())); 60 EXPECT_TRUE(value->Equals(basic_array_type->ToValue().get()));
59 } 61 }
60 } 62 }
61 63
62 TEST(JsonSchemaCompilerArrayTest, EnumArrayReference) { 64 TEST(JsonSchemaCompilerArrayTest, EnumArrayReference) {
63 // { "types": ["one", "two", "three"] } 65 // { "types": ["one", "two", "three"] }
64 base::ListValue* types = new base::ListValue(); 66 auto types = base::MakeUnique<base::ListValue>();
65 types->AppendString("one"); 67 types->AppendString("one");
66 types->AppendString("two"); 68 types->AppendString("two");
67 types->AppendString("three"); 69 types->AppendString("three");
68 base::DictionaryValue value; 70 base::DictionaryValue value;
69 value.Set("types", types); 71 value.Set("types", std::move(types));
70 72
71 EnumArrayReference enum_array_reference; 73 EnumArrayReference enum_array_reference;
72 74
73 // Test Populate. 75 // Test Populate.
74 ASSERT_TRUE(EnumArrayReference::Populate(value, &enum_array_reference)); 76 ASSERT_TRUE(EnumArrayReference::Populate(value, &enum_array_reference));
75 77
76 Enumeration expected_types[] = {ENUMERATION_ONE, ENUMERATION_TWO, 78 Enumeration expected_types[] = {ENUMERATION_ONE, ENUMERATION_TWO,
77 ENUMERATION_THREE}; 79 ENUMERATION_THREE};
78 EXPECT_EQ(std::vector<Enumeration>( 80 EXPECT_EQ(std::vector<Enumeration>(
79 expected_types, expected_types + arraysize(expected_types)), 81 expected_types, expected_types + arraysize(expected_types)),
80 enum_array_reference.types); 82 enum_array_reference.types);
81 83
82 // Test ToValue. 84 // Test ToValue.
83 std::unique_ptr<base::Value> as_value(enum_array_reference.ToValue()); 85 std::unique_ptr<base::Value> as_value(enum_array_reference.ToValue());
84 EXPECT_TRUE(value.Equals(as_value.get())) << value << " != " << *as_value; 86 EXPECT_TRUE(value.Equals(as_value.get())) << value << " != " << *as_value;
85 } 87 }
86 88
87 TEST(JsonSchemaCompilerArrayTest, EnumArrayMixed) { 89 TEST(JsonSchemaCompilerArrayTest, EnumArrayMixed) {
88 // { "types": ["one", "two", "three"] } 90 // { "types": ["one", "two", "three"] }
89 base::ListValue* infile_enums = new base::ListValue(); 91 auto infile_enums = base::MakeUnique<base::ListValue>();
90 infile_enums->AppendString("one"); 92 infile_enums->AppendString("one");
91 infile_enums->AppendString("two"); 93 infile_enums->AppendString("two");
92 infile_enums->AppendString("three"); 94 infile_enums->AppendString("three");
93 95
94 base::ListValue* external_enums = new base::ListValue(); 96 auto external_enums = base::MakeUnique<base::ListValue>();
95 external_enums->AppendString("one"); 97 external_enums->AppendString("one");
96 external_enums->AppendString("two"); 98 external_enums->AppendString("two");
97 external_enums->AppendString("three"); 99 external_enums->AppendString("three");
98 100
99 base::DictionaryValue value; 101 base::DictionaryValue value;
100 value.Set("infile_enums", infile_enums); 102 value.Set("infile_enums", std::move(infile_enums));
101 value.Set("external_enums", external_enums); 103 value.Set("external_enums", std::move(external_enums));
102 104
103 EnumArrayMixed enum_array_mixed; 105 EnumArrayMixed enum_array_mixed;
104 106
105 // Test Populate. 107 // Test Populate.
106 ASSERT_TRUE(EnumArrayMixed::Populate(value, &enum_array_mixed)); 108 ASSERT_TRUE(EnumArrayMixed::Populate(value, &enum_array_mixed));
107 109
108 Enumeration expected_infile_types[] = {ENUMERATION_ONE, ENUMERATION_TWO, 110 Enumeration expected_infile_types[] = {ENUMERATION_ONE, ENUMERATION_TWO,
109 ENUMERATION_THREE}; 111 ENUMERATION_THREE};
110 EXPECT_EQ(std::vector<Enumeration>( 112 EXPECT_EQ(std::vector<Enumeration>(
111 expected_infile_types, 113 expected_infile_types,
(...skipping 13 matching lines...) Expand all
125 EXPECT_TRUE(value.Equals(as_value.get())) << value << " != " << *as_value; 127 EXPECT_TRUE(value.Equals(as_value.get())) << value << " != " << *as_value;
126 } 128 }
127 129
128 TEST(JsonSchemaCompilerArrayTest, OptionalEnumArrayType) { 130 TEST(JsonSchemaCompilerArrayTest, OptionalEnumArrayType) {
129 { 131 {
130 std::vector<Enumeration> enums; 132 std::vector<Enumeration> enums;
131 enums.push_back(ENUMERATION_ONE); 133 enums.push_back(ENUMERATION_ONE);
132 enums.push_back(ENUMERATION_TWO); 134 enums.push_back(ENUMERATION_TWO);
133 enums.push_back(ENUMERATION_THREE); 135 enums.push_back(ENUMERATION_THREE);
134 136
135 std::unique_ptr<base::ListValue> types(new base::ListValue()); 137 auto types = base::MakeUnique<base::ListValue>();
136 for (size_t i = 0; i < enums.size(); ++i) 138 for (size_t i = 0; i < enums.size(); ++i)
137 types->AppendString(ToString(enums[i])); 139 types->AppendString(ToString(enums[i]));
138 140
139 base::DictionaryValue value; 141 base::DictionaryValue value;
140 value.Set("types", types.release()); 142 value.Set("types", std::move(types));
141 143
142 OptionalEnumArrayType enum_array_type; 144 OptionalEnumArrayType enum_array_type;
143 ASSERT_TRUE(OptionalEnumArrayType::Populate(value, &enum_array_type)); 145 ASSERT_TRUE(OptionalEnumArrayType::Populate(value, &enum_array_type));
144 EXPECT_EQ(enums, *enum_array_type.types); 146 EXPECT_EQ(enums, *enum_array_type.types);
145 } 147 }
146 { 148 {
147 base::DictionaryValue value; 149 base::DictionaryValue value;
148 std::unique_ptr<base::ListValue> enum_array(new base::ListValue()); 150 auto enum_array = base::MakeUnique<base::ListValue>();
149 enum_array->AppendString("invalid"); 151 enum_array->AppendString("invalid");
150 152
151 value.Set("types", enum_array.release()); 153 value.Set("types", std::move(enum_array));
152 OptionalEnumArrayType enum_array_type; 154 OptionalEnumArrayType enum_array_type;
153 ASSERT_FALSE(OptionalEnumArrayType::Populate(value, &enum_array_type)); 155 ASSERT_FALSE(OptionalEnumArrayType::Populate(value, &enum_array_type));
154 EXPECT_TRUE(enum_array_type.types->empty()); 156 EXPECT_TRUE(enum_array_type.types->empty());
155 } 157 }
156 } 158 }
157 159
158 TEST(JsonSchemaCompilerArrayTest, RefArrayType) { 160 TEST(JsonSchemaCompilerArrayTest, RefArrayType) {
159 { 161 {
160 std::unique_ptr<base::DictionaryValue> value(new base::DictionaryValue()); 162 auto value = base::MakeUnique<base::DictionaryValue>();
161 std::unique_ptr<base::ListValue> ref_array(new base::ListValue()); 163 auto ref_array = base::MakeUnique<base::ListValue>();
162 ref_array->Append(CreateItemValue(1)); 164 ref_array->Append(CreateItemValue(1));
163 ref_array->Append(CreateItemValue(2)); 165 ref_array->Append(CreateItemValue(2));
164 ref_array->Append(CreateItemValue(3)); 166 ref_array->Append(CreateItemValue(3));
165 value->Set("refs", ref_array.release()); 167 value->Set("refs", std::move(ref_array));
166 std::unique_ptr<RefArrayType> ref_array_type(new RefArrayType()); 168 auto ref_array_type = base::MakeUnique<RefArrayType>();
167 EXPECT_TRUE(RefArrayType::Populate(*value, ref_array_type.get())); 169 EXPECT_TRUE(RefArrayType::Populate(*value, ref_array_type.get()));
168 ASSERT_EQ(3u, ref_array_type->refs.size()); 170 ASSERT_EQ(3u, ref_array_type->refs.size());
169 EXPECT_EQ(1, ref_array_type->refs[0].val); 171 EXPECT_EQ(1, ref_array_type->refs[0].val);
170 EXPECT_EQ(2, ref_array_type->refs[1].val); 172 EXPECT_EQ(2, ref_array_type->refs[1].val);
171 EXPECT_EQ(3, ref_array_type->refs[2].val); 173 EXPECT_EQ(3, ref_array_type->refs[2].val);
172 } 174 }
173 { 175 {
174 std::unique_ptr<base::DictionaryValue> value(new base::DictionaryValue()); 176 auto value = base::MakeUnique<base::DictionaryValue>();
175 std::unique_ptr<base::ListValue> not_ref_array(new base::ListValue()); 177 auto not_ref_array = base::MakeUnique<base::ListValue>();
176 not_ref_array->Append(CreateItemValue(1)); 178 not_ref_array->Append(CreateItemValue(1));
177 not_ref_array->AppendInteger(3); 179 not_ref_array->AppendInteger(3);
178 value->Set("refs", not_ref_array.release()); 180 value->Set("refs", std::move(not_ref_array));
179 std::unique_ptr<RefArrayType> ref_array_type(new RefArrayType()); 181 auto ref_array_type = base::MakeUnique<RefArrayType>();
180 EXPECT_FALSE(RefArrayType::Populate(*value, ref_array_type.get())); 182 EXPECT_FALSE(RefArrayType::Populate(*value, ref_array_type.get()));
181 } 183 }
182 } 184 }
183 185
184 TEST(JsonSchemaCompilerArrayTest, IntegerArrayParamsCreate) { 186 TEST(JsonSchemaCompilerArrayTest, IntegerArrayParamsCreate) {
185 std::unique_ptr<base::ListValue> params_value(new base::ListValue()); 187 std::unique_ptr<base::ListValue> params_value(new base::ListValue());
186 std::unique_ptr<base::ListValue> integer_array(new base::ListValue()); 188 std::unique_ptr<base::ListValue> integer_array(new base::ListValue());
187 integer_array->AppendInteger(2); 189 integer_array->AppendInteger(2);
188 integer_array->AppendInteger(4); 190 integer_array->AppendInteger(4);
189 integer_array->AppendInteger(8); 191 integer_array->AppendInteger(8);
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after
269 std::unique_ptr<base::ListValue> expected_argument(new base::ListValue()); 271 std::unique_ptr<base::ListValue> expected_argument(new base::ListValue());
270 std::unique_ptr<base::DictionaryValue> first(new base::DictionaryValue()); 272 std::unique_ptr<base::DictionaryValue> first(new base::DictionaryValue());
271 first->SetInteger("val", 1); 273 first->SetInteger("val", 1);
272 expected_argument->Append(std::move(first)); 274 expected_argument->Append(std::move(first));
273 std::unique_ptr<base::DictionaryValue> second(new base::DictionaryValue()); 275 std::unique_ptr<base::DictionaryValue> second(new base::DictionaryValue());
274 second->SetInteger("val", 2); 276 second->SetInteger("val", 2);
275 expected_argument->Append(std::move(second)); 277 expected_argument->Append(std::move(second));
276 expected.Append(std::move(expected_argument)); 278 expected.Append(std::move(expected_argument));
277 EXPECT_TRUE(results->Equals(&expected)); 279 EXPECT_TRUE(results->Equals(&expected));
278 } 280 }
OLDNEW
« no previous file with comments | « tools/json_schema_compiler/test/additional_properties_unittest.cc ('k') | tools/json_schema_compiler/test/choices_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698