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/enums.h"
7 #include "testing/gtest/include/gtest/gtest.h"
8 #include "tools/json_schema_compiler/test/test_util.h"
10 using namespace test::api::enums
;
11 using json_schema_compiler::test_util::List
;
13 TEST(JsonSchemaCompilerEnumsTest
, EnumTypePopulate
) {
16 base::DictionaryValue value
;
17 value
.Set("type", new base::StringValue("one"));
18 EXPECT_TRUE(EnumType::Populate(value
, &enum_type
));
19 EXPECT_EQ(ENUMERATION_ONE
, enum_type
.type
);
20 EXPECT_TRUE(value
.Equals(enum_type
.ToValue().get()));
24 base::DictionaryValue value
;
25 value
.Set("type", new base::StringValue("invalid"));
26 EXPECT_FALSE(EnumType::Populate(value
, &enum_type
));
30 TEST(JsonSchemaCompilerEnumsTest
, EnumsAsTypes
) {
33 args
.Append(new base::StringValue("one"));
35 scoped_ptr
<TakesEnumAsType::Params
> params(
36 TakesEnumAsType::Params::Create(args
));
37 ASSERT_TRUE(params
.get());
38 EXPECT_EQ(ENUMERATION_ONE
, params
->enumeration
);
40 EXPECT_TRUE(args
.Equals(ReturnsEnumAsType::Results::Create(
41 ENUMERATION_ONE
).get()));
44 HasEnumeration enumeration
;
45 EXPECT_EQ(ENUMERATION_NONE
, enumeration
.enumeration
);
46 EXPECT_EQ(ENUMERATION_NONE
, enumeration
.optional_enumeration
);
49 HasEnumeration enumeration
;
50 base::DictionaryValue value
;
51 ASSERT_FALSE(HasEnumeration::Populate(value
, &enumeration
));
53 value
.Set("enumeration", new base::StringValue("one"));
54 ASSERT_TRUE(HasEnumeration::Populate(value
, &enumeration
));
55 EXPECT_TRUE(value
.Equals(enumeration
.ToValue().get()));
57 value
.Set("optional_enumeration", new base::StringValue("two"));
58 ASSERT_TRUE(HasEnumeration::Populate(value
, &enumeration
));
59 EXPECT_TRUE(value
.Equals(enumeration
.ToValue().get()));
62 ReferenceEnum enumeration
;
63 base::DictionaryValue value
;
64 ASSERT_FALSE(ReferenceEnum::Populate(value
, &enumeration
));
66 value
.Set("reference_enum", new base::StringValue("one"));
67 ASSERT_TRUE(ReferenceEnum::Populate(value
, &enumeration
));
68 EXPECT_TRUE(value
.Equals(enumeration
.ToValue().get()));
72 TEST(JsonSchemaCompilerEnumsTest
, EnumsArrayAsType
) {
74 base::ListValue params_value
;
75 params_value
.Append(List(new base::StringValue("one"),
76 new base::StringValue("two")).release());
77 scoped_ptr
<TakesEnumArrayAsType::Params
> params(
78 TakesEnumArrayAsType::Params::Create(params_value
));
80 EXPECT_EQ(2U, params
->values
.size());
81 EXPECT_EQ(ENUMERATION_ONE
, params
->values
[0]);
82 EXPECT_EQ(ENUMERATION_TWO
, params
->values
[1]);
85 base::ListValue params_value
;
86 params_value
.Append(List(new base::StringValue("invalid")).release());
87 scoped_ptr
<TakesEnumArrayAsType::Params
> params(
88 TakesEnumArrayAsType::Params::Create(params_value
));
93 TEST(JsonSchemaCompilerEnumsTest
, ReturnsEnumCreate
) {
95 Enumeration state
= ENUMERATION_ONE
;
96 scoped_ptr
<base::Value
> result(new base::StringValue(ToString(state
)));
97 scoped_ptr
<base::Value
> expected(new base::StringValue("one"));
98 EXPECT_TRUE(result
->Equals(expected
.get()));
101 Enumeration state
= ENUMERATION_ONE
;
102 scoped_ptr
<base::ListValue
> results
= ReturnsEnum::Results::Create(state
);
103 base::ListValue expected
;
104 expected
.Append(new base::StringValue("one"));
105 EXPECT_TRUE(results
->Equals(&expected
));
109 TEST(JsonSchemaCompilerEnumsTest
, ReturnsTwoEnumsCreate
) {
111 scoped_ptr
<base::ListValue
> results
= ReturnsTwoEnums::Results::Create(
112 ENUMERATION_ONE
, OTHER_ENUMERATION_HAM
);
113 base::ListValue expected
;
114 expected
.Append(new base::StringValue("one"));
115 expected
.Append(new base::StringValue("ham"));
116 EXPECT_TRUE(results
->Equals(&expected
));
120 TEST(JsonSchemaCompilerEnumsTest
, OptionalEnumTypePopulate
) {
122 OptionalEnumType enum_type
;
123 base::DictionaryValue value
;
124 value
.Set("type", new base::StringValue("two"));
125 EXPECT_TRUE(OptionalEnumType::Populate(value
, &enum_type
));
126 EXPECT_EQ(ENUMERATION_TWO
, enum_type
.type
);
127 EXPECT_TRUE(value
.Equals(enum_type
.ToValue().get()));
130 OptionalEnumType enum_type
;
131 base::DictionaryValue value
;
132 EXPECT_TRUE(OptionalEnumType::Populate(value
, &enum_type
));
133 EXPECT_EQ(ENUMERATION_NONE
, enum_type
.type
);
134 EXPECT_TRUE(value
.Equals(enum_type
.ToValue().get()));
137 OptionalEnumType enum_type
;
138 base::DictionaryValue value
;
139 value
.Set("type", new base::StringValue("invalid"));
140 EXPECT_FALSE(OptionalEnumType::Populate(value
, &enum_type
));
144 TEST(JsonSchemaCompilerEnumsTest
, TakesEnumParamsCreate
) {
146 base::ListValue params_value
;
147 params_value
.Append(new base::StringValue("two"));
148 scoped_ptr
<TakesEnum::Params
> params(
149 TakesEnum::Params::Create(params_value
));
150 EXPECT_TRUE(params
.get());
151 EXPECT_EQ(ENUMERATION_TWO
, params
->state
);
154 base::ListValue params_value
;
155 params_value
.Append(new base::StringValue("invalid"));
156 scoped_ptr
<TakesEnum::Params
> params(
157 TakesEnum::Params::Create(params_value
));
158 EXPECT_FALSE(params
.get());
162 TEST(JsonSchemaCompilerEnumsTest
, TakesEnumArrayParamsCreate
) {
164 base::ListValue params_value
;
165 params_value
.Append(List(new base::StringValue("one"),
166 new base::StringValue("two")).release());
167 scoped_ptr
<TakesEnumArray::Params
> params(
168 TakesEnumArray::Params::Create(params_value
));
170 EXPECT_EQ(2U, params
->values
.size());
171 EXPECT_EQ(ENUMERATION_ONE
, params
->values
[0]);
172 EXPECT_EQ(ENUMERATION_TWO
, params
->values
[1]);
175 base::ListValue params_value
;
176 params_value
.Append(List(new base::StringValue("invalid")).release());
177 scoped_ptr
<TakesEnumArray::Params
> params(
178 TakesEnumArray::Params::Create(params_value
));
179 EXPECT_FALSE(params
);
183 TEST(JsonSchemaCompilerEnumsTest
, TakesOptionalEnumParamsCreate
) {
185 base::ListValue params_value
;
186 params_value
.Append(new base::StringValue("three"));
187 scoped_ptr
<TakesOptionalEnum::Params
> params(
188 TakesOptionalEnum::Params::Create(params_value
));
189 EXPECT_TRUE(params
.get());
190 EXPECT_EQ(ENUMERATION_THREE
, params
->state
);
193 base::ListValue params_value
;
194 scoped_ptr
<TakesOptionalEnum::Params
> params(
195 TakesOptionalEnum::Params::Create(params_value
));
196 EXPECT_TRUE(params
.get());
197 EXPECT_EQ(ENUMERATION_NONE
, params
->state
);
200 base::ListValue params_value
;
201 params_value
.Append(new base::StringValue("invalid"));
202 scoped_ptr
<TakesOptionalEnum::Params
> params(
203 TakesOptionalEnum::Params::Create(params_value
));
204 EXPECT_FALSE(params
.get());
208 TEST(JsonSchemaCompilerEnumsTest
, TakesMultipleOptionalEnumsParamsCreate
) {
210 base::ListValue params_value
;
211 params_value
.Append(new base::StringValue("one"));
212 params_value
.Append(new base::StringValue("ham"));
213 scoped_ptr
<TakesMultipleOptionalEnums::Params
> params(
214 TakesMultipleOptionalEnums::Params::Create(params_value
));
215 EXPECT_TRUE(params
.get());
216 EXPECT_EQ(ENUMERATION_ONE
, params
->state
);
217 EXPECT_EQ(OTHER_ENUMERATION_HAM
, params
->type
);
220 base::ListValue params_value
;
221 params_value
.Append(new base::StringValue("one"));
222 scoped_ptr
<TakesMultipleOptionalEnums::Params
> params(
223 TakesMultipleOptionalEnums::Params::Create(params_value
));
224 EXPECT_TRUE(params
.get());
225 EXPECT_EQ(ENUMERATION_ONE
, params
->state
);
226 EXPECT_EQ(OTHER_ENUMERATION_NONE
, params
->type
);
229 base::ListValue params_value
;
230 scoped_ptr
<TakesMultipleOptionalEnums::Params
> params(
231 TakesMultipleOptionalEnums::Params::Create(params_value
));
232 EXPECT_TRUE(params
.get());
233 EXPECT_EQ(ENUMERATION_NONE
, params
->state
);
234 EXPECT_EQ(OTHER_ENUMERATION_NONE
, params
->type
);
237 base::ListValue params_value
;
238 params_value
.Append(new base::StringValue("three"));
239 params_value
.Append(new base::StringValue("invalid"));
240 scoped_ptr
<TakesMultipleOptionalEnums::Params
> params(
241 TakesMultipleOptionalEnums::Params::Create(params_value
));
242 EXPECT_FALSE(params
.get());
246 TEST(JsonSchemaCompilerEnumsTest
, OnEnumFiredCreate
) {
248 Enumeration some_enum
= ENUMERATION_ONE
;
249 scoped_ptr
<base::Value
> result(new base::StringValue(ToString(some_enum
)));
250 scoped_ptr
<base::Value
> expected(new base::StringValue("one"));
251 EXPECT_TRUE(result
->Equals(expected
.get()));
254 Enumeration some_enum
= ENUMERATION_ONE
;
255 scoped_ptr
<base::ListValue
> results(OnEnumFired::Create(some_enum
));
256 base::ListValue expected
;
257 expected
.Append(new base::StringValue("one"));
258 EXPECT_TRUE(results
->Equals(&expected
));
262 TEST(JsonSchemaCompilerEnumsTest
, OnTwoEnumsFiredCreate
) {
264 scoped_ptr
<base::Value
> results(
265 OnTwoEnumsFired::Create(ENUMERATION_ONE
, OTHER_ENUMERATION_HAM
));
266 base::ListValue expected
;
267 expected
.Append(new base::StringValue("one"));
268 expected
.Append(new base::StringValue("ham"));
269 EXPECT_TRUE(results
->Equals(&expected
));