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/policy/core/common/schema.h" | 5 #include "components/policy/core/common/schema.h" |
6 | 6 |
7 #include "components/policy/core/common/schema_internal.h" | 7 #include "components/policy/core/common/schema_internal.h" |
8 #include "testing/gtest/include/gtest/gtest.h" | 8 #include "testing/gtest/include/gtest/gtest.h" |
9 | 9 |
10 namespace policy { | 10 namespace policy { |
(...skipping 30 matching lines...) Expand all Loading... | |
41 " \"items\": { \"type\": \"string\" }" | 41 " \"items\": { \"type\": \"string\" }" |
42 " }" | 42 " }" |
43 " }," | 43 " }," |
44 " \"Object\": {" | 44 " \"Object\": {" |
45 " \"type\": \"object\"," | 45 " \"type\": \"object\"," |
46 " \"properties\": {" | 46 " \"properties\": {" |
47 " \"one\": { \"type\": \"boolean\" }," | 47 " \"one\": { \"type\": \"boolean\" }," |
48 " \"two\": { \"type\": \"integer\" }" | 48 " \"two\": { \"type\": \"integer\" }" |
49 " }," | 49 " }," |
50 " \"additionalProperties\": { \"type\": \"string\" }" | 50 " \"additionalProperties\": { \"type\": \"string\" }" |
51 " }," | |
52 " \"IntegerWithEnums\": {" | |
53 " \"type\": \"integer\"," | |
54 " \"enum\": [1, 2, 3]" | |
Joao da Silva
2014/01/20 20:22:18
This enumeration will get converted into a range (
binjin
2014/01/21 16:12:07
Done.
| |
55 " }," | |
56 " \"StringWithEnums\": {" | |
57 " \"type\": \"string\"," | |
58 " \"enum\": [\"one\", \"two\", \"three\"]" | |
59 " }," | |
60 " \"IntegerWithRange\": {" | |
61 " \"type\": \"integer\"," | |
62 " \"minimum\": 1," | |
63 " \"maximum\": 3" | |
51 " }" | 64 " }" |
52 " }" | 65 " }" |
53 "}"; | 66 "}"; |
54 | 67 |
55 bool ParseFails(const std::string& content) { | 68 bool ParseFails(const std::string& content) { |
56 std::string error; | 69 std::string error; |
57 Schema schema = Schema::Parse(content, &error); | 70 Schema schema = Schema::Parse(content, &error); |
58 if (schema.valid()) | 71 if (schema.valid()) |
59 return false; | 72 return false; |
60 EXPECT_FALSE(error.empty()); | 73 EXPECT_FALSE(error.empty()); |
(...skipping 170 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
231 subsub = sub.GetProperty("one"); | 244 subsub = sub.GetProperty("one"); |
232 ASSERT_TRUE(subsub.valid()); | 245 ASSERT_TRUE(subsub.valid()); |
233 EXPECT_EQ(base::Value::TYPE_BOOLEAN, subsub.type()); | 246 EXPECT_EQ(base::Value::TYPE_BOOLEAN, subsub.type()); |
234 subsub = sub.GetProperty("two"); | 247 subsub = sub.GetProperty("two"); |
235 ASSERT_TRUE(subsub.valid()); | 248 ASSERT_TRUE(subsub.valid()); |
236 EXPECT_EQ(base::Value::TYPE_INTEGER, subsub.type()); | 249 EXPECT_EQ(base::Value::TYPE_INTEGER, subsub.type()); |
237 subsub = sub.GetProperty("undeclared"); | 250 subsub = sub.GetProperty("undeclared"); |
238 ASSERT_TRUE(subsub.valid()); | 251 ASSERT_TRUE(subsub.valid()); |
239 EXPECT_EQ(base::Value::TYPE_STRING, subsub.type()); | 252 EXPECT_EQ(base::Value::TYPE_STRING, subsub.type()); |
240 | 253 |
254 sub = schema.GetProperty("IntegerWithEnums"); | |
255 ASSERT_TRUE(sub.valid()); | |
256 ASSERT_EQ(base::Value::TYPE_INTEGER, sub.type()); | |
257 | |
258 sub = schema.GetProperty("StringWithEnums"); | |
259 ASSERT_TRUE(sub.valid()); | |
260 ASSERT_EQ(base::Value::TYPE_STRING, sub.type()); | |
261 | |
262 sub = schema.GetProperty("IntegerWithRange"); | |
263 ASSERT_TRUE(sub.valid()); | |
264 ASSERT_EQ(base::Value::TYPE_INTEGER, sub.type()); | |
265 | |
241 struct { | 266 struct { |
242 const char* expected_key; | 267 const char* expected_key; |
243 base::Value::Type expected_type; | 268 base::Value::Type expected_type; |
244 } kExpectedProperties[] = { | 269 } kExpectedProperties[] = { |
245 { "Array", base::Value::TYPE_LIST }, | 270 { "Array", base::Value::TYPE_LIST }, |
246 { "ArrayOfArray", base::Value::TYPE_LIST }, | 271 { "ArrayOfArray", base::Value::TYPE_LIST }, |
247 { "ArrayOfObjects", base::Value::TYPE_LIST }, | 272 { "ArrayOfObjects", base::Value::TYPE_LIST }, |
248 { "Boolean", base::Value::TYPE_BOOLEAN }, | 273 { "Boolean", base::Value::TYPE_BOOLEAN }, |
249 { "Integer", base::Value::TYPE_INTEGER }, | 274 { "Integer", base::Value::TYPE_INTEGER }, |
275 { "IntegerWithEnums", base::Value::TYPE_INTEGER }, | |
276 { "IntegerWithRange", base::Value::TYPE_INTEGER }, | |
250 { "Null", base::Value::TYPE_NULL }, | 277 { "Null", base::Value::TYPE_NULL }, |
251 { "Number", base::Value::TYPE_DOUBLE }, | 278 { "Number", base::Value::TYPE_DOUBLE }, |
252 { "Object", base::Value::TYPE_DICTIONARY }, | 279 { "Object", base::Value::TYPE_DICTIONARY }, |
253 { "String", base::Value::TYPE_STRING }, | 280 { "String", base::Value::TYPE_STRING }, |
281 { "StringWithEnums", base::Value::TYPE_STRING }, | |
254 }; | 282 }; |
255 Schema::Iterator it = schema.GetPropertiesIterator(); | 283 Schema::Iterator it = schema.GetPropertiesIterator(); |
256 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kExpectedProperties); ++i) { | 284 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kExpectedProperties); ++i) { |
257 ASSERT_FALSE(it.IsAtEnd()); | 285 ASSERT_FALSE(it.IsAtEnd()); |
258 EXPECT_STREQ(kExpectedProperties[i].expected_key, it.key()); | 286 EXPECT_STREQ(kExpectedProperties[i].expected_key, it.key()); |
259 ASSERT_TRUE(it.schema().valid()); | 287 ASSERT_TRUE(it.schema().valid()); |
260 EXPECT_EQ(kExpectedProperties[i].expected_type, it.schema().type()); | 288 EXPECT_EQ(kExpectedProperties[i].expected_type, it.schema().type()); |
261 it.Advance(); | 289 it.Advance(); |
262 } | 290 } |
263 EXPECT_TRUE(it.IsAtEnd()); | 291 EXPECT_TRUE(it.IsAtEnd()); |
(...skipping 232 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
496 | 524 |
497 { | 525 { |
498 base::DictionaryValue dict; | 526 base::DictionaryValue dict; |
499 dict.SetBoolean("one", true); | 527 dict.SetBoolean("one", true); |
500 dict.SetInteger("two", 2); | 528 dict.SetInteger("two", 2); |
501 dict.SetString("additionally", "a string"); | 529 dict.SetString("additionally", "a string"); |
502 dict.SetString("and also", "another string"); | 530 dict.SetString("and also", "another string"); |
503 bundle.Set("Object", dict.DeepCopy()); | 531 bundle.Set("Object", dict.DeepCopy()); |
504 } | 532 } |
505 | 533 |
534 bundle.SetInteger("IntegerWithEnums", 1); | |
535 bundle.SetString("StringWithEnums", "two"); | |
536 bundle.SetInteger("IntegerWithRange", 3); | |
537 | |
506 EXPECT_TRUE(schema.Validate(bundle)); | 538 EXPECT_TRUE(schema.Validate(bundle)); |
507 | 539 |
508 bundle.SetString("boom", "bang"); | 540 bundle.SetString("boom", "bang"); |
509 EXPECT_FALSE(schema.Validate(bundle)); | 541 EXPECT_FALSE(schema.Validate(bundle)); |
542 bundle.Remove("boom", NULL); | |
543 | |
544 bundle.SetInteger("IntegerWithEnums", 0); | |
545 EXPECT_FALSE(schema.Validate(bundle)); | |
546 bundle.SetInteger("IntegerWithEnums", 1); | |
547 | |
548 bundle.SetString("StringWithEnums", "FOUR"); | |
549 EXPECT_FALSE(schema.Validate(bundle)); | |
550 bundle.SetString("StringWithEnums", "two"); | |
551 | |
552 bundle.SetInteger("IntegerWithRange", 4); | |
553 EXPECT_FALSE(schema.Validate(bundle)); | |
554 bundle.SetInteger("IntegerWithRange", 3); | |
510 | 555 |
Joao da Silva
2014/01/20 20:22:18
After adding the integer enum with gaps, test valu
binjin
2014/01/21 16:12:07
Done.
| |
511 } | 556 } |
512 TEST(SchemaTest, InvalidReferences) { | 557 TEST(SchemaTest, InvalidReferences) { |
513 // References to undeclared schemas fail. | 558 // References to undeclared schemas fail. |
514 EXPECT_TRUE(ParseFails( | 559 EXPECT_TRUE(ParseFails( |
515 "{" | 560 "{" |
516 " \"type\": \"object\"," | 561 " \"type\": \"object\"," |
517 " \"properties\": {" | 562 " \"properties\": {" |
518 " \"name\": { \"$ref\": \"undeclared\" }" | 563 " \"name\": { \"$ref\": \"undeclared\" }" |
519 " }" | 564 " }" |
520 "}")); | 565 "}")); |
(...skipping 240 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
761 | 806 |
762 EXPECT_FALSE(ParseFails(SchemaObjectWrapper( | 807 EXPECT_FALSE(ParseFails(SchemaObjectWrapper( |
763 "{" | 808 "{" |
764 " \"type\": \"integer\"," | 809 " \"type\": \"integer\"," |
765 " \"minimum\": 10," | 810 " \"minimum\": 10," |
766 " \"maximum\": 20" | 811 " \"maximum\": 20" |
767 "}"))); | 812 "}"))); |
768 } | 813 } |
769 | 814 |
770 } // namespace policy | 815 } // namespace policy |
OLD | NEW |