Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(171)

Side by Side Diff: tools/json_schema_compiler/test/error_generation_unittest.cc

Issue 22228002: Add optional schema compiler error messages (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Unit tests! Created 7 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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
10 using namespace test::api::error_generation;
11
12 // GenerateTypePopulate errors
13
14 TEST(JsonSchemaCompilerErrorTest, RequiredPropertyPopulate) {
15 {
16 scoped_ptr<DictionaryValue> value(new DictionaryValue());
17 value->SetWithoutPathExpansion("string", Value::CreateStringValue("bling"));
not at google - send to devlin 2013/08/09 00:08:49 there is a utility in test_util.h for this sort of
dhnishi (use Chromium) 2013/08/09 17:59:50 You are correct. Just found bug 160596 which is al
18 scoped_ptr<TestType> testType(new TestType());
not at google - send to devlin 2013/08/09 00:08:49 test_type, and also, just declare on stack.
dhnishi (use Chromium) 2013/08/09 17:59:50 Done.
19 std::string error;
20 EXPECT_TRUE(TestType::Populate(*value, testType.get(), &error));
21 }
22 {
23 scoped_ptr<base::BinaryValue> value(new base::BinaryValue());
24 scoped_ptr<TestType> testType(new TestType());
25 std::string error;
26 EXPECT_FALSE(TestType::Populate(*value, testType.get(), &error));
27 EXPECT_FALSE(error.compare("expected dictionary, got binary"));
not at google - send to devlin 2013/08/09 00:08:49 EXPECT_EQ is better, gives you nice test messages.
dhnishi (use Chromium) 2013/08/09 17:59:50 Done.
28 }
29 }
30
31 TEST(JsonSchemaCompilerErrorTest, UnexpectedTypePopulation) {
32 {
33 scoped_ptr<base::ListValue> good_value(new base::ListValue());
34 scoped_ptr<ChoiceType::Integers> choice_type(new ChoiceType::Integers());
35 std::string error;
36 EXPECT_TRUE(ChoiceType::Integers::Populate(*good_value,
37 choice_type.get(),
38 &error));
39 }
40 {
41 scoped_ptr<base::BinaryValue> bad_value(new base::BinaryValue());
42 scoped_ptr<ChoiceType::Integers> choice_type(new ChoiceType::Integers());
43 std::string error;
44 EXPECT_FALSE(ChoiceType::Integers::Populate(*bad_value,
45 choice_type.get(),
46 &error));
47 EXPECT_FALSE(error.compare("unexpected type, got binary"));
not at google - send to devlin 2013/08/09 00:08:49 this does look a bit weird, and we should be able
dhnishi (use Chromium) 2013/08/09 17:59:50 It formats arrays by just pluralizing it, but I've
48 }
49 }
50
51 // GenerateTypePopulateProperty errors
52
53 TEST(JsonSchemaCompilerErrorTest, TypeIsRequired) {
54 {
55 scoped_ptr<base::DictionaryValue> value(new base::DictionaryValue());
56 value->SetWithoutPathExpansion("integers", Value::CreateIntegerValue(5));
57 scoped_ptr<ChoiceType> choice_type(new ChoiceType());
58 std::string error;
59 EXPECT_TRUE(ChoiceType::Populate(*value, choice_type.get(), &error));
60 }
61 {
62 scoped_ptr<base::DictionaryValue> bad_value(new base::DictionaryValue());
63 scoped_ptr<ChoiceType> choice_type(new ChoiceType());
64 std::string error;
65 EXPECT_FALSE(ChoiceType::Populate(*bad_value, choice_type.get(), &error));
66 EXPECT_FALSE(error.compare("'integers' is required"));
67 }
68 }
69
70 // GenerateParamsCheck errors
71
72 TEST(JsonSchemaCompilerErrorTest, TooManyParameters) {
73 {
74 scoped_ptr<ListValue> params_value(new ListValue());
75 params_value->Append(Value::CreateIntegerValue(5));
76 std::string error;
77 EXPECT_TRUE(TestFunction::Params::Create(*params_value, &error));
78 }
79 {
80 scoped_ptr<ListValue> params_value(new ListValue());
81 params_value->Append(Value::CreateIntegerValue(5));
82 params_value->Append(Value::CreateIntegerValue(5));
not at google - send to devlin 2013/08/09 00:08:49 and here (and above, etc): scoped_ptr<ListValue>
dhnishi (use Chromium) 2013/08/09 17:59:50 Done.
83 std::string error;
84 EXPECT_FALSE(TestFunction::Params::Create(*params_value, &error));
85 EXPECT_FALSE(error.compare("expected 1 arguments, got 2"));
86 }
87 }
88
89 // GenerateFunctionParamsCreate errors
90
91 TEST(JsonSchemaCompilerErrorTest, ParamIsRequired) {
92 {
93 scoped_ptr<ListValue> params_value(new ListValue());
94 params_value->Append(Value::CreateIntegerValue(5));
95 std::string error;
96 EXPECT_TRUE(TestFunction::Params::Create(*params_value, &error));
97 }
98 {
99 scoped_ptr<ListValue> params_value(new ListValue());
100 params_value->Append(Value::CreateNullValue());
101 std::string error;
102 EXPECT_FALSE(TestFunction::Params::Create(*params_value, &error));
103 EXPECT_FALSE(error.compare("'num' is required"));
104 }
105 }
106
107 // GeneratePopulateVariableFromValue errors
108
109 TEST(JsonSchemaCompilerErrorTest, WrongPropertyValueType) {
110 {
111 scoped_ptr<DictionaryValue> value(new DictionaryValue());
112 value->SetWithoutPathExpansion("string", Value::CreateStringValue("Yes"));
113 scoped_ptr<TestType> testType(new TestType());
114 std::string error;
115 EXPECT_TRUE(TestType::Populate(*value, testType.get(), &error));
116 }
117 {
118 scoped_ptr<DictionaryValue> value(new DictionaryValue());
119 value->SetWithoutPathExpansion("string", Value::CreateDoubleValue(1.1));
120 scoped_ptr<TestType> testType(new TestType());
121 std::string error;
122 EXPECT_FALSE(TestType::Populate(*value, testType.get(), &error));
123 EXPECT_FALSE(error.compare("'string': expected std::string, got number"));
124 }
125 }
126
127 TEST(JsonSchemaCompilerErrorTest, WrongParameterCreationType) {
128 {
129 scoped_ptr<ListValue> params_value(new ListValue());
130 params_value->Append(Value::CreateIntegerValue(5));
131 std::string error;
132 EXPECT_FALSE(TestString::Params::Create(*params_value, &error));
133 EXPECT_FALSE(error.compare("'str': expected std::string, got integer"));
134 }
135 {
136 scoped_ptr<ListValue> params_value(new ListValue());
137 params_value->Append(Value::CreateIntegerValue(5));
138 std::string error;
139 EXPECT_FALSE(TestTypeInObject::Params::Create(*params_value, &error));
140 EXPECT_FALSE(error.compare(
141 "'paramObject': expected dictionary, got integer"));
142 }
143 }
144
145 TEST(JsonSchemaCompilerErrorTest, WrongTypeValueType) {
146 {
147 scoped_ptr<base::DictionaryValue> value(new base::DictionaryValue());
148 scoped_ptr<ObjectType> object_type(new ObjectType());
149 std::string error;
150 EXPECT_TRUE(ObjectType::Populate(*value, object_type.get(), &error));
151 }
152 {
153 scoped_ptr<base::DictionaryValue> value(new base::DictionaryValue());
154 scoped_ptr<ObjectType> object_type(new ObjectType());
155 std::string error;
156 value->SetWithoutPathExpansion("otherType", Value::CreateDoubleValue(1.1));
157 EXPECT_FALSE(ObjectType::Populate(*value, object_type.get(), &error));
158 EXPECT_FALSE(error.compare(
159 "'otherType': expected dictionary, got number"));
160 }
161 }
162
163 TEST(JsonSchemaCompilerErrorTest, UnableToPopulateArray) {
164 {
165 scoped_ptr<ListValue> params_value(new ListValue());
166 params_value->Append(Value::CreateIntegerValue(5));
167 params_value->Append(Value::CreateBooleanValue(6));
not at google - send to devlin 2013/08/09 00:08:49 6? that's just going to get coerced into true. Doe
dhnishi (use Chromium) 2013/08/09 17:59:50 Whoops. I originally was adding an Integer value t
168 scoped_ptr<ChoiceType::Integers> choice_type(new ChoiceType::Integers());
169 std::string error;
170 EXPECT_FALSE(ChoiceType::Integers::Populate(*params_value,
171 choice_type.get(),
172 &error));
173 EXPECT_FALSE(error.compare("unable to populate array 'integers'"));
not at google - send to devlin 2013/08/09 00:08:49 how hard would it be for this to say like "'intege
dhnishi (use Chromium) 2013/08/09 17:59:50 The generated code itself doesn't have enough info
174 }
175 {
176 scoped_ptr<ListValue> params_value(new ListValue());
177 params_value->Append(Value::CreateIntegerValue(5));
178 params_value->Append(Value::CreateBooleanValue(false));
179 scoped_ptr<ChoiceType::Integers> choice_type(new ChoiceType::Integers());
180 std::string error;
181 EXPECT_FALSE(ChoiceType::Integers::Populate(*params_value,
182 choice_type.get(),
183 &error));
184 EXPECT_FALSE(error.compare("unable to populate array 'integers'"));
185 }
186 }
187
188 TEST(JsonSchemaCompilerErrorTest, BinaryTypeExpected) {
189 {
190 base::DictionaryValue* value = new base::DictionaryValue();
191 scoped_ptr<BinaryData> binary_data(new BinaryData());
192 scoped_ptr<base::BinaryValue> test_value(new base::BinaryValue());
193 std::string error;
194 value->SetWithoutPathExpansion("data", test_value.get());
195 EXPECT_TRUE(BinaryData::Populate(*value,
196 binary_data.get(),
197 &error));
198 }
199 {
200 base::DictionaryValue* value = new base::DictionaryValue();
201 scoped_ptr<BinaryData> binary_data(new BinaryData());
202 std::string error;
203 value->SetWithoutPathExpansion("data", Value::CreateDoubleValue(1.1));
204 EXPECT_FALSE(BinaryData::Populate(*value, binary_data.get(), &error));
205 EXPECT_FALSE(error.compare("'data': expected BinaryValue, got number"));
not at google - send to devlin 2013/08/09 00:08:49 s/BinaryValue/binary/
dhnishi (use Chromium) 2013/08/09 17:59:50 Done.
206 }
207 }
208
209 // GenerateStringToEnumConversion errors
210
211 TEST(JsonSchemaCompilerErrorTest, BadEnumValue) {
212 {
213 scoped_ptr<base::DictionaryValue> value(new base::DictionaryValue());
214 scoped_ptr<HasEnumeration> hasEnumeration(new HasEnumeration());
not at google - send to devlin 2013/08/09 00:08:49 has_enumeration
dhnishi (use Chromium) 2013/08/09 17:59:50 In-lined.
215 std::string error;
216 value->SetWithoutPathExpansion("enumeration",
217 Value::CreateStringValue("one"));
218 EXPECT_TRUE(HasEnumeration::Populate(*value,
219 hasEnumeration.get(),
220 &error));
221 }
222 {
223 scoped_ptr<base::DictionaryValue> value(new base::DictionaryValue());
224 scoped_ptr<HasEnumeration> hasEnumeration(new HasEnumeration());
225 std::string error;
226 value->SetWithoutPathExpansion("enumeration",
227 Value::CreateStringValue("bad_sauce"));
228 EXPECT_FALSE(HasEnumeration::Populate(*value,
229 hasEnumeration.get(),
230 &error));
231 EXPECT_FALSE(error.compare("got bad enum value from 'enumeration'"));
not at google - send to devlin 2013/08/09 00:08:49 how about 'enumeration': expected "one" or "two"
dhnishi (use Chromium) 2013/08/09 17:59:50 Done.
232 }
233 }
OLDNEW
« no previous file with comments | « tools/json_schema_compiler/test/error_generation.json ('k') | tools/json_schema_compiler/test/json_schema_compiler_tests.gyp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698