X-Git-Url: http://plrg.eecs.uci.edu/git/?a=blobdiff_plain;f=folly%2Fexperimental%2FJSONSchemaTester.cpp;fp=folly%2Fexperimental%2FJSONSchemaTester.cpp;h=e2a0d3f61acc084d45aa191c6d1b7076aceb78e2;hb=e87194b349a6dc0c584911945f6f41dcbaa32b6b;hp=0000000000000000000000000000000000000000;hpb=88fb213fe19af92f50ee851a477c73a103dbcfe1;p=folly.git diff --git a/folly/experimental/JSONSchemaTester.cpp b/folly/experimental/JSONSchemaTester.cpp new file mode 100644 index 00000000..e2a0d3f6 --- /dev/null +++ b/folly/experimental/JSONSchemaTester.cpp @@ -0,0 +1,55 @@ +/* + * Copyright 2015 Facebook, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#include +#include +#include +#include + +/** + * A binary that supports testing against the official tests from: + * https://github.com/json-schema/JSON-Schema-Test-Suite + * + * Use it like: + * ./jsonschema_tester /path/to/test.json + */ + +int main(int argc, char** argv) { + if (argc < 2) { + printf("Usage: %s [testfile2]...\n", argv[0]); + return -1; + } + for (int i = 1; i < argc; ++i) { + printf("FILE: %s\n", argv[i]); + std::ifstream fin(argv[i]); + std::stringstream buffer; + buffer << fin.rdbuf(); + const folly::dynamic d = folly::parseJson(buffer.str()); + for (const auto& item : d) { + printf("TEST: %s\n", item["description"].c_str()); + auto v = folly::jsonschema::makeValidator(item["schema"]); + for (const auto& t : item["tests"]) { + printf("\t%s... ", t["description"].c_str()); + auto ew = v->try_validate(t["data"]); + bool had_error = !static_cast(ew); + if (had_error == t["valid"].asBool()) { + printf("passed\n"); + } else { + printf("FAILED\n"); + } + } + } + } +}