1 #include "subdoc-tests-common.h"
2 #include "subdoc/validate.h"
3 
4 using std::string;
5 using Subdoc::Validator;
6 
7 class ValidateTest : public ::testing::Test {
8 };
9 
TEST_F(ValidateTest, testPlain)10 TEST_F(ValidateTest, testPlain) {
11     // We're not testing jsonsl itself, but how we return/report validation
12     // errors.
13     string txt("[\"Hello\"]");
14     int rv = Validator::validate(txt, NULL);
15     ASSERT_EQ(0, rv) << "Simple validation works OK";
16 
17     txt = "[";
18     rv = Validator::validate(txt, NULL);
19     ASSERT_EQ(Validator::EPARTIAL, rv);
20 }
21 
TEST_F(ValidateTest, testContext)22 TEST_F(ValidateTest, testContext) {
23     // Tests stuff in various contexts
24     string txt("null");
25     int rv = Validator::validate(txt, NULL);
26     ASSERT_NE(0, rv) << "Basic mode does not accept primitives (should fail)";
27 
28     rv = Validator::validate(txt, NULL, -1, Validator::PARENT_ARRAY);
29     ASSERT_EQ(0, rv) << "primitive in context of array (ok)";
30 
31     rv = Validator::validate(txt, NULL, -1, Validator::PARENT_DICT);
32     ASSERT_EQ(0, rv) << "primitive in context of dictionary (ok)";
33 
34     txt = "null, null";
35     rv = Validator::validate(txt, NULL, -1, Validator::PARENT_ARRAY);
36     ASSERT_EQ(0, rv) << "multiple primitives in array (ok)";
37 
38     rv = Validator::validate(txt, NULL, -1, Validator::PARENT_DICT);
39     ASSERT_NE(0, rv) << "multiple primitives in dict (fail)";
40 
41     rv = Validator::validate(txt, NULL, -1,
42         Validator::PARENT_ARRAY|Validator::VALUE_SINGLE);
43     ASSERT_NE(0, rv) << "multiple primitives in array with VALUE_SINGLE (fail)";
44 
45     txt = "[1,2,3]";
46     rv = Validator::validate(txt, NULL, -1, Validator::PARENT_ARRAY);
47     ASSERT_EQ(0, rv) << "container in context of list (ok)";
48 
49     rv = Validator::validate(txt, NULL, -1, Validator::PARENT_DICT);
50     ASSERT_EQ(0, rv) << "container in context of dict (ok)";
51 
52     txt = "[[[]]]";
53     rv = Validator::validate(txt, NULL, -1, Validator::PARENT_DICT);
54     ASSERT_EQ(0, rv);
55 
56     rv = Validator::validate(txt, NULL, -1,
57         Validator::PARENT_DICT|Validator::VALUE_PRIMITIVE);
58     ASSERT_NE(0, rv) << "container with VALUE_PRIMITIVE (fail)";
59 }
60 
TEST_F(ValidateTest, testDepth)61 TEST_F(ValidateTest, testDepth) {
62     // Tests depth constraints
63     string txt("[[[]]]");
64     int rv;
65 
66     rv = Validator::validate(txt, NULL, 3);
67     ASSERT_EQ(0, rv);
68 
69     rv = Validator::validate(txt, NULL, 2);
70     ASSERT_EQ(Validator::ETOODEEP, rv);
71 
72     rv = Validator::validate(txt, NULL, 3, Validator::PARENT_DICT);
73     ASSERT_EQ(0, rv);
74 
75     rv = Validator::validate(txt, NULL, 2, Validator::PARENT_DICT);
76     ASSERT_EQ(Validator::ETOODEEP, rv);
77 }
78