Roll src/third_party/WebKit f007c95:0171005 (svn 185074:185088)
[chromium-blink-merge.git] / tools / json_schema_compiler / test / arrays_unittest.cc
blob79ebfb2360e8b1d75a273432d323d342742f35a0
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
3 // found in the LICENSE file.
5 #include "tools/json_schema_compiler/test/arrays.h"
7 #include "testing/gtest/include/gtest/gtest.h"
8 #include "tools/json_schema_compiler/test/enums.h"
10 using namespace test::api::arrays;
12 namespace {
14 // TODO(calamity): Change to AppendString etc once kalman's patch goes through
15 static scoped_ptr<base::DictionaryValue> CreateBasicArrayTypeDictionary() {
16 base::DictionaryValue* value = new base::DictionaryValue();
17 base::ListValue* strings_value = new base::ListValue();
18 strings_value->Append(new base::StringValue("a"));
19 strings_value->Append(new base::StringValue("b"));
20 strings_value->Append(new base::StringValue("c"));
21 strings_value->Append(new base::StringValue("it's easy as"));
22 base::ListValue* integers_value = new base::ListValue();
23 integers_value->Append(new base::FundamentalValue(1));
24 integers_value->Append(new base::FundamentalValue(2));
25 integers_value->Append(new base::FundamentalValue(3));
26 base::ListValue* booleans_value = new base::ListValue();
27 booleans_value->Append(new base::FundamentalValue(false));
28 booleans_value->Append(new base::FundamentalValue(true));
29 base::ListValue* numbers_value = new base::ListValue();
30 numbers_value->Append(new base::FundamentalValue(6.1));
31 value->Set("numbers", numbers_value);
32 value->Set("booleans", booleans_value);
33 value->Set("strings", strings_value);
34 value->Set("integers", integers_value);
35 return scoped_ptr<base::DictionaryValue>(value);
38 static base::Value* CreateItemValue(int val) {
39 base::DictionaryValue* value(new base::DictionaryValue());
40 value->Set("val", new base::FundamentalValue(val));
41 return value;
44 } // namespace
46 TEST(JsonSchemaCompilerArrayTest, BasicArrayType) {
48 scoped_ptr<base::DictionaryValue> value = CreateBasicArrayTypeDictionary();
49 scoped_ptr<BasicArrayType> basic_array_type(new BasicArrayType());
50 ASSERT_TRUE(BasicArrayType::Populate(*value, basic_array_type.get()));
51 EXPECT_TRUE(value->Equals(basic_array_type->ToValue().get()));
55 TEST(JsonSchemaCompilerArrayTest, EnumArrayType) {
56 // { "types": ["one", "two", "three"] }
57 base::ListValue* types = new base::ListValue();
58 types->AppendString("one");
59 types->AppendString("two");
60 types->AppendString("three");
61 base::DictionaryValue value;
62 value.Set("types", types);
64 EnumArrayType enum_array_type;
66 // Test Populate.
67 ASSERT_TRUE(EnumArrayType::Populate(value, &enum_array_type));
69 EnumArrayType::TypesType enums[] = {
70 EnumArrayType::TYPES_TYPE_ONE,
71 EnumArrayType::TYPES_TYPE_TWO,
72 EnumArrayType::TYPES_TYPE_THREE,
74 std::vector<EnumArrayType::TypesType> enums_vector(
75 enums, enums + arraysize(enums));
76 EXPECT_EQ(enums_vector, enum_array_type.types);
79 // Test ToValue.
80 scoped_ptr<base::Value> as_value(enum_array_type.ToValue());
81 EXPECT_TRUE(value.Equals(as_value.get())) << value << " != " << *as_value;
84 TEST(JsonSchemaCompilerArrayTest, EnumArrayReference) {
85 // { "types": ["one", "two", "three"] }
86 base::ListValue* types = new base::ListValue();
87 types->AppendString("one");
88 types->AppendString("two");
89 types->AppendString("three");
90 base::DictionaryValue value;
91 value.Set("types", types);
93 EnumArrayReference enum_array_reference;
95 // Test Populate.
96 ASSERT_TRUE(EnumArrayReference::Populate(value, &enum_array_reference));
98 Enumeration expected_types[] = {ENUMERATION_ONE, ENUMERATION_TWO,
99 ENUMERATION_THREE};
100 EXPECT_EQ(std::vector<Enumeration>(
101 expected_types, expected_types + arraysize(expected_types)),
102 enum_array_reference.types);
104 // Test ToValue.
105 scoped_ptr<base::Value> as_value(enum_array_reference.ToValue());
106 EXPECT_TRUE(value.Equals(as_value.get())) << value << " != " << *as_value;
109 TEST(JsonSchemaCompilerArrayTest, EnumArrayMixed) {
110 // { "types": ["one", "two", "three"] }
111 base::ListValue* inline_enums = new base::ListValue();
112 inline_enums->AppendString("one");
113 inline_enums->AppendString("two");
114 inline_enums->AppendString("three");
116 base::ListValue* infile_enums = new base::ListValue();
117 infile_enums->AppendString("one");
118 infile_enums->AppendString("two");
119 infile_enums->AppendString("three");
121 base::ListValue* external_enums = new base::ListValue();
122 external_enums->AppendString("one");
123 external_enums->AppendString("two");
124 external_enums->AppendString("three");
126 base::DictionaryValue value;
127 value.Set("inline_enums", inline_enums);
128 value.Set("infile_enums", infile_enums);
129 value.Set("external_enums", external_enums);
131 EnumArrayMixed enum_array_mixed;
133 // Test Populate.
134 ASSERT_TRUE(EnumArrayMixed::Populate(value, &enum_array_mixed));
136 EnumArrayMixed::Inline_enumsType expected_inline_types[] = {
137 EnumArrayMixed::INLINE_ENUMS_TYPE_ONE,
138 EnumArrayMixed::INLINE_ENUMS_TYPE_TWO,
139 EnumArrayMixed::INLINE_ENUMS_TYPE_THREE};
140 EXPECT_EQ(std::vector<EnumArrayMixed::Inline_enumsType>(
141 expected_inline_types,
142 expected_inline_types + arraysize(expected_inline_types)),
143 enum_array_mixed.inline_enums);
145 Enumeration expected_infile_types[] = {ENUMERATION_ONE, ENUMERATION_TWO,
146 ENUMERATION_THREE};
147 EXPECT_EQ(std::vector<Enumeration>(
148 expected_infile_types,
149 expected_infile_types + arraysize(expected_infile_types)),
150 enum_array_mixed.infile_enums);
152 test::api::enums::Enumeration expected_external_types[] = {
153 test::api::enums::ENUMERATION_ONE, test::api::enums::ENUMERATION_TWO,
154 test::api::enums::ENUMERATION_THREE};
155 EXPECT_EQ(std::vector<test::api::enums::Enumeration>(
156 expected_external_types,
157 expected_external_types + arraysize(expected_external_types)),
158 enum_array_mixed.external_enums);
160 // Test ToValue.
161 scoped_ptr<base::Value> as_value(enum_array_mixed.ToValue());
162 EXPECT_TRUE(value.Equals(as_value.get())) << value << " != " << *as_value;
165 TEST(JsonSchemaCompilerArrayTest, OptionalEnumArrayType) {
167 std::vector<OptionalEnumArrayType::TypesType> enums;
168 enums.push_back(OptionalEnumArrayType::TYPES_TYPE_ONE);
169 enums.push_back(OptionalEnumArrayType::TYPES_TYPE_TWO);
170 enums.push_back(OptionalEnumArrayType::TYPES_TYPE_THREE);
172 scoped_ptr<base::ListValue> types(new base::ListValue());
173 for (size_t i = 0; i < enums.size(); ++i) {
174 types->Append(new base::StringValue(
175 OptionalEnumArrayType::ToString(enums[i])));
178 base::DictionaryValue value;
179 value.Set("types", types.release());
181 OptionalEnumArrayType enum_array_type;
182 ASSERT_TRUE(OptionalEnumArrayType::Populate(value, &enum_array_type));
183 EXPECT_EQ(enums, *enum_array_type.types);
186 base::DictionaryValue value;
187 scoped_ptr<base::ListValue> enum_array(new base::ListValue());
188 enum_array->Append(new base::StringValue("invalid"));
190 value.Set("types", enum_array.release());
191 OptionalEnumArrayType enum_array_type;
192 ASSERT_FALSE(OptionalEnumArrayType::Populate(value, &enum_array_type));
193 EXPECT_TRUE(enum_array_type.types->empty());
197 TEST(JsonSchemaCompilerArrayTest, RefArrayType) {
199 scoped_ptr<base::DictionaryValue> value(new base::DictionaryValue());
200 scoped_ptr<base::ListValue> ref_array(new base::ListValue());
201 ref_array->Append(CreateItemValue(1));
202 ref_array->Append(CreateItemValue(2));
203 ref_array->Append(CreateItemValue(3));
204 value->Set("refs", ref_array.release());
205 scoped_ptr<RefArrayType> ref_array_type(new RefArrayType());
206 EXPECT_TRUE(RefArrayType::Populate(*value, ref_array_type.get()));
207 ASSERT_EQ(3u, ref_array_type->refs.size());
208 EXPECT_EQ(1, ref_array_type->refs[0]->val);
209 EXPECT_EQ(2, ref_array_type->refs[1]->val);
210 EXPECT_EQ(3, ref_array_type->refs[2]->val);
213 scoped_ptr<base::DictionaryValue> value(new base::DictionaryValue());
214 scoped_ptr<base::ListValue> not_ref_array(new base::ListValue());
215 not_ref_array->Append(CreateItemValue(1));
216 not_ref_array->Append(new base::FundamentalValue(3));
217 value->Set("refs", not_ref_array.release());
218 scoped_ptr<RefArrayType> ref_array_type(new RefArrayType());
219 EXPECT_FALSE(RefArrayType::Populate(*value, ref_array_type.get()));
223 TEST(JsonSchemaCompilerArrayTest, IntegerArrayParamsCreate) {
224 scoped_ptr<base::ListValue> params_value(new base::ListValue());
225 scoped_ptr<base::ListValue> integer_array(new base::ListValue());
226 integer_array->Append(new base::FundamentalValue(2));
227 integer_array->Append(new base::FundamentalValue(4));
228 integer_array->Append(new base::FundamentalValue(8));
229 params_value->Append(integer_array.release());
230 scoped_ptr<IntegerArray::Params> params(
231 IntegerArray::Params::Create(*params_value));
232 EXPECT_TRUE(params.get());
233 ASSERT_EQ(3u, params->nums.size());
234 EXPECT_EQ(2, params->nums[0]);
235 EXPECT_EQ(4, params->nums[1]);
236 EXPECT_EQ(8, params->nums[2]);
239 TEST(JsonSchemaCompilerArrayTest, AnyArrayParamsCreate) {
240 scoped_ptr<base::ListValue> params_value(new base::ListValue());
241 scoped_ptr<base::ListValue> any_array(new base::ListValue());
242 any_array->Append(new base::FundamentalValue(1));
243 any_array->Append(new base::StringValue("test"));
244 any_array->Append(CreateItemValue(2));
245 params_value->Append(any_array.release());
246 scoped_ptr<AnyArray::Params> params(
247 AnyArray::Params::Create(*params_value));
248 EXPECT_TRUE(params.get());
249 ASSERT_EQ(3u, params->anys.size());
250 int int_temp = 0;
251 EXPECT_TRUE(params->anys[0]->GetAsInteger(&int_temp));
252 EXPECT_EQ(1, int_temp);
255 TEST(JsonSchemaCompilerArrayTest, ObjectArrayParamsCreate) {
256 scoped_ptr<base::ListValue> params_value(new base::ListValue());
257 scoped_ptr<base::ListValue> item_array(new base::ListValue());
258 item_array->Append(CreateItemValue(1));
259 item_array->Append(CreateItemValue(2));
260 params_value->Append(item_array.release());
261 scoped_ptr<ObjectArray::Params> params(
262 ObjectArray::Params::Create(*params_value));
263 EXPECT_TRUE(params.get());
264 ASSERT_EQ(2u, params->objects.size());
265 EXPECT_EQ(1, params->objects[0]->additional_properties["val"]);
266 EXPECT_EQ(2, params->objects[1]->additional_properties["val"]);
269 TEST(JsonSchemaCompilerArrayTest, RefArrayParamsCreate) {
270 scoped_ptr<base::ListValue> params_value(new base::ListValue());
271 scoped_ptr<base::ListValue> item_array(new base::ListValue());
272 item_array->Append(CreateItemValue(1));
273 item_array->Append(CreateItemValue(2));
274 params_value->Append(item_array.release());
275 scoped_ptr<RefArray::Params> params(
276 RefArray::Params::Create(*params_value));
277 EXPECT_TRUE(params.get());
278 ASSERT_EQ(2u, params->refs.size());
279 EXPECT_EQ(1, params->refs[0]->val);
280 EXPECT_EQ(2, params->refs[1]->val);
283 TEST(JsonSchemaCompilerArrayTest, ReturnIntegerArrayResultCreate) {
284 std::vector<int> integers;
285 integers.push_back(1);
286 integers.push_back(2);
287 scoped_ptr<base::ListValue> results =
288 ReturnIntegerArray::Results::Create(integers);
290 base::ListValue expected;
291 base::ListValue* expected_argument = new base::ListValue();
292 expected_argument->Append(new base::FundamentalValue(1));
293 expected_argument->Append(new base::FundamentalValue(2));
294 expected.Append(expected_argument);
295 EXPECT_TRUE(results->Equals(&expected));
298 TEST(JsonSchemaCompilerArrayTest, ReturnRefArrayResultCreate) {
299 std::vector<linked_ptr<Item> > items;
300 items.push_back(linked_ptr<Item>(new Item()));
301 items.push_back(linked_ptr<Item>(new Item()));
302 items[0]->val = 1;
303 items[1]->val = 2;
304 scoped_ptr<base::ListValue> results =
305 ReturnRefArray::Results::Create(items);
307 base::ListValue expected;
308 base::ListValue* expected_argument = new base::ListValue();
309 base::DictionaryValue* first = new base::DictionaryValue();
310 first->SetInteger("val", 1);
311 expected_argument->Append(first);
312 base::DictionaryValue* second = new base::DictionaryValue();
313 second->SetInteger("val", 2);
314 expected_argument->Append(second);
315 expected.Append(expected_argument);
316 EXPECT_TRUE(results->Equals(&expected));