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 1df338a25b00b123b69251a7a30dfb5a2527a3ae..32ca9cd90df4bf8e887c527d7e791aade73358ef 100644 |
--- a/components/policy/core/common/schema_unittest.cc |
+++ b/components/policy/core/common/schema_unittest.cc |
@@ -13,6 +13,47 @@ namespace { |
#define OBJECT_TYPE "\"type\":\"object\"" |
+const char kTestSchema[] = |
+ "{" |
+ OBJECT_TYPE "," |
dconnelly
2013/10/31 10:04:16
I know this is just moved, but why is this necessa
Joao da Silva
2013/10/31 13:34:56
Done.
|
+ "\"properties\": {" |
+ " \"Boolean\": { \"type\": \"boolean\" }," |
+ " \"Integer\": { \"type\": \"integer\" }," |
+ " \"Null\": { \"type\": \"null\" }," |
+ " \"Number\": { \"type\": \"number\" }," |
+ " \"String\": { \"type\": \"string\" }," |
+ " \"Array\": {" |
+ " \"type\": \"array\"," |
+ " \"items\": { \"type\": \"string\" }" |
+ " }," |
+ " \"ArrayOfObjects\": {" |
+ " \"type\": \"array\"," |
+ " \"items\": {" |
+ " \"type\": \"object\"," |
+ " \"properties\": {" |
+ " \"one\": { \"type\": \"string\" }," |
+ " \"two\": { \"type\": \"integer\" }" |
+ " }" |
+ " }" |
+ " }," |
+ " \"ArrayOfArray\": {" |
+ " \"type\": \"array\"," |
+ " \"items\": {" |
+ " \"type\": \"array\"," |
+ " \"items\": { \"type\": \"string\" }" |
+ " }" |
+ " }," |
+ " \"Object\": {" |
+ " \"type\": \"object\"," |
+ " \"properties\": {" |
+ " \"one\": { \"type\": \"boolean\" }," |
+ " \"two\": { \"type\": \"integer\" }" |
+ " }," |
+ " \"additionalProperties\": { \"type\": \"string\" }" |
+ " }" |
+ "}" |
+ "}"; |
+ |
bool ParseFails(const std::string& content) { |
std::string error; |
Schema schema = Schema::Parse(content, &error); |
@@ -110,46 +151,7 @@ TEST(SchemaTest, Ownership) { |
TEST(SchemaTest, ValidSchema) { |
std::string error; |
- Schema schema = Schema::Parse( |
- "{" |
- OBJECT_TYPE "," |
- "\"properties\": {" |
- " \"Boolean\": { \"type\": \"boolean\" }," |
- " \"Integer\": { \"type\": \"integer\" }," |
- " \"Null\": { \"type\": \"null\" }," |
- " \"Number\": { \"type\": \"number\" }," |
- " \"String\": { \"type\": \"string\" }," |
- " \"Array\": {" |
- " \"type\": \"array\"," |
- " \"items\": { \"type\": \"string\" }" |
- " }," |
- " \"ArrayOfObjects\": {" |
- " \"type\": \"array\"," |
- " \"items\": {" |
- " \"type\": \"object\"," |
- " \"properties\": {" |
- " \"one\": { \"type\": \"string\" }," |
- " \"two\": { \"type\": \"integer\" }" |
- " }" |
- " }" |
- " }," |
- " \"ArrayOfArray\": {" |
- " \"type\": \"array\"," |
- " \"items\": {" |
- " \"type\": \"array\"," |
- " \"items\": { \"type\": \"string\" }" |
- " }" |
- " }," |
- " \"Object\": {" |
- " \"type\": \"object\"," |
- " \"properties\": {" |
- " \"one\": { \"type\": \"boolean\" }," |
- " \"two\": { \"type\": \"integer\" }" |
- " }," |
- " \"additionalProperties\": { \"type\": \"string\" }" |
- " }" |
- "}" |
- "}", &error); |
+ Schema schema = Schema::Parse(kTestSchema, &error); |
ASSERT_TRUE(schema.valid()) << error; |
ASSERT_EQ(base::Value::TYPE_DICTIONARY, schema.type()); |
@@ -384,4 +386,89 @@ TEST(SchemaTest, Wrap) { |
ASSERT_EQ(base::Value::TYPE_STRING, subsubsub.type()); |
} |
+TEST(SchemaTest, Matches) { |
+ std::string error; |
+ Schema schema = Schema::Parse(kTestSchema, &error); |
+ ASSERT_TRUE(schema.valid()) << error; |
+ |
+ base::DictionaryValue bundle; |
+ EXPECT_TRUE(schema.Matches(bundle)); |
+ |
+ // Wrong type, expected integer. |
+ bundle.SetBoolean("Integer", true); |
+ EXPECT_FALSE(schema.Matches(bundle)); |
+ |
+ // Wrong type, expected list of strings. |
+ { |
+ bundle.Clear(); |
+ base::ListValue list; |
+ list.AppendInteger(1); |
+ bundle.Set("Array", list.DeepCopy()); |
+ EXPECT_FALSE(schema.Matches(bundle)); |
+ } |
+ |
+ // Wrong type in a sub-object. |
+ { |
+ bundle.Clear(); |
+ base::DictionaryValue dict; |
+ dict.SetString("one", "one"); |
+ bundle.Set("Object", dict.DeepCopy()); |
+ EXPECT_FALSE(schema.Matches(bundle)); |
+ } |
+ |
+ // Unknown name. |
+ bundle.Clear(); |
+ bundle.SetBoolean("Unknown", true); |
+ EXPECT_FALSE(schema.Matches(bundle)); |
+ |
+ // All of these will be valid. |
+ bundle.Clear(); |
+ bundle.SetBoolean("Boolean", true); |
+ bundle.SetInteger("Integer", 123); |
+ bundle.Set("Null", base::Value::CreateNullValue()); |
+ bundle.Set("Number", base::Value::CreateDoubleValue(3.14)); |
+ bundle.SetString("String", "omg"); |
+ |
+ { |
+ base::ListValue list; |
+ list.AppendString("a string"); |
+ list.AppendString("another string"); |
+ bundle.Set("Array", list.DeepCopy()); |
+ } |
+ |
+ { |
+ base::DictionaryValue dict; |
+ dict.SetString("one", "string"); |
+ dict.SetInteger("two", 2); |
+ base::ListValue list; |
+ list.Append(dict.DeepCopy()); |
+ list.Append(dict.DeepCopy()); |
+ bundle.Set("ArrayOfObjects", list.DeepCopy()); |
+ } |
+ |
+ { |
+ base::ListValue list; |
+ list.AppendString("a string"); |
+ list.AppendString("another string"); |
+ base::ListValue listlist; |
+ listlist.Append(list.DeepCopy()); |
+ listlist.Append(list.DeepCopy()); |
+ bundle.Set("ArrayOfArray", listlist.DeepCopy()); |
+ } |
+ |
+ { |
+ base::DictionaryValue dict; |
+ dict.SetBoolean("one", true); |
+ dict.SetInteger("two", 2); |
+ dict.SetString("additionally", "a string"); |
+ dict.SetString("and also", "another string"); |
+ bundle.Set("Object", dict.DeepCopy()); |
+ } |
+ |
+ EXPECT_TRUE(schema.Matches(bundle)); |
+ |
+ bundle.SetString("boom", "bang"); |
+ EXPECT_FALSE(schema.Matches(bundle)); |
+} |
+ |
} // namespace policy |