Index: components/policy/core/common/schema_unittest.cc |
diff --git a/components/policy/core/common/schema_unittest.cc b/components/policy/core/common/schema_unittest.cc |
index 8effd896aabab93aaf9ec32abf1f6e6491f4fff1..cd8137a362446376976394f5da5f1773320e16c0 100644 |
--- a/components/policy/core/common/schema_unittest.cc |
+++ b/components/policy/core/common/schema_unittest.cc |
@@ -5,6 +5,7 @@ |
#include "components/policy/core/common/schema.h" |
#include "base/memory/scoped_ptr.h" |
+#include "base/strings/stringprintf.h" |
#include "components/policy/core/common/schema_internal.h" |
#include "testing/gtest/include/gtest/gtest.h" |
@@ -12,6 +13,8 @@ namespace policy { |
namespace { |
+#define TEST_SOURCE base::StringPrintf("%s:%i", __FILE__, __LINE__) |
Joao da Silva
2014/04/02 09:51:11
An easier way to do this:
#define TestSchemaVal
binjin
2014/04/03 10:13:37
Done.
|
+ |
const char kTestSchema[] = |
"{" |
" \"type\": \"object\"," |
@@ -99,6 +102,26 @@ const char kTestSchema[] = |
" }" |
" }" |
" }" |
+ " }," |
+ " \"StringWithPattern\": {" |
+ " \"type\": \"string\"," |
+ " \"pattern\": \"^foo+$\"" |
+ " }," |
+ " \"ObjectWithPatternProperties\": {" |
+ " \"type\": \"object\"," |
+ " \"patternProperties\": {" |
+ " \"^foo+$\": { \"type\": \"integer\" }," |
+ " \"^bar+$\": {" |
+ " \"type\": \"string\"," |
+ " \"enum\": [\"one\", \"two\"]" |
+ " }" |
+ " }," |
+ " \"properties\": {" |
+ " \"bar\": {" |
+ " \"type\": \"string\"," |
+ " \"enum\": [\"one\", \"three\"]" |
+ " }" |
+ " }" |
" }" |
" }" |
"}"; |
@@ -112,7 +135,8 @@ bool ParseFails(const std::string& content) { |
return true; |
} |
-void TestSchemaValidation(Schema schema, |
+void TestSchemaValidation(const std::string& source, |
+ Schema schema, |
const base::Value& value, |
SchemaOnErrorStrategy strategy, |
bool expected_return_value) { |
@@ -122,7 +146,7 @@ void TestSchemaValidation(Schema schema, |
// Test that Schema::Validate() works as expected. |
error = kNoErrorReturned; |
bool returned = schema.Validate(value, strategy, NULL, &error); |
- EXPECT_EQ(returned, expected_return_value) << error; |
+ ASSERT_EQ(expected_return_value, returned) << source << ": " << error; |
// Test that Schema::Normalize() will return the same value as |
// Schema::Validate(). |
@@ -131,18 +155,20 @@ void TestSchemaValidation(Schema schema, |
bool touched = false; |
returned = |
schema.Normalize(cloned_value.get(), strategy, NULL, &error, &touched); |
- EXPECT_EQ(returned, expected_return_value) << error; |
+ EXPECT_EQ(expected_return_value, returned) << source << ": " << error; |
bool strictly_valid = schema.Validate(value, SCHEMA_STRICT, NULL, &error); |
- EXPECT_EQ(!strictly_valid && returned, touched); |
+ EXPECT_EQ(touched, !strictly_valid && returned) << source; |
// Test that Schema::Normalize() have actually dropped invalid and unknown |
// properties. |
if (expected_return_value) { |
EXPECT_TRUE( |
- schema.Validate(*cloned_value.get(), SCHEMA_STRICT, NULL, &error)); |
- EXPECT_TRUE(schema.Normalize( |
- cloned_value.get(), SCHEMA_STRICT, NULL, &error, NULL)); |
+ schema.Validate(*cloned_value.get(), SCHEMA_STRICT, NULL, &error)) |
+ << source; |
+ EXPECT_TRUE( |
+ schema.Normalize(cloned_value.get(), SCHEMA_STRICT, NULL, &error, NULL)) |
+ << source; |
} |
} |
@@ -350,26 +376,36 @@ TEST(SchemaTest, ValidSchema) { |
ASSERT_TRUE(sub.valid()); |
ASSERT_EQ(base::Value::TYPE_INTEGER, sub.type()); |
+ sub = schema.GetProperty("StringWithPattern"); |
+ ASSERT_TRUE(sub.valid()); |
+ ASSERT_EQ(base::Value::TYPE_STRING, sub.type()); |
+ |
+ sub = schema.GetProperty("ObjectWithPatternProperties"); |
+ ASSERT_TRUE(sub.valid()); |
+ ASSERT_EQ(base::Value::TYPE_DICTIONARY, sub.type()); |
+ |
struct { |
const char* expected_key; |
base::Value::Type expected_type; |
} kExpectedProperties[] = { |
- { "Array", base::Value::TYPE_LIST }, |
- { "ArrayOfArray", base::Value::TYPE_LIST }, |
- { "ArrayOfObjectOfArray", base::Value::TYPE_LIST }, |
- { "ArrayOfObjects", base::Value::TYPE_LIST }, |
- { "Boolean", base::Value::TYPE_BOOLEAN }, |
- { "Integer", base::Value::TYPE_INTEGER }, |
- { "IntegerWithEnums", base::Value::TYPE_INTEGER }, |
- { "IntegerWithEnumsGaps", base::Value::TYPE_INTEGER }, |
- { "IntegerWithRange", base::Value::TYPE_INTEGER }, |
- { "Null", base::Value::TYPE_NULL }, |
- { "Number", base::Value::TYPE_DOUBLE }, |
- { "Object", base::Value::TYPE_DICTIONARY }, |
- { "ObjectOfArray", base::Value::TYPE_DICTIONARY }, |
- { "ObjectOfObject", base::Value::TYPE_DICTIONARY }, |
- { "String", base::Value::TYPE_STRING }, |
- { "StringWithEnums", base::Value::TYPE_STRING }, |
+ { "Array", base::Value::TYPE_LIST }, |
+ { "ArrayOfArray", base::Value::TYPE_LIST }, |
+ { "ArrayOfObjectOfArray", base::Value::TYPE_LIST }, |
+ { "ArrayOfObjects", base::Value::TYPE_LIST }, |
+ { "Boolean", base::Value::TYPE_BOOLEAN }, |
+ { "Integer", base::Value::TYPE_INTEGER }, |
+ { "IntegerWithEnums", base::Value::TYPE_INTEGER }, |
+ { "IntegerWithEnumsGaps", base::Value::TYPE_INTEGER }, |
+ { "IntegerWithRange", base::Value::TYPE_INTEGER }, |
+ { "Null", base::Value::TYPE_NULL }, |
+ { "Number", base::Value::TYPE_DOUBLE }, |
+ { "Object", base::Value::TYPE_DICTIONARY }, |
+ { "ObjectOfArray", base::Value::TYPE_DICTIONARY }, |
+ { "ObjectOfObject", base::Value::TYPE_DICTIONARY }, |
+ { "ObjectWithPatternProperties", base::Value::TYPE_DICTIONARY }, |
+ { "String", base::Value::TYPE_STRING }, |
+ { "StringWithEnums", base::Value::TYPE_STRING }, |
+ { "StringWithPattern", base::Value::TYPE_STRING }, |
}; |
Schema::Iterator it = schema.GetPropertiesIterator(); |
for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kExpectedProperties); ++i) { |
@@ -444,16 +480,17 @@ TEST(SchemaTest, Lookups) { |
TEST(SchemaTest, Wrap) { |
const internal::SchemaNode kSchemas[] = { |
- { base::Value::TYPE_DICTIONARY, 0 }, // 0: root node |
- { base::Value::TYPE_BOOLEAN, -1 }, // 1 |
- { base::Value::TYPE_INTEGER, -1 }, // 2 |
- { base::Value::TYPE_DOUBLE, -1 }, // 3 |
- { base::Value::TYPE_STRING, -1 }, // 4 |
- { base::Value::TYPE_LIST, 4 }, // 5: list of strings. |
- { base::Value::TYPE_LIST, 5 }, // 6: list of lists of strings. |
- { base::Value::TYPE_INTEGER, 0 }, // 7: integer enumerations. |
- { base::Value::TYPE_INTEGER, 1 }, // 8: ranged integers. |
- { base::Value::TYPE_STRING, 2 }, // 9: string enumerations. |
+ { base::Value::TYPE_DICTIONARY, 0 }, // 0: root node |
+ { base::Value::TYPE_BOOLEAN, -1 }, // 1 |
+ { base::Value::TYPE_INTEGER, -1 }, // 2 |
+ { base::Value::TYPE_DOUBLE, -1 }, // 3 |
+ { base::Value::TYPE_STRING, -1 }, // 4 |
+ { base::Value::TYPE_LIST, 4 }, // 5: list of strings. |
+ { base::Value::TYPE_LIST, 5 }, // 6: list of lists of strings. |
+ { base::Value::TYPE_INTEGER, 0 }, // 7: integer enumerations. |
+ { base::Value::TYPE_INTEGER, 1 }, // 8: ranged integers. |
+ { base::Value::TYPE_STRING, 2 }, // 9: string enumerations. |
+ { base::Value::TYPE_STRING, 3 }, // 10: string with pattern. |
}; |
const internal::PropertyNode kPropertyNodes[] = { |
@@ -465,26 +502,30 @@ TEST(SchemaTest, Wrap) { |
{ "IntEnum", 7 }, // 5 |
{ "RangedInt", 8 }, // 6 |
{ "StrEnum", 9 }, // 7 |
+ { "StrPat", 10 }, // 8 |
+ { "bar+$", 4 }, // 9 |
Joao da Silva
2014/04/02 09:51:11
nit: remove a space before the "4" to align the re
binjin
2014/04/03 10:13:37
Done.
|
}; |
const internal::PropertiesNode kProperties[] = { |
- // 0 to 8 (exclusive) are the known properties in kPropertyNodes, and 6 is |
- // the addionalProperties node. |
- { 0, 8, 6 }, |
+ // 0 to 9 (exclusive) are the known properties in kPropertyNodes, 9 is |
+ // patternProperties and 6 is the addionalProperties node. |
Joao da Silva
2014/04/02 09:51:11
additionalProperties
binjin
2014/04/03 10:13:37
Done.
|
+ { 0, 9, 10, 6 }, |
}; |
const internal::RestrictionNode kRestriction[] = { |
- {{0, 3}}, // [1, 2, 3] |
- {{5, 1}}, // minimum = 1, maximum = 5 |
- {{0, 3}}, // ["one", "two", "three"] |
+ {{0, 3}}, // 0: [1, 2, 3] |
+ {{5, 1}}, // 1: minimum = 1, maximum = 5 |
+ {{0, 3}}, // 2: ["one", "two", "three"] |
+ {{3, 3}}, // 3: pattern "foo+" |
}; |
const int kIntEnums[] = {1, 2, 3}; |
const char* kStringEnums[] = { |
- "one", |
- "two", |
- "three", |
+ "one", // 0 |
+ "two", // 1 |
+ "three", // 2 |
+ "foo+", // 3 |
}; |
const internal::SchemaData kData = { |
@@ -511,7 +552,8 @@ TEST(SchemaTest, Wrap) { |
{ "List", base::Value::TYPE_LIST }, |
{ "IntEnum", base::Value::TYPE_INTEGER }, |
{ "RangedInt", base::Value::TYPE_INTEGER }, |
- { "StrEnum", base::Value::TYPE_STRING } |
+ { "StrEnum", base::Value::TYPE_STRING }, |
+ { "StrPat", base::Value::TYPE_STRING }, |
}; |
Schema::Iterator it = schema.GetPropertiesIterator(); |
@@ -541,6 +583,15 @@ TEST(SchemaTest, Wrap) { |
Schema subsubsub = subsub.GetItems(); |
ASSERT_TRUE(subsubsub.valid()); |
ASSERT_EQ(base::Value::TYPE_STRING, subsubsub.type()); |
+ |
+ Schema::SchemaList schema_list = schema.GetPatternProperties("barr"); |
+ ASSERT_EQ(static_cast<size_t>(1), schema_list.size()); |
Joao da Silva
2014/04/02 09:51:11
1u
binjin
2014/04/03 10:13:37
Done.
|
+ sub = schema_list[0]; |
+ ASSERT_TRUE(sub.valid()); |
+ EXPECT_EQ(base::Value::TYPE_STRING, sub.type()); |
+ |
+ EXPECT_TRUE(schema.GetPatternProperties("ba").empty()); |
+ EXPECT_TRUE(schema.GetPatternProperties("bar+$").empty()); |
} |
TEST(SchemaTest, Validate) { |
@@ -549,11 +600,11 @@ TEST(SchemaTest, Validate) { |
ASSERT_TRUE(schema.valid()) << error; |
base::DictionaryValue bundle; |
- TestSchemaValidation(schema, bundle, SCHEMA_STRICT, true); |
+ TestSchemaValidation(TEST_SOURCE, schema, bundle, SCHEMA_STRICT, true); |
// Wrong type, expected integer. |
bundle.SetBoolean("Integer", true); |
- TestSchemaValidation(schema, bundle, SCHEMA_STRICT, false); |
+ TestSchemaValidation(TEST_SOURCE, schema, bundle, SCHEMA_STRICT, false); |
// Wrong type, expected list of strings. |
{ |
@@ -561,7 +612,7 @@ TEST(SchemaTest, Validate) { |
base::ListValue list; |
list.AppendInteger(1); |
bundle.Set("Array", list.DeepCopy()); |
- TestSchemaValidation(schema, bundle, SCHEMA_STRICT, false); |
+ TestSchemaValidation(TEST_SOURCE, schema, bundle, SCHEMA_STRICT, false); |
} |
// Wrong type in a sub-object. |
@@ -570,13 +621,13 @@ TEST(SchemaTest, Validate) { |
base::DictionaryValue dict; |
dict.SetString("one", "one"); |
bundle.Set("Object", dict.DeepCopy()); |
- TestSchemaValidation(schema, bundle, SCHEMA_STRICT, false); |
+ TestSchemaValidation(TEST_SOURCE, schema, bundle, SCHEMA_STRICT, false); |
} |
// Unknown name. |
bundle.Clear(); |
bundle.SetBoolean("Unknown", true); |
- TestSchemaValidation(schema, bundle, SCHEMA_STRICT, false); |
+ TestSchemaValidation(TEST_SOURCE, schema, bundle, SCHEMA_STRICT, false); |
// All of these will be valid. |
bundle.Clear(); |
@@ -627,55 +678,57 @@ TEST(SchemaTest, Validate) { |
bundle.SetString("StringWithEnums", "two"); |
bundle.SetInteger("IntegerWithRange", 3); |
- TestSchemaValidation(schema, bundle, SCHEMA_STRICT, true); |
+ TestSchemaValidation(TEST_SOURCE, schema, bundle, SCHEMA_STRICT, true); |
bundle.SetInteger("IntegerWithEnums", 0); |
- TestSchemaValidation(schema, bundle, SCHEMA_STRICT, false); |
+ TestSchemaValidation(TEST_SOURCE, schema, bundle, SCHEMA_STRICT, false); |
bundle.SetInteger("IntegerWithEnums", 1); |
bundle.SetInteger("IntegerWithEnumsGaps", 0); |
- TestSchemaValidation(schema, bundle, SCHEMA_STRICT, false); |
+ TestSchemaValidation(TEST_SOURCE, schema, bundle, SCHEMA_STRICT, false); |
bundle.SetInteger("IntegerWithEnumsGaps", 9); |
- TestSchemaValidation(schema, bundle, SCHEMA_STRICT, false); |
+ TestSchemaValidation(TEST_SOURCE, schema, bundle, SCHEMA_STRICT, false); |
bundle.SetInteger("IntegerWithEnumsGaps", 10); |
- TestSchemaValidation(schema, bundle, SCHEMA_STRICT, true); |
+ TestSchemaValidation(TEST_SOURCE, schema, bundle, SCHEMA_STRICT, true); |
bundle.SetInteger("IntegerWithEnumsGaps", 11); |
- TestSchemaValidation(schema, bundle, SCHEMA_STRICT, false); |
+ TestSchemaValidation(TEST_SOURCE, schema, bundle, SCHEMA_STRICT, false); |
bundle.SetInteger("IntegerWithEnumsGaps", 19); |
- TestSchemaValidation(schema, bundle, SCHEMA_STRICT, false); |
+ TestSchemaValidation(TEST_SOURCE, schema, bundle, SCHEMA_STRICT, false); |
bundle.SetInteger("IntegerWithEnumsGaps", 21); |
- TestSchemaValidation(schema, bundle, SCHEMA_STRICT, false); |
+ TestSchemaValidation(TEST_SOURCE, schema, bundle, SCHEMA_STRICT, false); |
bundle.SetInteger("IntegerWithEnumsGaps", 29); |
- TestSchemaValidation(schema, bundle, SCHEMA_STRICT, false); |
+ TestSchemaValidation(TEST_SOURCE, schema, bundle, SCHEMA_STRICT, false); |
bundle.SetInteger("IntegerWithEnumsGaps", 30); |
- TestSchemaValidation(schema, bundle, SCHEMA_STRICT, true); |
+ TestSchemaValidation(TEST_SOURCE, schema, bundle, SCHEMA_STRICT, true); |
bundle.SetInteger("IntegerWithEnumsGaps", 31); |
- TestSchemaValidation(schema, bundle, SCHEMA_STRICT, false); |
+ TestSchemaValidation(TEST_SOURCE, schema, bundle, SCHEMA_STRICT, false); |
bundle.SetInteger("IntegerWithEnumsGaps", 100); |
- TestSchemaValidation(schema, bundle, SCHEMA_STRICT, false); |
+ TestSchemaValidation(TEST_SOURCE, schema, bundle, SCHEMA_STRICT, false); |
bundle.SetInteger("IntegerWithEnumsGaps", 20); |
bundle.SetString("StringWithEnums", "FOUR"); |
- TestSchemaValidation(schema, bundle, SCHEMA_STRICT, false); |
+ TestSchemaValidation(TEST_SOURCE, schema, bundle, SCHEMA_STRICT, false); |
bundle.SetString("StringWithEnums", "two"); |
bundle.SetInteger("IntegerWithRange", 4); |
- TestSchemaValidation(schema, bundle, SCHEMA_STRICT, false); |
+ TestSchemaValidation(TEST_SOURCE, schema, bundle, SCHEMA_STRICT, false); |
bundle.SetInteger("IntegerWithRange", 3); |
// Unknown top level property. |
bundle.SetString("boom", "bang"); |
- TestSchemaValidation(schema, bundle, SCHEMA_STRICT, false); |
- TestSchemaValidation(schema, bundle, SCHEMA_ALLOW_UNKNOWN_TOPLEVEL, true); |
- TestSchemaValidation(schema, bundle, SCHEMA_ALLOW_UNKNOWN, true); |
+ TestSchemaValidation(TEST_SOURCE, schema, bundle, SCHEMA_STRICT, false); |
+ TestSchemaValidation( |
+ TEST_SOURCE, schema, bundle, SCHEMA_ALLOW_UNKNOWN_TOPLEVEL, true); |
+ TestSchemaValidation(TEST_SOURCE, schema, bundle, SCHEMA_ALLOW_UNKNOWN, true); |
TestSchemaValidationWithPath(schema, bundle, ""); |
bundle.Remove("boom", NULL); |
// Invalid top level property. |
bundle.SetInteger("Boolean", 12345); |
- TestSchemaValidation(schema, bundle, SCHEMA_STRICT, false); |
- TestSchemaValidation(schema, bundle, SCHEMA_ALLOW_INVALID_TOPLEVEL, true); |
- TestSchemaValidation(schema, bundle, SCHEMA_ALLOW_INVALID, true); |
+ TestSchemaValidation(TEST_SOURCE, schema, bundle, SCHEMA_STRICT, false); |
+ TestSchemaValidation( |
+ TEST_SOURCE, schema, bundle, SCHEMA_ALLOW_INVALID_TOPLEVEL, true); |
+ TestSchemaValidation(TEST_SOURCE, schema, bundle, SCHEMA_ALLOW_INVALID, true); |
TestSchemaValidationWithPath(schema, bundle, "Boolean"); |
bundle.SetBoolean("Boolean", true); |
@@ -687,21 +740,29 @@ TEST(SchemaTest, Validate) { |
// Unknown property. |
root.SetBoolean("Object.three", false); |
- TestSchemaValidation(subschema, root, SCHEMA_STRICT, false); |
- TestSchemaValidation(subschema, root, SCHEMA_ALLOW_UNKNOWN_TOPLEVEL, false); |
- TestSchemaValidation(subschema, root, SCHEMA_ALLOW_UNKNOWN, true); |
- TestSchemaValidation(subschema, root, SCHEMA_ALLOW_INVALID_TOPLEVEL, true); |
- TestSchemaValidation(subschema, root, SCHEMA_ALLOW_INVALID, true); |
+ TestSchemaValidation(TEST_SOURCE, subschema, root, SCHEMA_STRICT, false); |
+ TestSchemaValidation( |
+ TEST_SOURCE, subschema, root, SCHEMA_ALLOW_UNKNOWN_TOPLEVEL, false); |
+ TestSchemaValidation( |
+ TEST_SOURCE, subschema, root, SCHEMA_ALLOW_UNKNOWN, true); |
+ TestSchemaValidation( |
+ TEST_SOURCE, subschema, root, SCHEMA_ALLOW_INVALID_TOPLEVEL, true); |
+ TestSchemaValidation( |
+ TEST_SOURCE, subschema, root, SCHEMA_ALLOW_INVALID, true); |
TestSchemaValidationWithPath(subschema, root, "Object"); |
root.Remove("Object.three", NULL); |
// Invalid property. |
root.SetInteger("Object.one", 12345); |
- TestSchemaValidation(subschema, root, SCHEMA_STRICT, false); |
- TestSchemaValidation(subschema, root, SCHEMA_ALLOW_UNKNOWN_TOPLEVEL, false); |
- TestSchemaValidation(subschema, root, SCHEMA_ALLOW_UNKNOWN, false); |
- TestSchemaValidation(subschema, root, SCHEMA_ALLOW_INVALID_TOPLEVEL, true); |
- TestSchemaValidation(subschema, root, SCHEMA_ALLOW_INVALID, true); |
+ TestSchemaValidation(TEST_SOURCE, subschema, root, SCHEMA_STRICT, false); |
+ TestSchemaValidation( |
+ TEST_SOURCE, subschema, root, SCHEMA_ALLOW_UNKNOWN_TOPLEVEL, false); |
+ TestSchemaValidation( |
+ TEST_SOURCE, subschema, root, SCHEMA_ALLOW_UNKNOWN, false); |
+ TestSchemaValidation( |
+ TEST_SOURCE, subschema, root, SCHEMA_ALLOW_INVALID_TOPLEVEL, true); |
+ TestSchemaValidation( |
+ TEST_SOURCE, subschema, root, SCHEMA_ALLOW_INVALID, true); |
TestSchemaValidationWithPath(subschema, root, "Object.one"); |
root.Remove("Object.one", NULL); |
} |
@@ -716,11 +777,15 @@ TEST(SchemaTest, Validate) { |
base::DictionaryValue* dict_value = new base::DictionaryValue(); |
dict_value->SetBoolean("three", true); |
root.Append(dict_value); // Pass ownership to root. |
- TestSchemaValidation(subschema, root, SCHEMA_STRICT, false); |
- TestSchemaValidation(subschema, root, SCHEMA_ALLOW_UNKNOWN_TOPLEVEL, false); |
- TestSchemaValidation(subschema, root, SCHEMA_ALLOW_UNKNOWN, true); |
- TestSchemaValidation(subschema, root, SCHEMA_ALLOW_INVALID_TOPLEVEL, true); |
- TestSchemaValidation(subschema, root, SCHEMA_ALLOW_INVALID, true); |
+ TestSchemaValidation(TEST_SOURCE, subschema, root, SCHEMA_STRICT, false); |
+ TestSchemaValidation( |
+ TEST_SOURCE, subschema, root, SCHEMA_ALLOW_UNKNOWN_TOPLEVEL, false); |
+ TestSchemaValidation( |
+ TEST_SOURCE, subschema, root, SCHEMA_ALLOW_UNKNOWN, true); |
+ TestSchemaValidation( |
+ TEST_SOURCE, subschema, root, SCHEMA_ALLOW_INVALID_TOPLEVEL, true); |
+ TestSchemaValidation( |
+ TEST_SOURCE, subschema, root, SCHEMA_ALLOW_INVALID, true); |
TestSchemaValidationWithPath(subschema, root, "items[0]"); |
root.Remove(root.GetSize() - 1, NULL); |
@@ -728,11 +793,15 @@ TEST(SchemaTest, Validate) { |
dict_value = new base::DictionaryValue(); |
dict_value->SetBoolean("two", true); |
root.Append(dict_value); // Pass ownership to root. |
- TestSchemaValidation(subschema, root, SCHEMA_STRICT, false); |
- TestSchemaValidation(subschema, root, SCHEMA_ALLOW_UNKNOWN_TOPLEVEL, false); |
- TestSchemaValidation(subschema, root, SCHEMA_ALLOW_UNKNOWN, false); |
- TestSchemaValidation(subschema, root, SCHEMA_ALLOW_INVALID_TOPLEVEL, true); |
- TestSchemaValidation(subschema, root, SCHEMA_ALLOW_INVALID, true); |
+ TestSchemaValidation(TEST_SOURCE, subschema, root, SCHEMA_STRICT, false); |
+ TestSchemaValidation( |
+ TEST_SOURCE, subschema, root, SCHEMA_ALLOW_UNKNOWN_TOPLEVEL, false); |
+ TestSchemaValidation( |
+ TEST_SOURCE, subschema, root, SCHEMA_ALLOW_UNKNOWN, false); |
+ TestSchemaValidation( |
+ TEST_SOURCE, subschema, root, SCHEMA_ALLOW_INVALID_TOPLEVEL, true); |
+ TestSchemaValidation( |
+ TEST_SOURCE, subschema, root, SCHEMA_ALLOW_INVALID, true); |
TestSchemaValidationWithPath(subschema, root, "items[0].two"); |
root.Remove(root.GetSize() - 1, NULL); |
} |
@@ -748,19 +817,27 @@ TEST(SchemaTest, Validate) { |
// Test that there are not errors here. |
list_value->Append(new base::FundamentalValue(12345)); |
- TestSchemaValidation(subschema, root, SCHEMA_STRICT, true); |
- TestSchemaValidation(subschema, root, SCHEMA_ALLOW_UNKNOWN_TOPLEVEL, true); |
- TestSchemaValidation(subschema, root, SCHEMA_ALLOW_UNKNOWN, true); |
- TestSchemaValidation(subschema, root, SCHEMA_ALLOW_INVALID_TOPLEVEL, true); |
- TestSchemaValidation(subschema, root, SCHEMA_ALLOW_INVALID, true); |
+ TestSchemaValidation(TEST_SOURCE, subschema, root, SCHEMA_STRICT, true); |
+ TestSchemaValidation( |
+ TEST_SOURCE, subschema, root, SCHEMA_ALLOW_UNKNOWN_TOPLEVEL, true); |
+ TestSchemaValidation( |
+ TEST_SOURCE, subschema, root, SCHEMA_ALLOW_UNKNOWN, true); |
+ TestSchemaValidation( |
+ TEST_SOURCE, subschema, root, SCHEMA_ALLOW_INVALID_TOPLEVEL, true); |
+ TestSchemaValidation( |
+ TEST_SOURCE, subschema, root, SCHEMA_ALLOW_INVALID, true); |
// Invalid list item. |
list_value->Append(new base::StringValue("blabla")); |
- TestSchemaValidation(subschema, root, SCHEMA_STRICT, false); |
- TestSchemaValidation(subschema, root, SCHEMA_ALLOW_UNKNOWN_TOPLEVEL, false); |
- TestSchemaValidation(subschema, root, SCHEMA_ALLOW_UNKNOWN, false); |
- TestSchemaValidation(subschema, root, SCHEMA_ALLOW_INVALID_TOPLEVEL, true); |
- TestSchemaValidation(subschema, root, SCHEMA_ALLOW_INVALID, true); |
+ TestSchemaValidation(TEST_SOURCE, subschema, root, SCHEMA_STRICT, false); |
+ TestSchemaValidation( |
+ TEST_SOURCE, subschema, root, SCHEMA_ALLOW_UNKNOWN_TOPLEVEL, false); |
+ TestSchemaValidation( |
+ TEST_SOURCE, subschema, root, SCHEMA_ALLOW_UNKNOWN, false); |
+ TestSchemaValidation( |
+ TEST_SOURCE, subschema, root, SCHEMA_ALLOW_INVALID_TOPLEVEL, true); |
+ TestSchemaValidation( |
+ TEST_SOURCE, subschema, root, SCHEMA_ALLOW_INVALID, true); |
TestSchemaValidationWithPath(subschema, root, "List.items[1]"); |
} |
@@ -777,25 +854,124 @@ TEST(SchemaTest, Validate) { |
// Test that there are not errors here. |
list_value->Append(new base::StringValue("blabla")); |
- TestSchemaValidation(subschema, root, SCHEMA_STRICT, true); |
- TestSchemaValidation(subschema, root, SCHEMA_ALLOW_UNKNOWN_TOPLEVEL, true); |
- TestSchemaValidation(subschema, root, SCHEMA_ALLOW_UNKNOWN, true); |
- TestSchemaValidation(subschema, root, SCHEMA_ALLOW_INVALID_TOPLEVEL, true); |
- TestSchemaValidation(subschema, root, SCHEMA_ALLOW_INVALID, true); |
+ TestSchemaValidation(TEST_SOURCE, subschema, root, SCHEMA_STRICT, true); |
+ TestSchemaValidation( |
+ TEST_SOURCE, subschema, root, SCHEMA_ALLOW_UNKNOWN_TOPLEVEL, true); |
+ TestSchemaValidation( |
+ TEST_SOURCE, subschema, root, SCHEMA_ALLOW_UNKNOWN, true); |
+ TestSchemaValidation( |
+ TEST_SOURCE, subschema, root, SCHEMA_ALLOW_INVALID_TOPLEVEL, true); |
+ TestSchemaValidation( |
+ TEST_SOURCE, subschema, root, SCHEMA_ALLOW_INVALID, true); |
// Invalid list item. |
list_value->Append(new base::FundamentalValue(12345)); |
- TestSchemaValidation(subschema, root, SCHEMA_STRICT, false); |
- TestSchemaValidation(subschema, root, SCHEMA_ALLOW_UNKNOWN_TOPLEVEL, false); |
- TestSchemaValidation(subschema, root, SCHEMA_ALLOW_UNKNOWN, false); |
- TestSchemaValidation(subschema, root, SCHEMA_ALLOW_INVALID_TOPLEVEL, true); |
- TestSchemaValidation(subschema, root, SCHEMA_ALLOW_INVALID, true); |
+ TestSchemaValidation(TEST_SOURCE, subschema, root, SCHEMA_STRICT, false); |
+ TestSchemaValidation( |
+ TEST_SOURCE, subschema, root, SCHEMA_ALLOW_UNKNOWN_TOPLEVEL, false); |
+ TestSchemaValidation( |
+ TEST_SOURCE, subschema, root, SCHEMA_ALLOW_UNKNOWN, false); |
+ TestSchemaValidation( |
+ TEST_SOURCE, subschema, root, SCHEMA_ALLOW_INVALID_TOPLEVEL, true); |
+ TestSchemaValidation( |
+ TEST_SOURCE, subschema, root, SCHEMA_ALLOW_INVALID, true); |
TestSchemaValidationWithPath(subschema, root, "items[0].List.items[1]"); |
} |
+ // Tests on StringWithPattern. |
+ { |
+ Schema subschema = schema.GetProperty("StringWithPattern"); |
+ ASSERT_TRUE(subschema.valid()); |
+ |
+ TestSchemaValidation(TEST_SOURCE, |
+ subschema, |
+ base::StringValue(std::string("foobar")), |
Joao da Silva
2014/04/02 09:51:11
You can pass a const char* directly to StringValue
binjin
2014/04/03 10:13:37
Done.
|
+ SCHEMA_STRICT, |
+ false); |
+ TestSchemaValidation(TEST_SOURCE, |
+ subschema, |
+ base::StringValue(std::string("foo")), |
+ SCHEMA_STRICT, |
+ true); |
+ TestSchemaValidation(TEST_SOURCE, |
+ subschema, |
+ base::StringValue(std::string("fo")), |
+ SCHEMA_STRICT, |
+ false); |
+ TestSchemaValidation(TEST_SOURCE, |
+ subschema, |
+ base::StringValue(std::string("fooo")), |
+ SCHEMA_STRICT, |
+ true); |
+ TestSchemaValidation(TEST_SOURCE, |
+ subschema, |
+ base::StringValue(std::string("^foo+$")), |
+ SCHEMA_STRICT, |
+ false); |
+ } |
+ |
+ // Tests on ObjectWithPatternProperties |
Joao da Silva
2014/04/02 09:51:11
Terminate sentence with .
binjin
2014/04/03 10:13:37
Done.
|
+ { |
+ Schema subschema = schema.GetProperty("ObjectWithPatternProperties"); |
+ ASSERT_TRUE(subschema.valid()); |
+ base::DictionaryValue root; |
+ |
+ ASSERT_EQ(static_cast<size_t>(1), |
Joao da Silva
2014/04/02 09:51:11
1u (also below)
binjin
2014/04/03 10:13:37
Done.
|
+ subschema.GetPatternProperties("fooo").size()); |
+ ASSERT_EQ(static_cast<size_t>(1), |
+ subschema.GetPatternProperties("foo").size()); |
+ ASSERT_EQ(static_cast<size_t>(1), |
+ subschema.GetPatternProperties("barr").size()); |
+ ASSERT_EQ(static_cast<size_t>(1), |
+ subschema.GetPatternProperties("bar").size()); |
+ ASSERT_EQ(static_cast<size_t>(1), |
+ subschema.GetMatchingProperties("fooo").size()); |
+ ASSERT_EQ(static_cast<size_t>(1), |
+ subschema.GetMatchingProperties("foo").size()); |
+ ASSERT_EQ(static_cast<size_t>(1), |
+ subschema.GetMatchingProperties("barr").size()); |
+ ASSERT_EQ(static_cast<size_t>(2), |
+ subschema.GetMatchingProperties("bar").size()); |
+ ASSERT_TRUE(subschema.GetPatternProperties("foobar").empty()); |
+ |
+ root.SetInteger("fooo", 123); |
+ TestSchemaValidation(TEST_SOURCE, subschema, root, SCHEMA_STRICT, true); |
+ root.SetBoolean("fooo", false); |
+ TestSchemaValidation(TEST_SOURCE, subschema, root, SCHEMA_STRICT, false); |
+ root.Remove("fooo", NULL); |
+ |
+ root.SetInteger("foo", 123); |
+ TestSchemaValidation(TEST_SOURCE, subschema, root, SCHEMA_STRICT, true); |
+ root.SetBoolean("foo", false); |
+ TestSchemaValidation(TEST_SOURCE, subschema, root, SCHEMA_STRICT, false); |
+ root.Remove("foo", NULL); |
+ |
+ root.SetString("barr", "one"); |
+ TestSchemaValidation(TEST_SOURCE, subschema, root, SCHEMA_STRICT, true); |
+ root.SetString("barr", "three"); |
+ TestSchemaValidation(TEST_SOURCE, subschema, root, SCHEMA_STRICT, false); |
+ root.SetBoolean("barr", false); |
+ TestSchemaValidation(TEST_SOURCE, subschema, root, SCHEMA_STRICT, false); |
+ root.Remove("barr", NULL); |
+ |
+ root.SetString("bar", "one"); |
+ TestSchemaValidation(TEST_SOURCE, subschema, root, SCHEMA_STRICT, true); |
+ root.SetString("bar", "two"); |
+ TestSchemaValidation(TEST_SOURCE, subschema, root, SCHEMA_STRICT, false); |
+ root.SetString("bar", "three"); |
+ TestSchemaValidation(TEST_SOURCE, subschema, root, SCHEMA_STRICT, false); |
+ root.Remove("bar", NULL); |
+ |
+ root.SetInteger("foobar", 123); |
+ TestSchemaValidation(TEST_SOURCE, subschema, root, SCHEMA_STRICT, false); |
+ TestSchemaValidation( |
+ TEST_SOURCE, subschema, root, SCHEMA_ALLOW_UNKNOWN, true); |
+ root.Remove("foobar", NULL); |
+ } |
+ |
// Test that integer to double promotion is allowed. |
bundle.SetInteger("Number", 31415); |
- TestSchemaValidation(schema, bundle, SCHEMA_STRICT, true); |
+ TestSchemaValidation(TEST_SOURCE, schema, bundle, SCHEMA_STRICT, true); |
} |
TEST(SchemaTest, InvalidReferences) { |