OLD | NEW |
---|---|
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 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
111 ASSERT_TRUE(instance->GetDictionary(0, &item)); | 111 ASSERT_TRUE(instance->GetDictionary(0, &item)); |
112 item->SetString("url", "xxxxxxxxxxx"); | 112 item->SetString("url", "xxxxxxxxxxx"); |
113 | 113 |
114 ExpectNotValid(TEST_SOURCE, instance.get(), schema.get(), NULL, | 114 ExpectNotValid(TEST_SOURCE, instance.get(), schema.get(), NULL, |
115 "0.url", | 115 "0.url", |
116 JSONSchemaValidator::FormatErrorMessage( | 116 JSONSchemaValidator::FormatErrorMessage( |
117 JSONSchemaValidator::kStringMaxLength, "10")); | 117 JSONSchemaValidator::kStringMaxLength, "10")); |
118 } | 118 } |
119 | 119 |
120 void JSONSchemaValidatorTestBase::TestStringPattern() { | 120 void JSONSchemaValidatorTestBase::TestStringPattern() { |
121 // Regex patterns not supported in CPP validator. | |
122 if (type_ == CPP) | |
Joao da Silva
2014/03/21 10:15:16
You can remove "type_" from the .h file (this is
binjin
2014/03/21 14:57:43
I think keep type field might still be useful for
| |
123 return; | |
124 | |
125 scoped_ptr<base::DictionaryValue> schema(new base::DictionaryValue()); | 121 scoped_ptr<base::DictionaryValue> schema(new base::DictionaryValue()); |
126 schema->SetString(schema::kType, schema::kString); | 122 schema->SetString(schema::kType, schema::kString); |
127 schema->SetString(schema::kPattern, "foo+"); | 123 schema->SetString(schema::kPattern, "foo+"); |
128 | 124 |
129 ExpectValid(TEST_SOURCE, | 125 ExpectValid(TEST_SOURCE, |
130 scoped_ptr<base::Value>(new base::StringValue("foo")).get(), | 126 scoped_ptr<base::Value>(new base::StringValue("foo")).get(), |
131 schema.get(), NULL); | 127 schema.get(), NULL); |
132 ExpectValid(TEST_SOURCE, | 128 ExpectValid(TEST_SOURCE, |
133 scoped_ptr<base::Value>(new base::StringValue("foooooo")).get(), | 129 scoped_ptr<base::Value>(new base::StringValue("foooooo")).get(), |
134 schema.get(), NULL); | 130 schema.get(), NULL); |
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
217 | 213 |
218 scoped_ptr<base::DictionaryValue> instance(new base::DictionaryValue()); | 214 scoped_ptr<base::DictionaryValue> instance(new base::DictionaryValue()); |
219 instance->SetString("foo", "foo"); | 215 instance->SetString("foo", "foo"); |
220 instance->SetInteger("bar", 42); | 216 instance->SetInteger("bar", 42); |
221 | 217 |
222 ExpectValid(TEST_SOURCE, instance.get(), schema.get(), NULL); | 218 ExpectValid(TEST_SOURCE, instance.get(), schema.get(), NULL); |
223 | 219 |
224 instance->SetBoolean("extra", true); | 220 instance->SetBoolean("extra", true); |
225 ExpectNotValid(TEST_SOURCE, instance.get(), schema.get(), NULL, | 221 ExpectNotValid(TEST_SOURCE, instance.get(), schema.get(), NULL, |
226 "extra", JSONSchemaValidator::kUnexpectedProperty); | 222 "extra", JSONSchemaValidator::kUnexpectedProperty); |
223 instance->Remove("extra", NULL); | |
227 | 224 |
228 instance->Remove("extra", NULL); | |
229 instance->Remove("bar", NULL); | 225 instance->Remove("bar", NULL); |
230 ExpectNotValid(TEST_SOURCE, instance.get(), schema.get(), NULL, "bar", | 226 ExpectNotValid(TEST_SOURCE, instance.get(), schema.get(), NULL, "bar", |
231 JSONSchemaValidator::kObjectPropertyIsRequired); | 227 JSONSchemaValidator::kObjectPropertyIsRequired); |
232 | 228 |
233 instance->SetString("bar", "42"); | 229 instance->SetString("bar", "42"); |
234 ExpectNotValid(TEST_SOURCE, instance.get(), schema.get(), NULL, "bar", | 230 ExpectNotValid(TEST_SOURCE, instance.get(), schema.get(), NULL, "bar", |
235 JSONSchemaValidator::FormatErrorMessage( | 231 JSONSchemaValidator::FormatErrorMessage( |
236 JSONSchemaValidator::kInvalidType, | 232 JSONSchemaValidator::kInvalidType, |
237 schema::kInteger, | 233 schema::kInteger, |
238 schema::kString)); | 234 schema::kString)); |
235 instance->SetInteger("bar", 42); | |
239 | 236 |
237 // Test "patternProperties". | |
238 instance->SetInteger("extra", 42); | |
239 ExpectNotValid(TEST_SOURCE, instance.get(), schema.get(), NULL, | |
240 "extra", JSONSchemaValidator::kUnexpectedProperty); | |
241 schema->SetString("patternProperties.extra+.type", | |
242 schema::kInteger); | |
243 ExpectValid(TEST_SOURCE, instance.get(), schema.get(), NULL); | |
244 instance->Remove("extra", NULL); | |
245 instance->SetInteger("extraaa", 42); | |
246 ExpectValid(TEST_SOURCE, instance.get(), schema.get(), NULL); | |
247 instance->Remove("extraaa", NULL); | |
248 instance->SetInteger("extr", 42); | |
249 ExpectNotValid(TEST_SOURCE, instance.get(), schema.get(), NULL, | |
250 "extr", JSONSchemaValidator::kUnexpectedProperty); | |
251 instance->Remove("extr", NULL); | |
252 schema->Remove(schema::kPatternProperties, NULL); | |
253 | |
254 // Test "patternProperties" and "properties" schemas are both checked if | |
255 // applicable. | |
256 schema->SetString("patternProperties.fo+.type", schema::kInteger); | |
257 ExpectNotValid(TEST_SOURCE, instance.get(), schema.get(), NULL, "foo", | |
258 JSONSchemaValidator::FormatErrorMessage( | |
259 JSONSchemaValidator::kInvalidType, | |
260 schema::kInteger, | |
261 schema::kString)); | |
262 instance->SetInteger("foo", 123); | |
263 ExpectNotValid(TEST_SOURCE, instance.get(), schema.get(), NULL, "foo", | |
264 JSONSchemaValidator::FormatErrorMessage( | |
265 JSONSchemaValidator::kInvalidType, | |
266 schema::kString, | |
267 schema::kInteger)); | |
268 instance->SetString("foo", "foo"); | |
269 schema->Remove(schema::kPatternProperties, NULL); | |
270 | |
271 // Test additional properties. | |
240 base::DictionaryValue* additional_properties = new base::DictionaryValue(); | 272 base::DictionaryValue* additional_properties = new base::DictionaryValue(); |
241 additional_properties->SetString(schema::kType, schema::kAny); | 273 additional_properties->SetString(schema::kType, schema::kAny); |
242 schema->Set(schema::kAdditionalProperties, additional_properties); | 274 schema->Set(schema::kAdditionalProperties, additional_properties); |
243 | 275 |
244 instance->SetInteger("bar", 42); | |
245 instance->SetBoolean("extra", true); | 276 instance->SetBoolean("extra", true); |
246 ExpectValid(TEST_SOURCE, instance.get(), schema.get(), NULL); | 277 ExpectValid(TEST_SOURCE, instance.get(), schema.get(), NULL); |
247 | 278 |
248 instance->SetString("extra", "foo"); | 279 instance->SetString("extra", "foo"); |
249 ExpectValid(TEST_SOURCE, instance.get(), schema.get(), NULL); | 280 ExpectValid(TEST_SOURCE, instance.get(), schema.get(), NULL); |
250 | 281 |
251 additional_properties->SetString(schema::kType, schema::kBoolean); | 282 additional_properties->SetString(schema::kType, schema::kBoolean); |
252 instance->SetBoolean("extra", true); | 283 instance->SetBoolean("extra", true); |
253 ExpectValid(TEST_SOURCE, instance.get(), schema.get(), NULL); | 284 ExpectValid(TEST_SOURCE, instance.get(), schema.get(), NULL); |
254 | 285 |
255 instance->SetString("extra", "foo"); | 286 instance->SetString("extra", "foo"); |
256 ExpectNotValid(TEST_SOURCE, instance.get(), schema.get(), NULL, | 287 ExpectNotValid(TEST_SOURCE, instance.get(), schema.get(), NULL, |
257 "extra", JSONSchemaValidator::FormatErrorMessage( | 288 "extra", JSONSchemaValidator::FormatErrorMessage( |
258 JSONSchemaValidator::kInvalidType, | 289 JSONSchemaValidator::kInvalidType, |
259 schema::kBoolean, | 290 schema::kBoolean, |
260 schema::kString)); | 291 schema::kString)); |
292 instance->Remove("extra", NULL); | |
261 | 293 |
262 base::DictionaryValue* properties = NULL; | 294 base::DictionaryValue* properties = NULL; |
263 base::DictionaryValue* bar_property = NULL; | 295 base::DictionaryValue* bar_property = NULL; |
264 ASSERT_TRUE(schema->GetDictionary(schema::kProperties, &properties)); | 296 ASSERT_TRUE(schema->GetDictionary(schema::kProperties, &properties)); |
265 ASSERT_TRUE(properties->GetDictionary("bar", &bar_property)); | 297 ASSERT_TRUE(properties->GetDictionary("bar", &bar_property)); |
266 | 298 |
267 bar_property->SetBoolean(schema::kOptional, true); | 299 bar_property->SetBoolean(schema::kOptional, true); |
268 instance->Remove("extra", NULL); | |
269 ExpectValid(TEST_SOURCE, instance.get(), schema.get(), NULL); | 300 ExpectValid(TEST_SOURCE, instance.get(), schema.get(), NULL); |
270 instance->Remove("bar", NULL); | 301 instance->Remove("bar", NULL); |
271 ExpectValid(TEST_SOURCE, instance.get(), schema.get(), NULL); | 302 ExpectValid(TEST_SOURCE, instance.get(), schema.get(), NULL); |
272 instance->Set("bar", base::Value::CreateNullValue()); | 303 instance->Set("bar", base::Value::CreateNullValue()); |
273 ExpectNotValid(TEST_SOURCE, instance.get(), schema.get(), NULL, | 304 ExpectNotValid(TEST_SOURCE, instance.get(), schema.get(), NULL, |
274 "bar", JSONSchemaValidator::FormatErrorMessage( | 305 "bar", JSONSchemaValidator::FormatErrorMessage( |
275 JSONSchemaValidator::kInvalidType, | 306 JSONSchemaValidator::kInvalidType, |
276 schema::kInteger, | 307 schema::kInteger, |
277 schema::kNull)); | 308 schema::kNull)); |
278 instance->SetString("bar", "42"); | 309 instance->SetString("bar", "42"); |
279 ExpectNotValid(TEST_SOURCE, instance.get(), schema.get(), NULL, | 310 ExpectNotValid(TEST_SOURCE, instance.get(), schema.get(), NULL, |
280 "bar", JSONSchemaValidator::FormatErrorMessage( | 311 "bar", JSONSchemaValidator::FormatErrorMessage( |
281 JSONSchemaValidator::kInvalidType, | 312 JSONSchemaValidator::kInvalidType, |
282 schema::kInteger, | 313 schema::kInteger, |
283 schema::kString)); | 314 schema::kString)); |
315 | |
316 // Verify that JSON parser handles dot in "patternProperties" well. | |
317 schema.reset(LoadDictionary("pattern_properties_dot.json")); | |
318 ASSERT_TRUE(schema->GetDictionary(schema::kPatternProperties, &properties)); | |
319 ASSERT_TRUE(properties->HasKey("^.$")); | |
320 | |
321 instance.reset(new base::DictionaryValue()); | |
322 instance->SetString("a", "whatever"); | |
323 ExpectValid(TEST_SOURCE, instance.get(), schema.get(), NULL); | |
324 instance->SetString("foo", "bar"); | |
325 ExpectNotValid(TEST_SOURCE, instance.get(), schema.get(), NULL, | |
326 "foo", JSONSchemaValidator::kUnexpectedProperty); | |
284 } | 327 } |
285 | 328 |
286 void JSONSchemaValidatorTestBase::TestTypeReference() { | 329 void JSONSchemaValidatorTestBase::TestTypeReference() { |
287 scoped_ptr<base::ListValue> types(LoadList("reference_types.json")); | 330 scoped_ptr<base::ListValue> types(LoadList("reference_types.json")); |
288 ASSERT_TRUE(types.get()); | 331 ASSERT_TRUE(types.get()); |
289 | 332 |
290 scoped_ptr<base::DictionaryValue> schema(new base::DictionaryValue()); | 333 scoped_ptr<base::DictionaryValue> schema(new base::DictionaryValue()); |
291 schema->SetString(schema::kType, schema::kObject); | 334 schema->SetString(schema::kType, schema::kObject); |
292 schema->SetString("properties.foo.type", schema::kString); | 335 schema->SetString("properties.foo.type", schema::kString); |
293 schema->SetString("properties.bar.$ref", "Max10Int"); | 336 schema->SetString("properties.bar.$ref", "Max10Int"); |
(...skipping 427 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
721 schema->SetString(schema::kType, schema::kNull); | 764 schema->SetString(schema::kType, schema::kNull); |
722 ExpectNotValid( | 765 ExpectNotValid( |
723 TEST_SOURCE, | 766 TEST_SOURCE, |
724 scoped_ptr<base::Value>(new base::FundamentalValue(false)).get(), | 767 scoped_ptr<base::Value>(new base::FundamentalValue(false)).get(), |
725 schema.get(), | 768 schema.get(), |
726 NULL, | 769 NULL, |
727 std::string(), | 770 std::string(), |
728 JSONSchemaValidator::FormatErrorMessage( | 771 JSONSchemaValidator::FormatErrorMessage( |
729 JSONSchemaValidator::kInvalidType, schema::kNull, schema::kBoolean)); | 772 JSONSchemaValidator::kInvalidType, schema::kNull, schema::kBoolean)); |
730 } | 773 } |
OLD | NEW |