- add sources.
[platform/framework/web/crosswalk.git] / src / tools / json_schema_compiler / test / error_generation_unittest.cc
1 // Copyright 2013 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.
4
5 #include "tools/json_schema_compiler/test/error_generation.h"
6
7 #include "base/json/json_writer.h"
8 #include "base/strings/utf_string_conversions.h"
9 #include "testing/gtest/include/gtest/gtest.h"
10 #include "tools/json_schema_compiler/test/test_util.h"
11
12 using namespace test::api::error_generation;
13 using base::FundamentalValue;
14 using json_schema_compiler::test_util::Dictionary;
15 using json_schema_compiler::test_util::List;
16
17 template <typename T>
18 base::string16 GetPopulateError(const base::Value& value) {
19   base::string16 error;
20   T test_type;
21   T::Populate(value, &test_type, &error);
22   return error;
23 }
24
25 testing::AssertionResult EqualsUtf16(const std::string& expected,
26                                      const base::string16& actual) {
27   if (ASCIIToUTF16(expected) != actual)
28     return testing::AssertionFailure() << expected << " != " << actual;
29   return testing::AssertionSuccess();
30 }
31
32 // GenerateTypePopulate errors
33
34 TEST(JsonSchemaCompilerErrorTest, RequiredPropertyPopulate) {
35   {
36     scoped_ptr<DictionaryValue> value = Dictionary(
37         "string", new StringValue("bling"));
38     EXPECT_TRUE(EqualsUtf16("", GetPopulateError<TestType>(*value)));
39   }
40   {
41     scoped_ptr<base::BinaryValue> value(new base::BinaryValue());
42     EXPECT_TRUE(EqualsUtf16("expected dictionary, got binary",
43         GetPopulateError<TestType>(*value)));
44   }
45 }
46
47 TEST(JsonSchemaCompilerErrorTest, UnexpectedTypePopulation) {
48   {
49     scoped_ptr<base::ListValue> value(new base::ListValue());
50     EXPECT_TRUE(EqualsUtf16("",
51         GetPopulateError<ChoiceType::Integers>(*value)));
52   }
53   {
54     scoped_ptr<base::BinaryValue> value(new base::BinaryValue());
55     EXPECT_TRUE(EqualsUtf16("expected integers or integer, got binary",
56         GetPopulateError<ChoiceType::Integers>(*value)));
57   }
58 }
59
60 // GenerateTypePopulateProperty errors
61
62 TEST(JsonSchemaCompilerErrorTest, TypeIsRequired) {
63   {
64     scoped_ptr<DictionaryValue> value = Dictionary(
65         "integers", new FundamentalValue(5));
66     EXPECT_TRUE(EqualsUtf16("", GetPopulateError<ChoiceType>(*value)));
67   }
68   {
69     scoped_ptr<base::DictionaryValue> value(new base::DictionaryValue());
70     EXPECT_TRUE(EqualsUtf16("'integers' is required",
71         GetPopulateError<ChoiceType>(*value)));
72   }
73 }
74
75 // GenerateParamsCheck errors
76
77 TEST(JsonSchemaCompilerErrorTest, TooManyParameters) {
78   {
79     scoped_ptr<base::ListValue> params_value = List(
80         new FundamentalValue(5));
81     EXPECT_TRUE(TestFunction::Params::Create(*params_value));
82   }
83   {
84     scoped_ptr<base::ListValue> params_value = List(
85         new FundamentalValue(5),
86         new FundamentalValue(5));
87     base::string16 error;
88     EXPECT_FALSE(TestFunction::Params::Create(*params_value, &error));
89     EXPECT_TRUE(EqualsUtf16("expected 1 arguments, got 2", error));
90   }
91 }
92
93 // GenerateFunctionParamsCreate errors
94
95 TEST(JsonSchemaCompilerErrorTest, ParamIsRequired) {
96   {
97     scoped_ptr<base::ListValue> params_value = List(
98         new FundamentalValue(5));
99     EXPECT_TRUE(TestFunction::Params::Create(*params_value));
100   }
101   {
102     scoped_ptr<base::ListValue> params_value = List(
103         Value::CreateNullValue());
104     base::string16 error;
105     EXPECT_FALSE(TestFunction::Params::Create(*params_value, &error));
106     EXPECT_TRUE(EqualsUtf16("'num' is required", error));
107   }
108 }
109
110 // GeneratePopulateVariableFromValue errors
111
112 TEST(JsonSchemaCompilerErrorTest, WrongPropertyValueType) {
113   {
114     scoped_ptr<DictionaryValue> value = Dictionary(
115       "string", new StringValue("yes"));
116     EXPECT_TRUE(EqualsUtf16("", GetPopulateError<TestType>(*value)));
117   }
118   {
119     scoped_ptr<DictionaryValue> value = Dictionary(
120         "string", new FundamentalValue(1.1));
121     EXPECT_TRUE(EqualsUtf16("'string': expected string, got number",
122         GetPopulateError<TestType>(*value)));
123   }
124 }
125
126 TEST(JsonSchemaCompilerErrorTest, WrongParameterCreationType) {
127   {
128     scoped_ptr<base::ListValue> params_value = List(
129         new StringValue("Yeah!"));
130     EXPECT_TRUE(TestString::Params::Create(*params_value));
131   }
132   {
133     scoped_ptr<base::ListValue> params_value = List(
134         new FundamentalValue(5));
135     base::string16 error;
136     EXPECT_FALSE(TestTypeInObject::Params::Create(*params_value, &error));
137     EXPECT_TRUE(EqualsUtf16("'paramObject': expected dictionary, got integer",
138         error));
139   }
140 }
141
142 TEST(JsonSchemaCompilerErrorTest, WrongTypeValueType) {
143   {
144     scoped_ptr<base::DictionaryValue> value(new base::DictionaryValue());
145     EXPECT_TRUE(EqualsUtf16("", GetPopulateError<ObjectType>(*value)));
146   }
147   {
148     scoped_ptr<DictionaryValue> value = Dictionary(
149         "otherType", new FundamentalValue(1.1));
150     EXPECT_TRUE(EqualsUtf16("'otherType': expected dictionary, got number",
151         GetPopulateError<ObjectType>(*value)));
152   }
153 }
154
155 TEST(JsonSchemaCompilerErrorTest, UnableToPopulateArray) {
156   {
157     scoped_ptr<base::ListValue> params_value = List(
158         new FundamentalValue(5));
159     EXPECT_TRUE(EqualsUtf16("",
160         GetPopulateError<ChoiceType::Integers>(*params_value)));
161   }
162   {
163     scoped_ptr<base::ListValue> params_value = List(
164         new FundamentalValue(5),
165         new FundamentalValue(false));
166     EXPECT_TRUE(EqualsUtf16("unable to populate array 'integers'",
167         GetPopulateError<ChoiceType::Integers>(*params_value)));
168   }
169 }
170
171 TEST(JsonSchemaCompilerErrorTest, BinaryTypeExpected) {
172   {
173     scoped_ptr<DictionaryValue> value = Dictionary(
174         "data", new base::BinaryValue());
175     EXPECT_TRUE(EqualsUtf16("", GetPopulateError<BinaryData>(*value)));
176   }
177   {
178     scoped_ptr<DictionaryValue> value = Dictionary(
179         "data", new FundamentalValue(1.1));
180     EXPECT_TRUE(EqualsUtf16("'data': expected binary, got number",
181         GetPopulateError<BinaryData>(*value)));
182   }
183 }
184
185 TEST(JsonSchemaCompilerErrorTest, ListExpected) {
186   {
187     scoped_ptr<DictionaryValue> value = Dictionary(
188         "TheArray", new base::ListValue());
189     EXPECT_TRUE(EqualsUtf16("", GetPopulateError<ArrayObject>(*value)));
190   }
191   {
192     scoped_ptr<DictionaryValue> value = Dictionary(
193         "TheArray", new FundamentalValue(5));
194     EXPECT_TRUE(EqualsUtf16("'TheArray': expected list, got integer",
195         GetPopulateError<ArrayObject>(*value)));
196   }
197 }
198
199 // GenerateStringToEnumConversion errors
200
201 TEST(JsonSchemaCompilerErrorTest, BadEnumValue) {
202   {
203     scoped_ptr<DictionaryValue> value = Dictionary(
204         "enumeration", new StringValue("one"));
205     EXPECT_TRUE(EqualsUtf16("", GetPopulateError<HasEnumeration>(*value)));
206   }
207   {
208     scoped_ptr<DictionaryValue> value = Dictionary(
209         "enumeration", new StringValue("bad sauce"));
210     EXPECT_TRUE(EqualsUtf16("'enumeration': expected \"one\" or \"two\" "
211               "or \"three\", got \"bad sauce\"",
212         GetPopulateError<HasEnumeration>(*value)));
213   }
214 }