Roll src/third_party/WebKit 6f84130:7353389 (svn 184386:184391)
[chromium-blink-merge.git] / tools / json_schema_compiler / test / choices_unittest.cc
blob2cf1d8d358f79ef75ef79e168cbed8d8c95ef633
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/choices.h"
7 #include "base/strings/string_piece.h"
8 #include "testing/gtest/include/gtest/gtest.h"
9 #include "tools/json_schema_compiler/test/test_util.h"
11 namespace {
13 using namespace test::api::choices;
14 using json_schema_compiler::test_util::Dictionary;
15 using json_schema_compiler::test_util::List;
16 using json_schema_compiler::test_util::ReadJson;
17 using json_schema_compiler::test_util::Vector;
19 TEST(JsonSchemaCompilerChoicesTest, TakesIntegersParamsCreate) {
21 scoped_ptr<TakesIntegers::Params> params(
22 TakesIntegers::Params::Create(*List(new base::FundamentalValue(true))));
23 EXPECT_FALSE(params);
26 scoped_ptr<TakesIntegers::Params> params(
27 TakesIntegers::Params::Create(*List(new base::FundamentalValue(6))));
28 ASSERT_TRUE(params);
29 EXPECT_FALSE(params->nums.as_integers);
30 EXPECT_EQ(6, *params->nums.as_integer);
33 scoped_ptr<TakesIntegers::Params> params(TakesIntegers::Params::Create(
34 *List(List(new base::FundamentalValue(2),
35 new base::FundamentalValue(6),
36 new base::FundamentalValue(8)).release())));
37 ASSERT_TRUE(params);
38 ASSERT_TRUE(params->nums.as_integers);
39 EXPECT_EQ(Vector(2, 6, 8), *params->nums.as_integers);
43 TEST(JsonSchemaCompilerChoicesTest, ObjectWithChoicesParamsCreate) {
45 scoped_ptr<ObjectWithChoices::Params> params(
46 ObjectWithChoices::Params::Create(*List(
47 Dictionary("strings", new base::StringValue("asdf")).release())));
48 ASSERT_TRUE(params);
49 EXPECT_FALSE(params->string_info.strings.as_strings);
50 EXPECT_EQ("asdf", *params->string_info.strings.as_string);
51 EXPECT_FALSE(params->string_info.integers);
54 scoped_ptr<ObjectWithChoices::Params> params(
55 ObjectWithChoices::Params::Create(*List(
56 Dictionary("strings", new base::StringValue("asdf"),
57 "integers", new base::FundamentalValue(6)).release())));
58 ASSERT_TRUE(params);
59 EXPECT_FALSE(params->string_info.strings.as_strings);
60 EXPECT_EQ("asdf", *params->string_info.strings.as_string);
61 ASSERT_TRUE(params->string_info.integers);
62 EXPECT_FALSE(params->string_info.integers->as_integers);
63 EXPECT_EQ(6, *params->string_info.integers->as_integer);
67 // TODO(kalman): Clean up the rest of these tests to use the
68 // Vector/List/Dictionary helpers.
70 TEST(JsonSchemaCompilerChoicesTest, ObjectWithChoicesParamsCreateFail) {
72 scoped_ptr<base::DictionaryValue> object_param(new base::DictionaryValue());
73 object_param->SetWithoutPathExpansion("strings",
74 new base::FundamentalValue(5));
75 scoped_ptr<base::ListValue> params_value(new base::ListValue());
76 params_value->Append(object_param.release());
77 scoped_ptr<ObjectWithChoices::Params> params(
78 ObjectWithChoices::Params::Create(*params_value));
79 EXPECT_FALSE(params.get());
82 scoped_ptr<base::DictionaryValue> object_param(new base::DictionaryValue());
83 object_param->SetWithoutPathExpansion("strings",
84 new base::StringValue("asdf"));
85 object_param->SetWithoutPathExpansion("integers",
86 new base::StringValue("asdf"));
87 scoped_ptr<base::ListValue> params_value(new base::ListValue());
88 params_value->Append(object_param.release());
89 scoped_ptr<ObjectWithChoices::Params> params(
90 ObjectWithChoices::Params::Create(*params_value));
91 EXPECT_FALSE(params.get());
94 scoped_ptr<base::DictionaryValue> object_param(new base::DictionaryValue());
95 object_param->SetWithoutPathExpansion("integers",
96 new base::FundamentalValue(6));
97 scoped_ptr<base::ListValue> params_value(new base::ListValue());
98 params_value->Append(object_param.release());
99 scoped_ptr<ObjectWithChoices::Params> params(
100 ObjectWithChoices::Params::Create(*params_value));
101 EXPECT_FALSE(params.get());
105 TEST(JsonSchemaCompilerChoicesTest, PopulateChoiceType) {
106 std::vector<std::string> strings = Vector(std::string("list"),
107 std::string("of"),
108 std::string("strings"));
110 base::ListValue* strings_value = new base::ListValue();
111 for (size_t i = 0; i < strings.size(); ++i)
112 strings_value->Append(new base::StringValue(strings[i]));
114 base::DictionaryValue value;
115 value.SetInteger("integers", 4);
116 value.Set("strings", strings_value);
118 ChoiceType out;
119 ASSERT_TRUE(ChoiceType::Populate(value, &out));
120 ASSERT_TRUE(out.integers.as_integer.get());
121 EXPECT_FALSE(out.integers.as_integers.get());
122 EXPECT_EQ(4, *out.integers.as_integer);
124 EXPECT_FALSE(out.strings->as_string.get());
125 ASSERT_TRUE(out.strings->as_strings.get());
126 EXPECT_EQ(strings, *out.strings->as_strings);
129 TEST(JsonSchemaCompilerChoicesTest, ChoiceTypeToValue) {
130 base::ListValue* strings_value = new base::ListValue();
131 strings_value->Append(new base::StringValue("list"));
132 strings_value->Append(new base::StringValue("of"));
133 strings_value->Append(new base::StringValue("strings"));
135 base::DictionaryValue value;
136 value.SetInteger("integers", 5);
137 value.Set("strings", strings_value);
139 ChoiceType out;
140 ASSERT_TRUE(ChoiceType::Populate(value, &out));
142 EXPECT_TRUE(value.Equals(out.ToValue().get()));
145 TEST(JsonSchemaCompilerChoicesTest, ReturnChoices) {
147 ReturnChoices::Results::Result results;
148 results.as_integers.reset(new std::vector<int>(Vector(1, 2)));
150 scoped_ptr<base::Value> results_value = results.ToValue();
151 ASSERT_TRUE(results_value);
153 base::ListValue expected;
154 expected.AppendInteger(1);
155 expected.AppendInteger(2);
157 EXPECT_TRUE(expected.Equals(results_value.get()));
160 ReturnChoices::Results::Result results;
161 results.as_integer.reset(new int(5));
163 scoped_ptr<base::Value> results_value = results.ToValue();
164 ASSERT_TRUE(results_value);
166 base::FundamentalValue expected(5);
168 EXPECT_TRUE(expected.Equals(results_value.get()));
172 TEST(JsonSchemaCompilerChoicesTest, NestedChoices) {
173 // These test both ToValue and FromValue for every legitimate configuration of
174 // NestedChoices.
176 // The plain integer choice.
177 scoped_ptr<base::Value> value = ReadJson("42");
178 scoped_ptr<NestedChoice> obj = NestedChoice::FromValue(*value);
180 ASSERT_TRUE(obj);
181 ASSERT_TRUE(obj->as_integer);
182 EXPECT_FALSE(obj->as_choice1);
183 EXPECT_FALSE(obj->as_choice2);
184 EXPECT_EQ(42, *obj->as_integer);
186 EXPECT_TRUE(base::Value::Equals(value.get(), obj->ToValue().get()));
190 // The string choice within the first choice.
191 scoped_ptr<base::Value> value = ReadJson("\"foo\"");
192 scoped_ptr<NestedChoice> obj = NestedChoice::FromValue(*value);
194 ASSERT_TRUE(obj);
195 EXPECT_FALSE(obj->as_integer);
196 ASSERT_TRUE(obj->as_choice1);
197 EXPECT_FALSE(obj->as_choice2);
198 ASSERT_TRUE(obj->as_choice1->as_string);
199 EXPECT_FALSE(obj->as_choice1->as_boolean);
200 EXPECT_EQ("foo", *obj->as_choice1->as_string);
202 EXPECT_TRUE(base::Value::Equals(value.get(), obj->ToValue().get()));
206 // The boolean choice within the first choice.
207 scoped_ptr<base::Value> value = ReadJson("true");
208 scoped_ptr<NestedChoice> obj = NestedChoice::FromValue(*value);
210 ASSERT_TRUE(obj);
211 EXPECT_FALSE(obj->as_integer);
212 ASSERT_TRUE(obj->as_choice1);
213 EXPECT_FALSE(obj->as_choice2);
214 EXPECT_FALSE(obj->as_choice1->as_string);
215 ASSERT_TRUE(obj->as_choice1->as_boolean);
216 EXPECT_TRUE(*obj->as_choice1->as_boolean);
218 EXPECT_TRUE(base::Value::Equals(value.get(), obj->ToValue().get()));
222 // The double choice within the second choice.
223 scoped_ptr<base::Value> value = ReadJson("42.0");
224 scoped_ptr<NestedChoice> obj = NestedChoice::FromValue(*value);
226 ASSERT_TRUE(obj);
227 EXPECT_FALSE(obj->as_integer);
228 EXPECT_FALSE(obj->as_choice1);
229 ASSERT_TRUE(obj->as_choice2);
230 ASSERT_TRUE(obj->as_choice2->as_double);
231 EXPECT_FALSE(obj->as_choice2->as_choice_type);
232 EXPECT_FALSE(obj->as_choice2->as_choice_types);
233 EXPECT_EQ(42.0, *obj->as_choice2->as_double);
235 EXPECT_TRUE(base::Value::Equals(value.get(), obj->ToValue().get()));
239 // The ChoiceType choice within the second choice.
240 scoped_ptr<base::Value> value = ReadJson(
241 "{\"integers\": [1, 2], \"strings\": \"foo\"}");
242 scoped_ptr<NestedChoice> obj = NestedChoice::FromValue(*value);
244 ASSERT_TRUE(obj);
245 EXPECT_FALSE(obj->as_integer);
246 EXPECT_FALSE(obj->as_choice1);
247 ASSERT_TRUE(obj->as_choice2);
248 EXPECT_FALSE(obj->as_choice2->as_double);
249 ASSERT_TRUE(obj->as_choice2->as_choice_type);
250 EXPECT_FALSE(obj->as_choice2->as_choice_types);
252 ChoiceType* choice_type = obj->as_choice2->as_choice_type.get();
253 ASSERT_TRUE(choice_type->integers.as_integers);
254 EXPECT_FALSE(choice_type->integers.as_integer);
255 EXPECT_EQ(Vector(1, 2), *choice_type->integers.as_integers);
256 ASSERT_TRUE(choice_type->strings);
257 EXPECT_FALSE(choice_type->strings->as_strings);
258 ASSERT_TRUE(choice_type->strings->as_string);
259 EXPECT_EQ("foo", *choice_type->strings->as_string);
262 EXPECT_TRUE(base::Value::Equals(value.get(), obj->ToValue().get()));
266 // The array of ChoiceTypes within the second choice.
267 scoped_ptr<base::Value> value = ReadJson(
269 " {\"integers\": [1, 2], \"strings\": \"foo\"},"
270 " {\"integers\": 3, \"strings\": [\"bar\", \"baz\"]}"
271 "]");
272 scoped_ptr<NestedChoice> obj = NestedChoice::FromValue(*value);
274 ASSERT_TRUE(obj);
275 EXPECT_FALSE(obj->as_integer);
276 EXPECT_FALSE(obj->as_choice1);
277 ASSERT_TRUE(obj->as_choice2);
278 EXPECT_FALSE(obj->as_choice2->as_double);
279 EXPECT_FALSE(obj->as_choice2->as_choice_type);
280 ASSERT_TRUE(obj->as_choice2->as_choice_types);
282 std::vector<linked_ptr<ChoiceType> >* choice_types =
283 obj->as_choice2->as_choice_types.get();
284 // Bleh too much effort to test everything.
285 ASSERT_EQ(2u, choice_types->size());
288 EXPECT_TRUE(base::Value::Equals(value.get(), obj->ToValue().get()));
292 } // namespace