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(EnumType::TYPE_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 InlineAndReferenceEnum enumeration
;
63 base::DictionaryValue value
;
64 ASSERT_FALSE(InlineAndReferenceEnum::Populate(value
, &enumeration
));
66 value
.Set("inline_enum", new base::StringValue("test2"));
67 ASSERT_FALSE(InlineAndReferenceEnum::Populate(value
, &enumeration
));
69 value
.Set("reference_enum", new base::StringValue("one"));
70 ASSERT_TRUE(InlineAndReferenceEnum::Populate(value
, &enumeration
));
71 EXPECT_TRUE(value
.Equals(enumeration
.ToValue().get()));
75 TEST(JsonSchemaCompilerEnumsTest
, EnumsArrayAsType
) {
77 base::ListValue params_value
;
78 params_value
.Append(List(new base::StringValue("one"),
79 new base::StringValue("two")).release());
80 scoped_ptr
<TakesEnumArrayAsType::Params
> params(
81 TakesEnumArrayAsType::Params::Create(params_value
));
83 EXPECT_EQ(2U, params
->values
.size());
84 EXPECT_EQ(ENUMERATION_ONE
, params
->values
[0]);
85 EXPECT_EQ(ENUMERATION_TWO
, params
->values
[1]);
88 base::ListValue params_value
;
89 params_value
.Append(List(new base::StringValue("invalid")).release());
90 scoped_ptr
<TakesEnumArrayAsType::Params
> params(
91 TakesEnumArrayAsType::Params::Create(params_value
));
96 TEST(JsonSchemaCompilerEnumsTest
, ReturnsEnumCreate
) {
98 ReturnsEnum::Results::State state
= ReturnsEnum::Results::STATE_FOO
;
99 scoped_ptr
<base::Value
> result(
100 new base::StringValue(ReturnsEnum::Results::ToString(state
)));
101 scoped_ptr
<base::Value
> expected(new base::StringValue("foo"));
102 EXPECT_TRUE(result
->Equals(expected
.get()));
105 ReturnsEnum::Results::State state
= ReturnsEnum::Results::STATE_FOO
;
106 scoped_ptr
<base::ListValue
> results
= ReturnsEnum::Results::Create(state
);
107 base::ListValue expected
;
108 expected
.Append(new base::StringValue("foo"));
109 EXPECT_TRUE(results
->Equals(&expected
));
113 TEST(JsonSchemaCompilerEnumsTest
, ReturnsTwoEnumsCreate
) {
115 scoped_ptr
<base::ListValue
> results
= ReturnsTwoEnums::Results::Create(
116 ReturnsTwoEnums::Results::FIRST_STATE_FOO
,
117 ReturnsTwoEnums::Results::SECOND_STATE_HAM
);
118 base::ListValue expected
;
119 expected
.Append(new base::StringValue("foo"));
120 expected
.Append(new base::StringValue("ham"));
121 EXPECT_TRUE(results
->Equals(&expected
));
125 TEST(JsonSchemaCompilerEnumsTest
, OptionalEnumTypePopulate
) {
127 OptionalEnumType enum_type
;
128 base::DictionaryValue value
;
129 value
.Set("type", new base::StringValue("two"));
130 EXPECT_TRUE(OptionalEnumType::Populate(value
, &enum_type
));
131 EXPECT_EQ(OptionalEnumType::TYPE_TWO
, enum_type
.type
);
132 EXPECT_TRUE(value
.Equals(enum_type
.ToValue().get()));
135 OptionalEnumType enum_type
;
136 base::DictionaryValue value
;
137 EXPECT_TRUE(OptionalEnumType::Populate(value
, &enum_type
));
138 EXPECT_EQ(OptionalEnumType::TYPE_NONE
, enum_type
.type
);
139 EXPECT_TRUE(value
.Equals(enum_type
.ToValue().get()));
142 OptionalEnumType enum_type
;
143 base::DictionaryValue value
;
144 value
.Set("type", new base::StringValue("invalid"));
145 EXPECT_FALSE(OptionalEnumType::Populate(value
, &enum_type
));
149 TEST(JsonSchemaCompilerEnumsTest
, TakesEnumParamsCreate
) {
151 base::ListValue params_value
;
152 params_value
.Append(new base::StringValue("baz"));
153 scoped_ptr
<TakesEnum::Params
> params(
154 TakesEnum::Params::Create(params_value
));
155 EXPECT_TRUE(params
.get());
156 EXPECT_EQ(TakesEnum::Params::STATE_BAZ
, params
->state
);
159 base::ListValue params_value
;
160 params_value
.Append(new base::StringValue("invalid"));
161 scoped_ptr
<TakesEnum::Params
> params(
162 TakesEnum::Params::Create(params_value
));
163 EXPECT_FALSE(params
.get());
167 TEST(JsonSchemaCompilerEnumsTest
, TakesEnumArrayParamsCreate
) {
169 base::ListValue params_value
;
170 params_value
.Append(List(new base::StringValue("foo"),
171 new base::StringValue("bar")).release());
172 scoped_ptr
<TakesEnumArray::Params
> params(
173 TakesEnumArray::Params::Create(params_value
));
175 EXPECT_EQ(2U, params
->values
.size());
176 EXPECT_EQ(TakesEnumArray::Params::VALUES_TYPE_FOO
, params
->values
[0]);
177 EXPECT_EQ(TakesEnumArray::Params::VALUES_TYPE_BAR
, params
->values
[1]);
180 base::ListValue params_value
;
181 params_value
.Append(List(new base::StringValue("invalid")).release());
182 scoped_ptr
<TakesEnumArray::Params
> params(
183 TakesEnumArray::Params::Create(params_value
));
184 EXPECT_FALSE(params
);
188 TEST(JsonSchemaCompilerEnumsTest
, TakesOptionalEnumParamsCreate
) {
190 base::ListValue params_value
;
191 params_value
.Append(new base::StringValue("baz"));
192 scoped_ptr
<TakesOptionalEnum::Params
> params(
193 TakesOptionalEnum::Params::Create(params_value
));
194 EXPECT_TRUE(params
.get());
195 EXPECT_EQ(TakesOptionalEnum::Params::STATE_BAZ
, params
->state
);
198 base::ListValue params_value
;
199 scoped_ptr
<TakesOptionalEnum::Params
> params(
200 TakesOptionalEnum::Params::Create(params_value
));
201 EXPECT_TRUE(params
.get());
202 EXPECT_EQ(TakesOptionalEnum::Params::STATE_NONE
, params
->state
);
205 base::ListValue params_value
;
206 params_value
.Append(new base::StringValue("invalid"));
207 scoped_ptr
<TakesOptionalEnum::Params
> params(
208 TakesOptionalEnum::Params::Create(params_value
));
209 EXPECT_FALSE(params
.get());
213 TEST(JsonSchemaCompilerEnumsTest
, TakesMultipleOptionalEnumsParamsCreate
) {
215 base::ListValue params_value
;
216 params_value
.Append(new base::StringValue("foo"));
217 params_value
.Append(new base::StringValue("foo"));
218 scoped_ptr
<TakesMultipleOptionalEnums::Params
> params(
219 TakesMultipleOptionalEnums::Params::Create(params_value
));
220 EXPECT_TRUE(params
.get());
221 EXPECT_EQ(TakesMultipleOptionalEnums::Params::STATE_FOO
, params
->state
);
222 EXPECT_EQ(TakesMultipleOptionalEnums::Params::TYPE_FOO
, params
->type
);
225 base::ListValue params_value
;
226 params_value
.Append(new base::StringValue("foo"));
227 scoped_ptr
<TakesMultipleOptionalEnums::Params
> params(
228 TakesMultipleOptionalEnums::Params::Create(params_value
));
229 EXPECT_TRUE(params
.get());
230 EXPECT_EQ(TakesMultipleOptionalEnums::Params::STATE_FOO
, params
->state
);
231 EXPECT_EQ(TakesMultipleOptionalEnums::Params::TYPE_NONE
, params
->type
);
234 base::ListValue params_value
;
235 scoped_ptr
<TakesMultipleOptionalEnums::Params
> params(
236 TakesMultipleOptionalEnums::Params::Create(params_value
));
237 EXPECT_TRUE(params
.get());
238 EXPECT_EQ(TakesMultipleOptionalEnums::Params::STATE_NONE
, params
->state
);
239 EXPECT_EQ(TakesMultipleOptionalEnums::Params::TYPE_NONE
, params
->type
);
242 base::ListValue params_value
;
243 params_value
.Append(new base::StringValue("baz"));
244 params_value
.Append(new base::StringValue("invalid"));
245 scoped_ptr
<TakesMultipleOptionalEnums::Params
> params(
246 TakesMultipleOptionalEnums::Params::Create(params_value
));
247 EXPECT_FALSE(params
.get());
251 TEST(JsonSchemaCompilerEnumsTest
, OnEnumFiredCreate
) {
253 OnEnumFired::SomeEnum some_enum
= OnEnumFired::SOME_ENUM_FOO
;
254 scoped_ptr
<base::Value
> result(
255 new base::StringValue(OnEnumFired::ToString(some_enum
)));
256 scoped_ptr
<base::Value
> expected(new base::StringValue("foo"));
257 EXPECT_TRUE(result
->Equals(expected
.get()));
260 OnEnumFired::SomeEnum some_enum
= OnEnumFired::SOME_ENUM_FOO
;
261 scoped_ptr
<base::ListValue
> results(OnEnumFired::Create(some_enum
));
262 base::ListValue expected
;
263 expected
.Append(new base::StringValue("foo"));
264 EXPECT_TRUE(results
->Equals(&expected
));
268 TEST(JsonSchemaCompilerEnumsTest
, OnTwoEnumsFiredCreate
) {
270 scoped_ptr
<base::Value
> results(OnTwoEnumsFired::Create(
271 OnTwoEnumsFired::FIRST_ENUM_FOO
,
272 OnTwoEnumsFired::SECOND_ENUM_HAM
));
273 base::ListValue expected
;
274 expected
.Append(new base::StringValue("foo"));
275 expected
.Append(new base::StringValue("ham"));
276 EXPECT_TRUE(results
->Equals(&expected
));