folly copyright 2015 -> copyright 2016
[folly.git] / folly / experimental / JSONSchemaTester.cpp
1 /*
2  * Copyright 2016 Facebook, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *   http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 #include <folly/experimental/JSONSchema.h>
17 #include <folly/json.h>
18 #include <fstream>
19 #include <string>
20
21 /**
22  * A binary that supports testing against the official tests from:
23  * https://github.com/json-schema/JSON-Schema-Test-Suite
24  *
25  * Use it like:
26  *   ./jsonschema_tester /path/to/test.json
27  */
28
29 int main(int argc, char** argv) {
30   if (argc < 2) {
31     printf("Usage: %s <testfile> [testfile2]...\n", argv[0]);
32     return -1;
33   }
34   for (int i = 1; i < argc; ++i) {
35     printf("FILE: %s\n", argv[i]);
36     std::ifstream fin(argv[i]);
37     std::stringstream buffer;
38     buffer << fin.rdbuf();
39     const folly::dynamic d = folly::parseJson(buffer.str());
40     for (const auto& item : d) {
41       printf("TEST: %s\n", item["description"].c_str());
42       auto v = folly::jsonschema::makeValidator(item["schema"]);
43       for (const auto& t : item["tests"]) {
44         printf("\t%s... ", t["description"].c_str());
45         auto ew = v->try_validate(t["data"]);
46         bool had_error = !static_cast<bool>(ew);
47         if (had_error == t["valid"].asBool()) {
48           printf("passed\n");
49         } else {
50           printf("FAILED\n");
51         }
52       }
53     }
54   }
55 }