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

Side by Side Diff: components/json_schema/json_schema_validator_unittest_base.cc

Issue 195193002: Add regular expression support in json_schema component (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: fix comment Created 6 years, 9 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
1 // Copyright 2013 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "components/json_schema/json_schema_validator_unittest_base.h" 5 #include "components/json_schema/json_schema_validator_unittest_base.h"
6 6
7 #include <cfloat> 7 #include <cfloat>
8 #include <cmath> 8 #include <cmath>
9 #include <limits> 9 #include <limits>
10 10
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
60 } 60 }
61 61
62 base::DictionaryValue* LoadDictionary(const std::string& filename) { 62 base::DictionaryValue* LoadDictionary(const std::string& filename) {
63 return static_cast<base::DictionaryValue*>( 63 return static_cast<base::DictionaryValue*>(
64 LoadValue(filename, base::Value::TYPE_DICTIONARY)); 64 LoadValue(filename, base::Value::TYPE_DICTIONARY));
65 } 65 }
66 66
67 } // namespace 67 } // namespace
68 68
69 69
70 JSONSchemaValidatorTestBase::JSONSchemaValidatorTestBase( 70 JSONSchemaValidatorTestBase::JSONSchemaValidatorTestBase() {
71 JSONSchemaValidatorTestBase::ValidatorType type)
72 : type_(type) {
73 } 71 }
74 72
75 void JSONSchemaValidatorTestBase::RunTests() { 73 void JSONSchemaValidatorTestBase::RunTests() {
76 TestComplex(); 74 TestComplex();
77 TestStringPattern(); 75 TestStringPattern();
78 TestEnum(); 76 TestEnum();
79 TestChoices(); 77 TestChoices();
80 TestExtends(); 78 TestExtends();
81 TestObject(); 79 TestObject();
82 TestTypeReference(); 80 TestTypeReference();
(...skipping 28 matching lines...) Expand all
111 ASSERT_TRUE(instance->GetDictionary(0, &item)); 109 ASSERT_TRUE(instance->GetDictionary(0, &item));
112 item->SetString("url", "xxxxxxxxxxx"); 110 item->SetString("url", "xxxxxxxxxxx");
113 111
114 ExpectNotValid(TEST_SOURCE, instance.get(), schema.get(), NULL, 112 ExpectNotValid(TEST_SOURCE, instance.get(), schema.get(), NULL,
115 "0.url", 113 "0.url",
116 JSONSchemaValidator::FormatErrorMessage( 114 JSONSchemaValidator::FormatErrorMessage(
117 JSONSchemaValidator::kStringMaxLength, "10")); 115 JSONSchemaValidator::kStringMaxLength, "10"));
118 } 116 }
119 117
120 void JSONSchemaValidatorTestBase::TestStringPattern() { 118 void JSONSchemaValidatorTestBase::TestStringPattern() {
121 // Regex patterns not supported in CPP validator.
122 if (type_ == CPP)
123 return;
124
125 scoped_ptr<base::DictionaryValue> schema(new base::DictionaryValue()); 119 scoped_ptr<base::DictionaryValue> schema(new base::DictionaryValue());
126 schema->SetString(schema::kType, schema::kString); 120 schema->SetString(schema::kType, schema::kString);
127 schema->SetString(schema::kPattern, "foo+"); 121 schema->SetString(schema::kPattern, "foo+");
128 122
129 ExpectValid(TEST_SOURCE, 123 ExpectValid(TEST_SOURCE,
130 scoped_ptr<base::Value>(new base::StringValue("foo")).get(), 124 scoped_ptr<base::Value>(new base::StringValue("foo")).get(),
131 schema.get(), NULL); 125 schema.get(), NULL);
132 ExpectValid(TEST_SOURCE, 126 ExpectValid(TEST_SOURCE,
133 scoped_ptr<base::Value>(new base::StringValue("foooooo")).get(), 127 scoped_ptr<base::Value>(new base::StringValue("foooooo")).get(),
134 schema.get(), NULL); 128 schema.get(), NULL);
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after
217 211
218 scoped_ptr<base::DictionaryValue> instance(new base::DictionaryValue()); 212 scoped_ptr<base::DictionaryValue> instance(new base::DictionaryValue());
219 instance->SetString("foo", "foo"); 213 instance->SetString("foo", "foo");
220 instance->SetInteger("bar", 42); 214 instance->SetInteger("bar", 42);
221 215
222 ExpectValid(TEST_SOURCE, instance.get(), schema.get(), NULL); 216 ExpectValid(TEST_SOURCE, instance.get(), schema.get(), NULL);
223 217
224 instance->SetBoolean("extra", true); 218 instance->SetBoolean("extra", true);
225 ExpectNotValid(TEST_SOURCE, instance.get(), schema.get(), NULL, 219 ExpectNotValid(TEST_SOURCE, instance.get(), schema.get(), NULL,
226 "extra", JSONSchemaValidator::kUnexpectedProperty); 220 "extra", JSONSchemaValidator::kUnexpectedProperty);
221 instance->Remove("extra", NULL);
227 222
228 instance->Remove("extra", NULL);
229 instance->Remove("bar", NULL); 223 instance->Remove("bar", NULL);
230 ExpectNotValid(TEST_SOURCE, instance.get(), schema.get(), NULL, "bar", 224 ExpectNotValid(TEST_SOURCE, instance.get(), schema.get(), NULL, "bar",
231 JSONSchemaValidator::kObjectPropertyIsRequired); 225 JSONSchemaValidator::kObjectPropertyIsRequired);
232 226
233 instance->SetString("bar", "42"); 227 instance->SetString("bar", "42");
234 ExpectNotValid(TEST_SOURCE, instance.get(), schema.get(), NULL, "bar", 228 ExpectNotValid(TEST_SOURCE, instance.get(), schema.get(), NULL, "bar",
235 JSONSchemaValidator::FormatErrorMessage( 229 JSONSchemaValidator::FormatErrorMessage(
236 JSONSchemaValidator::kInvalidType, 230 JSONSchemaValidator::kInvalidType,
237 schema::kInteger, 231 schema::kInteger,
238 schema::kString)); 232 schema::kString));
233 instance->SetInteger("bar", 42);
239 234
235 // Test "patternProperties".
236 instance->SetInteger("extra", 42);
237 ExpectNotValid(TEST_SOURCE, instance.get(), schema.get(), NULL,
238 "extra", JSONSchemaValidator::kUnexpectedProperty);
239 schema->SetString("patternProperties.extra+.type",
240 schema::kInteger);
241 ExpectValid(TEST_SOURCE, instance.get(), schema.get(), NULL);
242 instance->Remove("extra", NULL);
243 instance->SetInteger("extraaa", 42);
244 ExpectValid(TEST_SOURCE, instance.get(), schema.get(), NULL);
245 instance->Remove("extraaa", NULL);
246 instance->SetInteger("extr", 42);
247 ExpectNotValid(TEST_SOURCE, instance.get(), schema.get(), NULL,
248 "extr", JSONSchemaValidator::kUnexpectedProperty);
249 instance->Remove("extr", NULL);
250 schema->Remove(schema::kPatternProperties, NULL);
251
252 // Test "patternProperties" and "properties" schemas are both checked if
253 // applicable.
254 schema->SetString("patternProperties.fo+.type", schema::kInteger);
255 ExpectNotValid(TEST_SOURCE, instance.get(), schema.get(), NULL, "foo",
256 JSONSchemaValidator::FormatErrorMessage(
257 JSONSchemaValidator::kInvalidType,
258 schema::kInteger,
259 schema::kString));
260 instance->SetInteger("foo", 123);
261 ExpectNotValid(TEST_SOURCE, instance.get(), schema.get(), NULL, "foo",
262 JSONSchemaValidator::FormatErrorMessage(
263 JSONSchemaValidator::kInvalidType,
264 schema::kString,
265 schema::kInteger));
266 instance->SetString("foo", "foo");
267 schema->Remove(schema::kPatternProperties, NULL);
268
269 // Test additional properties.
240 base::DictionaryValue* additional_properties = new base::DictionaryValue(); 270 base::DictionaryValue* additional_properties = new base::DictionaryValue();
241 additional_properties->SetString(schema::kType, schema::kAny); 271 additional_properties->SetString(schema::kType, schema::kAny);
242 schema->Set(schema::kAdditionalProperties, additional_properties); 272 schema->Set(schema::kAdditionalProperties, additional_properties);
243 273
244 instance->SetInteger("bar", 42);
245 instance->SetBoolean("extra", true); 274 instance->SetBoolean("extra", true);
246 ExpectValid(TEST_SOURCE, instance.get(), schema.get(), NULL); 275 ExpectValid(TEST_SOURCE, instance.get(), schema.get(), NULL);
247 276
248 instance->SetString("extra", "foo"); 277 instance->SetString("extra", "foo");
249 ExpectValid(TEST_SOURCE, instance.get(), schema.get(), NULL); 278 ExpectValid(TEST_SOURCE, instance.get(), schema.get(), NULL);
250 279
251 additional_properties->SetString(schema::kType, schema::kBoolean); 280 additional_properties->SetString(schema::kType, schema::kBoolean);
252 instance->SetBoolean("extra", true); 281 instance->SetBoolean("extra", true);
253 ExpectValid(TEST_SOURCE, instance.get(), schema.get(), NULL); 282 ExpectValid(TEST_SOURCE, instance.get(), schema.get(), NULL);
254 283
255 instance->SetString("extra", "foo"); 284 instance->SetString("extra", "foo");
256 ExpectNotValid(TEST_SOURCE, instance.get(), schema.get(), NULL, 285 ExpectNotValid(TEST_SOURCE, instance.get(), schema.get(), NULL,
257 "extra", JSONSchemaValidator::FormatErrorMessage( 286 "extra", JSONSchemaValidator::FormatErrorMessage(
258 JSONSchemaValidator::kInvalidType, 287 JSONSchemaValidator::kInvalidType,
259 schema::kBoolean, 288 schema::kBoolean,
260 schema::kString)); 289 schema::kString));
290 instance->Remove("extra", NULL);
261 291
262 base::DictionaryValue* properties = NULL; 292 base::DictionaryValue* properties = NULL;
263 base::DictionaryValue* bar_property = NULL; 293 base::DictionaryValue* bar_property = NULL;
264 ASSERT_TRUE(schema->GetDictionary(schema::kProperties, &properties)); 294 ASSERT_TRUE(schema->GetDictionary(schema::kProperties, &properties));
265 ASSERT_TRUE(properties->GetDictionary("bar", &bar_property)); 295 ASSERT_TRUE(properties->GetDictionary("bar", &bar_property));
266 296
267 bar_property->SetBoolean(schema::kOptional, true); 297 bar_property->SetBoolean(schema::kOptional, true);
268 instance->Remove("extra", NULL);
269 ExpectValid(TEST_SOURCE, instance.get(), schema.get(), NULL); 298 ExpectValid(TEST_SOURCE, instance.get(), schema.get(), NULL);
270 instance->Remove("bar", NULL); 299 instance->Remove("bar", NULL);
271 ExpectValid(TEST_SOURCE, instance.get(), schema.get(), NULL); 300 ExpectValid(TEST_SOURCE, instance.get(), schema.get(), NULL);
272 instance->Set("bar", base::Value::CreateNullValue()); 301 instance->Set("bar", base::Value::CreateNullValue());
273 ExpectNotValid(TEST_SOURCE, instance.get(), schema.get(), NULL, 302 ExpectNotValid(TEST_SOURCE, instance.get(), schema.get(), NULL,
274 "bar", JSONSchemaValidator::FormatErrorMessage( 303 "bar", JSONSchemaValidator::FormatErrorMessage(
275 JSONSchemaValidator::kInvalidType, 304 JSONSchemaValidator::kInvalidType,
276 schema::kInteger, 305 schema::kInteger,
277 schema::kNull)); 306 schema::kNull));
278 instance->SetString("bar", "42"); 307 instance->SetString("bar", "42");
279 ExpectNotValid(TEST_SOURCE, instance.get(), schema.get(), NULL, 308 ExpectNotValid(TEST_SOURCE, instance.get(), schema.get(), NULL,
280 "bar", JSONSchemaValidator::FormatErrorMessage( 309 "bar", JSONSchemaValidator::FormatErrorMessage(
281 JSONSchemaValidator::kInvalidType, 310 JSONSchemaValidator::kInvalidType,
282 schema::kInteger, 311 schema::kInteger,
283 schema::kString)); 312 schema::kString));
313
314 // Verify that JSON parser handles dot in "patternProperties" well.
315 schema.reset(LoadDictionary("pattern_properties_dot.json"));
316 ASSERT_TRUE(schema->GetDictionary(schema::kPatternProperties, &properties));
317 ASSERT_TRUE(properties->HasKey("^.$"));
318
319 instance.reset(new base::DictionaryValue());
320 instance->SetString("a", "whatever");
321 ExpectValid(TEST_SOURCE, instance.get(), schema.get(), NULL);
322 instance->SetString("foo", "bar");
323 ExpectNotValid(TEST_SOURCE, instance.get(), schema.get(), NULL,
324 "foo", JSONSchemaValidator::kUnexpectedProperty);
284 } 325 }
285 326
286 void JSONSchemaValidatorTestBase::TestTypeReference() { 327 void JSONSchemaValidatorTestBase::TestTypeReference() {
287 scoped_ptr<base::ListValue> types(LoadList("reference_types.json")); 328 scoped_ptr<base::ListValue> types(LoadList("reference_types.json"));
288 ASSERT_TRUE(types.get()); 329 ASSERT_TRUE(types.get());
289 330
290 scoped_ptr<base::DictionaryValue> schema(new base::DictionaryValue()); 331 scoped_ptr<base::DictionaryValue> schema(new base::DictionaryValue());
291 schema->SetString(schema::kType, schema::kObject); 332 schema->SetString(schema::kType, schema::kObject);
292 schema->SetString("properties.foo.type", schema::kString); 333 schema->SetString("properties.foo.type", schema::kString);
293 schema->SetString("properties.bar.$ref", "Max10Int"); 334 schema->SetString("properties.bar.$ref", "Max10Int");
(...skipping 427 matching lines...) Expand 10 before | Expand all | Expand 10 after
721 schema->SetString(schema::kType, schema::kNull); 762 schema->SetString(schema::kType, schema::kNull);
722 ExpectNotValid( 763 ExpectNotValid(
723 TEST_SOURCE, 764 TEST_SOURCE,
724 scoped_ptr<base::Value>(new base::FundamentalValue(false)).get(), 765 scoped_ptr<base::Value>(new base::FundamentalValue(false)).get(),
725 schema.get(), 766 schema.get(),
726 NULL, 767 NULL,
727 std::string(), 768 std::string(),
728 JSONSchemaValidator::FormatErrorMessage( 769 JSONSchemaValidator::FormatErrorMessage(
729 JSONSchemaValidator::kInvalidType, schema::kNull, schema::kBoolean)); 770 JSONSchemaValidator::kInvalidType, schema::kNull, schema::kBoolean));
730 } 771 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698