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

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: Code clean up, fixed tests, improved errors. 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 #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 // GenerateTypePopulate errors
17
18 TEST(JsonSchemaCompilerErrorTest, RequiredPropertyPopulate) {
19 {
20 scoped_ptr<DictionaryValue> value = Dictionary(
21 "string", new StringValue("bling"));
22 EXPECT_TRUE(TestType::Populate(*value, new TestType()));
23 }
24 {
25 scoped_ptr<base::BinaryValue> value(new base::BinaryValue());
26 std::string error;
27 EXPECT_FALSE(TestType::Populate(*value, new TestType(), &error));
not at google - send to devlin 2013/08/09 18:20:58 (new TestType()) looks leaky... other tests like t
dhnishi (use Chromium) 2013/08/09 20:50:48 Valgrind suggests an 4-16 byte leak for all of tho
28 EXPECT_EQ(error, "expected dictionary, got binary");
29 }
30 }
31
32 TEST(JsonSchemaCompilerErrorTest, UnexpectedTypePopulation) {
33 {
34 scoped_ptr<base::ListValue> good_value(new base::ListValue());
35 EXPECT_TRUE(ChoiceType::Integers::Populate(*good_value,
36 new ChoiceType::Integers()));
37 }
38 {
39 scoped_ptr<base::BinaryValue> bad_value(new base::BinaryValue());
40 std::string error;
41 EXPECT_FALSE(ChoiceType::Integers::Populate(*bad_value,
42 new ChoiceType::Integers(),
43 &error));
44 EXPECT_EQ(error, "expected integers or integer, got binary");
45 }
46 }
47
48 // GenerateTypePopulateProperty errors
49
50 TEST(JsonSchemaCompilerErrorTest, TypeIsRequired) {
51 {
52 scoped_ptr<DictionaryValue> value = Dictionary(
53 "integers", new FundamentalValue(5));
54 EXPECT_TRUE(ChoiceType::Populate(*value, new ChoiceType()));
55 }
56 {
57 scoped_ptr<base::DictionaryValue> bad_value(new base::DictionaryValue());
58 std::string error;
59 EXPECT_FALSE(ChoiceType::Populate(*bad_value, new ChoiceType(), &error));
60 EXPECT_EQ(error, "'integers' is required");
61 }
62 }
63
64 // GenerateParamsCheck errors
65
66 TEST(JsonSchemaCompilerErrorTest, TooManyParameters) {
67 {
68 scoped_ptr<base::ListValue> params_value = List(
69 new FundamentalValue(5));
70 EXPECT_TRUE(TestFunction::Params::Create(*params_value));
71 }
72 {
73 scoped_ptr<base::ListValue> params_value = List(
74 new FundamentalValue(5),
75 new FundamentalValue(5));
76 std::string error;
77 EXPECT_FALSE(TestFunction::Params::Create(*params_value, &error));
78 EXPECT_EQ(error, "expected 1 arguments, got 2");
79 }
80 }
81
82 // GenerateFunctionParamsCreate errors
83
84 TEST(JsonSchemaCompilerErrorTest, ParamIsRequired) {
85 {
86 scoped_ptr<base::ListValue> params_value = List(
87 new FundamentalValue(5));
88 EXPECT_TRUE(TestFunction::Params::Create(*params_value));
89 }
90 {
91 scoped_ptr<base::ListValue> params_value = List(
92 Value::CreateNullValue());
93 std::string error;
94 EXPECT_FALSE(TestFunction::Params::Create(*params_value, &error));
95 EXPECT_EQ(error, "'num' is required");
96 }
97 }
98
99 // GeneratePopulateVariableFromValue errors
100
101 TEST(JsonSchemaCompilerErrorTest, WrongPropertyValueType) {
102 {
103 scoped_ptr<DictionaryValue> value = Dictionary(
104 "string", new StringValue("yes");
105 EXPECT_TRUE(TestType::Populate(*value, new TestType()));
106 }
107 {
108 scoped_ptr<DictionaryValue> value = Dictionary(
109 "string", new FundamentalValue(1.1));
110 std::string error;
111 EXPECT_FALSE(TestType::Populate(*value, new TestType(), &error));
112 EXPECT_EQ(error, "'string': expected std::string, got number");
not at google - send to devlin 2013/08/09 18:20:58 s/std::string/string/
dhnishi (use Chromium) 2013/08/09 20:50:48 Done. Using the underlying_type now, rather than t
not at google - send to devlin 2013/08/09 21:13:01 Yeah these errors are for displaying to extension
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_TRUE(ObjectType::Populate(*value, new ObjectType()));
136 }
137 {
138 scoped_ptr<DictionaryValue> value = Dictionary(
139 "otherType", new FundamentalValue(1.1));
140 std::string error;
141 EXPECT_FALSE(ObjectType::Populate(*value, new ObjectType(), &error));
142 EXPECT_EQ(error,
143 "'otherType': expected dictionary, got number");
144 }
145 }
146
147 TEST(JsonSchemaCompilerErrorTest, UnableToPopulateArray) {
148 {
149 scoped_ptr<base::ListValue> params_value = List(
150 new FundamentalValue(5));
151 EXPECT_TRUE(ChoiceType::Integers::Populate(*params_value,
152 new ChoiceType::Integers()));
153 }
154 {
155 scoped_ptr<base::ListValue> params_value = List(
156 new FundamentalValue(5),
157 new FundamentalValue(false));
158 std::string error;
159 EXPECT_FALSE(ChoiceType::Integers::Populate(*params_value,
160 new ChoiceType::Integers(),
161 &error));
162 EXPECT_EQ(error, "unable to populate array 'integers'");
163 }
164 }
165
166 TEST(JsonSchemaCompilerErrorTest, BinaryTypeExpected) {
167 {
168 scoped_ptr<DictionaryValue> value = Dictionary(
169 "data", new base::BinaryValue());
170 EXPECT_TRUE(BinaryData::Populate(*value,
171 new BinaryData()));
172 }
173 {
174 scoped_ptr<DictionaryValue> value = Dictionary(
175 "data", new FundamentalValue(1.1));
176 std::string error;
177 EXPECT_FALSE(BinaryData::Populate(*value, new BinaryData(), &error));
178 EXPECT_EQ(error, "'data': expected binary, got number");
179 }
180 }
181
182 // GenerateStringToEnumConversion errors
183
184 TEST(JsonSchemaCompilerErrorTest, BadEnumValue) {
185 {
186 scoped_ptr<DictionaryValue> value = Dictionary(
187 "enumeration", new StringValue("one"));
188 EXPECT_TRUE(HasEnumeration::Populate(*value,
189 new HasEnumeration()));
190 }
191 {
192 scoped_ptr<DictionaryValue> value = Dictionary(
193 "enumeration", new StringValue("bad sauce"));
194 std::string error;
195 EXPECT_FALSE(HasEnumeration::Populate(*value,
196 new HasEnumeration(),
197 &error));
198 EXPECT_EQ(error,
199 "'enumeration': expected \"one\" or \"two\" or \"three\", "
200 "got \"bad sauce\"");
201 }
202 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698