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 DictionaryValue value
;
17 value
.Set("type", Value::CreateStringValue("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 DictionaryValue value
;
25 value
.Set("type", Value::CreateStringValue("invalid"));
26 EXPECT_FALSE(EnumType::Populate(value
, &enum_type
));
30 TEST(JsonSchemaCompilerEnumsTest
, EnumsAsTypes
) {
33 args
.Append(Value::CreateStringValue("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 DictionaryValue value
;
46 ASSERT_FALSE(HasEnumeration::Populate(value
, &enumeration
));
48 value
.Set("enumeration", Value::CreateStringValue("one"));
49 ASSERT_TRUE(HasEnumeration::Populate(value
, &enumeration
));
50 EXPECT_TRUE(value
.Equals(enumeration
.ToValue().get()));
52 value
.Set("optional_enumeration", Value::CreateStringValue("two"));
53 ASSERT_TRUE(HasEnumeration::Populate(value
, &enumeration
));
54 EXPECT_TRUE(value
.Equals(enumeration
.ToValue().get()));
58 TEST(JsonSchemaCompilerEnumsTest
, EnumsArrayAsType
) {
60 ListValue params_value
;
61 params_value
.Append(List(Value::CreateStringValue("one"),
62 Value::CreateStringValue("two")).release());
63 scoped_ptr
<TakesEnumArrayAsType::Params
> params(
64 TakesEnumArrayAsType::Params::Create(params_value
));
66 EXPECT_EQ(2U, params
->values
.size());
67 EXPECT_EQ(ENUMERATION_ONE
, params
->values
[0]);
68 EXPECT_EQ(ENUMERATION_TWO
, params
->values
[1]);
71 ListValue params_value
;
72 params_value
.Append(List(Value::CreateStringValue("invalid")).release());
73 scoped_ptr
<TakesEnumArrayAsType::Params
> params(
74 TakesEnumArrayAsType::Params::Create(params_value
));
79 TEST(JsonSchemaCompilerEnumsTest
, ReturnsEnumCreate
) {
81 ReturnsEnum::Results::State state
= ReturnsEnum::Results::STATE_FOO
;
82 scoped_ptr
<Value
> result(
83 new base::StringValue(ReturnsEnum::Results::ToString(state
)));
84 scoped_ptr
<Value
> expected(Value::CreateStringValue("foo"));
85 EXPECT_TRUE(result
->Equals(expected
.get()));
88 ReturnsEnum::Results::State state
= ReturnsEnum::Results::STATE_FOO
;
89 scoped_ptr
<ListValue
> results
= ReturnsEnum::Results::Create(state
);
91 expected
.Append(Value::CreateStringValue("foo"));
92 EXPECT_TRUE(results
->Equals(&expected
));
96 TEST(JsonSchemaCompilerEnumsTest
, ReturnsTwoEnumsCreate
) {
98 scoped_ptr
<ListValue
> results
= ReturnsTwoEnums::Results::Create(
99 ReturnsTwoEnums::Results::FIRST_STATE_FOO
,
100 ReturnsTwoEnums::Results::SECOND_STATE_HAM
);
102 expected
.Append(Value::CreateStringValue("foo"));
103 expected
.Append(Value::CreateStringValue("ham"));
104 EXPECT_TRUE(results
->Equals(&expected
));
108 TEST(JsonSchemaCompilerEnumsTest
, OptionalEnumTypePopulate
) {
110 OptionalEnumType enum_type
;
111 DictionaryValue value
;
112 value
.Set("type", Value::CreateStringValue("two"));
113 EXPECT_TRUE(OptionalEnumType::Populate(value
, &enum_type
));
114 EXPECT_EQ(OptionalEnumType::TYPE_TWO
, enum_type
.type
);
115 EXPECT_TRUE(value
.Equals(enum_type
.ToValue().get()));
118 OptionalEnumType enum_type
;
119 DictionaryValue value
;
120 EXPECT_TRUE(OptionalEnumType::Populate(value
, &enum_type
));
121 EXPECT_EQ(OptionalEnumType::TYPE_NONE
, enum_type
.type
);
122 EXPECT_TRUE(value
.Equals(enum_type
.ToValue().get()));
125 OptionalEnumType enum_type
;
126 DictionaryValue value
;
127 value
.Set("type", Value::CreateStringValue("invalid"));
128 EXPECT_FALSE(OptionalEnumType::Populate(value
, &enum_type
));
132 TEST(JsonSchemaCompilerEnumsTest
, TakesEnumParamsCreate
) {
134 ListValue params_value
;
135 params_value
.Append(Value::CreateStringValue("baz"));
136 scoped_ptr
<TakesEnum::Params
> params(
137 TakesEnum::Params::Create(params_value
));
138 EXPECT_TRUE(params
.get());
139 EXPECT_EQ(TakesEnum::Params::STATE_BAZ
, params
->state
);
142 ListValue params_value
;
143 params_value
.Append(Value::CreateStringValue("invalid"));
144 scoped_ptr
<TakesEnum::Params
> params(
145 TakesEnum::Params::Create(params_value
));
146 EXPECT_FALSE(params
.get());
150 TEST(JsonSchemaCompilerEnumsTest
, TakesEnumArrayParamsCreate
) {
152 ListValue params_value
;
153 params_value
.Append(List(Value::CreateStringValue("foo"),
154 Value::CreateStringValue("bar")).release());
155 scoped_ptr
<TakesEnumArray::Params
> params(
156 TakesEnumArray::Params::Create(params_value
));
158 EXPECT_EQ(2U, params
->values
.size());
159 EXPECT_EQ(TakesEnumArray::Params::VALUES_TYPE_FOO
, params
->values
[0]);
160 EXPECT_EQ(TakesEnumArray::Params::VALUES_TYPE_BAR
, params
->values
[1]);
163 ListValue params_value
;
164 params_value
.Append(List(Value::CreateStringValue("invalid")).release());
165 scoped_ptr
<TakesEnumArray::Params
> params(
166 TakesEnumArray::Params::Create(params_value
));
167 EXPECT_FALSE(params
);
171 TEST(JsonSchemaCompilerEnumsTest
, TakesOptionalEnumParamsCreate
) {
173 ListValue params_value
;
174 params_value
.Append(Value::CreateStringValue("baz"));
175 scoped_ptr
<TakesOptionalEnum::Params
> params(
176 TakesOptionalEnum::Params::Create(params_value
));
177 EXPECT_TRUE(params
.get());
178 EXPECT_EQ(TakesOptionalEnum::Params::STATE_BAZ
, params
->state
);
181 ListValue params_value
;
182 scoped_ptr
<TakesOptionalEnum::Params
> params(
183 TakesOptionalEnum::Params::Create(params_value
));
184 EXPECT_TRUE(params
.get());
185 EXPECT_EQ(TakesOptionalEnum::Params::STATE_NONE
, params
->state
);
188 ListValue params_value
;
189 params_value
.Append(Value::CreateStringValue("invalid"));
190 scoped_ptr
<TakesOptionalEnum::Params
> params(
191 TakesOptionalEnum::Params::Create(params_value
));
192 EXPECT_FALSE(params
.get());
196 TEST(JsonSchemaCompilerEnumsTest
, TakesMultipleOptionalEnumsParamsCreate
) {
198 ListValue params_value
;
199 params_value
.Append(Value::CreateStringValue("foo"));
200 params_value
.Append(Value::CreateStringValue("foo"));
201 scoped_ptr
<TakesMultipleOptionalEnums::Params
> params(
202 TakesMultipleOptionalEnums::Params::Create(params_value
));
203 EXPECT_TRUE(params
.get());
204 EXPECT_EQ(TakesMultipleOptionalEnums::Params::STATE_FOO
, params
->state
);
205 EXPECT_EQ(TakesMultipleOptionalEnums::Params::TYPE_FOO
, params
->type
);
208 ListValue params_value
;
209 params_value
.Append(Value::CreateStringValue("foo"));
210 scoped_ptr
<TakesMultipleOptionalEnums::Params
> params(
211 TakesMultipleOptionalEnums::Params::Create(params_value
));
212 EXPECT_TRUE(params
.get());
213 EXPECT_EQ(TakesMultipleOptionalEnums::Params::STATE_FOO
, params
->state
);
214 EXPECT_EQ(TakesMultipleOptionalEnums::Params::TYPE_NONE
, params
->type
);
217 ListValue params_value
;
218 scoped_ptr
<TakesMultipleOptionalEnums::Params
> params(
219 TakesMultipleOptionalEnums::Params::Create(params_value
));
220 EXPECT_TRUE(params
.get());
221 EXPECT_EQ(TakesMultipleOptionalEnums::Params::STATE_NONE
, params
->state
);
222 EXPECT_EQ(TakesMultipleOptionalEnums::Params::TYPE_NONE
, params
->type
);
225 ListValue params_value
;
226 params_value
.Append(Value::CreateStringValue("baz"));
227 params_value
.Append(Value::CreateStringValue("invalid"));
228 scoped_ptr
<TakesMultipleOptionalEnums::Params
> params(
229 TakesMultipleOptionalEnums::Params::Create(params_value
));
230 EXPECT_FALSE(params
.get());
234 TEST(JsonSchemaCompilerEnumsTest
, OnEnumFiredCreate
) {
236 OnEnumFired::SomeEnum some_enum
= OnEnumFired::SOME_ENUM_FOO
;
237 scoped_ptr
<Value
> result(
238 new base::StringValue(OnEnumFired::ToString(some_enum
)));
239 scoped_ptr
<Value
> expected(Value::CreateStringValue("foo"));
240 EXPECT_TRUE(result
->Equals(expected
.get()));
243 OnEnumFired::SomeEnum some_enum
= OnEnumFired::SOME_ENUM_FOO
;
244 scoped_ptr
<ListValue
> results(OnEnumFired::Create(some_enum
));
246 expected
.Append(Value::CreateStringValue("foo"));
247 EXPECT_TRUE(results
->Equals(&expected
));
251 TEST(JsonSchemaCompilerEnumsTest
, OnTwoEnumsFiredCreate
) {
253 scoped_ptr
<Value
> results(OnTwoEnumsFired::Create(
254 OnTwoEnumsFired::FIRST_ENUM_FOO
,
255 OnTwoEnumsFired::SECOND_ENUM_HAM
));
257 expected
.Append(Value::CreateStringValue("foo"));
258 expected
.Append(Value::CreateStringValue("ham"));
259 EXPECT_TRUE(results
->Equals(&expected
));