Blaze 0.0.1
The ultra high-performance JSON Schema evaluator
Loading...
Searching...
No Matches
test.h
1#ifndef SOURCEMETA_BLAZE_TEST_H_
2#define SOURCEMETA_BLAZE_TEST_H_
3
4#ifndef SOURCEMETA_BLAZE_TEST_EXPORT
5#include <sourcemeta/blaze/test_export.h>
6#endif
7
8#include <sourcemeta/blaze/test_error.h>
9
10#include <sourcemeta/blaze/compiler.h>
11#include <sourcemeta/blaze/evaluator.h>
12#include <sourcemeta/blaze/output.h>
13
14#include <sourcemeta/core/json.h>
15#include <sourcemeta/core/jsonpointer.h>
16#include <sourcemeta/core/jsonschema.h>
17
18#include <chrono> // std::chrono::steady_clock
19#include <cstddef> // std::size_t
20#include <filesystem> // std::filesystem
21#include <functional> // std::function
22#include <optional> // std::optional
23#include <string> // std::string
24#include <vector> // std::vector
25
34
35namespace sourcemeta::blaze {
36
39using TestTimestamp = std::chrono::steady_clock::time_point;
40
43struct SOURCEMETA_BLAZE_TEST_EXPORT TestCase {
44// See
45// https://learn.microsoft.com/en-us/cpp/error-messages/compiler-warnings/compiler-warning-level-1-c4251?view=msvc-170
46#if defined(_MSC_VER)
47#pragma warning(disable : 4251)
48#endif
50 sourcemeta::core::JSON::String description;
52 bool valid;
54 sourcemeta::core::JSON data;
56 std::optional<sourcemeta::core::JSON> rdf;
58 sourcemeta::core::PointerPositionTracker tracker;
60 sourcemeta::core::PointerPositionTracker::Position position;
61#if defined(_MSC_VER)
62#pragma warning(default : 4251)
63#endif
64
66 static auto
67 parse(const sourcemeta::core::JSON &test_case_json,
68 const sourcemeta::core::PointerPositionTracker &tracker,
69 const std::filesystem::path &base_path,
70 const sourcemeta::core::Pointer &location,
71 const sourcemeta::core::PointerPositionTracker::Position &position)
72 -> TestCase;
73};
74
77struct SOURCEMETA_BLAZE_TEST_EXPORT TestOutcome {
78// See
79// https://learn.microsoft.com/en-us/cpp/error-messages/compiler-warnings/compiler-warning-level-1-c4251?view=msvc-170
80#if defined(_MSC_VER)
81#pragma warning(disable : 4251)
82#endif
84 bool passed;
86 bool valid;
88 std::optional<sourcemeta::core::JSON> rdf;
90 std::optional<JSONLDResolutionError> rdf_error;
91#if defined(_MSC_VER)
92#pragma warning(default : 4251)
93#endif
94};
95
102struct SOURCEMETA_BLAZE_TEST_EXPORT TestTarget {
103// See
104// https://learn.microsoft.com/en-us/cpp/error-messages/compiler-warnings/compiler-warning-level-1-c4251?view=msvc-170
105#if defined(_MSC_VER)
106#pragma warning(disable : 4251)
107#endif
110 const sourcemeta::core::JSON &document;
114 const sourcemeta::core::SchemaFrame &frame;
119 sourcemeta::core::JSON::String entrypoint;
120#if defined(_MSC_VER)
121#pragma warning(default : 4251)
122#endif
123};
124
128// TODO(C++23): Use std::move_only_function when available in libc++
130 std::function<TestTarget(const sourcemeta::core::JSON::String &target)>;
131
140struct SOURCEMETA_BLAZE_TEST_EXPORT TestSuite {
141
143 struct Result {
145 std::size_t total;
147 std::size_t passed;
152 };
153
154// See
155// https://learn.microsoft.com/en-us/cpp/error-messages/compiler-warnings/compiler-warning-level-1-c4251?view=msvc-170
156#if defined(_MSC_VER)
157#pragma warning(disable : 4251)
158#endif
161 std::vector<sourcemeta::core::JSON::String> targets;
163 std::vector<TestCase> tests;
164#if defined(_MSC_VER)
165#pragma warning(default : 4251)
166#endif
169
171 // TODO(C++23): Use std::move_only_function when available in libc++
172 using Callback = std::function<void(
173 const sourcemeta::core::JSON::String &target, std::size_t target_index,
174 std::size_t index, std::size_t total, const TestCase &test_case,
175 const TestOutcome &outcome, TestTimestamp start, TestTimestamp end)>;
176
178 [[nodiscard]] auto fast(std::size_t target_index) const -> const Template &;
179
183 auto exhaustive(std::size_t target_index) -> const Template &;
184
262 auto run(const Callback &callback) -> Result;
263
321 static auto parse(const sourcemeta::core::JSON &document,
322 const sourcemeta::core::PointerPositionTracker &tracker,
323 const std::filesystem::path &base_path,
324 const TestTargetResolver &target_resolver,
325 const sourcemeta::core::SchemaResolver &schema_resolver,
326 const sourcemeta::core::SchemaWalker &walker,
327 const Compiler &compiler,
328 const std::optional<Tweaks> &tweaks = std::nullopt)
329 -> TestSuite;
330
331private:
332// See
333// https://learn.microsoft.com/en-us/cpp/error-messages/compiler-warnings/compiler-warning-level-1-c4251?view=msvc-170
334#if defined(_MSC_VER)
335#pragma warning(disable : 4251)
336#endif
337 // What a target resolved to, held the way a container can hold it, as an
338 // entry with a reference member cannot be assigned
339 struct ResolvedTarget {
340 const sourcemeta::core::JSON *document;
341 const sourcemeta::core::SchemaFrame *frame;
342 sourcemeta::core::JSON::String entrypoint;
343 };
344
345 [[nodiscard]] auto compile_target(std::size_t target_index, Mode mode) const
346 -> Template;
347
348 std::vector<ResolvedTarget> resolved_targets_;
349 std::vector<Template> schemas_fast_;
350 std::vector<std::optional<Template>> schemas_exhaustive_;
351 sourcemeta::core::SchemaResolver schema_resolver_;
352 sourcemeta::core::SchemaWalker walker_;
353 Compiler compiler_;
354 std::optional<Tweaks> tweaks_fast_;
355 std::optional<Tweaks> tweaks_exhaustive_;
356#if defined(_MSC_VER)
357#pragma warning(default : 4251)
358#endif
359};
360
361} // namespace sourcemeta::blaze
362
363#endif
std::function< Instructions(const KeywordContext &, const Instructions &)> Compiler
Definition compiler.h:105
Definition evaluator.h:83
Mode
Definition compiler.h:111
Definition evaluator.h:38
bool valid
The actual validity outcome of the test data against the target.
Definition test.h:86
const sourcemeta::core::JSON & document
Definition test.h:110
std::vector< TestCase > tests
The list of test cases in the suite.
Definition test.h:163
std::optional< sourcemeta::core::JSON > rdf
The actual expansion, when RDF promotion ran and succeeded.
Definition test.h:88
static auto parse(const sourcemeta::core::JSON &document, const sourcemeta::core::PointerPositionTracker &tracker, const std::filesystem::path &base_path, const TestTargetResolver &target_resolver, const sourcemeta::core::SchemaResolver &schema_resolver, const sourcemeta::core::SchemaWalker &walker, const Compiler &compiler, const std::optional< Tweaks > &tweaks=std::nullopt) -> TestSuite
sourcemeta::core::PointerPositionTracker::Position position
The position of this test case in the test suite file.
Definition test.h:60
sourcemeta::core::JSON::String entrypoint
Definition test.h:119
auto run(const Callback &callback) -> Result
auto exhaustive(std::size_t target_index) -> const Template &
std::function< void( const sourcemeta::core::JSON::String &target, std::size_t target_index, std::size_t index, std::size_t total, const TestCase &test_case, const TestOutcome &outcome, TestTimestamp start, TestTimestamp end)> Callback
A callback invoked for each test case during execution.
Definition test.h:172
std::optional< sourcemeta::core::JSON > rdf
The expected promotion of the test data to expanded-form JSON-LD.
Definition test.h:56
std::vector< sourcemeta::core::JSON::String > targets
Definition test.h:161
sourcemeta::core::JSON data
The test data to validate.
Definition test.h:54
std::optional< JSONLDResolutionError > rdf_error
The resolution error, when RDF promotion failed.
Definition test.h:90
auto fast(std::size_t target_index) const -> const Template &
The compiled schema template for fast validation of the given target.
const sourcemeta::core::SchemaFrame & frame
Definition test.h:114
static auto parse(const sourcemeta::core::JSON &test_case_json, const sourcemeta::core::PointerPositionTracker &tracker, const std::filesystem::path &base_path, const sourcemeta::core::Pointer &location, const sourcemeta::core::PointerPositionTracker::Position &position) -> TestCase
Parse a single test case.
sourcemeta::core::JSON::String description
The optional description of the test case.
Definition test.h:50
Evaluator evaluator
The evaluator instance used for validation.
Definition test.h:168
sourcemeta::core::PointerPositionTracker tracker
The position tracker for error reporting on the data.
Definition test.h:58
bool passed
Whether the test case passed overall.
Definition test.h:84
bool valid
Whether the test data is expected to be valid against the schema.
Definition test.h:52
std::chrono::steady_clock::time_point TestTimestamp
Definition test.h:39
std::function< TestTarget(const sourcemeta::core::JSON::String &target)> TestTargetResolver
Definition test.h:129
Definition test.h:43
Definition test.h:77
Definition test.h:140
Definition test.h:102
The result of running a test suite.
Definition test.h:143
std::size_t total
The total number of test cases.
Definition test.h:145
TestTimestamp end
The timestamp when the test suite finished executing.
Definition test.h:151
TestTimestamp start
The timestamp when the test suite started executing.
Definition test.h:149
std::size_t passed
The number of test cases that passed.
Definition test.h:147