فهرست منبع

test: add test suite for draft7

Sam Jaffe 1 سال پیش
والد
کامیت
4ab7d0f4fe
3فایلهای تغییر یافته به همراه143 افزوده شده و 0 حذف شده
  1. 28 0
      Makefile
  2. 38 0
      tests/json_schema_test_suite.h
  3. 77 0
      tests/selfvalidate_test.cxx

+ 28 - 0
Makefile

@@ -0,0 +1,28 @@
+CXX := clang++
+CXX_FLAGS := -std=c++20 -isystem include/ -DJVALIDATE_USE_EXCEPTIONS
+
+TEST_DIR := tests/
+INCLUDE_DIR := include/
+
+HEADERS := $(shell find $(INCLUDE_DIR) -name *.h)
+
+TEST_HEADERS := $(wildcard $(TEST_DIR)*.h)
+TEST_SOURCES := $(wildcard $(TEST_DIR)*.cxx)
+TEST_OBJECTS := $(patsubst %.cxx, .build/%.o, $(TEST_SOURCES))
+
+all: .build/bin/selfvalidate
+
+debug: CXX_FLAGS := $(CXX_FLAGS) -g
+debug: all
+
+clean:
+	@ rm -rf .build
+
+.build/tests/%.o: tests/%.cxx $(HEADERS) $(TEST_HEADERS)
+	@ mkdir -p .build/tests
+	$(CXX) $(CXX_FLAGS) -c $< -o $@
+
+
+.build/bin/selfvalidate: .build/tests/selfvalidate_test.o
+	@ mkdir -p .build/bin/tests
+	$(CXX) $< -o $@ -L/opt/local/lib -ljsoncpp -lgmock -lgtest

+ 38 - 0
tests/json_schema_test_suite.h

@@ -0,0 +1,38 @@
+#include <filesystem>
+
+#include <gmock/gmock.h>
+
+inline std::filesystem::path const & JSONSchemaTestSuiteDir() {
+  static auto const g_root =
+      std::filesystem::path(__FILE__).parent_path().parent_path().parent_path();
+  static auto const g_path = g_root / "thirdparty" / "JSON-Schema-Test-Suite";
+  return g_path;
+}
+
+inline auto SchemaTests(std::string_view draft) {
+  struct fs_iterator : std::filesystem::recursive_directory_iterator {
+    using super = std::filesystem::recursive_directory_iterator;
+    using value_type = std::filesystem::path;
+
+    using super::super;
+
+    auto const operator*() const { return super::operator*().path(); }
+
+    fs_iterator operator++() {
+      do {
+        super::operator++();
+      } while (*this != fs_iterator() && super::operator*().is_directory());
+      return *this;
+    }
+  };
+
+  auto const dir = JSONSchemaTestSuiteDir() / "tests" / draft;
+  return testing::ValuesIn(fs_iterator(dir), fs_iterator());
+}
+
+static auto SchemaTestName = [](auto const & info) {
+  std::string name = std::get<1>(info.param).stem();
+  std::transform(name.begin(), name.end(), name.begin(),
+                 [](char c) { return std::isalnum(c) ? c : '_'; });
+  return name;
+};

+ 77 - 0
tests/selfvalidate_test.cxx

@@ -0,0 +1,77 @@
+
+#include "gmock/gmock.h"
+#include "gtest/gtest-param-test.h"
+#include "gtest/gtest.h"
+#include <cstdio>
+#include <cstdlib>
+#include <filesystem>
+#include <fstream>
+#include <iostream>
+
+#include <jvalidate/adapter.h>
+#include <jvalidate/adapters/jsoncpp.h>
+#include <jvalidate/enum.h>
+#include <jvalidate/schema.h>
+#include <jvalidate/status.h>
+#include <jvalidate/uri.h>
+#include <jvalidate/validator.h>
+
+#include <json/reader.h>
+#include <json/value.h>
+#include <json/writer.h>
+
+#include "./json_schema_test_suite.h"
+
+using jvalidate::schema::Version;
+
+using testing::Combine;
+using testing::TestWithParam;
+using testing::Values;
+
+bool load_file(std::filesystem::path const & path, Json::Value & out) {
+  std::ifstream in(path);
+  Json::CharReaderBuilder builder;
+  return Json::parseFromStream(builder, in, &out, nullptr);
+}
+
+bool load_external_for_test(jvalidate::URI const & uri, Json::Value & out) {
+  constexpr std::string_view g_fake_url = "localhost:1234/";
+  if (uri.scheme() == "http" && uri.resource().starts_with(g_fake_url)) {
+    std::string_view path = uri.resource().substr(g_fake_url.size());
+    return load_file(JSONSchemaTestSuiteDir() / "remotes" / path, out);
+  } else if (uri.scheme() == "file") {
+    return load_file(uri.resource(), out);
+  } else {
+    return false;
+  }
+}
+
+struct JsonSchema : TestWithParam<std::tuple<Version, std::filesystem::path>> {};
+
+TEST_P(JsonSchema, TestSuite) {
+  auto const & [version, file] = GetParam();
+  Json::Value spec;
+
+  EXPECT_TRUE(load_file(file, spec));
+
+  for (auto const & suite : spec) {
+    std::cout << "\033[0;32m[ SUITE    ] \033[0;0m" << suite["description"].asString() << std::endl;
+    jvalidate::Schema schema(suite["schema"], version, &load_external_for_test);
+    for (auto const & test : suite["tests"]) {
+      try {
+        std::cout << "\033[0;32m[ CASE     ] \033[0;0m    " << test["description"].asString()
+                  << std::endl;
+        auto status = jvalidate::Validator(schema).validate(test["data"]);
+        EXPECT_THAT(status != jvalidate::Status::Reject, test["valid"].asBool()) << test["data"];
+      } catch (std::exception const & ex) { FAIL() << ex.what() << "\n" << test; }
+    }
+  }
+}
+
+INSTANTIATE_TEST_SUITE_P(Draft7, JsonSchema,
+                         Combine(Values(Version::Draft07), SchemaTests("draft7")), SchemaTestName);
+
+int main(int argc, char ** argv) {
+  testing::InitGoogleMock(&argc, argv);
+  return RUN_ALL_TESTS();
+}