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

Side by Side Diff: chrome/common/json_schema_validator_unittest_base.cc

Issue 10696034: Share JSON schema constants. (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Created 8 years, 5 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 (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 "chrome/common/json_schema_validator_unittest_base.h" 5 #include "chrome/common/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
11 #include "base/file_util.h" 11 #include "base/file_util.h"
12 #include "base/json/json_file_value_serializer.h" 12 #include "base/json/json_file_value_serializer.h"
13 #include "base/logging.h" 13 #include "base/logging.h"
14 #include "base/memory/scoped_ptr.h" 14 #include "base/memory/scoped_ptr.h"
15 #include "base/path_service.h" 15 #include "base/path_service.h"
16 #include "base/stringprintf.h" 16 #include "base/stringprintf.h"
17 #include "base/values.h" 17 #include "base/values.h"
18 #include "chrome/common/chrome_paths.h" 18 #include "chrome/common/chrome_paths.h"
19 #include "chrome/common/json_schema_constants.h"
19 #include "chrome/common/json_schema_validator.h" 20 #include "chrome/common/json_schema_validator.h"
20 21
22 using namespace json_schema_constants;
23
21 namespace { 24 namespace {
22 25
23 #define TEST_SOURCE base::StringPrintf("%s:%i", __FILE__, __LINE__) 26 #define TEST_SOURCE base::StringPrintf("%s:%i", __FILE__, __LINE__)
24 27
25 Value* LoadValue(const std::string& filename) { 28 Value* LoadValue(const std::string& filename) {
26 FilePath path; 29 FilePath path;
27 PathService::Get(chrome::DIR_TEST_DATA, &path); 30 PathService::Get(chrome::DIR_TEST_DATA, &path);
28 path = path.AppendASCII("json_schema_validator").AppendASCII(filename); 31 path = path.AppendASCII("json_schema_validator").AppendASCII(filename);
29 EXPECT_TRUE(file_util::PathExists(path)); 32 EXPECT_TRUE(file_util::PathExists(path));
30 33
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after
87 90
88 ASSERT_TRUE(schema.get()); 91 ASSERT_TRUE(schema.get());
89 ASSERT_TRUE(instance.get()); 92 ASSERT_TRUE(instance.get());
90 93
91 ExpectValid(TEST_SOURCE, instance.get(), schema.get(), NULL); 94 ExpectValid(TEST_SOURCE, instance.get(), schema.get(), NULL);
92 instance->Remove(instance->GetSize() - 1, NULL); 95 instance->Remove(instance->GetSize() - 1, NULL);
93 ExpectValid(TEST_SOURCE, instance.get(), schema.get(), NULL); 96 ExpectValid(TEST_SOURCE, instance.get(), schema.get(), NULL);
94 instance->Append(new DictionaryValue()); 97 instance->Append(new DictionaryValue());
95 ExpectNotValid(TEST_SOURCE, instance.get(), schema.get(), NULL, "1", 98 ExpectNotValid(TEST_SOURCE, instance.get(), schema.get(), NULL, "1",
96 JSONSchemaValidator::FormatErrorMessage( 99 JSONSchemaValidator::FormatErrorMessage(
97 JSONSchemaValidator::kInvalidType, "number", "object")); 100 JSONSchemaValidator::kInvalidType, kNumber, kObject));
98 instance->Remove(instance->GetSize() - 1, NULL); 101 instance->Remove(instance->GetSize() - 1, NULL);
99 102
100 DictionaryValue* item = NULL; 103 DictionaryValue* item = NULL;
101 ASSERT_TRUE(instance->GetDictionary(0, &item)); 104 ASSERT_TRUE(instance->GetDictionary(0, &item));
102 item->SetString("url", "xxxxxxxxxxx"); 105 item->SetString("url", "xxxxxxxxxxx");
103 106
104 ExpectNotValid(TEST_SOURCE, instance.get(), schema.get(), NULL, 107 ExpectNotValid(TEST_SOURCE, instance.get(), schema.get(), NULL,
105 "0.url", 108 "0.url",
106 JSONSchemaValidator::FormatErrorMessage( 109 JSONSchemaValidator::FormatErrorMessage(
107 JSONSchemaValidator::kStringMaxLength, "10")); 110 JSONSchemaValidator::kStringMaxLength, "10"));
108 } 111 }
109 112
110 void JSONSchemaValidatorTestBase::TestStringPattern() { 113 void JSONSchemaValidatorTestBase::TestStringPattern() {
111 // Regex patterns not supported in CPP validator. 114 // Regex patterns not supported in CPP validator.
112 if (type_ == CPP) 115 if (type_ == CPP)
113 return; 116 return;
114 117
115 scoped_ptr<DictionaryValue> schema(new DictionaryValue()); 118 scoped_ptr<DictionaryValue> schema(new DictionaryValue());
116 schema->SetString("type", "string"); 119 schema->SetString(kType, kString);
117 schema->SetString("pattern", "foo+"); 120 schema->SetString(kPattern, "foo+");
118 121
119 ExpectValid(TEST_SOURCE, 122 ExpectValid(TEST_SOURCE,
120 scoped_ptr<Value>(Value::CreateStringValue("foo")).get(), 123 scoped_ptr<Value>(Value::CreateStringValue("foo")).get(),
121 schema.get(), NULL); 124 schema.get(), NULL);
122 ExpectValid(TEST_SOURCE, 125 ExpectValid(TEST_SOURCE,
123 scoped_ptr<Value>(Value::CreateStringValue("foooooo")).get(), 126 scoped_ptr<Value>(Value::CreateStringValue("foooooo")).get(),
124 schema.get(), NULL); 127 schema.get(), NULL);
125 ExpectNotValid(TEST_SOURCE, 128 ExpectNotValid(TEST_SOURCE,
126 scoped_ptr<Value>(Value::CreateStringValue("bar")).get(), 129 scoped_ptr<Value>(Value::CreateStringValue("bar")).get(),
127 schema.get(), NULL, "", 130 schema.get(), NULL, "",
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
175 ExpectNotValid(TEST_SOURCE, instance.get(), 178 ExpectNotValid(TEST_SOURCE, instance.get(),
176 schema.get(), NULL, "", JSONSchemaValidator::kInvalidChoice); 179 schema.get(), NULL, "", JSONSchemaValidator::kInvalidChoice);
177 } 180 }
178 181
179 void JSONSchemaValidatorTestBase::TestExtends() { 182 void JSONSchemaValidatorTestBase::TestExtends() {
180 // TODO(aa): JS only 183 // TODO(aa): JS only
181 } 184 }
182 185
183 void JSONSchemaValidatorTestBase::TestObject() { 186 void JSONSchemaValidatorTestBase::TestObject() {
184 scoped_ptr<DictionaryValue> schema(new DictionaryValue()); 187 scoped_ptr<DictionaryValue> schema(new DictionaryValue());
185 schema->SetString("type", "object"); 188 schema->SetString(kType, kObject);
186 schema->SetString("properties.foo.type", "string"); 189 schema->SetString("properties.foo.type", kString);
187 schema->SetString("properties.bar.type", "integer"); 190 schema->SetString("properties.bar.type", kInteger);
188 191
189 scoped_ptr<DictionaryValue> instance(new DictionaryValue()); 192 scoped_ptr<DictionaryValue> instance(new DictionaryValue());
190 instance->SetString("foo", "foo"); 193 instance->SetString("foo", "foo");
191 instance->SetInteger("bar", 42); 194 instance->SetInteger("bar", 42);
192 195
193 ExpectValid(TEST_SOURCE, instance.get(), schema.get(), NULL); 196 ExpectValid(TEST_SOURCE, instance.get(), schema.get(), NULL);
194 197
195 instance->SetBoolean("extra", true); 198 instance->SetBoolean("extra", true);
196 ExpectNotValid(TEST_SOURCE, instance.get(), schema.get(), NULL, 199 ExpectNotValid(TEST_SOURCE, instance.get(), schema.get(), NULL,
197 "extra", JSONSchemaValidator::kUnexpectedProperty); 200 "extra", JSONSchemaValidator::kUnexpectedProperty);
198 201
199 instance->Remove("extra", NULL); 202 instance->Remove("extra", NULL);
200 instance->Remove("bar", NULL); 203 instance->Remove("bar", NULL);
201 ExpectNotValid(TEST_SOURCE, instance.get(), schema.get(), NULL, "bar", 204 ExpectNotValid(TEST_SOURCE, instance.get(), schema.get(), NULL, "bar",
202 JSONSchemaValidator::kObjectPropertyIsRequired); 205 JSONSchemaValidator::kObjectPropertyIsRequired);
203 206
204 instance->SetString("bar", "42"); 207 instance->SetString("bar", "42");
205 ExpectNotValid(TEST_SOURCE, instance.get(), schema.get(), NULL, "bar", 208 ExpectNotValid(TEST_SOURCE, instance.get(), schema.get(), NULL, "bar",
206 JSONSchemaValidator::FormatErrorMessage( 209 JSONSchemaValidator::FormatErrorMessage(
207 JSONSchemaValidator::kInvalidType, "integer", "string")); 210 JSONSchemaValidator::kInvalidType, kInteger, kString));
208 211
209 DictionaryValue* additional_properties = new DictionaryValue(); 212 DictionaryValue* additional_properties = new DictionaryValue();
210 additional_properties->SetString("type", "any"); 213 additional_properties->SetString(kType, kAny);
211 schema->Set("additionalProperties", additional_properties); 214 schema->Set(kAdditionalProperties, additional_properties);
212 215
213 instance->SetInteger("bar", 42); 216 instance->SetInteger("bar", 42);
214 instance->SetBoolean("extra", true); 217 instance->SetBoolean("extra", true);
215 ExpectValid(TEST_SOURCE, instance.get(), schema.get(), NULL); 218 ExpectValid(TEST_SOURCE, instance.get(), schema.get(), NULL);
216 219
217 instance->SetString("extra", "foo"); 220 instance->SetString("extra", "foo");
218 ExpectValid(TEST_SOURCE, instance.get(), schema.get(), NULL); 221 ExpectValid(TEST_SOURCE, instance.get(), schema.get(), NULL);
219 222
220 additional_properties->SetString("type", "boolean"); 223 additional_properties->SetString(kType, kBoolean);
221 instance->SetBoolean("extra", true); 224 instance->SetBoolean("extra", true);
222 ExpectValid(TEST_SOURCE, instance.get(), schema.get(), NULL); 225 ExpectValid(TEST_SOURCE, instance.get(), schema.get(), NULL);
223 226
224 instance->SetString("extra", "foo"); 227 instance->SetString("extra", "foo");
225 ExpectNotValid(TEST_SOURCE, instance.get(), schema.get(), NULL, 228 ExpectNotValid(TEST_SOURCE, instance.get(), schema.get(), NULL,
226 "extra", JSONSchemaValidator::FormatErrorMessage( 229 "extra", JSONSchemaValidator::FormatErrorMessage(
227 JSONSchemaValidator::kInvalidType, "boolean", "string")); 230 JSONSchemaValidator::kInvalidType, kBoolean, kString));
228 231
229 DictionaryValue* properties = NULL; 232 DictionaryValue* properties = NULL;
230 DictionaryValue* bar_property = NULL; 233 DictionaryValue* bar_property = NULL;
231 ASSERT_TRUE(schema->GetDictionary("properties", &properties)); 234 ASSERT_TRUE(schema->GetDictionary(kProperties, &properties));
232 ASSERT_TRUE(properties->GetDictionary("bar", &bar_property)); 235 ASSERT_TRUE(properties->GetDictionary("bar", &bar_property));
233 236
234 bar_property->SetBoolean("optional", true); 237 bar_property->SetBoolean(kOptional, true);
235 instance->Remove("extra", NULL); 238 instance->Remove("extra", NULL);
236 ExpectValid(TEST_SOURCE, instance.get(), schema.get(), NULL); 239 ExpectValid(TEST_SOURCE, instance.get(), schema.get(), NULL);
237 instance->Remove("bar", NULL); 240 instance->Remove("bar", NULL);
238 ExpectValid(TEST_SOURCE, instance.get(), schema.get(), NULL); 241 ExpectValid(TEST_SOURCE, instance.get(), schema.get(), NULL);
239 instance->Set("bar", Value::CreateNullValue()); 242 instance->Set("bar", Value::CreateNullValue());
240 ExpectNotValid(TEST_SOURCE, instance.get(), schema.get(), NULL, 243 ExpectNotValid(TEST_SOURCE, instance.get(), schema.get(), NULL,
241 "bar", JSONSchemaValidator::FormatErrorMessage( 244 "bar", JSONSchemaValidator::FormatErrorMessage(
242 JSONSchemaValidator::kInvalidType, "integer", "null")); 245 JSONSchemaValidator::kInvalidType, kInteger, kNull));
243 instance->SetString("bar", "42"); 246 instance->SetString("bar", "42");
244 ExpectNotValid(TEST_SOURCE, instance.get(), schema.get(), NULL, 247 ExpectNotValid(TEST_SOURCE, instance.get(), schema.get(), NULL,
245 "bar", JSONSchemaValidator::FormatErrorMessage( 248 "bar", JSONSchemaValidator::FormatErrorMessage(
246 JSONSchemaValidator::kInvalidType, "integer", "string")); 249 JSONSchemaValidator::kInvalidType, kInteger, kString));
247 } 250 }
248 251
249 void JSONSchemaValidatorTestBase::TestTypeReference() { 252 void JSONSchemaValidatorTestBase::TestTypeReference() {
250 scoped_ptr<ListValue> types(LoadList("reference_types.json")); 253 scoped_ptr<ListValue> types(LoadList("reference_types.json"));
251 ASSERT_TRUE(types.get()); 254 ASSERT_TRUE(types.get());
252 255
253 scoped_ptr<DictionaryValue> schema(new DictionaryValue()); 256 scoped_ptr<DictionaryValue> schema(new DictionaryValue());
254 schema->SetString("type", "object"); 257 schema->SetString(kType, kObject);
255 schema->SetString("properties.foo.type", "string"); 258 schema->SetString("properties.foo.type", kString);
256 schema->SetString("properties.bar.$ref", "Max10Int"); 259 schema->SetString("properties.bar.$ref", "Max10Int");
257 schema->SetString("properties.baz.$ref", "MinLengthString"); 260 schema->SetString("properties.baz.$ref", "MinLengthString");
258 261
259 scoped_ptr<DictionaryValue> schema_inline(new DictionaryValue()); 262 scoped_ptr<DictionaryValue> schema_inline(new DictionaryValue());
260 schema_inline->SetString("type", "object"); 263 schema_inline->SetString(kType, kObject);
261 schema_inline->SetString("properties.foo.type", "string"); 264 schema_inline->SetString("properties.foo.type", kString);
262 schema_inline->SetString("properties.bar.id", "NegativeInt"); 265 schema_inline->SetString("properties.bar.id", "NegativeInt");
263 schema_inline->SetString("properties.bar.type", "integer"); 266 schema_inline->SetString("properties.bar.type", kInteger);
264 schema_inline->SetInteger("properties.bar.maximum", 0); 267 schema_inline->SetInteger("properties.bar.maximum", 0);
265 schema_inline->SetString("properties.baz.$ref", "NegativeInt"); 268 schema_inline->SetString("properties.baz.$ref", "NegativeInt");
266 269
267 scoped_ptr<DictionaryValue> instance(new DictionaryValue()); 270 scoped_ptr<DictionaryValue> instance(new DictionaryValue());
268 instance->SetString("foo", "foo"); 271 instance->SetString("foo", "foo");
269 instance->SetInteger("bar", 4); 272 instance->SetInteger("bar", 4);
270 instance->SetString("baz", "ab"); 273 instance->SetString("baz", "ab");
271 274
272 scoped_ptr<DictionaryValue> instance_inline(new DictionaryValue()); 275 scoped_ptr<DictionaryValue> instance_inline(new DictionaryValue());
273 instance_inline->SetString("foo", "foo"); 276 instance_inline->SetString("foo", "foo");
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
322 325
323 instance->Remove(1, NULL); 326 instance->Remove(1, NULL);
324 instance->Remove(1, NULL); 327 instance->Remove(1, NULL);
325 ExpectNotValid(TEST_SOURCE, instance.get(), schema.get(), NULL, "1", 328 ExpectNotValid(TEST_SOURCE, instance.get(), schema.get(), NULL, "1",
326 JSONSchemaValidator::kArrayItemRequired); 329 JSONSchemaValidator::kArrayItemRequired);
327 330
328 instance->Set(0, Value::CreateIntegerValue(42)); 331 instance->Set(0, Value::CreateIntegerValue(42));
329 instance->Append(Value::CreateIntegerValue(42)); 332 instance->Append(Value::CreateIntegerValue(42));
330 ExpectNotValid(TEST_SOURCE, instance.get(), schema.get(), NULL, "0", 333 ExpectNotValid(TEST_SOURCE, instance.get(), schema.get(), NULL, "0",
331 JSONSchemaValidator::FormatErrorMessage( 334 JSONSchemaValidator::FormatErrorMessage(
332 JSONSchemaValidator::kInvalidType, "string", "integer")); 335 JSONSchemaValidator::kInvalidType, kString, kInteger));
333 336
334 DictionaryValue* additional_properties = new DictionaryValue(); 337 DictionaryValue* additional_properties = new DictionaryValue();
335 additional_properties->SetString("type", "any"); 338 additional_properties->SetString(kType, kAny);
336 schema->Set("additionalProperties", additional_properties); 339 schema->Set(kAdditionalProperties, additional_properties);
337 instance->Set(0, Value::CreateStringValue("42")); 340 instance->Set(0, Value::CreateStringValue("42"));
338 instance->Append(Value::CreateStringValue("anything")); 341 instance->Append(Value::CreateStringValue("anything"));
339 ExpectValid(TEST_SOURCE, instance.get(), schema.get(), NULL); 342 ExpectValid(TEST_SOURCE, instance.get(), schema.get(), NULL);
340 instance->Set(2, new ListValue()); 343 instance->Set(2, new ListValue());
341 ExpectValid(TEST_SOURCE, instance.get(), schema.get(), NULL); 344 ExpectValid(TEST_SOURCE, instance.get(), schema.get(), NULL);
342 345
343 additional_properties->SetString("type", "boolean"); 346 additional_properties->SetString(kType, kBoolean);
344 ExpectNotValid(TEST_SOURCE, instance.get(), schema.get(), NULL, "2", 347 ExpectNotValid(TEST_SOURCE, instance.get(), schema.get(), NULL, "2",
345 JSONSchemaValidator::FormatErrorMessage( 348 JSONSchemaValidator::FormatErrorMessage(
346 JSONSchemaValidator::kInvalidType, "boolean", "array")); 349 JSONSchemaValidator::kInvalidType, kBoolean, kArray));
347 instance->Set(2, Value::CreateBooleanValue(false)); 350 instance->Set(2, Value::CreateBooleanValue(false));
348 ExpectValid(TEST_SOURCE, instance.get(), schema.get(), NULL); 351 ExpectValid(TEST_SOURCE, instance.get(), schema.get(), NULL);
349 352
350 ListValue* items_schema = NULL; 353 ListValue* items_schema = NULL;
351 DictionaryValue* item0_schema = NULL; 354 DictionaryValue* item0_schema = NULL;
352 ASSERT_TRUE(schema->GetList("items", &items_schema)); 355 ASSERT_TRUE(schema->GetList(kItems, &items_schema));
353 ASSERT_TRUE(items_schema->GetDictionary(0, &item0_schema)); 356 ASSERT_TRUE(items_schema->GetDictionary(0, &item0_schema));
354 item0_schema->SetBoolean("optional", true); 357 item0_schema->SetBoolean(kOptional, true);
355 instance->Remove(2, NULL); 358 instance->Remove(2, NULL);
356 ExpectValid(TEST_SOURCE, instance.get(), schema.get(), NULL); 359 ExpectValid(TEST_SOURCE, instance.get(), schema.get(), NULL);
357 // TODO(aa): I think this is inconsistent with the handling of NULL+optional 360 // TODO(aa): I think this is inconsistent with the handling of NULL+optional
358 // for objects. 361 // for objects.
359 instance->Set(0, Value::CreateNullValue()); 362 instance->Set(0, Value::CreateNullValue());
360 ExpectValid(TEST_SOURCE, instance.get(), schema.get(), NULL); 363 ExpectValid(TEST_SOURCE, instance.get(), schema.get(), NULL);
361 instance->Set(0, Value::CreateIntegerValue(42)); 364 instance->Set(0, Value::CreateIntegerValue(42));
362 ExpectNotValid(TEST_SOURCE, instance.get(), schema.get(), NULL, "0", 365 ExpectNotValid(TEST_SOURCE, instance.get(), schema.get(), NULL, "0",
363 JSONSchemaValidator::FormatErrorMessage( 366 JSONSchemaValidator::FormatErrorMessage(
364 JSONSchemaValidator::kInvalidType, "string", "integer")); 367 JSONSchemaValidator::kInvalidType, kString, kInteger));
365 } 368 }
366 369
367 void JSONSchemaValidatorTestBase::TestArrayNonTuple() { 370 void JSONSchemaValidatorTestBase::TestArrayNonTuple() {
368 scoped_ptr<DictionaryValue> schema(new DictionaryValue()); 371 scoped_ptr<DictionaryValue> schema(new DictionaryValue());
369 schema->SetString("type", "array"); 372 schema->SetString(kType, kArray);
370 schema->SetString("items.type", "string"); 373 schema->SetString("items.type", kString);
371 schema->SetInteger("minItems", 2); 374 schema->SetInteger(kMinItems, 2);
372 schema->SetInteger("maxItems", 3); 375 schema->SetInteger(kMaxItems, 3);
373 376
374 scoped_ptr<ListValue> instance(new ListValue()); 377 scoped_ptr<ListValue> instance(new ListValue());
375 instance->Append(Value::CreateStringValue("x")); 378 instance->Append(Value::CreateStringValue("x"));
376 instance->Append(Value::CreateStringValue("x")); 379 instance->Append(Value::CreateStringValue("x"));
377 380
378 ExpectValid(TEST_SOURCE, instance.get(), schema.get(), NULL); 381 ExpectValid(TEST_SOURCE, instance.get(), schema.get(), NULL);
379 instance->Append(Value::CreateStringValue("x")); 382 instance->Append(Value::CreateStringValue("x"));
380 ExpectValid(TEST_SOURCE, instance.get(), schema.get(), NULL); 383 ExpectValid(TEST_SOURCE, instance.get(), schema.get(), NULL);
381 384
382 instance->Append(Value::CreateStringValue("x")); 385 instance->Append(Value::CreateStringValue("x"));
383 ExpectNotValid(TEST_SOURCE, instance.get(), schema.get(), NULL, "", 386 ExpectNotValid(TEST_SOURCE, instance.get(), schema.get(), NULL, "",
384 JSONSchemaValidator::FormatErrorMessage( 387 JSONSchemaValidator::FormatErrorMessage(
385 JSONSchemaValidator::kArrayMaxItems, "3")); 388 JSONSchemaValidator::kArrayMaxItems, "3"));
386 instance->Remove(1, NULL); 389 instance->Remove(1, NULL);
387 instance->Remove(1, NULL); 390 instance->Remove(1, NULL);
388 instance->Remove(1, NULL); 391 instance->Remove(1, NULL);
389 ExpectNotValid(TEST_SOURCE, instance.get(), schema.get(), NULL, "", 392 ExpectNotValid(TEST_SOURCE, instance.get(), schema.get(), NULL, "",
390 JSONSchemaValidator::FormatErrorMessage( 393 JSONSchemaValidator::FormatErrorMessage(
391 JSONSchemaValidator::kArrayMinItems, "2")); 394 JSONSchemaValidator::kArrayMinItems, "2"));
392 395
393 instance->Remove(1, NULL); 396 instance->Remove(1, NULL);
394 instance->Append(Value::CreateIntegerValue(42)); 397 instance->Append(Value::CreateIntegerValue(42));
395 ExpectNotValid(TEST_SOURCE, instance.get(), schema.get(), NULL, "1", 398 ExpectNotValid(TEST_SOURCE, instance.get(), schema.get(), NULL, "1",
396 JSONSchemaValidator::FormatErrorMessage( 399 JSONSchemaValidator::FormatErrorMessage(
397 JSONSchemaValidator::kInvalidType, "string", "integer")); 400 JSONSchemaValidator::kInvalidType, kString, kInteger));
398 } 401 }
399 402
400 void JSONSchemaValidatorTestBase::TestString() { 403 void JSONSchemaValidatorTestBase::TestString() {
401 scoped_ptr<DictionaryValue> schema(new DictionaryValue()); 404 scoped_ptr<DictionaryValue> schema(new DictionaryValue());
402 schema->SetString("type", "string"); 405 schema->SetString(kType, kString);
403 schema->SetInteger("minLength", 1); 406 schema->SetInteger(kMinLength, 1);
404 schema->SetInteger("maxLength", 10); 407 schema->SetInteger(kMaxLength, 10);
405 408
406 ExpectValid(TEST_SOURCE, 409 ExpectValid(TEST_SOURCE,
407 scoped_ptr<Value>(Value::CreateStringValue("x")).get(), 410 scoped_ptr<Value>(Value::CreateStringValue("x")).get(),
408 schema.get(), NULL); 411 schema.get(), NULL);
409 ExpectValid(TEST_SOURCE, 412 ExpectValid(TEST_SOURCE,
410 scoped_ptr<Value>(Value::CreateStringValue("xxxxxxxxxx")).get(), 413 scoped_ptr<Value>(Value::CreateStringValue("xxxxxxxxxx")).get(),
411 schema.get(), NULL); 414 schema.get(), NULL);
412 415
413 ExpectNotValid(TEST_SOURCE, 416 ExpectNotValid(TEST_SOURCE,
414 scoped_ptr<Value>(Value::CreateStringValue("")).get(), 417 scoped_ptr<Value>(Value::CreateStringValue("")).get(),
415 schema.get(), NULL, "", 418 schema.get(), NULL, "",
416 JSONSchemaValidator::FormatErrorMessage( 419 JSONSchemaValidator::FormatErrorMessage(
417 JSONSchemaValidator::kStringMinLength, "1")); 420 JSONSchemaValidator::kStringMinLength, "1"));
418 ExpectNotValid( 421 ExpectNotValid(
419 TEST_SOURCE, 422 TEST_SOURCE,
420 scoped_ptr<Value>(Value::CreateStringValue("xxxxxxxxxxx")).get(), 423 scoped_ptr<Value>(Value::CreateStringValue("xxxxxxxxxxx")).get(),
421 schema.get(), NULL, "", 424 schema.get(), NULL, "",
422 JSONSchemaValidator::FormatErrorMessage( 425 JSONSchemaValidator::FormatErrorMessage(
423 JSONSchemaValidator::kStringMaxLength, "10")); 426 JSONSchemaValidator::kStringMaxLength, "10"));
424 427
425 } 428 }
426 429
427 void JSONSchemaValidatorTestBase::TestNumber() { 430 void JSONSchemaValidatorTestBase::TestNumber() {
428 scoped_ptr<DictionaryValue> schema(new DictionaryValue()); 431 scoped_ptr<DictionaryValue> schema(new DictionaryValue());
429 schema->SetString("type", "number"); 432 schema->SetString(kType, kNumber);
430 schema->SetInteger("minimum", 1); 433 schema->SetInteger(kMinimum, 1);
431 schema->SetInteger("maximum", 100); 434 schema->SetInteger(kMaximum, 100);
432 schema->SetInteger("maxDecimal", 2); 435 schema->SetInteger("maxDecimal", 2);
433 436
434 ExpectValid(TEST_SOURCE, 437 ExpectValid(TEST_SOURCE,
435 scoped_ptr<Value>(Value::CreateIntegerValue(1)).get(), 438 scoped_ptr<Value>(Value::CreateIntegerValue(1)).get(),
436 schema.get(), NULL); 439 schema.get(), NULL);
437 ExpectValid(TEST_SOURCE, 440 ExpectValid(TEST_SOURCE,
438 scoped_ptr<Value>(Value::CreateIntegerValue(50)).get(), 441 scoped_ptr<Value>(Value::CreateIntegerValue(50)).get(),
439 schema.get(), NULL); 442 schema.get(), NULL);
440 ExpectValid(TEST_SOURCE, 443 ExpectValid(TEST_SOURCE,
441 scoped_ptr<Value>(Value::CreateIntegerValue(100)).get(), 444 scoped_ptr<Value>(Value::CreateIntegerValue(100)).get(),
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after
498 EXPECT_EQ("object", JSONSchemaValidator::GetJSONSchemaType( 501 EXPECT_EQ("object", JSONSchemaValidator::GetJSONSchemaType(
499 scoped_ptr<Value>(new DictionaryValue()).get())); 502 scoped_ptr<Value>(new DictionaryValue()).get()));
500 EXPECT_EQ("null", JSONSchemaValidator::GetJSONSchemaType( 503 EXPECT_EQ("null", JSONSchemaValidator::GetJSONSchemaType(
501 scoped_ptr<Value>(Value::CreateNullValue()).get())); 504 scoped_ptr<Value>(Value::CreateNullValue()).get()));
502 } 505 }
503 506
504 void JSONSchemaValidatorTestBase::TestTypes() { 507 void JSONSchemaValidatorTestBase::TestTypes() {
505 scoped_ptr<DictionaryValue> schema(new DictionaryValue()); 508 scoped_ptr<DictionaryValue> schema(new DictionaryValue());
506 509
507 // valid 510 // valid
508 schema->SetString("type", "object"); 511 schema->SetString(kType, kObject);
509 ExpectValid(TEST_SOURCE, scoped_ptr<Value>(new DictionaryValue()).get(), 512 ExpectValid(TEST_SOURCE, scoped_ptr<Value>(new DictionaryValue()).get(),
510 schema.get(), NULL); 513 schema.get(), NULL);
511 514
512 schema->SetString("type", "array"); 515 schema->SetString(kType, kArray);
513 ExpectValid(TEST_SOURCE, scoped_ptr<Value>(new ListValue()).get(), 516 ExpectValid(TEST_SOURCE, scoped_ptr<Value>(new ListValue()).get(),
514 schema.get(), NULL); 517 schema.get(), NULL);
515 518
516 schema->SetString("type", "string"); 519 schema->SetString(kType, kString);
517 ExpectValid(TEST_SOURCE, 520 ExpectValid(TEST_SOURCE,
518 scoped_ptr<Value>(Value::CreateStringValue("foobar")).get(), 521 scoped_ptr<Value>(Value::CreateStringValue("foobar")).get(),
519 schema.get(), NULL); 522 schema.get(), NULL);
520 523
521 schema->SetString("type", "number"); 524 schema->SetString(kType, kNumber);
522 ExpectValid(TEST_SOURCE, 525 ExpectValid(TEST_SOURCE,
523 scoped_ptr<Value>(Value::CreateDoubleValue(88.8)).get(), 526 scoped_ptr<Value>(Value::CreateDoubleValue(88.8)).get(),
524 schema.get(), NULL); 527 schema.get(), NULL);
525 ExpectValid(TEST_SOURCE, 528 ExpectValid(TEST_SOURCE,
526 scoped_ptr<Value>(Value::CreateDoubleValue(42)).get(), 529 scoped_ptr<Value>(Value::CreateDoubleValue(42)).get(),
527 schema.get(), NULL); 530 schema.get(), NULL);
528 ExpectValid(TEST_SOURCE, 531 ExpectValid(TEST_SOURCE,
529 scoped_ptr<Value>(Value::CreateIntegerValue(42)).get(), 532 scoped_ptr<Value>(Value::CreateIntegerValue(42)).get(),
530 schema.get(), NULL); 533 schema.get(), NULL);
531 ExpectValid(TEST_SOURCE, 534 ExpectValid(TEST_SOURCE,
532 scoped_ptr<Value>(Value::CreateIntegerValue(0)).get(), 535 scoped_ptr<Value>(Value::CreateIntegerValue(0)).get(),
533 schema.get(), NULL); 536 schema.get(), NULL);
534 537
535 schema->SetString("type", "integer"); 538 schema->SetString(kType, kInteger);
536 ExpectValid(TEST_SOURCE, 539 ExpectValid(TEST_SOURCE,
537 scoped_ptr<Value>(Value::CreateIntegerValue(42)).get(), 540 scoped_ptr<Value>(Value::CreateIntegerValue(42)).get(),
538 schema.get(), NULL); 541 schema.get(), NULL);
539 ExpectValid(TEST_SOURCE, 542 ExpectValid(TEST_SOURCE,
540 scoped_ptr<Value>(Value::CreateDoubleValue(42)).get(), 543 scoped_ptr<Value>(Value::CreateDoubleValue(42)).get(),
541 schema.get(), NULL); 544 schema.get(), NULL);
542 ExpectValid(TEST_SOURCE, 545 ExpectValid(TEST_SOURCE,
543 scoped_ptr<Value>(Value::CreateIntegerValue(0)).get(), 546 scoped_ptr<Value>(Value::CreateIntegerValue(0)).get(),
544 schema.get(), NULL); 547 schema.get(), NULL);
545 ExpectValid(TEST_SOURCE, 548 ExpectValid(TEST_SOURCE,
546 scoped_ptr<Value>( 549 scoped_ptr<Value>(
547 Value::CreateDoubleValue(pow(2.0, DBL_MANT_DIG))).get(), 550 Value::CreateDoubleValue(pow(2.0, DBL_MANT_DIG))).get(),
548 schema.get(), NULL); 551 schema.get(), NULL);
549 ExpectValid(TEST_SOURCE, 552 ExpectValid(TEST_SOURCE,
550 scoped_ptr<Value>( 553 scoped_ptr<Value>(
551 Value::CreateDoubleValue(pow(-2.0, DBL_MANT_DIG))).get(), 554 Value::CreateDoubleValue(pow(-2.0, DBL_MANT_DIG))).get(),
552 schema.get(), NULL); 555 schema.get(), NULL);
553 556
554 schema->SetString("type", "boolean"); 557 schema->SetString(kType, kBoolean);
555 ExpectValid(TEST_SOURCE, 558 ExpectValid(TEST_SOURCE,
556 scoped_ptr<Value>(Value::CreateBooleanValue(false)).get(), 559 scoped_ptr<Value>(Value::CreateBooleanValue(false)).get(),
557 schema.get(), NULL); 560 schema.get(), NULL);
558 ExpectValid(TEST_SOURCE, 561 ExpectValid(TEST_SOURCE,
559 scoped_ptr<Value>(Value::CreateBooleanValue(true)).get(), 562 scoped_ptr<Value>(Value::CreateBooleanValue(true)).get(),
560 schema.get(), NULL); 563 schema.get(), NULL);
561 564
562 schema->SetString("type", "null"); 565 schema->SetString(kType, kNull);
563 ExpectValid(TEST_SOURCE, 566 ExpectValid(TEST_SOURCE,
564 scoped_ptr<Value>(Value::CreateNullValue()).get(), 567 scoped_ptr<Value>(Value::CreateNullValue()).get(),
565 schema.get(), NULL); 568 schema.get(), NULL);
566 569
567 // not valid 570 // not valid
568 schema->SetString("type", "object"); 571 schema->SetString(kType, kObject);
569 ExpectNotValid(TEST_SOURCE, scoped_ptr<Value>(new ListValue()).get(), 572 ExpectNotValid(TEST_SOURCE, scoped_ptr<Value>(new ListValue()).get(),
570 schema.get(), NULL, "", 573 schema.get(), NULL, "",
571 JSONSchemaValidator::FormatErrorMessage( 574 JSONSchemaValidator::FormatErrorMessage(
572 JSONSchemaValidator::kInvalidType, "object", "array")); 575 JSONSchemaValidator::kInvalidType, kObject, kArray));
573 576
574 schema->SetString("type", "object"); 577 schema->SetString(kType, kObject);
575 ExpectNotValid(TEST_SOURCE, scoped_ptr<Value>(Value::CreateNullValue()).get(), 578 ExpectNotValid(TEST_SOURCE, scoped_ptr<Value>(Value::CreateNullValue()).get(),
576 schema.get(), NULL, "", 579 schema.get(), NULL, "",
577 JSONSchemaValidator::FormatErrorMessage( 580 JSONSchemaValidator::FormatErrorMessage(
578 JSONSchemaValidator::kInvalidType, "object", "null")); 581 JSONSchemaValidator::kInvalidType, kObject, kNull));
579 582
580 schema->SetString("type", "array"); 583 schema->SetString(kType, kArray);
581 ExpectNotValid(TEST_SOURCE, 584 ExpectNotValid(TEST_SOURCE,
582 scoped_ptr<Value>(Value::CreateIntegerValue(42)).get(), 585 scoped_ptr<Value>(Value::CreateIntegerValue(42)).get(),
583 schema.get(), NULL, "", 586 schema.get(), NULL, "",
584 JSONSchemaValidator::FormatErrorMessage( 587 JSONSchemaValidator::FormatErrorMessage(
585 JSONSchemaValidator::kInvalidType, "array", "integer")); 588 JSONSchemaValidator::kInvalidType, kArray, kInteger));
586 589
587 schema->SetString("type", "string"); 590 schema->SetString(kType, kString);
588 ExpectNotValid(TEST_SOURCE, 591 ExpectNotValid(TEST_SOURCE,
589 scoped_ptr<Value>(Value::CreateIntegerValue(42)).get(), 592 scoped_ptr<Value>(Value::CreateIntegerValue(42)).get(),
590 schema.get(), NULL, "", 593 schema.get(), NULL, "",
591 JSONSchemaValidator::FormatErrorMessage( 594 JSONSchemaValidator::FormatErrorMessage(
592 JSONSchemaValidator::kInvalidType, "string", "integer")); 595 JSONSchemaValidator::kInvalidType, kString, kInteger));
593 596
594 schema->SetString("type", "number"); 597 schema->SetString(kType, kNumber);
595 ExpectNotValid(TEST_SOURCE, 598 ExpectNotValid(TEST_SOURCE,
596 scoped_ptr<Value>(Value::CreateStringValue("42")).get(), 599 scoped_ptr<Value>(Value::CreateStringValue("42")).get(),
597 schema.get(), NULL, "", 600 schema.get(), NULL, "",
598 JSONSchemaValidator::FormatErrorMessage( 601 JSONSchemaValidator::FormatErrorMessage(
599 JSONSchemaValidator::kInvalidType, "number", "string")); 602 JSONSchemaValidator::kInvalidType, kNumber, kString));
600 603
601 schema->SetString("type", "integer"); 604 schema->SetString(kType, kInteger);
602 ExpectNotValid(TEST_SOURCE, 605 ExpectNotValid(TEST_SOURCE,
603 scoped_ptr<Value>(Value::CreateDoubleValue(88.8)).get(), 606 scoped_ptr<Value>(Value::CreateDoubleValue(88.8)).get(),
604 schema.get(), NULL, "", 607 schema.get(), NULL, "",
605 JSONSchemaValidator::FormatErrorMessage( 608 JSONSchemaValidator::FormatErrorMessage(
606 JSONSchemaValidator::kInvalidType, "integer", "number")); 609 JSONSchemaValidator::kInvalidType, kInteger, kNumber));
607 610
608 schema->SetString("type", "integer"); 611 schema->SetString(kType, kInteger);
609 ExpectNotValid(TEST_SOURCE, 612 ExpectNotValid(TEST_SOURCE,
610 scoped_ptr<Value>(Value::CreateDoubleValue(88.8)).get(), 613 scoped_ptr<Value>(Value::CreateDoubleValue(88.8)).get(),
611 schema.get(), NULL, "", 614 schema.get(), NULL, "",
612 JSONSchemaValidator::FormatErrorMessage( 615 JSONSchemaValidator::FormatErrorMessage(
613 JSONSchemaValidator::kInvalidType, "integer", "number")); 616 JSONSchemaValidator::kInvalidType, kInteger, kNumber));
614 617
615 schema->SetString("type", "boolean"); 618 schema->SetString(kType, kBoolean);
616 ExpectNotValid(TEST_SOURCE, 619 ExpectNotValid(TEST_SOURCE,
617 scoped_ptr<Value>(Value::CreateIntegerValue(1)).get(), 620 scoped_ptr<Value>(Value::CreateIntegerValue(1)).get(),
618 schema.get(), NULL, "", 621 schema.get(), NULL, "",
619 JSONSchemaValidator::FormatErrorMessage( 622 JSONSchemaValidator::FormatErrorMessage(
620 JSONSchemaValidator::kInvalidType, "boolean", "integer")); 623 JSONSchemaValidator::kInvalidType, kBoolean, kInteger));
621 624
622 schema->SetString("type", "null"); 625 schema->SetString(kType, kNull);
623 ExpectNotValid(TEST_SOURCE, 626 ExpectNotValid(TEST_SOURCE,
624 scoped_ptr<Value>(Value::CreateBooleanValue(false)).get(), 627 scoped_ptr<Value>(Value::CreateBooleanValue(false)).get(),
625 schema.get(), NULL, "", 628 schema.get(), NULL, "",
626 JSONSchemaValidator::FormatErrorMessage( 629 JSONSchemaValidator::FormatErrorMessage(
627 JSONSchemaValidator::kInvalidType, "null", "boolean")); 630 JSONSchemaValidator::kInvalidType, kNull, kBoolean));
628 } 631 }
OLDNEW
« chrome/common/json_schema_constants.h ('K') | « chrome/common/json_schema_validator.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698