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

Side by Side Diff: components/policy/core/common/schema_unittest.cc

Issue 50143010: Added a SchemaMap. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@chrome-policy-schema-5-ref-counted-schema
Patch Set: Created 7 years, 1 month 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 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 {
11 11
12 namespace { 12 namespace {
13 13
14 #define OBJECT_TYPE "\"type\":\"object\"" 14 #define OBJECT_TYPE "\"type\":\"object\""
15 15
16 const char kTestSchema[] =
17 "{"
18 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.
19 "\"properties\": {"
20 " \"Boolean\": { \"type\": \"boolean\" },"
21 " \"Integer\": { \"type\": \"integer\" },"
22 " \"Null\": { \"type\": \"null\" },"
23 " \"Number\": { \"type\": \"number\" },"
24 " \"String\": { \"type\": \"string\" },"
25 " \"Array\": {"
26 " \"type\": \"array\","
27 " \"items\": { \"type\": \"string\" }"
28 " },"
29 " \"ArrayOfObjects\": {"
30 " \"type\": \"array\","
31 " \"items\": {"
32 " \"type\": \"object\","
33 " \"properties\": {"
34 " \"one\": { \"type\": \"string\" },"
35 " \"two\": { \"type\": \"integer\" }"
36 " }"
37 " }"
38 " },"
39 " \"ArrayOfArray\": {"
40 " \"type\": \"array\","
41 " \"items\": {"
42 " \"type\": \"array\","
43 " \"items\": { \"type\": \"string\" }"
44 " }"
45 " },"
46 " \"Object\": {"
47 " \"type\": \"object\","
48 " \"properties\": {"
49 " \"one\": { \"type\": \"boolean\" },"
50 " \"two\": { \"type\": \"integer\" }"
51 " },"
52 " \"additionalProperties\": { \"type\": \"string\" }"
53 " }"
54 "}"
55 "}";
56
16 bool ParseFails(const std::string& content) { 57 bool ParseFails(const std::string& content) {
17 std::string error; 58 std::string error;
18 Schema schema = Schema::Parse(content, &error); 59 Schema schema = Schema::Parse(content, &error);
19 if (schema.valid()) 60 if (schema.valid())
20 return false; 61 return false;
21 EXPECT_FALSE(error.empty()); 62 EXPECT_FALSE(error.empty());
22 return true; 63 return true;
23 } 64 }
24 65
25 } // namespace 66 } // namespace
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after
103 } 144 }
104 145
105 ASSERT_TRUE(schema.valid()); 146 ASSERT_TRUE(schema.valid());
106 EXPECT_EQ(base::Value::TYPE_STRING, schema.type()); 147 EXPECT_EQ(base::Value::TYPE_STRING, schema.type());
107 148
108 // This test shouldn't leak nor use invalid memory. 149 // This test shouldn't leak nor use invalid memory.
109 } 150 }
110 151
111 TEST(SchemaTest, ValidSchema) { 152 TEST(SchemaTest, ValidSchema) {
112 std::string error; 153 std::string error;
113 Schema schema = Schema::Parse( 154 Schema schema = Schema::Parse(kTestSchema, &error);
114 "{"
115 OBJECT_TYPE ","
116 "\"properties\": {"
117 " \"Boolean\": { \"type\": \"boolean\" },"
118 " \"Integer\": { \"type\": \"integer\" },"
119 " \"Null\": { \"type\": \"null\" },"
120 " \"Number\": { \"type\": \"number\" },"
121 " \"String\": { \"type\": \"string\" },"
122 " \"Array\": {"
123 " \"type\": \"array\","
124 " \"items\": { \"type\": \"string\" }"
125 " },"
126 " \"ArrayOfObjects\": {"
127 " \"type\": \"array\","
128 " \"items\": {"
129 " \"type\": \"object\","
130 " \"properties\": {"
131 " \"one\": { \"type\": \"string\" },"
132 " \"two\": { \"type\": \"integer\" }"
133 " }"
134 " }"
135 " },"
136 " \"ArrayOfArray\": {"
137 " \"type\": \"array\","
138 " \"items\": {"
139 " \"type\": \"array\","
140 " \"items\": { \"type\": \"string\" }"
141 " }"
142 " },"
143 " \"Object\": {"
144 " \"type\": \"object\","
145 " \"properties\": {"
146 " \"one\": { \"type\": \"boolean\" },"
147 " \"two\": { \"type\": \"integer\" }"
148 " },"
149 " \"additionalProperties\": { \"type\": \"string\" }"
150 " }"
151 "}"
152 "}", &error);
153 ASSERT_TRUE(schema.valid()) << error; 155 ASSERT_TRUE(schema.valid()) << error;
154 156
155 ASSERT_EQ(base::Value::TYPE_DICTIONARY, schema.type()); 157 ASSERT_EQ(base::Value::TYPE_DICTIONARY, schema.type());
156 EXPECT_FALSE(schema.GetProperty("invalid").valid()); 158 EXPECT_FALSE(schema.GetProperty("invalid").valid());
157 159
158 Schema sub = schema.GetProperty("Boolean"); 160 Schema sub = schema.GetProperty("Boolean");
159 ASSERT_TRUE(sub.valid()); 161 ASSERT_TRUE(sub.valid());
160 EXPECT_EQ(base::Value::TYPE_BOOLEAN, sub.type()); 162 EXPECT_EQ(base::Value::TYPE_BOOLEAN, sub.type());
161 163
162 sub = schema.GetProperty("Integer"); 164 sub = schema.GetProperty("Integer");
(...skipping 214 matching lines...) Expand 10 before | Expand all | Expand 10 after
377 ASSERT_TRUE(sub.valid()); 379 ASSERT_TRUE(sub.valid());
378 ASSERT_EQ(base::Value::TYPE_LIST, sub.type()); 380 ASSERT_EQ(base::Value::TYPE_LIST, sub.type());
379 Schema subsub = sub.GetItems(); 381 Schema subsub = sub.GetItems();
380 ASSERT_TRUE(subsub.valid()); 382 ASSERT_TRUE(subsub.valid());
381 ASSERT_EQ(base::Value::TYPE_LIST, subsub.type()); 383 ASSERT_EQ(base::Value::TYPE_LIST, subsub.type());
382 Schema subsubsub = subsub.GetItems(); 384 Schema subsubsub = subsub.GetItems();
383 ASSERT_TRUE(subsubsub.valid()); 385 ASSERT_TRUE(subsubsub.valid());
384 ASSERT_EQ(base::Value::TYPE_STRING, subsubsub.type()); 386 ASSERT_EQ(base::Value::TYPE_STRING, subsubsub.type());
385 } 387 }
386 388
389 TEST(SchemaTest, Matches) {
390 std::string error;
391 Schema schema = Schema::Parse(kTestSchema, &error);
392 ASSERT_TRUE(schema.valid()) << error;
393
394 base::DictionaryValue bundle;
395 EXPECT_TRUE(schema.Matches(bundle));
396
397 // Wrong type, expected integer.
398 bundle.SetBoolean("Integer", true);
399 EXPECT_FALSE(schema.Matches(bundle));
400
401 // Wrong type, expected list of strings.
402 {
403 bundle.Clear();
404 base::ListValue list;
405 list.AppendInteger(1);
406 bundle.Set("Array", list.DeepCopy());
407 EXPECT_FALSE(schema.Matches(bundle));
408 }
409
410 // Wrong type in a sub-object.
411 {
412 bundle.Clear();
413 base::DictionaryValue dict;
414 dict.SetString("one", "one");
415 bundle.Set("Object", dict.DeepCopy());
416 EXPECT_FALSE(schema.Matches(bundle));
417 }
418
419 // Unknown name.
420 bundle.Clear();
421 bundle.SetBoolean("Unknown", true);
422 EXPECT_FALSE(schema.Matches(bundle));
423
424 // All of these will be valid.
425 bundle.Clear();
426 bundle.SetBoolean("Boolean", true);
427 bundle.SetInteger("Integer", 123);
428 bundle.Set("Null", base::Value::CreateNullValue());
429 bundle.Set("Number", base::Value::CreateDoubleValue(3.14));
430 bundle.SetString("String", "omg");
431
432 {
433 base::ListValue list;
434 list.AppendString("a string");
435 list.AppendString("another string");
436 bundle.Set("Array", list.DeepCopy());
437 }
438
439 {
440 base::DictionaryValue dict;
441 dict.SetString("one", "string");
442 dict.SetInteger("two", 2);
443 base::ListValue list;
444 list.Append(dict.DeepCopy());
445 list.Append(dict.DeepCopy());
446 bundle.Set("ArrayOfObjects", list.DeepCopy());
447 }
448
449 {
450 base::ListValue list;
451 list.AppendString("a string");
452 list.AppendString("another string");
453 base::ListValue listlist;
454 listlist.Append(list.DeepCopy());
455 listlist.Append(list.DeepCopy());
456 bundle.Set("ArrayOfArray", listlist.DeepCopy());
457 }
458
459 {
460 base::DictionaryValue dict;
461 dict.SetBoolean("one", true);
462 dict.SetInteger("two", 2);
463 dict.SetString("additionally", "a string");
464 dict.SetString("and also", "another string");
465 bundle.Set("Object", dict.DeepCopy());
466 }
467
468 EXPECT_TRUE(schema.Matches(bundle));
469
470 bundle.SetString("boom", "bang");
471 EXPECT_FALSE(schema.Matches(bundle));
472 }
473
387 } // namespace policy 474 } // namespace policy
OLDNEW
« components/policy/core/common/schema.h ('K') | « components/policy/core/common/schema.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698