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