diff --git a/.editorconfig b/.editorconfig index b73fdf748..d83eea8df 100644 --- a/.editorconfig +++ b/.editorconfig @@ -12,8 +12,5 @@ insert_final_newline = true [Makefile] indent_style = tab -[GNUmakefile] -indent_style = tab - [*.mk] indent_style = tab diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 7ad591cbc..e6a653ad5 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -28,7 +28,7 @@ jobs: CC: ${{ matrix.platform.cc }} CXX: ${{ matrix.platform.cxx }} steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@v3 - name: Install dependencies (macOS) if: runner.os == 'macos' run: brew bundle @@ -44,7 +44,7 @@ jobs: if: runner.os == 'linux' && matrix.platform.cc == 'clang' run: | sudo rm -f /etc/apt/sources.list.d/ubuntu-toolchain-r-ubuntu-test-jammy.list - sudo apt-get install --yes --allow-downgrades libc6=2.35-0ubuntu3.4 libc6-dev=2.35-0ubuntu3.4 libstdc++6=12.3.0-1ubuntu1~22.04 libgcc-s1=12.3.0-1ubuntu1~22.04 + sudo apt-get install --yes --allow-downgrades libc6=2.35-0ubuntu3.5 libc6-dev=2.35-0ubuntu3.5 libstdc++6=12.3.0-1ubuntu1~22.04 libgcc-s1=12.3.0-1ubuntu1~22.04 - name: Install dependencies (Windows) if: runner.os == 'windows' @@ -59,5 +59,5 @@ jobs: -DCMAKE_COMPILE_WARNING_AS_ERROR:BOOL=ON - run: cmake --build ./build --config Release --target shellcheck - run: cmake --build ./build --config Release --target clang_format_test - - run: cmake --build ./build --config Release --parallel 2 + - run: cmake --build ./build --config Release --parallel 4 - run: cd ./build && ctest --build-config Release --output-on-failure --parallel diff --git a/CMakeLists.txt b/CMakeLists.txt index 99c1b1979..9f43077aa 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,69 +1,117 @@ cmake_minimum_required(VERSION 3.24) -project("JSON BinPack" VERSION 0.0.1 LANGUAGES CXX +project(jsonbinpack VERSION 0.0.1 LANGUAGES C CXX DESCRIPTION "\ A space-efficient open-source binary JSON serialization \ format based on JSON Schema with \ -both schema-driven and schema-less support.") -include(vendor/noa/cmake/noa.cmake) +both schema-driven and schema-less support." + HOMEPAGE_URL "https://www.jsonbinpack.org") +list(APPEND CMAKE_MODULE_PATH "${PROJECT_SOURCE_DIR}/cmake") -include(cmake/warnings.cmake) +# Dependencies +# TODO: Disable non-required JSON Toolkit components +find_package(JSONToolkit REQUIRED) # Options option(JSONBINPACK_CLI "Build the JSON BinPack CLI" ON) -option(JSONBINPACK_TESTS "Build the JSON BinPack tests" ON) +option(JSONBINPACK_TESTS "Build the JSON BinPack tests" OFF) +option(JSONBINPACK_INSTALL "Install the JSON BinPack library" ON) -# JSON Toolkit -set(JSONTOOLKIT_BACKEND "rapidjson" CACHE STRING "Set JSON backend") -set(JSONTOOLKIT_BACKEND_PATH "${PROJECT_SOURCE_DIR}/vendor/rapidjson" CACHE STRING "Use vendored backend") -add_subdirectory(vendor/jsontoolkit) +# Global includes +include(vendor/noa/cmake/noa.cmake) +include(cmake/CompilerOptions.cmake) + +if(PROJECT_IS_TOP_LEVEL) + noa_target_clang_format(SOURCES + src/*.h src/*.cc + test/*.h test/*.cc) + noa_target_clang_tidy(SOURCES + src/*.h src/*.cc) + noa_target_shellcheck(SOURCES + test/*.sh scripts/*.sh) +endif() # Sources -add_subdirectory(src/alterschema) -add_subdirectory(src/canonicalizer) -add_subdirectory(src/encoding) -add_subdirectory(src/parser) add_subdirectory(src/numeric) +add_subdirectory(src/encoding) add_subdirectory(src/encoder) add_subdirectory(src/decoder) +add_subdirectory(src/parser) add_subdirectory(src/schemas) +add_subdirectory(src/canonicalizer) add_subdirectory(src/mapper) -if(JSONBINPACK_CLI) - add_subdirectory(src/cli) -endif() + +# if(JSONBINPACK_CLI) + # add_subdirectory(src/cli) +# endif() # Testing if(JSONBINPACK_TESTS) + find_package(GoogleTest REQUIRED) enable_testing() - set(BUILD_GMOCK OFF CACHE BOOL "disable googlemock") - set(INSTALL_GTEST OFF CACHE BOOL "disable installation") - include(GoogleTest) - add_subdirectory(vendor/googletest) - add_subdirectory(test/alterschema) - add_subdirectory(test/canonicalizer) - add_subdirectory(test/encoding) - add_subdirectory(test/parser) + add_subdirectory(test/numeric) + add_subdirectory(test/encoding) add_subdirectory(test/encoder) add_subdirectory(test/decoder) + add_subdirectory(test/parser) add_subdirectory(test/schemas) add_subdirectory(test/mapper) - if(JSONBINPACK_CLI) - add_subdirectory(test/cli) - endif() - add_subdirectory(test/e2e) endif() -# Only for top-level builds -if(PROJECT_IS_TOP_LEVEL) - file(GLOB_RECURSE JSONBINPACK_CXX_SOURCE_FILES src/*.cc src/*.h test/*.cc test/*.h) - noa_target_clang_format(SOURCES ${JSONBINPACK_CXX_SOURCE_FILES}) - noa_target_clang_tidy(SOURCES ${JSONBINPACK_CXX_SOURCE_FILES}) - noa_target_shellcheck(SOURCES test/*.sh scripts/*.sh) +# # Testing +# if(JSONBINPACK_TESTS) + # add_subdirectory(test/canonicalizer) + # if(JSONBINPACK_CLI) + # add_subdirectory(test/cli) + # endif() + # add_subdirectory(test/e2e) +# endif() +# Website +if(PROJECT_IS_TOP_LEVEL) string(TOLOWER ${CMAKE_BUILD_TYPE} JSONBINPACK_BUILD_TYPE) set(JSONBINPACK_WEBSITE_OUT ${PROJECT_SOURCE_DIR}/build/${JSONBINPACK_BUILD_TYPE}/www) set(JSONBINPACK_WEBSITE_SRC ${PROJECT_SOURCE_DIR}/www) - include(${PROJECT_SOURCE_DIR}/cmake/bundler.cmake) - include(${PROJECT_SOURCE_DIR}/cmake/jekyll.cmake) - include(${PROJECT_SOURCE_DIR}/cmake/doxygen.cmake) + + # TODO: Move to Makefile? + find_program(BUNDLE_BIN NAMES bundle) + set(JSONBINPACK_BUNDLER_PATH "${PROJECT_SOURCE_DIR}/build/bundler") + if(BUNDLE_BIN) + add_custom_target(bundler + WORKING_DIRECTORY "${PROJECT_SOURCE_DIR}" + VERBATIM + COMMAND "${BUNDLE_BIN}" install --path "${JSONBINPACK_BUNDLER_PATH}") + else() + message(WARNING "Could not find `bundler` in the system") + endif() + + find_program(BUNDLE_BIN NAMES bundle) + if(BUNDLE_BIN) + add_custom_target(jekyll + WORKING_DIRECTORY "${PROJECT_SOURCE_DIR}" + VERBATIM + COMMAND "${BUNDLE_BIN}" exec jekyll build + --source "${JSONBINPACK_WEBSITE_SRC}" --destination "${JSONBINPACK_WEBSITE_OUT}") + set_target_properties(jekyll + PROPERTIES FOLDER "Website") + else() + message(WARNING "Could not find `jekyll` in the system") + endif() + + # TODO: Use Noa for Doxygen + find_package(Doxygen) + if(DOXYGEN_FOUND) + set(DOXYGEN_IN "${PROJECT_SOURCE_DIR}/doxygen/Doxyfile.in") + set(DOXYGEN_OUT "${CMAKE_CURRENT_BINARY_DIR}/Doxyfile") + configure_file("${DOXYGEN_IN}" "${DOXYGEN_OUT}" @ONLY) + add_custom_target(doxygen + WORKING_DIRECTORY "${PROJECT_SOURCE_DIR}" + VERBATIM + COMMAND "${CMAKE_COMMAND}" -E make_directory "${JSONBINPACK_WEBSITE_OUT}" + COMMAND "${DOXYGEN_EXECUTABLE}" "${DOXYGEN_OUT}") + set_target_properties(doxygen + PROPERTIES FOLDER "Website") + else() + message(WARNING "Could not find `doxygen` in the system") + endif() endif() diff --git a/DEPENDENCIES b/DEPENDENCIES index d45ae9111..0a81868b6 100644 --- a/DEPENDENCIES +++ b/DEPENDENCIES @@ -1,6 +1,5 @@ vendorpull https://github.com/sourcemeta/vendorpull 6c398482e30bd85d7e8138c606e01f28270c29ae -rapidjson https://github.com/Tencent/rapidjson 06d58b9e848c650114556a23294d0b6440078c61 -jsontoolkit https://github.com/sourcemeta/jsontoolkit e0efe63940e8c27b3a78ce27ac0c1be70922b741 -googletest https://github.com/google/googletest e2239ee6043f73722e7aa812a459f54a28552929 +jsontoolkit https://github.com/sourcemeta/jsontoolkit 52a23630b2f467d7197c277805ddd7159068f9cf +googletest https://github.com/google/googletest 987e225614755fec7253aa95bf959c09e0d380d7 bootstrap https://github.com/twbs/bootstrap 1a6fdfae6be09b09eaced8f0e442ca6f7680a61e -noa https://github.com/sourcemeta/noa f7dc92ef5bf95ade5f585494e3a6de3692efdf4f +noa https://github.com/sourcemeta/noa 0862a96772362a6997ce2f5fda877b182a7b6f71 diff --git a/GNUmakefile b/GNUmakefile deleted file mode 100644 index 190c0d259..000000000 --- a/GNUmakefile +++ /dev/null @@ -1,62 +0,0 @@ -.DEFAULT_GOAL = all - -CMAKE ?= cmake -CTEST ?= ctest -BUNDLE ?= bundle - -PRESET ?= debug - -.PHONY: configure -configure: - $(CMAKE) -S . -B ./build \ - -DCMAKE_BUILD_TYPE:STRING=$(PRESET) \ - -DJSONBINPACK_CLI:BOOL=ON \ - -DJSONBINPACK_TESTS:BOOL=ON \ - -DCMAKE_COMPILE_WARNING_AS_ERROR:BOOL=ON - -.PHONY: compile -compile: - $(CMAKE) --build ./build --config $(PRESET) --target clang_format - $(CMAKE) --build ./build --config $(PRESET) --parallel - -.PHONY: all -all: configure compile - $(CTEST) --test-dir ./build --build-config $(PRESET) --parallel - -.PHONY: test -CASE ?= -ifdef CASE -test: configure compile - $(CTEST) --test-dir ./build --build-config $(PRESET) --verbose --tests-regex $(CASE) -else -test: configure compile - $(CTEST) --test-dir ./build --build-config $(PRESET) --verbose -endif - -.PHONY: debug -ifdef CASE -debug: scripts/lldb.sh configure compile - ./$< $(CASE) -else -debug: - @echo "Missing CASE option" 1>&2 - exit 1 -endif - -.PHONY: clean -lint: - $(CMAKE) --build ./build --config $(PRESET) --target clang_tidy - -.PHONY: clean -clean: - $(CMAKE) -E rm -R -f build .bundle .sass-cache - -.PHONY: jekyll -jekyll: configure - $(CMAKE) --build ./build --config $(PRESET) --target bundler - $(BUNDLE) exec jekyll serve --watch --incremental --trace \ - --source www --destination build/$(PRESET)/www - -.PHONY: doxygen -doxygen: configure - $(CMAKE) --build ./build --config $(PRESET) --target doxygen diff --git a/Makefile b/Makefile index 9a3bf47f3..12fb39f5b 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,11 @@ +# Programs CMAKE = cmake CTEST = ctest -PRESET = debug +# Options +PRESET = Debug + +all: configure compile test configure: .always $(CMAKE) -S . -B ./build \ @@ -13,12 +17,14 @@ configure: .always compile: .always $(CMAKE) --build ./build --config $(PRESET) --target clang_format $(CMAKE) --build ./build --config $(PRESET) --parallel + $(CMAKE) --install ./build --prefix ./build/dist --config $(PRESET) --verbose \ + --component sourcemeta_jsonbinpack + $(CMAKE) --install ./build --prefix ./build/dist --config $(PRESET) --verbose \ + --component sourcemeta_jsonbinpack_dev -all: configure compile - $(CTEST) --test-dir ./build --build-config $(PRESET) --parallel - -test: configure compile - $(CTEST) --test-dir ./build --build-config $(PRESET) --verbose --parallel +test: .always + $(CTEST) --test-dir ./build --build-config $(PRESET) \ + --output-on-failure --progress --parallel lint: .always $(CMAKE) --build ./build --config $(PRESET) --target clang_tidy @@ -26,7 +32,7 @@ lint: .always clean: .always $(CMAKE) -E rm -R -f build -doxygen: configure +doxygen: $(CMAKE) --build ./build --config $(PRESET) --target doxygen # For NMake, which doesn't support .PHONY diff --git a/cmake/CompilerOptions.cmake b/cmake/CompilerOptions.cmake new file mode 100644 index 000000000..b6a632c7e --- /dev/null +++ b/cmake/CompilerOptions.cmake @@ -0,0 +1,72 @@ +function(sourcemeta_jsonbinpack_add_compile_options visibility target) + if(NOA_COMPILER_MSVC) + # See https://learn.microsoft.com/en-us/cpp/build/reference/compiler-options-listed-by-category + target_compile_options("${target}" ${visibility} + /options:strict + /W4 + /WL + /sdl) + else() + target_compile_options("${target}" ${visibility} + -Wall + -Wextra + -Wpedantic + -Wshadow + -Wdouble-promotion + -Wconversion + -Wunused-parameter + -Wtrigraphs + -Wunreachable-code + -Wmissing-braces + -Wparentheses + -Wswitch + -Wunused-function + -Wunused-label + -Wunused-parameter + -Wunused-variable + -Wunused-value + -Wempty-body + -Wuninitialized + -Wshadow + -Wconversion + -Wenum-conversion + -Wfloat-conversion + -Wimplicit-fallthrough + -Wsign-compare + # TODO: Enable this flag for safety + -Wno-sign-conversion + -Wunknown-pragmas + -Wnon-virtual-dtor + -Woverloaded-virtual + -Winvalid-offsetof + + # Assume that signed arithmetic overflow of addition, subtraction and + # multiplication wraps around using twos-complement representation + # See https://users.cs.utah.edu/~regehr/papers/overflow12.pdf + # See https://www.postgresql.org/message-id/1689.1134422394@sss.pgh.pa.us + -fwrapv) + endif() + + if(NOA_COMPILER_LLVM) + target_compile_options("${target}" ${visibility} + -Wbool-conversion + -Wint-conversion + -Wpointer-sign + -Wconditional-uninitialized + -Wconstant-conversion + -Wnon-literal-null-conversion + -Wshorten-64-to-32 + -Wdeprecated-implementations + -Winfinite-recursion + -Wnewline-eof + -Wfour-char-constants + -Wselector + -Wundeclared-selector + -Wdocumentation + -Wmove + -Wc++11-extensions + -Wcomma + -Wno-exit-time-destructors + -Wrange-loop-analysis) + endif() +endfunction() diff --git a/cmake/FindGoogleTest.cmake b/cmake/FindGoogleTest.cmake new file mode 100644 index 000000000..99c2c02e3 --- /dev/null +++ b/cmake/FindGoogleTest.cmake @@ -0,0 +1,7 @@ +if(NOT GoogleTest_FOUND) + set(BUILD_GMOCK OFF CACHE BOOL "disable googlemock") + set(INSTALL_GTEST OFF CACHE BOOL "disable installation") + add_subdirectory("${PROJECT_SOURCE_DIR}/vendor/googletest") + include(GoogleTest) + set(GoogleTest_FOUND ON) +endif() diff --git a/cmake/FindJSONToolkit.cmake b/cmake/FindJSONToolkit.cmake new file mode 100644 index 000000000..ab15464b6 --- /dev/null +++ b/cmake/FindJSONToolkit.cmake @@ -0,0 +1,4 @@ +if(NOT JSONToolkit_FOUND) + add_subdirectory("${PROJECT_SOURCE_DIR}/vendor/jsontoolkit") + set(JSONToolkit_FOUND ON) +endif() diff --git a/cmake/bundler.cmake b/cmake/bundler.cmake deleted file mode 100644 index 203c09226..000000000 --- a/cmake/bundler.cmake +++ /dev/null @@ -1,10 +0,0 @@ -find_program(BUNDLE_BIN NAMES bundle) -set(JSONBINPACK_BUNDLER_PATH "${PROJECT_SOURCE_DIR}/build/bundler") -if(BUNDLE_BIN) - add_custom_target(bundler - WORKING_DIRECTORY "${PROJECT_SOURCE_DIR}" - VERBATIM - COMMAND "${BUNDLE_BIN}" install --path "${JSONBINPACK_BUNDLER_PATH}") -else() - message(WARNING "Could not find `bundler` in the system") -endif() diff --git a/cmake/doxygen.cmake b/cmake/doxygen.cmake deleted file mode 100644 index 84897b11b..000000000 --- a/cmake/doxygen.cmake +++ /dev/null @@ -1,15 +0,0 @@ -find_package(Doxygen) -if(DOXYGEN_FOUND) - set(DOXYGEN_IN "${PROJECT_SOURCE_DIR}/doxygen/Doxyfile.in") - set(DOXYGEN_OUT "${CMAKE_CURRENT_BINARY_DIR}/Doxyfile") - configure_file("${DOXYGEN_IN}" "${DOXYGEN_OUT}" @ONLY) - add_custom_target(doxygen - WORKING_DIRECTORY "${PROJECT_SOURCE_DIR}" - VERBATIM - COMMAND "${CMAKE_COMMAND}" -E make_directory "${JSONBINPACK_WEBSITE_OUT}" - COMMAND "${DOXYGEN_EXECUTABLE}" "${DOXYGEN_OUT}") - set_target_properties(doxygen - PROPERTIES FOLDER "Website") -else() - message(WARNING "Could not find `doxygen` in the system") -endif() diff --git a/cmake/jekyll.cmake b/cmake/jekyll.cmake deleted file mode 100644 index 052434ae2..000000000 --- a/cmake/jekyll.cmake +++ /dev/null @@ -1,12 +0,0 @@ -find_program(BUNDLE_BIN NAMES bundle) -if(BUNDLE_BIN) - add_custom_target(jekyll - WORKING_DIRECTORY "${PROJECT_SOURCE_DIR}" - VERBATIM - COMMAND "${BUNDLE_BIN}" exec jekyll build - --source "${JSONBINPACK_WEBSITE_SRC}" --destination "${JSONBINPACK_WEBSITE_OUT}") - set_target_properties(jekyll - PROPERTIES FOLDER "Website") -else() - message(WARNING "Could not find `jekyll` in the system") -endif() diff --git a/cmake/warnings.cmake b/cmake/warnings.cmake deleted file mode 100644 index e44acf115..000000000 --- a/cmake/warnings.cmake +++ /dev/null @@ -1,93 +0,0 @@ -if(NOA_COMPILER_LLVM) - add_compile_options( - -Wall - -Wextra - -Wpedantic - -Wshadow - -Wdouble-promotion - -Wconversion - -Wunused-parameter - -Wno-trigraphs - -Wunreachable-code - -Wno-missing-braces - -Wparentheses - -Wswitch - -Wunused-function - -Wno-unused-label - -Wno-unused-parameter - -Wunused-variable - -Wunused-value - -Wempty-body - -Wuninitialized - -Wno-shadow - -Wno-four-char-constants - -Wenum-conversion - -Wno-float-conversion - -Wno-newline-eof - -Wno-implicit-fallthrough - -Wno-sign-conversion - -Wno-unknown-pragmas - -Wno-non-virtual-dtor - -Wno-overloaded-virtual - -Wno-exit-time-destructors - -Wno-c++11-extensions - -Winvalid-offsetof - -Wbool-conversion - -Wint-conversion - -Wpointer-sign - -Wconditional-uninitialized - -Wconstant-conversion - -Wnon-literal-null-conversion - -Wshorten-64-to-32 - -Wdeprecated-implementations - -Winfinite-recursion - -Wcomma - -Wdocumentation - -Wmove - -Wrange-loop-analysis - # Assume that signed arithmetic overflow of addition, subtraction and - # multiplication wraps around using twos-complement representation - # See https://users.cs.utah.edu/~regehr/papers/overflow12.pdf - # See https://www.postgresql.org/message-id/1689.1134422394@sss.pgh.pa.us - -fwrapv) -elseif(NOA_COMPILER_GCC) - add_compile_options( - -Wall - -Wextra - -Werror - -Wpedantic - -Wshadow - -Wdouble-promotion - -Wconversion - -Wunused-parameter - -Wno-trigraphs - -Wunreachable-code - -Wno-missing-braces - -Wparentheses - -Wswitch - -Wunused-function - -Wno-unused-label - -Wno-unused-parameter - -Wunused-variable - -Wunused-value - -Wempty-body - -Wuninitialized - -Wno-shadow - -Wenum-conversion - -Wno-float-conversion - -Wno-implicit-fallthrough - -Wno-sign-conversion - -Wno-unknown-pragmas - -Wno-non-virtual-dtor - -Wno-overloaded-virtual - -Winvalid-offsetof - # Assume that signed arithmetic overflow of addition, subtraction and - # multiplication wraps around using twos-complement representation - # See https://users.cs.utah.edu/~regehr/papers/overflow12.pdf - # See https://www.postgresql.org/message-id/1689.1134422394@sss.pgh.pa.us - -fwrapv) -elseif(NOA_COMPILER_MSVC) - add_compile_options(/options:strict /W4 /WL /sdl) -else() - message(WARNING "Unrecognized compiler: ${CMAKE_CXX_COMPILER_ID}") -endif() diff --git a/scripts/lldb.sh b/scripts/lldb.sh deleted file mode 100755 index 329e09602..000000000 --- a/scripts/lldb.sh +++ /dev/null @@ -1,18 +0,0 @@ -#!/bin/sh - -set -o errexit -set -o nounset - -if [ $# -lt 1 ] -then - echo "Usage: $0 " 1>&2 - exit 1 -fi - -COMMANDS_JSON="$(ctest --test-dir ./build --tests-regex "$1" --show-only=json-v1 \ - | jq '.tests[0].command')" -COMMAND="$(echo "$COMMANDS_JSON" | jq --raw-output '.[0]')" -OPTIONS="$(echo "$COMMANDS_JSON" | jq --raw-output '.[1:] | join ("\n")')" - -# shellcheck disable=SC2086 -exec lldb "$COMMAND" -- $OPTIONS diff --git a/src/alterschema/CMakeLists.txt b/src/alterschema/CMakeLists.txt deleted file mode 100644 index accc23712..000000000 --- a/src/alterschema/CMakeLists.txt +++ /dev/null @@ -1,10 +0,0 @@ -add_library(sourcemeta_alterschema - include/alterschema/bundle.h - include/alterschema/rule.h - bundle.cc rule.cc) -target_include_directories(sourcemeta_alterschema PUBLIC - "${CMAKE_CURRENT_SOURCE_DIR}/include") -target_link_libraries(sourcemeta_alterschema PUBLIC sourcemeta::jsontoolkit::json) -target_link_libraries(sourcemeta_alterschema PUBLIC sourcemeta::jsontoolkit::jsonschema) -set_target_properties(sourcemeta_alterschema - PROPERTIES FOLDER "JSON BinPack/Alterschema") diff --git a/src/alterschema/bundle.cc b/src/alterschema/bundle.cc deleted file mode 100644 index 9044eadf4..000000000 --- a/src/alterschema/bundle.cc +++ /dev/null @@ -1,75 +0,0 @@ -#include - -#include // std::optional -#include // std::ostringstream -#include // std::runtime_error -#include // std::unordered_set - -auto sourcemeta::alterschema::Bundle::apply( - sourcemeta::jsontoolkit::JSON &document, - sourcemeta::jsontoolkit::Value &value, - const std::string &default_metaschema) const -> void { - // TODO: Handle schemas with embedded schemas that use different - // dialects/vocabularies - - const std::string metaschema{ - sourcemeta::jsontoolkit::metaschema(value).value_or(default_metaschema)}; - const std::optional draft{ - sourcemeta::jsontoolkit::draft(value, this->resolver_, metaschema).get()}; - if (!draft.has_value()) { - throw std::runtime_error( - "Could not determine the draft of the given schema"); - } - - const std::unordered_map vocabularies{ - sourcemeta::jsontoolkit::vocabularies(document, this->resolver_, - default_metaschema) - .get()}; - return apply_subschema(document, value, metaschema, draft.value(), - vocabularies, 0); -} - -auto sourcemeta::alterschema::Bundle::apply_subschema( - sourcemeta::jsontoolkit::JSON &document, - sourcemeta::jsontoolkit::Value &value, const std::string &metaschema, - const std::string &draft, - const std::unordered_map &vocabularies, - const std::size_t level) const -> void { - // (1) Canonicalize the current schema object - // Avoid recursion to not blow up the stack even on highly complex schemas - std::unordered_set processed_rules; - while (true) { - auto matches = processed_rules.size(); - for (auto const &pair : this->rules) { - const bool was_transformed{ - pair.second->apply(document, value, draft, vocabularies, level)}; - if (was_transformed) { - if (processed_rules.find(pair.first) != std::end(processed_rules)) { - std::ostringstream error; - error << "Rules must only be processed once: " << pair.first; - throw std::runtime_error(error.str()); - } - - processed_rules.insert(pair.first); - } - } - - if (matches < processed_rules.size()) { - continue; - } - - break; - } - - // (2) Canonicalize its sub-schemas - for (auto &subschema : sourcemeta::jsontoolkit::flat_subschema_iterator( - value, this->walker_, this->resolver_, metaschema)) { - apply_subschema(document, subschema, metaschema, draft, vocabularies, - level + 1); - } -} - -auto sourcemeta::alterschema::Bundle::resolver() const - -> const sourcemeta::jsontoolkit::schema_resolver_t & { - return this->resolver_; -} diff --git a/src/alterschema/include/alterschema/bundle.h b/src/alterschema/include/alterschema/bundle.h deleted file mode 100644 index 98c6d5aee..000000000 --- a/src/alterschema/include/alterschema/bundle.h +++ /dev/null @@ -1,60 +0,0 @@ -#ifndef SOURCEMETA_ALTERSCHEMA_BUNDLE_H_ -#define SOURCEMETA_ALTERSCHEMA_BUNDLE_H_ - -#include -#include -#include - -#include // assert -#include // std::derived_from -#include // std::map -#include // std::unique_ptr, std::make_unique -#include // std::string -#include // std::unordered_map -#include // std::move - -namespace sourcemeta::alterschema { -class Bundle { -public: - /// Instantiate an Alterschema bundle - Bundle(const sourcemeta::jsontoolkit::schema_walker_t &walker, - const sourcemeta::jsontoolkit::schema_resolver_t &resolver) - : walker_{walker}, resolver_{resolver} {} - - /// Add an Alterschema rule to the bundle - template T> auto add() -> void { - auto rule{std::make_unique()}; - const std::string &name{rule->name()}; - // Rules must only be defined once - assert(this->rules.find(name) == std::end(this->rules)); - this->rules.insert({name, std::move(rule)}); - } - - /// Apply the bundle of rules to a JSON document - auto apply(sourcemeta::jsontoolkit::JSON &document, - sourcemeta::jsontoolkit::Value &value, - const std::string &default_metaschema) const -> void; - - /// Apply the bundle of rules to a JSON document - inline auto apply(sourcemeta::jsontoolkit::JSON &document, - const std::string &default_metaschema) const -> void { - return this->apply(document, document, default_metaschema); - } - - /// Fetch the bundle resolver - auto resolver() const -> const sourcemeta::jsontoolkit::schema_resolver_t &; - -private: - auto - apply_subschema(sourcemeta::jsontoolkit::JSON &document, - sourcemeta::jsontoolkit::Value &value, - const std::string &metaschema, const std::string &draft, - const std::unordered_map &vocabularies, - const std::size_t level) const -> void; - sourcemeta::jsontoolkit::schema_walker_t walker_; - sourcemeta::jsontoolkit::schema_resolver_t resolver_; - std::map> rules; -}; -} // namespace sourcemeta::alterschema - -#endif diff --git a/src/alterschema/include/alterschema/rule.h b/src/alterschema/include/alterschema/rule.h deleted file mode 100644 index 7df64b9be..000000000 --- a/src/alterschema/include/alterschema/rule.h +++ /dev/null @@ -1,56 +0,0 @@ -#ifndef SOURCEMETA_ALTERSCHEMA_RULE_H_ -#define SOURCEMETA_ALTERSCHEMA_RULE_H_ - -#include - -#include // std::string -#include // std::unordered_map -#include // std::move - -namespace sourcemeta::alterschema { -class Rule { -public: - /// Create an Alterschema rule. Each rule must have a unique name. - Rule(std::string name) : name_{std::move(name)} {} - - // Necessary to wrap rules on smart pointers - virtual ~Rule() = default; - - // We don't need any of these - Rule(const Rule &) = delete; - Rule(Rule &&) = delete; - auto operator=(const Rule &) -> Rule & = delete; - auto operator=(Rule &&) -> Rule & = delete; - - /// Compare an Alterschema rule against another rule. - auto operator==(const Rule &other) const -> bool; - - /// Compare an Alterschema rule against another rule. - auto operator!=(const Rule &other) const -> bool; - - /// Fetch the name of an Alterschema rule - [[nodiscard]] auto name() const -> const std::string &; - - /// Apply the Alterschema rule to a JSON document - auto apply(sourcemeta::jsontoolkit::JSON &document, - sourcemeta::jsontoolkit::Value &value, const std::string &draft, - const std::unordered_map &vocabularies, - const std::size_t level) const -> bool; - -private: - /// The rule condition - [[nodiscard]] virtual auto - condition(const sourcemeta::jsontoolkit::Value &schema, - const std::string &draft, - const std::unordered_map &vocabularies, - const std::size_t level) const -> bool = 0; - - /// The rule transformation - virtual auto transform(sourcemeta::jsontoolkit::JSON &document, - sourcemeta::jsontoolkit::Value &value) const - -> void = 0; - const std::string name_; -}; -} // namespace sourcemeta::alterschema - -#endif diff --git a/src/alterschema/rule.cc b/src/alterschema/rule.cc deleted file mode 100644 index 79d49ebad..000000000 --- a/src/alterschema/rule.cc +++ /dev/null @@ -1,40 +0,0 @@ -#include - -#include // std::ostringstream -#include // std::runtime_error - -auto sourcemeta::alterschema::Rule::operator==( - const sourcemeta::alterschema::Rule &other) const -> bool { - return this->name() == other.name(); -} - -auto sourcemeta::alterschema::Rule::operator!=( - const sourcemeta::alterschema::Rule &other) const -> bool { - return !this->operator==(other); -} - -auto sourcemeta::alterschema::Rule::name() const -> const std::string & { - return this->name_; -} - -auto sourcemeta::alterschema::Rule::apply( - sourcemeta::jsontoolkit::JSON &document, - sourcemeta::jsontoolkit::Value &value, const std::string &draft, - const std::unordered_map &vocabularies, - const std::size_t level) const -> bool { - if (!this->condition(value, draft, vocabularies, level)) { - return false; - } - - this->transform(document, value); - - // The condition must always be false after applying the - // transformation in order to avoid infinite loops - if (this->condition(value, draft, vocabularies, level)) { - std::ostringstream error; - error << "Rule condition holds after application: " << this->name(); - throw std::runtime_error(error.str()); - } - - return true; -} diff --git a/src/canonicalizer/CMakeLists.txt b/src/canonicalizer/CMakeLists.txt index a8c5cdca9..97f078382 100644 --- a/src/canonicalizer/CMakeLists.txt +++ b/src/canonicalizer/CMakeLists.txt @@ -1,74 +1,79 @@ -add_library(sourcemeta_jsonbinpack_canonicalizer - include/jsonbinpack/canonicalizer/canonicalizer.h - canonicalizer.cc utils.h - rules/boolean_as_enum.h - rules/boolean_schema.h - rules/const_as_enum.h - rules/content_schema_without_content_media_type.h - rules/default_metaschema_2020_12.h - rules/dependent_required_tautology.h - rules/drop_non_array_keywords_applicator.h - rules/drop_non_array_keywords_content.h - rules/drop_non_array_keywords_format.h - rules/drop_non_array_keywords_unevaluated.h - rules/drop_non_array_keywords_validation.h - rules/drop_non_boolean_keywords_applicator.h - rules/drop_non_boolean_keywords_content.h - rules/drop_non_boolean_keywords_format.h - rules/drop_non_boolean_keywords_unevaluated.h - rules/drop_non_boolean_keywords_validation.h - rules/drop_non_null_keywords_applicator.h - rules/drop_non_null_keywords_content.h - rules/drop_non_null_keywords_format.h - rules/drop_non_null_keywords_unevaluated.h - rules/drop_non_null_keywords_validation.h - rules/drop_non_numeric_keywords_applicator.h - rules/drop_non_numeric_keywords_content.h - rules/drop_non_numeric_keywords_format.h - rules/drop_non_numeric_keywords_unevaluated.h - rules/drop_non_numeric_keywords_validation.h - rules/drop_non_object_keywords_applicator.h - rules/drop_non_object_keywords_content.h - rules/drop_non_object_keywords_format.h - rules/drop_non_object_keywords_unevaluated.h - rules/drop_non_object_keywords_validation.h - rules/drop_non_string_keywords_applicator.h - rules/drop_non_string_keywords_unevaluated.h - rules/drop_non_string_keywords_validation.h - rules/duplicate_allof_branches.h - rules/duplicate_anyof_branches.h - rules/duplicate_enum_values.h - rules/duplicate_required_values.h - rules/empty_array_as_const.h - rules/empty_dependent_required.h - rules/empty_object_as_const.h - rules/empty_pattern_properties.h - rules/empty_string_as_const.h - rules/equal_numeric_bounds_as_const.h - rules/exclusive_maximum_and_maximum.h - rules/exclusive_maximum_to_maximum.h - rules/exclusive_minimum_and_minimum.h - rules/exclusive_minimum_to_minimum.h - rules/if_without_then_else.h - rules/implicit_array_lower_bound.h - rules/implicit_object_lower_bound.h - rules/implicit_object_properties.h - rules/implicit_object_required.h - rules/implicit_string_lower_bound.h - rules/implicit_type_union.h - rules/implicit_unit_multiple_of.h - rules/max_contains_without_contains.h - rules/maximum_real_for_integer.h - rules/min_contains_without_contains.h - rules/min_properties_required_tautology.h - rules/minimum_real_for_integer.h - rules/null_as_const.h - rules/then_else_without_if.h - rules/type_union_anyof.h - rules/unsatisfiable_max_contains.h) -target_include_directories(sourcemeta_jsonbinpack_canonicalizer PUBLIC - "${CMAKE_CURRENT_SOURCE_DIR}/include") -target_link_libraries(sourcemeta_jsonbinpack_canonicalizer PUBLIC sourcemeta::jsontoolkit::json) -target_link_libraries(sourcemeta_jsonbinpack_canonicalizer PUBLIC sourcemeta_alterschema) -set_target_properties(sourcemeta_jsonbinpack_canonicalizer - PROPERTIES FOLDER "JSON BinPack/Canonicalizer") +noa_library(NAMESPACE sourcemeta PROJECT jsonbinpack NAME canonicalizer + FOLDER "JSON BinPack/canonicalizer" + SOURCES canonicalizer.cc utils.h + rules/boolean_as_enum.h + rules/boolean_schema.h + rules/const_as_enum.h + rules/content_schema_without_content_media_type.h + rules/default_metaschema_2020_12.h + rules/dependent_required_tautology.h + rules/drop_non_array_keywords_applicator.h + rules/drop_non_array_keywords_content.h + rules/drop_non_array_keywords_format.h + rules/drop_non_array_keywords_unevaluated.h + rules/drop_non_array_keywords_validation.h + rules/drop_non_boolean_keywords_applicator.h + rules/drop_non_boolean_keywords_content.h + rules/drop_non_boolean_keywords_format.h + rules/drop_non_boolean_keywords_unevaluated.h + rules/drop_non_boolean_keywords_validation.h + rules/drop_non_null_keywords_applicator.h + rules/drop_non_null_keywords_content.h + rules/drop_non_null_keywords_format.h + rules/drop_non_null_keywords_unevaluated.h + rules/drop_non_null_keywords_validation.h + rules/drop_non_numeric_keywords_applicator.h + rules/drop_non_numeric_keywords_content.h + rules/drop_non_numeric_keywords_format.h + rules/drop_non_numeric_keywords_unevaluated.h + rules/drop_non_numeric_keywords_validation.h + rules/drop_non_object_keywords_applicator.h + rules/drop_non_object_keywords_content.h + rules/drop_non_object_keywords_format.h + rules/drop_non_object_keywords_unevaluated.h + rules/drop_non_object_keywords_validation.h + rules/drop_non_string_keywords_applicator.h + rules/drop_non_string_keywords_unevaluated.h + rules/drop_non_string_keywords_validation.h + rules/duplicate_allof_branches.h + rules/duplicate_anyof_branches.h + rules/duplicate_enum_values.h + rules/duplicate_required_values.h + rules/empty_array_as_const.h + rules/empty_dependent_required.h + rules/empty_object_as_const.h + rules/empty_pattern_properties.h + rules/empty_string_as_const.h + rules/equal_numeric_bounds_as_const.h + rules/exclusive_maximum_and_maximum.h + rules/exclusive_maximum_to_maximum.h + rules/exclusive_minimum_and_minimum.h + rules/exclusive_minimum_to_minimum.h + rules/if_without_then_else.h + rules/implicit_array_lower_bound.h + rules/implicit_object_lower_bound.h + rules/implicit_object_properties.h + rules/implicit_object_required.h + rules/implicit_string_lower_bound.h + rules/implicit_type_union.h + rules/implicit_unit_multiple_of.h + rules/max_contains_without_contains.h + rules/maximum_real_for_integer.h + rules/min_contains_without_contains.h + rules/min_properties_required_tautology.h + rules/minimum_real_for_integer.h + rules/null_as_const.h + rules/then_else_without_if.h + rules/type_union_anyof.h + rules/unsatisfiable_max_contains.h) + +if(JSONBINPACK_INSTALL) + noa_library_install(NAMESPACE sourcemeta PROJECT jsonbinpack NAME canonicalizer) +endif() + +sourcemeta_jsonbinpack_add_compile_options(PRIVATE sourcemeta_jsonbinpack_canonicalizer) + +target_link_libraries(sourcemeta_jsonbinpack_canonicalizer PUBLIC + sourcemeta::jsontoolkit::json) +target_link_libraries(sourcemeta_jsonbinpack_canonicalizer PUBLIC + sourcemeta::jsontoolkit::jsonschema) diff --git a/src/canonicalizer/canonicalizer.cc b/src/canonicalizer/canonicalizer.cc index 86bd9c6a4..b8d452f53 100644 --- a/src/canonicalizer/canonicalizer.cc +++ b/src/canonicalizer/canonicalizer.cc @@ -1,153 +1,152 @@ -#include +#include // To be used by the rules below #include "utils.h" -#include #include // std::floor, std::ceil #include // std::set // Rules -#include "rules/boolean_as_enum.h" -#include "rules/boolean_schema.h" -#include "rules/const_as_enum.h" -#include "rules/content_schema_without_content_media_type.h" -#include "rules/default_metaschema_2020_12.h" -#include "rules/dependent_required_tautology.h" -#include "rules/drop_non_array_keywords_applicator.h" -#include "rules/drop_non_array_keywords_content.h" -#include "rules/drop_non_array_keywords_format.h" -#include "rules/drop_non_array_keywords_unevaluated.h" -#include "rules/drop_non_array_keywords_validation.h" -#include "rules/drop_non_boolean_keywords_applicator.h" -#include "rules/drop_non_boolean_keywords_content.h" -#include "rules/drop_non_boolean_keywords_format.h" -#include "rules/drop_non_boolean_keywords_unevaluated.h" -#include "rules/drop_non_boolean_keywords_validation.h" -#include "rules/drop_non_null_keywords_applicator.h" -#include "rules/drop_non_null_keywords_content.h" -#include "rules/drop_non_null_keywords_format.h" -#include "rules/drop_non_null_keywords_unevaluated.h" -#include "rules/drop_non_null_keywords_validation.h" -#include "rules/drop_non_numeric_keywords_applicator.h" -#include "rules/drop_non_numeric_keywords_content.h" -#include "rules/drop_non_numeric_keywords_format.h" -#include "rules/drop_non_numeric_keywords_unevaluated.h" -#include "rules/drop_non_numeric_keywords_validation.h" -#include "rules/drop_non_object_keywords_applicator.h" -#include "rules/drop_non_object_keywords_content.h" -#include "rules/drop_non_object_keywords_format.h" -#include "rules/drop_non_object_keywords_unevaluated.h" -#include "rules/drop_non_object_keywords_validation.h" -#include "rules/drop_non_string_keywords_applicator.h" -#include "rules/drop_non_string_keywords_unevaluated.h" -#include "rules/drop_non_string_keywords_validation.h" -#include "rules/duplicate_allof_branches.h" -#include "rules/duplicate_anyof_branches.h" -#include "rules/duplicate_enum_values.h" -#include "rules/duplicate_required_values.h" -#include "rules/empty_array_as_const.h" -#include "rules/empty_dependent_required.h" -#include "rules/empty_object_as_const.h" -#include "rules/empty_pattern_properties.h" -#include "rules/empty_string_as_const.h" -#include "rules/equal_numeric_bounds_as_const.h" -#include "rules/exclusive_maximum_and_maximum.h" -#include "rules/exclusive_maximum_to_maximum.h" -#include "rules/exclusive_minimum_and_minimum.h" -#include "rules/exclusive_minimum_to_minimum.h" -#include "rules/if_without_then_else.h" -#include "rules/implicit_array_lower_bound.h" -#include "rules/implicit_object_lower_bound.h" -#include "rules/implicit_object_properties.h" -#include "rules/implicit_object_required.h" -#include "rules/implicit_string_lower_bound.h" -#include "rules/implicit_type_union.h" -#include "rules/implicit_unit_multiple_of.h" -#include "rules/max_contains_without_contains.h" -#include "rules/maximum_real_for_integer.h" -#include "rules/min_contains_without_contains.h" -#include "rules/min_properties_required_tautology.h" -#include "rules/minimum_real_for_integer.h" -#include "rules/null_as_const.h" -#include "rules/then_else_without_if.h" -#include "rules/type_union_anyof.h" -#include "rules/unsatisfiable_max_contains.h" +// #include "rules/boolean_as_enum.h" +// #include "rules/boolean_schema.h" +// #include "rules/const_as_enum.h" +// #include "rules/content_schema_without_content_media_type.h" +// #include "rules/default_metaschema_2020_12.h" +// #include "rules/dependent_required_tautology.h" +// #include "rules/drop_non_array_keywords_applicator.h" +// #include "rules/drop_non_array_keywords_content.h" +// #include "rules/drop_non_array_keywords_format.h" +// #include "rules/drop_non_array_keywords_unevaluated.h" +// #include "rules/drop_non_array_keywords_validation.h" +// #include "rules/drop_non_boolean_keywords_applicator.h" +// #include "rules/drop_non_boolean_keywords_content.h" +// #include "rules/drop_non_boolean_keywords_format.h" +// #include "rules/drop_non_boolean_keywords_unevaluated.h" +// #include "rules/drop_non_boolean_keywords_validation.h" +// #include "rules/drop_non_null_keywords_applicator.h" +// #include "rules/drop_non_null_keywords_content.h" +// #include "rules/drop_non_null_keywords_format.h" +// #include "rules/drop_non_null_keywords_unevaluated.h" +// #include "rules/drop_non_null_keywords_validation.h" +// #include "rules/drop_non_numeric_keywords_applicator.h" +// #include "rules/drop_non_numeric_keywords_content.h" +// #include "rules/drop_non_numeric_keywords_format.h" +// #include "rules/drop_non_numeric_keywords_unevaluated.h" +// #include "rules/drop_non_numeric_keywords_validation.h" +// #include "rules/drop_non_object_keywords_applicator.h" +// #include "rules/drop_non_object_keywords_content.h" +// #include "rules/drop_non_object_keywords_format.h" +// #include "rules/drop_non_object_keywords_unevaluated.h" +// #include "rules/drop_non_object_keywords_validation.h" +// #include "rules/drop_non_string_keywords_applicator.h" +// #include "rules/drop_non_string_keywords_unevaluated.h" +// #include "rules/drop_non_string_keywords_validation.h" +// #include "rules/duplicate_allof_branches.h" +// #include "rules/duplicate_anyof_branches.h" +// #include "rules/duplicate_enum_values.h" +// #include "rules/duplicate_required_values.h" +// #include "rules/empty_array_as_const.h" +// #include "rules/empty_dependent_required.h" +// #include "rules/empty_object_as_const.h" +// #include "rules/empty_pattern_properties.h" +// #include "rules/empty_string_as_const.h" +// #include "rules/equal_numeric_bounds_as_const.h" +// #include "rules/exclusive_maximum_and_maximum.h" +// #include "rules/exclusive_maximum_to_maximum.h" +// #include "rules/exclusive_minimum_and_minimum.h" +// #include "rules/exclusive_minimum_to_minimum.h" +// #include "rules/if_without_then_else.h" +// #include "rules/implicit_array_lower_bound.h" +// #include "rules/implicit_object_lower_bound.h" +// #include "rules/implicit_object_properties.h" +// #include "rules/implicit_object_required.h" +// #include "rules/implicit_string_lower_bound.h" +// #include "rules/implicit_type_union.h" +// #include "rules/implicit_unit_multiple_of.h" +// #include "rules/max_contains_without_contains.h" +// #include "rules/maximum_real_for_integer.h" +// #include "rules/min_contains_without_contains.h" +// #include "rules/min_properties_required_tautology.h" +// #include "rules/minimum_real_for_integer.h" +// #include "rules/null_as_const.h" +// #include "rules/then_else_without_if.h" +// #include "rules/type_union_anyof.h" +// #include "rules/unsatisfiable_max_contains.h" -sourcemeta::jsonbinpack::Canonicalizer::Canonicalizer( - const sourcemeta::jsontoolkit::schema_resolver_t &resolver) - : bundle{sourcemeta::jsontoolkit::default_schema_walker, resolver} { +sourcemeta::jsonbinpack::Canonicalizer::Canonicalizer() { using namespace sourcemeta::jsonbinpack::canonicalizer; - this->bundle.template add(); - this->bundle.template add(); - this->bundle.template add(); - this->bundle.template add(); - this->bundle.template add(); - this->bundle.template add(); - this->bundle.template add(); - this->bundle.template add(); - this->bundle.template add(); - this->bundle.template add(); - this->bundle.template add(); - this->bundle.template add(); - this->bundle.template add(); - this->bundle.template add(); - this->bundle.template add(); - this->bundle.template add(); - this->bundle.template add(); - this->bundle.template add(); - this->bundle.template add(); - this->bundle.template add(); - this->bundle.template add(); - this->bundle.template add(); - this->bundle.template add(); - this->bundle.template add(); - this->bundle.template add(); - this->bundle.template add(); - this->bundle.template add(); - this->bundle.template add(); - this->bundle.template add(); - this->bundle.template add(); - this->bundle.template add(); - this->bundle.template add(); - this->bundle.template add(); - this->bundle.template add(); - this->bundle.template add(); - this->bundle.template add(); - this->bundle.template add(); - this->bundle.template add(); - this->bundle.template add(); - this->bundle.template add(); - this->bundle.template add(); - this->bundle.template add(); - this->bundle.template add(); - this->bundle.template add(); - this->bundle.template add(); - this->bundle.template add(); - this->bundle.template add(); - this->bundle.template add(); - this->bundle.template add(); - this->bundle.template add(); - this->bundle.template add(); - this->bundle.template add(); - this->bundle.template add(); - this->bundle.template add(); - this->bundle.template add(); - this->bundle.template add(); - this->bundle.template add(); - this->bundle.template add(); - this->bundle.template add(); - this->bundle.template add(); - this->bundle.template add(); - this->bundle.template add(); - this->bundle.template add(); - this->bundle.template add(); - this->bundle.template add(); + // this->bundle.template add(); + // this->bundle.template add(); + // this->bundle.template add(); + // this->bundle.template add(); + // this->bundle.template add(); + // this->bundle.template add(); + // this->bundle.template add(); + // this->bundle.template add(); + // this->bundle.template add(); + // this->bundle.template add(); + // this->bundle.template add(); + // this->bundle.template add(); + // this->bundle.template add(); + // this->bundle.template add(); + // this->bundle.template add(); + // this->bundle.template add(); + // this->bundle.template add(); + // this->bundle.template add(); + // this->bundle.template add(); + // this->bundle.template add(); + // this->bundle.template add(); + // this->bundle.template add(); + // this->bundle.template add(); + // this->bundle.template add(); + // this->bundle.template add(); + // this->bundle.template add(); + // this->bundle.template add(); + // this->bundle.template add(); + // this->bundle.template add(); + // this->bundle.template add(); + // this->bundle.template add(); + // this->bundle.template add(); + // this->bundle.template add(); + // this->bundle.template add(); + // this->bundle.template add(); + // this->bundle.template add(); + // this->bundle.template add(); + // this->bundle.template add(); + // this->bundle.template add(); + // this->bundle.template add(); + // this->bundle.template add(); + // this->bundle.template add(); + // this->bundle.template add(); + // this->bundle.template add(); + // this->bundle.template add(); + // this->bundle.template add(); + // this->bundle.template add(); + // this->bundle.template add(); + // this->bundle.template add(); + // this->bundle.template add(); + // this->bundle.template add(); + // this->bundle.template add(); + // this->bundle.template add(); + // this->bundle.template add(); + // this->bundle.template add(); + // this->bundle.template add(); + // this->bundle.template add(); + // this->bundle.template add(); + // this->bundle.template add(); + // this->bundle.template add(); + // this->bundle.template add(); + // this->bundle.template add(); + // this->bundle.template add(); + // this->bundle.template add(); + // this->bundle.template add(); } auto sourcemeta::jsonbinpack::Canonicalizer::apply( sourcemeta::jsontoolkit::JSON &document, - sourcemeta::jsontoolkit::Value &value, - const std::string &default_metaschema) const -> void { - this->bundle.apply(document, value, default_metaschema); + const sourcemeta::jsontoolkit::SchemaWalker &walker, + const sourcemeta::jsontoolkit::SchemaResolver &resolver, + const std::optional &default_dialect) const -> void { + this->bundle.apply(document, walker, resolver, + sourcemeta::jsontoolkit::empty_pointer, default_dialect); } diff --git a/src/canonicalizer/include/jsonbinpack/canonicalizer/canonicalizer.h b/src/canonicalizer/include/sourcemeta/jsonbinpack/canonicalizer.h similarity index 84% rename from src/canonicalizer/include/jsonbinpack/canonicalizer/canonicalizer.h rename to src/canonicalizer/include/sourcemeta/jsonbinpack/canonicalizer.h index fc8cab5a9..6e0750622 100644 --- a/src/canonicalizer/include/jsonbinpack/canonicalizer/canonicalizer.h +++ b/src/canonicalizer/include/sourcemeta/jsonbinpack/canonicalizer.h @@ -1,5 +1,5 @@ -#ifndef SOURCEMETA_JSONBINPACK_CANONICALIZER_CANONICALIZER_H_ -#define SOURCEMETA_JSONBINPACK_CANONICALIZER_CANONICALIZER_H_ +#ifndef SOURCEMETA_JSONBINPACK_CANONICALIZER_H_ +#define SOURCEMETA_JSONBINPACK_CANONICALIZER_H_ /// @defgroup canonicalizer Canonicalizer /// @brief A pure and deterministic function that simplifies a JSON Schema @@ -72,11 +72,11 @@ /// transformations for simplification purposes. /// @ingroup canonicalizer -#include -#include -#include +#include +#include -#include // std::string +#include // std::optional +#include // std::string namespace sourcemeta::jsonbinpack { @@ -84,21 +84,16 @@ namespace sourcemeta::jsonbinpack { class Canonicalizer { public: /// Create a Canonicalizer object - Canonicalizer(const sourcemeta::jsontoolkit::schema_resolver_t &resolver); + Canonicalizer(); /// Canonicalize the given JSON Schema auto apply(sourcemeta::jsontoolkit::JSON &document, - sourcemeta::jsontoolkit::Value &value, - const std::string &default_metaschema) const -> void; - - /// Canonicalize the given JSON Schema - inline auto apply(sourcemeta::jsontoolkit::JSON &document, - const std::string &default_metaschema) const -> void { - return apply(document, document, default_metaschema); - } + const sourcemeta::jsontoolkit::SchemaWalker &walker, + const sourcemeta::jsontoolkit::SchemaResolver &resolver, + const std::optional &default_dialect) const -> void; private: - sourcemeta::alterschema::Bundle bundle; + sourcemeta::jsontoolkit::SchemaTransformBundle bundle; }; } // namespace sourcemeta::jsonbinpack diff --git a/src/canonicalizer/rules/boolean_as_enum.h b/src/canonicalizer/rules/boolean_as_enum.h index fc1428b0b..473514fbf 100644 --- a/src/canonicalizer/rules/boolean_as_enum.h +++ b/src/canonicalizer/rules/boolean_as_enum.h @@ -17,31 +17,29 @@ namespace sourcemeta::jsonbinpack::canonicalizer { /// \f[\frac{S.type = boolean}{S \mapsto S \cup \{ enum \mapsto \langle false, /// true \rangle \} \setminus \{ type \} }\f] -class BooleanAsEnum final : public sourcemeta::alterschema::Rule { +class BooleanAsEnum final + : public sourcemeta::jsontoolkit::SchemaTransformRule { public: - BooleanAsEnum() : Rule("boolean_as_enum"){}; + BooleanAsEnum() : SchemaTransformRule("boolean_as_enum"){}; /// The rule condition - [[nodiscard]] auto - condition(const sourcemeta::jsontoolkit::Value &schema, - const std::string &draft, - const std::unordered_map &vocabularies, - const std::size_t) const -> bool override { - return draft == "https://json-schema.org/draft/2020-12/schema" && + [[nodiscard]] auto condition(const sourcemeta::jsontoolkit::JSON &schema, + const std::string &dialect, + const std::set &vocabularies, + const sourcemeta::jsontoolkit::Pointer &) const + -> bool override { + return dialect == "https://json-schema.org/draft/2020-12/schema" && vocabularies.contains( "https://json-schema.org/draft/2020-12/vocab/validation") && - sourcemeta::jsontoolkit::is_object(schema) && - sourcemeta::jsontoolkit::defines(schema, "type") && - sourcemeta::jsontoolkit::is_string( - sourcemeta::jsontoolkit::at(schema, "type")) && - sourcemeta::jsontoolkit::to_string( - sourcemeta::jsontoolkit::at(schema, "type")) == "boolean" && - !sourcemeta::jsontoolkit::defines(schema, "enum"); + schema.is_object() && schema.defines("type") && + sourcemeta::jsontoolkit::is_string(schema.at("type")) && + sourcemeta::jsontoolkit::to_string(schema.at("type")) == "boolean" && + !schema.defines("enum"); } /// The rule transformation - auto transform(sourcemeta::jsontoolkit::JSON &document, - sourcemeta::jsontoolkit::Value &value) const -> void override { + auto transform(sourcemeta::jsontoolkit::SchemaTransformer &schema) const + -> void override { auto choices{sourcemeta::jsontoolkit::make_array()}; sourcemeta::jsontoolkit::push_back(choices, sourcemeta::jsontoolkit::from(false)); diff --git a/src/canonicalizer/rules/boolean_schema.h b/src/canonicalizer/rules/boolean_schema.h index 5dc3c249f..16c69f5a9 100644 --- a/src/canonicalizer/rules/boolean_schema.h +++ b/src/canonicalizer/rules/boolean_schema.h @@ -19,21 +19,23 @@ namespace sourcemeta::jsonbinpack::canonicalizer { /// /// \f[\frac{S = false}{S \mapsto \{ not \mapsto \{\} \}}\f] -class BooleanSchema final : public sourcemeta::alterschema::Rule { +class BooleanSchema final + : public sourcemeta::jsontoolkit::SchemaTransformRule { public: - BooleanSchema() : Rule("boolean_schema"){}; + BooleanSchema() : SchemaTransformRule("boolean_schema"){}; /// The rule condition - [[nodiscard]] auto condition(const sourcemeta::jsontoolkit::Value &schema, + [[nodiscard]] auto condition(const sourcemeta::jsontoolkit::JSON &schema, const std::string &, - const std::unordered_map &, - const std::size_t) const -> bool override { + const std::set &, + const sourcemeta::jsontoolkit::Pointer &) const + -> bool override { return sourcemeta::jsontoolkit::is_boolean(schema); } /// The rule transformation - auto transform(sourcemeta::jsontoolkit::JSON &document, - sourcemeta::jsontoolkit::Value &value) const -> void override { + auto transform(sourcemeta::jsontoolkit::SchemaTransformer &schema) const + -> void override { const bool current_value{sourcemeta::jsontoolkit::to_boolean(value)}; sourcemeta::jsontoolkit::make_object(value); if (!current_value) { diff --git a/src/canonicalizer/rules/const_as_enum.h b/src/canonicalizer/rules/const_as_enum.h index 6ff861ead..7044248a2 100644 --- a/src/canonicalizer/rules/const_as_enum.h +++ b/src/canonicalizer/rules/const_as_enum.h @@ -16,26 +16,25 @@ namespace sourcemeta::jsonbinpack::canonicalizer { /// \f[\frac{const \in dom(S)}{S \mapsto S \cup \{ enum \mapsto \langle S.const /// \rangle \} \setminus \{const\} }\f] -class ConstAsEnum final : public sourcemeta::alterschema::Rule { +class ConstAsEnum final : public sourcemeta::jsontoolkit::SchemaTransformRule { public: - ConstAsEnum() : Rule("const_as_enum"){}; + ConstAsEnum() : SchemaTransformRule("const_as_enum"){}; /// The rule condition - [[nodiscard]] auto - condition(const sourcemeta::jsontoolkit::Value &schema, - const std::string &draft, - const std::unordered_map &vocabularies, - const std::size_t) const -> bool override { - return draft == "https://json-schema.org/draft/2020-12/schema" && + [[nodiscard]] auto condition(const sourcemeta::jsontoolkit::JSON &schema, + const std::string &dialect, + const std::set &vocabularies, + const sourcemeta::jsontoolkit::Pointer &) const + -> bool override { + return dialect == "https://json-schema.org/draft/2020-12/schema" && vocabularies.contains( "https://json-schema.org/draft/2020-12/vocab/validation") && - sourcemeta::jsontoolkit::is_object(schema) && - sourcemeta::jsontoolkit::defines(schema, "const"); + schema.is_object() && schema.defines("const"); } /// The rule transformation - auto transform(sourcemeta::jsontoolkit::JSON &document, - sourcemeta::jsontoolkit::Value &value) const -> void override { + auto transform(sourcemeta::jsontoolkit::SchemaTransformer &schema) const + -> void override { sourcemeta::jsontoolkit::JSON values{sourcemeta::jsontoolkit::make_array()}; sourcemeta::jsontoolkit::push_back( values, sourcemeta::jsontoolkit::at(value, "const")); diff --git a/src/canonicalizer/rules/content_schema_without_content_media_type.h b/src/canonicalizer/rules/content_schema_without_content_media_type.h index 061d09cea..6e830ecfa 100644 --- a/src/canonicalizer/rules/content_schema_without_content_media_type.h +++ b/src/canonicalizer/rules/content_schema_without_content_media_type.h @@ -16,28 +16,27 @@ namespace sourcemeta::jsonbinpack::canonicalizer { /// \f[\frac{contentSchema \in dom(S) \land contentMediaType \not\in dom(S)}{S /// \mapsto S \setminus \{contentSchema\} }\f] class ContentSchemaWithoutContentMediaType final - : public sourcemeta::alterschema::Rule { + : public sourcemeta::jsontoolkit::SchemaTransformRule { public: ContentSchemaWithoutContentMediaType() - : Rule("content_schema_without_content_media_type"){}; + : SchemaTransformRule("content_schema_without_content_media_type"){}; /// The rule condition - [[nodiscard]] auto - condition(const sourcemeta::jsontoolkit::Value &schema, - const std::string &draft, - const std::unordered_map &vocabularies, - const std::size_t) const -> bool override { - return draft == "https://json-schema.org/draft/2020-12/schema" && + [[nodiscard]] auto condition(const sourcemeta::jsontoolkit::JSON &schema, + const std::string &dialect, + const std::set &vocabularies, + const sourcemeta::jsontoolkit::Pointer &) const + -> bool override { + return dialect == "https://json-schema.org/draft/2020-12/schema" && vocabularies.contains( "https://json-schema.org/draft/2020-12/vocab/content") && - sourcemeta::jsontoolkit::is_object(schema) && - sourcemeta::jsontoolkit::defines(schema, "contentSchema") && - !sourcemeta::jsontoolkit::defines(schema, "contentMediaType"); + schema.is_object() && schema.defines("contentSchema") && + !schema.defines("contentMediaType"); } /// The rule transformation auto transform(sourcemeta::jsontoolkit::JSON &, - sourcemeta::jsontoolkit::Value &value) const -> void override { + sourcemeta::jsontoolkit::JSON &value) const -> void override { sourcemeta::jsontoolkit::erase(value, "contentSchema"); } }; diff --git a/src/canonicalizer/rules/default_metaschema_2020_12.h b/src/canonicalizer/rules/default_metaschema_2020_12.h index 7bca5f8e4..83c18041a 100644 --- a/src/canonicalizer/rules/default_metaschema_2020_12.h +++ b/src/canonicalizer/rules/default_metaschema_2020_12.h @@ -16,26 +16,26 @@ namespace sourcemeta::jsonbinpack::canonicalizer { /// \mapsto S \cup \{ \$schema \mapsto /// \textsl{https://json-schema.org/draft/2020-12/schema} \} }\f] -class DefaultMetaschema_2020_12 final : public sourcemeta::alterschema::Rule { +class DefaultMetaschema_2020_12 final + : public sourcemeta::jsontoolkit::SchemaTransformRule { public: - DefaultMetaschema_2020_12() : Rule("default_metaschema_2020_12"){}; + DefaultMetaschema_2020_12() + : SchemaTransformRule("default_metaschema_2020_12"){}; /// The rule condition - [[nodiscard]] auto - condition(const sourcemeta::jsontoolkit::Value &schema, - const std::string &draft, - const std::unordered_map &vocabularies, - const std::size_t level) const -> bool override { - return draft == "https://json-schema.org/draft/2020-12/schema" && + [[nodiscard]] auto condition( + const sourcemeta::jsontoolkit::JSON &schema, const std::string &dialect, + const std::set &vocabularies, + const sourcemeta::jsontoolkit::Pointer &level) const -> bool override { + return dialect == "https://json-schema.org/draft/2020-12/schema" && vocabularies.contains( "https://json-schema.org/draft/2020-12/vocab/core") && - sourcemeta::jsontoolkit::is_object(schema) && - !sourcemeta::jsontoolkit::defines(schema, "$schema") && level == 0; + schema.is_object() && !schema.defines("$schema") && level == 0; } /// The rule transformation - auto transform(sourcemeta::jsontoolkit::JSON &document, - sourcemeta::jsontoolkit::Value &value) const -> void override { + auto transform(sourcemeta::jsontoolkit::SchemaTransformer &schema) const + -> void override { sourcemeta::jsontoolkit::assign( document, value, "$schema", sourcemeta::jsontoolkit::from( diff --git a/src/canonicalizer/rules/dependent_required_tautology.h b/src/canonicalizer/rules/dependent_required_tautology.h index fa069525b..7e08273b6 100644 --- a/src/canonicalizer/rules/dependent_required_tautology.h +++ b/src/canonicalizer/rules/dependent_required_tautology.h @@ -27,31 +27,28 @@ namespace sourcemeta::jsonbinpack::canonicalizer { /// \f[R = \bigcup \{ v \mid (k, v) \in S.dependentRequired \land k \in /// S.required \}\f] -class DependentRequiredTautology final : public sourcemeta::alterschema::Rule { +class DependentRequiredTautology final + : public sourcemeta::jsontoolkit::SchemaTransformRule { public: - DependentRequiredTautology() : Rule("dependent_required_tautology"){}; + DependentRequiredTautology() + : SchemaTransformRule("dependent_required_tautology"){}; /// The rule condition - [[nodiscard]] auto - condition(const sourcemeta::jsontoolkit::Value &schema, - const std::string &draft, - const std::unordered_map &vocabularies, - const std::size_t) const -> bool override { - return draft == "https://json-schema.org/draft/2020-12/schema" && + [[nodiscard]] auto condition(const sourcemeta::jsontoolkit::JSON &schema, + const std::string &dialect, + const std::set &vocabularies, + const sourcemeta::jsontoolkit::Pointer &) const + -> bool override { + return dialect == "https://json-schema.org/draft/2020-12/schema" && vocabularies.contains( "https://json-schema.org/draft/2020-12/vocab/validation") && - sourcemeta::jsontoolkit::is_object(schema) && - sourcemeta::jsontoolkit::defines(schema, "dependentRequired") && - sourcemeta::jsontoolkit::is_object( - sourcemeta::jsontoolkit::at(schema, "dependentRequired")) && - sourcemeta::jsontoolkit::defines(schema, "required") && - sourcemeta::jsontoolkit::is_array( - sourcemeta::jsontoolkit::at(schema, "required")) && + schema.is_object() && schema.defines("dependentRequired") && + sourcemeta::jsontoolkit::is_object(schema.at("dependentRequired")) && + schema.defines("required") && + sourcemeta::jsontoolkit::is_array(schema.at("required")) && std::any_of( - sourcemeta::jsontoolkit::cbegin_array( - sourcemeta::jsontoolkit::at(schema, "required")), - sourcemeta::jsontoolkit::cend_array( - sourcemeta::jsontoolkit::at(schema, "required")), + sourcemeta::jsontoolkit::cbegin_array(schema.at("required")), + sourcemeta::jsontoolkit::cend_array(schema.at("required")), [&schema](const auto &element) { return sourcemeta::jsontoolkit::is_string(element) && sourcemeta::jsontoolkit::defines( @@ -62,8 +59,8 @@ class DependentRequiredTautology final : public sourcemeta::alterschema::Rule { } /// The rule transformation - auto transform(sourcemeta::jsontoolkit::JSON &document, - sourcemeta::jsontoolkit::Value &value) const -> void override { + auto transform(sourcemeta::jsontoolkit::SchemaTransformer &schema) const + -> void override { const auto ¤t_requirements{sourcemeta::jsontoolkit::from( sourcemeta::jsontoolkit::at(value, "required"))}; diff --git a/src/canonicalizer/rules/drop_non_array_keywords_applicator.h b/src/canonicalizer/rules/drop_non_array_keywords_applicator.h index a2df9cb82..863732b1a 100644 --- a/src/canonicalizer/rules/drop_non_array_keywords_applicator.h +++ b/src/canonicalizer/rules/drop_non_array_keywords_applicator.h @@ -23,25 +23,23 @@ namespace sourcemeta::jsonbinpack::canonicalizer { /// dependentSchemas, propertyNames\}\f] class DropNonArrayKeywordsApplicator final - : public sourcemeta::alterschema::Rule { + : public sourcemeta::jsontoolkit::SchemaTransformRule { public: DropNonArrayKeywordsApplicator() - : Rule("drop_non_array_keywords_applicator"){}; + : SchemaTransformRule("drop_non_array_keywords_applicator"){}; /// The rule condition - [[nodiscard]] auto - condition(const sourcemeta::jsontoolkit::Value &schema, - const std::string &draft, - const std::unordered_map &vocabularies, - const std::size_t) const -> bool override { - return draft == "https://json-schema.org/draft/2020-12/schema" && + [[nodiscard]] auto condition(const sourcemeta::jsontoolkit::JSON &schema, + const std::string &dialect, + const std::set &vocabularies, + const sourcemeta::jsontoolkit::Pointer &) const + -> bool override { + return dialect == "https://json-schema.org/draft/2020-12/schema" && vocabularies.contains( "https://json-schema.org/draft/2020-12/vocab/validation") && - sourcemeta::jsontoolkit::defines(schema, "type") && - sourcemeta::jsontoolkit::is_string( - sourcemeta::jsontoolkit::at(schema, "type")) && - sourcemeta::jsontoolkit::to_string( - sourcemeta::jsontoolkit::at(schema, "type")) == "array" && + schema.defines("type") && + sourcemeta::jsontoolkit::is_string(schema.at("type")) && + sourcemeta::jsontoolkit::to_string(schema.at("type")) == "array" && vocabularies.contains( "https://json-schema.org/draft/2020-12/vocab/applicator") && sourcemeta::jsontoolkit::defines_any(schema, @@ -50,7 +48,7 @@ class DropNonArrayKeywordsApplicator final /// The rule transformation auto transform(sourcemeta::jsontoolkit::JSON &, - sourcemeta::jsontoolkit::Value &value) const -> void override { + sourcemeta::jsontoolkit::JSON &value) const -> void override { sourcemeta::jsontoolkit::erase_many(value, this->BLACKLIST_APPLICATOR); } diff --git a/src/canonicalizer/rules/drop_non_array_keywords_content.h b/src/canonicalizer/rules/drop_non_array_keywords_content.h index 9359bf6a8..51a041fbc 100644 --- a/src/canonicalizer/rules/drop_non_array_keywords_content.h +++ b/src/canonicalizer/rules/drop_non_array_keywords_content.h @@ -21,25 +21,24 @@ namespace sourcemeta::jsonbinpack::canonicalizer { /// /// \f[K = \{contentEncoding, contentMediaType, contentSchema\}\f] -class DropNonArrayKeywordsContent final : public sourcemeta::alterschema::Rule { +class DropNonArrayKeywordsContent final + : public sourcemeta::jsontoolkit::SchemaTransformRule { public: - DropNonArrayKeywordsContent() : Rule("drop_non_array_keywords_content"){}; + DropNonArrayKeywordsContent() + : SchemaTransformRule("drop_non_array_keywords_content"){}; /// The rule condition - [[nodiscard]] auto - condition(const sourcemeta::jsontoolkit::Value &schema, - const std::string &draft, - const std::unordered_map &vocabularies, - const std::size_t) const -> bool override { - return draft == "https://json-schema.org/draft/2020-12/schema" && + [[nodiscard]] auto condition(const sourcemeta::jsontoolkit::JSON &schema, + const std::string &dialect, + const std::set &vocabularies, + const sourcemeta::jsontoolkit::Pointer &) const + -> bool override { + return dialect == "https://json-schema.org/draft/2020-12/schema" && vocabularies.contains( "https://json-schema.org/draft/2020-12/vocab/validation") && - sourcemeta::jsontoolkit::is_object(schema) && - sourcemeta::jsontoolkit::defines(schema, "type") && - sourcemeta::jsontoolkit::is_string( - sourcemeta::jsontoolkit::at(schema, "type")) && - sourcemeta::jsontoolkit::to_string( - sourcemeta::jsontoolkit::at(schema, "type")) == "array" && + schema.is_object() && schema.defines("type") && + sourcemeta::jsontoolkit::is_string(schema.at("type")) && + sourcemeta::jsontoolkit::to_string(schema.at("type")) == "array" && vocabularies.contains( "https://json-schema.org/draft/2020-12/vocab/content") && sourcemeta::jsontoolkit::defines_any(schema, @@ -48,7 +47,7 @@ class DropNonArrayKeywordsContent final : public sourcemeta::alterschema::Rule { /// The rule transformation auto transform(sourcemeta::jsontoolkit::JSON &, - sourcemeta::jsontoolkit::Value &value) const -> void override { + sourcemeta::jsontoolkit::JSON &value) const -> void override { sourcemeta::jsontoolkit::erase_many(value, this->BLACKLIST_CONTENT); } diff --git a/src/canonicalizer/rules/drop_non_array_keywords_format.h b/src/canonicalizer/rules/drop_non_array_keywords_format.h index 1ffe429e3..c10483c90 100644 --- a/src/canonicalizer/rules/drop_non_array_keywords_format.h +++ b/src/canonicalizer/rules/drop_non_array_keywords_format.h @@ -17,24 +17,24 @@ namespace sourcemeta::jsonbinpack::canonicalizer { /// \f[\frac{S.type = array \land format \in dom(S) }{S /// \mapsto S \setminus \{ format \} }\f] -class DropNonArrayKeywordsFormat final : public sourcemeta::alterschema::Rule { +class DropNonArrayKeywordsFormat final + : public sourcemeta::jsontoolkit::SchemaTransformRule { public: - DropNonArrayKeywordsFormat() : Rule("drop_non_array_keywords_format"){}; + DropNonArrayKeywordsFormat() + : SchemaTransformRule("drop_non_array_keywords_format"){}; /// The rule condition - [[nodiscard]] auto - condition(const sourcemeta::jsontoolkit::Value &schema, - const std::string &draft, - const std::unordered_map &vocabularies, - const std::size_t) const -> bool override { - return draft == "https://json-schema.org/draft/2020-12/schema" && + [[nodiscard]] auto condition(const sourcemeta::jsontoolkit::JSON &schema, + const std::string &dialect, + const std::set &vocabularies, + const sourcemeta::jsontoolkit::Pointer &) const + -> bool override { + return dialect == "https://json-schema.org/draft/2020-12/schema" && vocabularies.contains( "https://json-schema.org/draft/2020-12/vocab/validation") && - sourcemeta::jsontoolkit::defines(schema, "type") && - sourcemeta::jsontoolkit::is_string( - sourcemeta::jsontoolkit::at(schema, "type")) && - sourcemeta::jsontoolkit::to_string( - sourcemeta::jsontoolkit::at(schema, "type")) == "array" && + schema.defines("type") && + sourcemeta::jsontoolkit::is_string(schema.at("type")) && + sourcemeta::jsontoolkit::to_string(schema.at("type")) == "array" && (vocabularies.contains("https://json-schema.org/draft/2020-12/vocab/" "format-annotation") || vocabularies.contains("https://json-schema.org/draft/2020-12/vocab/" @@ -44,7 +44,7 @@ class DropNonArrayKeywordsFormat final : public sourcemeta::alterschema::Rule { /// The rule transformation auto transform(sourcemeta::jsontoolkit::JSON &, - sourcemeta::jsontoolkit::Value &value) const -> void override { + sourcemeta::jsontoolkit::JSON &value) const -> void override { sourcemeta::jsontoolkit::erase_many(value, this->BLACKLIST_FORMAT); } diff --git a/src/canonicalizer/rules/drop_non_array_keywords_unevaluated.h b/src/canonicalizer/rules/drop_non_array_keywords_unevaluated.h index 35ae0beeb..86e23a9b0 100644 --- a/src/canonicalizer/rules/drop_non_array_keywords_unevaluated.h +++ b/src/canonicalizer/rules/drop_non_array_keywords_unevaluated.h @@ -18,25 +18,23 @@ namespace sourcemeta::jsonbinpack::canonicalizer { /// \mapsto S \setminus \{ unevaluatedProperties \} }\f] class DropNonArrayKeywordsUnevaluated final - : public sourcemeta::alterschema::Rule { + : public sourcemeta::jsontoolkit::SchemaTransformRule { public: DropNonArrayKeywordsUnevaluated() - : Rule("drop_non_array_keywords_unevaluated"){}; + : SchemaTransformRule("drop_non_array_keywords_unevaluated"){}; /// The rule condition - [[nodiscard]] auto - condition(const sourcemeta::jsontoolkit::Value &schema, - const std::string &draft, - const std::unordered_map &vocabularies, - const std::size_t) const -> bool override { - return draft == "https://json-schema.org/draft/2020-12/schema" && + [[nodiscard]] auto condition(const sourcemeta::jsontoolkit::JSON &schema, + const std::string &dialect, + const std::set &vocabularies, + const sourcemeta::jsontoolkit::Pointer &) const + -> bool override { + return dialect == "https://json-schema.org/draft/2020-12/schema" && vocabularies.contains( "https://json-schema.org/draft/2020-12/vocab/validation") && - sourcemeta::jsontoolkit::defines(schema, "type") && - sourcemeta::jsontoolkit::is_string( - sourcemeta::jsontoolkit::at(schema, "type")) && - sourcemeta::jsontoolkit::to_string( - sourcemeta::jsontoolkit::at(schema, "type")) == "array" && + schema.defines("type") && + sourcemeta::jsontoolkit::is_string(schema.at("type")) && + sourcemeta::jsontoolkit::to_string(schema.at("type")) == "array" && vocabularies.contains( "https://json-schema.org/draft/2020-12/vocab/unevaluated") && sourcemeta::jsontoolkit::defines_any(schema, @@ -45,7 +43,7 @@ class DropNonArrayKeywordsUnevaluated final /// The rule transformation auto transform(sourcemeta::jsontoolkit::JSON &, - sourcemeta::jsontoolkit::Value &value) const -> void override { + sourcemeta::jsontoolkit::JSON &value) const -> void override { sourcemeta::jsontoolkit::erase_many(value, this->BLACKLIST_UNEVALUATED); } diff --git a/src/canonicalizer/rules/drop_non_array_keywords_validation.h b/src/canonicalizer/rules/drop_non_array_keywords_validation.h index 81d44d03d..8b906f9c7 100644 --- a/src/canonicalizer/rules/drop_non_array_keywords_validation.h +++ b/src/canonicalizer/rules/drop_non_array_keywords_validation.h @@ -27,32 +27,30 @@ namespace sourcemeta::jsonbinpack::canonicalizer { /// required\}\f] class DropNonArrayKeywordsValidation final - : public sourcemeta::alterschema::Rule { + : public sourcemeta::jsontoolkit::SchemaTransformRule { public: DropNonArrayKeywordsValidation() - : Rule("drop_non_array_keywords_validation"){}; + : SchemaTransformRule("drop_non_array_keywords_validation"){}; /// The rule condition - [[nodiscard]] auto - condition(const sourcemeta::jsontoolkit::Value &schema, - const std::string &draft, - const std::unordered_map &vocabularies, - const std::size_t) const -> bool override { - return draft == "https://json-schema.org/draft/2020-12/schema" && + [[nodiscard]] auto condition(const sourcemeta::jsontoolkit::JSON &schema, + const std::string &dialect, + const std::set &vocabularies, + const sourcemeta::jsontoolkit::Pointer &) const + -> bool override { + return dialect == "https://json-schema.org/draft/2020-12/schema" && vocabularies.contains( "https://json-schema.org/draft/2020-12/vocab/validation") && - sourcemeta::jsontoolkit::defines(schema, "type") && - sourcemeta::jsontoolkit::is_string( - sourcemeta::jsontoolkit::at(schema, "type")) && - sourcemeta::jsontoolkit::to_string( - sourcemeta::jsontoolkit::at(schema, "type")) == "array" && + schema.defines("type") && + sourcemeta::jsontoolkit::is_string(schema.at("type")) && + sourcemeta::jsontoolkit::to_string(schema.at("type")) == "array" && sourcemeta::jsontoolkit::defines_any(schema, this->BLACKLIST_VALIDATION); } /// The rule transformation auto transform(sourcemeta::jsontoolkit::JSON &, - sourcemeta::jsontoolkit::Value &value) const -> void override { + sourcemeta::jsontoolkit::JSON &value) const -> void override { sourcemeta::jsontoolkit::erase_many(value, this->BLACKLIST_VALIDATION); } diff --git a/src/canonicalizer/rules/drop_non_boolean_keywords_applicator.h b/src/canonicalizer/rules/drop_non_boolean_keywords_applicator.h index cd8c29024..8eea34397 100644 --- a/src/canonicalizer/rules/drop_non_boolean_keywords_applicator.h +++ b/src/canonicalizer/rules/drop_non_boolean_keywords_applicator.h @@ -25,18 +25,18 @@ namespace sourcemeta::jsonbinpack::canonicalizer { /// \f[K_{array} = \{prefixItems, contains, items\}\f] class DropNonBooleanKeywordsApplicator final - : public sourcemeta::alterschema::Rule { + : public sourcemeta::jsontoolkit::SchemaTransformRule { public: DropNonBooleanKeywordsApplicator() - : Rule("drop_non_boolean_keywords_applicator"){}; + : SchemaTransformRule("drop_non_boolean_keywords_applicator"){}; /// The rule condition - [[nodiscard]] auto - condition(const sourcemeta::jsontoolkit::Value &schema, - const std::string &draft, - const std::unordered_map &vocabularies, - const std::size_t) const -> bool override { - return draft == "https://json-schema.org/draft/2020-12/schema" && + [[nodiscard]] auto condition(const sourcemeta::jsontoolkit::JSON &schema, + const std::string &dialect, + const std::set &vocabularies, + const sourcemeta::jsontoolkit::Pointer &) const + -> bool override { + return dialect == "https://json-schema.org/draft/2020-12/schema" && is_boolean_schema(schema, vocabularies) && vocabularies.contains( "https://json-schema.org/draft/2020-12/vocab/applicator") && @@ -46,7 +46,7 @@ class DropNonBooleanKeywordsApplicator final /// The rule transformation auto transform(sourcemeta::jsontoolkit::JSON &, - sourcemeta::jsontoolkit::Value &value) const -> void override { + sourcemeta::jsontoolkit::JSON &value) const -> void override { sourcemeta::jsontoolkit::erase_many(value, this->BLACKLIST_APPLICATOR); } diff --git a/src/canonicalizer/rules/drop_non_boolean_keywords_content.h b/src/canonicalizer/rules/drop_non_boolean_keywords_content.h index babac6b2e..92dad46c6 100644 --- a/src/canonicalizer/rules/drop_non_boolean_keywords_content.h +++ b/src/canonicalizer/rules/drop_non_boolean_keywords_content.h @@ -22,17 +22,18 @@ namespace sourcemeta::jsonbinpack::canonicalizer { /// \f[K = \{contentEncoding, contentMediaType, contentSchema\}\f] class DropNonBooleanKeywordsContent final - : public sourcemeta::alterschema::Rule { + : public sourcemeta::jsontoolkit::SchemaTransformRule { public: - DropNonBooleanKeywordsContent() : Rule("drop_non_boolean_keywords_content"){}; + DropNonBooleanKeywordsContent() + : SchemaTransformRule("drop_non_boolean_keywords_content"){}; /// The rule condition - [[nodiscard]] auto - condition(const sourcemeta::jsontoolkit::Value &schema, - const std::string &draft, - const std::unordered_map &vocabularies, - const std::size_t) const -> bool override { - return draft == "https://json-schema.org/draft/2020-12/schema" && + [[nodiscard]] auto condition(const sourcemeta::jsontoolkit::JSON &schema, + const std::string &dialect, + const std::set &vocabularies, + const sourcemeta::jsontoolkit::Pointer &) const + -> bool override { + return dialect == "https://json-schema.org/draft/2020-12/schema" && is_boolean_schema(schema, vocabularies) && vocabularies.contains( "https://json-schema.org/draft/2020-12/vocab/content") && @@ -42,7 +43,7 @@ class DropNonBooleanKeywordsContent final /// The rule transformation auto transform(sourcemeta::jsontoolkit::JSON &, - sourcemeta::jsontoolkit::Value &value) const -> void override { + sourcemeta::jsontoolkit::JSON &value) const -> void override { sourcemeta::jsontoolkit::erase_many(value, this->BLACKLIST_CONTENT); } diff --git a/src/canonicalizer/rules/drop_non_boolean_keywords_format.h b/src/canonicalizer/rules/drop_non_boolean_keywords_format.h index 6211c9ebb..9729cc9ed 100644 --- a/src/canonicalizer/rules/drop_non_boolean_keywords_format.h +++ b/src/canonicalizer/rules/drop_non_boolean_keywords_format.h @@ -18,17 +18,18 @@ namespace sourcemeta::jsonbinpack::canonicalizer { /// \mapsto S \setminus \{ format \} }\f] class DropNonBooleanKeywordsFormat final - : public sourcemeta::alterschema::Rule { + : public sourcemeta::jsontoolkit::SchemaTransformRule { public: - DropNonBooleanKeywordsFormat() : Rule("drop_non_boolean_keywords_format"){}; + DropNonBooleanKeywordsFormat() + : SchemaTransformRule("drop_non_boolean_keywords_format"){}; /// The rule condition - [[nodiscard]] auto - condition(const sourcemeta::jsontoolkit::Value &schema, - const std::string &draft, - const std::unordered_map &vocabularies, - const std::size_t) const -> bool override { - return draft == "https://json-schema.org/draft/2020-12/schema" && + [[nodiscard]] auto condition(const sourcemeta::jsontoolkit::JSON &schema, + const std::string &dialect, + const std::set &vocabularies, + const sourcemeta::jsontoolkit::Pointer &) const + -> bool override { + return dialect == "https://json-schema.org/draft/2020-12/schema" && is_boolean_schema(schema, vocabularies) && (vocabularies.contains("https://json-schema.org/draft/2020-12/vocab/" "format-annotation") || @@ -39,7 +40,7 @@ class DropNonBooleanKeywordsFormat final /// The rule transformation auto transform(sourcemeta::jsontoolkit::JSON &, - sourcemeta::jsontoolkit::Value &value) const -> void override { + sourcemeta::jsontoolkit::JSON &value) const -> void override { sourcemeta::jsontoolkit::erase_many(value, this->BLACKLIST_FORMAT); } diff --git a/src/canonicalizer/rules/drop_non_boolean_keywords_unevaluated.h b/src/canonicalizer/rules/drop_non_boolean_keywords_unevaluated.h index b3f6b7e3e..c0b8c3e0c 100644 --- a/src/canonicalizer/rules/drop_non_boolean_keywords_unevaluated.h +++ b/src/canonicalizer/rules/drop_non_boolean_keywords_unevaluated.h @@ -22,18 +22,18 @@ namespace sourcemeta::jsonbinpack::canonicalizer { /// \f[K = \{ unevaluatedItems, unevaluatedProperties \}\f] class DropNonBooleanKeywordsUnevaluated final - : public sourcemeta::alterschema::Rule { + : public sourcemeta::jsontoolkit::SchemaTransformRule { public: DropNonBooleanKeywordsUnevaluated() - : Rule("drop_non_boolean_keywords_unevaluated"){}; + : SchemaTransformRule("drop_non_boolean_keywords_unevaluated"){}; /// The rule condition - [[nodiscard]] auto - condition(const sourcemeta::jsontoolkit::Value &schema, - const std::string &draft, - const std::unordered_map &vocabularies, - const std::size_t) const -> bool override { - return draft == "https://json-schema.org/draft/2020-12/schema" && + [[nodiscard]] auto condition(const sourcemeta::jsontoolkit::JSON &schema, + const std::string &dialect, + const std::set &vocabularies, + const sourcemeta::jsontoolkit::Pointer &) const + -> bool override { + return dialect == "https://json-schema.org/draft/2020-12/schema" && is_boolean_schema(schema, vocabularies) && vocabularies.contains( "https://json-schema.org/draft/2020-12/vocab/unevaluated") && @@ -43,7 +43,7 @@ class DropNonBooleanKeywordsUnevaluated final /// The rule transformation auto transform(sourcemeta::jsontoolkit::JSON &, - sourcemeta::jsontoolkit::Value &value) const -> void override { + sourcemeta::jsontoolkit::JSON &value) const -> void override { sourcemeta::jsontoolkit::erase_many(value, this->BLACKLIST_UNEVALUATED); } diff --git a/src/canonicalizer/rules/drop_non_boolean_keywords_validation.h b/src/canonicalizer/rules/drop_non_boolean_keywords_validation.h index 840c93b4e..69e7ace36 100644 --- a/src/canonicalizer/rules/drop_non_boolean_keywords_validation.h +++ b/src/canonicalizer/rules/drop_non_boolean_keywords_validation.h @@ -30,18 +30,18 @@ namespace sourcemeta::jsonbinpack::canonicalizer { /// uniqueItems\}\f] class DropNonBooleanKeywordsValidation final - : public sourcemeta::alterschema::Rule { + : public sourcemeta::jsontoolkit::SchemaTransformRule { public: DropNonBooleanKeywordsValidation() - : Rule("drop_non_boolean_keywords_validation"){}; + : SchemaTransformRule("drop_non_boolean_keywords_validation"){}; /// The rule condition - [[nodiscard]] auto - condition(const sourcemeta::jsontoolkit::Value &schema, - const std::string &draft, - const std::unordered_map &vocabularies, - const std::size_t) const -> bool override { - return draft == "https://json-schema.org/draft/2020-12/schema" && + [[nodiscard]] auto condition(const sourcemeta::jsontoolkit::JSON &schema, + const std::string &dialect, + const std::set &vocabularies, + const sourcemeta::jsontoolkit::Pointer &) const + -> bool override { + return dialect == "https://json-schema.org/draft/2020-12/schema" && is_boolean_schema(schema, vocabularies) && vocabularies.contains( "https://json-schema.org/draft/2020-12/vocab/validation") && @@ -51,7 +51,7 @@ class DropNonBooleanKeywordsValidation final /// The rule transformation auto transform(sourcemeta::jsontoolkit::JSON &, - sourcemeta::jsontoolkit::Value &value) const -> void override { + sourcemeta::jsontoolkit::JSON &value) const -> void override { sourcemeta::jsontoolkit::erase_many(value, this->BLACKLIST_VALIDATION); } diff --git a/src/canonicalizer/rules/drop_non_null_keywords_applicator.h b/src/canonicalizer/rules/drop_non_null_keywords_applicator.h index d077203a1..67cf542c8 100644 --- a/src/canonicalizer/rules/drop_non_null_keywords_applicator.h +++ b/src/canonicalizer/rules/drop_non_null_keywords_applicator.h @@ -25,17 +25,18 @@ namespace sourcemeta::jsonbinpack::canonicalizer { /// \f[K_{array} = \{prefixItems, contains, items\}\f] class DropNonNullKeywordsApplicator final - : public sourcemeta::alterschema::Rule { + : public sourcemeta::jsontoolkit::SchemaTransformRule { public: - DropNonNullKeywordsApplicator() : Rule("drop_non_null_keywords_applicator"){}; + DropNonNullKeywordsApplicator() + : SchemaTransformRule("drop_non_null_keywords_applicator"){}; /// The rule condition - [[nodiscard]] auto - condition(const sourcemeta::jsontoolkit::Value &schema, - const std::string &draft, - const std::unordered_map &vocabularies, - const std::size_t) const -> bool override { - return draft == "https://json-schema.org/draft/2020-12/schema" && + [[nodiscard]] auto condition(const sourcemeta::jsontoolkit::JSON &schema, + const std::string &dialect, + const std::set &vocabularies, + const sourcemeta::jsontoolkit::Pointer &) const + -> bool override { + return dialect == "https://json-schema.org/draft/2020-12/schema" && is_null_schema(schema, vocabularies) && vocabularies.contains( "https://json-schema.org/draft/2020-12/vocab/applicator") && @@ -45,7 +46,7 @@ class DropNonNullKeywordsApplicator final /// The rule transformation auto transform(sourcemeta::jsontoolkit::JSON &, - sourcemeta::jsontoolkit::Value &value) const -> void override { + sourcemeta::jsontoolkit::JSON &value) const -> void override { sourcemeta::jsontoolkit::erase_many(value, this->BLACKLIST_APPLICATOR); } diff --git a/src/canonicalizer/rules/drop_non_null_keywords_content.h b/src/canonicalizer/rules/drop_non_null_keywords_content.h index 4c59f0983..9cf5de0cf 100644 --- a/src/canonicalizer/rules/drop_non_null_keywords_content.h +++ b/src/canonicalizer/rules/drop_non_null_keywords_content.h @@ -21,17 +21,19 @@ namespace sourcemeta::jsonbinpack::canonicalizer { /// /// \f[K = \{contentEncoding, contentMediaType, contentSchema\}\f] -class DropNonNullKeywordsContent final : public sourcemeta::alterschema::Rule { +class DropNonNullKeywordsContent final + : public sourcemeta::jsontoolkit::SchemaTransformRule { public: - DropNonNullKeywordsContent() : Rule("drop_non_null_keywords_content"){}; + DropNonNullKeywordsContent() + : SchemaTransformRule("drop_non_null_keywords_content"){}; /// The rule condition - [[nodiscard]] auto - condition(const sourcemeta::jsontoolkit::Value &schema, - const std::string &draft, - const std::unordered_map &vocabularies, - const std::size_t) const -> bool override { - return draft == "https://json-schema.org/draft/2020-12/schema" && + [[nodiscard]] auto condition(const sourcemeta::jsontoolkit::JSON &schema, + const std::string &dialect, + const std::set &vocabularies, + const sourcemeta::jsontoolkit::Pointer &) const + -> bool override { + return dialect == "https://json-schema.org/draft/2020-12/schema" && is_null_schema(schema, vocabularies) && vocabularies.contains( "https://json-schema.org/draft/2020-12/vocab/content") && @@ -41,7 +43,7 @@ class DropNonNullKeywordsContent final : public sourcemeta::alterschema::Rule { /// The rule transformation auto transform(sourcemeta::jsontoolkit::JSON &, - sourcemeta::jsontoolkit::Value &value) const -> void override { + sourcemeta::jsontoolkit::JSON &value) const -> void override { sourcemeta::jsontoolkit::erase_many(value, this->BLACKLIST_CONTENT); } diff --git a/src/canonicalizer/rules/drop_non_null_keywords_format.h b/src/canonicalizer/rules/drop_non_null_keywords_format.h index 6793fd425..0ea7e2f62 100644 --- a/src/canonicalizer/rules/drop_non_null_keywords_format.h +++ b/src/canonicalizer/rules/drop_non_null_keywords_format.h @@ -17,17 +17,19 @@ namespace sourcemeta::jsonbinpack::canonicalizer { /// \f[\frac{S.type = null \land format \in dom(S) }{S /// \mapsto S \setminus \{ format \} }\f] -class DropNonNullKeywordsFormat final : public sourcemeta::alterschema::Rule { +class DropNonNullKeywordsFormat final + : public sourcemeta::jsontoolkit::SchemaTransformRule { public: - DropNonNullKeywordsFormat() : Rule("drop_non_null_keywords_format"){}; + DropNonNullKeywordsFormat() + : SchemaTransformRule("drop_non_null_keywords_format"){}; /// The rule condition - [[nodiscard]] auto - condition(const sourcemeta::jsontoolkit::Value &schema, - const std::string &draft, - const std::unordered_map &vocabularies, - const std::size_t) const -> bool override { - return draft == "https://json-schema.org/draft/2020-12/schema" && + [[nodiscard]] auto condition(const sourcemeta::jsontoolkit::JSON &schema, + const std::string &dialect, + const std::set &vocabularies, + const sourcemeta::jsontoolkit::Pointer &) const + -> bool override { + return dialect == "https://json-schema.org/draft/2020-12/schema" && is_null_schema(schema, vocabularies) && (vocabularies.contains("https://json-schema.org/draft/2020-12/vocab/" "format-annotation") || @@ -38,7 +40,7 @@ class DropNonNullKeywordsFormat final : public sourcemeta::alterschema::Rule { /// The rule transformation auto transform(sourcemeta::jsontoolkit::JSON &, - sourcemeta::jsontoolkit::Value &value) const -> void override { + sourcemeta::jsontoolkit::JSON &value) const -> void override { sourcemeta::jsontoolkit::erase_many(value, this->BLACKLIST_FORMAT); } diff --git a/src/canonicalizer/rules/drop_non_null_keywords_unevaluated.h b/src/canonicalizer/rules/drop_non_null_keywords_unevaluated.h index b98cadd4d..8025ff0f1 100644 --- a/src/canonicalizer/rules/drop_non_null_keywords_unevaluated.h +++ b/src/canonicalizer/rules/drop_non_null_keywords_unevaluated.h @@ -22,18 +22,18 @@ namespace sourcemeta::jsonbinpack::canonicalizer { /// \f[K = \{ unevaluatedItems, unevaluatedProperties \}\f] class DropNonNullKeywordsUnevaluated final - : public sourcemeta::alterschema::Rule { + : public sourcemeta::jsontoolkit::SchemaTransformRule { public: DropNonNullKeywordsUnevaluated() - : Rule("drop_non_null_keywords_unevaluated"){}; + : SchemaTransformRule("drop_non_null_keywords_unevaluated"){}; /// The rule condition - [[nodiscard]] auto - condition(const sourcemeta::jsontoolkit::Value &schema, - const std::string &draft, - const std::unordered_map &vocabularies, - const std::size_t) const -> bool override { - return draft == "https://json-schema.org/draft/2020-12/schema" && + [[nodiscard]] auto condition(const sourcemeta::jsontoolkit::JSON &schema, + const std::string &dialect, + const std::set &vocabularies, + const sourcemeta::jsontoolkit::Pointer &) const + -> bool override { + return dialect == "https://json-schema.org/draft/2020-12/schema" && is_null_schema(schema, vocabularies) && vocabularies.contains( "https://json-schema.org/draft/2020-12/vocab/unevaluated") && @@ -43,7 +43,7 @@ class DropNonNullKeywordsUnevaluated final /// The rule transformation auto transform(sourcemeta::jsontoolkit::JSON &, - sourcemeta::jsontoolkit::Value &value) const -> void override { + sourcemeta::jsontoolkit::JSON &value) const -> void override { sourcemeta::jsontoolkit::erase_many(value, this->BLACKLIST_UNEVALUATED); } diff --git a/src/canonicalizer/rules/drop_non_null_keywords_validation.h b/src/canonicalizer/rules/drop_non_null_keywords_validation.h index 69a833389..d10a8e686 100644 --- a/src/canonicalizer/rules/drop_non_null_keywords_validation.h +++ b/src/canonicalizer/rules/drop_non_null_keywords_validation.h @@ -30,17 +30,18 @@ namespace sourcemeta::jsonbinpack::canonicalizer { /// uniqueItems\}\f] class DropNonNullKeywordsValidation final - : public sourcemeta::alterschema::Rule { + : public sourcemeta::jsontoolkit::SchemaTransformRule { public: - DropNonNullKeywordsValidation() : Rule("drop_non_null_keywords_validation"){}; + DropNonNullKeywordsValidation() + : SchemaTransformRule("drop_non_null_keywords_validation"){}; /// The rule condition - [[nodiscard]] auto - condition(const sourcemeta::jsontoolkit::Value &schema, - const std::string &draft, - const std::unordered_map &vocabularies, - const std::size_t) const -> bool override { - return draft == "https://json-schema.org/draft/2020-12/schema" && + [[nodiscard]] auto condition(const sourcemeta::jsontoolkit::JSON &schema, + const std::string &dialect, + const std::set &vocabularies, + const sourcemeta::jsontoolkit::Pointer &) const + -> bool override { + return dialect == "https://json-schema.org/draft/2020-12/schema" && is_null_schema(schema, vocabularies) && vocabularies.contains( "https://json-schema.org/draft/2020-12/vocab/validation") && @@ -50,7 +51,7 @@ class DropNonNullKeywordsValidation final /// The rule transformation auto transform(sourcemeta::jsontoolkit::JSON &, - sourcemeta::jsontoolkit::Value &value) const -> void override { + sourcemeta::jsontoolkit::JSON &value) const -> void override { sourcemeta::jsontoolkit::erase_many(value, this->BLACKLIST_VALIDATION); } diff --git a/src/canonicalizer/rules/drop_non_numeric_keywords_applicator.h b/src/canonicalizer/rules/drop_non_numeric_keywords_applicator.h index fdef765b3..f9e3eb588 100644 --- a/src/canonicalizer/rules/drop_non_numeric_keywords_applicator.h +++ b/src/canonicalizer/rules/drop_non_numeric_keywords_applicator.h @@ -26,27 +26,26 @@ namespace sourcemeta::jsonbinpack::canonicalizer { /// \f[K_{array} = \{prefixItems, contains, items\}\f] class DropNonNumericKeywordsApplicator final - : public sourcemeta::alterschema::Rule { + : public sourcemeta::jsontoolkit::SchemaTransformRule { public: DropNonNumericKeywordsApplicator() - : Rule("drop_non_numeric_keywords_applicator"){}; + : SchemaTransformRule("drop_non_numeric_keywords_applicator"){}; /// The rule condition - [[nodiscard]] auto - condition(const sourcemeta::jsontoolkit::Value &schema, - const std::string &draft, - const std::unordered_map &vocabularies, - const std::size_t) const -> bool override { - return draft == "https://json-schema.org/draft/2020-12/schema" && + [[nodiscard]] auto condition(const sourcemeta::jsontoolkit::JSON &schema, + const std::string &dialect, + const std::set &vocabularies, + const sourcemeta::jsontoolkit::Pointer &) const + -> bool override { + return dialect == "https://json-schema.org/draft/2020-12/schema" && vocabularies.contains( "https://json-schema.org/draft/2020-12/vocab/validation") && - sourcemeta::jsontoolkit::defines(schema, "type") && - sourcemeta::jsontoolkit::is_string( - sourcemeta::jsontoolkit::at(schema, "type")) && - (sourcemeta::jsontoolkit::to_string( - sourcemeta::jsontoolkit::at(schema, "type")) == "integer" || - sourcemeta::jsontoolkit::to_string( - sourcemeta::jsontoolkit::at(schema, "type")) == "number") && + schema.defines("type") && + sourcemeta::jsontoolkit::is_string(schema.at("type")) && + (sourcemeta::jsontoolkit::to_string(schema.at("type")) == + "integer" || + sourcemeta::jsontoolkit::to_string(schema.at("type")) == + "number") && vocabularies.contains( "https://json-schema.org/draft/2020-12/vocab/applicator") && sourcemeta::jsontoolkit::defines_any(schema, @@ -55,7 +54,7 @@ class DropNonNumericKeywordsApplicator final /// The rule transformation auto transform(sourcemeta::jsontoolkit::JSON &, - sourcemeta::jsontoolkit::Value &value) const -> void override { + sourcemeta::jsontoolkit::JSON &value) const -> void override { sourcemeta::jsontoolkit::erase_many(value, this->BLACKLIST_APPLICATOR); } diff --git a/src/canonicalizer/rules/drop_non_numeric_keywords_content.h b/src/canonicalizer/rules/drop_non_numeric_keywords_content.h index 0a022fd02..bd2c2d7e0 100644 --- a/src/canonicalizer/rules/drop_non_numeric_keywords_content.h +++ b/src/canonicalizer/rules/drop_non_numeric_keywords_content.h @@ -22,26 +22,26 @@ namespace sourcemeta::jsonbinpack::canonicalizer { /// \f[K = \{contentEncoding, contentMediaType, contentSchema\}\f] class DropNonNumericKeywordsContent final - : public sourcemeta::alterschema::Rule { + : public sourcemeta::jsontoolkit::SchemaTransformRule { public: - DropNonNumericKeywordsContent() : Rule("drop_non_numeric_keywords_content"){}; + DropNonNumericKeywordsContent() + : SchemaTransformRule("drop_non_numeric_keywords_content"){}; /// The rule condition - [[nodiscard]] auto - condition(const sourcemeta::jsontoolkit::Value &schema, - const std::string &draft, - const std::unordered_map &vocabularies, - const std::size_t) const -> bool override { - return draft == "https://json-schema.org/draft/2020-12/schema" && + [[nodiscard]] auto condition(const sourcemeta::jsontoolkit::JSON &schema, + const std::string &dialect, + const std::set &vocabularies, + const sourcemeta::jsontoolkit::Pointer &) const + -> bool override { + return dialect == "https://json-schema.org/draft/2020-12/schema" && vocabularies.contains( "https://json-schema.org/draft/2020-12/vocab/validation") && - sourcemeta::jsontoolkit::defines(schema, "type") && - sourcemeta::jsontoolkit::is_string( - sourcemeta::jsontoolkit::at(schema, "type")) && - (sourcemeta::jsontoolkit::to_string( - sourcemeta::jsontoolkit::at(schema, "type")) == "integer" || - sourcemeta::jsontoolkit::to_string( - sourcemeta::jsontoolkit::at(schema, "type")) == "number") && + schema.defines("type") && + sourcemeta::jsontoolkit::is_string(schema.at("type")) && + (sourcemeta::jsontoolkit::to_string(schema.at("type")) == + "integer" || + sourcemeta::jsontoolkit::to_string(schema.at("type")) == + "number") && vocabularies.contains( "https://json-schema.org/draft/2020-12/vocab/content") && sourcemeta::jsontoolkit::defines_any(schema, @@ -50,7 +50,7 @@ class DropNonNumericKeywordsContent final /// The rule transformation auto transform(sourcemeta::jsontoolkit::JSON &, - sourcemeta::jsontoolkit::Value &value) const -> void override { + sourcemeta::jsontoolkit::JSON &value) const -> void override { sourcemeta::jsontoolkit::erase_many(value, this->BLACKLIST_CONTENT); } diff --git a/src/canonicalizer/rules/drop_non_numeric_keywords_format.h b/src/canonicalizer/rules/drop_non_numeric_keywords_format.h index 0931c7330..6f8c7d3cf 100644 --- a/src/canonicalizer/rules/drop_non_numeric_keywords_format.h +++ b/src/canonicalizer/rules/drop_non_numeric_keywords_format.h @@ -19,26 +19,26 @@ namespace sourcemeta::jsonbinpack::canonicalizer { /// \mapsto S \setminus \{ format \} }\f] class DropNonNumericKeywordsFormat final - : public sourcemeta::alterschema::Rule { + : public sourcemeta::jsontoolkit::SchemaTransformRule { public: - DropNonNumericKeywordsFormat() : Rule("drop_non_numeric_keywords_format"){}; + DropNonNumericKeywordsFormat() + : SchemaTransformRule("drop_non_numeric_keywords_format"){}; /// The rule condition - [[nodiscard]] auto - condition(const sourcemeta::jsontoolkit::Value &schema, - const std::string &draft, - const std::unordered_map &vocabularies, - const std::size_t) const -> bool override { - return draft == "https://json-schema.org/draft/2020-12/schema" && + [[nodiscard]] auto condition(const sourcemeta::jsontoolkit::JSON &schema, + const std::string &dialect, + const std::set &vocabularies, + const sourcemeta::jsontoolkit::Pointer &) const + -> bool override { + return dialect == "https://json-schema.org/draft/2020-12/schema" && vocabularies.contains( "https://json-schema.org/draft/2020-12/vocab/validation") && - sourcemeta::jsontoolkit::defines(schema, "type") && - sourcemeta::jsontoolkit::is_string( - sourcemeta::jsontoolkit::at(schema, "type")) && - (sourcemeta::jsontoolkit::to_string( - sourcemeta::jsontoolkit::at(schema, "type")) == "integer" || - sourcemeta::jsontoolkit::to_string( - sourcemeta::jsontoolkit::at(schema, "type")) == "number") && + schema.defines("type") && + sourcemeta::jsontoolkit::is_string(schema.at("type")) && + (sourcemeta::jsontoolkit::to_string(schema.at("type")) == + "integer" || + sourcemeta::jsontoolkit::to_string(schema.at("type")) == + "number") && (vocabularies.contains("https://json-schema.org/draft/2020-12/vocab/" "format-annotation") || vocabularies.contains("https://json-schema.org/draft/2020-12/vocab/" @@ -48,7 +48,7 @@ class DropNonNumericKeywordsFormat final /// The rule transformation auto transform(sourcemeta::jsontoolkit::JSON &, - sourcemeta::jsontoolkit::Value &value) const -> void override { + sourcemeta::jsontoolkit::JSON &value) const -> void override { sourcemeta::jsontoolkit::erase_many(value, this->BLACKLIST_FORMAT); } diff --git a/src/canonicalizer/rules/drop_non_numeric_keywords_unevaluated.h b/src/canonicalizer/rules/drop_non_numeric_keywords_unevaluated.h index f360e9707..5fedf1bd9 100644 --- a/src/canonicalizer/rules/drop_non_numeric_keywords_unevaluated.h +++ b/src/canonicalizer/rules/drop_non_numeric_keywords_unevaluated.h @@ -22,27 +22,26 @@ namespace sourcemeta::jsonbinpack::canonicalizer { /// \f[K = \{ unevaluatedItems, unevaluatedProperties \}\f] class DropNonNumericKeywordsUnevaluated final - : public sourcemeta::alterschema::Rule { + : public sourcemeta::jsontoolkit::SchemaTransformRule { public: DropNonNumericKeywordsUnevaluated() - : Rule("drop_non_numeric_keywords_unevaluated"){}; + : SchemaTransformRule("drop_non_numeric_keywords_unevaluated"){}; /// The rule condition - [[nodiscard]] auto - condition(const sourcemeta::jsontoolkit::Value &schema, - const std::string &draft, - const std::unordered_map &vocabularies, - const std::size_t) const -> bool override { - return draft == "https://json-schema.org/draft/2020-12/schema" && + [[nodiscard]] auto condition(const sourcemeta::jsontoolkit::JSON &schema, + const std::string &dialect, + const std::set &vocabularies, + const sourcemeta::jsontoolkit::Pointer &) const + -> bool override { + return dialect == "https://json-schema.org/draft/2020-12/schema" && vocabularies.contains( "https://json-schema.org/draft/2020-12/vocab/validation") && - sourcemeta::jsontoolkit::defines(schema, "type") && - sourcemeta::jsontoolkit::is_string( - sourcemeta::jsontoolkit::at(schema, "type")) && - (sourcemeta::jsontoolkit::to_string( - sourcemeta::jsontoolkit::at(schema, "type")) == "integer" || - sourcemeta::jsontoolkit::to_string( - sourcemeta::jsontoolkit::at(schema, "type")) == "number") && + schema.defines("type") && + sourcemeta::jsontoolkit::is_string(schema.at("type")) && + (sourcemeta::jsontoolkit::to_string(schema.at("type")) == + "integer" || + sourcemeta::jsontoolkit::to_string(schema.at("type")) == + "number") && vocabularies.contains( "https://json-schema.org/draft/2020-12/vocab/unevaluated") && sourcemeta::jsontoolkit::defines_any(schema, @@ -51,7 +50,7 @@ class DropNonNumericKeywordsUnevaluated final /// The rule transformation auto transform(sourcemeta::jsontoolkit::JSON &, - sourcemeta::jsontoolkit::Value &value) const -> void override { + sourcemeta::jsontoolkit::JSON &value) const -> void override { sourcemeta::jsontoolkit::erase_many(value, this->BLACKLIST_UNEVALUATED); } diff --git a/src/canonicalizer/rules/drop_non_numeric_keywords_validation.h b/src/canonicalizer/rules/drop_non_numeric_keywords_validation.h index e52e1637d..98b2cfd5d 100644 --- a/src/canonicalizer/rules/drop_non_numeric_keywords_validation.h +++ b/src/canonicalizer/rules/drop_non_numeric_keywords_validation.h @@ -27,34 +27,33 @@ namespace sourcemeta::jsonbinpack::canonicalizer { /// uniqueItems\}\f] class DropNonNumericKeywordsValidation final - : public sourcemeta::alterschema::Rule { + : public sourcemeta::jsontoolkit::SchemaTransformRule { public: DropNonNumericKeywordsValidation() - : Rule("drop_non_numeric_keywords_validation"){}; + : SchemaTransformRule("drop_non_numeric_keywords_validation"){}; /// The rule condition - [[nodiscard]] auto - condition(const sourcemeta::jsontoolkit::Value &schema, - const std::string &draft, - const std::unordered_map &vocabularies, - const std::size_t) const -> bool override { - return draft == "https://json-schema.org/draft/2020-12/schema" && + [[nodiscard]] auto condition(const sourcemeta::jsontoolkit::JSON &schema, + const std::string &dialect, + const std::set &vocabularies, + const sourcemeta::jsontoolkit::Pointer &) const + -> bool override { + return dialect == "https://json-schema.org/draft/2020-12/schema" && vocabularies.contains( "https://json-schema.org/draft/2020-12/vocab/validation") && - sourcemeta::jsontoolkit::defines(schema, "type") && - sourcemeta::jsontoolkit::is_string( - sourcemeta::jsontoolkit::at(schema, "type")) && - (sourcemeta::jsontoolkit::to_string( - sourcemeta::jsontoolkit::at(schema, "type")) == "integer" || - sourcemeta::jsontoolkit::to_string( - sourcemeta::jsontoolkit::at(schema, "type")) == "number") && + schema.defines("type") && + sourcemeta::jsontoolkit::is_string(schema.at("type")) && + (sourcemeta::jsontoolkit::to_string(schema.at("type")) == + "integer" || + sourcemeta::jsontoolkit::to_string(schema.at("type")) == + "number") && sourcemeta::jsontoolkit::defines_any(schema, this->BLACKLIST_VALIDATION); } /// The rule transformation auto transform(sourcemeta::jsontoolkit::JSON &, - sourcemeta::jsontoolkit::Value &value) const -> void override { + sourcemeta::jsontoolkit::JSON &value) const -> void override { sourcemeta::jsontoolkit::erase_many(value, this->BLACKLIST_VALIDATION); } diff --git a/src/canonicalizer/rules/drop_non_object_keywords_applicator.h b/src/canonicalizer/rules/drop_non_object_keywords_applicator.h index d6c371a44..433f7e931 100644 --- a/src/canonicalizer/rules/drop_non_object_keywords_applicator.h +++ b/src/canonicalizer/rules/drop_non_object_keywords_applicator.h @@ -22,25 +22,23 @@ namespace sourcemeta::jsonbinpack::canonicalizer { /// \f[K = \{ prefixItems, contains, items \}\f] class DropNonObjectKeywordsApplicator final - : public sourcemeta::alterschema::Rule { + : public sourcemeta::jsontoolkit::SchemaTransformRule { public: DropNonObjectKeywordsApplicator() - : Rule("drop_non_object_keywords_applicator"){}; + : SchemaTransformRule("drop_non_object_keywords_applicator"){}; /// The rule condition - [[nodiscard]] auto - condition(const sourcemeta::jsontoolkit::Value &schema, - const std::string &draft, - const std::unordered_map &vocabularies, - const std::size_t) const -> bool override { - return draft == "https://json-schema.org/draft/2020-12/schema" && + [[nodiscard]] auto condition(const sourcemeta::jsontoolkit::JSON &schema, + const std::string &dialect, + const std::set &vocabularies, + const sourcemeta::jsontoolkit::Pointer &) const + -> bool override { + return dialect == "https://json-schema.org/draft/2020-12/schema" && vocabularies.contains( "https://json-schema.org/draft/2020-12/vocab/validation") && - sourcemeta::jsontoolkit::defines(schema, "type") && - sourcemeta::jsontoolkit::is_string( - sourcemeta::jsontoolkit::at(schema, "type")) && - sourcemeta::jsontoolkit::to_string( - sourcemeta::jsontoolkit::at(schema, "type")) == "object" && + schema.defines("type") && + sourcemeta::jsontoolkit::is_string(schema.at("type")) && + sourcemeta::jsontoolkit::to_string(schema.at("type")) == "object" && vocabularies.contains( "https://json-schema.org/draft/2020-12/vocab/applicator") && sourcemeta::jsontoolkit::defines_any(schema, @@ -49,7 +47,7 @@ class DropNonObjectKeywordsApplicator final /// The rule transformation auto transform(sourcemeta::jsontoolkit::JSON &, - sourcemeta::jsontoolkit::Value &value) const -> void override { + sourcemeta::jsontoolkit::JSON &value) const -> void override { sourcemeta::jsontoolkit::erase_many(value, this->BLACKLIST_APPLICATOR); } diff --git a/src/canonicalizer/rules/drop_non_object_keywords_content.h b/src/canonicalizer/rules/drop_non_object_keywords_content.h index 1207f962b..52cf832ce 100644 --- a/src/canonicalizer/rules/drop_non_object_keywords_content.h +++ b/src/canonicalizer/rules/drop_non_object_keywords_content.h @@ -22,24 +22,23 @@ namespace sourcemeta::jsonbinpack::canonicalizer { /// \f[K = \{contentEncoding, contentMediaType, contentSchema\}\f] class DropNonObjectKeywordsContent final - : public sourcemeta::alterschema::Rule { + : public sourcemeta::jsontoolkit::SchemaTransformRule { public: - DropNonObjectKeywordsContent() : Rule("drop_non_object_keywords_content"){}; + DropNonObjectKeywordsContent() + : SchemaTransformRule("drop_non_object_keywords_content"){}; /// The rule condition - [[nodiscard]] auto - condition(const sourcemeta::jsontoolkit::Value &schema, - const std::string &draft, - const std::unordered_map &vocabularies, - const std::size_t) const -> bool override { - return draft == "https://json-schema.org/draft/2020-12/schema" && + [[nodiscard]] auto condition(const sourcemeta::jsontoolkit::JSON &schema, + const std::string &dialect, + const std::set &vocabularies, + const sourcemeta::jsontoolkit::Pointer &) const + -> bool override { + return dialect == "https://json-schema.org/draft/2020-12/schema" && vocabularies.contains( "https://json-schema.org/draft/2020-12/vocab/validation") && - sourcemeta::jsontoolkit::defines(schema, "type") && - sourcemeta::jsontoolkit::is_string( - sourcemeta::jsontoolkit::at(schema, "type")) && - sourcemeta::jsontoolkit::to_string( - sourcemeta::jsontoolkit::at(schema, "type")) == "object" && + schema.defines("type") && + sourcemeta::jsontoolkit::is_string(schema.at("type")) && + sourcemeta::jsontoolkit::to_string(schema.at("type")) == "object" && vocabularies.contains( "https://json-schema.org/draft/2020-12/vocab/content") && sourcemeta::jsontoolkit::defines_any(schema, @@ -48,7 +47,7 @@ class DropNonObjectKeywordsContent final /// The rule transformation auto transform(sourcemeta::jsontoolkit::JSON &, - sourcemeta::jsontoolkit::Value &value) const -> void override { + sourcemeta::jsontoolkit::JSON &value) const -> void override { sourcemeta::jsontoolkit::erase_many(value, this->BLACKLIST_CONTENT); } diff --git a/src/canonicalizer/rules/drop_non_object_keywords_format.h b/src/canonicalizer/rules/drop_non_object_keywords_format.h index 9995cd0b5..900f7e980 100644 --- a/src/canonicalizer/rules/drop_non_object_keywords_format.h +++ b/src/canonicalizer/rules/drop_non_object_keywords_format.h @@ -17,24 +17,24 @@ namespace sourcemeta::jsonbinpack::canonicalizer { /// \f[\frac{S.type = object \land format \in dom(S) }{S /// \mapsto S \setminus \{ format \} }\f] -class DropNonObjectKeywordsFormat final : public sourcemeta::alterschema::Rule { +class DropNonObjectKeywordsFormat final + : public sourcemeta::jsontoolkit::SchemaTransformRule { public: - DropNonObjectKeywordsFormat() : Rule("drop_non_object_keywords_format"){}; + DropNonObjectKeywordsFormat() + : SchemaTransformRule("drop_non_object_keywords_format"){}; /// The rule condition - [[nodiscard]] auto - condition(const sourcemeta::jsontoolkit::Value &schema, - const std::string &draft, - const std::unordered_map &vocabularies, - const std::size_t) const -> bool override { - return draft == "https://json-schema.org/draft/2020-12/schema" && + [[nodiscard]] auto condition(const sourcemeta::jsontoolkit::JSON &schema, + const std::string &dialect, + const std::set &vocabularies, + const sourcemeta::jsontoolkit::Pointer &) const + -> bool override { + return dialect == "https://json-schema.org/draft/2020-12/schema" && vocabularies.contains( "https://json-schema.org/draft/2020-12/vocab/validation") && - sourcemeta::jsontoolkit::defines(schema, "type") && - sourcemeta::jsontoolkit::is_string( - sourcemeta::jsontoolkit::at(schema, "type")) && - sourcemeta::jsontoolkit::to_string( - sourcemeta::jsontoolkit::at(schema, "type")) == "object" && + schema.defines("type") && + sourcemeta::jsontoolkit::is_string(schema.at("type")) && + sourcemeta::jsontoolkit::to_string(schema.at("type")) == "object" && (vocabularies.contains("https://json-schema.org/draft/2020-12/vocab/" "format-annotation") || vocabularies.contains("https://json-schema.org/draft/2020-12/vocab/" @@ -44,7 +44,7 @@ class DropNonObjectKeywordsFormat final : public sourcemeta::alterschema::Rule { /// The rule transformation auto transform(sourcemeta::jsontoolkit::JSON &, - sourcemeta::jsontoolkit::Value &value) const -> void override { + sourcemeta::jsontoolkit::JSON &value) const -> void override { sourcemeta::jsontoolkit::erase_many(value, this->BLACKLIST_FORMAT); } diff --git a/src/canonicalizer/rules/drop_non_object_keywords_unevaluated.h b/src/canonicalizer/rules/drop_non_object_keywords_unevaluated.h index 7bcd4c814..89e0bdd6f 100644 --- a/src/canonicalizer/rules/drop_non_object_keywords_unevaluated.h +++ b/src/canonicalizer/rules/drop_non_object_keywords_unevaluated.h @@ -18,25 +18,23 @@ namespace sourcemeta::jsonbinpack::canonicalizer { /// \mapsto S \setminus \{ unevaluatedItems \} }\f] class DropNonObjectKeywordsUnevaluated final - : public sourcemeta::alterschema::Rule { + : public sourcemeta::jsontoolkit::SchemaTransformRule { public: DropNonObjectKeywordsUnevaluated() - : Rule("drop_non_object_keywords_unevaluated"){}; + : SchemaTransformRule("drop_non_object_keywords_unevaluated"){}; /// The rule condition - [[nodiscard]] auto - condition(const sourcemeta::jsontoolkit::Value &schema, - const std::string &draft, - const std::unordered_map &vocabularies, - const std::size_t) const -> bool override { - return draft == "https://json-schema.org/draft/2020-12/schema" && + [[nodiscard]] auto condition(const sourcemeta::jsontoolkit::JSON &schema, + const std::string &dialect, + const std::set &vocabularies, + const sourcemeta::jsontoolkit::Pointer &) const + -> bool override { + return dialect == "https://json-schema.org/draft/2020-12/schema" && vocabularies.contains( "https://json-schema.org/draft/2020-12/vocab/validation") && - sourcemeta::jsontoolkit::defines(schema, "type") && - sourcemeta::jsontoolkit::is_string( - sourcemeta::jsontoolkit::at(schema, "type")) && - sourcemeta::jsontoolkit::to_string( - sourcemeta::jsontoolkit::at(schema, "type")) == "object" && + schema.defines("type") && + sourcemeta::jsontoolkit::is_string(schema.at("type")) && + sourcemeta::jsontoolkit::to_string(schema.at("type")) == "object" && vocabularies.contains( "https://json-schema.org/draft/2020-12/vocab/unevaluated") && sourcemeta::jsontoolkit::defines_any(schema, @@ -45,7 +43,7 @@ class DropNonObjectKeywordsUnevaluated final /// The rule transformation auto transform(sourcemeta::jsontoolkit::JSON &, - sourcemeta::jsontoolkit::Value &value) const -> void override { + sourcemeta::jsontoolkit::JSON &value) const -> void override { sourcemeta::jsontoolkit::erase_many(value, this->BLACKLIST_UNEVALUATED); } diff --git a/src/canonicalizer/rules/drop_non_object_keywords_validation.h b/src/canonicalizer/rules/drop_non_object_keywords_validation.h index 69ce18055..6ea307a71 100644 --- a/src/canonicalizer/rules/drop_non_object_keywords_validation.h +++ b/src/canonicalizer/rules/drop_non_object_keywords_validation.h @@ -27,32 +27,30 @@ namespace sourcemeta::jsonbinpack::canonicalizer { /// uniqueItems\}\f] class DropNonObjectKeywordsValidation final - : public sourcemeta::alterschema::Rule { + : public sourcemeta::jsontoolkit::SchemaTransformRule { public: DropNonObjectKeywordsValidation() - : Rule("drop_non_object_keywords_validation"){}; + : SchemaTransformRule("drop_non_object_keywords_validation"){}; /// The rule condition - [[nodiscard]] auto - condition(const sourcemeta::jsontoolkit::Value &schema, - const std::string &draft, - const std::unordered_map &vocabularies, - const std::size_t) const -> bool override { - return draft == "https://json-schema.org/draft/2020-12/schema" && + [[nodiscard]] auto condition(const sourcemeta::jsontoolkit::JSON &schema, + const std::string &dialect, + const std::set &vocabularies, + const sourcemeta::jsontoolkit::Pointer &) const + -> bool override { + return dialect == "https://json-schema.org/draft/2020-12/schema" && vocabularies.contains( "https://json-schema.org/draft/2020-12/vocab/validation") && - sourcemeta::jsontoolkit::defines(schema, "type") && - sourcemeta::jsontoolkit::is_string( - sourcemeta::jsontoolkit::at(schema, "type")) && - sourcemeta::jsontoolkit::to_string( - sourcemeta::jsontoolkit::at(schema, "type")) == "object" && + schema.defines("type") && + sourcemeta::jsontoolkit::is_string(schema.at("type")) && + sourcemeta::jsontoolkit::to_string(schema.at("type")) == "object" && sourcemeta::jsontoolkit::defines_any(schema, this->BLACKLIST_VALIDATION); } /// The rule transformation auto transform(sourcemeta::jsontoolkit::JSON &, - sourcemeta::jsontoolkit::Value &value) const -> void override { + sourcemeta::jsontoolkit::JSON &value) const -> void override { sourcemeta::jsontoolkit::erase_many(value, this->BLACKLIST_VALIDATION); } diff --git a/src/canonicalizer/rules/drop_non_string_keywords_applicator.h b/src/canonicalizer/rules/drop_non_string_keywords_applicator.h index adbb5dc7f..6b9eb14f4 100644 --- a/src/canonicalizer/rules/drop_non_string_keywords_applicator.h +++ b/src/canonicalizer/rules/drop_non_string_keywords_applicator.h @@ -25,25 +25,23 @@ namespace sourcemeta::jsonbinpack::canonicalizer { /// \f[K_{array} = \{prefixItems, contains, items\}\f] class DropNonStringKeywordsApplicator final - : public sourcemeta::alterschema::Rule { + : public sourcemeta::jsontoolkit::SchemaTransformRule { public: DropNonStringKeywordsApplicator() - : Rule("drop_non_string_keywords_applicator"){}; + : SchemaTransformRule("drop_non_string_keywords_applicator"){}; /// The rule condition - [[nodiscard]] auto - condition(const sourcemeta::jsontoolkit::Value &schema, - const std::string &draft, - const std::unordered_map &vocabularies, - const std::size_t) const -> bool override { - return draft == "https://json-schema.org/draft/2020-12/schema" && + [[nodiscard]] auto condition(const sourcemeta::jsontoolkit::JSON &schema, + const std::string &dialect, + const std::set &vocabularies, + const sourcemeta::jsontoolkit::Pointer &) const + -> bool override { + return dialect == "https://json-schema.org/draft/2020-12/schema" && vocabularies.contains( "https://json-schema.org/draft/2020-12/vocab/validation") && - sourcemeta::jsontoolkit::defines(schema, "type") && - sourcemeta::jsontoolkit::is_string( - sourcemeta::jsontoolkit::at(schema, "type")) && - sourcemeta::jsontoolkit::to_string( - sourcemeta::jsontoolkit::at(schema, "type")) == "string" && + schema.defines("type") && + sourcemeta::jsontoolkit::is_string(schema.at("type")) && + sourcemeta::jsontoolkit::to_string(schema.at("type")) == "string" && vocabularies.contains( "https://json-schema.org/draft/2020-12/vocab/applicator") && sourcemeta::jsontoolkit::defines_any(schema, @@ -52,7 +50,7 @@ class DropNonStringKeywordsApplicator final /// The rule transformation auto transform(sourcemeta::jsontoolkit::JSON &, - sourcemeta::jsontoolkit::Value &value) const -> void override { + sourcemeta::jsontoolkit::JSON &value) const -> void override { sourcemeta::jsontoolkit::erase_many(value, this->BLACKLIST_APPLICATOR); } diff --git a/src/canonicalizer/rules/drop_non_string_keywords_unevaluated.h b/src/canonicalizer/rules/drop_non_string_keywords_unevaluated.h index 81dc8e751..3d0c74ccd 100644 --- a/src/canonicalizer/rules/drop_non_string_keywords_unevaluated.h +++ b/src/canonicalizer/rules/drop_non_string_keywords_unevaluated.h @@ -22,25 +22,23 @@ namespace sourcemeta::jsonbinpack::canonicalizer { /// \f[K = \{ unevaluatedItems, unevaluatedProperties \}\f] class DropNonStringKeywordsUnevaluated final - : public sourcemeta::alterschema::Rule { + : public sourcemeta::jsontoolkit::SchemaTransformRule { public: DropNonStringKeywordsUnevaluated() - : Rule("drop_non_string_keywords_unevaluated"){}; + : SchemaTransformRule("drop_non_string_keywords_unevaluated"){}; /// The rule condition - [[nodiscard]] auto - condition(const sourcemeta::jsontoolkit::Value &schema, - const std::string &draft, - const std::unordered_map &vocabularies, - const std::size_t) const -> bool override { - return draft == "https://json-schema.org/draft/2020-12/schema" && + [[nodiscard]] auto condition(const sourcemeta::jsontoolkit::JSON &schema, + const std::string &dialect, + const std::set &vocabularies, + const sourcemeta::jsontoolkit::Pointer &) const + -> bool override { + return dialect == "https://json-schema.org/draft/2020-12/schema" && vocabularies.contains( "https://json-schema.org/draft/2020-12/vocab/validation") && - sourcemeta::jsontoolkit::defines(schema, "type") && - sourcemeta::jsontoolkit::is_string( - sourcemeta::jsontoolkit::at(schema, "type")) && - sourcemeta::jsontoolkit::to_string( - sourcemeta::jsontoolkit::at(schema, "type")) == "string" && + schema.defines("type") && + sourcemeta::jsontoolkit::is_string(schema.at("type")) && + sourcemeta::jsontoolkit::to_string(schema.at("type")) == "string" && vocabularies.contains( "https://json-schema.org/draft/2020-12/vocab/unevaluated") && sourcemeta::jsontoolkit::defines_any(schema, @@ -49,7 +47,7 @@ class DropNonStringKeywordsUnevaluated final /// The rule transformation auto transform(sourcemeta::jsontoolkit::JSON &, - sourcemeta::jsontoolkit::Value &value) const -> void override { + sourcemeta::jsontoolkit::JSON &value) const -> void override { sourcemeta::jsontoolkit::erase_many(value, this->BLACKLIST_UNEVALUATED); } diff --git a/src/canonicalizer/rules/drop_non_string_keywords_validation.h b/src/canonicalizer/rules/drop_non_string_keywords_validation.h index 6e73c7b57..2e6e15705 100644 --- a/src/canonicalizer/rules/drop_non_string_keywords_validation.h +++ b/src/canonicalizer/rules/drop_non_string_keywords_validation.h @@ -28,32 +28,30 @@ namespace sourcemeta::jsonbinpack::canonicalizer { /// uniqueItems\}\f] class DropNonStringKeywordsValidation final - : public sourcemeta::alterschema::Rule { + : public sourcemeta::jsontoolkit::SchemaTransformRule { public: DropNonStringKeywordsValidation() - : Rule("drop_non_string_keywords_validation"){}; + : SchemaTransformRule("drop_non_string_keywords_validation"){}; /// The rule condition - [[nodiscard]] auto - condition(const sourcemeta::jsontoolkit::Value &schema, - const std::string &draft, - const std::unordered_map &vocabularies, - const std::size_t) const -> bool override { - return draft == "https://json-schema.org/draft/2020-12/schema" && + [[nodiscard]] auto condition(const sourcemeta::jsontoolkit::JSON &schema, + const std::string &dialect, + const std::set &vocabularies, + const sourcemeta::jsontoolkit::Pointer &) const + -> bool override { + return dialect == "https://json-schema.org/draft/2020-12/schema" && vocabularies.contains( "https://json-schema.org/draft/2020-12/vocab/validation") && - sourcemeta::jsontoolkit::defines(schema, "type") && - sourcemeta::jsontoolkit::is_string( - sourcemeta::jsontoolkit::at(schema, "type")) && - sourcemeta::jsontoolkit::to_string( - sourcemeta::jsontoolkit::at(schema, "type")) == "string" && + schema.defines("type") && + sourcemeta::jsontoolkit::is_string(schema.at("type")) && + sourcemeta::jsontoolkit::to_string(schema.at("type")) == "string" && sourcemeta::jsontoolkit::defines_any(schema, this->BLACKLIST_VALIDATION); } /// The rule transformation auto transform(sourcemeta::jsontoolkit::JSON &, - sourcemeta::jsontoolkit::Value &value) const -> void override { + sourcemeta::jsontoolkit::JSON &value) const -> void override { sourcemeta::jsontoolkit::erase_many(value, this->BLACKLIST_VALIDATION); } diff --git a/src/canonicalizer/rules/duplicate_allof_branches.h b/src/canonicalizer/rules/duplicate_allof_branches.h index 7cc7430fd..618c2d9da 100644 --- a/src/canonicalizer/rules/duplicate_allof_branches.h +++ b/src/canonicalizer/rules/duplicate_allof_branches.h @@ -15,29 +15,28 @@ namespace sourcemeta::jsonbinpack::canonicalizer { /// \f[\frac{allOf \in dom(S) \land \#S.allOf \neq \#\{ x \mid S.allOf \}}{S /// \mapsto S \cup \{ allOf \mapsto seq \; \{ x \mid S.allOf \} \} }\f] -class DuplicateAllOfBranches final : public sourcemeta::alterschema::Rule { +class DuplicateAllOfBranches final + : public sourcemeta::jsontoolkit::SchemaTransformRule { public: - DuplicateAllOfBranches() : Rule("duplicate_allof_branches"){}; + DuplicateAllOfBranches() : SchemaTransformRule("duplicate_allof_branches"){}; /// The rule condition - [[nodiscard]] auto - condition(const sourcemeta::jsontoolkit::Value &schema, - const std::string &draft, - const std::unordered_map &vocabularies, - const std::size_t) const -> bool override { - return draft == "https://json-schema.org/draft/2020-12/schema" && + [[nodiscard]] auto condition(const sourcemeta::jsontoolkit::JSON &schema, + const std::string &dialect, + const std::set &vocabularies, + const sourcemeta::jsontoolkit::Pointer &) const + -> bool override { + return dialect == "https://json-schema.org/draft/2020-12/schema" && vocabularies.contains( "https://json-schema.org/draft/2020-12/vocab/applicator") && - sourcemeta::jsontoolkit::is_object(schema) && - sourcemeta::jsontoolkit::defines(schema, "allOf") && - sourcemeta::jsontoolkit::is_array( - sourcemeta::jsontoolkit::at(schema, "allOf")) && - !is_unique(sourcemeta::jsontoolkit::at(schema, "allOf")); + schema.is_object() && schema.defines("allOf") && + sourcemeta::jsontoolkit::is_array(schema.at("allOf")) && + !is_unique(schema.at("allOf")); } /// The rule transformation auto transform(sourcemeta::jsontoolkit::JSON &, - sourcemeta::jsontoolkit::Value &value) const -> void override { + sourcemeta::jsontoolkit::JSON &value) const -> void override { auto &collection{sourcemeta::jsontoolkit::at(value, "allOf")}; std::sort(sourcemeta::jsontoolkit::begin_array(collection), sourcemeta::jsontoolkit::end_array(collection), diff --git a/src/canonicalizer/rules/duplicate_anyof_branches.h b/src/canonicalizer/rules/duplicate_anyof_branches.h index 8d507bd25..a21ee300b 100644 --- a/src/canonicalizer/rules/duplicate_anyof_branches.h +++ b/src/canonicalizer/rules/duplicate_anyof_branches.h @@ -15,29 +15,28 @@ namespace sourcemeta::jsonbinpack::canonicalizer { /// \f[\frac{anyOf \in dom(S) \land \#S.anyOf \neq \#\{ x \mid S.anyOf \}}{S /// \mapsto S \cup \{ anyOf \mapsto seq \; \{ x \mid S.anyOf \} \} }\f] -class DuplicateAnyOfBranches final : public sourcemeta::alterschema::Rule { +class DuplicateAnyOfBranches final + : public sourcemeta::jsontoolkit::SchemaTransformRule { public: - DuplicateAnyOfBranches() : Rule("duplicate_anyof_branches"){}; + DuplicateAnyOfBranches() : SchemaTransformRule("duplicate_anyof_branches"){}; /// The rule condition - [[nodiscard]] auto - condition(const sourcemeta::jsontoolkit::Value &schema, - const std::string &draft, - const std::unordered_map &vocabularies, - const std::size_t) const -> bool override { - return draft == "https://json-schema.org/draft/2020-12/schema" && + [[nodiscard]] auto condition(const sourcemeta::jsontoolkit::JSON &schema, + const std::string &dialect, + const std::set &vocabularies, + const sourcemeta::jsontoolkit::Pointer &) const + -> bool override { + return dialect == "https://json-schema.org/draft/2020-12/schema" && vocabularies.contains( "https://json-schema.org/draft/2020-12/vocab/applicator") && - sourcemeta::jsontoolkit::is_object(schema) && - sourcemeta::jsontoolkit::defines(schema, "anyOf") && - sourcemeta::jsontoolkit::is_array( - sourcemeta::jsontoolkit::at(schema, "anyOf")) && - !is_unique(sourcemeta::jsontoolkit::at(schema, "anyOf")); + schema.is_object() && schema.defines("anyOf") && + sourcemeta::jsontoolkit::is_array(schema.at("anyOf")) && + !is_unique(schema.at("anyOf")); } /// The rule transformation auto transform(sourcemeta::jsontoolkit::JSON &, - sourcemeta::jsontoolkit::Value &value) const -> void override { + sourcemeta::jsontoolkit::JSON &value) const -> void override { auto &collection{sourcemeta::jsontoolkit::at(value, "anyOf")}; std::sort(sourcemeta::jsontoolkit::begin_array(collection), sourcemeta::jsontoolkit::end_array(collection), diff --git a/src/canonicalizer/rules/duplicate_enum_values.h b/src/canonicalizer/rules/duplicate_enum_values.h index c7855a6ad..a6e8942f6 100644 --- a/src/canonicalizer/rules/duplicate_enum_values.h +++ b/src/canonicalizer/rules/duplicate_enum_values.h @@ -15,29 +15,28 @@ namespace sourcemeta::jsonbinpack::canonicalizer { /// \f[\frac{enum \in dom(S) \land \#S.enum \neq \#\{ x \mid S.enum \}}{S /// \mapsto S \cup \{ enum \mapsto seq \; \{ x \mid S.enum \} \} }\f] -class DuplicateEnumValues final : public sourcemeta::alterschema::Rule { +class DuplicateEnumJSONs final + : public sourcemeta::jsontoolkit::SchemaTransformRule { public: - DuplicateEnumValues() : Rule("duplicate_enum_values"){}; + DuplicateEnumJSONs() : SchemaTransformRule("duplicate_enum_values"){}; /// The rule condition - [[nodiscard]] auto - condition(const sourcemeta::jsontoolkit::Value &schema, - const std::string &draft, - const std::unordered_map &vocabularies, - const std::size_t) const -> bool override { - return draft == "https://json-schema.org/draft/2020-12/schema" && + [[nodiscard]] auto condition(const sourcemeta::jsontoolkit::JSON &schema, + const std::string &dialect, + const std::set &vocabularies, + const sourcemeta::jsontoolkit::Pointer &) const + -> bool override { + return dialect == "https://json-schema.org/draft/2020-12/schema" && vocabularies.contains( "https://json-schema.org/draft/2020-12/vocab/applicator") && - sourcemeta::jsontoolkit::is_object(schema) && - sourcemeta::jsontoolkit::defines(schema, "enum") && - sourcemeta::jsontoolkit::is_array( - sourcemeta::jsontoolkit::at(schema, "enum")) && - !is_unique(sourcemeta::jsontoolkit::at(schema, "enum")); + schema.is_object() && schema.defines("enum") && + sourcemeta::jsontoolkit::is_array(schema.at("enum")) && + !is_unique(schema.at("enum")); } /// The rule transformation auto transform(sourcemeta::jsontoolkit::JSON &, - sourcemeta::jsontoolkit::Value &value) const -> void override { + sourcemeta::jsontoolkit::JSON &value) const -> void override { auto &collection{sourcemeta::jsontoolkit::at(value, "enum")}; std::sort(sourcemeta::jsontoolkit::begin_array(collection), sourcemeta::jsontoolkit::end_array(collection), diff --git a/src/canonicalizer/rules/duplicate_required_values.h b/src/canonicalizer/rules/duplicate_required_values.h index 31169e02b..0f52a8bc0 100644 --- a/src/canonicalizer/rules/duplicate_required_values.h +++ b/src/canonicalizer/rules/duplicate_required_values.h @@ -16,29 +16,28 @@ namespace sourcemeta::jsonbinpack::canonicalizer { /// \}}{S \mapsto S \cup \{ required \mapsto seq \; \{ x \mid S.required \} \} /// }\f] -class DuplicateRequiredValues final : public sourcemeta::alterschema::Rule { +class DuplicateRequiredJSONs final + : public sourcemeta::jsontoolkit::SchemaTransformRule { public: - DuplicateRequiredValues() : Rule("duplicate_required_values"){}; + DuplicateRequiredJSONs() : SchemaTransformRule("duplicate_required_values"){}; /// The rule condition - [[nodiscard]] auto - condition(const sourcemeta::jsontoolkit::Value &schema, - const std::string &draft, - const std::unordered_map &vocabularies, - const std::size_t) const -> bool override { - return draft == "https://json-schema.org/draft/2020-12/schema" && + [[nodiscard]] auto condition(const sourcemeta::jsontoolkit::JSON &schema, + const std::string &dialect, + const std::set &vocabularies, + const sourcemeta::jsontoolkit::Pointer &) const + -> bool override { + return dialect == "https://json-schema.org/draft/2020-12/schema" && vocabularies.contains( "https://json-schema.org/draft/2020-12/vocab/validation") && - sourcemeta::jsontoolkit::is_object(schema) && - sourcemeta::jsontoolkit::defines(schema, "required") && - sourcemeta::jsontoolkit::is_array( - sourcemeta::jsontoolkit::at(schema, "required")) && - !is_unique(sourcemeta::jsontoolkit::at(schema, "required")); + schema.is_object() && schema.defines("required") && + sourcemeta::jsontoolkit::is_array(schema.at("required")) && + !is_unique(schema.at("required")); } /// The rule transformation auto transform(sourcemeta::jsontoolkit::JSON &, - sourcemeta::jsontoolkit::Value &value) const -> void override { + sourcemeta::jsontoolkit::JSON &value) const -> void override { auto &collection{sourcemeta::jsontoolkit::at(value, "required")}; std::sort(sourcemeta::jsontoolkit::begin_array(collection), sourcemeta::jsontoolkit::end_array(collection), diff --git a/src/canonicalizer/rules/empty_array_as_const.h b/src/canonicalizer/rules/empty_array_as_const.h index a8af0812e..1e6cdf2cb 100644 --- a/src/canonicalizer/rules/empty_array_as_const.h +++ b/src/canonicalizer/rules/empty_array_as_const.h @@ -16,35 +16,31 @@ namespace sourcemeta::jsonbinpack::canonicalizer { /// \mapsto S \cup \{ const \mapsto \langle\rangle \} \setminus \{ maxItems \} /// }\f] -class EmptyArrayAsConst final : public sourcemeta::alterschema::Rule { +class EmptyArrayAsConst final + : public sourcemeta::jsontoolkit::SchemaTransformRule { public: - EmptyArrayAsConst() : Rule("empty_array_as_const"){}; + EmptyArrayAsConst() : SchemaTransformRule("empty_array_as_const"){}; /// The rule condition - [[nodiscard]] auto - condition(const sourcemeta::jsontoolkit::Value &schema, - const std::string &draft, - const std::unordered_map &vocabularies, - const std::size_t) const -> bool override { - return draft == "https://json-schema.org/draft/2020-12/schema" && + [[nodiscard]] auto condition(const sourcemeta::jsontoolkit::JSON &schema, + const std::string &dialect, + const std::set &vocabularies, + const sourcemeta::jsontoolkit::Pointer &) const + -> bool override { + return dialect == "https://json-schema.org/draft/2020-12/schema" && vocabularies.contains( "https://json-schema.org/draft/2020-12/vocab/validation") && - sourcemeta::jsontoolkit::is_object(schema) && - sourcemeta::jsontoolkit::defines(schema, "type") && - sourcemeta::jsontoolkit::is_string( - sourcemeta::jsontoolkit::at(schema, "type")) && - sourcemeta::jsontoolkit::to_string( - sourcemeta::jsontoolkit::at(schema, "type")) == "array" && - sourcemeta::jsontoolkit::defines(schema, "maxItems") && - sourcemeta::jsontoolkit::is_integer( - sourcemeta::jsontoolkit::at(schema, "maxItems")) && - sourcemeta::jsontoolkit::to_integer( - sourcemeta::jsontoolkit::at(schema, "maxItems")) == 0; + schema.is_object() && schema.defines("type") && + sourcemeta::jsontoolkit::is_string(schema.at("type")) && + sourcemeta::jsontoolkit::to_string(schema.at("type")) == "array" && + schema.defines("maxItems") && + sourcemeta::jsontoolkit::is_integer(schema.at("maxItems")) && + sourcemeta::jsontoolkit::to_integer(schema.at("maxItems")) == 0; } /// The rule transformation - auto transform(sourcemeta::jsontoolkit::JSON &document, - sourcemeta::jsontoolkit::Value &value) const -> void override { + auto transform(sourcemeta::jsontoolkit::SchemaTransformer &schema) const + -> void override { sourcemeta::jsontoolkit::assign(document, value, "const", sourcemeta::jsontoolkit::make_array()); sourcemeta::jsontoolkit::erase(value, "maxItems"); diff --git a/src/canonicalizer/rules/empty_dependent_required.h b/src/canonicalizer/rules/empty_dependent_required.h index e1c8abcc8..2c3c5c64b 100644 --- a/src/canonicalizer/rules/empty_dependent_required.h +++ b/src/canonicalizer/rules/empty_dependent_required.h @@ -16,30 +16,28 @@ namespace sourcemeta::jsonbinpack::canonicalizer { /// \mapsto S \setminus \{ dependentRequired \} /// }\f] -class EmptyDependentRequired final : public sourcemeta::alterschema::Rule { +class EmptyDependentRequired final + : public sourcemeta::jsontoolkit::SchemaTransformRule { public: - EmptyDependentRequired() : Rule("empty_dependent_required"){}; + EmptyDependentRequired() : SchemaTransformRule("empty_dependent_required"){}; /// The rule condition - [[nodiscard]] auto - condition(const sourcemeta::jsontoolkit::Value &schema, - const std::string &draft, - const std::unordered_map &vocabularies, - const std::size_t) const -> bool override { - return draft == "https://json-schema.org/draft/2020-12/schema" && + [[nodiscard]] auto condition(const sourcemeta::jsontoolkit::JSON &schema, + const std::string &dialect, + const std::set &vocabularies, + const sourcemeta::jsontoolkit::Pointer &) const + -> bool override { + return dialect == "https://json-schema.org/draft/2020-12/schema" && vocabularies.contains( "https://json-schema.org/draft/2020-12/vocab/validation") && - sourcemeta::jsontoolkit::is_object(schema) && - sourcemeta::jsontoolkit::defines(schema, "dependentRequired") && - sourcemeta::jsontoolkit::is_object( - sourcemeta::jsontoolkit::at(schema, "dependentRequired")) && - sourcemeta::jsontoolkit::empty( - sourcemeta::jsontoolkit::at(schema, "dependentRequired")); + schema.is_object() && schema.defines("dependentRequired") && + sourcemeta::jsontoolkit::is_object(schema.at("dependentRequired")) && + sourcemeta::jsontoolkit::empty(schema.at("dependentRequired")); } /// The rule transformation auto transform(sourcemeta::jsontoolkit::JSON &, - sourcemeta::jsontoolkit::Value &value) const -> void override { + sourcemeta::jsontoolkit::JSON &value) const -> void override { sourcemeta::jsontoolkit::erase(value, "dependentRequired"); } }; diff --git a/src/canonicalizer/rules/empty_object_as_const.h b/src/canonicalizer/rules/empty_object_as_const.h index 0c165d9c3..209674534 100644 --- a/src/canonicalizer/rules/empty_object_as_const.h +++ b/src/canonicalizer/rules/empty_object_as_const.h @@ -16,35 +16,31 @@ namespace sourcemeta::jsonbinpack::canonicalizer { /// \mapsto S \cup \{ const \mapsto \{\} \} \setminus \{ maxProperties \} /// }\f] -class EmptyObjectAsConst final : public sourcemeta::alterschema::Rule { +class EmptyObjectAsConst final + : public sourcemeta::jsontoolkit::SchemaTransformRule { public: - EmptyObjectAsConst() : Rule("empty_object_as_const"){}; + EmptyObjectAsConst() : SchemaTransformRule("empty_object_as_const"){}; /// The rule condition - [[nodiscard]] auto - condition(const sourcemeta::jsontoolkit::Value &schema, - const std::string &draft, - const std::unordered_map &vocabularies, - const std::size_t) const -> bool override { - return draft == "https://json-schema.org/draft/2020-12/schema" && + [[nodiscard]] auto condition(const sourcemeta::jsontoolkit::JSON &schema, + const std::string &dialect, + const std::set &vocabularies, + const sourcemeta::jsontoolkit::Pointer &) const + -> bool override { + return dialect == "https://json-schema.org/draft/2020-12/schema" && vocabularies.contains( "https://json-schema.org/draft/2020-12/vocab/validation") && - sourcemeta::jsontoolkit::is_object(schema) && - sourcemeta::jsontoolkit::defines(schema, "type") && - sourcemeta::jsontoolkit::is_string( - sourcemeta::jsontoolkit::at(schema, "type")) && - sourcemeta::jsontoolkit::to_string( - sourcemeta::jsontoolkit::at(schema, "type")) == "object" && - sourcemeta::jsontoolkit::defines(schema, "maxProperties") && - sourcemeta::jsontoolkit::is_integer( - sourcemeta::jsontoolkit::at(schema, "maxProperties")) && - sourcemeta::jsontoolkit::to_integer( - sourcemeta::jsontoolkit::at(schema, "maxProperties")) == 0; + schema.is_object() && schema.defines("type") && + sourcemeta::jsontoolkit::is_string(schema.at("type")) && + sourcemeta::jsontoolkit::to_string(schema.at("type")) == "object" && + schema.defines("maxProperties") && + sourcemeta::jsontoolkit::is_integer(schema.at("maxProperties")) && + sourcemeta::jsontoolkit::to_integer(schema.at("maxProperties")) == 0; } /// The rule transformation - auto transform(sourcemeta::jsontoolkit::JSON &document, - sourcemeta::jsontoolkit::Value &value) const -> void override { + auto transform(sourcemeta::jsontoolkit::SchemaTransformer &schema) const + -> void override { sourcemeta::jsontoolkit::assign(document, value, "const", sourcemeta::jsontoolkit::make_object()); sourcemeta::jsontoolkit::erase(value, "maxProperties"); diff --git a/src/canonicalizer/rules/empty_pattern_properties.h b/src/canonicalizer/rules/empty_pattern_properties.h index b113f3e17..cdd2cd4dc 100644 --- a/src/canonicalizer/rules/empty_pattern_properties.h +++ b/src/canonicalizer/rules/empty_pattern_properties.h @@ -16,30 +16,28 @@ namespace sourcemeta::jsonbinpack::canonicalizer { /// \mapsto S \setminus \{ patternProperties \} /// }\f] -class EmptyPatternProperties final : public sourcemeta::alterschema::Rule { +class EmptyPatternProperties final + : public sourcemeta::jsontoolkit::SchemaTransformRule { public: - EmptyPatternProperties() : Rule("empty_pattern_properties"){}; + EmptyPatternProperties() : SchemaTransformRule("empty_pattern_properties"){}; /// The rule condition - [[nodiscard]] auto - condition(const sourcemeta::jsontoolkit::Value &schema, - const std::string &draft, - const std::unordered_map &vocabularies, - const std::size_t) const -> bool override { - return draft == "https://json-schema.org/draft/2020-12/schema" && + [[nodiscard]] auto condition(const sourcemeta::jsontoolkit::JSON &schema, + const std::string &dialect, + const std::set &vocabularies, + const sourcemeta::jsontoolkit::Pointer &) const + -> bool override { + return dialect == "https://json-schema.org/draft/2020-12/schema" && vocabularies.contains( "https://json-schema.org/draft/2020-12/vocab/applicator") && - sourcemeta::jsontoolkit::is_object(schema) && - sourcemeta::jsontoolkit::defines(schema, "patternProperties") && - sourcemeta::jsontoolkit::is_object( - sourcemeta::jsontoolkit::at(schema, "patternProperties")) && - sourcemeta::jsontoolkit::empty( - sourcemeta::jsontoolkit::at(schema, "patternProperties")); + schema.is_object() && schema.defines("patternProperties") && + sourcemeta::jsontoolkit::is_object(schema.at("patternProperties")) && + sourcemeta::jsontoolkit::empty(schema.at("patternProperties")); } /// The rule transformation auto transform(sourcemeta::jsontoolkit::JSON &, - sourcemeta::jsontoolkit::Value &value) const -> void override { + sourcemeta::jsontoolkit::JSON &value) const -> void override { sourcemeta::jsontoolkit::erase(value, "patternProperties"); } }; diff --git a/src/canonicalizer/rules/empty_string_as_const.h b/src/canonicalizer/rules/empty_string_as_const.h index f1467a089..6d12a8e0e 100644 --- a/src/canonicalizer/rules/empty_string_as_const.h +++ b/src/canonicalizer/rules/empty_string_as_const.h @@ -16,35 +16,31 @@ namespace sourcemeta::jsonbinpack::canonicalizer { /// \mapsto S \cup \{ const \mapsto "" \} \setminus \{ maxLength \} /// }\f] -class EmptyStringAsConst final : public sourcemeta::alterschema::Rule { +class EmptyStringAsConst final + : public sourcemeta::jsontoolkit::SchemaTransformRule { public: - EmptyStringAsConst() : Rule("empty_string_as_const"){}; + EmptyStringAsConst() : SchemaTransformRule("empty_string_as_const"){}; /// The rule condition - [[nodiscard]] auto - condition(const sourcemeta::jsontoolkit::Value &schema, - const std::string &draft, - const std::unordered_map &vocabularies, - const std::size_t) const -> bool override { - return draft == "https://json-schema.org/draft/2020-12/schema" && + [[nodiscard]] auto condition(const sourcemeta::jsontoolkit::JSON &schema, + const std::string &dialect, + const std::set &vocabularies, + const sourcemeta::jsontoolkit::Pointer &) const + -> bool override { + return dialect == "https://json-schema.org/draft/2020-12/schema" && vocabularies.contains( "https://json-schema.org/draft/2020-12/vocab/validation") && - sourcemeta::jsontoolkit::is_object(schema) && - sourcemeta::jsontoolkit::defines(schema, "type") && - sourcemeta::jsontoolkit::is_string( - sourcemeta::jsontoolkit::at(schema, "type")) && - sourcemeta::jsontoolkit::to_string( - sourcemeta::jsontoolkit::at(schema, "type")) == "string" && - sourcemeta::jsontoolkit::defines(schema, "maxLength") && - sourcemeta::jsontoolkit::is_integer( - sourcemeta::jsontoolkit::at(schema, "maxLength")) && - sourcemeta::jsontoolkit::to_integer( - sourcemeta::jsontoolkit::at(schema, "maxLength")) == 0; + schema.is_object() && schema.defines("type") && + sourcemeta::jsontoolkit::is_string(schema.at("type")) && + sourcemeta::jsontoolkit::to_string(schema.at("type")) == "string" && + schema.defines("maxLength") && + sourcemeta::jsontoolkit::is_integer(schema.at("maxLength")) && + sourcemeta::jsontoolkit::to_integer(schema.at("maxLength")) == 0; } /// The rule transformation - auto transform(sourcemeta::jsontoolkit::JSON &document, - sourcemeta::jsontoolkit::Value &value) const -> void override { + auto transform(sourcemeta::jsontoolkit::SchemaTransformer &schema) const + -> void override { sourcemeta::jsontoolkit::assign(document, value, "const", sourcemeta::jsontoolkit::from("")); sourcemeta::jsontoolkit::erase(value, "maxLength"); diff --git a/src/canonicalizer/rules/equal_numeric_bounds_as_const.h b/src/canonicalizer/rules/equal_numeric_bounds_as_const.h index b9426246a..7f10dcd3b 100644 --- a/src/canonicalizer/rules/equal_numeric_bounds_as_const.h +++ b/src/canonicalizer/rules/equal_numeric_bounds_as_const.h @@ -18,40 +18,37 @@ namespace sourcemeta::jsonbinpack::canonicalizer { /// \mapsto S.minimum \} \setminus \{ minimum, maximum \} /// }\f] -class EqualNumericBoundsAsConst final : public sourcemeta::alterschema::Rule { +class EqualNumericBoundsAsConst final + : public sourcemeta::jsontoolkit::SchemaTransformRule { public: - EqualNumericBoundsAsConst() : Rule("equal_numeric_bounds_as_const"){}; + EqualNumericBoundsAsConst() + : SchemaTransformRule("equal_numeric_bounds_as_const"){}; /// The rule condition - [[nodiscard]] auto - condition(const sourcemeta::jsontoolkit::Value &schema, - const std::string &draft, - const std::unordered_map &vocabularies, - const std::size_t) const -> bool override { - return draft == "https://json-schema.org/draft/2020-12/schema" && + [[nodiscard]] auto condition(const sourcemeta::jsontoolkit::JSON &schema, + const std::string &dialect, + const std::set &vocabularies, + const sourcemeta::jsontoolkit::Pointer &) const + -> bool override { + return dialect == "https://json-schema.org/draft/2020-12/schema" && vocabularies.contains( "https://json-schema.org/draft/2020-12/vocab/validation") && - sourcemeta::jsontoolkit::is_object(schema) && - sourcemeta::jsontoolkit::defines(schema, "type") && - sourcemeta::jsontoolkit::is_string( - sourcemeta::jsontoolkit::at(schema, "type")) && - (sourcemeta::jsontoolkit::to_string( - sourcemeta::jsontoolkit::at(schema, "type")) == "integer" || - sourcemeta::jsontoolkit::to_string( - sourcemeta::jsontoolkit::at(schema, "type")) == "number") && - sourcemeta::jsontoolkit::defines(schema, "minimum") && - sourcemeta::jsontoolkit::is_number( - sourcemeta::jsontoolkit::at(schema, "minimum")) && - sourcemeta::jsontoolkit::defines(schema, "maximum") && - sourcemeta::jsontoolkit::is_number( - sourcemeta::jsontoolkit::at(schema, "maximum")) && - sourcemeta::jsontoolkit::at(schema, "minimum") == - sourcemeta::jsontoolkit::at(schema, "maximum"); + schema.is_object() && schema.defines("type") && + sourcemeta::jsontoolkit::is_string(schema.at("type")) && + (sourcemeta::jsontoolkit::to_string(schema.at("type")) == + "integer" || + sourcemeta::jsontoolkit::to_string(schema.at("type")) == + "number") && + schema.defines("minimum") && + sourcemeta::jsontoolkit::is_number(schema.at("minimum")) && + schema.defines("maximum") && + sourcemeta::jsontoolkit::is_number(schema.at("maximum")) && + schema.at("minimum") == schema.at("maximum"); } /// The rule transformation - auto transform(sourcemeta::jsontoolkit::JSON &document, - sourcemeta::jsontoolkit::Value &value) const -> void override { + auto transform(sourcemeta::jsontoolkit::SchemaTransformer &schema) const + -> void override { sourcemeta::jsontoolkit::assign( document, value, "const", sourcemeta::jsontoolkit::at(value, "minimum")); diff --git a/src/canonicalizer/rules/exclusive_maximum_and_maximum.h b/src/canonicalizer/rules/exclusive_maximum_and_maximum.h index dbc519763..47d0a6480 100644 --- a/src/canonicalizer/rules/exclusive_maximum_and_maximum.h +++ b/src/canonicalizer/rules/exclusive_maximum_and_maximum.h @@ -24,31 +24,30 @@ namespace sourcemeta::jsonbinpack::canonicalizer { /// S.exclusiveMaximum}{S \mapsto S \setminus \{ maximum \} /// }\f] -class ExclusiveMaximumAndMaximum final : public sourcemeta::alterschema::Rule { +class ExclusiveMaximumAndMaximum final + : public sourcemeta::jsontoolkit::SchemaTransformRule { public: - ExclusiveMaximumAndMaximum() : Rule("exclusive_maximum_and_maximum"){}; + ExclusiveMaximumAndMaximum() + : SchemaTransformRule("exclusive_maximum_and_maximum"){}; /// The rule condition - [[nodiscard]] auto - condition(const sourcemeta::jsontoolkit::Value &schema, - const std::string &draft, - const std::unordered_map &vocabularies, - const std::size_t) const -> bool override { - return draft == "https://json-schema.org/draft/2020-12/schema" && + [[nodiscard]] auto condition(const sourcemeta::jsontoolkit::JSON &schema, + const std::string &dialect, + const std::set &vocabularies, + const sourcemeta::jsontoolkit::Pointer &) const + -> bool override { + return dialect == "https://json-schema.org/draft/2020-12/schema" && vocabularies.contains( "https://json-schema.org/draft/2020-12/vocab/validation") && - sourcemeta::jsontoolkit::is_object(schema) && - sourcemeta::jsontoolkit::defines(schema, "maximum") && - sourcemeta::jsontoolkit::defines(schema, "exclusiveMaximum") && - sourcemeta::jsontoolkit::is_number( - sourcemeta::jsontoolkit::at(schema, "maximum")) && - sourcemeta::jsontoolkit::is_number( - sourcemeta::jsontoolkit::at(schema, "exclusiveMaximum")); + schema.is_object() && schema.defines("maximum") && + schema.defines("exclusiveMaximum") && + sourcemeta::jsontoolkit::is_number(schema.at("maximum")) && + sourcemeta::jsontoolkit::is_number(schema.at("exclusiveMaximum")); } /// The rule transformation auto transform(sourcemeta::jsontoolkit::JSON &, - sourcemeta::jsontoolkit::Value &value) const -> void override { + sourcemeta::jsontoolkit::JSON &value) const -> void override { const bool maximum_less_than_exclusive_maximum{ sourcemeta::jsontoolkit::compare( sourcemeta::jsontoolkit::at(value, "maximum"), diff --git a/src/canonicalizer/rules/exclusive_maximum_to_maximum.h b/src/canonicalizer/rules/exclusive_maximum_to_maximum.h index 8b408ca27..561752a47 100644 --- a/src/canonicalizer/rules/exclusive_maximum_to_maximum.h +++ b/src/canonicalizer/rules/exclusive_maximum_to_maximum.h @@ -16,29 +16,29 @@ namespace sourcemeta::jsonbinpack::canonicalizer { /// S \cup \{ maximum \mapsto S.exclusiveMaximum - 1 \} \setminus \{ /// exclusiveMaximum \} }\f] -class ExclusiveMaximumToMaximum final : public sourcemeta::alterschema::Rule { +class ExclusiveMaximumToMaximum final + : public sourcemeta::jsontoolkit::SchemaTransformRule { public: - ExclusiveMaximumToMaximum() : Rule("exclusive_maximum_to_maximum"){}; + ExclusiveMaximumToMaximum() + : SchemaTransformRule("exclusive_maximum_to_maximum"){}; /// The rule condition - [[nodiscard]] auto - condition(const sourcemeta::jsontoolkit::Value &schema, - const std::string &draft, - const std::unordered_map &vocabularies, - const std::size_t) const -> bool override { - return draft == "https://json-schema.org/draft/2020-12/schema" && + [[nodiscard]] auto condition(const sourcemeta::jsontoolkit::JSON &schema, + const std::string &dialect, + const std::set &vocabularies, + const sourcemeta::jsontoolkit::Pointer &) const + -> bool override { + return dialect == "https://json-schema.org/draft/2020-12/schema" && vocabularies.contains( "https://json-schema.org/draft/2020-12/vocab/validation") && - sourcemeta::jsontoolkit::is_object(schema) && - sourcemeta::jsontoolkit::defines(schema, "exclusiveMaximum") && - sourcemeta::jsontoolkit::is_number( - sourcemeta::jsontoolkit::at(schema, "exclusiveMaximum")) && - !sourcemeta::jsontoolkit::defines(schema, "maximum"); + schema.is_object() && schema.defines("exclusiveMaximum") && + sourcemeta::jsontoolkit::is_number(schema.at("exclusiveMaximum")) && + !schema.defines("maximum"); } /// The rule transformation - auto transform(sourcemeta::jsontoolkit::JSON &document, - sourcemeta::jsontoolkit::Value &value) const -> void override { + auto transform(sourcemeta::jsontoolkit::SchemaTransformer &schema) const + -> void override { auto new_maximum{sourcemeta::jsontoolkit::from( sourcemeta::jsontoolkit::at(value, "exclusiveMaximum"))}; sourcemeta::jsontoolkit::add(new_maximum, diff --git a/src/canonicalizer/rules/exclusive_minimum_and_minimum.h b/src/canonicalizer/rules/exclusive_minimum_and_minimum.h index 9920edb9f..cb6e8084f 100644 --- a/src/canonicalizer/rules/exclusive_minimum_and_minimum.h +++ b/src/canonicalizer/rules/exclusive_minimum_and_minimum.h @@ -24,31 +24,30 @@ namespace sourcemeta::jsonbinpack::canonicalizer { /// S.exclusiveMinimum \geq S.minimum}{S \mapsto S \setminus \{ minimum \} /// }\f] -class ExclusiveMinimumAndMinimum final : public sourcemeta::alterschema::Rule { +class ExclusiveMinimumAndMinimum final + : public sourcemeta::jsontoolkit::SchemaTransformRule { public: - ExclusiveMinimumAndMinimum() : Rule("exclusive_minimum_and_minimum"){}; + ExclusiveMinimumAndMinimum() + : SchemaTransformRule("exclusive_minimum_and_minimum"){}; /// The rule condition - [[nodiscard]] auto - condition(const sourcemeta::jsontoolkit::Value &schema, - const std::string &draft, - const std::unordered_map &vocabularies, - const std::size_t) const -> bool override { - return draft == "https://json-schema.org/draft/2020-12/schema" && + [[nodiscard]] auto condition(const sourcemeta::jsontoolkit::JSON &schema, + const std::string &dialect, + const std::set &vocabularies, + const sourcemeta::jsontoolkit::Pointer &) const + -> bool override { + return dialect == "https://json-schema.org/draft/2020-12/schema" && vocabularies.contains( "https://json-schema.org/draft/2020-12/vocab/validation") && - sourcemeta::jsontoolkit::is_object(schema) && - sourcemeta::jsontoolkit::defines(schema, "minimum") && - sourcemeta::jsontoolkit::defines(schema, "exclusiveMinimum") && - sourcemeta::jsontoolkit::is_number( - sourcemeta::jsontoolkit::at(schema, "minimum")) && - sourcemeta::jsontoolkit::is_number( - sourcemeta::jsontoolkit::at(schema, "exclusiveMinimum")); + schema.is_object() && schema.defines("minimum") && + schema.defines("exclusiveMinimum") && + sourcemeta::jsontoolkit::is_number(schema.at("minimum")) && + sourcemeta::jsontoolkit::is_number(schema.at("exclusiveMinimum")); } /// The rule transformation auto transform(sourcemeta::jsontoolkit::JSON &, - sourcemeta::jsontoolkit::Value &value) const -> void override { + sourcemeta::jsontoolkit::JSON &value) const -> void override { const bool exclusive_minimum_less_than_minimum{ sourcemeta::jsontoolkit::compare( sourcemeta::jsontoolkit::at(value, "exclusiveMinimum"), diff --git a/src/canonicalizer/rules/exclusive_minimum_to_minimum.h b/src/canonicalizer/rules/exclusive_minimum_to_minimum.h index e9fc83dfb..b02c6d8f2 100644 --- a/src/canonicalizer/rules/exclusive_minimum_to_minimum.h +++ b/src/canonicalizer/rules/exclusive_minimum_to_minimum.h @@ -16,29 +16,29 @@ namespace sourcemeta::jsonbinpack::canonicalizer { /// S \cup \{ minimum \mapsto S.exclusiveMinimum + 1 \} \setminus \{ /// exclusiveMinimum \} }\f] -class ExclusiveMinimumToMinimum final : public sourcemeta::alterschema::Rule { +class ExclusiveMinimumToMinimum final + : public sourcemeta::jsontoolkit::SchemaTransformRule { public: - ExclusiveMinimumToMinimum() : Rule("exclusive_minimum_to_minimum"){}; + ExclusiveMinimumToMinimum() + : SchemaTransformRule("exclusive_minimum_to_minimum"){}; /// The rule condition - [[nodiscard]] auto - condition(const sourcemeta::jsontoolkit::Value &schema, - const std::string &draft, - const std::unordered_map &vocabularies, - const std::size_t) const -> bool override { - return draft == "https://json-schema.org/draft/2020-12/schema" && + [[nodiscard]] auto condition(const sourcemeta::jsontoolkit::JSON &schema, + const std::string &dialect, + const std::set &vocabularies, + const sourcemeta::jsontoolkit::Pointer &) const + -> bool override { + return dialect == "https://json-schema.org/draft/2020-12/schema" && vocabularies.contains( "https://json-schema.org/draft/2020-12/vocab/validation") && - sourcemeta::jsontoolkit::is_object(schema) && - sourcemeta::jsontoolkit::defines(schema, "exclusiveMinimum") && - sourcemeta::jsontoolkit::is_number( - sourcemeta::jsontoolkit::at(schema, "exclusiveMinimum")) && - !sourcemeta::jsontoolkit::defines(schema, "minimum"); + schema.is_object() && schema.defines("exclusiveMinimum") && + sourcemeta::jsontoolkit::is_number(schema.at("exclusiveMinimum")) && + !schema.defines("minimum"); } /// The rule transformation - auto transform(sourcemeta::jsontoolkit::JSON &document, - sourcemeta::jsontoolkit::Value &value) const -> void override { + auto transform(sourcemeta::jsontoolkit::SchemaTransformer &schema) const + -> void override { auto new_minimum{sourcemeta::jsontoolkit::from( sourcemeta::jsontoolkit::at(value, "exclusiveMinimum"))}; sourcemeta::jsontoolkit::add(new_minimum, sourcemeta::jsontoolkit::from(1)); diff --git a/src/canonicalizer/rules/if_without_then_else.h b/src/canonicalizer/rules/if_without_then_else.h index 78069280f..aa1de898b 100644 --- a/src/canonicalizer/rules/if_without_then_else.h +++ b/src/canonicalizer/rules/if_without_then_else.h @@ -21,28 +21,27 @@ namespace sourcemeta::jsonbinpack::canonicalizer { /// \mapsto S \setminus \{ if \} /// }\f] -class IfWithoutThenElse final : public sourcemeta::alterschema::Rule { +class IfWithoutThenElse final + : public sourcemeta::jsontoolkit::SchemaTransformRule { public: - IfWithoutThenElse() : Rule("if_without_then_else"){}; + IfWithoutThenElse() : SchemaTransformRule("if_without_then_else"){}; /// The rule condition - [[nodiscard]] auto - condition(const sourcemeta::jsontoolkit::Value &schema, - const std::string &draft, - const std::unordered_map &vocabularies, - const std::size_t) const -> bool override { - return draft == "https://json-schema.org/draft/2020-12/schema" && + [[nodiscard]] auto condition(const sourcemeta::jsontoolkit::JSON &schema, + const std::string &dialect, + const std::set &vocabularies, + const sourcemeta::jsontoolkit::Pointer &) const + -> bool override { + return dialect == "https://json-schema.org/draft/2020-12/schema" && vocabularies.contains( "https://json-schema.org/draft/2020-12/vocab/applicator") && - sourcemeta::jsontoolkit::is_object(schema) && - sourcemeta::jsontoolkit::defines(schema, "if") && - !sourcemeta::jsontoolkit::defines(schema, "then") && - !sourcemeta::jsontoolkit::defines(schema, "else"); + schema.is_object() && schema.defines("if") && + !schema.defines("then") && !schema.defines("else"); } /// The rule transformation auto transform(sourcemeta::jsontoolkit::JSON &, - sourcemeta::jsontoolkit::Value &value) const -> void override { + sourcemeta::jsontoolkit::JSON &value) const -> void override { sourcemeta::jsontoolkit::erase(value, "if"); } }; diff --git a/src/canonicalizer/rules/implicit_array_lower_bound.h b/src/canonicalizer/rules/implicit_array_lower_bound.h index ac908672c..d72638641 100644 --- a/src/canonicalizer/rules/implicit_array_lower_bound.h +++ b/src/canonicalizer/rules/implicit_array_lower_bound.h @@ -16,31 +16,30 @@ namespace sourcemeta::jsonbinpack::canonicalizer { /// \mapsto S \cup \{ minItems \mapsto 0 \} /// }\f] -class ImplicitArrayLowerBound final : public sourcemeta::alterschema::Rule { +class ImplicitArrayLowerBound final + : public sourcemeta::jsontoolkit::SchemaTransformRule { public: - ImplicitArrayLowerBound() : Rule("implicit_array_lower_bound"){}; + ImplicitArrayLowerBound() + : SchemaTransformRule("implicit_array_lower_bound"){}; /// The rule condition - [[nodiscard]] auto - condition(const sourcemeta::jsontoolkit::Value &schema, - const std::string &draft, - const std::unordered_map &vocabularies, - const std::size_t) const -> bool override { - return draft == "https://json-schema.org/draft/2020-12/schema" && + [[nodiscard]] auto condition(const sourcemeta::jsontoolkit::JSON &schema, + const std::string &dialect, + const std::set &vocabularies, + const sourcemeta::jsontoolkit::Pointer &) const + -> bool override { + return dialect == "https://json-schema.org/draft/2020-12/schema" && vocabularies.contains( "https://json-schema.org/draft/2020-12/vocab/validation") && - sourcemeta::jsontoolkit::is_object(schema) && - sourcemeta::jsontoolkit::defines(schema, "type") && - sourcemeta::jsontoolkit::is_string( - sourcemeta::jsontoolkit::at(schema, "type")) && - sourcemeta::jsontoolkit::to_string( - sourcemeta::jsontoolkit::at(schema, "type")) == "array" && - !sourcemeta::jsontoolkit::defines(schema, "minItems"); + schema.is_object() && schema.defines("type") && + sourcemeta::jsontoolkit::is_string(schema.at("type")) && + sourcemeta::jsontoolkit::to_string(schema.at("type")) == "array" && + !schema.defines("minItems"); } /// The rule transformation - auto transform(sourcemeta::jsontoolkit::JSON &document, - sourcemeta::jsontoolkit::Value &value) const -> void override { + auto transform(sourcemeta::jsontoolkit::SchemaTransformer &schema) const + -> void override { sourcemeta::jsontoolkit::assign(document, value, "minItems", sourcemeta::jsontoolkit::from(0)); } diff --git a/src/canonicalizer/rules/implicit_object_lower_bound.h b/src/canonicalizer/rules/implicit_object_lower_bound.h index ecdf91cd9..2ab22f8fc 100644 --- a/src/canonicalizer/rules/implicit_object_lower_bound.h +++ b/src/canonicalizer/rules/implicit_object_lower_bound.h @@ -16,31 +16,30 @@ namespace sourcemeta::jsonbinpack::canonicalizer { /// \mapsto S \cup \{ minProperties \mapsto 0 \} /// }\f] -class ImplicitObjectLowerBound final : public sourcemeta::alterschema::Rule { +class ImplicitObjectLowerBound final + : public sourcemeta::jsontoolkit::SchemaTransformRule { public: - ImplicitObjectLowerBound() : Rule("implicit_object_lower_bound"){}; + ImplicitObjectLowerBound() + : SchemaTransformRule("implicit_object_lower_bound"){}; /// The rule condition - [[nodiscard]] auto - condition(const sourcemeta::jsontoolkit::Value &schema, - const std::string &draft, - const std::unordered_map &vocabularies, - const std::size_t) const -> bool override { - return draft == "https://json-schema.org/draft/2020-12/schema" && + [[nodiscard]] auto condition(const sourcemeta::jsontoolkit::JSON &schema, + const std::string &dialect, + const std::set &vocabularies, + const sourcemeta::jsontoolkit::Pointer &) const + -> bool override { + return dialect == "https://json-schema.org/draft/2020-12/schema" && vocabularies.contains( "https://json-schema.org/draft/2020-12/vocab/validation") && - sourcemeta::jsontoolkit::is_object(schema) && - sourcemeta::jsontoolkit::defines(schema, "type") && - sourcemeta::jsontoolkit::is_string( - sourcemeta::jsontoolkit::at(schema, "type")) && - sourcemeta::jsontoolkit::to_string( - sourcemeta::jsontoolkit::at(schema, "type")) == "object" && - !sourcemeta::jsontoolkit::defines(schema, "minProperties"); + schema.is_object() && schema.defines("type") && + sourcemeta::jsontoolkit::is_string(schema.at("type")) && + sourcemeta::jsontoolkit::to_string(schema.at("type")) == "object" && + !schema.defines("minProperties"); } /// The rule transformation - auto transform(sourcemeta::jsontoolkit::JSON &document, - sourcemeta::jsontoolkit::Value &value) const -> void override { + auto transform(sourcemeta::jsontoolkit::SchemaTransformer &schema) const + -> void override { sourcemeta::jsontoolkit::assign(document, value, "minProperties", sourcemeta::jsontoolkit::from(0)); } diff --git a/src/canonicalizer/rules/implicit_object_properties.h b/src/canonicalizer/rules/implicit_object_properties.h index bb6037358..dc6359d21 100644 --- a/src/canonicalizer/rules/implicit_object_properties.h +++ b/src/canonicalizer/rules/implicit_object_properties.h @@ -17,33 +17,32 @@ namespace sourcemeta::jsonbinpack::canonicalizer { /// \mapsto S \cup \{ properties \mapsto \{\} \} /// }\f] -class ImplicitObjectProperties final : public sourcemeta::alterschema::Rule { +class ImplicitObjectProperties final + : public sourcemeta::jsontoolkit::SchemaTransformRule { public: - ImplicitObjectProperties() : Rule("implicit_object_properties"){}; + ImplicitObjectProperties() + : SchemaTransformRule("implicit_object_properties"){}; /// The rule condition - [[nodiscard]] auto - condition(const sourcemeta::jsontoolkit::Value &schema, - const std::string &draft, - const std::unordered_map &vocabularies, - const std::size_t) const -> bool override { - return draft == "https://json-schema.org/draft/2020-12/schema" && + [[nodiscard]] auto condition(const sourcemeta::jsontoolkit::JSON &schema, + const std::string &dialect, + const std::set &vocabularies, + const sourcemeta::jsontoolkit::Pointer &) const + -> bool override { + return dialect == "https://json-schema.org/draft/2020-12/schema" && vocabularies.contains( "https://json-schema.org/draft/2020-12/vocab/validation") && vocabularies.contains( "https://json-schema.org/draft/2020-12/vocab/applicator") && - sourcemeta::jsontoolkit::is_object(schema) && - sourcemeta::jsontoolkit::defines(schema, "type") && - sourcemeta::jsontoolkit::is_string( - sourcemeta::jsontoolkit::at(schema, "type")) && - sourcemeta::jsontoolkit::to_string( - sourcemeta::jsontoolkit::at(schema, "type")) == "object" && - !sourcemeta::jsontoolkit::defines(schema, "properties"); + schema.is_object() && schema.defines("type") && + sourcemeta::jsontoolkit::is_string(schema.at("type")) && + sourcemeta::jsontoolkit::to_string(schema.at("type")) == "object" && + !schema.defines("properties"); } /// The rule transformation - auto transform(sourcemeta::jsontoolkit::JSON &document, - sourcemeta::jsontoolkit::Value &value) const -> void override { + auto transform(sourcemeta::jsontoolkit::SchemaTransformer &schema) const + -> void override { sourcemeta::jsontoolkit::assign(document, value, "properties", sourcemeta::jsontoolkit::make_object()); } diff --git a/src/canonicalizer/rules/implicit_object_required.h b/src/canonicalizer/rules/implicit_object_required.h index 5fde3d3a8..acd0576fa 100644 --- a/src/canonicalizer/rules/implicit_object_required.h +++ b/src/canonicalizer/rules/implicit_object_required.h @@ -16,31 +16,29 @@ namespace sourcemeta::jsonbinpack::canonicalizer { /// \mapsto S \cup \{ required \mapsto \langle \rangle \} /// }\f] -class ImplicitObjectRequired final : public sourcemeta::alterschema::Rule { +class ImplicitObjectRequired final + : public sourcemeta::jsontoolkit::SchemaTransformRule { public: - ImplicitObjectRequired() : Rule("implicit_object_required"){}; + ImplicitObjectRequired() : SchemaTransformRule("implicit_object_required"){}; /// The rule condition - [[nodiscard]] auto - condition(const sourcemeta::jsontoolkit::Value &schema, - const std::string &draft, - const std::unordered_map &vocabularies, - const std::size_t) const -> bool override { - return draft == "https://json-schema.org/draft/2020-12/schema" && + [[nodiscard]] auto condition(const sourcemeta::jsontoolkit::JSON &schema, + const std::string &dialect, + const std::set &vocabularies, + const sourcemeta::jsontoolkit::Pointer &) const + -> bool override { + return dialect == "https://json-schema.org/draft/2020-12/schema" && vocabularies.contains( "https://json-schema.org/draft/2020-12/vocab/validation") && - sourcemeta::jsontoolkit::is_object(schema) && - sourcemeta::jsontoolkit::defines(schema, "type") && - sourcemeta::jsontoolkit::is_string( - sourcemeta::jsontoolkit::at(schema, "type")) && - sourcemeta::jsontoolkit::to_string( - sourcemeta::jsontoolkit::at(schema, "type")) == "object" && - !sourcemeta::jsontoolkit::defines(schema, "required"); + schema.is_object() && schema.defines("type") && + sourcemeta::jsontoolkit::is_string(schema.at("type")) && + sourcemeta::jsontoolkit::to_string(schema.at("type")) == "object" && + !schema.defines("required"); } /// The rule transformation - auto transform(sourcemeta::jsontoolkit::JSON &document, - sourcemeta::jsontoolkit::Value &value) const -> void override { + auto transform(sourcemeta::jsontoolkit::SchemaTransformer &schema) const + -> void override { sourcemeta::jsontoolkit::assign(document, value, "required", sourcemeta::jsontoolkit::make_array()); } diff --git a/src/canonicalizer/rules/implicit_string_lower_bound.h b/src/canonicalizer/rules/implicit_string_lower_bound.h index 48c9d9083..5bb7f0764 100644 --- a/src/canonicalizer/rules/implicit_string_lower_bound.h +++ b/src/canonicalizer/rules/implicit_string_lower_bound.h @@ -16,31 +16,30 @@ namespace sourcemeta::jsonbinpack::canonicalizer { /// \mapsto S \cup \{ minLength \mapsto 0 \} /// }\f] -class ImplicitStringLowerBound final : public sourcemeta::alterschema::Rule { +class ImplicitStringLowerBound final + : public sourcemeta::jsontoolkit::SchemaTransformRule { public: - ImplicitStringLowerBound() : Rule("implicit_string_lower_bound"){}; + ImplicitStringLowerBound() + : SchemaTransformRule("implicit_string_lower_bound"){}; /// The rule condition - [[nodiscard]] auto - condition(const sourcemeta::jsontoolkit::Value &schema, - const std::string &draft, - const std::unordered_map &vocabularies, - const std::size_t) const -> bool override { - return draft == "https://json-schema.org/draft/2020-12/schema" && + [[nodiscard]] auto condition(const sourcemeta::jsontoolkit::JSON &schema, + const std::string &dialect, + const std::set &vocabularies, + const sourcemeta::jsontoolkit::Pointer &) const + -> bool override { + return dialect == "https://json-schema.org/draft/2020-12/schema" && vocabularies.contains( "https://json-schema.org/draft/2020-12/vocab/validation") && - sourcemeta::jsontoolkit::is_object(schema) && - sourcemeta::jsontoolkit::defines(schema, "type") && - sourcemeta::jsontoolkit::is_string( - sourcemeta::jsontoolkit::at(schema, "type")) && - sourcemeta::jsontoolkit::to_string( - sourcemeta::jsontoolkit::at(schema, "type")) == "string" && - !sourcemeta::jsontoolkit::defines(schema, "minLength"); + schema.is_object() && schema.defines("type") && + sourcemeta::jsontoolkit::is_string(schema.at("type")) && + sourcemeta::jsontoolkit::to_string(schema.at("type")) == "string" && + !schema.defines("minLength"); } /// The rule transformation - auto transform(sourcemeta::jsontoolkit::JSON &document, - sourcemeta::jsontoolkit::Value &value) const -> void override { + auto transform(sourcemeta::jsontoolkit::SchemaTransformer &schema) const + -> void override { sourcemeta::jsontoolkit::assign(document, value, "minLength", sourcemeta::jsontoolkit::from(0)); } diff --git a/src/canonicalizer/rules/implicit_type_union.h b/src/canonicalizer/rules/implicit_type_union.h index 950bda21c..e1012a4e9 100644 --- a/src/canonicalizer/rules/implicit_type_union.h +++ b/src/canonicalizer/rules/implicit_type_union.h @@ -34,16 +34,17 @@ namespace sourcemeta::jsonbinpack::canonicalizer { /// /// \f[B_{applicator} = \{ anyOf, allOf, oneOf, not, if, then, else \} \f] -class ImplicitTypeUnion final : public sourcemeta::alterschema::Rule { +class ImplicitTypeUnion final + : public sourcemeta::jsontoolkit::SchemaTransformRule { public: - ImplicitTypeUnion() : Rule("implicit_type_union"){}; + ImplicitTypeUnion() : SchemaTransformRule("implicit_type_union"){}; /// The rule condition - [[nodiscard]] auto - condition(const sourcemeta::jsontoolkit::Value &schema, - const std::string &draft, - const std::unordered_map &vocabularies, - const std::size_t) const -> bool override { + [[nodiscard]] auto condition(const sourcemeta::jsontoolkit::JSON &schema, + const std::string &dialect, + const std::set &vocabularies, + const sourcemeta::jsontoolkit::Pointer &) const + -> bool override { const bool has_core_blacklist{ vocabularies.contains( "https://json-schema.org/draft/2020-12/vocab/core") && @@ -59,16 +60,16 @@ class ImplicitTypeUnion final : public sourcemeta::alterschema::Rule { sourcemeta::jsontoolkit::defines_any(schema, {"type", "const", "enum"})}; - return draft == "https://json-schema.org/draft/2020-12/schema" && + return dialect == "https://json-schema.org/draft/2020-12/schema" && vocabularies.contains( "https://json-schema.org/draft/2020-12/vocab/validation") && - sourcemeta::jsontoolkit::is_object(schema) && !has_core_blacklist && + schema.is_object() && !has_core_blacklist && !has_applicator_blacklist && !has_validation_blacklist; } /// The rule transformation - auto transform(sourcemeta::jsontoolkit::JSON &document, - sourcemeta::jsontoolkit::Value &value) const -> void override { + auto transform(sourcemeta::jsontoolkit::SchemaTransformer &schema) const + -> void override { sourcemeta::jsontoolkit::JSON types{sourcemeta::jsontoolkit::make_array()}; // All possible JSON Schema types diff --git a/src/canonicalizer/rules/implicit_unit_multiple_of.h b/src/canonicalizer/rules/implicit_unit_multiple_of.h index 9fd8f5f1e..b5859dd1e 100644 --- a/src/canonicalizer/rules/implicit_unit_multiple_of.h +++ b/src/canonicalizer/rules/implicit_unit_multiple_of.h @@ -17,31 +17,29 @@ namespace sourcemeta::jsonbinpack::canonicalizer { /// \mapsto S \cup \{ multipleOf \mapsto 1 \} /// }\f] -class ImplicitUnitMultipleOf final : public sourcemeta::alterschema::Rule { +class ImplicitUnitMultipleOf final + : public sourcemeta::jsontoolkit::SchemaTransformRule { public: - ImplicitUnitMultipleOf() : Rule("implicit_unit_multiple_of"){}; + ImplicitUnitMultipleOf() : SchemaTransformRule("implicit_unit_multiple_of"){}; /// The rule condition - [[nodiscard]] auto - condition(const sourcemeta::jsontoolkit::Value &schema, - const std::string &draft, - const std::unordered_map &vocabularies, - const std::size_t) const -> bool override { - return draft == "https://json-schema.org/draft/2020-12/schema" && + [[nodiscard]] auto condition(const sourcemeta::jsontoolkit::JSON &schema, + const std::string &dialect, + const std::set &vocabularies, + const sourcemeta::jsontoolkit::Pointer &) const + -> bool override { + return dialect == "https://json-schema.org/draft/2020-12/schema" && vocabularies.contains( "https://json-schema.org/draft/2020-12/vocab/validation") && - sourcemeta::jsontoolkit::is_object(schema) && - sourcemeta::jsontoolkit::defines(schema, "type") && - sourcemeta::jsontoolkit::is_string( - sourcemeta::jsontoolkit::at(schema, "type")) && - sourcemeta::jsontoolkit::to_string( - sourcemeta::jsontoolkit::at(schema, "type")) == "integer" && - !sourcemeta::jsontoolkit::defines(schema, "multipleOf"); + schema.is_object() && schema.defines("type") && + sourcemeta::jsontoolkit::is_string(schema.at("type")) && + sourcemeta::jsontoolkit::to_string(schema.at("type")) == "integer" && + !schema.defines("multipleOf"); } /// The rule transformation - auto transform(sourcemeta::jsontoolkit::JSON &document, - sourcemeta::jsontoolkit::Value &value) const -> void override { + auto transform(sourcemeta::jsontoolkit::SchemaTransformer &schema) const + -> void override { sourcemeta::jsontoolkit::assign(document, value, "multipleOf", sourcemeta::jsontoolkit::from(1)); } diff --git a/src/canonicalizer/rules/max_contains_without_contains.h b/src/canonicalizer/rules/max_contains_without_contains.h index 18d3ab60d..e9456e59a 100644 --- a/src/canonicalizer/rules/max_contains_without_contains.h +++ b/src/canonicalizer/rules/max_contains_without_contains.h @@ -17,29 +17,30 @@ namespace sourcemeta::jsonbinpack::canonicalizer { /// \f[\frac{maxContains \in dom(S) \land contains \not\in dom(S) }{S \mapsto S /// \setminus \{ maxContains \} }\f] -class MaxContainsWithoutContains final : public sourcemeta::alterschema::Rule { +class MaxContainsWithoutContains final + : public sourcemeta::jsontoolkit::SchemaTransformRule { public: - MaxContainsWithoutContains() : Rule("max_contains_without_contains"){}; + MaxContainsWithoutContains() + : SchemaTransformRule("max_contains_without_contains"){}; /// The rule condition - [[nodiscard]] auto - condition(const sourcemeta::jsontoolkit::Value &schema, - const std::string &draft, - const std::unordered_map &vocabularies, - const std::size_t) const -> bool override { - return draft == "https://json-schema.org/draft/2020-12/schema" && + [[nodiscard]] auto condition(const sourcemeta::jsontoolkit::JSON &schema, + const std::string &dialect, + const std::set &vocabularies, + const sourcemeta::jsontoolkit::Pointer &) const + -> bool override { + return dialect == "https://json-schema.org/draft/2020-12/schema" && vocabularies.contains( "https://json-schema.org/draft/2020-12/vocab/applicator") && vocabularies.contains( "https://json-schema.org/draft/2020-12/vocab/validation") && - sourcemeta::jsontoolkit::is_object(schema) && - sourcemeta::jsontoolkit::defines(schema, "maxContains") && - !sourcemeta::jsontoolkit::defines(schema, "contains"); + schema.is_object() && schema.defines("maxContains") && + !schema.defines("contains"); } /// The rule transformation auto transform(sourcemeta::jsontoolkit::JSON &, - sourcemeta::jsontoolkit::Value &value) const -> void override { + sourcemeta::jsontoolkit::JSON &value) const -> void override { sourcemeta::jsontoolkit::erase(value, "maxContains"); } }; diff --git a/src/canonicalizer/rules/maximum_real_for_integer.h b/src/canonicalizer/rules/maximum_real_for_integer.h index 2f17920a8..bbd0abb06 100644 --- a/src/canonicalizer/rules/maximum_real_for_integer.h +++ b/src/canonicalizer/rules/maximum_real_for_integer.h @@ -16,33 +16,30 @@ namespace sourcemeta::jsonbinpack::canonicalizer { /// \mathbb{R}}{S \mapsto S \cup \{ maximum \mapsto \lfloor S.maximum \rfloor \} /// }\f] -class MaximumRealForInteger final : public sourcemeta::alterschema::Rule { +class MaximumRealForInteger final + : public sourcemeta::jsontoolkit::SchemaTransformRule { public: - MaximumRealForInteger() : Rule("maximum_real_for_integer"){}; + MaximumRealForInteger() : SchemaTransformRule("maximum_real_for_integer"){}; /// The rule condition - [[nodiscard]] auto - condition(const sourcemeta::jsontoolkit::Value &schema, - const std::string &draft, - const std::unordered_map &vocabularies, - const std::size_t) const -> bool override { - return draft == "https://json-schema.org/draft/2020-12/schema" && + [[nodiscard]] auto condition(const sourcemeta::jsontoolkit::JSON &schema, + const std::string &dialect, + const std::set &vocabularies, + const sourcemeta::jsontoolkit::Pointer &) const + -> bool override { + return dialect == "https://json-schema.org/draft/2020-12/schema" && vocabularies.contains( "https://json-schema.org/draft/2020-12/vocab/validation") && - sourcemeta::jsontoolkit::is_object(schema) && - sourcemeta::jsontoolkit::defines(schema, "type") && - sourcemeta::jsontoolkit::is_string( - sourcemeta::jsontoolkit::at(schema, "type")) && - sourcemeta::jsontoolkit::to_string( - sourcemeta::jsontoolkit::at(schema, "type")) == "integer" && - sourcemeta::jsontoolkit::defines(schema, "maximum") && - sourcemeta::jsontoolkit::is_real( - sourcemeta::jsontoolkit::at(schema, "maximum")); + schema.is_object() && schema.defines("type") && + sourcemeta::jsontoolkit::is_string(schema.at("type")) && + sourcemeta::jsontoolkit::to_string(schema.at("type")) == "integer" && + schema.defines("maximum") && + sourcemeta::jsontoolkit::is_real(schema.at("maximum")); } /// The rule transformation - auto transform(sourcemeta::jsontoolkit::JSON &document, - sourcemeta::jsontoolkit::Value &value) const -> void override { + auto transform(sourcemeta::jsontoolkit::SchemaTransformer &schema) const + -> void override { const auto current{sourcemeta::jsontoolkit::to_real( sourcemeta::jsontoolkit::at(value, "maximum"))}; const auto new_value{static_cast(std::floor(current))}; diff --git a/src/canonicalizer/rules/min_contains_without_contains.h b/src/canonicalizer/rules/min_contains_without_contains.h index 6d9b822ae..991cd060f 100644 --- a/src/canonicalizer/rules/min_contains_without_contains.h +++ b/src/canonicalizer/rules/min_contains_without_contains.h @@ -17,29 +17,30 @@ namespace sourcemeta::jsonbinpack::canonicalizer { /// \f[\frac{minContains \in dom(S) \land contains \not\in dom(S) }{S \mapsto S /// \setminus \{ minContains \} }\f] -class MinContainsWithoutContains final : public sourcemeta::alterschema::Rule { +class MinContainsWithoutContains final + : public sourcemeta::jsontoolkit::SchemaTransformRule { public: - MinContainsWithoutContains() : Rule("min_contains_without_contains"){}; + MinContainsWithoutContains() + : SchemaTransformRule("min_contains_without_contains"){}; /// The rule condition - [[nodiscard]] auto - condition(const sourcemeta::jsontoolkit::Value &schema, - const std::string &draft, - const std::unordered_map &vocabularies, - const std::size_t) const -> bool override { - return draft == "https://json-schema.org/draft/2020-12/schema" && + [[nodiscard]] auto condition(const sourcemeta::jsontoolkit::JSON &schema, + const std::string &dialect, + const std::set &vocabularies, + const sourcemeta::jsontoolkit::Pointer &) const + -> bool override { + return dialect == "https://json-schema.org/draft/2020-12/schema" && vocabularies.contains( "https://json-schema.org/draft/2020-12/vocab/applicator") && vocabularies.contains( "https://json-schema.org/draft/2020-12/vocab/validation") && - sourcemeta::jsontoolkit::is_object(schema) && - sourcemeta::jsontoolkit::defines(schema, "minContains") && - !sourcemeta::jsontoolkit::defines(schema, "contains"); + schema.is_object() && schema.defines("minContains") && + !schema.defines("contains"); } /// The rule transformation auto transform(sourcemeta::jsontoolkit::JSON &, - sourcemeta::jsontoolkit::Value &value) const -> void override { + sourcemeta::jsontoolkit::JSON &value) const -> void override { sourcemeta::jsontoolkit::erase(value, "minContains"); } }; diff --git a/src/canonicalizer/rules/min_properties_required_tautology.h b/src/canonicalizer/rules/min_properties_required_tautology.h index ff8cd8788..e0148aee2 100644 --- a/src/canonicalizer/rules/min_properties_required_tautology.h +++ b/src/canonicalizer/rules/min_properties_required_tautology.h @@ -13,7 +13,7 @@ namespace sourcemeta::jsonbinpack::canonicalizer { /// `minProperties` keyword from the Validation vocabulary declares *how many* /// properties must be present in the object instance. Therefore, assuming the /// `required` keyword does not contain duplicates (as handled by @ref -/// sourcemeta::jsonbinpack::canonicalizer::DuplicateRequiredValues), the +/// sourcemeta::jsonbinpack::canonicalizer::DuplicateRequiredJSONs), the /// integer in `minProperties` has to be at least equal to the length of the /// `required` array. /// @@ -22,37 +22,33 @@ namespace sourcemeta::jsonbinpack::canonicalizer { /// }\f] class MinPropertiesRequiredTautology final - : public sourcemeta::alterschema::Rule { + : public sourcemeta::jsontoolkit::SchemaTransformRule { public: MinPropertiesRequiredTautology() - : Rule("min_properties_required_tautology"){}; + : SchemaTransformRule("min_properties_required_tautology"){}; /// The rule condition - [[nodiscard]] auto - condition(const sourcemeta::jsontoolkit::Value &schema, - const std::string &draft, - const std::unordered_map &vocabularies, - const std::size_t) const -> bool override { - return draft == "https://json-schema.org/draft/2020-12/schema" && + [[nodiscard]] auto condition(const sourcemeta::jsontoolkit::JSON &schema, + const std::string &dialect, + const std::set &vocabularies, + const sourcemeta::jsontoolkit::Pointer &) const + -> bool override { + return dialect == "https://json-schema.org/draft/2020-12/schema" && vocabularies.contains( "https://json-schema.org/draft/2020-12/vocab/validation") && - sourcemeta::jsontoolkit::is_object(schema) && - sourcemeta::jsontoolkit::defines(schema, "minProperties") && - sourcemeta::jsontoolkit::is_integer( - sourcemeta::jsontoolkit::at(schema, "minProperties")) && - sourcemeta::jsontoolkit::defines(schema, "required") && - sourcemeta::jsontoolkit::is_array( - sourcemeta::jsontoolkit::at(schema, "required")) && - is_unique(sourcemeta::jsontoolkit::at(schema, "required")) && - sourcemeta::jsontoolkit::size( - sourcemeta::jsontoolkit::at(schema, "required")) > + schema.is_object() && schema.defines("minProperties") && + sourcemeta::jsontoolkit::is_integer(schema.at("minProperties")) && + schema.defines("required") && + sourcemeta::jsontoolkit::is_array(schema.at("required")) && + is_unique(schema.at("required")) && + sourcemeta::jsontoolkit::size(schema.at("required")) > static_cast(sourcemeta::jsontoolkit::to_integer( - sourcemeta::jsontoolkit::at(schema, "minProperties"))); + schema.at("minProperties"))); } /// The rule transformation - auto transform(sourcemeta::jsontoolkit::JSON &document, - sourcemeta::jsontoolkit::Value &value) const -> void override { + auto transform(sourcemeta::jsontoolkit::SchemaTransformer &schema) const + -> void override { sourcemeta::jsontoolkit::assign( document, value, "minProperties", sourcemeta::jsontoolkit::from(sourcemeta::jsontoolkit::size( diff --git a/src/canonicalizer/rules/minimum_real_for_integer.h b/src/canonicalizer/rules/minimum_real_for_integer.h index d8fe166ef..b31e4e962 100644 --- a/src/canonicalizer/rules/minimum_real_for_integer.h +++ b/src/canonicalizer/rules/minimum_real_for_integer.h @@ -16,33 +16,30 @@ namespace sourcemeta::jsonbinpack::canonicalizer { /// \mathbb{R}}{S \mapsto S \cup \{ minimum \mapsto \lceil S.minimum \rceil \} /// }\f] -class MinimumRealForInteger final : public sourcemeta::alterschema::Rule { +class MinimumRealForInteger final + : public sourcemeta::jsontoolkit::SchemaTransformRule { public: - MinimumRealForInteger() : Rule("minimum_real_for_integer"){}; + MinimumRealForInteger() : SchemaTransformRule("minimum_real_for_integer"){}; /// The rule condition - [[nodiscard]] auto - condition(const sourcemeta::jsontoolkit::Value &schema, - const std::string &draft, - const std::unordered_map &vocabularies, - const std::size_t) const -> bool override { - return draft == "https://json-schema.org/draft/2020-12/schema" && + [[nodiscard]] auto condition(const sourcemeta::jsontoolkit::JSON &schema, + const std::string &dialect, + const std::set &vocabularies, + const sourcemeta::jsontoolkit::Pointer &) const + -> bool override { + return dialect == "https://json-schema.org/draft/2020-12/schema" && vocabularies.contains( "https://json-schema.org/draft/2020-12/vocab/validation") && - sourcemeta::jsontoolkit::is_object(schema) && - sourcemeta::jsontoolkit::defines(schema, "type") && - sourcemeta::jsontoolkit::is_string( - sourcemeta::jsontoolkit::at(schema, "type")) && - sourcemeta::jsontoolkit::to_string( - sourcemeta::jsontoolkit::at(schema, "type")) == "integer" && - sourcemeta::jsontoolkit::defines(schema, "minimum") && - sourcemeta::jsontoolkit::is_real( - sourcemeta::jsontoolkit::at(schema, "minimum")); + schema.is_object() && schema.defines("type") && + sourcemeta::jsontoolkit::is_string(schema.at("type")) && + sourcemeta::jsontoolkit::to_string(schema.at("type")) == "integer" && + schema.defines("minimum") && + sourcemeta::jsontoolkit::is_real(schema.at("minimum")); } /// The rule transformation - auto transform(sourcemeta::jsontoolkit::JSON &document, - sourcemeta::jsontoolkit::Value &value) const -> void override { + auto transform(sourcemeta::jsontoolkit::SchemaTransformer &schema) const + -> void override { const auto current{sourcemeta::jsontoolkit::to_real( sourcemeta::jsontoolkit::at(value, "minimum"))}; const auto new_value{static_cast(std::ceil(current))}; diff --git a/src/canonicalizer/rules/null_as_const.h b/src/canonicalizer/rules/null_as_const.h index 15d2bfde5..ea08ebc57 100644 --- a/src/canonicalizer/rules/null_as_const.h +++ b/src/canonicalizer/rules/null_as_const.h @@ -16,30 +16,27 @@ namespace sourcemeta::jsonbinpack::canonicalizer { /// \mapsto S \cup \{ const \mapsto null \} \setminus \{ type \} /// }\f] -class NullAsConst final : public sourcemeta::alterschema::Rule { +class NullAsConst final : public sourcemeta::jsontoolkit::SchemaTransformRule { public: - NullAsConst() : Rule("null_as_const"){}; + NullAsConst() : SchemaTransformRule("null_as_const"){}; /// The rule condition - [[nodiscard]] auto - condition(const sourcemeta::jsontoolkit::Value &schema, - const std::string &draft, - const std::unordered_map &vocabularies, - const std::size_t) const -> bool override { - return draft == "https://json-schema.org/draft/2020-12/schema" && + [[nodiscard]] auto condition(const sourcemeta::jsontoolkit::JSON &schema, + const std::string &dialect, + const std::set &vocabularies, + const sourcemeta::jsontoolkit::Pointer &) const + -> bool override { + return dialect == "https://json-schema.org/draft/2020-12/schema" && vocabularies.contains( "https://json-schema.org/draft/2020-12/vocab/applicator") && - sourcemeta::jsontoolkit::is_object(schema) && - sourcemeta::jsontoolkit::defines(schema, "type") && - sourcemeta::jsontoolkit::is_string( - sourcemeta::jsontoolkit::at(schema, "type")) && - sourcemeta::jsontoolkit::to_string( - sourcemeta::jsontoolkit::at(schema, "type")) == "null"; + schema.is_object() && schema.defines("type") && + sourcemeta::jsontoolkit::is_string(schema.at("type")) && + sourcemeta::jsontoolkit::to_string(schema.at("type")) == "null"; } /// The rule transformation - auto transform(sourcemeta::jsontoolkit::JSON &document, - sourcemeta::jsontoolkit::Value &value) const -> void override { + auto transform(sourcemeta::jsontoolkit::SchemaTransformer &schema) const + -> void override { sourcemeta::jsontoolkit::assign(document, value, "const", sourcemeta::jsontoolkit::from(nullptr)); sourcemeta::jsontoolkit::erase(value, "type"); diff --git a/src/canonicalizer/rules/then_else_without_if.h b/src/canonicalizer/rules/then_else_without_if.h index c7df845b9..f7c300aca 100644 --- a/src/canonicalizer/rules/then_else_without_if.h +++ b/src/canonicalizer/rules/then_else_without_if.h @@ -17,28 +17,27 @@ namespace sourcemeta::jsonbinpack::canonicalizer { /// }{S \mapsto S \setminus \{ then, else \} /// }\f] -class ThenElseWithoutIf final : public sourcemeta::alterschema::Rule { +class ThenElseWithoutIf final + : public sourcemeta::jsontoolkit::SchemaTransformRule { public: - ThenElseWithoutIf() : Rule("then_else_without_if"){}; + ThenElseWithoutIf() : SchemaTransformRule("then_else_without_if"){}; /// The rule condition - [[nodiscard]] auto - condition(const sourcemeta::jsontoolkit::Value &schema, - const std::string &draft, - const std::unordered_map &vocabularies, - const std::size_t) const -> bool override { - return draft == "https://json-schema.org/draft/2020-12/schema" && + [[nodiscard]] auto condition(const sourcemeta::jsontoolkit::JSON &schema, + const std::string &dialect, + const std::set &vocabularies, + const sourcemeta::jsontoolkit::Pointer &) const + -> bool override { + return dialect == "https://json-schema.org/draft/2020-12/schema" && vocabularies.contains( "https://json-schema.org/draft/2020-12/vocab/applicator") && - sourcemeta::jsontoolkit::is_object(schema) && - !sourcemeta::jsontoolkit::defines(schema, "if") && - (sourcemeta::jsontoolkit::defines(schema, "then") || - sourcemeta::jsontoolkit::defines(schema, "else")); + schema.is_object() && !schema.defines("if") && + (schema.defines("then") || schema.defines("else")); } /// The rule transformation auto transform(sourcemeta::jsontoolkit::JSON &, - sourcemeta::jsontoolkit::Value &value) const -> void override { + sourcemeta::jsontoolkit::JSON &value) const -> void override { sourcemeta::jsontoolkit::erase(value, "then"); sourcemeta::jsontoolkit::erase(value, "else"); } diff --git a/src/canonicalizer/rules/type_union_anyof.h b/src/canonicalizer/rules/type_union_anyof.h index 68a4aefd7..3c47b830a 100644 --- a/src/canonicalizer/rules/type_union_anyof.h +++ b/src/canonicalizer/rules/type_union_anyof.h @@ -2,30 +2,29 @@ namespace sourcemeta::jsonbinpack::canonicalizer { // TODO: Document this rule /// @ingroup canonicalizer_rules_heterogeneous -class TypeUnionAnyOf final : public sourcemeta::alterschema::Rule { +class TypeUnionAnyOf final + : public sourcemeta::jsontoolkit::SchemaTransformRule { public: - TypeUnionAnyOf() : Rule("type_union_anyof"){}; + TypeUnionAnyOf() : SchemaTransformRule("type_union_anyof"){}; /// The rule condition - [[nodiscard]] auto - condition(const sourcemeta::jsontoolkit::Value &schema, - const std::string &draft, - const std::unordered_map &vocabularies, - const std::size_t) const -> bool override { - return draft == "https://json-schema.org/draft/2020-12/schema" && + [[nodiscard]] auto condition(const sourcemeta::jsontoolkit::JSON &schema, + const std::string &dialect, + const std::set &vocabularies, + const sourcemeta::jsontoolkit::Pointer &) const + -> bool override { + return dialect == "https://json-schema.org/draft/2020-12/schema" && vocabularies.contains( "https://json-schema.org/draft/2020-12/vocab/validation") && vocabularies.contains( "https://json-schema.org/draft/2020-12/vocab/applicator") && - sourcemeta::jsontoolkit::is_object(schema) && - sourcemeta::jsontoolkit::defines(schema, "type") && - sourcemeta::jsontoolkit::is_array( - sourcemeta::jsontoolkit::at(schema, "type")); + schema.is_object() && schema.defines("type") && + sourcemeta::jsontoolkit::is_array(schema.at("type")); } /// The rule transformation - auto transform(sourcemeta::jsontoolkit::JSON &document, - sourcemeta::jsontoolkit::Value &value) const -> void override { + auto transform(sourcemeta::jsontoolkit::SchemaTransformer &schema) const + -> void override { sourcemeta::jsontoolkit::JSON disjunctors{ sourcemeta::jsontoolkit::make_array()}; for (const auto &type : sourcemeta::jsontoolkit::array_iterator( diff --git a/src/canonicalizer/rules/unsatisfiable_max_contains.h b/src/canonicalizer/rules/unsatisfiable_max_contains.h index f0e6956e7..e28a2d7d7 100644 --- a/src/canonicalizer/rules/unsatisfiable_max_contains.h +++ b/src/canonicalizer/rules/unsatisfiable_max_contains.h @@ -16,35 +16,32 @@ namespace sourcemeta::jsonbinpack::canonicalizer { /// \f[\frac{\{ maxContains, maxItems \} \subseteq dom(S) \land S.maxContains /// \geq S.maxItems }{S \mapsto S \setminus \{ maxContains \} }\f] -class UnsatisfiableMaxContains final : public sourcemeta::alterschema::Rule { +class UnsatisfiableMaxContains final + : public sourcemeta::jsontoolkit::SchemaTransformRule { public: - UnsatisfiableMaxContains() : Rule("unsatisfiable_max_contains"){}; + UnsatisfiableMaxContains() + : SchemaTransformRule("unsatisfiable_max_contains"){}; /// The rule condition - [[nodiscard]] auto - condition(const sourcemeta::jsontoolkit::Value &schema, - const std::string &draft, - const std::unordered_map &vocabularies, - const std::size_t) const -> bool override { - return draft == "https://json-schema.org/draft/2020-12/schema" && + [[nodiscard]] auto condition(const sourcemeta::jsontoolkit::JSON &schema, + const std::string &dialect, + const std::set &vocabularies, + const sourcemeta::jsontoolkit::Pointer &) const + -> bool override { + return dialect == "https://json-schema.org/draft/2020-12/schema" && vocabularies.contains( "https://json-schema.org/draft/2020-12/vocab/validation") && - sourcemeta::jsontoolkit::is_object(schema) && - sourcemeta::jsontoolkit::defines(schema, "maxContains") && - sourcemeta::jsontoolkit::is_integer( - sourcemeta::jsontoolkit::at(schema, "maxContains")) && - sourcemeta::jsontoolkit::defines(schema, "maxItems") && - sourcemeta::jsontoolkit::is_integer( - sourcemeta::jsontoolkit::at(schema, "maxItems")) && - sourcemeta::jsontoolkit::to_integer( - sourcemeta::jsontoolkit::at(schema, "maxContains")) >= - sourcemeta::jsontoolkit::to_integer( - sourcemeta::jsontoolkit::at(schema, "maxItems")); + schema.is_object() && schema.defines("maxContains") && + sourcemeta::jsontoolkit::is_integer(schema.at("maxContains")) && + schema.defines("maxItems") && + sourcemeta::jsontoolkit::is_integer(schema.at("maxItems")) && + sourcemeta::jsontoolkit::to_integer(schema.at("maxContains")) >= + sourcemeta::jsontoolkit::to_integer(schema.at("maxItems")); } /// The rule transformation auto transform(sourcemeta::jsontoolkit::JSON &, - sourcemeta::jsontoolkit::Value &value) const -> void override { + sourcemeta::jsontoolkit::JSON &value) const -> void override { sourcemeta::jsontoolkit::erase(value, "maxContains"); } }; diff --git a/src/canonicalizer/utils.h b/src/canonicalizer/utils.h index dafd21169..d1e6945c8 100644 --- a/src/canonicalizer/utils.h +++ b/src/canonicalizer/utils.h @@ -1,65 +1,48 @@ #ifndef SOURCEMETA_JSONBINPACK_CANONICALIZER_UTILS_H_ #define SOURCEMETA_JSONBINPACK_CANONICALIZER_UTILS_H_ -#include -#include +#include +#include -#include // std::sort, std::unique, std::all_of -#include // std::string -#include // std::unordered_map +#include // std::sort, std::unique, std::all_of +#include // std::set +#include // std::string namespace sourcemeta::jsonbinpack::canonicalizer { // We don't have to check for "type: boolean" as that type // is collapsed to an enum by other canonicalizer rules. -auto is_boolean_schema( - const sourcemeta::jsontoolkit::Value &schema, - const std::unordered_map &vocabularies) -> bool { +auto is_boolean_schema(const sourcemeta::jsontoolkit::JSON &schema, + const std::set &vocabularies) -> bool { // If it is an enumeration of booleans return vocabularies.contains( "https://json-schema.org/draft/2020-12/vocab/validation") && - sourcemeta::jsontoolkit::is_object(schema) && - sourcemeta::jsontoolkit::defines(schema, "enum") && - sourcemeta::jsontoolkit::is_array( - sourcemeta::jsontoolkit::at(schema, "enum")) && - std::all_of(sourcemeta::jsontoolkit::cbegin_array( - sourcemeta::jsontoolkit::at(schema, "enum")), - sourcemeta::jsontoolkit::cend_array( - sourcemeta::jsontoolkit::at(schema, "enum")), - [](const auto &element) { - return sourcemeta::jsontoolkit::is_boolean(element); - }); + schema.is_object() && schema.defines("enum") && + schema.at("enum").is_array() && + std::all_of(schema.at("enum").as_array().cbegin(), + schema.at("enum").as_array().cend(), + [](const auto &element) { return element.is_boolean(); }); } // We don't have to check for "type: null" as that type // is collapsed to an enum by other canonicalizer rules. -auto is_null_schema(const sourcemeta::jsontoolkit::Value &schema, - const std::unordered_map &vocabularies) - -> bool { +auto is_null_schema(const sourcemeta::jsontoolkit::JSON &schema, + const std::set &vocabularies) -> bool { // If it is an enumeration of nulls return vocabularies.contains( "https://json-schema.org/draft/2020-12/vocab/validation") && - sourcemeta::jsontoolkit::is_object(schema) && - sourcemeta::jsontoolkit::defines(schema, "enum") && - sourcemeta::jsontoolkit::is_array( - sourcemeta::jsontoolkit::at(schema, "enum")) && - std::all_of(sourcemeta::jsontoolkit::cbegin_array( - sourcemeta::jsontoolkit::at(schema, "enum")), - sourcemeta::jsontoolkit::cend_array( - sourcemeta::jsontoolkit::at(schema, "enum")), - [](const auto &element) { - return sourcemeta::jsontoolkit::is_null(element); - }); + schema.is_object() && schema.defines("enum") && + schema.at("enum").is_array() && + std::all_of(schema.at("enum").as_array().cbegin(), + schema.at("enum").as_array().cend(), + [](const auto &element) { return element.is_null(); }); } -auto is_unique(const sourcemeta::jsontoolkit::Value &document) -> bool { - auto copy{sourcemeta::jsontoolkit::from(document)}; - std::sort(sourcemeta::jsontoolkit::begin_array(copy), - sourcemeta::jsontoolkit::end_array(copy), - sourcemeta::jsontoolkit::compare); - return std::unique(sourcemeta::jsontoolkit::begin_array(copy), - sourcemeta::jsontoolkit::end_array(copy)) == - sourcemeta::jsontoolkit::end_array(copy); +auto is_unique(const sourcemeta::jsontoolkit::JSON &document) -> bool { + auto copy{document}; + std::sort(copy.as_array().begin(), copy.as_array().end()); + return std::unique(copy.as_array().begin(), copy.as_array().end()) == + copy.as_array().end(); } } // namespace sourcemeta::jsonbinpack::canonicalizer diff --git a/src/decoder/CMakeLists.txt b/src/decoder/CMakeLists.txt index 874e3bde0..c5549899f 100644 --- a/src/decoder/CMakeLists.txt +++ b/src/decoder/CMakeLists.txt @@ -1,12 +1,16 @@ -add_library(sourcemeta_jsonbinpack_decoder INTERFACE - include/jsonbinpack/decoder/varint.h - include/jsonbinpack/decoder/zigzag.h - include/jsonbinpack/decoder/basic_decoder.h - include/jsonbinpack/decoder/decoder.h) -target_include_directories(sourcemeta_jsonbinpack_decoder INTERFACE - "${CMAKE_CURRENT_SOURCE_DIR}/include") -target_link_libraries(sourcemeta_jsonbinpack_decoder INTERFACE sourcemeta::jsontoolkit::json) -target_link_libraries(sourcemeta_jsonbinpack_decoder INTERFACE sourcemeta_jsonbinpack_numeric) -target_link_libraries(sourcemeta_jsonbinpack_decoder INTERFACE sourcemeta_jsonbinpack_encoding) -set_target_properties(sourcemeta_jsonbinpack_decoder - PROPERTIES FOLDER "JSON BinPack/Decoder") +noa_library(NAMESPACE sourcemeta PROJECT jsonbinpack NAME decoder + FOLDER "JSON BinPack/Decoder" + PRIVATE_HEADERS varint.h zigzag.h basic.h) + +if(JSONBINPACK_INSTALL) + noa_library_install(NAMESPACE sourcemeta PROJECT jsonbinpack NAME decoder) +endif() + +sourcemeta_jsonbinpack_add_compile_options(INTERFACE sourcemeta_jsonbinpack_decoder) + +target_link_libraries(sourcemeta_jsonbinpack_decoder INTERFACE + sourcemeta::jsontoolkit::json) +target_link_libraries(sourcemeta_jsonbinpack_decoder INTERFACE + sourcemeta::jsonbinpack::numeric) +target_link_libraries(sourcemeta_jsonbinpack_decoder INTERFACE + sourcemeta::jsonbinpack::encoding) diff --git a/src/decoder/include/jsonbinpack/decoder/decoder.h b/src/decoder/include/sourcemeta/jsonbinpack/decoder.h similarity index 79% rename from src/decoder/include/jsonbinpack/decoder/decoder.h rename to src/decoder/include/sourcemeta/jsonbinpack/decoder.h index 1b827ddd2..686db6a41 100644 --- a/src/decoder/include/jsonbinpack/decoder/decoder.h +++ b/src/decoder/include/sourcemeta/jsonbinpack/decoder.h @@ -1,16 +1,16 @@ -#ifndef SOURCEMETA_JSONBINPACK_DECODER_DECODER_H_ -#define SOURCEMETA_JSONBINPACK_DECODER_DECODER_H_ +#ifndef SOURCEMETA_JSONBINPACK_DECODER_H_ +#define SOURCEMETA_JSONBINPACK_DECODER_H_ /// @defgroup decoder Decoder /// @brief A set of procedures to deserialize a bit-string given a Encoding /// Schema -#include -#include -#include -#include -#include -#include +#include +#include +#include +#include +#include +#include #include // assert #include // std::pow, std::abs @@ -80,16 +80,16 @@ class Decoder : private BasicDecoder { static_cast(closest_minimum) * options.multiplier}; // We trust the encoder that the data we are seeing // corresponds to a valid 64-bit signed integer. - return sourcemeta::jsontoolkit::from(static_cast( - (byte * options.multiplier) + closest_minimum_multiple)); + return sourcemeta::jsontoolkit::JSON{static_cast( + (byte * options.multiplier) + closest_minimum_multiple)}; } else { const std::uint64_t closest_minimum_multiple{ static_cast(std::abs(closest_minimum)) * options.multiplier}; // We trust the encoder that the data we are seeing // corresponds to a valid 64-bit signed integer. - return sourcemeta::jsontoolkit::from(static_cast( - (byte * options.multiplier) - closest_minimum_multiple)); + return sourcemeta::jsontoolkit::JSON{static_cast( + (byte * options.multiplier) - closest_minimum_multiple)}; } } @@ -103,18 +103,18 @@ class Decoder : private BasicDecoder { static_cast(closest_minimum) * options.multiplier}; // We trust the encoder that the data we are seeing // corresponds to a valid 64-bit signed integer. - return sourcemeta::jsontoolkit::from( + return sourcemeta::jsontoolkit::JSON{ static_cast((this->get_varint() * options.multiplier) + - closest_minimum_multiple)); + closest_minimum_multiple)}; } else { const std::uint64_t closest_minimum_multiple{ static_cast(std::abs(closest_minimum)) * options.multiplier}; // We trust the encoder that the data we are seeing // corresponds to a valid 64-bit signed integer. - return sourcemeta::jsontoolkit::from( + return sourcemeta::jsontoolkit::JSON{ static_cast((this->get_varint() * options.multiplier) - - closest_minimum_multiple)); + closest_minimum_multiple)}; } } @@ -129,20 +129,20 @@ class Decoder : private BasicDecoder { static_cast(closest_maximum) * options.multiplier}; // We trust the encoder that the data we are seeing // corresponds to a valid 64-bit signed integer. - return sourcemeta::jsontoolkit::from( + return sourcemeta::jsontoolkit::JSON{static_cast( -(static_cast(this->get_varint() * options.multiplier)) + - closest_maximum_multiple); + closest_maximum_multiple)}; } else { const std::uint64_t closest_maximum_multiple{ static_cast(std::abs(closest_maximum)) * options.multiplier}; // We trust the encoder that the data we are seeing // corresponds to a valid 64-bit signed integer. - return sourcemeta::jsontoolkit::from(static_cast( + return sourcemeta::jsontoolkit::JSON{static_cast( -(static_cast(this->get_varint() * options.multiplier)) - - closest_maximum_multiple)); + closest_maximum_multiple)}; } } @@ -152,8 +152,8 @@ class Decoder : private BasicDecoder { assert(options.multiplier > 0); // We trust the encoder that the data we are seeing // corresponds to a valid 64-bit signed integer. - return sourcemeta::jsontoolkit::from(static_cast( - this->get_varint_zigzag() * options.multiplier)); + return sourcemeta::jsontoolkit::JSON{static_cast( + this->get_varint_zigzag() * options.multiplier)}; } /// @} @@ -167,7 +167,7 @@ class Decoder : private BasicDecoder { const std::int64_t digits{this->get_varint_zigzag()}; const std::uint64_t point{this->get_varint()}; const double divisor{std::pow(10, static_cast(point))}; - return sourcemeta::jsontoolkit::from(static_cast(digits) / divisor); + return sourcemeta::jsontoolkit::JSON{static_cast(digits) / divisor}; } /// @} @@ -182,7 +182,7 @@ class Decoder : private BasicDecoder { assert(is_byte(options.choices.size())); const std::uint8_t index{this->get_byte()}; assert(options.choices.size() > index); - return sourcemeta::jsontoolkit::from(options.choices[index]); + return sourcemeta::jsontoolkit::JSON{options.choices[index]}; } auto LARGE_CHOICE_INDEX(const LARGE_CHOICE_INDEX &options) @@ -190,7 +190,7 @@ class Decoder : private BasicDecoder { assert(!options.choices.empty()); const std::uint64_t index{this->get_varint()}; assert(options.choices.size() > index); - return sourcemeta::jsontoolkit::from(options.choices[index]); + return sourcemeta::jsontoolkit::JSON{options.choices[index]}; } auto TOP_LEVEL_BYTE_CHOICE_INDEX(const TOP_LEVEL_BYTE_CHOICE_INDEX &options) @@ -198,17 +198,17 @@ class Decoder : private BasicDecoder { assert(!options.choices.empty()); assert(is_byte(options.choices.size())); if (!this->has_more_data()) { - return sourcemeta::jsontoolkit::from(options.choices.front()); + return sourcemeta::jsontoolkit::JSON{options.choices.front()}; } else { const std::uint16_t index{ static_cast(this->get_byte() + 1)}; assert(options.choices.size() > index); - return sourcemeta::jsontoolkit::from(options.choices[index]); + return sourcemeta::jsontoolkit::JSON{options.choices[index]}; } } auto CONST_NONE(const CONST_NONE &options) -> sourcemeta::jsontoolkit::JSON { - return sourcemeta::jsontoolkit::from(options.value); + return sourcemeta::jsontoolkit::JSON{options.value}; } /// @} @@ -219,7 +219,7 @@ class Decoder : private BasicDecoder { auto UTF8_STRING_NO_LENGTH(const UTF8_STRING_NO_LENGTH &options) -> sourcemeta::jsontoolkit::JSON { - return sourcemeta::jsontoolkit::from(this->get_string_utf8(options.size)); + return sourcemeta::jsontoolkit::JSON{this->get_string_utf8(options.size)}; } auto FLOOR_VARINT_PREFIX_UTF8_STRING_SHARED( @@ -234,8 +234,7 @@ class Decoder : private BasicDecoder { if (is_shared) { const std::uint64_t position{this->position()}; const std::uint64_t current{this->rewind(this->get_varint(), position)}; - sourcemeta::jsontoolkit::JSON string{ - sourcemeta::jsontoolkit::from(this->get_string_utf8(length))}; + sourcemeta::jsontoolkit::JSON string{this->get_string_utf8(length)}; this->seek(current); return string; } else { @@ -255,7 +254,7 @@ class Decoder : private BasicDecoder { if (is_shared) { const std::uint64_t position{this->position()}; const std::uint64_t current{this->rewind(this->get_varint(), position)}; - sourcemeta::jsontoolkit::JSON string{UTF8_STRING_NO_LENGTH({length})}; + sourcemeta::jsontoolkit::JSON string = UTF8_STRING_NO_LENGTH({length}); this->seek(current); return string; } else { @@ -277,7 +276,7 @@ class Decoder : private BasicDecoder { if (is_shared) { const std::uint64_t position{this->position()}; const std::uint64_t current{this->rewind(this->get_varint(), position)}; - sourcemeta::jsontoolkit::JSON string{UTF8_STRING_NO_LENGTH({length})}; + sourcemeta::jsontoolkit::JSON string = UTF8_STRING_NO_LENGTH({length}); this->seek(current); return string; } else { @@ -305,7 +304,7 @@ class Decoder : private BasicDecoder { output << "-"; output << std::setw(2) << static_cast(day); - return sourcemeta::jsontoolkit::from(output.str()); + return sourcemeta::jsontoolkit::JSON{output.str()}; } auto PREFIX_VARINT_LENGTH_STRING_SHARED( @@ -315,12 +314,12 @@ class Decoder : private BasicDecoder { if (prefix == 0) { const std::uint64_t position{this->position()}; const std::uint64_t current{this->rewind(this->get_varint(), position)}; - sourcemeta::jsontoolkit::JSON string{ - PREFIX_VARINT_LENGTH_STRING_SHARED(options)}; + sourcemeta::jsontoolkit::JSON string = + PREFIX_VARINT_LENGTH_STRING_SHARED(options); this->seek(current); return string; } else { - return sourcemeta::jsontoolkit::from(this->get_string_utf8(prefix - 1)); + return sourcemeta::jsontoolkit::JSON{this->get_string_utf8(prefix - 1)}; } } @@ -338,15 +337,16 @@ class Decoder : private BasicDecoder { auto FIXED_TYPED_ARRAY(const FIXED_TYPED_ARRAY &options) -> sourcemeta::jsontoolkit::JSON { const auto prefix_encodings{options.prefix_encodings.size()}; - sourcemeta::jsontoolkit::JSON result{sourcemeta::jsontoolkit::make_array()}; + sourcemeta::jsontoolkit::JSON result = + sourcemeta::jsontoolkit::JSON::make_array(); for (std::size_t index = 0; index < options.size; index++) { const Encoding &encoding{prefix_encodings > index ? options.prefix_encodings[index].value : options.encoding->value}; - sourcemeta::jsontoolkit::push_back(result, this->decode(encoding)); + result.push_back(this->decode(encoding)); } - assert(sourcemeta::jsontoolkit::size(result) == options.size); + assert(result.size() == options.size); return result; }; @@ -388,18 +388,16 @@ class Decoder : private BasicDecoder { auto FIXED_TYPED_ARBITRARY_OBJECT(const FIXED_TYPED_ARBITRARY_OBJECT &options) -> sourcemeta::jsontoolkit::JSON { - sourcemeta::jsontoolkit::JSON document{ - sourcemeta::jsontoolkit::make_object()}; + sourcemeta::jsontoolkit::JSON document = + sourcemeta::jsontoolkit::JSON::make_object(); for (std::size_t index = 0; index < options.size; index++) { - const sourcemeta::jsontoolkit::JSON key{ - this->decode(options.key_encoding->value)}; - assert(sourcemeta::jsontoolkit::is_string(key)); - sourcemeta::jsontoolkit::assign(document, - sourcemeta::jsontoolkit::to_string(key), - this->decode(options.encoding->value)); + const sourcemeta::jsontoolkit::JSON key = + this->decode(options.key_encoding->value); + assert(key.is_string()); + document.assign(key.to_string(), this->decode(options.encoding->value)); } - assert(sourcemeta::jsontoolkit::size(document) == options.size); + assert(document.size() == options.size); return document; }; @@ -407,18 +405,16 @@ class Decoder : private BasicDecoder { VARINT_TYPED_ARBITRARY_OBJECT(const VARINT_TYPED_ARBITRARY_OBJECT &options) -> sourcemeta::jsontoolkit::JSON { const std::uint64_t size{this->get_varint()}; - sourcemeta::jsontoolkit::JSON document{ - sourcemeta::jsontoolkit::make_object()}; + sourcemeta::jsontoolkit::JSON document = + sourcemeta::jsontoolkit::JSON::make_object(); for (std::size_t index = 0; index < size; index++) { - const sourcemeta::jsontoolkit::JSON key{ - this->decode(options.key_encoding->value)}; - assert(sourcemeta::jsontoolkit::is_string(key)); - sourcemeta::jsontoolkit::assign(document, - sourcemeta::jsontoolkit::to_string(key), - this->decode(options.encoding->value)); + const sourcemeta::jsontoolkit::JSON key = + this->decode(options.key_encoding->value); + assert(key.is_string()); + document.assign(key.to_string(), this->decode(options.encoding->value)); } - assert(sourcemeta::jsontoolkit::size(document) == size); + assert(document.size() == size); return document; }; @@ -439,30 +435,31 @@ class Decoder : private BasicDecoder { if (type == TYPE_OTHER) { switch (subtype) { case SUBTYPE_NULL: - return sourcemeta::jsontoolkit::from(nullptr); + return sourcemeta::jsontoolkit::JSON{nullptr}; case SUBTYPE_FALSE: - return sourcemeta::jsontoolkit::from(false); + return sourcemeta::jsontoolkit::JSON{false}; case SUBTYPE_TRUE: - return sourcemeta::jsontoolkit::from(true); + return sourcemeta::jsontoolkit::JSON{true}; case SUBTYPE_NUMBER: return this->DOUBLE_VARINT_TUPLE({}); case SUBTYPE_POSITIVE_INTEGER: - return sourcemeta::jsontoolkit::from(this->get_varint()); + return sourcemeta::jsontoolkit::JSON{ + static_cast(this->get_varint())}; case SUBTYPE_NEGATIVE_INTEGER: - return sourcemeta::jsontoolkit::from( - -static_cast(this->get_varint()) - 1); + return sourcemeta::jsontoolkit::JSON{ + -static_cast(this->get_varint()) - 1}; case SUBTYPE_LONG_STRING_BASE_EXPONENT_7: - return sourcemeta::jsontoolkit::from( - this->get_string_utf8(this->get_varint() + 128)); + return sourcemeta::jsontoolkit::JSON{ + this->get_string_utf8(this->get_varint() + 128)}; case SUBTYPE_LONG_STRING_BASE_EXPONENT_8: - return sourcemeta::jsontoolkit::from( - this->get_string_utf8(this->get_varint() + 256)); + return sourcemeta::jsontoolkit::JSON{ + this->get_string_utf8(this->get_varint() + 256)}; case SUBTYPE_LONG_STRING_BASE_EXPONENT_9: - return sourcemeta::jsontoolkit::from( - this->get_string_utf8(this->get_varint() + 512)); + return sourcemeta::jsontoolkit::JSON{ + this->get_string_utf8(this->get_varint() + 512)}; case SUBTYPE_LONG_STRING_BASE_EXPONENT_10: - return sourcemeta::jsontoolkit::from( - this->get_string_utf8(this->get_varint() + 1024)); + return sourcemeta::jsontoolkit::JSON{ + this->get_string_utf8(this->get_varint() + 1024)}; default: // We should never get here. If so, it is definitely a bug assert(false); @@ -471,12 +468,12 @@ class Decoder : private BasicDecoder { } else { switch (type) { case TYPE_POSITIVE_INTEGER_BYTE: - return sourcemeta::jsontoolkit::from(subtype > 0 ? subtype - 1 - : this->get_byte()); + return sourcemeta::jsontoolkit::JSON{subtype > 0 ? subtype - 1 + : this->get_byte()}; case TYPE_NEGATIVE_INTEGER_BYTE: - return sourcemeta::jsontoolkit::from( + return sourcemeta::jsontoolkit::JSON{ subtype > 0 ? static_cast(-subtype) - : static_cast(-this->get_byte() - 1)); + : static_cast(-this->get_byte() - 1)}; case TYPE_SHARED_STRING: { const auto length = subtype == 0 ? this->get_varint() - 1 + uint_max<5> * 2 @@ -484,19 +481,18 @@ class Decoder : private BasicDecoder { const std::uint64_t position{this->position()}; const std::uint64_t current{ this->rewind(this->get_varint(), position)}; - sourcemeta::jsontoolkit::JSON string{ - sourcemeta::jsontoolkit::from(this->get_string_utf8(length))}; + sourcemeta::jsontoolkit::JSON string{this->get_string_utf8(length)}; this->seek(current); return string; }; case TYPE_STRING: return subtype == 0 ? this->FLOOR_VARINT_PREFIX_UTF8_STRING_SHARED( {uint_max<5> * 2}) - : sourcemeta::jsontoolkit::from( - this->get_string_utf8(subtype - 1)); + : sourcemeta::jsontoolkit::JSON{ + this->get_string_utf8(subtype - 1)}; case TYPE_LONG_STRING: - return sourcemeta::jsontoolkit::from( - this->get_string_utf8(subtype + uint_max<5>)); + return sourcemeta::jsontoolkit::JSON{ + this->get_string_utf8(subtype + uint_max<5>)}; case TYPE_ARRAY: return subtype == 0 ? this->FIXED_TYPED_ARRAY( diff --git a/src/decoder/include/jsonbinpack/decoder/basic_decoder.h b/src/decoder/include/sourcemeta/jsonbinpack/decoder_basic.h similarity index 93% rename from src/decoder/include/jsonbinpack/decoder/basic_decoder.h rename to src/decoder/include/sourcemeta/jsonbinpack/decoder_basic.h index 4913c5e53..929f38a0a 100644 --- a/src/decoder/include/jsonbinpack/decoder/basic_decoder.h +++ b/src/decoder/include/sourcemeta/jsonbinpack/decoder_basic.h @@ -1,8 +1,8 @@ -#ifndef SOURCEMETA_JSONBINPACK_DECODER_BASIC_DECODER_H_ -#define SOURCEMETA_JSONBINPACK_DECODER_BASIC_DECODER_H_ +#ifndef SOURCEMETA_JSONBINPACK_DECODER_BASIC_H_ +#define SOURCEMETA_JSONBINPACK_DECODER_BASIC_H_ -#include -#include +#include +#include #include // assert #include // std::ceil diff --git a/src/decoder/include/jsonbinpack/decoder/varint.h b/src/decoder/include/sourcemeta/jsonbinpack/decoder_varint.h similarity index 100% rename from src/decoder/include/jsonbinpack/decoder/varint.h rename to src/decoder/include/sourcemeta/jsonbinpack/decoder_varint.h diff --git a/src/decoder/include/jsonbinpack/decoder/zigzag.h b/src/decoder/include/sourcemeta/jsonbinpack/decoder_zigzag.h similarity index 100% rename from src/decoder/include/jsonbinpack/decoder/zigzag.h rename to src/decoder/include/sourcemeta/jsonbinpack/decoder_zigzag.h diff --git a/src/encoder/CMakeLists.txt b/src/encoder/CMakeLists.txt index 262efc4df..a63f18e32 100644 --- a/src/encoder/CMakeLists.txt +++ b/src/encoder/CMakeLists.txt @@ -1,14 +1,16 @@ -add_library(sourcemeta_jsonbinpack_encoder INTERFACE - include/jsonbinpack/encoder/varint.h - include/jsonbinpack/encoder/zigzag.h - include/jsonbinpack/encoder/real.h - include/jsonbinpack/encoder/basic_encoder.h - include/jsonbinpack/encoder/encoder.h - include/jsonbinpack/encoder/context.h) -target_include_directories(sourcemeta_jsonbinpack_encoder INTERFACE - "${CMAKE_CURRENT_SOURCE_DIR}/include") -target_link_libraries(sourcemeta_jsonbinpack_encoder INTERFACE sourcemeta::jsontoolkit::json) -target_link_libraries(sourcemeta_jsonbinpack_encoder INTERFACE sourcemeta_jsonbinpack_numeric) -target_link_libraries(sourcemeta_jsonbinpack_encoder INTERFACE sourcemeta_jsonbinpack_encoding) -set_target_properties(sourcemeta_jsonbinpack_encoder - PROPERTIES FOLDER "JSON BinPack/Encoder") +noa_library(NAMESPACE sourcemeta PROJECT jsonbinpack NAME encoder + FOLDER "JSON BinPack/Encoder" + PRIVATE_HEADERS varint.h zigzag.h real.h basic.h context.h) + +if(JSONBINPACK_INSTALL) + noa_library_install(NAMESPACE sourcemeta PROJECT jsonbinpack NAME encoder) +endif() + +sourcemeta_jsonbinpack_add_compile_options(INTERFACE sourcemeta_jsonbinpack_encoder) + +target_link_libraries(sourcemeta_jsonbinpack_encoder INTERFACE + sourcemeta::jsontoolkit::json) +target_link_libraries(sourcemeta_jsonbinpack_encoder INTERFACE + sourcemeta::jsonbinpack::numeric) +target_link_libraries(sourcemeta_jsonbinpack_encoder INTERFACE + sourcemeta::jsonbinpack::encoding) diff --git a/src/encoder/include/jsonbinpack/encoder/encoder.h b/src/encoder/include/sourcemeta/jsonbinpack/encoder.h similarity index 73% rename from src/encoder/include/jsonbinpack/encoder/encoder.h rename to src/encoder/include/sourcemeta/jsonbinpack/encoder.h index 6dcc87252..6d53a64f5 100644 --- a/src/encoder/include/jsonbinpack/encoder/encoder.h +++ b/src/encoder/include/sourcemeta/jsonbinpack/encoder.h @@ -1,17 +1,17 @@ -#ifndef SOURCEMETA_JSONBINPACK_ENCODER_ENCODER_H_ -#define SOURCEMETA_JSONBINPACK_ENCODER_ENCODER_H_ +#ifndef SOURCEMETA_JSONBINPACK_ENCODER_H_ +#define SOURCEMETA_JSONBINPACK_ENCODER_H_ /// @defgroup encoder Encoder /// @brief A set of procedures to serialize a JSON document given a Encoding /// Schema -#include -#include -#include -#include -#include -#include -#include +#include +#include +#include +#include +#include +#include +#include #include // std::abs #include // std::uint8_t, std::uint16_t, std::int64_t, std::uint64_t @@ -29,7 +29,7 @@ class Encoder : private BasicEncoder { Encoder(std::basic_ostream &output) : BasicEncoder{output} {} - auto encode(const sourcemeta::jsontoolkit::Value &document, + auto encode(const sourcemeta::jsontoolkit::JSON &document, const Encoding &encoding) -> void { switch (encoding.index()) { #define HANDLE_ENCODING(index, name) \ @@ -71,13 +71,13 @@ class Encoder : private BasicEncoder { /// @{ auto BOUNDED_MULTIPLE_8BITS_ENUM_FIXED( - const sourcemeta::jsontoolkit::Value &document, + const sourcemeta::jsontoolkit::JSON &document, const BOUNDED_MULTIPLE_8BITS_ENUM_FIXED &options) -> void { - assert(sourcemeta::jsontoolkit::is_integer(document)); - const std::int64_t value{sourcemeta::jsontoolkit::to_integer(document)}; + assert(document.is_integer()); + const std::int64_t value{document.to_integer()}; assert(is_within(value, options.minimum, options.maximum)); assert(options.multiplier > 0); - assert(value % options.multiplier == 0); + assert(std::abs(value) % options.multiplier == 0); const std::int64_t enum_minimum{ divide_ceil(options.minimum, options.multiplier)}; #ifndef NDEBUG @@ -89,15 +89,14 @@ class Encoder : private BasicEncoder { static_cast((value / options.multiplier) - enum_minimum)); } - auto - FLOOR_MULTIPLE_ENUM_VARINT(const sourcemeta::jsontoolkit::Value &document, - const FLOOR_MULTIPLE_ENUM_VARINT &options) + auto FLOOR_MULTIPLE_ENUM_VARINT(const sourcemeta::jsontoolkit::JSON &document, + const FLOOR_MULTIPLE_ENUM_VARINT &options) -> void { - assert(sourcemeta::jsontoolkit::is_integer(document)); - const std::int64_t value{sourcemeta::jsontoolkit::to_integer(document)}; + assert(document.is_integer()); + const std::int64_t value{document.to_integer()}; assert(options.minimum <= value); assert(options.multiplier > 0); - assert(value % options.multiplier == 0); + assert(std::abs(value) % options.multiplier == 0); if (options.multiplier == 1) { return this->put_varint(value - options.minimum); } @@ -107,13 +106,13 @@ class Encoder : private BasicEncoder { } auto ROOF_MULTIPLE_MIRROR_ENUM_VARINT( - const sourcemeta::jsontoolkit::Value &document, + const sourcemeta::jsontoolkit::JSON &document, const ROOF_MULTIPLE_MIRROR_ENUM_VARINT &options) -> void { - assert(sourcemeta::jsontoolkit::is_integer(document)); - const std::int64_t value{sourcemeta::jsontoolkit::to_integer(document)}; + assert(document.is_integer()); + const std::int64_t value{document.to_integer()}; assert(value <= options.maximum); assert(options.multiplier > 0); - assert(value % options.multiplier == 0); + assert(std::abs(value) % options.multiplier == 0); if (options.multiplier == 1) { return this->put_varint(options.maximum - value); } @@ -123,12 +122,12 @@ class Encoder : private BasicEncoder { } auto ARBITRARY_MULTIPLE_ZIGZAG_VARINT( - const sourcemeta::jsontoolkit::Value &document, + const sourcemeta::jsontoolkit::JSON &document, const ARBITRARY_MULTIPLE_ZIGZAG_VARINT &options) -> void { - assert(sourcemeta::jsontoolkit::is_integer(document)); - const std::int64_t value{sourcemeta::jsontoolkit::to_integer(document)}; + assert(document.is_integer()); + const std::int64_t value{document.to_integer()}; assert(options.multiplier > 0); - assert(value % options.multiplier == 0); + assert(std::abs(value) % options.multiplier == 0); this->put_varint_zigzag(value / options.multiplier); } @@ -138,10 +137,10 @@ class Encoder : private BasicEncoder { /// @defgroup encoder_number Number /// @{ - auto DOUBLE_VARINT_TUPLE(const sourcemeta::jsontoolkit::Value &document, + auto DOUBLE_VARINT_TUPLE(const sourcemeta::jsontoolkit::JSON &document, const DOUBLE_VARINT_TUPLE &) -> void { - assert(sourcemeta::jsontoolkit::is_real(document)); - const auto value{sourcemeta::jsontoolkit::to_real(document)}; + assert(document.is_real()); + const auto value{document.to_real()}; std::uint64_t point_position; const std::int64_t integral{ encoder::real_digits(value, &point_position)}; @@ -155,7 +154,7 @@ class Encoder : private BasicEncoder { /// @defgroup encoder_enum Enumeration /// @{ - auto BYTE_CHOICE_INDEX(const sourcemeta::jsontoolkit::Value &document, + auto BYTE_CHOICE_INDEX(const sourcemeta::jsontoolkit::JSON &document, const BYTE_CHOICE_INDEX &options) -> void { assert(!options.choices.empty()); assert(is_byte(options.choices.size())); @@ -169,7 +168,7 @@ class Encoder : private BasicEncoder { this->put_byte(static_cast(cursor)); } - auto LARGE_CHOICE_INDEX(const sourcemeta::jsontoolkit::Value &document, + auto LARGE_CHOICE_INDEX(const sourcemeta::jsontoolkit::JSON &document, const LARGE_CHOICE_INDEX &options) -> void { assert(options.choices.size() > 0); const auto iterator{std::find_if( @@ -182,7 +181,7 @@ class Encoder : private BasicEncoder { } auto - TOP_LEVEL_BYTE_CHOICE_INDEX(const sourcemeta::jsontoolkit::Value &document, + TOP_LEVEL_BYTE_CHOICE_INDEX(const sourcemeta::jsontoolkit::JSON &document, const TOP_LEVEL_BYTE_CHOICE_INDEX &options) -> void { assert(options.choices.size() > 0); @@ -202,9 +201,9 @@ class Encoder : private BasicEncoder { auto CONST_NONE( #ifndef NDEBUG - const sourcemeta::jsontoolkit::Value &document, const CONST_NONE &options) + const sourcemeta::jsontoolkit::JSON &document, const CONST_NONE &options) #else - const sourcemeta::jsontoolkit::Value &, const CONST_NONE &) + const sourcemeta::jsontoolkit::JSON &, const CONST_NONE &) #endif -> void { assert(document == options.value); @@ -216,22 +215,20 @@ class Encoder : private BasicEncoder { /// @defgroup encoder_string String /// @{ - auto UTF8_STRING_NO_LENGTH(const sourcemeta::jsontoolkit::Value &document, + auto UTF8_STRING_NO_LENGTH(const sourcemeta::jsontoolkit::JSON &document, const UTF8_STRING_NO_LENGTH &options) -> void { - assert(sourcemeta::jsontoolkit::is_string(document)); - const std::basic_string value{ - sourcemeta::jsontoolkit::to_string(document)}; + assert(document.is_string()); + const std::basic_string value{document.to_string()}; this->put_string_utf8(value, options.size); } auto FLOOR_VARINT_PREFIX_UTF8_STRING_SHARED( - const sourcemeta::jsontoolkit::Value &document, + const sourcemeta::jsontoolkit::JSON &document, const FLOOR_VARINT_PREFIX_UTF8_STRING_SHARED &options) -> void { - assert(sourcemeta::jsontoolkit::is_string(document)); - const std::basic_string value{ - sourcemeta::jsontoolkit::to_string(document)}; + assert(document.is_string()); + const std::basic_string value{document.to_string()}; const auto size{value.size()}; - assert(sourcemeta::jsontoolkit::size(document) == size); + assert(document.size() == size); const bool is_shared{this->context().has(value, ContextType::Standalone)}; // (1) Write 0x00 if shared, else do nothing @@ -253,13 +250,12 @@ class Encoder : private BasicEncoder { } auto ROOF_VARINT_PREFIX_UTF8_STRING_SHARED( - const sourcemeta::jsontoolkit::Value &document, + const sourcemeta::jsontoolkit::JSON &document, const ROOF_VARINT_PREFIX_UTF8_STRING_SHARED &options) -> void { - assert(sourcemeta::jsontoolkit::is_string(document)); - const std::basic_string value{ - sourcemeta::jsontoolkit::to_string(document)}; + assert(document.is_string()); + const std::basic_string value{document.to_string()}; const auto size{value.size()}; - assert(sourcemeta::jsontoolkit::size(document) == size); + assert(document.size() == size); assert(size <= options.maximum); const bool is_shared{this->context().has(value, ContextType::Standalone)}; @@ -282,13 +278,12 @@ class Encoder : private BasicEncoder { } auto BOUNDED_8BIT_PREFIX_UTF8_STRING_SHARED( - const sourcemeta::jsontoolkit::Value &document, + const sourcemeta::jsontoolkit::JSON &document, const BOUNDED_8BIT_PREFIX_UTF8_STRING_SHARED &options) -> void { - assert(sourcemeta::jsontoolkit::is_string(document)); - const std::basic_string value{ - sourcemeta::jsontoolkit::to_string(document)}; + assert(document.is_string()); + const std::basic_string value{document.to_string()}; const auto size{value.size()}; - assert(sourcemeta::jsontoolkit::size(document) == size); + assert(document.size() == size); assert(options.minimum <= options.maximum); assert(is_byte(options.maximum - options.minimum + 1)); assert(is_within(size, options.minimum, options.maximum)); @@ -313,12 +308,12 @@ class Encoder : private BasicEncoder { } auto - RFC3339_DATE_INTEGER_TRIPLET(const sourcemeta::jsontoolkit::Value &document, + RFC3339_DATE_INTEGER_TRIPLET(const sourcemeta::jsontoolkit::JSON &document, const RFC3339_DATE_INTEGER_TRIPLET &) -> void { - assert(sourcemeta::jsontoolkit::is_string(document)); - const auto &value{sourcemeta::jsontoolkit::to_string(document)}; + assert(document.is_string()); + const auto &value{document.to_string()}; assert(value.size() == 10); - assert(sourcemeta::jsontoolkit::size(document) == value.size()); + assert(document.size() == value.size()); // As according to RFC3339: Internet Protocols MUST // generate four digit years in dates. @@ -337,11 +332,10 @@ class Encoder : private BasicEncoder { } auto PREFIX_VARINT_LENGTH_STRING_SHARED( - const sourcemeta::jsontoolkit::Value &document, + const sourcemeta::jsontoolkit::JSON &document, const PREFIX_VARINT_LENGTH_STRING_SHARED &) -> void { - assert(sourcemeta::jsontoolkit::is_string(document)); - const std::basic_string value{ - sourcemeta::jsontoolkit::to_string(document)}; + assert(document.is_string()); + const std::basic_string value{document.to_string()}; if (this->context().has(value, ContextType::PrefixLengthVarintPlusOne)) { const auto new_offset{this->position()}; @@ -354,7 +348,7 @@ class Encoder : private BasicEncoder { ContextType::PrefixLengthVarintPlusOne); } else { const auto size{value.size()}; - assert(sourcemeta::jsontoolkit::size(document) == size); + assert(document.size() == size); this->context().record(value, this->position(), ContextType::PrefixLengthVarintPlusOne); this->put_varint(size + 1); @@ -375,25 +369,25 @@ class Encoder : private BasicEncoder { /// @defgroup encoder_array Array /// @{ - auto FIXED_TYPED_ARRAY(const sourcemeta::jsontoolkit::Value &document, + auto FIXED_TYPED_ARRAY(const sourcemeta::jsontoolkit::JSON &document, const FIXED_TYPED_ARRAY &options) -> void { - assert(sourcemeta::jsontoolkit::is_array(document)); - assert(sourcemeta::jsontoolkit::size(document) == options.size); + assert(document.is_array()); + assert(document.size() == options.size); const auto prefix_encodings{options.prefix_encodings.size()}; - assert(prefix_encodings <= sourcemeta::jsontoolkit::size(document)); + assert(prefix_encodings <= document.size()); for (std::size_t index = 0; index < options.size; index++) { const Encoding &encoding{prefix_encodings > index ? options.prefix_encodings[index].value : options.encoding->value}; - this->encode(sourcemeta::jsontoolkit::at(document, index), encoding); + this->encode(document.at(index), encoding); } } - auto BOUNDED_8BITS_TYPED_ARRAY(const sourcemeta::jsontoolkit::Value &document, + auto BOUNDED_8BITS_TYPED_ARRAY(const sourcemeta::jsontoolkit::JSON &document, const BOUNDED_8BITS_TYPED_ARRAY &options) -> void { assert(options.maximum >= options.minimum); - const auto size{sourcemeta::jsontoolkit::size(document)}; + const auto size{document.size()}; assert(is_within(size, options.minimum, options.maximum)); assert(is_byte(options.maximum - options.minimum)); this->put_byte(static_cast(size - options.minimum)); @@ -401,18 +395,18 @@ class Encoder : private BasicEncoder { std::move(options.prefix_encodings)}); } - auto FLOOR_TYPED_ARRAY(const sourcemeta::jsontoolkit::Value &document, + auto FLOOR_TYPED_ARRAY(const sourcemeta::jsontoolkit::JSON &document, const FLOOR_TYPED_ARRAY &options) -> void { - const auto size{sourcemeta::jsontoolkit::size(document)}; + const auto size{document.size()}; assert(size >= options.minimum); this->put_varint(size - options.minimum); this->FIXED_TYPED_ARRAY(document, {size, std::move(options.encoding), std::move(options.prefix_encodings)}); } - auto ROOF_TYPED_ARRAY(const sourcemeta::jsontoolkit::Value &document, + auto ROOF_TYPED_ARRAY(const sourcemeta::jsontoolkit::JSON &document, const ROOF_TYPED_ARRAY &options) -> void { - const auto size{sourcemeta::jsontoolkit::size(document)}; + const auto size{document.size()}; assert(size <= options.maximum); this->put_varint(options.maximum - size); this->FIXED_TYPED_ARRAY(document, {size, std::move(options.encoding), @@ -426,35 +420,31 @@ class Encoder : private BasicEncoder { /// @{ auto - FIXED_TYPED_ARBITRARY_OBJECT(const sourcemeta::jsontoolkit::Value &document, + FIXED_TYPED_ARBITRARY_OBJECT(const sourcemeta::jsontoolkit::JSON &document, const FIXED_TYPED_ARBITRARY_OBJECT &options) -> void { - assert(sourcemeta::jsontoolkit::is_object(document)); - assert(sourcemeta::jsontoolkit::size(document) == options.size); - for (const auto &pair : - sourcemeta::jsontoolkit::object_iterator(document)) { - this->encode( - sourcemeta::jsontoolkit::from(sourcemeta::jsontoolkit::key(pair)), - options.key_encoding->value); - this->encode(sourcemeta::jsontoolkit::value(pair), - options.encoding->value); + assert(document.is_object()); + assert(document.size() == options.size); + + for (const auto &[key, value] : document.as_object()) { + this->encode(sourcemeta::jsontoolkit::JSON{key}, + options.key_encoding->value); + this->encode(value, options.encoding->value); } } auto - VARINT_TYPED_ARBITRARY_OBJECT(const sourcemeta::jsontoolkit::Value &document, + VARINT_TYPED_ARBITRARY_OBJECT(const sourcemeta::jsontoolkit::JSON &document, const VARINT_TYPED_ARBITRARY_OBJECT &options) -> void { - assert(sourcemeta::jsontoolkit::is_object(document)); - const auto size{sourcemeta::jsontoolkit::size(document)}; + assert(document.is_object()); + const auto size{document.size()}; this->put_varint(size); - for (const auto &pair : - sourcemeta::jsontoolkit::object_iterator(document)) { - this->encode( - sourcemeta::jsontoolkit::from(sourcemeta::jsontoolkit::key(pair)), - options.key_encoding->value); - this->encode(sourcemeta::jsontoolkit::value(pair), - options.encoding->value); + + for (const auto &[key, value] : document.as_object()) { + this->encode(sourcemeta::jsontoolkit::JSON{key}, + options.key_encoding->value); + this->encode(value, options.encoding->value); } } @@ -464,23 +454,23 @@ class Encoder : private BasicEncoder { /// @defgroup encoder_any Any /// @{ - auto ANY_PACKED_TYPE_TAG_BYTE_PREFIX( - const sourcemeta::jsontoolkit::Value &document, - const ANY_PACKED_TYPE_TAG_BYTE_PREFIX &) -> void { + auto + ANY_PACKED_TYPE_TAG_BYTE_PREFIX(const sourcemeta::jsontoolkit::JSON &document, + const ANY_PACKED_TYPE_TAG_BYTE_PREFIX &) + -> void { using namespace tag::ANY_PACKED_TYPE_TAG_BYTE_PREFIX; - if (sourcemeta::jsontoolkit::is_null(document)) { + if (document.is_null()) { this->put_byte(TYPE_OTHER | (SUBTYPE_NULL << type_size)); - } else if (sourcemeta::jsontoolkit::is_boolean(document)) { - const std::uint8_t subtype{sourcemeta::jsontoolkit::to_boolean(document) - ? SUBTYPE_TRUE - : SUBTYPE_FALSE}; + } else if (document.is_boolean()) { + const std::uint8_t subtype{document.to_boolean() ? SUBTYPE_TRUE + : SUBTYPE_FALSE}; this->put_byte(TYPE_OTHER | static_cast(subtype << type_size)); - } else if (sourcemeta::jsontoolkit::is_real(document)) { + } else if (document.is_real()) { this->put_byte(TYPE_OTHER | SUBTYPE_NUMBER << type_size); this->DOUBLE_VARINT_TUPLE(document, {}); - } else if (sourcemeta::jsontoolkit::is_integer(document)) { - const std::int64_t value{sourcemeta::jsontoolkit::to_integer(document)}; + } else if (document.is_integer()) { + const std::int64_t value{document.to_integer()}; const bool is_positive{value >= 0}; const std::uint64_t absolute{is_positive ? static_cast(value) @@ -503,10 +493,9 @@ class Encoder : private BasicEncoder { static_cast(subtype << type_size)); this->put_varint(absolute); } - } else if (sourcemeta::jsontoolkit::is_string(document)) { - const std::basic_string value{ - sourcemeta::jsontoolkit::to_string(document)}; - const auto size{sourcemeta::jsontoolkit::size(document)}; + } else if (document.is_string()) { + const std::basic_string value{document.to_string()}; + const auto size{document.size()}; const bool is_shared{this->context().has(value, ContextType::Standalone)}; if (size < uint_max<5>) { const std::uint8_t type{is_shared ? TYPE_SHARED_STRING : TYPE_STRING}; @@ -546,8 +535,8 @@ class Encoder : private BasicEncoder { return FLOOR_VARINT_PREFIX_UTF8_STRING_SHARED(document, {uint_max<5> * 2}); } - } else if (sourcemeta::jsontoolkit::is_array(document)) { - const auto size{sourcemeta::jsontoolkit::size(document)}; + } else if (document.is_array()) { + const auto size{document.size()}; if (size >= uint_max<5>) { this->put_byte(TYPE_ARRAY); this->put_varint(size - uint_max<5>); @@ -559,8 +548,8 @@ class Encoder : private BasicEncoder { Encoding encoding{ sourcemeta::jsonbinpack::ANY_PACKED_TYPE_TAG_BYTE_PREFIX{}}; this->FIXED_TYPED_ARRAY(document, {size, wrap(std::move(encoding)), {}}); - } else if (sourcemeta::jsontoolkit::is_object(document)) { - const auto size{sourcemeta::jsontoolkit::size(document)}; + } else if (document.is_object()) { + const auto size{document.size()}; if (size >= uint_max<5>) { this->put_byte(TYPE_OBJECT); this->put_varint(size - uint_max<5>); diff --git a/src/encoder/include/jsonbinpack/encoder/basic_encoder.h b/src/encoder/include/sourcemeta/jsonbinpack/encoder_basic.h similarity index 89% rename from src/encoder/include/jsonbinpack/encoder/basic_encoder.h rename to src/encoder/include/sourcemeta/jsonbinpack/encoder_basic.h index 3f966374c..8f6684959 100644 --- a/src/encoder/include/jsonbinpack/encoder/basic_encoder.h +++ b/src/encoder/include/sourcemeta/jsonbinpack/encoder_basic.h @@ -1,9 +1,9 @@ -#ifndef SOURCEMETA_JSONBINPACK_ENCODER_BASIC_ENCODER_H_ -#define SOURCEMETA_JSONBINPACK_ENCODER_BASIC_ENCODER_H_ +#ifndef SOURCEMETA_JSONBINPACK_ENCODER_BASIC_H_ +#define SOURCEMETA_JSONBINPACK_ENCODER_BASIC_H_ -#include -#include -#include +#include +#include +#include #include // std::find_if #include // assert diff --git a/src/encoder/include/jsonbinpack/encoder/context.h b/src/encoder/include/sourcemeta/jsonbinpack/encoder_context.h similarity index 100% rename from src/encoder/include/jsonbinpack/encoder/context.h rename to src/encoder/include/sourcemeta/jsonbinpack/encoder_context.h diff --git a/src/encoder/include/jsonbinpack/encoder/real.h b/src/encoder/include/sourcemeta/jsonbinpack/encoder_real.h similarity index 100% rename from src/encoder/include/jsonbinpack/encoder/real.h rename to src/encoder/include/sourcemeta/jsonbinpack/encoder_real.h diff --git a/src/encoder/include/jsonbinpack/encoder/varint.h b/src/encoder/include/sourcemeta/jsonbinpack/encoder_varint.h similarity index 100% rename from src/encoder/include/jsonbinpack/encoder/varint.h rename to src/encoder/include/sourcemeta/jsonbinpack/encoder_varint.h diff --git a/src/encoder/include/jsonbinpack/encoder/zigzag.h b/src/encoder/include/sourcemeta/jsonbinpack/encoder_zigzag.h similarity index 100% rename from src/encoder/include/jsonbinpack/encoder/zigzag.h rename to src/encoder/include/sourcemeta/jsonbinpack/encoder_zigzag.h diff --git a/src/encoding/CMakeLists.txt b/src/encoding/CMakeLists.txt index 9f85a4daa..f6084445a 100644 --- a/src/encoding/CMakeLists.txt +++ b/src/encoding/CMakeLists.txt @@ -1,13 +1,14 @@ -add_library(sourcemeta_jsonbinpack_encoding - include/jsonbinpack/encoding/encoding.h - include/jsonbinpack/encoding/wrap.h - include/jsonbinpack/encoding/tag.h - encoding.cc) -target_link_libraries(sourcemeta_jsonbinpack_encoding PUBLIC +noa_library(NAMESPACE sourcemeta PROJECT jsonbinpack NAME encoding + FOLDER "JSON BinPack/Encoding" + PRIVATE_HEADERS tag.h wrap.h) + +if(JSONBINPACK_INSTALL) + noa_library_install(NAMESPACE sourcemeta PROJECT jsonbinpack NAME encoding) +endif() + +sourcemeta_jsonbinpack_add_compile_options(INTERFACE sourcemeta_jsonbinpack_encoding) + +target_link_libraries(sourcemeta_jsonbinpack_encoding INTERFACE sourcemeta::jsontoolkit::json) -target_link_libraries(sourcemeta_jsonbinpack_encoding PUBLIC - sourcemeta_jsonbinpack_numeric) -target_include_directories(sourcemeta_jsonbinpack_encoding PUBLIC - "${CMAKE_CURRENT_SOURCE_DIR}/include") -set_target_properties(sourcemeta_jsonbinpack_encoding - PROPERTIES FOLDER "JSON BinPack/Encoding") +target_link_libraries(sourcemeta_jsonbinpack_encoding INTERFACE + sourcemeta::jsonbinpack::numeric) diff --git a/src/encoding/encoding.cc b/src/encoding/encoding.cc deleted file mode 100644 index c083f5371..000000000 --- a/src/encoding/encoding.cc +++ /dev/null @@ -1,36 +0,0 @@ -#include - -#include // std::move - -namespace sourcemeta::jsonbinpack { - -BYTE_CHOICE_INDEX::BYTE_CHOICE_INDEX(const BYTE_CHOICE_INDEX &other) - : choices{sourcemeta::jsontoolkit::copy(other.choices)} {} - -BYTE_CHOICE_INDEX::BYTE_CHOICE_INDEX( - std::vector &&other) - : choices{std::move(other)} {} - -LARGE_CHOICE_INDEX::LARGE_CHOICE_INDEX(const LARGE_CHOICE_INDEX &other) - : choices{sourcemeta::jsontoolkit::copy(other.choices)} {} - -LARGE_CHOICE_INDEX::LARGE_CHOICE_INDEX( - std::vector &&other) - : choices{std::move(other)} {} - -TOP_LEVEL_BYTE_CHOICE_INDEX::TOP_LEVEL_BYTE_CHOICE_INDEX( - const TOP_LEVEL_BYTE_CHOICE_INDEX &other) - : choices{sourcemeta::jsontoolkit::copy(other.choices)} {} - -TOP_LEVEL_BYTE_CHOICE_INDEX::TOP_LEVEL_BYTE_CHOICE_INDEX( - std::vector &&other) - : choices{std::move(other)} {} - -CONST_NONE::CONST_NONE(const sourcemeta::jsontoolkit::JSON &input) - : value{sourcemeta::jsontoolkit::from(input)} {} -CONST_NONE::CONST_NONE(const sourcemeta::jsontoolkit::Value &input) - : value{sourcemeta::jsontoolkit::from(input)} {} -CONST_NONE::CONST_NONE(const CONST_NONE &other) - : value{sourcemeta::jsontoolkit::from(other.value)} {} - -} // namespace sourcemeta::jsonbinpack diff --git a/src/encoding/include/jsonbinpack/encoding/encoding.h b/src/encoding/include/sourcemeta/jsonbinpack/encoding.h similarity index 97% rename from src/encoding/include/jsonbinpack/encoding/encoding.h rename to src/encoding/include/sourcemeta/jsonbinpack/encoding.h index edb50abc7..fb4a3856a 100644 --- a/src/encoding/include/jsonbinpack/encoding/encoding.h +++ b/src/encoding/include/sourcemeta/jsonbinpack/encoding.h @@ -1,12 +1,12 @@ -#ifndef SOURCEMETA_JSONBINPACK_ENCODING_ENCODING_H_ -#define SOURCEMETA_JSONBINPACK_ENCODING_ENCODING_H_ +#ifndef SOURCEMETA_JSONBINPACK_ENCODING_H_ +#define SOURCEMETA_JSONBINPACK_ENCODING_H_ /// @defgroup encoding Encodings /// /// @see sourcemeta::jsonbinpack::Encoder /// @see sourcemeta::jsonbinpack::Decoder -#include +#include #include // std::int64_t, std::uint64_t #include // std::shared_ptr @@ -271,11 +271,6 @@ struct DOUBLE_VARINT_TUPLE {}; /// ``` // clang-format on struct BYTE_CHOICE_INDEX { -#ifndef DOXYGEN - // Because JSON documents are not easily copyable - BYTE_CHOICE_INDEX(const BYTE_CHOICE_INDEX &); - BYTE_CHOICE_INDEX(std::vector &&); -#endif /// The set of choice values const std::vector choices; }; @@ -310,11 +305,6 @@ struct BYTE_CHOICE_INDEX { /// ``` // clang-format on struct LARGE_CHOICE_INDEX { -#ifndef DOXYGEN - // Because JSON documents are not easily copyable - LARGE_CHOICE_INDEX(const LARGE_CHOICE_INDEX &); - LARGE_CHOICE_INDEX(std::vector &&); -#endif /// The set of choice values const std::vector choices; }; @@ -354,11 +344,6 @@ struct LARGE_CHOICE_INDEX { /// value is not encoded. // clang-format on struct TOP_LEVEL_BYTE_CHOICE_INDEX { -#ifndef DOXYGEN - // Because JSON documents are not easily copyable - TOP_LEVEL_BYTE_CHOICE_INDEX(const TOP_LEVEL_BYTE_CHOICE_INDEX &); - TOP_LEVEL_BYTE_CHOICE_INDEX(std::vector &&); -#endif /// The set of choice values const std::vector choices; }; @@ -381,12 +366,6 @@ struct TOP_LEVEL_BYTE_CHOICE_INDEX { /// The input value that matches the `value` option is not encoded. // clang-format on struct CONST_NONE { -#ifndef DOXYGEN - // These constructors allow this encoding to be movable - CONST_NONE(const sourcemeta::jsontoolkit::JSON &); - CONST_NONE(const sourcemeta::jsontoolkit::Value &); - CONST_NONE(const CONST_NONE &); -#endif /// The constant value const sourcemeta::jsontoolkit::JSON value; }; diff --git a/src/encoding/include/jsonbinpack/encoding/tag.h b/src/encoding/include/sourcemeta/jsonbinpack/encoding_tag.h similarity index 98% rename from src/encoding/include/jsonbinpack/encoding/tag.h rename to src/encoding/include/sourcemeta/jsonbinpack/encoding_tag.h index f20361ced..5a78c1021 100644 --- a/src/encoding/include/jsonbinpack/encoding/tag.h +++ b/src/encoding/include/sourcemeta/jsonbinpack/encoding_tag.h @@ -1,7 +1,7 @@ #ifndef SOURCEMETA_JSONBINPACK_ENCODING_TAG_H_ #define SOURCEMETA_JSONBINPACK_ENCODING_TAG_H_ -#include +#include namespace sourcemeta::jsonbinpack::tag { diff --git a/src/encoding/include/jsonbinpack/encoding/wrap.h b/src/encoding/include/sourcemeta/jsonbinpack/encoding_wrap.h similarity index 97% rename from src/encoding/include/jsonbinpack/encoding/wrap.h rename to src/encoding/include/sourcemeta/jsonbinpack/encoding_wrap.h index 086c2cc8c..f73dd9da6 100644 --- a/src/encoding/include/jsonbinpack/encoding/wrap.h +++ b/src/encoding/include/sourcemeta/jsonbinpack/encoding_wrap.h @@ -1,7 +1,7 @@ #ifndef SOURCEMETA_JSONBINPACK_ENCODING_WRAP_H_ #define SOURCEMETA_JSONBINPACK_ENCODING_WRAP_H_ -#include +#include #include // std::transform #include // std::initializer_list diff --git a/src/mapper/CMakeLists.txt b/src/mapper/CMakeLists.txt index e2a2a9079..b31ecc39b 100644 --- a/src/mapper/CMakeLists.txt +++ b/src/mapper/CMakeLists.txt @@ -1,28 +1,34 @@ -add_library(sourcemeta_jsonbinpack_mapper - include/jsonbinpack/mapper/mapper.h - include/jsonbinpack/mapper/states.h - include/jsonbinpack/mapper/encoding.h - rules/enum_8_bit.h - rules/enum_8_bit_top_level.h - rules/enum_arbitrary.h - rules/enum_singleton.h - rules/integer_bounded_8_bit.h - rules/integer_bounded_greater_than_8_bit.h - rules/integer_bounded_multiplier_8_bit.h - rules/integer_bounded_multiplier_greater_than_8_bit.h - rules/integer_lower_bound.h - rules/integer_lower_bound_multiplier.h - rules/integer_unbound.h - rules/integer_unbound_multiplier.h - rules/integer_upper_bound.h - rules/integer_upper_bound_multiplier.h - rules/number_arbitrary.h - mapper.cc states.cc encoding.cc) -target_include_directories(sourcemeta_jsonbinpack_mapper PUBLIC - "${CMAKE_CURRENT_SOURCE_DIR}/include") -target_link_libraries(sourcemeta_jsonbinpack_mapper PUBLIC sourcemeta::jsontoolkit::json) -target_link_libraries(sourcemeta_jsonbinpack_mapper PUBLIC sourcemeta_alterschema) -target_link_libraries(sourcemeta_jsonbinpack_mapper PUBLIC sourcemeta_jsonbinpack_schemas) -target_link_libraries(sourcemeta_jsonbinpack_mapper PRIVATE sourcemeta_jsonbinpack_numeric) -set_target_properties(sourcemeta_jsonbinpack_mapper - PROPERTIES FOLDER "JSON BinPack/Mapper") +noa_library(NAMESPACE sourcemeta PROJECT jsonbinpack NAME mapper + FOLDER "JSON BinPack/Mapper" + PRIVATE_HEADERS encoding.h states.h + SOURCES encoding.cc mapper.cc states.cc + rules/enum_8_bit.h + rules/enum_8_bit_top_level.h + rules/enum_arbitrary.h + rules/enum_singleton.h + rules/integer_bounded_8_bit.h + rules/integer_bounded_greater_than_8_bit.h + rules/integer_bounded_multiplier_8_bit.h + rules/integer_bounded_multiplier_greater_than_8_bit.h + rules/integer_lower_bound.h + rules/integer_lower_bound_multiplier.h + rules/integer_unbound.h + rules/integer_unbound_multiplier.h + rules/integer_upper_bound.h + rules/integer_upper_bound_multiplier.h + rules/number_arbitrary.h) + +if(JSONBINPACK_INSTALL) + noa_library_install(NAMESPACE sourcemeta PROJECT jsonbinpack NAME mapper) +endif() + +sourcemeta_jsonbinpack_add_compile_options(PRIVATE sourcemeta_jsonbinpack_mapper) + +target_link_libraries(sourcemeta_jsonbinpack_mapper PUBLIC + sourcemeta::jsontoolkit::json) +target_link_libraries(sourcemeta_jsonbinpack_mapper PUBLIC + sourcemeta::jsontoolkit::jsonschema) +target_link_libraries(sourcemeta_jsonbinpack_mapper PUBLIC + sourcemeta_jsonbinpack_schemas) +target_link_libraries(sourcemeta_jsonbinpack_mapper PRIVATE + sourcemeta_jsonbinpack_numeric) diff --git a/src/mapper/encoding.cc b/src/mapper/encoding.cc index b4a38717a..0d11eaf79 100644 --- a/src/mapper/encoding.cc +++ b/src/mapper/encoding.cc @@ -1,31 +1,27 @@ -#include -#include -#include +#include +#include + #include // std::optional auto sourcemeta::jsonbinpack::mapper::is_encoding( - const sourcemeta::jsontoolkit::Value &document) -> bool { - const std::optional metaschema{ - sourcemeta::jsontoolkit::metaschema(document)}; - return metaschema.has_value() && - sourcemeta::jsontoolkit::defines(document, keywords::name) && - sourcemeta::jsontoolkit::defines(document, keywords::options) && - metaschema.value() == - sourcemeta::jsonbinpack::schemas::encoding::v1::id; + const sourcemeta::jsontoolkit::JSON &document) -> bool { + const std::optional dialect{ + sourcemeta::jsontoolkit::dialect(document)}; + return dialect.has_value() && document.defines(keywords::name) && + document.defines(keywords::options) && + dialect.value() == sourcemeta::jsonbinpack::schemas::encoding::v1::id; } auto sourcemeta::jsonbinpack::mapper::make_encoding( - sourcemeta::jsontoolkit::JSON &document, - sourcemeta::jsontoolkit::Value &value, const std::string &encoding, - const sourcemeta::jsontoolkit::Value &options) -> void { - sourcemeta::jsontoolkit::make_object(value); - sourcemeta::jsontoolkit::assign( - document, value, keywords::version, - sourcemeta::jsontoolkit::from( - sourcemeta::jsonbinpack::schemas::encoding::v1::id)); - sourcemeta::jsontoolkit::assign(document, value, keywords::name, - sourcemeta::jsontoolkit::from(encoding)); - sourcemeta::jsontoolkit::assign(document, value, keywords::options, options); + sourcemeta::jsontoolkit::SchemaTransformer &document, + const std::string &encoding, const sourcemeta::jsontoolkit::JSON &options) + -> void { + document.into_object(); + document.assign(keywords::version, + sourcemeta::jsontoolkit::JSON{ + sourcemeta::jsonbinpack::schemas::encoding::v1::id}); + document.assign(keywords::name, sourcemeta::jsontoolkit::JSON{encoding}); + document.assign(keywords::options, options); } auto sourcemeta::jsonbinpack::mapper::resolver(const std::string &identifier) diff --git a/src/mapper/include/jsonbinpack/mapper/mapper.h b/src/mapper/include/jsonbinpack/mapper/mapper.h deleted file mode 100644 index 446d13481..000000000 --- a/src/mapper/include/jsonbinpack/mapper/mapper.h +++ /dev/null @@ -1,39 +0,0 @@ -#ifndef SOURCEMETA_JSONBINPACK_MAPPER_MAPPER_H_ -#define SOURCEMETA_JSONBINPACK_MAPPER_MAPPER_H_ - -/// @defgroup mapper Mapper -/// @brief A pure and deterministic function that converts a Canonical JSON -/// Schema into a JSON BinPack Encoding Schema -/// -/// @defgroup mapper_rules Rules -/// @ingroup mapper - -#include -#include -#include - -#include // std::string - -namespace sourcemeta::jsonbinpack { - -/// @ingroup mapper -class Mapper { -public: - Mapper(const sourcemeta::jsontoolkit::schema_resolver_t &resolver); - auto apply(sourcemeta::jsontoolkit::JSON &document, - sourcemeta::jsontoolkit::Value &value, - const std::string &default_metaschema) const -> void; - - // For convenience - inline auto apply(sourcemeta::jsontoolkit::JSON &document, - const std::string &default_metaschema) const -> void { - return apply(document, document, default_metaschema); - } - -private: - sourcemeta::alterschema::Bundle bundle; -}; - -} // namespace sourcemeta::jsonbinpack - -#endif diff --git a/src/mapper/include/sourcemeta/jsonbinpack/mapper.h b/src/mapper/include/sourcemeta/jsonbinpack/mapper.h new file mode 100644 index 000000000..d939d2ebc --- /dev/null +++ b/src/mapper/include/sourcemeta/jsonbinpack/mapper.h @@ -0,0 +1,34 @@ +#ifndef SOURCEMETA_JSONBINPACK_MAPPER_H_ +#define SOURCEMETA_JSONBINPACK_MAPPER_H_ + +/// @defgroup mapper Mapper +/// @brief A pure and deterministic function that converts a Canonical JSON +/// Schema into a JSON BinPack Encoding Schema +/// +/// @defgroup mapper_rules Rules +/// @ingroup mapper + +#include +#include + +#include // std::optional +#include // std::string + +namespace sourcemeta::jsonbinpack { + +/// @ingroup mapper +class Mapper { +public: + Mapper(); + auto apply(sourcemeta::jsontoolkit::JSON &document, + const sourcemeta::jsontoolkit::SchemaWalker &walker, + const sourcemeta::jsontoolkit::SchemaResolver &resolver, + const std::optional &default_dialect) const -> void; + +private: + sourcemeta::jsontoolkit::SchemaTransformBundle bundle; +}; + +} // namespace sourcemeta::jsonbinpack + +#endif diff --git a/src/mapper/include/jsonbinpack/mapper/encoding.h b/src/mapper/include/sourcemeta/jsonbinpack/mapper_encoding.h similarity index 52% rename from src/mapper/include/jsonbinpack/mapper/encoding.h rename to src/mapper/include/sourcemeta/jsonbinpack/mapper_encoding.h index 371921de2..77d108c88 100644 --- a/src/mapper/include/jsonbinpack/mapper/encoding.h +++ b/src/mapper/include/sourcemeta/jsonbinpack/mapper_encoding.h @@ -1,7 +1,8 @@ #ifndef SOURCEMETA_JSONBINPACK_MAPPER_ENCODING_H_ #define SOURCEMETA_JSONBINPACK_MAPPER_ENCODING_H_ -#include +#include +#include #include // std::future #include // std::optional @@ -15,19 +16,11 @@ const std::string name{"name"}; const std::string options{"options"}; } // namespace keywords -auto is_encoding(const sourcemeta::jsontoolkit::Value &document) -> bool; +auto is_encoding(const sourcemeta::jsontoolkit::JSON &document) -> bool; -auto make_encoding(sourcemeta::jsontoolkit::JSON &document, - sourcemeta::jsontoolkit::Value &value, +auto make_encoding(sourcemeta::jsontoolkit::SchemaTransformer &document, const std::string &encoding, - const sourcemeta::jsontoolkit::Value &options) -> void; - -inline auto make_encoding(sourcemeta::jsontoolkit::JSON &document, - const std::string &encoding, - const sourcemeta::jsontoolkit::Value &options) - -> void { - return make_encoding(document, document, encoding, options); -} + const sourcemeta::jsontoolkit::JSON &options) -> void; auto resolver(const std::string &identifier) -> std::future>; diff --git a/src/mapper/include/jsonbinpack/mapper/states.h b/src/mapper/include/sourcemeta/jsonbinpack/mapper_states.h similarity index 51% rename from src/mapper/include/jsonbinpack/mapper/states.h rename to src/mapper/include/sourcemeta/jsonbinpack/mapper_states.h index 75c17ae2d..d5d38aa51 100644 --- a/src/mapper/include/jsonbinpack/mapper/states.h +++ b/src/mapper/include/sourcemeta/jsonbinpack/mapper_states.h @@ -1,17 +1,17 @@ #ifndef SOURCEMETA_JSONBINPACK_MAPPER_STATES_H_ #define SOURCEMETA_JSONBINPACK_MAPPER_STATES_H_ -#include +#include -#include // std::optional -#include // std::unordered_map -#include // std::vector +#include // std::optional +#include // std::set +#include // std::vector namespace sourcemeta::jsonbinpack::mapper::states { // No value means infinite -auto integer(const sourcemeta::jsontoolkit::Value &schema, - const std::unordered_map &vocabularies) +auto integer(const sourcemeta::jsontoolkit::JSON &schema, + const std::set &vocabularies) -> std::optional>; } // namespace sourcemeta::jsonbinpack::mapper::states diff --git a/src/mapper/mapper.cc b/src/mapper/mapper.cc index b1aa5b42a..d8365daf7 100644 --- a/src/mapper/mapper.cc +++ b/src/mapper/mapper.cc @@ -1,15 +1,13 @@ -#include -#include +#include +#include -#include // assert -#include // std::function -#include // std::domain_error +#include // assert +#include // std::domain_error // To be used by the rules below -#include -#include -#include -#include +#include +#include +#include #include "rules/enum_8_bit.h" #include "rules/enum_8_bit_top_level.h" @@ -27,37 +25,7 @@ #include "rules/integer_upper_bound_multiplier.h" #include "rules/number_arbitrary.h" -// TODO: Improve this helper method that joins multiple -// resolvers and move it upstream -class ResolverJoin { -public: - ResolverJoin(const sourcemeta::jsontoolkit::schema_resolver_t &left, - const sourcemeta::jsontoolkit::schema_resolver_t &right) - : left_{left}, right_{right} {} - - auto operator()(const std::string &identifier) - -> std::future> { - const std::optional result{ - this->left_(identifier).get()}; - if (result.has_value()) { - std::promise> promise; - promise.set_value(sourcemeta::jsontoolkit::from(result.value())); - return promise.get_future(); - } - - return this->right_(identifier); - } - -private: - const sourcemeta::jsontoolkit::schema_resolver_t left_; - const sourcemeta::jsontoolkit::schema_resolver_t right_; -}; - -sourcemeta::jsonbinpack::Mapper::Mapper( - const sourcemeta::jsontoolkit::schema_resolver_t &resolver) - : bundle{ - sourcemeta::jsontoolkit::schema_walker_none, - ResolverJoin{sourcemeta::jsonbinpack::mapper::resolver, resolver}} { +sourcemeta::jsonbinpack::Mapper::Mapper() { using namespace sourcemeta::jsonbinpack::mapper; // Enums @@ -84,24 +52,28 @@ sourcemeta::jsonbinpack::Mapper::Mapper( auto sourcemeta::jsonbinpack::Mapper::apply( sourcemeta::jsontoolkit::JSON &document, - sourcemeta::jsontoolkit::Value &value, - const std::string &default_metaschema) const -> void { - const std::optional draft{ - sourcemeta::jsontoolkit::draft(value, this->bundle.resolver(), - default_metaschema) + const sourcemeta::jsontoolkit::SchemaWalker &walker, + const sourcemeta::jsontoolkit::SchemaResolver &resolver, + const std::optional &default_dialect) const -> void { + const std::optional base_dialect{ + sourcemeta::jsontoolkit::base_dialect(document, resolver, default_dialect) .get()}; - if (!draft.has_value() || - draft.value() != "https://json-schema.org/draft/2020-12/schema") { + + // TODO: Use a custom error here + if (!base_dialect.has_value() || + base_dialect.value() != "https://json-schema.org/draft/2020-12/schema") { throw std::domain_error("Only JSON Schema 2020-12 is supported"); } - this->bundle.apply(document, value, default_metaschema); + this->bundle.apply(document, walker, resolver, + sourcemeta::jsontoolkit::empty_pointer, default_dialect); // The "any" encoding is always the last resort - if (!mapper::is_encoding(value)) { - mapper::make_encoding(document, value, "ANY_PACKED_TYPE_TAG_BYTE_PREFIX", - sourcemeta::jsontoolkit::make_object()); + if (!mapper::is_encoding(document)) { + sourcemeta::jsontoolkit::SchemaTransformer transformer{document}; + mapper::make_encoding(transformer, "ANY_PACKED_TYPE_TAG_BYTE_PREFIX", + sourcemeta::jsontoolkit::JSON::make_object()); } - assert(mapper::is_encoding(value)); + assert(mapper::is_encoding(document)); } diff --git a/src/mapper/rules/enum_8_bit.h b/src/mapper/rules/enum_8_bit.h index 5566508a0..c492d7047 100644 --- a/src/mapper/rules/enum_8_bit.h +++ b/src/mapper/rules/enum_8_bit.h @@ -2,38 +2,28 @@ namespace sourcemeta::jsonbinpack::mapper { // TODO: Unit test this mapping once we have container encodings /// @ingroup mapper_rules -class Enum8Bit final : public sourcemeta::alterschema::Rule { +class Enum8Bit final : public sourcemeta::jsontoolkit::SchemaTransformRule { public: - Enum8Bit() : Rule("enum_8_bit"){}; + Enum8Bit() : sourcemeta::jsontoolkit::SchemaTransformRule("enum_8_bit"){}; - [[nodiscard]] auto - condition(const sourcemeta::jsontoolkit::Value &schema, - const std::string &draft, - const std::unordered_map &vocabularies, - const std::size_t level) const -> bool override { + [[nodiscard]] auto condition( + const sourcemeta::jsontoolkit::JSON &schema, const std::string &dialect, + const std::set &vocabularies, + const sourcemeta::jsontoolkit::Pointer &pointer) const -> bool override { return !is_encoding(schema) && - draft == "https://json-schema.org/draft/2020-12/schema" && + dialect == "https://json-schema.org/draft/2020-12/schema" && vocabularies.contains( "https://json-schema.org/draft/2020-12/vocab/validation") && - sourcemeta::jsontoolkit::defines(schema, "enum") && - sourcemeta::jsontoolkit::is_array( - sourcemeta::jsontoolkit::at(schema, "enum")) && - level > 0 && - sourcemeta::jsontoolkit::size( - sourcemeta::jsontoolkit::at(schema, "enum")) > 1 && - is_byte(sourcemeta::jsontoolkit::size( - sourcemeta::jsontoolkit::at(schema, "enum")) - - 1); + schema.defines("enum") && schema.at("enum").is_array() && + !pointer.empty() && schema.at("enum").size() > 1 && + is_byte(schema.at("enum").size() - 1); } - auto transform(sourcemeta::jsontoolkit::JSON &document, - sourcemeta::jsontoolkit::Value &value) const -> void override { - auto options{sourcemeta::jsontoolkit::make_object()}; - sourcemeta::jsontoolkit::assign( - options, "choices", - sourcemeta::jsontoolkit::from( - sourcemeta::jsontoolkit::at(value, "enum"))); - make_encoding(document, value, "BYTE_CHOICE_INDEX", options); + auto transform(sourcemeta::jsontoolkit::SchemaTransformer &schema) const + -> void override { + auto options{sourcemeta::jsontoolkit::JSON::make_object()}; + options.assign("choices", sourcemeta::jsontoolkit::JSON(schema.at("enum"))); + make_encoding(schema, "BYTE_CHOICE_INDEX", options); } }; diff --git a/src/mapper/rules/enum_8_bit_top_level.h b/src/mapper/rules/enum_8_bit_top_level.h index 9ec4c3681..040584f09 100644 --- a/src/mapper/rules/enum_8_bit_top_level.h +++ b/src/mapper/rules/enum_8_bit_top_level.h @@ -1,38 +1,30 @@ namespace sourcemeta::jsonbinpack::mapper { /// @ingroup mapper_rules -class Enum8BitTopLevel final : public sourcemeta::alterschema::Rule { +class Enum8BitTopLevel final + : public sourcemeta::jsontoolkit::SchemaTransformRule { public: - Enum8BitTopLevel() : Rule("enum_8_bit_top_level"){}; + Enum8BitTopLevel() + : sourcemeta::jsontoolkit::SchemaTransformRule("enum_8_bit_top_level"){}; - [[nodiscard]] auto - condition(const sourcemeta::jsontoolkit::Value &schema, - const std::string &draft, - const std::unordered_map &vocabularies, - const std::size_t level) const -> bool override { + [[nodiscard]] auto condition( + const sourcemeta::jsontoolkit::JSON &schema, const std::string &dialect, + const std::set &vocabularies, + const sourcemeta::jsontoolkit::Pointer &pointer) const -> bool override { return !is_encoding(schema) && - draft == "https://json-schema.org/draft/2020-12/schema" && + dialect == "https://json-schema.org/draft/2020-12/schema" && vocabularies.contains( "https://json-schema.org/draft/2020-12/vocab/validation") && - sourcemeta::jsontoolkit::defines(schema, "enum") && - sourcemeta::jsontoolkit::is_array( - sourcemeta::jsontoolkit::at(schema, "enum")) && - level == 0 && - sourcemeta::jsontoolkit::size( - sourcemeta::jsontoolkit::at(schema, "enum")) > 1 && - is_byte(sourcemeta::jsontoolkit::size( - sourcemeta::jsontoolkit::at(schema, "enum")) - - 1); + schema.defines("enum") && schema.at("enum").is_array() && + pointer.empty() && schema.at("enum").size() > 1 && + is_byte(schema.at("enum").size() - 1); } - auto transform(sourcemeta::jsontoolkit::JSON &document, - sourcemeta::jsontoolkit::Value &value) const -> void override { - auto options{sourcemeta::jsontoolkit::make_object()}; - sourcemeta::jsontoolkit::assign( - options, "choices", - sourcemeta::jsontoolkit::from( - sourcemeta::jsontoolkit::at(value, "enum"))); - make_encoding(document, value, "TOP_LEVEL_BYTE_CHOICE_INDEX", options); + auto transform(sourcemeta::jsontoolkit::SchemaTransformer &schema) const + -> void override { + auto options{sourcemeta::jsontoolkit::JSON::make_object()}; + options.assign("choices", sourcemeta::jsontoolkit::JSON(schema.at("enum"))); + make_encoding(schema, "TOP_LEVEL_BYTE_CHOICE_INDEX", options); } }; diff --git a/src/mapper/rules/enum_arbitrary.h b/src/mapper/rules/enum_arbitrary.h index 3cac1098a..ffbdd81a8 100644 --- a/src/mapper/rules/enum_arbitrary.h +++ b/src/mapper/rules/enum_arbitrary.h @@ -2,38 +2,30 @@ namespace sourcemeta::jsonbinpack::mapper { // TODO: Unit test this mapping once we have container encodings /// @ingroup mapper_rules -class EnumArbitrary final : public sourcemeta::alterschema::Rule { +class EnumArbitrary final + : public sourcemeta::jsontoolkit::SchemaTransformRule { public: - EnumArbitrary() : Rule("enum_arbitrary"){}; + EnumArbitrary() + : sourcemeta::jsontoolkit::SchemaTransformRule("enum_arbitrary"){}; - [[nodiscard]] auto - condition(const sourcemeta::jsontoolkit::Value &schema, - const std::string &draft, - const std::unordered_map &vocabularies, - const std::size_t level) const -> bool override { + [[nodiscard]] auto condition( + const sourcemeta::jsontoolkit::JSON &schema, const std::string &dialect, + const std::set &vocabularies, + const sourcemeta::jsontoolkit::Pointer &pointer) const -> bool override { return !is_encoding(schema) && - draft == "https://json-schema.org/draft/2020-12/schema" && + dialect == "https://json-schema.org/draft/2020-12/schema" && vocabularies.contains( "https://json-schema.org/draft/2020-12/vocab/validation") && - sourcemeta::jsontoolkit::defines(schema, "enum") && - sourcemeta::jsontoolkit::is_array( - sourcemeta::jsontoolkit::at(schema, "enum")) && - level > 0 && - sourcemeta::jsontoolkit::size( - sourcemeta::jsontoolkit::at(schema, "enum")) > 1 && - !is_byte(sourcemeta::jsontoolkit::size( - sourcemeta::jsontoolkit::at(schema, "enum")) - - 1); + schema.defines("enum") && schema.at("enum").is_array() && + !pointer.empty() && schema.at("enum").size() > 1 && + !is_byte(schema.at("enum").size() - 1); } - auto transform(sourcemeta::jsontoolkit::JSON &document, - sourcemeta::jsontoolkit::Value &value) const -> void override { - auto options{sourcemeta::jsontoolkit::make_object()}; - sourcemeta::jsontoolkit::assign( - options, "choices", - sourcemeta::jsontoolkit::from( - sourcemeta::jsontoolkit::at(value, "enum"))); - make_encoding(document, value, "LARGE_CHOICE_INDEX", options); + auto transform(sourcemeta::jsontoolkit::SchemaTransformer &schema) const + -> void override { + auto options{sourcemeta::jsontoolkit::JSON::make_object()}; + options.assign("choices", sourcemeta::jsontoolkit::JSON(schema.at("enum"))); + make_encoding(schema, "LARGE_CHOICE_INDEX", options); } }; diff --git a/src/mapper/rules/enum_singleton.h b/src/mapper/rules/enum_singleton.h index acab6e200..2331ec83c 100644 --- a/src/mapper/rules/enum_singleton.h +++ b/src/mapper/rules/enum_singleton.h @@ -1,35 +1,31 @@ namespace sourcemeta::jsonbinpack::mapper { /// @ingroup mapper_rules -class EnumSingleton final : public sourcemeta::alterschema::Rule { +class EnumSingleton final + : public sourcemeta::jsontoolkit::SchemaTransformRule { public: - EnumSingleton() : Rule("enum_singleton"){}; + EnumSingleton() + : sourcemeta::jsontoolkit::SchemaTransformRule("enum_singleton"){}; - [[nodiscard]] auto - condition(const sourcemeta::jsontoolkit::Value &schema, - const std::string &draft, - const std::unordered_map &vocabularies, - const std::size_t) const -> bool override { + [[nodiscard]] auto condition(const sourcemeta::jsontoolkit::JSON &schema, + const std::string &dialect, + const std::set &vocabularies, + const sourcemeta::jsontoolkit::Pointer &) const + -> bool override { return !is_encoding(schema) && - draft == "https://json-schema.org/draft/2020-12/schema" && + dialect == "https://json-schema.org/draft/2020-12/schema" && vocabularies.contains( "https://json-schema.org/draft/2020-12/vocab/validation") && - sourcemeta::jsontoolkit::defines(schema, "enum") && - sourcemeta::jsontoolkit::is_array( - sourcemeta::jsontoolkit::at(schema, "enum")) && - sourcemeta::jsontoolkit::size( - sourcemeta::jsontoolkit::at(schema, "enum")) == 1; + schema.defines("enum") && schema.at("enum").is_array() && + schema.at("enum").size() == 1; } - auto transform(sourcemeta::jsontoolkit::JSON &document, - sourcemeta::jsontoolkit::Value &value) const -> void override { - auto options{sourcemeta::jsontoolkit::make_object()}; - sourcemeta::jsontoolkit::assign( - options, "value", - sourcemeta::jsontoolkit::from(sourcemeta::jsontoolkit::front( - sourcemeta::jsontoolkit::at(value, "enum")))); - - make_encoding(document, value, "CONST_NONE", options); + auto transform(sourcemeta::jsontoolkit::SchemaTransformer &schema) const + -> void override { + auto options{sourcemeta::jsontoolkit::JSON::make_object()}; + options.assign("choices", + sourcemeta::jsontoolkit::JSON(schema.at("enum").at(0))); + make_encoding(schema, "CONST_NONE", options); } }; diff --git a/src/mapper/rules/integer_bounded_8_bit.h b/src/mapper/rules/integer_bounded_8_bit.h index c1c8c2c82..4685f9401 100644 --- a/src/mapper/rules/integer_bounded_8_bit.h +++ b/src/mapper/rules/integer_bounded_8_bit.h @@ -1,46 +1,38 @@ namespace sourcemeta::jsonbinpack::mapper { /// @ingroup mapper_rules -class IntegerBounded8Bit final : public sourcemeta::alterschema::Rule { +class IntegerBounded8Bit final + : public sourcemeta::jsontoolkit::SchemaTransformRule { public: - IntegerBounded8Bit() : Rule("integer_bounded_8_bit"){}; + IntegerBounded8Bit() + : sourcemeta::jsontoolkit::SchemaTransformRule("integer_bounded_8_bit"){}; - [[nodiscard]] auto - condition(const sourcemeta::jsontoolkit::Value &schema, - const std::string &draft, - const std::unordered_map &vocabularies, - const std::size_t) const -> bool override { + [[nodiscard]] auto condition(const sourcemeta::jsontoolkit::JSON &schema, + const std::string &dialect, + const std::set &vocabularies, + const sourcemeta::jsontoolkit::Pointer &) const + -> bool override { return !is_encoding(schema) && - draft == "https://json-schema.org/draft/2020-12/schema" && + dialect == "https://json-schema.org/draft/2020-12/schema" && vocabularies.contains( "https://json-schema.org/draft/2020-12/vocab/validation") && - sourcemeta::jsontoolkit::defines(schema, "type") && - sourcemeta::jsontoolkit::to_string( - sourcemeta::jsontoolkit::at(schema, "type")) == "integer" && - sourcemeta::jsontoolkit::defines(schema, "minimum") && - sourcemeta::jsontoolkit::defines(schema, "maximum") && - is_byte(sourcemeta::jsontoolkit::to_integer( - sourcemeta::jsontoolkit::at(schema, "maximum")) - - sourcemeta::jsontoolkit::to_integer( - sourcemeta::jsontoolkit::at(schema, "minimum"))) && - !sourcemeta::jsontoolkit::defines(schema, "multipleOf"); + schema.defines("type") && + schema.at("type").to_string() == "integer" && + schema.defines("minimum") && schema.defines("maximum") && + is_byte(schema.at("maximum").to_integer() - + schema.at("minimum").to_integer()) && + !schema.defines("multipleOf"); } - auto transform(sourcemeta::jsontoolkit::JSON &document, - sourcemeta::jsontoolkit::Value &value) const -> void override { - const auto minimum{sourcemeta::jsontoolkit::to_integer( - sourcemeta::jsontoolkit::at(value, "minimum"))}; - const auto maximum{sourcemeta::jsontoolkit::to_integer( - sourcemeta::jsontoolkit::at(value, "maximum"))}; - auto options{sourcemeta::jsontoolkit::make_object()}; - sourcemeta::jsontoolkit::assign(options, "minimum", - sourcemeta::jsontoolkit::from(minimum)); - sourcemeta::jsontoolkit::assign(options, "maximum", - sourcemeta::jsontoolkit::from(maximum)); - sourcemeta::jsontoolkit::assign(options, "multiplier", - sourcemeta::jsontoolkit::from(1)); - make_encoding(document, value, "BOUNDED_MULTIPLE_8BITS_ENUM_FIXED", - options); + auto transform(sourcemeta::jsontoolkit::SchemaTransformer &schema) const + -> void override { + auto minimum{schema.at("minimum")}; + auto maximum{schema.at("maximum")}; + auto options{sourcemeta::jsontoolkit::JSON::make_object()}; + options.assign("minimum", std::move(minimum)); + options.assign("maximum", std::move(maximum)); + options.assign("multiplier", sourcemeta::jsontoolkit::JSON{1}); + make_encoding(schema, "BOUNDED_MULTIPLE_8BITS_ENUM_FIXED", options); } }; diff --git a/src/mapper/rules/integer_bounded_greater_than_8_bit.h b/src/mapper/rules/integer_bounded_greater_than_8_bit.h index 0b40a9f1c..5e5d903ef 100644 --- a/src/mapper/rules/integer_bounded_greater_than_8_bit.h +++ b/src/mapper/rules/integer_bounded_greater_than_8_bit.h @@ -2,42 +2,36 @@ namespace sourcemeta::jsonbinpack::mapper { /// @ingroup mapper_rules class IntegerBoundedGreaterThan8Bit final - : public sourcemeta::alterschema::Rule { + : public sourcemeta::jsontoolkit::SchemaTransformRule { public: IntegerBoundedGreaterThan8Bit() - : Rule("integer_bounded_greater_than_8_bit"){}; + : sourcemeta::jsontoolkit::SchemaTransformRule( + "integer_bounded_greater_than_8_bit"){}; - [[nodiscard]] auto - condition(const sourcemeta::jsontoolkit::Value &schema, - const std::string &draft, - const std::unordered_map &vocabularies, - const std::size_t) const -> bool override { + [[nodiscard]] auto condition(const sourcemeta::jsontoolkit::JSON &schema, + const std::string &dialect, + const std::set &vocabularies, + const sourcemeta::jsontoolkit::Pointer &) const + -> bool override { return !is_encoding(schema) && - draft == "https://json-schema.org/draft/2020-12/schema" && + dialect == "https://json-schema.org/draft/2020-12/schema" && vocabularies.contains( "https://json-schema.org/draft/2020-12/vocab/validation") && - sourcemeta::jsontoolkit::defines(schema, "type") && - sourcemeta::jsontoolkit::to_string( - sourcemeta::jsontoolkit::at(schema, "type")) == "integer" && - sourcemeta::jsontoolkit::defines(schema, "minimum") && - sourcemeta::jsontoolkit::defines(schema, "maximum") && - !is_byte(sourcemeta::jsontoolkit::to_integer( - sourcemeta::jsontoolkit::at(schema, "maximum")) - - sourcemeta::jsontoolkit::to_integer( - sourcemeta::jsontoolkit::at(schema, "minimum"))) && - !sourcemeta::jsontoolkit::defines(schema, "multipleOf"); + schema.defines("type") && + schema.at("type").to_string() == "integer" && + schema.defines("minimum") && schema.defines("maximum") && + !is_byte(schema.at("maximum").to_integer() - + schema.at("minimum").to_integer()) && + !schema.defines("multipleOf"); } - auto transform(sourcemeta::jsontoolkit::JSON &document, - sourcemeta::jsontoolkit::Value &value) const -> void override { - const auto minimum{sourcemeta::jsontoolkit::to_integer( - sourcemeta::jsontoolkit::at(value, "minimum"))}; - auto options{sourcemeta::jsontoolkit::make_object()}; - sourcemeta::jsontoolkit::assign(options, "minimum", - sourcemeta::jsontoolkit::from(minimum)); - sourcemeta::jsontoolkit::assign(options, "multiplier", - sourcemeta::jsontoolkit::from(1)); - make_encoding(document, value, "FLOOR_MULTIPLE_ENUM_VARINT", options); + auto transform(sourcemeta::jsontoolkit::SchemaTransformer &schema) const + -> void override { + auto minimum{schema.at("minimum")}; + auto options{sourcemeta::jsontoolkit::JSON::make_object()}; + options.assign("minimum", std::move(minimum)); + options.assign("multiplier", sourcemeta::jsontoolkit::JSON{1}); + make_encoding(schema, "FLOOR_MULTIPLE_ENUM_VARINT", options); } }; diff --git a/src/mapper/rules/integer_bounded_multiplier_8_bit.h b/src/mapper/rules/integer_bounded_multiplier_8_bit.h index b5ac9fada..668463b3d 100644 --- a/src/mapper/rules/integer_bounded_multiplier_8_bit.h +++ b/src/mapper/rules/integer_bounded_multiplier_8_bit.h @@ -2,27 +2,25 @@ namespace sourcemeta::jsonbinpack::mapper { /// @ingroup mapper_rules class IntegerBoundedMultiplier8Bit final - : public sourcemeta::alterschema::Rule { + : public sourcemeta::jsontoolkit::SchemaTransformRule { public: - IntegerBoundedMultiplier8Bit() : Rule("integer_bounded_multiplier_8_bit"){}; + IntegerBoundedMultiplier8Bit() + : sourcemeta::jsontoolkit::SchemaTransformRule( + "integer_bounded_multiplier_8_bit"){}; - [[nodiscard]] auto - condition(const sourcemeta::jsontoolkit::Value &schema, - const std::string &draft, - const std::unordered_map &vocabularies, - const std::size_t) const -> bool override { + [[nodiscard]] auto condition(const sourcemeta::jsontoolkit::JSON &schema, + const std::string &dialect, + const std::set &vocabularies, + const sourcemeta::jsontoolkit::Pointer &) const + -> bool override { if (is_encoding(schema) || - draft != "https://json-schema.org/draft/2020-12/schema" || + dialect != "https://json-schema.org/draft/2020-12/schema" || !vocabularies.contains( "https://json-schema.org/draft/2020-12/vocab/validation") || - !sourcemeta::jsontoolkit::defines(schema, "type") || - sourcemeta::jsontoolkit::to_string( - sourcemeta::jsontoolkit::at(schema, "type")) != "integer" || - !sourcemeta::jsontoolkit::defines(schema, "minimum") || - !sourcemeta::jsontoolkit::defines(schema, "maximum") || - !sourcemeta::jsontoolkit::defines(schema, "multipleOf") || - !sourcemeta::jsontoolkit::is_integer( - sourcemeta::jsontoolkit::at(schema, "multipleOf"))) { + !schema.defines("type") || schema.at("type").to_string() != "integer" || + !schema.defines("minimum") || !schema.defines("maximum") || + !schema.defines("multipleOf") || + !schema.at("multipleOf").is_integer()) { return false; } @@ -32,24 +30,17 @@ class IntegerBoundedMultiplier8Bit final states->size() <= std::numeric_limits::max(); } - auto transform(sourcemeta::jsontoolkit::JSON &document, - sourcemeta::jsontoolkit::Value &value) const -> void override { - const auto minimum{sourcemeta::jsontoolkit::to_integer( - sourcemeta::jsontoolkit::at(value, "minimum"))}; - const auto maximum{sourcemeta::jsontoolkit::to_integer( - sourcemeta::jsontoolkit::at(value, "maximum"))}; - const auto multiplier{sourcemeta::jsontoolkit::to_integer( - sourcemeta::jsontoolkit::at(value, "multipleOf"))}; + auto transform(sourcemeta::jsontoolkit::SchemaTransformer &schema) const + -> void override { + auto minimum{schema.at("minimum")}; + auto maximum{schema.at("maximum")}; + auto multiplier{schema.at("multipleOf")}; - auto options{sourcemeta::jsontoolkit::make_object()}; - sourcemeta::jsontoolkit::assign(options, "minimum", - sourcemeta::jsontoolkit::from(minimum)); - sourcemeta::jsontoolkit::assign(options, "maximum", - sourcemeta::jsontoolkit::from(maximum)); - sourcemeta::jsontoolkit::assign(options, "multiplier", - sourcemeta::jsontoolkit::from(multiplier)); - make_encoding(document, value, "BOUNDED_MULTIPLE_8BITS_ENUM_FIXED", - options); + auto options{sourcemeta::jsontoolkit::JSON::make_object()}; + options.assign("minimum", std::move(minimum)); + options.assign("maximum", std::move(maximum)); + options.assign("multiplier", std::move(multiplier)); + make_encoding(schema, "BOUNDED_MULTIPLE_8BITS_ENUM_FIXED", options); } }; diff --git a/src/mapper/rules/integer_bounded_multiplier_greater_than_8_bit.h b/src/mapper/rules/integer_bounded_multiplier_greater_than_8_bit.h index 6f10d2f30..f3e9de25b 100644 --- a/src/mapper/rules/integer_bounded_multiplier_greater_than_8_bit.h +++ b/src/mapper/rules/integer_bounded_multiplier_greater_than_8_bit.h @@ -1,4 +1,4 @@ -#include +#include #include // std::uint8_t #include // std::numeric_limits @@ -7,28 +7,25 @@ namespace sourcemeta::jsonbinpack::mapper { /// @ingroup mapper_rules class IntegerBoundedMultiplierGreaterThan8Bit final - : public sourcemeta::alterschema::Rule { + : public sourcemeta::jsontoolkit::SchemaTransformRule { public: IntegerBoundedMultiplierGreaterThan8Bit() - : Rule("integer_bounded_multiplier_greater_than_8_bit"){}; - - [[nodiscard]] auto - condition(const sourcemeta::jsontoolkit::Value &schema, - const std::string &draft, - const std::unordered_map &vocabularies, - const std::size_t) const -> bool override { + : sourcemeta::jsontoolkit::SchemaTransformRule( + "integer_bounded_multiplier_greater_than_8_bit"){}; + + [[nodiscard]] auto condition(const sourcemeta::jsontoolkit::JSON &schema, + const std::string &dialect, + const std::set &vocabularies, + const sourcemeta::jsontoolkit::Pointer &) const + -> bool override { if (is_encoding(schema) || - draft != "https://json-schema.org/draft/2020-12/schema" || + dialect != "https://json-schema.org/draft/2020-12/schema" || !vocabularies.contains( "https://json-schema.org/draft/2020-12/vocab/validation") || - !sourcemeta::jsontoolkit::defines(schema, "type") || - sourcemeta::jsontoolkit::to_string( - sourcemeta::jsontoolkit::at(schema, "type")) != "integer" || - !sourcemeta::jsontoolkit::defines(schema, "minimum") || - !sourcemeta::jsontoolkit::defines(schema, "maximum") || - !sourcemeta::jsontoolkit::defines(schema, "multipleOf") || - !sourcemeta::jsontoolkit::is_integer( - sourcemeta::jsontoolkit::at(schema, "multipleOf"))) { + !schema.defines("type") || schema.at("type").to_string() != "integer" || + !schema.defines("minimum") || !schema.defines("maximum") || + !schema.defines("multipleOf") || + !schema.at("multipleOf").is_integer()) { return false; } @@ -37,19 +34,14 @@ class IntegerBoundedMultiplierGreaterThan8Bit final return !states.has_value() || !is_byte(states->size()); } - auto transform(sourcemeta::jsontoolkit::JSON &document, - sourcemeta::jsontoolkit::Value &value) const -> void override { - const auto minimum{sourcemeta::jsontoolkit::to_integer( - sourcemeta::jsontoolkit::at(value, "minimum"))}; - const auto multiplier{sourcemeta::jsontoolkit::to_integer( - sourcemeta::jsontoolkit::at(value, "multipleOf"))}; - - auto options{sourcemeta::jsontoolkit::make_object()}; - sourcemeta::jsontoolkit::assign(options, "minimum", - sourcemeta::jsontoolkit::from(minimum)); - sourcemeta::jsontoolkit::assign(options, "multiplier", - sourcemeta::jsontoolkit::from(multiplier)); - make_encoding(document, value, "FLOOR_MULTIPLE_ENUM_VARINT", options); + auto transform(sourcemeta::jsontoolkit::SchemaTransformer &schema) const + -> void override { + auto minimum{schema.at("minimum")}; + auto multiplier{schema.at("multipleOf")}; + auto options{sourcemeta::jsontoolkit::JSON::make_object()}; + options.assign("minimum", std::move(minimum)); + options.assign("multiplier", std::move(multiplier)); + make_encoding(schema, "FLOOR_MULTIPLE_ENUM_VARINT", options); } }; diff --git a/src/mapper/rules/integer_lower_bound.h b/src/mapper/rules/integer_lower_bound.h index 975014ae3..f45489e2b 100644 --- a/src/mapper/rules/integer_lower_bound.h +++ b/src/mapper/rules/integer_lower_bound.h @@ -1,37 +1,34 @@ namespace sourcemeta::jsonbinpack::mapper { /// @ingroup mapper_rules -class IntegerLowerBound final : public sourcemeta::alterschema::Rule { +class IntegerLowerBound final + : public sourcemeta::jsontoolkit::SchemaTransformRule { public: - IntegerLowerBound() : Rule("integer_lower_bound"){}; + IntegerLowerBound() + : sourcemeta::jsontoolkit::SchemaTransformRule("integer_lower_bound"){}; - [[nodiscard]] auto - condition(const sourcemeta::jsontoolkit::Value &schema, - const std::string &draft, - const std::unordered_map &vocabularies, - const std::size_t) const -> bool override { + [[nodiscard]] auto condition(const sourcemeta::jsontoolkit::JSON &schema, + const std::string &dialect, + const std::set &vocabularies, + const sourcemeta::jsontoolkit::Pointer &) const + -> bool override { return !is_encoding(schema) && - draft == "https://json-schema.org/draft/2020-12/schema" && + dialect == "https://json-schema.org/draft/2020-12/schema" && vocabularies.contains( "https://json-schema.org/draft/2020-12/vocab/validation") && - sourcemeta::jsontoolkit::defines(schema, "type") && - sourcemeta::jsontoolkit::to_string( - sourcemeta::jsontoolkit::at(schema, "type")) == "integer" && - sourcemeta::jsontoolkit::defines(schema, "minimum") && - !sourcemeta::jsontoolkit::defines(schema, "maximum") && - !sourcemeta::jsontoolkit::defines(schema, "multipleOf"); + schema.defines("type") && + schema.at("type").to_string() == "integer" && + schema.defines("minimum") && !schema.defines("maximum") && + !schema.defines("multipleOf"); } - auto transform(sourcemeta::jsontoolkit::JSON &document, - sourcemeta::jsontoolkit::Value &value) const -> void override { - const auto minimum{sourcemeta::jsontoolkit::to_integer( - sourcemeta::jsontoolkit::at(value, "minimum"))}; - auto options{sourcemeta::jsontoolkit::make_object()}; - sourcemeta::jsontoolkit::assign(options, "minimum", - sourcemeta::jsontoolkit::from(minimum)); - sourcemeta::jsontoolkit::assign(options, "multiplier", - sourcemeta::jsontoolkit::from(1)); - make_encoding(document, value, "FLOOR_MULTIPLE_ENUM_VARINT", options); + auto transform(sourcemeta::jsontoolkit::SchemaTransformer &schema) const + -> void override { + auto minimum{schema.at("minimum")}; + auto options{sourcemeta::jsontoolkit::JSON::make_object()}; + options.assign("minimum", std::move(minimum)); + options.assign("multiplier", sourcemeta::jsontoolkit::JSON{1}); + make_encoding(schema, "FLOOR_MULTIPLE_ENUM_VARINT", options); } }; diff --git a/src/mapper/rules/integer_lower_bound_multiplier.h b/src/mapper/rules/integer_lower_bound_multiplier.h index ace0c54ad..628d02a44 100644 --- a/src/mapper/rules/integer_lower_bound_multiplier.h +++ b/src/mapper/rules/integer_lower_bound_multiplier.h @@ -1,41 +1,36 @@ namespace sourcemeta::jsonbinpack::mapper { /// @ingroup mapper_rules -class IntegerLowerBoundMultiplier final : public sourcemeta::alterschema::Rule { +class IntegerLowerBoundMultiplier final + : public sourcemeta::jsontoolkit::SchemaTransformRule { public: - IntegerLowerBoundMultiplier() : Rule("integer_lower_bound_multiplier"){}; + IntegerLowerBoundMultiplier() + : sourcemeta::jsontoolkit::SchemaTransformRule( + "integer_lower_bound_multiplier"){}; - [[nodiscard]] auto - condition(const sourcemeta::jsontoolkit::Value &schema, - const std::string &draft, - const std::unordered_map &vocabularies, - const std::size_t) const -> bool override { + [[nodiscard]] auto condition(const sourcemeta::jsontoolkit::JSON &schema, + const std::string &dialect, + const std::set &vocabularies, + const sourcemeta::jsontoolkit::Pointer &) const + -> bool override { return !is_encoding(schema) && - draft == "https://json-schema.org/draft/2020-12/schema" && + dialect == "https://json-schema.org/draft/2020-12/schema" && vocabularies.contains( "https://json-schema.org/draft/2020-12/vocab/validation") && - sourcemeta::jsontoolkit::defines(schema, "type") && - sourcemeta::jsontoolkit::to_string( - sourcemeta::jsontoolkit::at(schema, "type")) == "integer" && - sourcemeta::jsontoolkit::defines(schema, "minimum") && - !sourcemeta::jsontoolkit::defines(schema, "maximum") && - sourcemeta::jsontoolkit::defines(schema, "multipleOf") && - sourcemeta::jsontoolkit::is_integer( - sourcemeta::jsontoolkit::at(schema, "multipleOf")); + schema.defines("type") && + schema.at("type").to_string() == "integer" && + schema.defines("minimum") && !schema.defines("maximum") && + schema.defines("multipleOf") && schema.at("multipleOf").is_integer(); } - auto transform(sourcemeta::jsontoolkit::JSON &document, - sourcemeta::jsontoolkit::Value &value) const -> void override { - const auto minimum{sourcemeta::jsontoolkit::to_integer( - sourcemeta::jsontoolkit::at(value, "minimum"))}; - const auto multiplier{sourcemeta::jsontoolkit::to_integer( - sourcemeta::jsontoolkit::at(value, "multipleOf"))}; - auto options{sourcemeta::jsontoolkit::make_object()}; - sourcemeta::jsontoolkit::assign(options, "minimum", - sourcemeta::jsontoolkit::from(minimum)); - sourcemeta::jsontoolkit::assign(options, "multiplier", - sourcemeta::jsontoolkit::from(multiplier)); - make_encoding(document, value, "FLOOR_MULTIPLE_ENUM_VARINT", options); + auto transform(sourcemeta::jsontoolkit::SchemaTransformer &schema) const + -> void override { + auto minimum{schema.at("minimum")}; + auto multiplier{schema.at("multipleOf")}; + auto options{sourcemeta::jsontoolkit::JSON::make_object()}; + options.assign("minimum", std::move(minimum)); + options.assign("multiplier", std::move(multiplier)); + make_encoding(schema, "FLOOR_MULTIPLE_ENUM_VARINT", options); } }; diff --git a/src/mapper/rules/integer_unbound.h b/src/mapper/rules/integer_unbound.h index 7d4d64efb..918230bd1 100644 --- a/src/mapper/rules/integer_unbound.h +++ b/src/mapper/rules/integer_unbound.h @@ -1,33 +1,32 @@ namespace sourcemeta::jsonbinpack::mapper { /// @ingroup mapper_rules -class IntegerUnbound final : public sourcemeta::alterschema::Rule { +class IntegerUnbound final + : public sourcemeta::jsontoolkit::SchemaTransformRule { public: - IntegerUnbound() : Rule("integer_unbound"){}; + IntegerUnbound() + : sourcemeta::jsontoolkit::SchemaTransformRule("integer_unbound"){}; - [[nodiscard]] auto - condition(const sourcemeta::jsontoolkit::Value &schema, - const std::string &draft, - const std::unordered_map &vocabularies, - const std::size_t) const -> bool override { + [[nodiscard]] auto condition(const sourcemeta::jsontoolkit::JSON &schema, + const std::string &dialect, + const std::set &vocabularies, + const sourcemeta::jsontoolkit::Pointer &) const + -> bool override { return !is_encoding(schema) && - draft == "https://json-schema.org/draft/2020-12/schema" && + dialect == "https://json-schema.org/draft/2020-12/schema" && vocabularies.contains( "https://json-schema.org/draft/2020-12/vocab/validation") && - sourcemeta::jsontoolkit::defines(schema, "type") && - sourcemeta::jsontoolkit::to_string( - sourcemeta::jsontoolkit::at(schema, "type")) == "integer" && - !sourcemeta::jsontoolkit::defines(schema, "minimum") && - !sourcemeta::jsontoolkit::defines(schema, "maximum") && - !sourcemeta::jsontoolkit::defines(schema, "multipleOf"); + schema.defines("type") && + schema.at("type").to_string() == "integer" && + !schema.defines("minimum") && !schema.defines("maximum") && + !schema.defines("multipleOf"); } - auto transform(sourcemeta::jsontoolkit::JSON &document, - sourcemeta::jsontoolkit::Value &value) const -> void override { - auto options{sourcemeta::jsontoolkit::make_object()}; - sourcemeta::jsontoolkit::assign(options, "multiplier", - sourcemeta::jsontoolkit::from(1)); - make_encoding(document, value, "ARBITRARY_MULTIPLE_ZIGZAG_VARINT", options); + auto transform(sourcemeta::jsontoolkit::SchemaTransformer &schema) const + -> void override { + auto options{sourcemeta::jsontoolkit::JSON::make_object()}; + options.assign("multiplier", sourcemeta::jsontoolkit::JSON{1}); + make_encoding(schema, "ARBITRARY_MULTIPLE_ZIGZAG_VARINT", options); } }; diff --git a/src/mapper/rules/integer_unbound_multiplier.h b/src/mapper/rules/integer_unbound_multiplier.h index 755a4e17d..a9f6002d3 100644 --- a/src/mapper/rules/integer_unbound_multiplier.h +++ b/src/mapper/rules/integer_unbound_multiplier.h @@ -1,37 +1,34 @@ namespace sourcemeta::jsonbinpack::mapper { /// @ingroup mapper_rules -class IntegerUnboundMultiplier final : public sourcemeta::alterschema::Rule { +class IntegerUnboundMultiplier final + : public sourcemeta::jsontoolkit::SchemaTransformRule { public: - IntegerUnboundMultiplier() : Rule("integer_unbound_multiplier"){}; + IntegerUnboundMultiplier() + : sourcemeta::jsontoolkit::SchemaTransformRule( + "integer_unbound_multiplier"){}; - [[nodiscard]] auto - condition(const sourcemeta::jsontoolkit::Value &schema, - const std::string &draft, - const std::unordered_map &vocabularies, - const std::size_t) const -> bool override { + [[nodiscard]] auto condition(const sourcemeta::jsontoolkit::JSON &schema, + const std::string &dialect, + const std::set &vocabularies, + const sourcemeta::jsontoolkit::Pointer &) const + -> bool override { return !is_encoding(schema) && - draft == "https://json-schema.org/draft/2020-12/schema" && + dialect == "https://json-schema.org/draft/2020-12/schema" && vocabularies.contains( "https://json-schema.org/draft/2020-12/vocab/validation") && - sourcemeta::jsontoolkit::defines(schema, "type") && - sourcemeta::jsontoolkit::to_string( - sourcemeta::jsontoolkit::at(schema, "type")) == "integer" && - !sourcemeta::jsontoolkit::defines(schema, "minimum") && - !sourcemeta::jsontoolkit::defines(schema, "maximum") && - sourcemeta::jsontoolkit::defines(schema, "multipleOf") && - sourcemeta::jsontoolkit::is_integer( - sourcemeta::jsontoolkit::at(schema, "multipleOf")); + schema.defines("type") && + schema.at("type").to_string() == "integer" && + !schema.defines("minimum") && !schema.defines("maximum") && + schema.defines("multipleOf") && schema.at("multipleOf").is_integer(); } - auto transform(sourcemeta::jsontoolkit::JSON &document, - sourcemeta::jsontoolkit::Value &value) const -> void override { - const auto multiplier{sourcemeta::jsontoolkit::to_integer( - sourcemeta::jsontoolkit::at(value, "multipleOf"))}; - auto options{sourcemeta::jsontoolkit::make_object()}; - sourcemeta::jsontoolkit::assign(options, "multiplier", - sourcemeta::jsontoolkit::from(multiplier)); - make_encoding(document, value, "ARBITRARY_MULTIPLE_ZIGZAG_VARINT", options); + auto transform(sourcemeta::jsontoolkit::SchemaTransformer &schema) const + -> void override { + auto multiplier{schema.at("multipleOf")}; + auto options{sourcemeta::jsontoolkit::JSON::make_object()}; + options.assign("multiplier", std::move(multiplier)); + make_encoding(schema, "ARBITRARY_MULTIPLE_ZIGZAG_VARINT", options); } }; diff --git a/src/mapper/rules/integer_upper_bound.h b/src/mapper/rules/integer_upper_bound.h index 8d51b4c57..323f5d06a 100644 --- a/src/mapper/rules/integer_upper_bound.h +++ b/src/mapper/rules/integer_upper_bound.h @@ -1,37 +1,34 @@ namespace sourcemeta::jsonbinpack::mapper { /// @ingroup mapper_rules -class IntegerUpperBound final : public sourcemeta::alterschema::Rule { +class IntegerUpperBound final + : public sourcemeta::jsontoolkit::SchemaTransformRule { public: - IntegerUpperBound() : Rule("integer_upper_bound"){}; + IntegerUpperBound() + : sourcemeta::jsontoolkit::SchemaTransformRule("integer_upper_bound"){}; - [[nodiscard]] auto - condition(const sourcemeta::jsontoolkit::Value &schema, - const std::string &draft, - const std::unordered_map &vocabularies, - const std::size_t) const -> bool override { + [[nodiscard]] auto condition(const sourcemeta::jsontoolkit::JSON &schema, + const std::string &dialect, + const std::set &vocabularies, + const sourcemeta::jsontoolkit::Pointer &) const + -> bool override { return !is_encoding(schema) && - draft == "https://json-schema.org/draft/2020-12/schema" && + dialect == "https://json-schema.org/draft/2020-12/schema" && vocabularies.contains( "https://json-schema.org/draft/2020-12/vocab/validation") && - sourcemeta::jsontoolkit::defines(schema, "type") && - sourcemeta::jsontoolkit::to_string( - sourcemeta::jsontoolkit::at(schema, "type")) == "integer" && - !sourcemeta::jsontoolkit::defines(schema, "minimum") && - sourcemeta::jsontoolkit::defines(schema, "maximum") && - !sourcemeta::jsontoolkit::defines(schema, "multipleOf"); + schema.defines("type") && + schema.at("type").to_string() == "integer" && + !schema.defines("minimum") && schema.defines("maximum") && + !schema.defines("multipleOf"); } - auto transform(sourcemeta::jsontoolkit::JSON &document, - sourcemeta::jsontoolkit::Value &value) const -> void override { - const auto maximum{sourcemeta::jsontoolkit::to_integer( - sourcemeta::jsontoolkit::at(value, "maximum"))}; - auto options{sourcemeta::jsontoolkit::make_object()}; - sourcemeta::jsontoolkit::assign(options, "maximum", - sourcemeta::jsontoolkit::from(maximum)); - sourcemeta::jsontoolkit::assign(options, "multiplier", - sourcemeta::jsontoolkit::from(1)); - make_encoding(document, value, "ROOF_MULTIPLE_MIRROR_ENUM_VARINT", options); + auto transform(sourcemeta::jsontoolkit::SchemaTransformer &schema) const + -> void override { + auto maximum{schema.at("maximum")}; + auto options{sourcemeta::jsontoolkit::JSON::make_object()}; + options.assign("maximum", std::move(maximum)); + options.assign("multiplier", sourcemeta::jsontoolkit::JSON{1}); + make_encoding(schema, "ROOF_MULTIPLE_MIRROR_ENUM_VARINT", options); } }; diff --git a/src/mapper/rules/integer_upper_bound_multiplier.h b/src/mapper/rules/integer_upper_bound_multiplier.h index 410754bdb..d0b46dede 100644 --- a/src/mapper/rules/integer_upper_bound_multiplier.h +++ b/src/mapper/rules/integer_upper_bound_multiplier.h @@ -1,41 +1,36 @@ namespace sourcemeta::jsonbinpack::mapper { /// @ingroup mapper_rules -class IntegerUpperBoundMultiplier final : public sourcemeta::alterschema::Rule { +class IntegerUpperBoundMultiplier final + : public sourcemeta::jsontoolkit::SchemaTransformRule { public: - IntegerUpperBoundMultiplier() : Rule("integer_upper_bound_multiplier"){}; + IntegerUpperBoundMultiplier() + : sourcemeta::jsontoolkit::SchemaTransformRule( + "integer_upper_bound_multiplier"){}; - [[nodiscard]] auto - condition(const sourcemeta::jsontoolkit::Value &schema, - const std::string &draft, - const std::unordered_map &vocabularies, - const std::size_t) const -> bool override { + [[nodiscard]] auto condition(const sourcemeta::jsontoolkit::JSON &schema, + const std::string &dialect, + const std::set &vocabularies, + const sourcemeta::jsontoolkit::Pointer &) const + -> bool override { return !is_encoding(schema) && - draft == "https://json-schema.org/draft/2020-12/schema" && + dialect == "https://json-schema.org/draft/2020-12/schema" && vocabularies.contains( "https://json-schema.org/draft/2020-12/vocab/validation") && - sourcemeta::jsontoolkit::defines(schema, "type") && - sourcemeta::jsontoolkit::to_string( - sourcemeta::jsontoolkit::at(schema, "type")) == "integer" && - !sourcemeta::jsontoolkit::defines(schema, "minimum") && - sourcemeta::jsontoolkit::defines(schema, "maximum") && - sourcemeta::jsontoolkit::defines(schema, "multipleOf") && - sourcemeta::jsontoolkit::is_integer( - sourcemeta::jsontoolkit::at(schema, "multipleOf")); + schema.defines("type") && + schema.at("type").to_string() == "integer" && + !schema.defines("minimum") && schema.defines("maximum") && + schema.defines("multipleOf") && schema.at("multipleOf").is_integer(); } - auto transform(sourcemeta::jsontoolkit::JSON &document, - sourcemeta::jsontoolkit::Value &value) const -> void override { - const auto maximum{sourcemeta::jsontoolkit::to_integer( - sourcemeta::jsontoolkit::at(value, "maximum"))}; - const auto multiplier{sourcemeta::jsontoolkit::to_integer( - sourcemeta::jsontoolkit::at(value, "multipleOf"))}; - auto options{sourcemeta::jsontoolkit::make_object()}; - sourcemeta::jsontoolkit::assign(options, "maximum", - sourcemeta::jsontoolkit::from(maximum)); - sourcemeta::jsontoolkit::assign(options, "multiplier", - sourcemeta::jsontoolkit::from(multiplier)); - make_encoding(document, value, "ROOF_MULTIPLE_MIRROR_ENUM_VARINT", options); + auto transform(sourcemeta::jsontoolkit::SchemaTransformer &schema) const + -> void override { + auto maximum{schema.at("maximum")}; + auto multiplier{schema.at("multipleOf")}; + auto options{sourcemeta::jsontoolkit::JSON::make_object()}; + options.assign("maximum", std::move(maximum)); + options.assign("multiplier", std::move(multiplier)); + make_encoding(schema, "ROOF_MULTIPLE_MIRROR_ENUM_VARINT", options); } }; diff --git a/src/mapper/rules/number_arbitrary.h b/src/mapper/rules/number_arbitrary.h index ce71f1c7f..5eb7b8681 100644 --- a/src/mapper/rules/number_arbitrary.h +++ b/src/mapper/rules/number_arbitrary.h @@ -1,28 +1,28 @@ namespace sourcemeta::jsonbinpack::mapper { /// @ingroup mapper_rules -class NumberArbitrary final : public sourcemeta::alterschema::Rule { +class NumberArbitrary final + : public sourcemeta::jsontoolkit::SchemaTransformRule { public: - NumberArbitrary() : Rule("number_arbitrary"){}; + NumberArbitrary() + : sourcemeta::jsontoolkit::SchemaTransformRule("number_arbitrary"){}; - [[nodiscard]] auto - condition(const sourcemeta::jsontoolkit::Value &schema, - const std::string &draft, - const std::unordered_map &vocabularies, - const std::size_t) const -> bool override { + [[nodiscard]] auto condition(const sourcemeta::jsontoolkit::JSON &schema, + const std::string &dialect, + const std::set &vocabularies, + const sourcemeta::jsontoolkit::Pointer &) const + -> bool override { return !is_encoding(schema) && - draft == "https://json-schema.org/draft/2020-12/schema" && + dialect == "https://json-schema.org/draft/2020-12/schema" && vocabularies.contains( "https://json-schema.org/draft/2020-12/vocab/validation") && - sourcemeta::jsontoolkit::defines(schema, "type") && - sourcemeta::jsontoolkit::to_string( - sourcemeta::jsontoolkit::at(schema, "type")) == "number"; + schema.defines("type") && schema.at("type").to_string() == "number"; } - auto transform(sourcemeta::jsontoolkit::JSON &document, - sourcemeta::jsontoolkit::Value &value) const -> void override { - make_encoding(document, value, "DOUBLE_VARINT_TUPLE", - sourcemeta::jsontoolkit::make_object()); + auto transform(sourcemeta::jsontoolkit::SchemaTransformer &schema) const + -> void override { + make_encoding(schema, "DOUBLE_VARINT_TUPLE", + sourcemeta::jsontoolkit::JSON::make_object()); } }; diff --git a/src/mapper/states.cc b/src/mapper/states.cc index 61d81b568..b7dc3f65b 100644 --- a/src/mapper/states.cc +++ b/src/mapper/states.cc @@ -1,74 +1,56 @@ -#include +#include #include // assert #include // std::abs, std::ceil, std::floor #include // std::vector auto sourcemeta::jsonbinpack::mapper::states::integer( - const sourcemeta::jsontoolkit::Value &schema, - const std::unordered_map &vocabularies) + const sourcemeta::jsontoolkit::JSON &schema, + const std::set &vocabularies) -> std::optional> { - assert(sourcemeta::jsontoolkit::is_object(schema)); - assert(sourcemeta::jsontoolkit::defines(schema, "type")); - assert(sourcemeta::jsontoolkit::to_string( - sourcemeta::jsontoolkit::at(schema, "type")) == "integer"); + assert(schema.is_object()); + assert(schema.defines("type")); + assert(schema.at("type").is_string()); + assert(schema.at("type").to_string() == "integer"); if (!vocabularies.contains( "https://json-schema.org/draft/2020-12/vocab/validation")) { return std::nullopt; } // The integer is unbounded by definition - if (!sourcemeta::jsontoolkit::defines(schema, "minimum") || - !sourcemeta::jsontoolkit::defines(schema, "maximum")) { + if (!schema.defines("minimum") || !schema.defines("maximum")) { return std::nullopt; } // The bounds must be numeric - assert(sourcemeta::jsontoolkit::is_integer( - sourcemeta::jsontoolkit::at(schema, "minimum")) || - sourcemeta::jsontoolkit::is_real( - sourcemeta::jsontoolkit::at(schema, "minimum"))); - assert(sourcemeta::jsontoolkit::is_integer( - sourcemeta::jsontoolkit::at(schema, "maximum")) || - sourcemeta::jsontoolkit::is_real( - sourcemeta::jsontoolkit::at(schema, "maximum"))); + assert(schema.at("minimum").is_number()); + assert(schema.at("maximum").is_number()); // Get bounds as integers - const std::int64_t minimum{ - sourcemeta::jsontoolkit::is_integer( - sourcemeta::jsontoolkit::at(schema, "minimum")) - ? sourcemeta::jsontoolkit::to_integer( - sourcemeta::jsontoolkit::at(schema, "minimum")) - : static_cast( - std::ceil(sourcemeta::jsontoolkit::to_real( - sourcemeta::jsontoolkit::at(schema, "minimum"))))}; - const std::int64_t maximum{ - sourcemeta::jsontoolkit::is_integer( - sourcemeta::jsontoolkit::at(schema, "maximum")) - ? sourcemeta::jsontoolkit::to_integer( - sourcemeta::jsontoolkit::at(schema, "maximum")) - : static_cast( - std::floor(sourcemeta::jsontoolkit::to_real( - sourcemeta::jsontoolkit::at(schema, "maximum"))))}; + const std::int64_t minimum{schema.at("minimum").is_integer() + ? schema.at("minimum").to_integer() + : static_cast(std::ceil( + schema.at("minimum").to_real()))}; + const std::int64_t maximum{schema.at("maximum").is_integer() + ? schema.at("maximum").to_integer() + : static_cast(std::floor( + schema.at("maximum").to_real()))}; // For simplicity, we do not support real multipliers yet. // It does not come up in practice much. - if (sourcemeta::jsontoolkit::defines(schema, "multipleOf") && - sourcemeta::jsontoolkit::is_real( - sourcemeta::jsontoolkit::at(schema, "multipleOf"))) { + if (schema.defines("multipleOf") && schema.at("multipleOf").is_real()) { return std::nullopt; } const std::int64_t multiplier{ - sourcemeta::jsontoolkit::defines(schema, "multipleOf") - ? std::abs(sourcemeta::jsontoolkit::to_integer( - sourcemeta::jsontoolkit::at(schema, "multipleOf"))) + schema.defines("multipleOf") + ? std::abs(schema.at("multipleOf").to_integer()) : 1}; std::vector states; for (std::int64_t state = minimum; state <= maximum; state++) { if (state % multiplier == 0) { - states.push_back(sourcemeta::jsontoolkit::from(state)); + states.emplace_back(state); } } diff --git a/src/numeric/CMakeLists.txt b/src/numeric/CMakeLists.txt index c19a2dbf8..f97cc4e39 100644 --- a/src/numeric/CMakeLists.txt +++ b/src/numeric/CMakeLists.txt @@ -1,6 +1,8 @@ -add_library(sourcemeta_jsonbinpack_numeric INTERFACE - include/jsonbinpack/numeric/numeric.h) -target_include_directories(sourcemeta_jsonbinpack_numeric INTERFACE - "${CMAKE_CURRENT_SOURCE_DIR}/include") -set_target_properties(sourcemeta_jsonbinpack_numeric - PROPERTIES FOLDER "JSON BinPack/Numeric") +noa_library(NAMESPACE sourcemeta PROJECT jsonbinpack NAME numeric + FOLDER "JSON BinPack/Numeric") + +if(JSONBINPACK_INSTALL) + noa_library_install(NAMESPACE sourcemeta PROJECT jsonbinpack NAME numeric) +endif() + +sourcemeta_jsonbinpack_add_compile_options(INTERFACE sourcemeta_jsonbinpack_numeric) diff --git a/src/numeric/include/jsonbinpack/numeric/numeric.h b/src/numeric/include/sourcemeta/jsonbinpack/numeric.h similarity index 83% rename from src/numeric/include/jsonbinpack/numeric/numeric.h rename to src/numeric/include/sourcemeta/jsonbinpack/numeric.h index 584394f69..2f17e2aab 100644 --- a/src/numeric/include/jsonbinpack/numeric/numeric.h +++ b/src/numeric/include/sourcemeta/jsonbinpack/numeric.h @@ -1,5 +1,5 @@ -#ifndef SOURCEMETA_JSONBINPACK_NUMERIC_NUMERIC_H_ -#define SOURCEMETA_JSONBINPACK_NUMERIC_NUMERIC_H_ +#ifndef SOURCEMETA_JSONBINPACK_NUMERIC_H_ +#define SOURCEMETA_JSONBINPACK_NUMERIC_H_ #include // assert #include // std::abs @@ -30,10 +30,12 @@ constexpr auto divide_ceil(const std::int64_t dividend, return dividend; } else if (dividend >= 0) { // This branch guards against overflows - if (dividend + divisor < divisor) { - return (dividend / divisor) + 1 - (1 / divisor); + if (static_cast(dividend) + divisor < divisor) { + return static_cast( + (static_cast(dividend) / divisor) + 1 - (1 / divisor)); } else { - return (dividend + divisor - 1) / divisor; + return static_cast( + (static_cast(dividend) + divisor - 1) / divisor); } } else { // `dividend` is negative, so `abs(dividend)` is ensured to be positive @@ -54,7 +56,8 @@ constexpr auto divide_floor(const std::int64_t dividend, if (divisor == 1) { return dividend; } else if (dividend >= 0) { - return dividend / divisor; + return static_cast(static_cast(dividend) / + divisor); } else { const std::uint64_t absolute_dividend{ static_cast(std::abs(dividend))}; diff --git a/src/parser/CMakeLists.txt b/src/parser/CMakeLists.txt index 66326655b..f0699eb40 100644 --- a/src/parser/CMakeLists.txt +++ b/src/parser/CMakeLists.txt @@ -1,9 +1,14 @@ -add_library(sourcemeta_jsonbinpack_parser - include/jsonbinpack/parser/parser.h parser.cc - v1_any.h v1_array.h v1_enum.h v1_integer.h v1_number.h v1_string.h) -target_include_directories(sourcemeta_jsonbinpack_parser PUBLIC - "${CMAKE_CURRENT_SOURCE_DIR}/include") -target_link_libraries(sourcemeta_jsonbinpack_parser PUBLIC sourcemeta::jsontoolkit::json) -target_link_libraries(sourcemeta_jsonbinpack_parser PRIVATE sourcemeta_jsonbinpack_encoding) -set_target_properties(sourcemeta_jsonbinpack_parser - PROPERTIES FOLDER "JSON BinPack/Parser") +noa_library(NAMESPACE sourcemeta PROJECT jsonbinpack NAME parser + FOLDER "JSON BinPack/Parser" + SOURCES parser.cc v1_any.h v1_array.h v1_enum.h v1_integer.h v1_number.h v1_string.h) + +if(JSONBINPACK_INSTALL) + noa_library_install(NAMESPACE sourcemeta PROJECT jsonbinpack NAME parser) +endif() + +sourcemeta_jsonbinpack_add_compile_options(PRIVATE sourcemeta_jsonbinpack_parser) + +target_link_libraries(sourcemeta_jsonbinpack_parser PUBLIC + sourcemeta::jsontoolkit::json) +target_link_libraries(sourcemeta_jsonbinpack_parser PRIVATE + sourcemeta::jsonbinpack::encoding) diff --git a/src/parser/include/jsonbinpack/parser/parser.h b/src/parser/include/jsonbinpack/parser/parser.h deleted file mode 100644 index 013278cb9..000000000 --- a/src/parser/include/jsonbinpack/parser/parser.h +++ /dev/null @@ -1,13 +0,0 @@ -#ifndef SOURCEMETA_JSONBINPACK_PARSER_PARSER_H_ -#define SOURCEMETA_JSONBINPACK_PARSER_PARSER_H_ - -#include -#include - -namespace sourcemeta::jsonbinpack { - -auto parse(const sourcemeta::jsontoolkit::Value &input) -> Encoding; - -} // namespace sourcemeta::jsonbinpack - -#endif diff --git a/src/parser/include/sourcemeta/jsonbinpack/parser.h b/src/parser/include/sourcemeta/jsonbinpack/parser.h new file mode 100644 index 000000000..eb6a29210 --- /dev/null +++ b/src/parser/include/sourcemeta/jsonbinpack/parser.h @@ -0,0 +1,13 @@ +#ifndef SOURCEMETA_JSONBINPACK_PARSER_H_ +#define SOURCEMETA_JSONBINPACK_PARSER_H_ + +#include +#include + +namespace sourcemeta::jsonbinpack { + +auto parse(const sourcemeta::jsontoolkit::JSON &input) -> Encoding; + +} // namespace sourcemeta::jsonbinpack + +#endif diff --git a/src/parser/parser.cc b/src/parser/parser.cc index 6a69e69ed..e9283baf0 100644 --- a/src/parser/parser.cc +++ b/src/parser/parser.cc @@ -1,4 +1,4 @@ -#include +#include #include "v1_any.h" #include "v1_array.h" @@ -13,12 +13,11 @@ namespace sourcemeta::jsonbinpack { -auto parse(const sourcemeta::jsontoolkit::Value &input) -> Encoding { - assert(sourcemeta::jsontoolkit::defines(input, "name")); - assert(sourcemeta::jsontoolkit::defines(input, "options")); - const auto encoding{sourcemeta::jsontoolkit::to_string( - sourcemeta::jsontoolkit::at(input, "name"))}; - const auto &options{sourcemeta::jsontoolkit::at(input, "options")}; +auto parse(const sourcemeta::jsontoolkit::JSON &input) -> Encoding { + assert(input.defines("name")); + assert(input.defines("options")); + const auto encoding{input.at("name").to_string()}; + const auto &options{input.at("options")}; #define PARSE_ENCODING(version, name) \ if (encoding == #name) \ @@ -53,6 +52,7 @@ auto parse(const sourcemeta::jsontoolkit::Value &input) -> Encoding { #undef PARSE_ENCODING + // TODO: Have a custom error for this std::ostringstream error; error << "Unrecognized encoding: " << encoding; throw std::runtime_error(error.str()); diff --git a/src/parser/v1_any.h b/src/parser/v1_any.h index b6b6b8848..c5cff435a 100644 --- a/src/parser/v1_any.h +++ b/src/parser/v1_any.h @@ -1,12 +1,12 @@ #ifndef SOURCEMETA_JSONBINPACK_PARSER_V1_ANY_H_ #define SOURCEMETA_JSONBINPACK_PARSER_V1_ANY_H_ -#include -#include +#include +#include namespace sourcemeta::jsonbinpack::parser::v1 { -auto ANY_PACKED_TYPE_TAG_BYTE_PREFIX(const sourcemeta::jsontoolkit::Value &) +auto ANY_PACKED_TYPE_TAG_BYTE_PREFIX(const sourcemeta::jsontoolkit::JSON &) -> Encoding { return sourcemeta::jsonbinpack::ANY_PACKED_TYPE_TAG_BYTE_PREFIX{}; } diff --git a/src/parser/v1_array.h b/src/parser/v1_array.h index 761b7538d..16dce36f3 100644 --- a/src/parser/v1_array.h +++ b/src/parser/v1_array.h @@ -1,10 +1,10 @@ #ifndef SOURCEMETA_JSONBINPACK_PARSER_V1_ARRAY_H_ #define SOURCEMETA_JSONBINPACK_PARSER_V1_ARRAY_H_ -#include -#include -#include -#include +#include +#include +#include +#include #include // std::transform #include // assert @@ -14,104 +14,100 @@ namespace sourcemeta::jsonbinpack::parser::v1 { -auto FIXED_TYPED_ARRAY(const sourcemeta::jsontoolkit::Value &options) +auto FIXED_TYPED_ARRAY(const sourcemeta::jsontoolkit::JSON &options) -> Encoding { - assert(sourcemeta::jsontoolkit::defines(options, "size")); - assert(sourcemeta::jsontoolkit::defines(options, "encoding")); - assert(sourcemeta::jsontoolkit::defines(options, "prefixEncodings")); - const auto &size{sourcemeta::jsontoolkit::at(options, "size")}; - const auto &array_encoding{sourcemeta::jsontoolkit::at(options, "encoding")}; - const auto &prefix_encodings{ - sourcemeta::jsontoolkit::at(options, "prefixEncodings")}; - assert(sourcemeta::jsontoolkit::is_integer(size)); - assert(sourcemeta::jsontoolkit::is_positive(size)); - assert(sourcemeta::jsontoolkit::is_object(array_encoding)); - assert(sourcemeta::jsontoolkit::is_array(prefix_encodings)); + assert(options.defines("size")); + assert(options.defines("encoding")); + assert(options.defines("prefixEncodings")); + const auto &size{options.at("size")}; + const auto &array_encoding{options.at("encoding")}; + const auto &prefix_encodings{options.at("prefixEncodings")}; + assert(size.is_integer()); + assert(size.is_positive()); + assert(array_encoding.is_object()); + assert(prefix_encodings.is_array()); std::vector encodings; - std::transform(sourcemeta::jsontoolkit::cbegin_array(prefix_encodings), - sourcemeta::jsontoolkit::cend_array(prefix_encodings), + std::transform(prefix_encodings.as_array().cbegin(), + prefix_encodings.as_array().cend(), std::back_inserter(encodings), [](const auto &element) { return parse(element); }); - assert(encodings.size() == sourcemeta::jsontoolkit::size(prefix_encodings)); + assert(encodings.size() == prefix_encodings.size()); return sourcemeta::jsonbinpack::FIXED_TYPED_ARRAY{ - static_cast(sourcemeta::jsontoolkit::to_integer(size)), + static_cast(size.to_integer()), wrap(parse(array_encoding)), wrap(encodings.begin(), encodings.end())}; } -auto BOUNDED_8BITS_TYPED_ARRAY(const sourcemeta::jsontoolkit::Value &options) +auto BOUNDED_8BITS_TYPED_ARRAY(const sourcemeta::jsontoolkit::JSON &options) -> Encoding { - assert(sourcemeta::jsontoolkit::defines(options, "minimum")); - assert(sourcemeta::jsontoolkit::defines(options, "maximum")); - assert(sourcemeta::jsontoolkit::defines(options, "encoding")); - assert(sourcemeta::jsontoolkit::defines(options, "prefixEncodings")); - const auto &minimum{sourcemeta::jsontoolkit::at(options, "minimum")}; - const auto &maximum{sourcemeta::jsontoolkit::at(options, "maximum")}; - const auto &array_encoding{sourcemeta::jsontoolkit::at(options, "encoding")}; - const auto &prefix_encodings{ - sourcemeta::jsontoolkit::at(options, "prefixEncodings")}; - assert(sourcemeta::jsontoolkit::is_integer(minimum)); - assert(sourcemeta::jsontoolkit::is_integer(maximum)); - assert(sourcemeta::jsontoolkit::is_positive(minimum)); - assert(sourcemeta::jsontoolkit::is_positive(maximum)); - assert(sourcemeta::jsontoolkit::is_object(array_encoding)); - assert(sourcemeta::jsontoolkit::is_array(prefix_encodings)); + assert(options.defines("minimum")); + assert(options.defines("maximum")); + assert(options.defines("encoding")); + assert(options.defines("prefixEncodings")); + const auto &minimum{options.at("minimum")}; + const auto &maximum{options.at("maximum")}; + const auto &array_encoding{options.at("encoding")}; + const auto &prefix_encodings{options.at("prefixEncodings")}; + assert(minimum.is_integer()); + assert(maximum.is_integer()); + assert(minimum.is_positive()); + assert(maximum.is_positive()); + assert(array_encoding.is_object()); + assert(prefix_encodings.is_array()); std::vector encodings; - std::transform(sourcemeta::jsontoolkit::cbegin_array(prefix_encodings), - sourcemeta::jsontoolkit::cend_array(prefix_encodings), + std::transform(prefix_encodings.as_array().cbegin(), + prefix_encodings.as_array().cend(), std::back_inserter(encodings), [](const auto &element) { return parse(element); }); - assert(encodings.size() == sourcemeta::jsontoolkit::size(prefix_encodings)); + assert(encodings.size() == prefix_encodings.size()); return sourcemeta::jsonbinpack::BOUNDED_8BITS_TYPED_ARRAY{ - static_cast(sourcemeta::jsontoolkit::to_integer(minimum)), - static_cast(sourcemeta::jsontoolkit::to_integer(maximum)), + static_cast(minimum.to_integer()), + static_cast(maximum.to_integer()), wrap(parse(array_encoding)), wrap(encodings.begin(), encodings.end())}; } -auto FLOOR_TYPED_ARRAY(const sourcemeta::jsontoolkit::Value &options) +auto FLOOR_TYPED_ARRAY(const sourcemeta::jsontoolkit::JSON &options) -> Encoding { - assert(sourcemeta::jsontoolkit::defines(options, "minimum")); - assert(sourcemeta::jsontoolkit::defines(options, "encoding")); - assert(sourcemeta::jsontoolkit::defines(options, "prefixEncodings")); - const auto &minimum{sourcemeta::jsontoolkit::at(options, "minimum")}; - const auto &array_encoding{sourcemeta::jsontoolkit::at(options, "encoding")}; - const auto &prefix_encodings{ - sourcemeta::jsontoolkit::at(options, "prefixEncodings")}; - assert(sourcemeta::jsontoolkit::is_integer(minimum)); - assert(sourcemeta::jsontoolkit::is_positive(minimum)); - assert(sourcemeta::jsontoolkit::is_object(array_encoding)); - assert(sourcemeta::jsontoolkit::is_array(prefix_encodings)); + assert(options.defines("minimum")); + assert(options.defines("encoding")); + assert(options.defines("prefixEncodings")); + const auto &minimum{options.at("minimum")}; + const auto &array_encoding{options.at("encoding")}; + const auto &prefix_encodings{options.at("prefixEncodings")}; + assert(minimum.is_integer()); + assert(minimum.is_positive()); + assert(array_encoding.is_object()); + assert(prefix_encodings.is_array()); std::vector encodings; - std::transform(sourcemeta::jsontoolkit::cbegin_array(prefix_encodings), - sourcemeta::jsontoolkit::cend_array(prefix_encodings), + std::transform(prefix_encodings.as_array().cbegin(), + prefix_encodings.as_array().cend(), std::back_inserter(encodings), [](const auto &element) { return parse(element); }); - assert(encodings.size() == sourcemeta::jsontoolkit::size(prefix_encodings)); + assert(encodings.size() == prefix_encodings.size()); return sourcemeta::jsonbinpack::FLOOR_TYPED_ARRAY{ - static_cast(sourcemeta::jsontoolkit::to_integer(minimum)), + static_cast(minimum.to_integer()), wrap(parse(array_encoding)), wrap(encodings.begin(), encodings.end())}; } -auto ROOF_TYPED_ARRAY(const sourcemeta::jsontoolkit::Value &options) +auto ROOF_TYPED_ARRAY(const sourcemeta::jsontoolkit::JSON &options) -> Encoding { - assert(sourcemeta::jsontoolkit::defines(options, "maximum")); - assert(sourcemeta::jsontoolkit::defines(options, "encoding")); - assert(sourcemeta::jsontoolkit::defines(options, "prefixEncodings")); - const auto &maximum{sourcemeta::jsontoolkit::at(options, "maximum")}; - const auto &array_encoding{sourcemeta::jsontoolkit::at(options, "encoding")}; - const auto &prefix_encodings{ - sourcemeta::jsontoolkit::at(options, "prefixEncodings")}; - assert(sourcemeta::jsontoolkit::is_integer(maximum)); - assert(sourcemeta::jsontoolkit::is_positive(maximum)); - assert(sourcemeta::jsontoolkit::is_object(array_encoding)); - assert(sourcemeta::jsontoolkit::is_array(prefix_encodings)); + assert(options.defines("maximum")); + assert(options.defines("encoding")); + assert(options.defines("prefixEncodings")); + const auto &maximum{options.at("maximum")}; + const auto &array_encoding{options.at("encoding")}; + const auto &prefix_encodings{options.at("prefixEncodings")}; + assert(maximum.is_integer()); + assert(maximum.is_positive()); + assert(array_encoding.is_object()); + assert(prefix_encodings.is_array()); std::vector encodings; - std::transform(sourcemeta::jsontoolkit::cbegin_array(prefix_encodings), - sourcemeta::jsontoolkit::cend_array(prefix_encodings), + std::transform(prefix_encodings.as_array().cbegin(), + prefix_encodings.as_array().cend(), std::back_inserter(encodings), [](const auto &element) { return parse(element); }); - assert(encodings.size() == sourcemeta::jsontoolkit::size(prefix_encodings)); + assert(encodings.size() == prefix_encodings.size()); return sourcemeta::jsonbinpack::ROOF_TYPED_ARRAY{ - static_cast(sourcemeta::jsontoolkit::to_integer(maximum)), + static_cast(maximum.to_integer()), wrap(parse(array_encoding)), wrap(encodings.begin(), encodings.end())}; } diff --git a/src/parser/v1_enum.h b/src/parser/v1_enum.h index c6cfc23d6..35dec3a3f 100644 --- a/src/parser/v1_enum.h +++ b/src/parser/v1_enum.h @@ -1,44 +1,52 @@ #ifndef SOURCEMETA_JSONBINPACK_PARSER_V1_ENUM_H_ #define SOURCEMETA_JSONBINPACK_PARSER_V1_ENUM_H_ -#include -#include +#include +#include #include // assert +#include // std::move +#include // std::vector namespace sourcemeta::jsonbinpack::parser::v1 { -auto BYTE_CHOICE_INDEX(const sourcemeta::jsontoolkit::Value &options) +auto BYTE_CHOICE_INDEX(const sourcemeta::jsontoolkit::JSON &options) -> Encoding { - assert(sourcemeta::jsontoolkit::defines(options, "choices")); - const auto &choices{sourcemeta::jsontoolkit::at(options, "choices")}; - assert(sourcemeta::jsontoolkit::is_array(choices)); - return sourcemeta::jsonbinpack::BYTE_CHOICE_INDEX( - sourcemeta::jsontoolkit::to_vector(choices)); + assert(options.defines("choices")); + const auto &choices{options.at("choices")}; + assert(choices.is_array()); + const auto &array{choices.as_array()}; + std::vector elements{array.cbegin(), + array.cend()}; + return sourcemeta::jsonbinpack::BYTE_CHOICE_INDEX({std::move(elements)}); } -auto LARGE_CHOICE_INDEX(const sourcemeta::jsontoolkit::Value &options) +auto LARGE_CHOICE_INDEX(const sourcemeta::jsontoolkit::JSON &options) -> Encoding { - assert(sourcemeta::jsontoolkit::defines(options, "choices")); - const auto &choices{sourcemeta::jsontoolkit::at(options, "choices")}; - assert(sourcemeta::jsontoolkit::is_array(choices)); - return sourcemeta::jsonbinpack::LARGE_CHOICE_INDEX( - sourcemeta::jsontoolkit::to_vector(choices)); + assert(options.defines("choices")); + const auto &choices{options.at("choices")}; + assert(choices.is_array()); + const auto &array{choices.as_array()}; + std::vector elements{array.cbegin(), + array.cend()}; + return sourcemeta::jsonbinpack::LARGE_CHOICE_INDEX({std::move(elements)}); } -auto TOP_LEVEL_BYTE_CHOICE_INDEX(const sourcemeta::jsontoolkit::Value &options) +auto TOP_LEVEL_BYTE_CHOICE_INDEX(const sourcemeta::jsontoolkit::JSON &options) -> Encoding { - assert(sourcemeta::jsontoolkit::defines(options, "choices")); - const auto &choices{sourcemeta::jsontoolkit::at(options, "choices")}; - assert(sourcemeta::jsontoolkit::is_array(choices)); + assert(options.defines("choices")); + const auto &choices{options.at("choices")}; + assert(choices.is_array()); + const auto &array{choices.as_array()}; + std::vector elements{array.cbegin(), + array.cend()}; return sourcemeta::jsonbinpack::TOP_LEVEL_BYTE_CHOICE_INDEX( - sourcemeta::jsontoolkit::to_vector(choices)); + {std::move(elements)}); } -auto CONST_NONE(const sourcemeta::jsontoolkit::Value &options) -> Encoding { - assert(sourcemeta::jsontoolkit::defines(options, "value")); - return sourcemeta::jsonbinpack::CONST_NONE( - sourcemeta::jsontoolkit::at(options, "value")); +auto CONST_NONE(const sourcemeta::jsontoolkit::JSON &options) -> Encoding { + assert(options.defines("value")); + return sourcemeta::jsonbinpack::CONST_NONE({options.at("value")}); } } // namespace sourcemeta::jsonbinpack::parser::v1 diff --git a/src/parser/v1_integer.h b/src/parser/v1_integer.h index 0423bdede..72b15c0b0 100644 --- a/src/parser/v1_integer.h +++ b/src/parser/v1_integer.h @@ -1,8 +1,8 @@ #ifndef SOURCEMETA_JSONBINPACK_PARSER_V1_INTEGER_H_ #define SOURCEMETA_JSONBINPACK_PARSER_V1_INTEGER_H_ -#include -#include +#include +#include #include // assert #include // std::uint64_t @@ -10,63 +10,58 @@ namespace sourcemeta::jsonbinpack::parser::v1 { auto BOUNDED_MULTIPLE_8BITS_ENUM_FIXED( - const sourcemeta::jsontoolkit::Value &options) -> Encoding { - assert(sourcemeta::jsontoolkit::defines(options, "minimum")); - assert(sourcemeta::jsontoolkit::defines(options, "maximum")); - assert(sourcemeta::jsontoolkit::defines(options, "multiplier")); - const auto &minimum{sourcemeta::jsontoolkit::at(options, "minimum")}; - const auto &maximum{sourcemeta::jsontoolkit::at(options, "maximum")}; - const auto &multiplier{sourcemeta::jsontoolkit::at(options, "multiplier")}; - assert(sourcemeta::jsontoolkit::is_integer(minimum)); - assert(sourcemeta::jsontoolkit::is_integer(maximum)); - assert(sourcemeta::jsontoolkit::is_integer(multiplier)); - assert(sourcemeta::jsontoolkit::is_positive(multiplier)); + const sourcemeta::jsontoolkit::JSON &options) -> Encoding { + assert(options.defines("minimum")); + assert(options.defines("maximum")); + assert(options.defines("multiplier")); + const auto &minimum{options.at("minimum")}; + const auto &maximum{options.at("maximum")}; + const auto &multiplier{options.at("multiplier")}; + assert(minimum.is_integer()); + assert(maximum.is_integer()); + assert(multiplier.is_integer()); + assert(multiplier.is_positive()); return sourcemeta::jsonbinpack::BOUNDED_MULTIPLE_8BITS_ENUM_FIXED{ - sourcemeta::jsontoolkit::to_integer(minimum), - sourcemeta::jsontoolkit::to_integer(maximum), - static_cast( - sourcemeta::jsontoolkit::to_integer(multiplier))}; + minimum.to_integer(), maximum.to_integer(), + static_cast(multiplier.to_integer())}; } -auto FLOOR_MULTIPLE_ENUM_VARINT(const sourcemeta::jsontoolkit::Value &options) +auto FLOOR_MULTIPLE_ENUM_VARINT(const sourcemeta::jsontoolkit::JSON &options) -> Encoding { - assert(sourcemeta::jsontoolkit::defines(options, "minimum")); - assert(sourcemeta::jsontoolkit::defines(options, "multiplier")); - const auto &minimum{sourcemeta::jsontoolkit::at(options, "minimum")}; - const auto &multiplier{sourcemeta::jsontoolkit::at(options, "multiplier")}; - assert(sourcemeta::jsontoolkit::is_integer(minimum)); - assert(sourcemeta::jsontoolkit::is_integer(multiplier)); - assert(sourcemeta::jsontoolkit::is_positive(multiplier)); + assert(options.defines("minimum")); + assert(options.defines("multiplier")); + const auto &minimum{options.at("minimum")}; + const auto &multiplier{options.at("multiplier")}; + assert(minimum.is_integer()); + assert(multiplier.is_integer()); + assert(multiplier.is_positive()); return sourcemeta::jsonbinpack::FLOOR_MULTIPLE_ENUM_VARINT{ - sourcemeta::jsontoolkit::to_integer(minimum), - static_cast( - sourcemeta::jsontoolkit::to_integer(multiplier))}; + minimum.to_integer(), + static_cast(multiplier.to_integer())}; } auto ROOF_MULTIPLE_MIRROR_ENUM_VARINT( - const sourcemeta::jsontoolkit::Value &options) -> Encoding { - assert(sourcemeta::jsontoolkit::defines(options, "maximum")); - assert(sourcemeta::jsontoolkit::defines(options, "multiplier")); - const auto &maximum{sourcemeta::jsontoolkit::at(options, "maximum")}; - const auto &multiplier{sourcemeta::jsontoolkit::at(options, "multiplier")}; - assert(sourcemeta::jsontoolkit::is_integer(maximum)); - assert(sourcemeta::jsontoolkit::is_integer(multiplier)); - assert(sourcemeta::jsontoolkit::is_positive(multiplier)); + const sourcemeta::jsontoolkit::JSON &options) -> Encoding { + assert(options.defines("maximum")); + assert(options.defines("multiplier")); + const auto &maximum{options.at("maximum")}; + const auto &multiplier{options.at("multiplier")}; + assert(maximum.is_integer()); + assert(multiplier.is_integer()); + assert(multiplier.is_positive()); return sourcemeta::jsonbinpack::ROOF_MULTIPLE_MIRROR_ENUM_VARINT{ - sourcemeta::jsontoolkit::to_integer(maximum), - static_cast( - sourcemeta::jsontoolkit::to_integer(multiplier))}; + maximum.to_integer(), + static_cast(multiplier.to_integer())}; } auto ARBITRARY_MULTIPLE_ZIGZAG_VARINT( - const sourcemeta::jsontoolkit::Value &options) -> Encoding { - assert(sourcemeta::jsontoolkit::defines(options, "multiplier")); - const auto &multiplier{sourcemeta::jsontoolkit::at(options, "multiplier")}; - assert(sourcemeta::jsontoolkit::is_integer(multiplier)); - assert(sourcemeta::jsontoolkit::is_positive(multiplier)); + const sourcemeta::jsontoolkit::JSON &options) -> Encoding { + assert(options.defines("multiplier")); + const auto &multiplier{options.at("multiplier")}; + assert(multiplier.is_integer()); + assert(multiplier.is_positive()); return sourcemeta::jsonbinpack::ARBITRARY_MULTIPLE_ZIGZAG_VARINT{ - static_cast( - sourcemeta::jsontoolkit::to_integer(multiplier))}; + static_cast(multiplier.to_integer())}; } } // namespace sourcemeta::jsonbinpack::parser::v1 diff --git a/src/parser/v1_number.h b/src/parser/v1_number.h index 6251b0630..20fee6852 100644 --- a/src/parser/v1_number.h +++ b/src/parser/v1_number.h @@ -1,12 +1,12 @@ #ifndef SOURCEMETA_JSONBINPACK_PARSER_V1_NUMBER_H_ #define SOURCEMETA_JSONBINPACK_PARSER_V1_NUMBER_H_ -#include -#include +#include +#include namespace sourcemeta::jsonbinpack::parser::v1 { -auto DOUBLE_VARINT_TUPLE(const sourcemeta::jsontoolkit::Value &) -> Encoding { +auto DOUBLE_VARINT_TUPLE(const sourcemeta::jsontoolkit::JSON &) -> Encoding { return sourcemeta::jsonbinpack::DOUBLE_VARINT_TUPLE{}; } diff --git a/src/parser/v1_string.h b/src/parser/v1_string.h index 9bccf47eb..562681766 100644 --- a/src/parser/v1_string.h +++ b/src/parser/v1_string.h @@ -1,65 +1,65 @@ #ifndef SOURCEMETA_JSONBINPACK_PARSER_V1_STRING_H_ #define SOURCEMETA_JSONBINPACK_PARSER_V1_STRING_H_ -#include -#include +#include +#include #include // assert #include // std::uint64_t namespace sourcemeta::jsonbinpack::parser::v1 { -auto UTF8_STRING_NO_LENGTH(const sourcemeta::jsontoolkit::Value &options) +auto UTF8_STRING_NO_LENGTH(const sourcemeta::jsontoolkit::JSON &options) -> Encoding { - assert(sourcemeta::jsontoolkit::defines(options, "size")); - const auto &size{sourcemeta::jsontoolkit::at(options, "size")}; - assert(sourcemeta::jsontoolkit::is_integer(size)); - assert(sourcemeta::jsontoolkit::is_positive(size)); + assert(options.defines("size")); + const auto &size{options.at("size")}; + assert(size.is_integer()); + assert(size.is_positive()); return sourcemeta::jsonbinpack::UTF8_STRING_NO_LENGTH{ - static_cast(sourcemeta::jsontoolkit::to_integer(size))}; + static_cast(size.to_integer())}; } auto FLOOR_VARINT_PREFIX_UTF8_STRING_SHARED( - const sourcemeta::jsontoolkit::Value &options) -> Encoding { - assert(sourcemeta::jsontoolkit::defines(options, "minimum")); - const auto &minimum{sourcemeta::jsontoolkit::at(options, "minimum")}; - assert(sourcemeta::jsontoolkit::is_integer(minimum)); - assert(sourcemeta::jsontoolkit::is_positive(minimum)); + const sourcemeta::jsontoolkit::JSON &options) -> Encoding { + assert(options.defines("minimum")); + const auto &minimum{options.at("minimum")}; + assert(minimum.is_integer()); + assert(minimum.is_positive()); return sourcemeta::jsonbinpack::FLOOR_VARINT_PREFIX_UTF8_STRING_SHARED{ - static_cast(sourcemeta::jsontoolkit::to_integer(minimum))}; + static_cast(minimum.to_integer())}; } auto ROOF_VARINT_PREFIX_UTF8_STRING_SHARED( - const sourcemeta::jsontoolkit::Value &options) -> Encoding { - assert(sourcemeta::jsontoolkit::defines(options, "maximum")); - const auto &maximum{sourcemeta::jsontoolkit::at(options, "maximum")}; - assert(sourcemeta::jsontoolkit::is_integer(maximum)); - assert(sourcemeta::jsontoolkit::is_positive(maximum)); + const sourcemeta::jsontoolkit::JSON &options) -> Encoding { + assert(options.defines("maximum")); + const auto &maximum{options.at("maximum")}; + assert(maximum.is_integer()); + assert(maximum.is_positive()); return sourcemeta::jsonbinpack::ROOF_VARINT_PREFIX_UTF8_STRING_SHARED{ - static_cast(sourcemeta::jsontoolkit::to_integer(maximum))}; + static_cast(maximum.to_integer())}; } auto BOUNDED_8BIT_PREFIX_UTF8_STRING_SHARED( - const sourcemeta::jsontoolkit::Value &options) -> Encoding { - assert(sourcemeta::jsontoolkit::defines(options, "minimum")); - assert(sourcemeta::jsontoolkit::defines(options, "maximum")); - const auto &minimum{sourcemeta::jsontoolkit::at(options, "minimum")}; - const auto &maximum{sourcemeta::jsontoolkit::at(options, "maximum")}; - assert(sourcemeta::jsontoolkit::is_integer(minimum)); - assert(sourcemeta::jsontoolkit::is_integer(maximum)); - assert(sourcemeta::jsontoolkit::is_positive(minimum)); - assert(sourcemeta::jsontoolkit::is_positive(maximum)); + const sourcemeta::jsontoolkit::JSON &options) -> Encoding { + assert(options.defines("minimum")); + assert(options.defines("maximum")); + const auto &minimum{options.at("minimum")}; + const auto &maximum{options.at("maximum")}; + assert(minimum.is_integer()); + assert(maximum.is_integer()); + assert(minimum.is_positive()); + assert(maximum.is_positive()); return sourcemeta::jsonbinpack::BOUNDED_8BIT_PREFIX_UTF8_STRING_SHARED{ - static_cast(sourcemeta::jsontoolkit::to_integer(minimum)), - static_cast(sourcemeta::jsontoolkit::to_integer(maximum))}; + static_cast(minimum.to_integer()), + static_cast(maximum.to_integer())}; } -auto RFC3339_DATE_INTEGER_TRIPLET(const sourcemeta::jsontoolkit::Value &) +auto RFC3339_DATE_INTEGER_TRIPLET(const sourcemeta::jsontoolkit::JSON &) -> Encoding { return sourcemeta::jsonbinpack::RFC3339_DATE_INTEGER_TRIPLET{}; } -auto PREFIX_VARINT_LENGTH_STRING_SHARED(const sourcemeta::jsontoolkit::Value &) +auto PREFIX_VARINT_LENGTH_STRING_SHARED(const sourcemeta::jsontoolkit::JSON &) -> Encoding { return sourcemeta::jsonbinpack::PREFIX_VARINT_LENGTH_STRING_SHARED{}; } diff --git a/src/schemas/CMakeLists.txt b/src/schemas/CMakeLists.txt index 68a4b7c3b..85a6238eb 100644 --- a/src/schemas/CMakeLists.txt +++ b/src/schemas/CMakeLists.txt @@ -1,13 +1,12 @@ -# Schemas -file(READ "${CMAKE_CURRENT_SOURCE_DIR}/encoding/v1.json" SCHEMAS_ENCODING_V1_JSON) -# TODO: Can we extract IDs directly from the JSON file instead of hardcoding them here? -set(SCHEMAS_ENCODING_V1_ID "https://www.jsonbinpack.org/schemas/encoding/v1.json") +noa_library(NAMESPACE sourcemeta PROJECT jsonbinpack NAME schemas + FOLDER "JSON BinPack/Schemas") -configure_file(schemas.h.in include/jsonbinpack/schemas/schemas.h @ONLY) -add_library(sourcemeta_jsonbinpack_schemas INTERFACE schemas.h.in) -target_link_libraries(sourcemeta_jsonbinpack_schemas - INTERFACE sourcemeta::jsontoolkit::json) -target_include_directories(sourcemeta_jsonbinpack_schemas - INTERFACE "${CMAKE_CURRENT_BINARY_DIR}/include") -set_target_properties(sourcemeta_jsonbinpack_schemas - PROPERTIES FOLDER "JSON BinPack/Schemas") +if(JSONBINPACK_INSTALL) + noa_library_install(NAMESPACE sourcemeta PROJECT jsonbinpack NAME schemas) +endif() + +sourcemeta_jsonbinpack_add_compile_options( + INTERFACE sourcemeta_jsonbinpack_schemas) + +target_link_libraries(sourcemeta_jsonbinpack_schemas INTERFACE + sourcemeta::jsontoolkit::json) diff --git a/src/schemas/encoding/v1.json b/src/schemas/encoding/v1.json deleted file mode 100644 index d3e4c1df3..000000000 --- a/src/schemas/encoding/v1.json +++ /dev/null @@ -1,8 +0,0 @@ -{ - "$id": "https://www.jsonbinpack.org/schemas/encoding/v1.json", - "$schema": "https://json-schema.org/draft/2020-12/schema", - "$vocabulary": { - "https://json-schema.org/draft/2020-12/vocab/core": true - }, - "$comment": "TODO: Make this a proper encoding schema" -} diff --git a/src/schemas/include/sourcemeta/jsonbinpack/schemas.h b/src/schemas/include/sourcemeta/jsonbinpack/schemas.h new file mode 100644 index 000000000..cb31e5725 --- /dev/null +++ b/src/schemas/include/sourcemeta/jsonbinpack/schemas.h @@ -0,0 +1,20 @@ +#ifndef SOURCEMETA_JSONBINPACK_SCHEMAS_H_ +#define SOURCEMETA_JSONBINPACK_SCHEMAS_H_ + +#include // std::string + +namespace sourcemeta::jsonbinpack::schemas { +namespace encoding::v1 { +const std::string id{"https://www.jsonbinpack.org/schemas/encoding/v1.json"}; +const char *const json = R"JSON({ + "$id": "https://www.jsonbinpack.org/schemas/encoding/v1.json", + "$schema": "https://json-schema.org/draft/2020-12/schema", + "$vocabulary": { + "https://json-schema.org/draft/2020-12/vocab/core": true + }, + "$comment": "TODO: Make this a proper encoding schema" +})JSON"; +} // namespace encoding::v1 +} // namespace sourcemeta::jsonbinpack::schemas + +#endif diff --git a/src/schemas/schemas.h.in b/src/schemas/schemas.h.in deleted file mode 100644 index 4a53a8b5b..000000000 --- a/src/schemas/schemas.h.in +++ /dev/null @@ -1,13 +0,0 @@ -#ifndef SOURCEMETA_JSONBINPACK_SCHEMAS_H_ -#define SOURCEMETA_JSONBINPACK_SCHEMAS_H_ - -#include // std::string - -namespace sourcemeta::jsonbinpack::schemas { - namespace encoding::v1 { - const std::string id{"@SCHEMAS_ENCODING_V1_ID@"}; - const char * const json = R"JSON(@SCHEMAS_ENCODING_V1_JSON@)JSON"; - } -} - -#endif diff --git a/test/alterschema/CMakeLists.txt b/test/alterschema/CMakeLists.txt deleted file mode 100644 index 80683e233..000000000 --- a/test/alterschema/CMakeLists.txt +++ /dev/null @@ -1,7 +0,0 @@ -add_executable(sourcemeta_alterschema_unit - bundle_test.cc rule_test.cc sample_rules.h sample_resolver.h) -target_link_libraries(sourcemeta_alterschema_unit PRIVATE gtest_main) -target_link_libraries(sourcemeta_alterschema_unit PRIVATE sourcemeta_alterschema) -gtest_discover_tests(sourcemeta_alterschema_unit) -set_target_properties(sourcemeta_alterschema_unit - PROPERTIES FOLDER "Alterschema") diff --git a/test/alterschema/bundle_test.cc b/test/alterschema/bundle_test.cc deleted file mode 100644 index 81df95bed..000000000 --- a/test/alterschema/bundle_test.cc +++ /dev/null @@ -1,215 +0,0 @@ -#include -#include -#include - -#include -#include // std::logic_error, std::runtime_error -#include // std::string - -#include "sample_resolver.h" -#include "sample_rules.h" - -TEST(Bundle, can_add_a_rule) { - sourcemeta::jsontoolkit::DefaultResolver resolver; - sourcemeta::alterschema::Bundle bundle{ - sourcemeta::jsontoolkit::schema_walker_none, resolver}; - EXPECT_NO_THROW({ bundle.add(); }); -} - -TEST(Bundle, can_add_multiple_rules) { - sourcemeta::jsontoolkit::DefaultResolver resolver; - sourcemeta::alterschema::Bundle bundle{ - sourcemeta::jsontoolkit::schema_walker_none, resolver}; - bundle.add(); - EXPECT_NO_THROW({ bundle.add(); }); -} - -TEST(Bundle, alter_flat_document_no_applicators) { - sourcemeta::jsontoolkit::DefaultResolver resolver; - sourcemeta::alterschema::Bundle bundle{ - sourcemeta::jsontoolkit::schema_walker_none, resolver}; - bundle.add(); - bundle.add(); - - sourcemeta::jsontoolkit::JSON document{sourcemeta::jsontoolkit::parse(R"JSON({ - "$schema": "https://json-schema.org/draft/2020-12/schema", - "foo": "bar", - "bar": "baz", - "qux": "xxx" - })JSON")}; - - bundle.apply(document, "https://json-schema.org/draft/2020-12/schema"); - - sourcemeta::jsontoolkit::JSON expected{sourcemeta::jsontoolkit::parse(R"JSON({ - "$schema": "https://json-schema.org/draft/2020-12/schema", - "qux": "xxx" - })JSON")}; - - EXPECT_EQ(expected, document); -} - -TEST(Bundle, condition_draft) { - sourcemeta::jsontoolkit::DefaultResolver resolver; - sourcemeta::alterschema::Bundle bundle{ - sourcemeta::jsontoolkit::schema_walker_none, resolver}; - bundle.add(); - - sourcemeta::jsontoolkit::JSON document{sourcemeta::jsontoolkit::parse(R"JSON({ - "$schema": "https://json-schema.org/draft/2020-12/schema" - })JSON")}; - - bundle.apply(document, "https://json-schema.org/draft/2020-12/schema"); - - sourcemeta::jsontoolkit::JSON expected{sourcemeta::jsontoolkit::parse(R"JSON({ - "$schema": "https://json-schema.org/draft/2020-12/schema", - "draft": "2020-12" - })JSON")}; - - EXPECT_EQ(expected, document); -} - -TEST(Bundle, throw_if_no_draft_invalid_default) { - sourcemeta::jsontoolkit::DefaultResolver resolver; - sourcemeta::alterschema::Bundle bundle{ - sourcemeta::jsontoolkit::schema_walker_none, resolver}; - - sourcemeta::jsontoolkit::JSON document{sourcemeta::jsontoolkit::parse(R"JSON({ - "foo": "bar", - "bar": "baz", - "qux": "xxx" - })JSON")}; - - EXPECT_THROW(bundle.apply(document, "https://example.com/invalid"), - std::runtime_error); -} - -TEST(Bundle, no_draft_valid_default) { - sourcemeta::jsontoolkit::DefaultResolver resolver; - sourcemeta::alterschema::Bundle bundle{ - sourcemeta::jsontoolkit::schema_walker_none, resolver}; - bundle.add(); - bundle.add(); - - sourcemeta::jsontoolkit::JSON document{sourcemeta::jsontoolkit::parse(R"JSON({ - "foo": "bar", - "bar": "baz", - "qux": "xxx" - })JSON")}; - - bundle.apply(document, "https://json-schema.org/draft/2020-12/schema"); - - sourcemeta::jsontoolkit::JSON expected{sourcemeta::jsontoolkit::parse(R"JSON({ - "qux": "xxx" - })JSON")}; - - EXPECT_EQ(expected, document); -} - -TEST(Bundle, throw_on_rules_called_twice) { - sourcemeta::jsontoolkit::DefaultResolver resolver; - sourcemeta::alterschema::Bundle bundle{ - sourcemeta::jsontoolkit::schema_walker_none, resolver}; - bundle.add(); - bundle.add(); - - sourcemeta::jsontoolkit::JSON document{sourcemeta::jsontoolkit::parse(R"JSON({ - "$schema": "https://json-schema.org/draft/2020-12/schema", - "foo": "bar" - })JSON")}; - - EXPECT_THROW( - bundle.apply(document, "https://json-schema.org/draft/2020-12/schema"), - std::runtime_error); -} - -static auto -walker_test(const std::string &keyword, - const std::unordered_map &vocabularies) - -> sourcemeta::jsontoolkit::schema_walker_strategy_t { - if (vocabularies.contains("https://jsonbinpack.org/vocab/test")) { - if (keyword == "array") { - return sourcemeta::jsontoolkit::schema_walker_strategy_t::Elements; - } - - if (keyword == "object") { - return sourcemeta::jsontoolkit::schema_walker_strategy_t::Members; - } - - if (keyword == "value") { - return sourcemeta::jsontoolkit::schema_walker_strategy_t::Value; - } - } - - return sourcemeta::jsontoolkit::schema_walker_strategy_t::None; -} - -TEST(Bundle, alter_nested_document_with_applicators) { - sourcemeta::alterschema::Bundle bundle{walker_test, SampleResolver{}}; - bundle.add(); - bundle.add(); - - sourcemeta::jsontoolkit::JSON document{sourcemeta::jsontoolkit::parse(R"JSON({ - "$schema": "https://jsonbinpack.org/test-metaschema-1", - "array": [ - { - "foo": "bar", - "bar": "baz", - "qux": "xxx" - }, - { - "value": { - "foo": "bar", - "bar": "baz", - "qux": "xxx" - } - } - ], - "foo": "bar", - "value": { - "foo": "bar", - "bar": "baz", - "qux": "xxx" - }, - "object": { - "first": { - "foo": "bar", - "bar": "baz", - "qux": "xxx" - }, - "second": { - "foo": "bar", - "bar": "baz", - "qux": "xxx" - } - } - })JSON")}; - - bundle.apply(document, "https://json-schema.org/draft/2020-12/schema"); - - sourcemeta::jsontoolkit::JSON expected{sourcemeta::jsontoolkit::parse(R"JSON({ - "$schema": "https://jsonbinpack.org/test-metaschema-1", - "array": [ - { - "qux": "xxx" - }, - { - "value": { - "qux": "xxx" - } - } - ], - "value": { - "qux": "xxx" - }, - "object": { - "first": { - "qux": "xxx" - }, - "second": { - "qux": "xxx" - } - } - })JSON")}; - - EXPECT_EQ(expected, document); -} diff --git a/test/alterschema/rule_test.cc b/test/alterschema/rule_test.cc deleted file mode 100644 index 85a739a72..000000000 --- a/test/alterschema/rule_test.cc +++ /dev/null @@ -1,26 +0,0 @@ -#include -#include - -#include -#include // std::unique_ptr -#include // std::string - -#include "sample_rules.h" - -TEST(Rule, instances_of_same_rule_are_equal) { - const ExampleRule1 foo{}; - const ExampleRule1 bar{}; - EXPECT_EQ(foo, bar); -} - -TEST(Rule, instances_of_same_rule_are_equal_with_unique_ptr) { - const std::unique_ptr foo{}; - const std::unique_ptr bar{}; - EXPECT_EQ(foo, bar); -} - -TEST(Rule, instances_of_different_rules_are_different) { - const ExampleRule1 foo{}; - const ExampleRule2 bar{}; - EXPECT_NE(foo, bar); -} diff --git a/test/alterschema/sample_resolver.h b/test/alterschema/sample_resolver.h deleted file mode 100644 index 882c764b0..000000000 --- a/test/alterschema/sample_resolver.h +++ /dev/null @@ -1,43 +0,0 @@ -#ifndef SOURCEMETA_TEST_ALTERSCHEMA_SAMPLE_RESOLVER_H_ -#define SOURCEMETA_TEST_ALTERSCHEMA_SAMPLE_RESOLVER_H_ - -#include - -#include // std::promise, std::future -#include // std::optional -#include // std::string - -class SampleResolver { -public: - auto operator()(const std::string &identifier) - -> std::future> { - std::promise> promise; - - if (identifier == "https://jsonbinpack.org/test-metaschema-1") { - promise.set_value(sourcemeta::jsontoolkit::parse(R"JSON({ - "$id": "https://jsonbinpack.org/test-metaschema-1", - "$schema": "https://json-schema.org/draft/2020-12/schema", - "$vocabulary": { - "https://json-schema.org/draft/2020-12/vocab/core": true, - "https://jsonbinpack.org/vocab/test": true - } - })JSON")); - return promise.get_future(); - } - - const std::optional result{ - this->resolver(identifier).get()}; - if (result.has_value()) { - promise.set_value(sourcemeta::jsontoolkit::from(result.value())); - } else { - promise.set_value(std::nullopt); - } - - return promise.get_future(); - } - -private: - sourcemeta::jsontoolkit::DefaultResolver resolver; -}; - -#endif diff --git a/test/alterschema/sample_rules.h b/test/alterschema/sample_rules.h deleted file mode 100644 index e3ab9b639..000000000 --- a/test/alterschema/sample_rules.h +++ /dev/null @@ -1,103 +0,0 @@ -#ifndef SOURCEMETA_TEST_ALTERSCHEMA_SAMPLE_RULES_H_ -#define SOURCEMETA_TEST_ALTERSCHEMA_SAMPLE_RULES_H_ - -#include -#include - -class ExampleRule1 final : public sourcemeta::alterschema::Rule { -public: - ExampleRule1() : sourcemeta::alterschema::Rule("example_rule_1"){}; - - [[nodiscard]] auto condition(const sourcemeta::jsontoolkit::Value &schema, - const std::string &, - const std::unordered_map &, - const std::size_t) const -> bool override { - return sourcemeta::jsontoolkit::defines(schema, "foo"); - } - - auto transform(sourcemeta::jsontoolkit::JSON &, - sourcemeta::jsontoolkit::Value &schema) const - -> void override { - sourcemeta::jsontoolkit::erase(schema, "foo"); - } -}; - -class ExampleRule1Extra final : public sourcemeta::alterschema::Rule { -public: - // This sample rule is a different class than ExampleRule1 but with its same - // name - ExampleRule1Extra() : sourcemeta::alterschema::Rule("example_rule_1"){}; - - [[nodiscard]] auto condition(const sourcemeta::jsontoolkit::Value &schema, - const std::string &, - const std::unordered_map &, - const std::size_t) const -> bool override { - return sourcemeta::jsontoolkit::defines(schema, "xxx"); - } - - auto transform(sourcemeta::jsontoolkit::JSON &, - sourcemeta::jsontoolkit::Value &schema) const - -> void override { - sourcemeta::jsontoolkit::erase(schema, "xxx"); - } -}; - -class ExampleRule2 final : public sourcemeta::alterschema::Rule { -public: - ExampleRule2() : sourcemeta::alterschema::Rule("example_rule_2"){}; - - [[nodiscard]] auto condition(const sourcemeta::jsontoolkit::Value &schema, - const std::string &, - const std::unordered_map &, - const std::size_t) const -> bool override { - return sourcemeta::jsontoolkit::defines(schema, "bar"); - } - - auto transform(sourcemeta::jsontoolkit::JSON &, - sourcemeta::jsontoolkit::Value &schema) const - -> void override { - sourcemeta::jsontoolkit::erase(schema, "bar"); - } -}; - -class ExampleRule3 final : public sourcemeta::alterschema::Rule { -public: - ExampleRule3() : sourcemeta::alterschema::Rule("example_rule_3"){}; - - [[nodiscard]] auto condition(const sourcemeta::jsontoolkit::Value &schema, - const std::string &, - const std::unordered_map &, - const std::size_t) const -> bool override { - return sourcemeta::jsontoolkit::defines(schema, "$schema") && - sourcemeta::jsontoolkit::size(schema) == 1; - } - - auto transform(sourcemeta::jsontoolkit::JSON &document, - sourcemeta::jsontoolkit::Value &schema) const - -> void override { - sourcemeta::jsontoolkit::assign(document, schema, "foo", - sourcemeta::jsontoolkit::from(true)); - } -}; - -class ExampleRule4 final : public sourcemeta::alterschema::Rule { -public: - ExampleRule4() : sourcemeta::alterschema::Rule("example_rule_4"){}; - - [[nodiscard]] auto condition(const sourcemeta::jsontoolkit::Value &schema, - const std::string &draft, - const std::unordered_map &, - const std::size_t) const -> bool override { - return draft == "https://json-schema.org/draft/2020-12/schema" && - !sourcemeta::jsontoolkit::defines(schema, "draft"); - } - - auto transform(sourcemeta::jsontoolkit::JSON &document, - sourcemeta::jsontoolkit::Value &schema) const - -> void override { - sourcemeta::jsontoolkit::assign(document, schema, "draft", - sourcemeta::jsontoolkit::from("2020-12")); - } -}; - -#endif diff --git a/test/decoder/CMakeLists.txt b/test/decoder/CMakeLists.txt index b8f5748bf..8867bc077 100644 --- a/test/decoder/CMakeLists.txt +++ b/test/decoder/CMakeLists.txt @@ -1,6 +1,9 @@ add_executable(sourcemeta_jsonbinpack_decoder_unit - decode_utils.h decode_test.cc decode_varint_test.cc - decode_zigzag_test.cc decode_traits_test.cc + decode_utils.h + decode_test.cc + decode_varint_test.cc + decode_zigzag_test.cc + decode_traits_test.cc decode_any_test.cc decode_array_test.cc decode_enum_test.cc @@ -8,9 +11,17 @@ add_executable(sourcemeta_jsonbinpack_decoder_unit decode_number_test.cc decode_object_test.cc decode_string_test.cc) -target_link_libraries(sourcemeta_jsonbinpack_decoder_unit PRIVATE gtest_main) -target_link_libraries(sourcemeta_jsonbinpack_decoder_unit PRIVATE sourcemeta_jsonbinpack_decoder) -target_link_libraries(sourcemeta_jsonbinpack_decoder_unit PRIVATE sourcemeta::jsontoolkit::json) + +sourcemeta_jsonbinpack_add_compile_options( + PRIVATE sourcemeta_jsonbinpack_decoder_unit) + +target_link_libraries(sourcemeta_jsonbinpack_decoder_unit + PRIVATE GTest::gtest GTest::gtest_main) +target_link_libraries(sourcemeta_jsonbinpack_decoder_unit + PRIVATE sourcemeta::jsonbinpack::decoder) +target_link_libraries(sourcemeta_jsonbinpack_decoder_unit + PRIVATE sourcemeta::jsontoolkit::json) + gtest_discover_tests(sourcemeta_jsonbinpack_decoder_unit) set_target_properties(sourcemeta_jsonbinpack_decoder_unit - PROPERTIES FOLDER "Decoder") + PROPERTIES FOLDER "JSON BinPack/Decoder") diff --git a/test/decoder/decode_any_test.cc b/test/decoder/decode_any_test.cc index 1e6611365..e6357ab34 100644 --- a/test/decoder/decode_any_test.cc +++ b/test/decoder/decode_any_test.cc @@ -1,8 +1,10 @@ +#include + #include "decode_utils.h" -#include -#include -#include +#include +#include + #include TEST(Decoder, ANY_PACKED_TYPE_TAG_BYTE_PREFIX__null) { @@ -11,8 +13,7 @@ TEST(Decoder, ANY_PACKED_TYPE_TAG_BYTE_PREFIX__null) { Decoder decoder{stream}; const sourcemeta::jsontoolkit::JSON result{ decoder.ANY_PACKED_TYPE_TAG_BYTE_PREFIX({})}; - const sourcemeta::jsontoolkit::JSON expected{ - sourcemeta::jsontoolkit::from(nullptr)}; + const sourcemeta::jsontoolkit::JSON expected{nullptr}; EXPECT_EQ(result, expected); } @@ -22,8 +23,7 @@ TEST(Decoder, ANY_PACKED_TYPE_TAG_BYTE_PREFIX__false) { Decoder decoder{stream}; const sourcemeta::jsontoolkit::JSON result{ decoder.ANY_PACKED_TYPE_TAG_BYTE_PREFIX({})}; - const sourcemeta::jsontoolkit::JSON expected{ - sourcemeta::jsontoolkit::from(false)}; + const sourcemeta::jsontoolkit::JSON expected{false}; EXPECT_EQ(result, expected); } @@ -33,8 +33,7 @@ TEST(Decoder, ANY_PACKED_TYPE_TAG_BYTE_PREFIX__true) { Decoder decoder{stream}; const sourcemeta::jsontoolkit::JSON result{ decoder.ANY_PACKED_TYPE_TAG_BYTE_PREFIX({})}; - const sourcemeta::jsontoolkit::JSON expected{ - sourcemeta::jsontoolkit::from(true)}; + const sourcemeta::jsontoolkit::JSON expected{true}; EXPECT_EQ(result, expected); } @@ -44,8 +43,7 @@ TEST(Decoder, ANY_PACKED_TYPE_TAG_BYTE_PREFIX__real_3_14) { Decoder decoder{stream}; const sourcemeta::jsontoolkit::JSON result{ decoder.ANY_PACKED_TYPE_TAG_BYTE_PREFIX({})}; - const sourcemeta::jsontoolkit::JSON expected{ - sourcemeta::jsontoolkit::from(3.14)}; + const sourcemeta::jsontoolkit::JSON expected{3.14}; EXPECT_EQ(result, expected); } @@ -55,8 +53,7 @@ TEST(Decoder, ANY_PACKED_TYPE_TAG_BYTE_PREFIX__256) { Decoder decoder{stream}; const sourcemeta::jsontoolkit::JSON result{ decoder.ANY_PACKED_TYPE_TAG_BYTE_PREFIX({})}; - const sourcemeta::jsontoolkit::JSON expected{ - sourcemeta::jsontoolkit::from(256)}; + const sourcemeta::jsontoolkit::JSON expected{256}; EXPECT_EQ(result, expected); } @@ -66,8 +63,7 @@ TEST(Decoder, ANY_PACKED_TYPE_TAG_BYTE_PREFIX__minus_257) { Decoder decoder{stream}; const sourcemeta::jsontoolkit::JSON result{ decoder.ANY_PACKED_TYPE_TAG_BYTE_PREFIX({})}; - const sourcemeta::jsontoolkit::JSON expected{ - sourcemeta::jsontoolkit::from(-257)}; + const sourcemeta::jsontoolkit::JSON expected{-257}; EXPECT_EQ(result, expected); } @@ -77,8 +73,7 @@ TEST(Decoder, ANY_PACKED_TYPE_TAG_BYTE_PREFIX__255) { Decoder decoder{stream}; const sourcemeta::jsontoolkit::JSON result{ decoder.ANY_PACKED_TYPE_TAG_BYTE_PREFIX({})}; - const sourcemeta::jsontoolkit::JSON expected{ - sourcemeta::jsontoolkit::from(255)}; + const sourcemeta::jsontoolkit::JSON expected{255}; EXPECT_EQ(result, expected); } @@ -88,8 +83,7 @@ TEST(Decoder, ANY_PACKED_TYPE_TAG_BYTE_PREFIX__minus_256) { Decoder decoder{stream}; const sourcemeta::jsontoolkit::JSON result{ decoder.ANY_PACKED_TYPE_TAG_BYTE_PREFIX({})}; - const sourcemeta::jsontoolkit::JSON expected{ - sourcemeta::jsontoolkit::from(-256)}; + const sourcemeta::jsontoolkit::JSON expected{-256}; EXPECT_EQ(result, expected); } @@ -99,8 +93,7 @@ TEST(Decoder, ANY_PACKED_TYPE_TAG_BYTE_PREFIX__0) { Decoder decoder{stream}; const sourcemeta::jsontoolkit::JSON result{ decoder.ANY_PACKED_TYPE_TAG_BYTE_PREFIX({})}; - const sourcemeta::jsontoolkit::JSON expected{ - sourcemeta::jsontoolkit::from(0)}; + const sourcemeta::jsontoolkit::JSON expected{0}; EXPECT_EQ(result, expected); } @@ -110,8 +103,7 @@ TEST(Decoder, ANY_PACKED_TYPE_TAG_BYTE_PREFIX__minus_1) { Decoder decoder{stream}; const sourcemeta::jsontoolkit::JSON result{ decoder.ANY_PACKED_TYPE_TAG_BYTE_PREFIX({})}; - const sourcemeta::jsontoolkit::JSON expected{ - sourcemeta::jsontoolkit::from(-1)}; + const sourcemeta::jsontoolkit::JSON expected{-1}; EXPECT_EQ(result, expected); } @@ -121,8 +113,7 @@ TEST(Decoder, ANY_PACKED_TYPE_TAG_BYTE_PREFIX__string_space) { Decoder decoder{stream}; const sourcemeta::jsontoolkit::JSON result{ decoder.ANY_PACKED_TYPE_TAG_BYTE_PREFIX({})}; - const sourcemeta::jsontoolkit::JSON expected{ - sourcemeta::jsontoolkit::from(" ")}; + const sourcemeta::jsontoolkit::JSON expected{" "}; EXPECT_EQ(result, expected); } @@ -132,8 +123,7 @@ TEST(Decoder, ANY_PACKED_TYPE_TAG_BYTE_PREFIX__string_foo) { Decoder decoder{stream}; const sourcemeta::jsontoolkit::JSON result{ decoder.ANY_PACKED_TYPE_TAG_BYTE_PREFIX({})}; - const sourcemeta::jsontoolkit::JSON expected{ - sourcemeta::jsontoolkit::from("foo")}; + const sourcemeta::jsontoolkit::JSON expected{"foo"}; EXPECT_EQ(result, expected); } @@ -146,8 +136,7 @@ TEST(Decoder, ANY_PACKED_TYPE_TAG_BYTE_PREFIX__string_30_xs) { Decoder decoder{stream}; const sourcemeta::jsontoolkit::JSON result{ decoder.ANY_PACKED_TYPE_TAG_BYTE_PREFIX({})}; - const sourcemeta::jsontoolkit::JSON expected{ - sourcemeta::jsontoolkit::from(std::string(30, 'x'))}; + const sourcemeta::jsontoolkit::JSON expected{std::string(30, 'x')}; EXPECT_EQ(result, expected); } @@ -161,12 +150,9 @@ TEST(Decoder, ANY_PACKED_TYPE_TAG_BYTE_PREFIX__shared_string_foo) { decoder.ANY_PACKED_TYPE_TAG_BYTE_PREFIX({})}; const sourcemeta::jsontoolkit::JSON result3{ decoder.ANY_PACKED_TYPE_TAG_BYTE_PREFIX({})}; - const sourcemeta::jsontoolkit::JSON expected1{ - sourcemeta::jsontoolkit::from("foo")}; - const sourcemeta::jsontoolkit::JSON expected2{ - sourcemeta::jsontoolkit::from("foo")}; - const sourcemeta::jsontoolkit::JSON expected3{ - sourcemeta::jsontoolkit::from("foo")}; + const sourcemeta::jsontoolkit::JSON expected1{"foo"}; + const sourcemeta::jsontoolkit::JSON expected2{"foo"}; + const sourcemeta::jsontoolkit::JSON expected3{"foo"}; EXPECT_EQ(result1, expected1); EXPECT_EQ(result2, expected2); EXPECT_EQ(result3, expected3); @@ -181,8 +167,7 @@ TEST(Decoder, ANY_PACKED_TYPE_TAG_BYTE_PREFIX__string_31_xs) { Decoder decoder{stream}; const sourcemeta::jsontoolkit::JSON result{ decoder.ANY_PACKED_TYPE_TAG_BYTE_PREFIX({})}; - const sourcemeta::jsontoolkit::JSON expected{ - sourcemeta::jsontoolkit::from(std::string(31, 'x'))}; + const sourcemeta::jsontoolkit::JSON expected{std::string(31, 'x')}; EXPECT_EQ(result, expected); } @@ -198,8 +183,7 @@ TEST(Decoder, ANY_PACKED_TYPE_TAG_BYTE_PREFIX__string_61_xs) { Decoder decoder{stream}; const sourcemeta::jsontoolkit::JSON result{ decoder.ANY_PACKED_TYPE_TAG_BYTE_PREFIX({})}; - const sourcemeta::jsontoolkit::JSON expected{ - sourcemeta::jsontoolkit::from(std::string(61, 'x'))}; + const sourcemeta::jsontoolkit::JSON expected{std::string(61, 'x')}; EXPECT_EQ(result, expected); } @@ -213,7 +197,7 @@ TEST(Decoder, ANY_PACKED_TYPE_TAG_BYTE_PREFIX__string_url) { const sourcemeta::jsontoolkit::JSON result{ decoder.ANY_PACKED_TYPE_TAG_BYTE_PREFIX({})}; const sourcemeta::jsontoolkit::JSON expected{ - sourcemeta::jsontoolkit::from("https://soundcloud.com/dandymusicnl")}; + "https://soundcloud.com/dandymusicnl"}; EXPECT_EQ(result, expected); } @@ -234,8 +218,7 @@ TEST(Decoder, ANY_PACKED_TYPE_TAG_BYTE_PREFIX__string_128_xs) { Decoder decoder{stream}; const sourcemeta::jsontoolkit::JSON result{ decoder.ANY_PACKED_TYPE_TAG_BYTE_PREFIX({})}; - const sourcemeta::jsontoolkit::JSON expected{ - sourcemeta::jsontoolkit::from(std::string(128, 'x'))}; + const sourcemeta::jsontoolkit::JSON expected{std::string(128, 'x')}; EXPECT_EQ(result, expected); } @@ -256,8 +239,7 @@ TEST(Decoder, ANY_PACKED_TYPE_TAG_BYTE_PREFIX__string_130_xs) { Decoder decoder{stream}; const sourcemeta::jsontoolkit::JSON result{ decoder.ANY_PACKED_TYPE_TAG_BYTE_PREFIX({})}; - const sourcemeta::jsontoolkit::JSON expected{ - sourcemeta::jsontoolkit::from(std::string(130, 'x'))}; + const sourcemeta::jsontoolkit::JSON expected{std::string(130, 'x')}; EXPECT_EQ(result, expected); } @@ -292,8 +274,7 @@ TEST(Decoder, ANY_PACKED_TYPE_TAG_BYTE_PREFIX__string_256_xs) { Decoder decoder{stream}; const sourcemeta::jsontoolkit::JSON result{ decoder.ANY_PACKED_TYPE_TAG_BYTE_PREFIX({})}; - const sourcemeta::jsontoolkit::JSON expected{ - sourcemeta::jsontoolkit::from(std::string(256, 'x'))}; + const sourcemeta::jsontoolkit::JSON expected{std::string(256, 'x')}; EXPECT_EQ(result, expected); } @@ -328,8 +309,7 @@ TEST(Decoder, ANY_PACKED_TYPE_TAG_BYTE_PREFIX__string_258_xs) { Decoder decoder{stream}; const sourcemeta::jsontoolkit::JSON result{ decoder.ANY_PACKED_TYPE_TAG_BYTE_PREFIX({})}; - const sourcemeta::jsontoolkit::JSON expected{ - sourcemeta::jsontoolkit::from(std::string(258, 'x'))}; + const sourcemeta::jsontoolkit::JSON expected{std::string(258, 'x')}; EXPECT_EQ(result, expected); } @@ -392,8 +372,7 @@ TEST(Decoder, ANY_PACKED_TYPE_TAG_BYTE_PREFIX__string_512_xs) { Decoder decoder{stream}; const sourcemeta::jsontoolkit::JSON result{ decoder.ANY_PACKED_TYPE_TAG_BYTE_PREFIX({})}; - const sourcemeta::jsontoolkit::JSON expected{ - sourcemeta::jsontoolkit::from(std::string(512, 'x'))}; + const sourcemeta::jsontoolkit::JSON expected{std::string(512, 'x')}; EXPECT_EQ(result, expected); } @@ -457,8 +436,7 @@ TEST(Decoder, ANY_PACKED_TYPE_TAG_BYTE_PREFIX__string_513_xs) { Decoder decoder{stream}; const sourcemeta::jsontoolkit::JSON result{ decoder.ANY_PACKED_TYPE_TAG_BYTE_PREFIX({})}; - const sourcemeta::jsontoolkit::JSON expected{ - sourcemeta::jsontoolkit::from(std::string(513, 'x'))}; + const sourcemeta::jsontoolkit::JSON expected{std::string(513, 'x')}; EXPECT_EQ(result, expected); } @@ -572,8 +550,7 @@ TEST(Decoder, ANY_PACKED_TYPE_TAG_BYTE_PREFIX__string_1024_xs) { Decoder decoder{stream}; const sourcemeta::jsontoolkit::JSON result{ decoder.ANY_PACKED_TYPE_TAG_BYTE_PREFIX({})}; - const sourcemeta::jsontoolkit::JSON expected{ - sourcemeta::jsontoolkit::from(std::string(1024, 'x'))}; + const sourcemeta::jsontoolkit::JSON expected{std::string(1024, 'x')}; EXPECT_EQ(result, expected); } @@ -589,8 +566,7 @@ TEST(Decoder, ANY_PACKED_TYPE_TAG_BYTE_PREFIX__string_62_xs_non_shared) { Decoder decoder{stream}; const sourcemeta::jsontoolkit::JSON result{ decoder.ANY_PACKED_TYPE_TAG_BYTE_PREFIX({})}; - const sourcemeta::jsontoolkit::JSON expected{ - sourcemeta::jsontoolkit::from(std::string(62, 'x'))}; + const sourcemeta::jsontoolkit::JSON expected{std::string(62, 'x')}; EXPECT_EQ(result, expected); } @@ -606,8 +582,7 @@ TEST(Decoder, ANY_PACKED_TYPE_TAG_BYTE_PREFIX__string_63_xs_non_shared) { Decoder decoder{stream}; const sourcemeta::jsontoolkit::JSON result{ decoder.ANY_PACKED_TYPE_TAG_BYTE_PREFIX({})}; - const sourcemeta::jsontoolkit::JSON expected{ - sourcemeta::jsontoolkit::from(std::string(63, 'x'))}; + const sourcemeta::jsontoolkit::JSON expected{std::string(63, 'x')}; EXPECT_EQ(result, expected); } @@ -619,12 +594,12 @@ TEST(Decoder, ANY_PACKED_TYPE_TAG_BYTE_PREFIX__foo_true_2000) { const sourcemeta::jsontoolkit::JSON result{ decoder.ANY_PACKED_TYPE_TAG_BYTE_PREFIX({})}; - EXPECT_TRUE(sourcemeta::jsontoolkit::is_array(result)); - EXPECT_EQ(sourcemeta::jsontoolkit::size(result), 3); + EXPECT_TRUE(result.is_array()); + EXPECT_EQ(result.size(), 3); using namespace sourcemeta::jsontoolkit; - EXPECT_EQ(at(result, 0), from("foo")); - EXPECT_EQ(at(result, 1), from(true)); - EXPECT_EQ(at(result, 2), from(2000)); + EXPECT_EQ(result.at(0), JSON{"foo"}); + EXPECT_EQ(result.at(1), JSON{true}); + EXPECT_EQ(result.at(2), JSON{2000}); } TEST(Decoder, ANY_PACKED_TYPE_TAG_BYTE_PREFIX__array_30) { @@ -637,42 +612,42 @@ TEST(Decoder, ANY_PACKED_TYPE_TAG_BYTE_PREFIX__array_30) { const sourcemeta::jsontoolkit::JSON result{ decoder.ANY_PACKED_TYPE_TAG_BYTE_PREFIX({})}; - EXPECT_TRUE(sourcemeta::jsontoolkit::is_array(result)); - EXPECT_EQ(sourcemeta::jsontoolkit::size(result), 30); + EXPECT_TRUE(result.is_array()); + EXPECT_EQ(result.size(), 30); using namespace sourcemeta::jsontoolkit; - EXPECT_EQ(at(result, 0), from(true)); - EXPECT_EQ(at(result, 1), from(true)); - EXPECT_EQ(at(result, 2), from(true)); - EXPECT_EQ(at(result, 3), from(true)); - EXPECT_EQ(at(result, 4), from(true)); - EXPECT_EQ(at(result, 5), from(true)); - EXPECT_EQ(at(result, 6), from(true)); - EXPECT_EQ(at(result, 7), from(true)); - EXPECT_EQ(at(result, 8), from(true)); - EXPECT_EQ(at(result, 9), from(true)); - - EXPECT_EQ(at(result, 10), from(true)); - EXPECT_EQ(at(result, 11), from(true)); - EXPECT_EQ(at(result, 12), from(true)); - EXPECT_EQ(at(result, 13), from(true)); - EXPECT_EQ(at(result, 14), from(true)); - EXPECT_EQ(at(result, 15), from(true)); - EXPECT_EQ(at(result, 16), from(true)); - EXPECT_EQ(at(result, 17), from(true)); - EXPECT_EQ(at(result, 18), from(true)); - EXPECT_EQ(at(result, 19), from(true)); - - EXPECT_EQ(at(result, 20), from(true)); - EXPECT_EQ(at(result, 21), from(true)); - EXPECT_EQ(at(result, 22), from(true)); - EXPECT_EQ(at(result, 23), from(true)); - EXPECT_EQ(at(result, 24), from(true)); - EXPECT_EQ(at(result, 25), from(true)); - EXPECT_EQ(at(result, 26), from(true)); - EXPECT_EQ(at(result, 27), from(true)); - EXPECT_EQ(at(result, 28), from(true)); - EXPECT_EQ(at(result, 29), from(true)); + EXPECT_EQ(result.at(0), JSON{true}); + EXPECT_EQ(result.at(1), JSON{true}); + EXPECT_EQ(result.at(2), JSON{true}); + EXPECT_EQ(result.at(3), JSON{true}); + EXPECT_EQ(result.at(4), JSON{true}); + EXPECT_EQ(result.at(5), JSON{true}); + EXPECT_EQ(result.at(6), JSON{true}); + EXPECT_EQ(result.at(7), JSON{true}); + EXPECT_EQ(result.at(8), JSON{true}); + EXPECT_EQ(result.at(9), JSON{true}); + + EXPECT_EQ(result.at(10), JSON{true}); + EXPECT_EQ(result.at(11), JSON{true}); + EXPECT_EQ(result.at(12), JSON{true}); + EXPECT_EQ(result.at(13), JSON{true}); + EXPECT_EQ(result.at(14), JSON{true}); + EXPECT_EQ(result.at(15), JSON{true}); + EXPECT_EQ(result.at(16), JSON{true}); + EXPECT_EQ(result.at(17), JSON{true}); + EXPECT_EQ(result.at(18), JSON{true}); + EXPECT_EQ(result.at(19), JSON{true}); + + EXPECT_EQ(result.at(20), JSON{true}); + EXPECT_EQ(result.at(21), JSON{true}); + EXPECT_EQ(result.at(22), JSON{true}); + EXPECT_EQ(result.at(23), JSON{true}); + EXPECT_EQ(result.at(24), JSON{true}); + EXPECT_EQ(result.at(25), JSON{true}); + EXPECT_EQ(result.at(26), JSON{true}); + EXPECT_EQ(result.at(27), JSON{true}); + EXPECT_EQ(result.at(28), JSON{true}); + EXPECT_EQ(result.at(29), JSON{true}); } TEST(Decoder, ANY_PACKED_TYPE_TAG_BYTE_PREFIX__array_31) { @@ -685,44 +660,44 @@ TEST(Decoder, ANY_PACKED_TYPE_TAG_BYTE_PREFIX__array_31) { const sourcemeta::jsontoolkit::JSON result{ decoder.ANY_PACKED_TYPE_TAG_BYTE_PREFIX({})}; - EXPECT_TRUE(sourcemeta::jsontoolkit::is_array(result)); - EXPECT_EQ(sourcemeta::jsontoolkit::size(result), 31); + EXPECT_TRUE(result.is_array()); + EXPECT_EQ(result.size(), 31); using namespace sourcemeta::jsontoolkit; - EXPECT_EQ(at(result, 0), from(true)); - EXPECT_EQ(at(result, 1), from(true)); - EXPECT_EQ(at(result, 2), from(true)); - EXPECT_EQ(at(result, 3), from(true)); - EXPECT_EQ(at(result, 4), from(true)); - EXPECT_EQ(at(result, 5), from(true)); - EXPECT_EQ(at(result, 6), from(true)); - EXPECT_EQ(at(result, 7), from(true)); - EXPECT_EQ(at(result, 8), from(true)); - EXPECT_EQ(at(result, 9), from(true)); - - EXPECT_EQ(at(result, 10), from(true)); - EXPECT_EQ(at(result, 11), from(true)); - EXPECT_EQ(at(result, 12), from(true)); - EXPECT_EQ(at(result, 13), from(true)); - EXPECT_EQ(at(result, 14), from(true)); - EXPECT_EQ(at(result, 15), from(true)); - EXPECT_EQ(at(result, 16), from(true)); - EXPECT_EQ(at(result, 17), from(true)); - EXPECT_EQ(at(result, 18), from(true)); - EXPECT_EQ(at(result, 19), from(true)); - - EXPECT_EQ(at(result, 20), from(true)); - EXPECT_EQ(at(result, 21), from(true)); - EXPECT_EQ(at(result, 22), from(true)); - EXPECT_EQ(at(result, 23), from(true)); - EXPECT_EQ(at(result, 24), from(true)); - EXPECT_EQ(at(result, 25), from(true)); - EXPECT_EQ(at(result, 26), from(true)); - EXPECT_EQ(at(result, 27), from(true)); - EXPECT_EQ(at(result, 28), from(true)); - EXPECT_EQ(at(result, 29), from(true)); - - EXPECT_EQ(at(result, 30), from(true)); + EXPECT_EQ(result.at(0), JSON{true}); + EXPECT_EQ(result.at(1), JSON{true}); + EXPECT_EQ(result.at(2), JSON{true}); + EXPECT_EQ(result.at(3), JSON{true}); + EXPECT_EQ(result.at(4), JSON{true}); + EXPECT_EQ(result.at(5), JSON{true}); + EXPECT_EQ(result.at(6), JSON{true}); + EXPECT_EQ(result.at(7), JSON{true}); + EXPECT_EQ(result.at(8), JSON{true}); + EXPECT_EQ(result.at(9), JSON{true}); + + EXPECT_EQ(result.at(10), JSON{true}); + EXPECT_EQ(result.at(11), JSON{true}); + EXPECT_EQ(result.at(12), JSON{true}); + EXPECT_EQ(result.at(13), JSON{true}); + EXPECT_EQ(result.at(14), JSON{true}); + EXPECT_EQ(result.at(15), JSON{true}); + EXPECT_EQ(result.at(16), JSON{true}); + EXPECT_EQ(result.at(17), JSON{true}); + EXPECT_EQ(result.at(18), JSON{true}); + EXPECT_EQ(result.at(19), JSON{true}); + + EXPECT_EQ(result.at(20), JSON{true}); + EXPECT_EQ(result.at(21), JSON{true}); + EXPECT_EQ(result.at(22), JSON{true}); + EXPECT_EQ(result.at(23), JSON{true}); + EXPECT_EQ(result.at(24), JSON{true}); + EXPECT_EQ(result.at(25), JSON{true}); + EXPECT_EQ(result.at(26), JSON{true}); + EXPECT_EQ(result.at(27), JSON{true}); + EXPECT_EQ(result.at(28), JSON{true}); + EXPECT_EQ(result.at(29), JSON{true}); + + EXPECT_EQ(result.at(30), JSON{true}); } TEST(Decoder, ANY_PACKED_TYPE_TAG_BYTE_PREFIX__array_32) { @@ -735,45 +710,45 @@ TEST(Decoder, ANY_PACKED_TYPE_TAG_BYTE_PREFIX__array_32) { const sourcemeta::jsontoolkit::JSON result{ decoder.ANY_PACKED_TYPE_TAG_BYTE_PREFIX({})}; - EXPECT_TRUE(sourcemeta::jsontoolkit::is_array(result)); - EXPECT_EQ(sourcemeta::jsontoolkit::size(result), 32); + EXPECT_TRUE(result.is_array()); + EXPECT_EQ(result.size(), 32); using namespace sourcemeta::jsontoolkit; - EXPECT_EQ(at(result, 0), from(true)); - EXPECT_EQ(at(result, 1), from(true)); - EXPECT_EQ(at(result, 2), from(true)); - EXPECT_EQ(at(result, 3), from(true)); - EXPECT_EQ(at(result, 4), from(true)); - EXPECT_EQ(at(result, 5), from(true)); - EXPECT_EQ(at(result, 6), from(true)); - EXPECT_EQ(at(result, 7), from(true)); - EXPECT_EQ(at(result, 8), from(true)); - EXPECT_EQ(at(result, 9), from(true)); - - EXPECT_EQ(at(result, 10), from(true)); - EXPECT_EQ(at(result, 11), from(true)); - EXPECT_EQ(at(result, 12), from(true)); - EXPECT_EQ(at(result, 13), from(true)); - EXPECT_EQ(at(result, 14), from(true)); - EXPECT_EQ(at(result, 15), from(true)); - EXPECT_EQ(at(result, 16), from(true)); - EXPECT_EQ(at(result, 17), from(true)); - EXPECT_EQ(at(result, 18), from(true)); - EXPECT_EQ(at(result, 19), from(true)); - - EXPECT_EQ(at(result, 20), from(true)); - EXPECT_EQ(at(result, 21), from(true)); - EXPECT_EQ(at(result, 22), from(true)); - EXPECT_EQ(at(result, 23), from(true)); - EXPECT_EQ(at(result, 24), from(true)); - EXPECT_EQ(at(result, 25), from(true)); - EXPECT_EQ(at(result, 26), from(true)); - EXPECT_EQ(at(result, 27), from(true)); - EXPECT_EQ(at(result, 28), from(true)); - EXPECT_EQ(at(result, 29), from(true)); - - EXPECT_EQ(at(result, 30), from(true)); - EXPECT_EQ(at(result, 31), from(true)); + EXPECT_EQ(result.at(0), JSON{true}); + EXPECT_EQ(result.at(1), JSON{true}); + EXPECT_EQ(result.at(2), JSON{true}); + EXPECT_EQ(result.at(3), JSON{true}); + EXPECT_EQ(result.at(4), JSON{true}); + EXPECT_EQ(result.at(5), JSON{true}); + EXPECT_EQ(result.at(6), JSON{true}); + EXPECT_EQ(result.at(7), JSON{true}); + EXPECT_EQ(result.at(8), JSON{true}); + EXPECT_EQ(result.at(9), JSON{true}); + + EXPECT_EQ(result.at(10), JSON{true}); + EXPECT_EQ(result.at(11), JSON{true}); + EXPECT_EQ(result.at(12), JSON{true}); + EXPECT_EQ(result.at(13), JSON{true}); + EXPECT_EQ(result.at(14), JSON{true}); + EXPECT_EQ(result.at(15), JSON{true}); + EXPECT_EQ(result.at(16), JSON{true}); + EXPECT_EQ(result.at(17), JSON{true}); + EXPECT_EQ(result.at(18), JSON{true}); + EXPECT_EQ(result.at(19), JSON{true}); + + EXPECT_EQ(result.at(20), JSON{true}); + EXPECT_EQ(result.at(21), JSON{true}); + EXPECT_EQ(result.at(22), JSON{true}); + EXPECT_EQ(result.at(23), JSON{true}); + EXPECT_EQ(result.at(24), JSON{true}); + EXPECT_EQ(result.at(25), JSON{true}); + EXPECT_EQ(result.at(26), JSON{true}); + EXPECT_EQ(result.at(27), JSON{true}); + EXPECT_EQ(result.at(28), JSON{true}); + EXPECT_EQ(result.at(29), JSON{true}); + + EXPECT_EQ(result.at(30), JSON{true}); + EXPECT_EQ(result.at(31), JSON{true}); } TEST(Decoder, ANY_PACKED_TYPE_TAG_BYTE_PREFIX__object_foo_bar_baz_1) { @@ -783,27 +758,24 @@ TEST(Decoder, ANY_PACKED_TYPE_TAG_BYTE_PREFIX__object_foo_bar_baz_1) { Decoder decoder{stream}; const sourcemeta::jsontoolkit::JSON result{ decoder.ANY_PACKED_TYPE_TAG_BYTE_PREFIX({})}; - EXPECT_TRUE(sourcemeta::jsontoolkit::is_object(result)); - EXPECT_EQ(sourcemeta::jsontoolkit::size(result), 2); + EXPECT_TRUE(result.is_object()); + EXPECT_EQ(result.size(), 2); - EXPECT_TRUE(sourcemeta::jsontoolkit::defines(result, "foo")); - const auto &foo{sourcemeta::jsontoolkit::at(result, "foo")}; - EXPECT_TRUE(sourcemeta::jsontoolkit::is_string(foo)); - EXPECT_EQ(sourcemeta::jsontoolkit::to_string(foo), "bar"); + EXPECT_TRUE(result.defines("foo")); + const auto &foo{result.at("foo")}; + EXPECT_TRUE(foo.is_string()); + EXPECT_EQ(foo.to_string(), "bar"); - EXPECT_TRUE(sourcemeta::jsontoolkit::defines(result, "baz")); - const auto &baz{sourcemeta::jsontoolkit::at(result, "baz")}; - EXPECT_TRUE(sourcemeta::jsontoolkit::is_integer(baz)); - EXPECT_EQ(sourcemeta::jsontoolkit::to_integer(baz), 1); + EXPECT_TRUE(result.defines("baz")); + const auto &baz{result.at("baz")}; + EXPECT_TRUE(baz.is_integer()); + EXPECT_EQ(baz.to_integer(), 1); } static auto is_member_true(const sourcemeta::jsontoolkit::JSON &object, const std::string &key) -> bool { - return sourcemeta::jsontoolkit::defines(object, key) && - sourcemeta::jsontoolkit::is_boolean( - sourcemeta::jsontoolkit::at(object, key)) && - sourcemeta::jsontoolkit::to_boolean( - sourcemeta::jsontoolkit::at(object, key)); + return object.defines(key) && object.at(key).is_boolean() && + object.at(key).to_boolean(); } TEST(Decoder, ANY_PACKED_TYPE_TAG_BYTE_PREFIX__object_30_entries) { @@ -823,8 +795,8 @@ TEST(Decoder, ANY_PACKED_TYPE_TAG_BYTE_PREFIX__object_30_entries) { Decoder decoder{stream}; const sourcemeta::jsontoolkit::JSON result{ decoder.ANY_PACKED_TYPE_TAG_BYTE_PREFIX({})}; - EXPECT_TRUE(sourcemeta::jsontoolkit::is_object(result)); - EXPECT_EQ(sourcemeta::jsontoolkit::size(result), 30); + EXPECT_TRUE(result.is_object()); + EXPECT_EQ(result.size(), 30); EXPECT_TRUE(is_member_true(result, "00")); EXPECT_TRUE(is_member_true(result, "01")); @@ -877,8 +849,8 @@ TEST(Decoder, ANY_PACKED_TYPE_TAG_BYTE_PREFIX__object_31_entries) { Decoder decoder{stream}; const sourcemeta::jsontoolkit::JSON result{ decoder.ANY_PACKED_TYPE_TAG_BYTE_PREFIX({})}; - EXPECT_TRUE(sourcemeta::jsontoolkit::is_object(result)); - EXPECT_EQ(sourcemeta::jsontoolkit::size(result), 31); + EXPECT_TRUE(result.is_object()); + EXPECT_EQ(result.size(), 31); EXPECT_TRUE(is_member_true(result, "00")); EXPECT_TRUE(is_member_true(result, "01")); @@ -933,8 +905,8 @@ TEST(Decoder, ANY_PACKED_TYPE_TAG_BYTE_PREFIX__object_32_entries) { Decoder decoder{stream}; const sourcemeta::jsontoolkit::JSON result{ decoder.ANY_PACKED_TYPE_TAG_BYTE_PREFIX({})}; - EXPECT_TRUE(sourcemeta::jsontoolkit::is_object(result)); - EXPECT_EQ(sourcemeta::jsontoolkit::size(result), 32); + EXPECT_TRUE(result.is_object()); + EXPECT_EQ(result.size(), 32); EXPECT_TRUE(is_member_true(result, "00")); EXPECT_TRUE(is_member_true(result, "01")); @@ -986,16 +958,16 @@ TEST(Decoder, ANY_PACKED_TYPE_TAG_BYTE_PREFIX__object_62_xs_shared) { Decoder decoder{stream}; const sourcemeta::jsontoolkit::JSON result{ decoder.ANY_PACKED_TYPE_TAG_BYTE_PREFIX({})}; - EXPECT_TRUE(sourcemeta::jsontoolkit::is_object(result)); - EXPECT_EQ(sourcemeta::jsontoolkit::size(result), 2); + EXPECT_TRUE(result.is_object()); + EXPECT_EQ(result.size(), 2); - EXPECT_TRUE(sourcemeta::jsontoolkit::defines(result, "foo")); - const auto &foo{sourcemeta::jsontoolkit::at(result, "foo")}; - EXPECT_TRUE(sourcemeta::jsontoolkit::is_string(foo)); - EXPECT_EQ(sourcemeta::jsontoolkit::to_string(foo), std::string(62, 'x')); + EXPECT_TRUE(result.defines("foo")); + const auto &foo{result.at("foo")}; + EXPECT_TRUE(foo.is_string()); + EXPECT_EQ(foo.to_string(), std::string(62, 'x')); - EXPECT_TRUE(sourcemeta::jsontoolkit::defines(result, "bar")); - const auto &baz{sourcemeta::jsontoolkit::at(result, "bar")}; - EXPECT_TRUE(sourcemeta::jsontoolkit::is_string(baz)); - EXPECT_EQ(sourcemeta::jsontoolkit::to_string(baz), std::string(62, 'x')); + EXPECT_TRUE(result.defines("bar")); + const auto &baz{result.at("bar")}; + EXPECT_TRUE(baz.is_string()); + EXPECT_EQ(baz.to_string(), std::string(62, 'x')); } diff --git a/test/decoder/decode_array_test.cc b/test/decoder/decode_array_test.cc index 1f0ddf2db..1fa36e1d9 100644 --- a/test/decoder/decode_array_test.cc +++ b/test/decoder/decode_array_test.cc @@ -1,9 +1,11 @@ +#include + #include "decode_utils.h" -#include -#include -#include -#include +#include +#include +#include + #include TEST(Decoder, FIXED_TYPED_ARRAY_0_1_2__no_prefix_encodings) { @@ -23,8 +25,8 @@ TEST(Decoder, FIXED_TYPED_ARRAY_0_1_true__semityped) { Decoder decoder{stream}; std::vector choices; - choices.push_back(sourcemeta::jsontoolkit::from(false)); - choices.push_back(sourcemeta::jsontoolkit::from(true)); + choices.emplace_back(false); + choices.emplace_back(true); Encoding first{BOUNDED_MULTIPLE_8BITS_ENUM_FIXED{0, 10, 1}}; Encoding second{BOUNDED_MULTIPLE_8BITS_ENUM_FIXED{0, 10, 1}}; @@ -55,8 +57,8 @@ TEST(Decoder, BOUNDED_8BITS_TYPED_ARRAY_true_false_true__no_prefix_encodings) { Decoder decoder{stream}; std::vector choices; - choices.push_back(sourcemeta::jsontoolkit::from(false)); - choices.push_back(sourcemeta::jsontoolkit::from(true)); + choices.emplace_back(false); + choices.emplace_back(true); const sourcemeta::jsontoolkit::JSON result{decoder.BOUNDED_8BITS_TYPED_ARRAY( {0, 3, wrap(BYTE_CHOICE_INDEX{std::move(choices)}), {}})}; @@ -72,8 +74,8 @@ TEST(Decoder, BOUNDED_8BITS_TYPED_ARRAY_true_false_true__same_max_min) { Decoder decoder{stream}; std::vector choices; - choices.push_back(sourcemeta::jsontoolkit::from(false)); - choices.push_back(sourcemeta::jsontoolkit::from(true)); + choices.emplace_back(false); + choices.emplace_back(true); const sourcemeta::jsontoolkit::JSON result{decoder.BOUNDED_8BITS_TYPED_ARRAY( {3, 3, wrap(BYTE_CHOICE_INDEX{std::move(choices)}), {}})}; @@ -89,13 +91,12 @@ TEST(Decoder, BOUNDED_8BITS_TYPED_ARRAY_true_false_5__1_3) { Decoder decoder{stream}; std::vector choices; - choices.push_back(sourcemeta::jsontoolkit::from(false)); - choices.push_back(sourcemeta::jsontoolkit::from(true)); + choices.emplace_back(false); + choices.emplace_back(true); const sourcemeta::jsontoolkit::JSON result{decoder.BOUNDED_8BITS_TYPED_ARRAY( {1, 3, wrap(BOUNDED_MULTIPLE_8BITS_ENUM_FIXED{0, 255, 1}), - wrap({BYTE_CHOICE_INDEX{sourcemeta::jsontoolkit::copy(choices)}, - BYTE_CHOICE_INDEX{sourcemeta::jsontoolkit::copy(choices)}})})}; + wrap({BYTE_CHOICE_INDEX{choices}, BYTE_CHOICE_INDEX{choices}})})}; const sourcemeta::jsontoolkit::JSON expected{ sourcemeta::jsontoolkit::parse("[ true, false, 5 ]")}; @@ -108,12 +109,12 @@ TEST(Decoder, BOUNDED_8BITS_TYPED_ARRAY_complex) { Decoder decoder{stream}; std::vector choices; - choices.push_back(sourcemeta::jsontoolkit::from(false)); - choices.push_back(sourcemeta::jsontoolkit::from(true)); + choices.emplace_back(false); + choices.emplace_back(true); const sourcemeta::jsontoolkit::JSON result{decoder.BOUNDED_8BITS_TYPED_ARRAY( {0, 10, wrap(FLOOR_MULTIPLE_ENUM_VARINT{-2, 4}), - wrap({BYTE_CHOICE_INDEX{sourcemeta::jsontoolkit::copy(choices)}, + wrap({BYTE_CHOICE_INDEX{choices}, FLOOR_VARINT_PREFIX_UTF8_STRING_SHARED{3}})})}; const sourcemeta::jsontoolkit::JSON expected{ @@ -127,8 +128,8 @@ TEST(Decoder, FLOOR_TYPED_ARRAY_true_false_true__no_prefix_encodings) { Decoder decoder{stream}; std::vector choices; - choices.push_back(sourcemeta::jsontoolkit::from(false)); - choices.push_back(sourcemeta::jsontoolkit::from(true)); + choices.emplace_back(false); + choices.emplace_back(true); const sourcemeta::jsontoolkit::JSON result{decoder.FLOOR_TYPED_ARRAY( {0, wrap(BYTE_CHOICE_INDEX{std::move(choices)}), {}})}; @@ -144,13 +145,12 @@ TEST(Decoder, FLOOR_TYPED_ARRAY_true_false_5__1_3) { Decoder decoder{stream}; std::vector choices; - choices.push_back(sourcemeta::jsontoolkit::from(false)); - choices.push_back(sourcemeta::jsontoolkit::from(true)); + choices.emplace_back(false); + choices.emplace_back(true); const sourcemeta::jsontoolkit::JSON result{decoder.FLOOR_TYPED_ARRAY( {1, wrap(BOUNDED_MULTIPLE_8BITS_ENUM_FIXED{0, 255, 1}), - wrap({BYTE_CHOICE_INDEX{sourcemeta::jsontoolkit::copy(choices)}, - BYTE_CHOICE_INDEX{sourcemeta::jsontoolkit::copy(choices)}})})}; + wrap({BYTE_CHOICE_INDEX{choices}, BYTE_CHOICE_INDEX{choices}})})}; const sourcemeta::jsontoolkit::JSON expected{ sourcemeta::jsontoolkit::parse("[ true, false, 5 ]")}; @@ -163,12 +163,12 @@ TEST(Decoder, FLOOR_TYPED_ARRAY_complex) { Decoder decoder{stream}; std::vector choices; - choices.push_back(sourcemeta::jsontoolkit::from(false)); - choices.push_back(sourcemeta::jsontoolkit::from(true)); + choices.emplace_back(false); + choices.emplace_back(true); const sourcemeta::jsontoolkit::JSON result{decoder.FLOOR_TYPED_ARRAY( {0, wrap(FLOOR_MULTIPLE_ENUM_VARINT{-2, 4}), - wrap({BYTE_CHOICE_INDEX{sourcemeta::jsontoolkit::copy(choices)}, + wrap({BYTE_CHOICE_INDEX{choices}, FLOOR_VARINT_PREFIX_UTF8_STRING_SHARED{3}})})}; const sourcemeta::jsontoolkit::JSON expected{ @@ -182,8 +182,8 @@ TEST(Decoder, ROOF_TYPED_ARRAY_true_false_true__no_prefix_encodings) { Decoder decoder{stream}; std::vector choices; - choices.push_back(sourcemeta::jsontoolkit::from(false)); - choices.push_back(sourcemeta::jsontoolkit::from(true)); + choices.emplace_back(false); + choices.emplace_back(true); const sourcemeta::jsontoolkit::JSON result{decoder.ROOF_TYPED_ARRAY( {6, wrap(BYTE_CHOICE_INDEX{std::move(choices)}), {}})}; @@ -199,13 +199,12 @@ TEST(Decoder, ROOF_TYPED_ARRAY_true_false_5__1_3) { Decoder decoder{stream}; std::vector choices; - choices.push_back(sourcemeta::jsontoolkit::from(false)); - choices.push_back(sourcemeta::jsontoolkit::from(true)); + choices.emplace_back(false); + choices.emplace_back(true); const sourcemeta::jsontoolkit::JSON result{decoder.ROOF_TYPED_ARRAY( {5, wrap(BOUNDED_MULTIPLE_8BITS_ENUM_FIXED{0, 255, 1}), - wrap({BYTE_CHOICE_INDEX{sourcemeta::jsontoolkit::copy(choices)}, - BYTE_CHOICE_INDEX{sourcemeta::jsontoolkit::copy(choices)}})})}; + wrap({BYTE_CHOICE_INDEX{choices}, BYTE_CHOICE_INDEX{choices}})})}; const sourcemeta::jsontoolkit::JSON expected{ sourcemeta::jsontoolkit::parse("[ true, false, 5 ]")}; @@ -218,12 +217,12 @@ TEST(Decoder, ROOF_TYPED_ARRAY_complex) { Decoder decoder{stream}; std::vector choices; - choices.push_back(sourcemeta::jsontoolkit::from(false)); - choices.push_back(sourcemeta::jsontoolkit::from(true)); + choices.emplace_back(false); + choices.emplace_back(true); const sourcemeta::jsontoolkit::JSON result{decoder.ROOF_TYPED_ARRAY( {6, wrap(FLOOR_MULTIPLE_ENUM_VARINT{-2, 4}), - wrap({BYTE_CHOICE_INDEX{sourcemeta::jsontoolkit::copy(choices)}, + wrap({BYTE_CHOICE_INDEX{choices}, ROOF_VARINT_PREFIX_UTF8_STRING_SHARED{3}})})}; const sourcemeta::jsontoolkit::JSON expected{ diff --git a/test/decoder/decode_enum_test.cc b/test/decoder/decode_enum_test.cc index 21f0c0021..46be7929b 100644 --- a/test/decoder/decode_enum_test.cc +++ b/test/decoder/decode_enum_test.cc @@ -1,8 +1,9 @@ +#include + #include "decode_utils.h" -#include -#include -#include +#include +#include #include // std::move #include // std::vector @@ -11,13 +12,12 @@ TEST(Decoder, BYTE_CHOICE_INDEX_1__1_0_0) { InputByteStream stream{0x00}; sourcemeta::jsonbinpack::Decoder decoder{stream}; std::vector choices; - choices.push_back(sourcemeta::jsontoolkit::from(1)); - choices.push_back(sourcemeta::jsontoolkit::from(0)); - choices.push_back(sourcemeta::jsontoolkit::from(0)); + choices.emplace_back(1); + choices.emplace_back(0); + choices.emplace_back(0); const sourcemeta::jsontoolkit::JSON result{ decoder.BYTE_CHOICE_INDEX({std::move(choices)})}; - const sourcemeta::jsontoolkit::JSON expected{ - sourcemeta::jsontoolkit::from(1)}; + const sourcemeta::jsontoolkit::JSON expected{1}; EXPECT_EQ(result, expected); } @@ -25,13 +25,12 @@ TEST(Decoder, BYTE_CHOICE_INDEX_1__0_1_0) { InputByteStream stream{0x01}; sourcemeta::jsonbinpack::Decoder decoder{stream}; std::vector choices; - choices.push_back(sourcemeta::jsontoolkit::from(0)); - choices.push_back(sourcemeta::jsontoolkit::from(1)); - choices.push_back(sourcemeta::jsontoolkit::from(0)); + choices.emplace_back(0); + choices.emplace_back(1); + choices.emplace_back(0); const sourcemeta::jsontoolkit::JSON result{ decoder.BYTE_CHOICE_INDEX({std::move(choices)})}; - const sourcemeta::jsontoolkit::JSON expected{ - sourcemeta::jsontoolkit::from(1)}; + const sourcemeta::jsontoolkit::JSON expected{1}; EXPECT_EQ(result, expected); } @@ -39,13 +38,12 @@ TEST(Decoder, BYTE_CHOICE_INDEX_1__0_0_1) { InputByteStream stream{0x02}; sourcemeta::jsonbinpack::Decoder decoder{stream}; std::vector choices; - choices.push_back(sourcemeta::jsontoolkit::from(0)); - choices.push_back(sourcemeta::jsontoolkit::from(0)); - choices.push_back(sourcemeta::jsontoolkit::from(1)); + choices.emplace_back(0); + choices.emplace_back(0); + choices.emplace_back(1); const sourcemeta::jsontoolkit::JSON result{ decoder.BYTE_CHOICE_INDEX({std::move(choices)})}; - const sourcemeta::jsontoolkit::JSON expected{ - sourcemeta::jsontoolkit::from(1)}; + const sourcemeta::jsontoolkit::JSON expected{1}; EXPECT_EQ(result, expected); } @@ -53,13 +51,12 @@ TEST(Decoder, BYTE_CHOICE_INDEX_bar__foo_bar_bar) { InputByteStream stream{0x01}; sourcemeta::jsonbinpack::Decoder decoder{stream}; std::vector choices; - choices.push_back(sourcemeta::jsontoolkit::from("foo")); - choices.push_back(sourcemeta::jsontoolkit::from("bar")); - choices.push_back(sourcemeta::jsontoolkit::from("bar")); + choices.emplace_back("foo"); + choices.emplace_back("bar"); + choices.emplace_back("bar"); const sourcemeta::jsontoolkit::JSON result{ decoder.BYTE_CHOICE_INDEX({std::move(choices)})}; - const sourcemeta::jsontoolkit::JSON expected{ - sourcemeta::jsontoolkit::from("bar")}; + const sourcemeta::jsontoolkit::JSON expected{"bar"}; EXPECT_EQ(result, expected); } @@ -83,13 +80,12 @@ TEST(Decoder, LARGE_CHOICE_INDEX_1__1_0_0) { InputByteStream stream{0x00}; sourcemeta::jsonbinpack::Decoder decoder{stream}; std::vector choices; - choices.push_back(sourcemeta::jsontoolkit::from(1)); - choices.push_back(sourcemeta::jsontoolkit::from(0)); - choices.push_back(sourcemeta::jsontoolkit::from(0)); + choices.emplace_back(1); + choices.emplace_back(0); + choices.emplace_back(0); const sourcemeta::jsontoolkit::JSON result{ decoder.LARGE_CHOICE_INDEX({std::move(choices)})}; - const sourcemeta::jsontoolkit::JSON expected{ - sourcemeta::jsontoolkit::from(1)}; + const sourcemeta::jsontoolkit::JSON expected{1}; EXPECT_EQ(result, expected); } @@ -97,13 +93,12 @@ TEST(Decoder, LARGE_CHOICE_INDEX_1__0_1_0) { InputByteStream stream{0x01}; sourcemeta::jsonbinpack::Decoder decoder{stream}; std::vector choices; - choices.push_back(sourcemeta::jsontoolkit::from(0)); - choices.push_back(sourcemeta::jsontoolkit::from(1)); - choices.push_back(sourcemeta::jsontoolkit::from(0)); + choices.emplace_back(0); + choices.emplace_back(1); + choices.emplace_back(0); const sourcemeta::jsontoolkit::JSON result{ decoder.LARGE_CHOICE_INDEX({std::move(choices)})}; - const sourcemeta::jsontoolkit::JSON expected{ - sourcemeta::jsontoolkit::from(1)}; + const sourcemeta::jsontoolkit::JSON expected{1}; EXPECT_EQ(result, expected); } @@ -111,13 +106,12 @@ TEST(Decoder, LARGE_CHOICE_INDEX_1__0_0_1) { InputByteStream stream{0x02}; sourcemeta::jsonbinpack::Decoder decoder{stream}; std::vector choices; - choices.push_back(sourcemeta::jsontoolkit::from(0)); - choices.push_back(sourcemeta::jsontoolkit::from(0)); - choices.push_back(sourcemeta::jsontoolkit::from(1)); + choices.emplace_back(0); + choices.emplace_back(0); + choices.emplace_back(1); const sourcemeta::jsontoolkit::JSON result{ decoder.LARGE_CHOICE_INDEX({std::move(choices)})}; - const sourcemeta::jsontoolkit::JSON expected{ - sourcemeta::jsontoolkit::from(1)}; + const sourcemeta::jsontoolkit::JSON expected{1}; EXPECT_EQ(result, expected); } @@ -125,13 +119,12 @@ TEST(Decoder, LARGE_CHOICE_INDEX_bar__foo_bar_bar) { InputByteStream stream{0x01}; sourcemeta::jsonbinpack::Decoder decoder{stream}; std::vector choices; - choices.push_back(sourcemeta::jsontoolkit::from("foo")); - choices.push_back(sourcemeta::jsontoolkit::from("bar")); - choices.push_back(sourcemeta::jsontoolkit::from("bar")); + choices.emplace_back("foo"); + choices.emplace_back("bar"); + choices.emplace_back("bar"); const sourcemeta::jsontoolkit::JSON result{ decoder.LARGE_CHOICE_INDEX({std::move(choices)})}; - const sourcemeta::jsontoolkit::JSON expected{ - sourcemeta::jsontoolkit::from("bar")}; + const sourcemeta::jsontoolkit::JSON expected{"bar"}; EXPECT_EQ(result, expected); } @@ -157,13 +150,12 @@ TEST(Decoder, LARGE_CHOICE_INDEX_enum_250) { std::vector choices; for (std::int64_t x = 0; x < 255; x++) { - choices.push_back(sourcemeta::jsontoolkit::from(x)); + choices.emplace_back(x); } const sourcemeta::jsontoolkit::JSON result{ decoder.LARGE_CHOICE_INDEX({std::move(choices)})}; - const sourcemeta::jsontoolkit::JSON expected{ - sourcemeta::jsontoolkit::from(250)}; + const sourcemeta::jsontoolkit::JSON expected{250}; EXPECT_EQ(result, expected); } @@ -171,13 +163,12 @@ TEST(Decoder, TOP_LEVEL_BYTE_CHOICE_INDEX_1__1_0_0) { InputByteStream stream{}; sourcemeta::jsonbinpack::Decoder decoder{stream}; std::vector choices; - choices.push_back(sourcemeta::jsontoolkit::from(1)); - choices.push_back(sourcemeta::jsontoolkit::from(0)); - choices.push_back(sourcemeta::jsontoolkit::from(0)); + choices.emplace_back(1); + choices.emplace_back(0); + choices.emplace_back(0); const sourcemeta::jsontoolkit::JSON result{ decoder.TOP_LEVEL_BYTE_CHOICE_INDEX({std::move(choices)})}; - const sourcemeta::jsontoolkit::JSON expected{ - sourcemeta::jsontoolkit::from(1)}; + const sourcemeta::jsontoolkit::JSON expected{1}; EXPECT_EQ(result, expected); } @@ -185,13 +176,12 @@ TEST(Decoder, TOP_LEVEL_BYTE_CHOICE_INDEX_1__0_1_0) { InputByteStream stream{0x00}; sourcemeta::jsonbinpack::Decoder decoder{stream}; std::vector choices; - choices.push_back(sourcemeta::jsontoolkit::from(0)); - choices.push_back(sourcemeta::jsontoolkit::from(1)); - choices.push_back(sourcemeta::jsontoolkit::from(0)); + choices.emplace_back(0); + choices.emplace_back(1); + choices.emplace_back(0); const sourcemeta::jsontoolkit::JSON result{ decoder.TOP_LEVEL_BYTE_CHOICE_INDEX({std::move(choices)})}; - const sourcemeta::jsontoolkit::JSON expected{ - sourcemeta::jsontoolkit::from(1)}; + const sourcemeta::jsontoolkit::JSON expected{1}; EXPECT_EQ(result, expected); } @@ -199,13 +189,12 @@ TEST(Decoder, TOP_LEVEL_BYTE_CHOICE_INDEX_1__0_0_1) { InputByteStream stream{0x01}; sourcemeta::jsonbinpack::Decoder decoder{stream}; std::vector choices; - choices.push_back(sourcemeta::jsontoolkit::from(0)); - choices.push_back(sourcemeta::jsontoolkit::from(0)); - choices.push_back(sourcemeta::jsontoolkit::from(1)); + choices.emplace_back(0); + choices.emplace_back(0); + choices.emplace_back(1); const sourcemeta::jsontoolkit::JSON result{ decoder.TOP_LEVEL_BYTE_CHOICE_INDEX({std::move(choices)})}; - const sourcemeta::jsontoolkit::JSON expected{ - sourcemeta::jsontoolkit::from(1)}; + const sourcemeta::jsontoolkit::JSON expected{1}; EXPECT_EQ(result, expected); } @@ -213,13 +202,12 @@ TEST(Decoder, TOP_LEVEL_BYTE_CHOICE_INDEX_bar__foo_bar_bar) { InputByteStream stream{0x00}; sourcemeta::jsonbinpack::Decoder decoder{stream}; std::vector choices; - choices.push_back(sourcemeta::jsontoolkit::from("foo")); - choices.push_back(sourcemeta::jsontoolkit::from("bar")); - choices.push_back(sourcemeta::jsontoolkit::from("bar")); + choices.emplace_back("foo"); + choices.emplace_back("bar"); + choices.emplace_back("bar"); const sourcemeta::jsontoolkit::JSON result{ decoder.TOP_LEVEL_BYTE_CHOICE_INDEX({std::move(choices)})}; - const sourcemeta::jsontoolkit::JSON expected{ - sourcemeta::jsontoolkit::from("bar")}; + const sourcemeta::jsontoolkit::JSON expected{"bar"}; EXPECT_EQ(result, expected); } @@ -243,9 +231,8 @@ TEST(Decoder, CONST_NONE_scalar) { InputByteStream stream{}; sourcemeta::jsonbinpack::Decoder decoder{stream}; const sourcemeta::jsontoolkit::JSON result{ - decoder.CONST_NONE({sourcemeta::jsontoolkit::from(1)})}; - const sourcemeta::jsontoolkit::JSON expected{ - sourcemeta::jsontoolkit::from(1)}; + decoder.CONST_NONE({sourcemeta::jsontoolkit::JSON{1}})}; + const sourcemeta::jsontoolkit::JSON expected{1}; EXPECT_EQ(result, expected); } @@ -254,7 +241,7 @@ TEST(Decoder, CONST_NONE_complex) { sourcemeta::jsonbinpack::Decoder decoder{stream}; const sourcemeta::jsontoolkit::JSON result{ decoder.CONST_NONE({sourcemeta::jsontoolkit::parse("{ \"foo\": 1 }")})}; - const sourcemeta::jsontoolkit::JSON expected{ - sourcemeta::jsontoolkit::parse("{ \"foo\": 1 }")}; + const sourcemeta::jsontoolkit::JSON expected = + sourcemeta::jsontoolkit::parse("{ \"foo\": 1 }"); EXPECT_EQ(result, expected); } diff --git a/test/decoder/decode_integer_test.cc b/test/decoder/decode_integer_test.cc index 25cb5d85f..49f390892 100644 --- a/test/decoder/decode_integer_test.cc +++ b/test/decoder/decode_integer_test.cc @@ -1,8 +1,10 @@ +#include + #include "decode_utils.h" -#include -#include -#include +#include +#include + #include // std::numeric_limits TEST(Decoder, BOUNDED_MULTIPLE_8BITS_ENUM_FIXED__minus_5_minus_5_minus_1_1) { @@ -10,8 +12,7 @@ TEST(Decoder, BOUNDED_MULTIPLE_8BITS_ENUM_FIXED__minus_5_minus_5_minus_1_1) { sourcemeta::jsonbinpack::Decoder decoder{stream}; const sourcemeta::jsontoolkit::JSON result{ decoder.BOUNDED_MULTIPLE_8BITS_ENUM_FIXED({-5, -1, 1})}; - const sourcemeta::jsontoolkit::JSON expected{ - sourcemeta::jsontoolkit::from(-5)}; + const sourcemeta::jsontoolkit::JSON expected{-5}; EXPECT_EQ(result, expected); } @@ -20,8 +21,7 @@ TEST(Decoder, BOUNDED_MULTIPLE_8BITS_ENUM_FIXED__2_minus_5_5_1) { sourcemeta::jsonbinpack::Decoder decoder{stream}; const sourcemeta::jsontoolkit::JSON result{ decoder.BOUNDED_MULTIPLE_8BITS_ENUM_FIXED({-5, 5, 1})}; - const sourcemeta::jsontoolkit::JSON expected{ - sourcemeta::jsontoolkit::from(2)}; + const sourcemeta::jsontoolkit::JSON expected{2}; EXPECT_EQ(result, expected); } @@ -30,8 +30,7 @@ TEST(Decoder, BOUNDED_MULTIPLE_8BITS_ENUM_FIXED__5_2_8_1) { sourcemeta::jsonbinpack::Decoder decoder{stream}; const sourcemeta::jsontoolkit::JSON result{ decoder.BOUNDED_MULTIPLE_8BITS_ENUM_FIXED({2, 8, 1})}; - const sourcemeta::jsontoolkit::JSON expected{ - sourcemeta::jsontoolkit::from(5)}; + const sourcemeta::jsontoolkit::JSON expected{5}; EXPECT_EQ(result, expected); } @@ -40,8 +39,7 @@ TEST(Decoder, BOUNDED_MULTIPLE_8BITS_ENUM_FIXED__5_1_19_5) { sourcemeta::jsonbinpack::Decoder decoder{stream}; const sourcemeta::jsontoolkit::JSON result{ decoder.BOUNDED_MULTIPLE_8BITS_ENUM_FIXED({1, 19, 5})}; - const sourcemeta::jsontoolkit::JSON expected{ - sourcemeta::jsontoolkit::from(5)}; + const sourcemeta::jsontoolkit::JSON expected{5}; EXPECT_EQ(result, expected); } @@ -50,8 +48,7 @@ TEST(Decoder, BOUNDED_MULTIPLE_8BITS_ENUM_FIXED__255_0_255_1) { sourcemeta::jsonbinpack::Decoder decoder{stream}; const sourcemeta::jsontoolkit::JSON result{ decoder.BOUNDED_MULTIPLE_8BITS_ENUM_FIXED({0, 255, 1})}; - const sourcemeta::jsontoolkit::JSON expected{ - sourcemeta::jsontoolkit::from(255)}; + const sourcemeta::jsontoolkit::JSON expected{255}; EXPECT_EQ(result, expected); } @@ -60,8 +57,7 @@ TEST(Decoder, FLOOR_MULTIPLE_ENUM_VARINT__minus_3_minus_10_1) { sourcemeta::jsonbinpack::Decoder decoder{stream}; const sourcemeta::jsontoolkit::JSON result{ decoder.FLOOR_MULTIPLE_ENUM_VARINT({-10, 1})}; - const sourcemeta::jsontoolkit::JSON expected{ - sourcemeta::jsontoolkit::from(-3)}; + const sourcemeta::jsontoolkit::JSON expected{-3}; EXPECT_EQ(result, expected); } @@ -70,8 +66,7 @@ TEST(Decoder, FLOOR_MULTIPLE_ENUM_VARINT__5_2_1) { sourcemeta::jsonbinpack::Decoder decoder{stream}; const sourcemeta::jsontoolkit::JSON result{ decoder.FLOOR_MULTIPLE_ENUM_VARINT({2, 1})}; - const sourcemeta::jsontoolkit::JSON expected{ - sourcemeta::jsontoolkit::from(5)}; + const sourcemeta::jsontoolkit::JSON expected{5}; EXPECT_EQ(result, expected); } @@ -80,8 +75,7 @@ TEST(Decoder, FLOOR_MULTIPLE_ENUM_VARINT__10_5_5) { sourcemeta::jsonbinpack::Decoder decoder{stream}; const sourcemeta::jsontoolkit::JSON result{ decoder.FLOOR_MULTIPLE_ENUM_VARINT({5, 5})}; - const sourcemeta::jsontoolkit::JSON expected{ - sourcemeta::jsontoolkit::from(10)}; + const sourcemeta::jsontoolkit::JSON expected{10}; EXPECT_EQ(result, expected); } @@ -90,8 +84,7 @@ TEST(Decoder, FLOOR_MULTIPLE_ENUM_VARINT__10_2_5) { sourcemeta::jsonbinpack::Decoder decoder{stream}; const sourcemeta::jsontoolkit::JSON result{ decoder.FLOOR_MULTIPLE_ENUM_VARINT({2, 5})}; - const sourcemeta::jsontoolkit::JSON expected{ - sourcemeta::jsontoolkit::from(10)}; + const sourcemeta::jsontoolkit::JSON expected{10}; EXPECT_EQ(result, expected); } @@ -100,8 +93,7 @@ TEST(Decoder, FLOOR_MULTIPLE_ENUM_VARINT__1000_minus_2_4) { sourcemeta::jsonbinpack::Decoder decoder{stream}; const sourcemeta::jsontoolkit::JSON result{ decoder.FLOOR_MULTIPLE_ENUM_VARINT({-2, 4})}; - const sourcemeta::jsontoolkit::JSON expected{ - sourcemeta::jsontoolkit::from(1000)}; + const sourcemeta::jsontoolkit::JSON expected{1000}; EXPECT_EQ(result, expected); } @@ -110,8 +102,7 @@ TEST(Decoder, ROOF_MULTIPLE_MIRROR_ENUM_VARINT__minus_3_minus_2_1) { sourcemeta::jsonbinpack::Decoder decoder{stream}; const sourcemeta::jsontoolkit::JSON result{ decoder.ROOF_MULTIPLE_MIRROR_ENUM_VARINT({-2, 1})}; - const sourcemeta::jsontoolkit::JSON expected{ - sourcemeta::jsontoolkit::from(-3)}; + const sourcemeta::jsontoolkit::JSON expected{-3}; EXPECT_EQ(result, expected); } @@ -120,8 +111,7 @@ TEST(Decoder, ROOF_MULTIPLE_MIRROR_ENUM_VARINT__8_10_1) { sourcemeta::jsonbinpack::Decoder decoder{stream}; const sourcemeta::jsontoolkit::JSON result{ decoder.ROOF_MULTIPLE_MIRROR_ENUM_VARINT({10, 1})}; - const sourcemeta::jsontoolkit::JSON expected{ - sourcemeta::jsontoolkit::from(8)}; + const sourcemeta::jsontoolkit::JSON expected{8}; EXPECT_EQ(result, expected); } @@ -130,8 +120,7 @@ TEST(Decoder, ROOF_MULTIPLE_MIRROR_ENUM_VARINT__5_16_5) { sourcemeta::jsonbinpack::Decoder decoder{stream}; const sourcemeta::jsontoolkit::JSON result{ decoder.ROOF_MULTIPLE_MIRROR_ENUM_VARINT({16, 5})}; - const sourcemeta::jsontoolkit::JSON expected{ - sourcemeta::jsontoolkit::from(5)}; + const sourcemeta::jsontoolkit::JSON expected{5}; EXPECT_EQ(result, expected); } @@ -140,8 +129,7 @@ TEST(Decoder, ROOF_MULTIPLE_MIRROR_ENUM_VARINT__10_15_5) { sourcemeta::jsonbinpack::Decoder decoder{stream}; const sourcemeta::jsontoolkit::JSON result{ decoder.ROOF_MULTIPLE_MIRROR_ENUM_VARINT({15, 5})}; - const sourcemeta::jsontoolkit::JSON expected{ - sourcemeta::jsontoolkit::from(10)}; + const sourcemeta::jsontoolkit::JSON expected{10}; EXPECT_EQ(result, expected); } @@ -150,8 +138,7 @@ TEST(Decoder, ARBITRARY_MULTIPLE_ZIGZAG_VARINT__minus_25200_1) { sourcemeta::jsonbinpack::Decoder decoder{stream}; const sourcemeta::jsontoolkit::JSON result{ decoder.ARBITRARY_MULTIPLE_ZIGZAG_VARINT({1})}; - const sourcemeta::jsontoolkit::JSON expected{ - sourcemeta::jsontoolkit::from(-25200)}; + const sourcemeta::jsontoolkit::JSON expected{-25200}; EXPECT_EQ(result, expected); } @@ -160,8 +147,7 @@ TEST(Decoder, ARBITRARY_MULTIPLE_ZIGZAG_VARINT__10_5) { sourcemeta::jsonbinpack::Decoder decoder{stream}; const sourcemeta::jsontoolkit::JSON result{ decoder.ARBITRARY_MULTIPLE_ZIGZAG_VARINT({5})}; - const sourcemeta::jsontoolkit::JSON expected{ - sourcemeta::jsontoolkit::from(10)}; + const sourcemeta::jsontoolkit::JSON expected{10}; EXPECT_EQ(result, expected); } @@ -172,8 +158,7 @@ TEST(Decoder, ARBITRARY_MULTIPLE_ZIGZAG_VARINT__int64_max_1) { const sourcemeta::jsontoolkit::JSON result{ decoder.ARBITRARY_MULTIPLE_ZIGZAG_VARINT({1})}; const std::int64_t value = std::numeric_limits::max(); - const sourcemeta::jsontoolkit::JSON expected{ - sourcemeta::jsontoolkit::from(value)}; + const sourcemeta::jsontoolkit::JSON expected{value}; EXPECT_EQ(result, expected); } @@ -184,7 +169,6 @@ TEST(Decoder, ARBITRARY_MULTIPLE_ZIGZAG_VARINT__int64_min_1) { const sourcemeta::jsontoolkit::JSON result{ decoder.ARBITRARY_MULTIPLE_ZIGZAG_VARINT({1})}; const std::int64_t value = std::numeric_limits::min() + 1; - const sourcemeta::jsontoolkit::JSON expected{ - sourcemeta::jsontoolkit::from(value)}; + const sourcemeta::jsontoolkit::JSON expected{value}; EXPECT_EQ(result, expected); } diff --git a/test/decoder/decode_number_test.cc b/test/decoder/decode_number_test.cc index 3ceca80e1..405f58cb8 100644 --- a/test/decoder/decode_number_test.cc +++ b/test/decoder/decode_number_test.cc @@ -1,15 +1,15 @@ +#include + #include "decode_utils.h" -#include -#include -#include +#include +#include TEST(Decoder, DOUBLE_VARINT_TUPLE_5) { InputByteStream stream{0x0a, 0x00}; sourcemeta::jsonbinpack::Decoder decoder{stream}; const sourcemeta::jsontoolkit::JSON result{decoder.DOUBLE_VARINT_TUPLE({})}; - const sourcemeta::jsontoolkit::JSON expected{ - sourcemeta::jsontoolkit::from(5.0)}; + const sourcemeta::jsontoolkit::JSON expected{5.0}; EXPECT_EQ(result, expected); } @@ -17,8 +17,7 @@ TEST(Decoder, DOUBLE_VARINT_TUPLE_minus_3_point_14) { InputByteStream stream{0xf3, 0x04, 0x02}; sourcemeta::jsonbinpack::Decoder decoder{stream}; const sourcemeta::jsontoolkit::JSON result{decoder.DOUBLE_VARINT_TUPLE({})}; - const sourcemeta::jsontoolkit::JSON expected{ - sourcemeta::jsontoolkit::from(-3.14)}; + const sourcemeta::jsontoolkit::JSON expected{-3.14}; EXPECT_EQ(result, expected); } @@ -26,8 +25,7 @@ TEST(Decoder, DOUBLE_VARINT_TUPLE_minus_5) { InputByteStream stream{0x09, 0x00}; sourcemeta::jsonbinpack::Decoder decoder{stream}; const sourcemeta::jsontoolkit::JSON result{decoder.DOUBLE_VARINT_TUPLE({})}; - const sourcemeta::jsontoolkit::JSON expected{ - sourcemeta::jsontoolkit::from(-5.0)}; + const sourcemeta::jsontoolkit::JSON expected{-5.0}; EXPECT_EQ(result, expected); } @@ -35,8 +33,7 @@ TEST(Decoder, DOUBLE_VARINT_TUPLE_zero) { InputByteStream stream{0x00, 0x00}; sourcemeta::jsonbinpack::Decoder decoder{stream}; const sourcemeta::jsontoolkit::JSON result{decoder.DOUBLE_VARINT_TUPLE({})}; - const sourcemeta::jsontoolkit::JSON expected{ - sourcemeta::jsontoolkit::from(0.0)}; + const sourcemeta::jsontoolkit::JSON expected{0.0}; EXPECT_EQ(result, expected); } @@ -44,8 +41,7 @@ TEST(Decoder, DOUBLE_VARINT_TUPLE_1235) { InputByteStream stream{0xa6, 0x13, 0x00}; sourcemeta::jsonbinpack::Decoder decoder{stream}; const sourcemeta::jsontoolkit::JSON result{decoder.DOUBLE_VARINT_TUPLE({})}; - const sourcemeta::jsontoolkit::JSON expected{ - sourcemeta::jsontoolkit::from(1235)}; + const sourcemeta::jsontoolkit::JSON expected{1235.0}; EXPECT_EQ(result, expected); } @@ -53,8 +49,7 @@ TEST(Decoder, DOUBLE_VARINT_TUPLE_0_point_1235) { InputByteStream stream{0xa6, 0x13, 0x04}; sourcemeta::jsonbinpack::Decoder decoder{stream}; const sourcemeta::jsontoolkit::JSON result{decoder.DOUBLE_VARINT_TUPLE({})}; - const sourcemeta::jsontoolkit::JSON expected{ - sourcemeta::jsontoolkit::from(0.1235)}; + const sourcemeta::jsontoolkit::JSON expected{0.1235}; EXPECT_EQ(result, expected); } @@ -62,8 +57,7 @@ TEST(Decoder, DOUBLE_VARINT_TUPLE_1_point_235) { InputByteStream stream{0xa6, 0x13, 0x03}; sourcemeta::jsonbinpack::Decoder decoder{stream}; const sourcemeta::jsontoolkit::JSON result{decoder.DOUBLE_VARINT_TUPLE({})}; - const sourcemeta::jsontoolkit::JSON expected{ - sourcemeta::jsontoolkit::from(1.235)}; + const sourcemeta::jsontoolkit::JSON expected{1.235}; EXPECT_EQ(result, expected); } @@ -71,8 +65,7 @@ TEST(Decoder, DOUBLE_VARINT_TUPLE_0_point_01235) { InputByteStream stream{0xa6, 0x13, 0x05}; sourcemeta::jsonbinpack::Decoder decoder{stream}; const sourcemeta::jsontoolkit::JSON result{decoder.DOUBLE_VARINT_TUPLE({})}; - const sourcemeta::jsontoolkit::JSON expected{ - sourcemeta::jsontoolkit::from(0.01235)}; + const sourcemeta::jsontoolkit::JSON expected{0.01235}; EXPECT_EQ(result, expected); } @@ -80,8 +73,7 @@ TEST(Decoder, DOUBLE_VARINT_TUPLE_12_point_35) { InputByteStream stream{0xa6, 0x13, 0x02}; sourcemeta::jsonbinpack::Decoder decoder{stream}; const sourcemeta::jsontoolkit::JSON result{decoder.DOUBLE_VARINT_TUPLE({})}; - const sourcemeta::jsontoolkit::JSON expected{ - sourcemeta::jsontoolkit::from(12.35)}; + const sourcemeta::jsontoolkit::JSON expected{12.35}; EXPECT_EQ(result, expected); } @@ -89,8 +81,7 @@ TEST(Decoder, DOUBLE_VARINT_TUPLE_0_point_001235) { InputByteStream stream{0xa6, 0x13, 0x06}; sourcemeta::jsonbinpack::Decoder decoder{stream}; const sourcemeta::jsontoolkit::JSON result{decoder.DOUBLE_VARINT_TUPLE({})}; - const sourcemeta::jsontoolkit::JSON expected{ - sourcemeta::jsontoolkit::from(0.001235)}; + const sourcemeta::jsontoolkit::JSON expected{0.001235}; EXPECT_EQ(result, expected); } @@ -98,8 +89,7 @@ TEST(Decoder, DOUBLE_VARINT_TUPLE_123_point_5) { InputByteStream stream{0xa6, 0x13, 0x01}; sourcemeta::jsonbinpack::Decoder decoder{stream}; const sourcemeta::jsontoolkit::JSON result{decoder.DOUBLE_VARINT_TUPLE({})}; - const sourcemeta::jsontoolkit::JSON expected{ - sourcemeta::jsontoolkit::from(123.5)}; + const sourcemeta::jsontoolkit::JSON expected{123.5}; EXPECT_EQ(result, expected); } @@ -107,8 +97,7 @@ TEST(Decoder, DOUBLE_VARINT_TUPLE_314) { InputByteStream stream{0xf4, 0x04, 0x00}; sourcemeta::jsonbinpack::Decoder decoder{stream}; const sourcemeta::jsontoolkit::JSON result{decoder.DOUBLE_VARINT_TUPLE({})}; - const sourcemeta::jsontoolkit::JSON expected{ - sourcemeta::jsontoolkit::from(314)}; + const sourcemeta::jsontoolkit::JSON expected{314.0}; EXPECT_EQ(result, expected); } @@ -116,8 +105,7 @@ TEST(Decoder, DOUBLE_VARINT_TUPLE_0_point_314) { InputByteStream stream{0xf4, 0x04, 0x03}; sourcemeta::jsonbinpack::Decoder decoder{stream}; const sourcemeta::jsontoolkit::JSON result{decoder.DOUBLE_VARINT_TUPLE({})}; - const sourcemeta::jsontoolkit::JSON expected{ - sourcemeta::jsontoolkit::from(0.314)}; + const sourcemeta::jsontoolkit::JSON expected{0.314}; EXPECT_EQ(result, expected); } @@ -125,8 +113,7 @@ TEST(Decoder, DOUBLE_VARINT_TUPLE_3_point_14) { InputByteStream stream{0xf4, 0x04, 0x02}; sourcemeta::jsonbinpack::Decoder decoder{stream}; const sourcemeta::jsontoolkit::JSON result{decoder.DOUBLE_VARINT_TUPLE({})}; - const sourcemeta::jsontoolkit::JSON expected{ - sourcemeta::jsontoolkit::from(3.14)}; + const sourcemeta::jsontoolkit::JSON expected{3.14}; EXPECT_EQ(result, expected); } @@ -134,8 +121,7 @@ TEST(Decoder, DOUBLE_VARINT_TUPLE_0_point_0314) { InputByteStream stream{0xf4, 0x04, 0x04}; sourcemeta::jsonbinpack::Decoder decoder{stream}; const sourcemeta::jsontoolkit::JSON result{decoder.DOUBLE_VARINT_TUPLE({})}; - const sourcemeta::jsontoolkit::JSON expected{ - sourcemeta::jsontoolkit::from(0.0314)}; + const sourcemeta::jsontoolkit::JSON expected{0.0314}; EXPECT_EQ(result, expected); } @@ -143,7 +129,6 @@ TEST(Decoder, DOUBLE_VARINT_TUPLE_31_point_4) { InputByteStream stream{0xf4, 0x04, 0x01}; sourcemeta::jsonbinpack::Decoder decoder{stream}; const sourcemeta::jsontoolkit::JSON result{decoder.DOUBLE_VARINT_TUPLE({})}; - const sourcemeta::jsontoolkit::JSON expected{ - sourcemeta::jsontoolkit::from(31.4)}; + const sourcemeta::jsontoolkit::JSON expected{31.4}; EXPECT_EQ(result, expected); } diff --git a/test/decoder/decode_object_test.cc b/test/decoder/decode_object_test.cc index bc6a6971e..988494cd1 100644 --- a/test/decoder/decode_object_test.cc +++ b/test/decoder/decode_object_test.cc @@ -1,9 +1,10 @@ +#include + #include "decode_utils.h" -#include -#include -#include -#include +#include +#include +#include TEST(Decoder, FIXED_TYPED_ARBITRARY_OBJECT__no_length_string__integer) { using namespace sourcemeta::jsonbinpack; @@ -18,16 +19,16 @@ TEST(Decoder, FIXED_TYPED_ARBITRARY_OBJECT__no_length_string__integer) { decoder.FIXED_TYPED_ARBITRARY_OBJECT( {2, wrap(UTF8_STRING_NO_LENGTH{3}), wrap(BOUNDED_MULTIPLE_8BITS_ENUM_FIXED{0, 10, 1})})}; - EXPECT_TRUE(sourcemeta::jsontoolkit::is_object(result)); - EXPECT_EQ(sourcemeta::jsontoolkit::size(result), 2); - EXPECT_TRUE(sourcemeta::jsontoolkit::defines(result, "foo")); - EXPECT_TRUE(sourcemeta::jsontoolkit::defines(result, "bar")); - const auto &foo{sourcemeta::jsontoolkit::at(result, "foo")}; - const auto &bar{sourcemeta::jsontoolkit::at(result, "bar")}; - EXPECT_TRUE(sourcemeta::jsontoolkit::is_integer(foo)); - EXPECT_TRUE(sourcemeta::jsontoolkit::is_integer(bar)); - EXPECT_EQ(sourcemeta::jsontoolkit::to_integer(foo), 1); - EXPECT_EQ(sourcemeta::jsontoolkit::to_integer(bar), 2); + EXPECT_TRUE(result.is_object()); + EXPECT_EQ(result.size(), 2); + EXPECT_TRUE(result.defines("foo")); + EXPECT_TRUE(result.defines("bar")); + const auto &foo{result.at("foo")}; + const auto &bar{result.at("bar")}; + EXPECT_TRUE(foo.is_integer()); + EXPECT_TRUE(bar.is_integer()); + EXPECT_EQ(foo.to_integer(), 1); + EXPECT_EQ(bar.to_integer(), 2); } TEST(Decoder, VARINT_TYPED_ARBITRARY_OBJECT__no_length_string__integer) { @@ -44,14 +45,14 @@ TEST(Decoder, VARINT_TYPED_ARBITRARY_OBJECT__no_length_string__integer) { decoder.VARINT_TYPED_ARBITRARY_OBJECT( {wrap(UTF8_STRING_NO_LENGTH{3}), wrap(BOUNDED_MULTIPLE_8BITS_ENUM_FIXED{0, 10, 1})})}; - EXPECT_TRUE(sourcemeta::jsontoolkit::is_object(result)); - EXPECT_EQ(sourcemeta::jsontoolkit::size(result), 2); - EXPECT_TRUE(sourcemeta::jsontoolkit::defines(result, "foo")); - EXPECT_TRUE(sourcemeta::jsontoolkit::defines(result, "bar")); - const auto &foo{sourcemeta::jsontoolkit::at(result, "foo")}; - const auto &bar{sourcemeta::jsontoolkit::at(result, "bar")}; - EXPECT_TRUE(sourcemeta::jsontoolkit::is_integer(foo)); - EXPECT_TRUE(sourcemeta::jsontoolkit::is_integer(bar)); - EXPECT_EQ(sourcemeta::jsontoolkit::to_integer(foo), 1); - EXPECT_EQ(sourcemeta::jsontoolkit::to_integer(bar), 2); + EXPECT_TRUE(result.is_object()); + EXPECT_EQ(result.size(), 2); + EXPECT_TRUE(result.defines("foo")); + EXPECT_TRUE(result.defines("bar")); + const auto &foo{result.at("foo")}; + const auto &bar{result.at("bar")}; + EXPECT_TRUE(foo.is_integer()); + EXPECT_TRUE(bar.is_integer()); + EXPECT_EQ(foo.to_integer(), 1); + EXPECT_EQ(bar.to_integer(), 2); } diff --git a/test/decoder/decode_string_test.cc b/test/decoder/decode_string_test.cc index 278ec9a47..a06f0231b 100644 --- a/test/decoder/decode_string_test.cc +++ b/test/decoder/decode_string_test.cc @@ -1,16 +1,16 @@ +#include + #include "decode_utils.h" -#include -#include -#include +#include +#include TEST(Decoder, UTF8_STRING_NO_LENGTH_foo_bar) { InputByteStream stream{0x66, 0x6f, 0x6f, 0x20, 0x62, 0x61, 0x72}; sourcemeta::jsonbinpack::Decoder decoder{stream}; const sourcemeta::jsontoolkit::JSON result{ decoder.UTF8_STRING_NO_LENGTH({7})}; - const sourcemeta::jsontoolkit::JSON expected{ - sourcemeta::jsontoolkit::from("foo bar")}; + const sourcemeta::jsontoolkit::JSON expected{"foo bar"}; EXPECT_EQ(result, expected); } @@ -19,8 +19,7 @@ TEST(Decoder, FLOOR_VARINT_PREFIX_UTF8_STRING_SHARED_foo_3) { sourcemeta::jsonbinpack::Decoder decoder{stream}; const sourcemeta::jsontoolkit::JSON result{ decoder.FLOOR_VARINT_PREFIX_UTF8_STRING_SHARED({3})}; - const sourcemeta::jsontoolkit::JSON expected{ - sourcemeta::jsontoolkit::from("foo")}; + const sourcemeta::jsontoolkit::JSON expected{"foo"}; EXPECT_EQ(result, expected); } @@ -29,8 +28,7 @@ TEST(Decoder, FLOOR_VARINT_PREFIX_UTF8_STRING_SHARED_foo_0_foo_3) { sourcemeta::jsonbinpack::Decoder decoder{stream}; const sourcemeta::jsontoolkit::JSON result1{ decoder.FLOOR_VARINT_PREFIX_UTF8_STRING_SHARED({0})}; - const sourcemeta::jsontoolkit::JSON expected{ - sourcemeta::jsontoolkit::from("foo")}; + const sourcemeta::jsontoolkit::JSON expected{"foo"}; EXPECT_EQ(result1, expected); // The cursor is now on the second string @@ -46,8 +44,7 @@ TEST(Decoder, ROOF_VARINT_PREFIX_UTF8_STRING_SHARED_foo_4) { sourcemeta::jsonbinpack::Decoder decoder{stream}; const sourcemeta::jsontoolkit::JSON result{ decoder.ROOF_VARINT_PREFIX_UTF8_STRING_SHARED({4})}; - const sourcemeta::jsontoolkit::JSON expected{ - sourcemeta::jsontoolkit::from("foo")}; + const sourcemeta::jsontoolkit::JSON expected{"foo"}; EXPECT_EQ(result, expected); } @@ -56,8 +53,7 @@ TEST(Decoder, ROOF_VARINT_PREFIX_UTF8_STRING_SHARED_foo_3_foo_5) { sourcemeta::jsonbinpack::Decoder decoder{stream}; const sourcemeta::jsontoolkit::JSON result1{ decoder.ROOF_VARINT_PREFIX_UTF8_STRING_SHARED({3})}; - const sourcemeta::jsontoolkit::JSON expected{ - sourcemeta::jsontoolkit::from("foo")}; + const sourcemeta::jsontoolkit::JSON expected{"foo"}; EXPECT_EQ(result1, expected); // The cursor is now on the second string @@ -73,8 +69,7 @@ TEST(Decoder, BOUNDED_8BIT_PREFIX_UTF8_STRING_SHARED_foo_3_5) { sourcemeta::jsonbinpack::Decoder decoder{stream}; const sourcemeta::jsontoolkit::JSON result{ decoder.BOUNDED_8BIT_PREFIX_UTF8_STRING_SHARED({3, 5})}; - const sourcemeta::jsontoolkit::JSON expected{ - sourcemeta::jsontoolkit::from("foo")}; + const sourcemeta::jsontoolkit::JSON expected{"foo"}; EXPECT_EQ(result, expected); } @@ -83,8 +78,7 @@ TEST(Decoder, BOUNDED_8BIT_PREFIX_UTF8_STRING_SHARED_foo_3_3) { sourcemeta::jsonbinpack::Decoder decoder{stream}; const sourcemeta::jsontoolkit::JSON result{ decoder.BOUNDED_8BIT_PREFIX_UTF8_STRING_SHARED({3, 3})}; - const sourcemeta::jsontoolkit::JSON expected{ - sourcemeta::jsontoolkit::from("foo")}; + const sourcemeta::jsontoolkit::JSON expected{"foo"}; EXPECT_EQ(result, expected); } @@ -93,8 +87,7 @@ TEST(Decoder, BOUNDED_8BIT_PREFIX_UTF8_STRING_SHARED_foo_0_6_foo_3_100) { sourcemeta::jsonbinpack::Decoder decoder{stream}; const sourcemeta::jsontoolkit::JSON result1{ decoder.BOUNDED_8BIT_PREFIX_UTF8_STRING_SHARED({0, 6})}; - const sourcemeta::jsontoolkit::JSON expected{ - sourcemeta::jsontoolkit::from("foo")}; + const sourcemeta::jsontoolkit::JSON expected{"foo"}; EXPECT_EQ(result1, expected); // The cursor is now on the second string @@ -110,8 +103,7 @@ TEST(Decoder, RFC3339_DATE_INTEGER_TRIPLET_2014_10_01) { sourcemeta::jsonbinpack::Decoder decoder{stream}; const sourcemeta::jsontoolkit::JSON result{ decoder.RFC3339_DATE_INTEGER_TRIPLET({})}; - const sourcemeta::jsontoolkit::JSON expected{ - sourcemeta::jsontoolkit::from("2014-10-01")}; + const sourcemeta::jsontoolkit::JSON expected{"2014-10-01"}; EXPECT_EQ(result, expected); } @@ -120,8 +112,7 @@ TEST(Decoder, PREFIX_VARINT_LENGTH_STRING_SHARED_foo) { sourcemeta::jsonbinpack::Decoder decoder{stream}; const sourcemeta::jsontoolkit::JSON result{ decoder.PREFIX_VARINT_LENGTH_STRING_SHARED({})}; - const sourcemeta::jsontoolkit::JSON expected{ - sourcemeta::jsontoolkit::from("foo")}; + const sourcemeta::jsontoolkit::JSON expected{"foo"}; EXPECT_EQ(result, expected); } @@ -138,8 +129,7 @@ TEST(Decoder, PREFIX_VARINT_LENGTH_STRING_SHARED_foo_foo_foo_foo) { const sourcemeta::jsontoolkit::JSON result4{ decoder.PREFIX_VARINT_LENGTH_STRING_SHARED({})}; - const sourcemeta::jsontoolkit::JSON expected{ - sourcemeta::jsontoolkit::from("foo")}; + const sourcemeta::jsontoolkit::JSON expected{"foo"}; EXPECT_EQ(result1, expected); EXPECT_EQ(result2, expected); EXPECT_EQ(result3, expected); @@ -153,8 +143,7 @@ TEST(Decoder, PREFIX_VARINT_LENGTH_STRING_SHARED_non_key_foo_key_foo) { decoder.FLOOR_VARINT_PREFIX_UTF8_STRING_SHARED({3})}; const sourcemeta::jsontoolkit::JSON result2{ decoder.PREFIX_VARINT_LENGTH_STRING_SHARED({})}; - const sourcemeta::jsontoolkit::JSON expected{ - sourcemeta::jsontoolkit::from("foo")}; + const sourcemeta::jsontoolkit::JSON expected{"foo"}; EXPECT_EQ(result1, expected); EXPECT_EQ(result2, expected); } @@ -166,8 +155,7 @@ TEST(Decoder, PREFIX_VARINT_LENGTH_STRING_SHARED_key_foo_non_key_foo) { decoder.PREFIX_VARINT_LENGTH_STRING_SHARED({})}; const sourcemeta::jsontoolkit::JSON result2{ decoder.FLOOR_VARINT_PREFIX_UTF8_STRING_SHARED({3})}; - const sourcemeta::jsontoolkit::JSON expected{ - sourcemeta::jsontoolkit::from("foo")}; + const sourcemeta::jsontoolkit::JSON expected{"foo"}; EXPECT_EQ(result1, expected); EXPECT_EQ(result2, expected); } diff --git a/test/decoder/decode_test.cc b/test/decoder/decode_test.cc index 8918f2b78..c33882d8a 100644 --- a/test/decoder/decode_test.cc +++ b/test/decoder/decode_test.cc @@ -1,16 +1,16 @@ +#include + #include "decode_utils.h" -#include -#include -#include +#include +#include TEST(Decoder, generic_decode_BOUNDED_MULTIPLE_8BITS_ENUM_FIXED) { using namespace sourcemeta::jsonbinpack; InputByteStream stream{0x00}; Decoder decoder{stream}; BOUNDED_MULTIPLE_8BITS_ENUM_FIXED options{-5, -1, 1}; - const sourcemeta::jsontoolkit::JSON result{decoder.decode(options)}; - const sourcemeta::jsontoolkit::JSON expected{ - sourcemeta::jsontoolkit::from(-5)}; + const sourcemeta::jsontoolkit::JSON result = decoder.decode(options); + const sourcemeta::jsontoolkit::JSON expected{-5}; EXPECT_EQ(result, expected); } diff --git a/test/decoder/decode_traits_test.cc b/test/decoder/decode_traits_test.cc index 59203f096..64f1febc9 100644 --- a/test/decoder/decode_traits_test.cc +++ b/test/decoder/decode_traits_test.cc @@ -1,5 +1,7 @@ #include -#include + +#include + #include // std::char_traits #include diff --git a/test/decoder/decode_varint_test.cc b/test/decoder/decode_varint_test.cc index ed2cb61ab..15371b7d4 100644 --- a/test/decoder/decode_varint_test.cc +++ b/test/decoder/decode_varint_test.cc @@ -1,8 +1,10 @@ +#include + #include "decode_utils.h" -#include -#include -#include +#include +#include + #include // std::numeric_limits TEST(Decoder, varint_0x01) { diff --git a/test/decoder/decode_zigzag_test.cc b/test/decoder/decode_zigzag_test.cc index 16489e27c..69d5f59ba 100644 --- a/test/decoder/decode_zigzag_test.cc +++ b/test/decoder/decode_zigzag_test.cc @@ -1,7 +1,8 @@ -#include -#include - #include + +#include +#include + #include // std::numeric_limits TEST(Decoder, zigzag_int_0_0) { diff --git a/test/encoder/CMakeLists.txt b/test/encoder/CMakeLists.txt index cd29b5dcb..3020ab270 100644 --- a/test/encoder/CMakeLists.txt +++ b/test/encoder/CMakeLists.txt @@ -1,6 +1,11 @@ add_executable(sourcemeta_jsonbinpack_encoder_unit - encode_utils.h encode_test.cc context_test.cc encode_varint_test.cc - encode_zigzag_test.cc encode_traits_test.cc encode_real_test.cc + encode_utils.h + encode_test.cc + encode_context_test.cc + encode_varint_test.cc + encode_zigzag_test.cc + encode_traits_test.cc + encode_real_test.cc encode_any_test.cc encode_array_test.cc encode_enum_test.cc @@ -8,9 +13,17 @@ add_executable(sourcemeta_jsonbinpack_encoder_unit encode_number_test.cc encode_object_test.cc encode_string_test.cc) -target_link_libraries(sourcemeta_jsonbinpack_encoder_unit PRIVATE gtest_main) -target_link_libraries(sourcemeta_jsonbinpack_encoder_unit PRIVATE sourcemeta_jsonbinpack_encoder) -target_link_libraries(sourcemeta_jsonbinpack_encoder_unit PRIVATE sourcemeta::jsontoolkit::json) + +sourcemeta_jsonbinpack_add_compile_options( + PRIVATE sourcemeta_jsonbinpack_encoder_unit) + +target_link_libraries(sourcemeta_jsonbinpack_encoder_unit + PRIVATE GTest::gtest GTest::gtest_main) +target_link_libraries(sourcemeta_jsonbinpack_encoder_unit + PRIVATE sourcemeta::jsonbinpack::encoder) +target_link_libraries(sourcemeta_jsonbinpack_encoder_unit + PRIVATE sourcemeta::jsontoolkit::json) + gtest_discover_tests(sourcemeta_jsonbinpack_encoder_unit) set_target_properties(sourcemeta_jsonbinpack_encoder_unit - PROPERTIES FOLDER "Encoder") + PROPERTIES FOLDER "JSON BinPack/Encoder") diff --git a/test/encoder/encode_any_test.cc b/test/encoder/encode_any_test.cc index e8d8e70db..393fa5886 100644 --- a/test/encoder/encode_any_test.cc +++ b/test/encoder/encode_any_test.cc @@ -1,14 +1,14 @@ -#include "encode_utils.h" -#include -#include - #include + #include +#include "encode_utils.h" +#include +#include + TEST(Encoder, ANY_PACKED_TYPE_TAG_BYTE_PREFIX__null) { using namespace sourcemeta::jsonbinpack; - sourcemeta::jsontoolkit::JSON document{ - sourcemeta::jsontoolkit::from(nullptr)}; + sourcemeta::jsontoolkit::JSON document{nullptr}; OutputByteStream stream{}; Encoder encoder{stream}; @@ -18,7 +18,7 @@ TEST(Encoder, ANY_PACKED_TYPE_TAG_BYTE_PREFIX__null) { TEST(Encoder, ANY_PACKED_TYPE_TAG_BYTE_PREFIX__false) { using namespace sourcemeta::jsonbinpack; - sourcemeta::jsontoolkit::JSON document{sourcemeta::jsontoolkit::from(false)}; + sourcemeta::jsontoolkit::JSON document{false}; OutputByteStream stream{}; Encoder encoder{stream}; @@ -28,7 +28,7 @@ TEST(Encoder, ANY_PACKED_TYPE_TAG_BYTE_PREFIX__false) { TEST(Encoder, ANY_PACKED_TYPE_TAG_BYTE_PREFIX__true) { using namespace sourcemeta::jsonbinpack; - sourcemeta::jsontoolkit::JSON document{sourcemeta::jsontoolkit::from(true)}; + sourcemeta::jsontoolkit::JSON document{true}; OutputByteStream stream{}; Encoder encoder{stream}; @@ -38,7 +38,7 @@ TEST(Encoder, ANY_PACKED_TYPE_TAG_BYTE_PREFIX__true) { TEST(Encoder, ANY_PACKED_TYPE_TAG_BYTE_PREFIX__real_3_14) { using namespace sourcemeta::jsonbinpack; - sourcemeta::jsontoolkit::JSON document{sourcemeta::jsontoolkit::from(3.14)}; + sourcemeta::jsontoolkit::JSON document{3.14}; OutputByteStream stream{}; Encoder encoder{stream}; @@ -51,7 +51,7 @@ TEST(Encoder, ANY_PACKED_TYPE_TAG_BYTE_PREFIX__real_3_14) { TEST(Encoder, ANY_PACKED_TYPE_TAG_BYTE_PREFIX__256) { using namespace sourcemeta::jsonbinpack; - sourcemeta::jsontoolkit::JSON document{sourcemeta::jsontoolkit::from(256)}; + sourcemeta::jsontoolkit::JSON document{256}; OutputByteStream stream{}; Encoder encoder{stream}; @@ -64,7 +64,7 @@ TEST(Encoder, ANY_PACKED_TYPE_TAG_BYTE_PREFIX__256) { TEST(Encoder, ANY_PACKED_TYPE_TAG_BYTE_PREFIX__minus_257) { using namespace sourcemeta::jsonbinpack; - sourcemeta::jsontoolkit::JSON document{sourcemeta::jsontoolkit::from(-257)}; + sourcemeta::jsontoolkit::JSON document{-257}; OutputByteStream stream{}; Encoder encoder{stream}; @@ -77,7 +77,7 @@ TEST(Encoder, ANY_PACKED_TYPE_TAG_BYTE_PREFIX__minus_257) { TEST(Encoder, ANY_PACKED_TYPE_TAG_BYTE_PREFIX__255) { using namespace sourcemeta::jsonbinpack; - sourcemeta::jsontoolkit::JSON document{sourcemeta::jsontoolkit::from(255)}; + sourcemeta::jsontoolkit::JSON document{255}; OutputByteStream stream{}; Encoder encoder{stream}; @@ -90,7 +90,7 @@ TEST(Encoder, ANY_PACKED_TYPE_TAG_BYTE_PREFIX__255) { TEST(Encoder, ANY_PACKED_TYPE_TAG_BYTE_PREFIX__minus_256) { using namespace sourcemeta::jsonbinpack; - sourcemeta::jsontoolkit::JSON document{sourcemeta::jsontoolkit::from(-256)}; + sourcemeta::jsontoolkit::JSON document{-256}; OutputByteStream stream{}; Encoder encoder{stream}; @@ -103,7 +103,7 @@ TEST(Encoder, ANY_PACKED_TYPE_TAG_BYTE_PREFIX__minus_256) { TEST(Encoder, ANY_PACKED_TYPE_TAG_BYTE_PREFIX__0) { using namespace sourcemeta::jsonbinpack; - sourcemeta::jsontoolkit::JSON document{sourcemeta::jsontoolkit::from(0)}; + sourcemeta::jsontoolkit::JSON document{0}; OutputByteStream stream{}; Encoder encoder{stream}; @@ -113,7 +113,7 @@ TEST(Encoder, ANY_PACKED_TYPE_TAG_BYTE_PREFIX__0) { TEST(Encoder, ANY_PACKED_TYPE_TAG_BYTE_PREFIX__minus_1) { using namespace sourcemeta::jsonbinpack; - sourcemeta::jsontoolkit::JSON document{sourcemeta::jsontoolkit::from(-1)}; + sourcemeta::jsontoolkit::JSON document{-1}; OutputByteStream stream{}; Encoder encoder{stream}; @@ -123,7 +123,7 @@ TEST(Encoder, ANY_PACKED_TYPE_TAG_BYTE_PREFIX__minus_1) { TEST(Encoder, ANY_PACKED_TYPE_TAG_BYTE_PREFIX__string_space) { using namespace sourcemeta::jsonbinpack; - sourcemeta::jsontoolkit::JSON document{sourcemeta::jsontoolkit::from(" ")}; + sourcemeta::jsontoolkit::JSON document{" "}; OutputByteStream stream{}; Encoder encoder{stream}; @@ -133,7 +133,7 @@ TEST(Encoder, ANY_PACKED_TYPE_TAG_BYTE_PREFIX__string_space) { TEST(Encoder, ANY_PACKED_TYPE_TAG_BYTE_PREFIX__string_foo) { using namespace sourcemeta::jsonbinpack; - sourcemeta::jsontoolkit::JSON document{sourcemeta::jsontoolkit::from("foo")}; + sourcemeta::jsontoolkit::JSON document{"foo"}; OutputByteStream stream{}; Encoder encoder{stream}; @@ -143,9 +143,8 @@ TEST(Encoder, ANY_PACKED_TYPE_TAG_BYTE_PREFIX__string_foo) { TEST(Encoder, ANY_PACKED_TYPE_TAG_BYTE_PREFIX__string_30_xs) { using namespace sourcemeta::jsonbinpack; - sourcemeta::jsontoolkit::JSON document{ - sourcemeta::jsontoolkit::from(std::string(30, 'x'))}; - EXPECT_EQ(sourcemeta::jsontoolkit::size(document), 30); + sourcemeta::jsontoolkit::JSON document{std::string(30, 'x')}; + EXPECT_EQ(document.size(), 30); OutputByteStream stream{}; Encoder encoder{stream}; @@ -158,7 +157,7 @@ TEST(Encoder, ANY_PACKED_TYPE_TAG_BYTE_PREFIX__string_30_xs) { TEST(Encoder, ANY_PACKED_TYPE_TAG_BYTE_PREFIX__shared_string_foo) { using namespace sourcemeta::jsonbinpack; - sourcemeta::jsontoolkit::JSON document{sourcemeta::jsontoolkit::from("foo")}; + sourcemeta::jsontoolkit::JSON document{"foo"}; OutputByteStream stream{}; Encoder encoder{stream}; @@ -177,9 +176,8 @@ TEST(Encoder, ANY_PACKED_TYPE_TAG_BYTE_PREFIX__shared_string_foo) { TEST(Encoder, ANY_PACKED_TYPE_TAG_BYTE_PREFIX__string_31_xs) { using namespace sourcemeta::jsonbinpack; - sourcemeta::jsontoolkit::JSON document{ - sourcemeta::jsontoolkit::from(std::string(31, 'x'))}; - EXPECT_EQ(sourcemeta::jsontoolkit::size(document), 31); + sourcemeta::jsontoolkit::JSON document{std::string(31, 'x')}; + EXPECT_EQ(document.size(), 31); OutputByteStream stream{}; Encoder encoder{stream}; @@ -192,9 +190,8 @@ TEST(Encoder, ANY_PACKED_TYPE_TAG_BYTE_PREFIX__string_31_xs) { TEST(Encoder, ANY_PACKED_TYPE_TAG_BYTE_PREFIX__string_61_xs) { using namespace sourcemeta::jsonbinpack; - sourcemeta::jsontoolkit::JSON document{ - sourcemeta::jsontoolkit::from(std::string(61, 'x'))}; - EXPECT_EQ(sourcemeta::jsontoolkit::size(document), 61); + sourcemeta::jsontoolkit::JSON document{std::string(61, 'x')}; + EXPECT_EQ(document.size(), 61); OutputByteStream stream{}; Encoder encoder{stream}; @@ -210,8 +207,7 @@ TEST(Encoder, ANY_PACKED_TYPE_TAG_BYTE_PREFIX__string_61_xs) { TEST(Encoder, ANY_PACKED_TYPE_TAG_BYTE_PREFIX__string_url) { using namespace sourcemeta::jsonbinpack; - sourcemeta::jsontoolkit::JSON document{ - sourcemeta::jsontoolkit::from("https://soundcloud.com/dandymusicnl")}; + sourcemeta::jsontoolkit::JSON document{"https://soundcloud.com/dandymusicnl"}; OutputByteStream stream{}; Encoder encoder{stream}; @@ -224,9 +220,8 @@ TEST(Encoder, ANY_PACKED_TYPE_TAG_BYTE_PREFIX__string_url) { TEST(Encoder, ANY_PACKED_TYPE_TAG_BYTE_PREFIX__string_128_xs) { using namespace sourcemeta::jsonbinpack; - sourcemeta::jsontoolkit::JSON document{ - sourcemeta::jsontoolkit::from(std::string(128, 'x'))}; - EXPECT_EQ(sourcemeta::jsontoolkit::size(document), 128); + sourcemeta::jsontoolkit::JSON document{std::string(128, 'x')}; + EXPECT_EQ(document.size(), 128); OutputByteStream stream{}; Encoder encoder{stream}; @@ -250,9 +245,8 @@ TEST(Encoder, ANY_PACKED_TYPE_TAG_BYTE_PREFIX__string_128_xs) { TEST(Encoder, ANY_PACKED_TYPE_TAG_BYTE_PREFIX__string_130_xs) { using namespace sourcemeta::jsonbinpack; - sourcemeta::jsontoolkit::JSON document{ - sourcemeta::jsontoolkit::from(std::string(130, 'x'))}; - EXPECT_EQ(sourcemeta::jsontoolkit::size(document), 130); + sourcemeta::jsontoolkit::JSON document{std::string(130, 'x')}; + EXPECT_EQ(document.size(), 130); OutputByteStream stream{}; Encoder encoder{stream}; @@ -276,9 +270,8 @@ TEST(Encoder, ANY_PACKED_TYPE_TAG_BYTE_PREFIX__string_130_xs) { TEST(Encoder, ANY_PACKED_TYPE_TAG_BYTE_PREFIX__string_256_xs) { using namespace sourcemeta::jsonbinpack; - sourcemeta::jsontoolkit::JSON document{ - sourcemeta::jsontoolkit::from(std::string(256, 'x'))}; - EXPECT_EQ(sourcemeta::jsontoolkit::size(document), 256); + sourcemeta::jsontoolkit::JSON document{std::string(256, 'x')}; + EXPECT_EQ(document.size(), 256); OutputByteStream stream{}; Encoder encoder{stream}; @@ -316,9 +309,8 @@ TEST(Encoder, ANY_PACKED_TYPE_TAG_BYTE_PREFIX__string_256_xs) { TEST(Encoder, ANY_PACKED_TYPE_TAG_BYTE_PREFIX__string_258_xs) { using namespace sourcemeta::jsonbinpack; - sourcemeta::jsontoolkit::JSON document{ - sourcemeta::jsontoolkit::from(std::string(258, 'x'))}; - EXPECT_EQ(sourcemeta::jsontoolkit::size(document), 258); + sourcemeta::jsontoolkit::JSON document{std::string(258, 'x')}; + EXPECT_EQ(document.size(), 258); OutputByteStream stream{}; Encoder encoder{stream}; @@ -356,9 +348,8 @@ TEST(Encoder, ANY_PACKED_TYPE_TAG_BYTE_PREFIX__string_258_xs) { TEST(Encoder, ANY_PACKED_TYPE_TAG_BYTE_PREFIX__string_512_xs) { using namespace sourcemeta::jsonbinpack; - sourcemeta::jsontoolkit::JSON document{ - sourcemeta::jsontoolkit::from(std::string(512, 'x'))}; - EXPECT_EQ(sourcemeta::jsontoolkit::size(document), 512); + sourcemeta::jsontoolkit::JSON document{std::string(512, 'x')}; + EXPECT_EQ(document.size(), 512); OutputByteStream stream{}; Encoder encoder{stream}; @@ -423,9 +414,8 @@ TEST(Encoder, ANY_PACKED_TYPE_TAG_BYTE_PREFIX__string_512_xs) { TEST(Encoder, ANY_PACKED_TYPE_TAG_BYTE_PREFIX__string_513_xs) { using namespace sourcemeta::jsonbinpack; - sourcemeta::jsontoolkit::JSON document{ - sourcemeta::jsontoolkit::from(std::string(513, 'x'))}; - EXPECT_EQ(sourcemeta::jsontoolkit::size(document), 513); + sourcemeta::jsontoolkit::JSON document{std::string(513, 'x')}; + EXPECT_EQ(document.size(), 513); OutputByteStream stream{}; Encoder encoder{stream}; @@ -491,9 +481,8 @@ TEST(Encoder, ANY_PACKED_TYPE_TAG_BYTE_PREFIX__string_513_xs) { TEST(Encoder, ANY_PACKED_TYPE_TAG_BYTE_PREFIX__string_1024_xs) { using namespace sourcemeta::jsonbinpack; - sourcemeta::jsontoolkit::JSON document{ - sourcemeta::jsontoolkit::from(std::string(1024, 'x'))}; - EXPECT_EQ(sourcemeta::jsontoolkit::size(document), 1024); + sourcemeta::jsontoolkit::JSON document{std::string(1024, 'x')}; + EXPECT_EQ(document.size(), 1024); OutputByteStream stream{}; Encoder encoder{stream}; @@ -609,9 +598,8 @@ TEST(Encoder, ANY_PACKED_TYPE_TAG_BYTE_PREFIX__string_1024_xs) { TEST(Encoder, ANY_PACKED_TYPE_TAG_BYTE_PREFIX__string_62_xs_non_shared) { using namespace sourcemeta::jsonbinpack; - sourcemeta::jsontoolkit::JSON document{ - sourcemeta::jsontoolkit::from(std::string(62, 'x'))}; - EXPECT_EQ(sourcemeta::jsontoolkit::size(document), 62); + sourcemeta::jsontoolkit::JSON document{std::string(62, 'x')}; + EXPECT_EQ(document.size(), 62); OutputByteStream stream{}; Encoder encoder{stream}; @@ -627,9 +615,8 @@ TEST(Encoder, ANY_PACKED_TYPE_TAG_BYTE_PREFIX__string_62_xs_non_shared) { TEST(Encoder, ANY_PACKED_TYPE_TAG_BYTE_PREFIX__string_63_xs_non_shared) { using namespace sourcemeta::jsonbinpack; - sourcemeta::jsontoolkit::JSON document{ - sourcemeta::jsontoolkit::from(std::string(63, 'x'))}; - EXPECT_EQ(sourcemeta::jsontoolkit::size(document), 63); + sourcemeta::jsontoolkit::JSON document{std::string(63, 'x')}; + EXPECT_EQ(document.size(), 63); OutputByteStream stream{}; Encoder encoder{stream}; @@ -661,13 +648,13 @@ TEST(Encoder, ANY_PACKED_TYPE_TAG_BYTE_PREFIX__foo_true_2000) { TEST(Encoder, ANY_PACKED_TYPE_TAG_BYTE_PREFIX__array_30) { using namespace sourcemeta::jsonbinpack; - sourcemeta::jsontoolkit::JSON document{sourcemeta::jsontoolkit::make_array()}; + sourcemeta::jsontoolkit::JSON document{ + sourcemeta::jsontoolkit::JSON::Array{}}; for (auto count = 0; count < 30; count++) { - sourcemeta::jsontoolkit::push_back(document, - sourcemeta::jsontoolkit::from(true)); + document.push_back(sourcemeta::jsontoolkit::JSON{true}); } - EXPECT_EQ(sourcemeta::jsontoolkit::size(document), 30); + EXPECT_EQ(document.size(), 30); OutputByteStream stream{}; Encoder encoder{stream}; @@ -681,13 +668,13 @@ TEST(Encoder, ANY_PACKED_TYPE_TAG_BYTE_PREFIX__array_30) { TEST(Encoder, ANY_PACKED_TYPE_TAG_BYTE_PREFIX__array_31) { using namespace sourcemeta::jsonbinpack; - sourcemeta::jsontoolkit::JSON document{sourcemeta::jsontoolkit::make_array()}; + sourcemeta::jsontoolkit::JSON document{ + sourcemeta::jsontoolkit::JSON::Array{}}; for (auto count = 0; count < 31; count++) { - sourcemeta::jsontoolkit::push_back(document, - sourcemeta::jsontoolkit::from(true)); + document.push_back(sourcemeta::jsontoolkit::JSON{true}); } - EXPECT_EQ(sourcemeta::jsontoolkit::size(document), 31); + EXPECT_EQ(document.size(), 31); OutputByteStream stream{}; Encoder encoder{stream}; @@ -703,13 +690,13 @@ TEST(Encoder, ANY_PACKED_TYPE_TAG_BYTE_PREFIX__array_31) { TEST(Encoder, ANY_PACKED_TYPE_TAG_BYTE_PREFIX__array_32) { using namespace sourcemeta::jsonbinpack; - sourcemeta::jsontoolkit::JSON document{sourcemeta::jsontoolkit::make_array()}; + sourcemeta::jsontoolkit::JSON document{ + sourcemeta::jsontoolkit::JSON::Array{}}; for (auto count = 0; count < 32; count++) { - sourcemeta::jsontoolkit::push_back(document, - sourcemeta::jsontoolkit::from(true)); + document.push_back(sourcemeta::jsontoolkit::JSON{true}); } - EXPECT_EQ(sourcemeta::jsontoolkit::size(document), 32); + EXPECT_EQ(document.size(), 32); OutputByteStream stream{}; Encoder encoder{stream}; @@ -734,52 +721,51 @@ TEST(Encoder, ANY_PACKED_TYPE_TAG_BYTE_PREFIX__object_foo_bar_baz_1) { EXPECT_BYTES(stream, { 0x1b, // tag: object (with length 2) - 0x04, 0x66, 0x6f, 0x6f, // String length 3 + "foo" - 0x21, 0x62, 0x61, 0x72, // String tag + length + 'bar' 0x04, 0x62, 0x61, 0x7a, // String length 3 + 'baz' - 0x15 // Positive integer tag with value 1 + 0x15, // Positive integer tag with value 1 + 0x04, 0x66, 0x6f, 0x6f, // String length 3 + "foo" + 0x21, 0x62, 0x61, 0x72 // String tag + length + 'bar' }); } TEST(Encoder, ANY_PACKED_TYPE_TAG_BYTE_PREFIX__object_30_entries) { using namespace sourcemeta::jsonbinpack; sourcemeta::jsontoolkit::JSON document{ - sourcemeta::jsontoolkit::make_object()}; - const sourcemeta::jsontoolkit::JSON value{ - sourcemeta::jsontoolkit::from(true)}; - - sourcemeta::jsontoolkit::assign(document, "00", value); - sourcemeta::jsontoolkit::assign(document, "01", value); - sourcemeta::jsontoolkit::assign(document, "02", value); - sourcemeta::jsontoolkit::assign(document, "03", value); - sourcemeta::jsontoolkit::assign(document, "04", value); - sourcemeta::jsontoolkit::assign(document, "05", value); - sourcemeta::jsontoolkit::assign(document, "06", value); - sourcemeta::jsontoolkit::assign(document, "07", value); - sourcemeta::jsontoolkit::assign(document, "08", value); - sourcemeta::jsontoolkit::assign(document, "09", value); - - sourcemeta::jsontoolkit::assign(document, "10", value); - sourcemeta::jsontoolkit::assign(document, "11", value); - sourcemeta::jsontoolkit::assign(document, "12", value); - sourcemeta::jsontoolkit::assign(document, "13", value); - sourcemeta::jsontoolkit::assign(document, "14", value); - sourcemeta::jsontoolkit::assign(document, "15", value); - sourcemeta::jsontoolkit::assign(document, "16", value); - sourcemeta::jsontoolkit::assign(document, "17", value); - sourcemeta::jsontoolkit::assign(document, "18", value); - sourcemeta::jsontoolkit::assign(document, "19", value); - - sourcemeta::jsontoolkit::assign(document, "20", value); - sourcemeta::jsontoolkit::assign(document, "21", value); - sourcemeta::jsontoolkit::assign(document, "22", value); - sourcemeta::jsontoolkit::assign(document, "23", value); - sourcemeta::jsontoolkit::assign(document, "24", value); - sourcemeta::jsontoolkit::assign(document, "25", value); - sourcemeta::jsontoolkit::assign(document, "26", value); - sourcemeta::jsontoolkit::assign(document, "27", value); - sourcemeta::jsontoolkit::assign(document, "28", value); - sourcemeta::jsontoolkit::assign(document, "29", value); + sourcemeta::jsontoolkit::JSON::Object{}}; + const sourcemeta::jsontoolkit::JSON value{true}; + + document.assign("00", value); + document.assign("01", value); + document.assign("02", value); + document.assign("03", value); + document.assign("04", value); + document.assign("05", value); + document.assign("06", value); + document.assign("07", value); + document.assign("08", value); + document.assign("09", value); + + document.assign("10", value); + document.assign("11", value); + document.assign("12", value); + document.assign("13", value); + document.assign("14", value); + document.assign("15", value); + document.assign("16", value); + document.assign("17", value); + document.assign("18", value); + document.assign("19", value); + + document.assign("20", value); + document.assign("21", value); + document.assign("22", value); + document.assign("23", value); + document.assign("24", value); + document.assign("25", value); + document.assign("26", value); + document.assign("27", value); + document.assign("28", value); + document.assign("29", value); OutputByteStream stream{}; @@ -826,44 +812,43 @@ TEST(Encoder, ANY_PACKED_TYPE_TAG_BYTE_PREFIX__object_30_entries) { TEST(Encoder, ANY_PACKED_TYPE_TAG_BYTE_PREFIX__object_31_entries) { using namespace sourcemeta::jsonbinpack; sourcemeta::jsontoolkit::JSON document{ - sourcemeta::jsontoolkit::make_object()}; - const sourcemeta::jsontoolkit::JSON value{ - sourcemeta::jsontoolkit::from(true)}; - - sourcemeta::jsontoolkit::assign(document, "00", value); - sourcemeta::jsontoolkit::assign(document, "01", value); - sourcemeta::jsontoolkit::assign(document, "02", value); - sourcemeta::jsontoolkit::assign(document, "03", value); - sourcemeta::jsontoolkit::assign(document, "04", value); - sourcemeta::jsontoolkit::assign(document, "05", value); - sourcemeta::jsontoolkit::assign(document, "06", value); - sourcemeta::jsontoolkit::assign(document, "07", value); - sourcemeta::jsontoolkit::assign(document, "08", value); - sourcemeta::jsontoolkit::assign(document, "09", value); - - sourcemeta::jsontoolkit::assign(document, "10", value); - sourcemeta::jsontoolkit::assign(document, "11", value); - sourcemeta::jsontoolkit::assign(document, "12", value); - sourcemeta::jsontoolkit::assign(document, "13", value); - sourcemeta::jsontoolkit::assign(document, "14", value); - sourcemeta::jsontoolkit::assign(document, "15", value); - sourcemeta::jsontoolkit::assign(document, "16", value); - sourcemeta::jsontoolkit::assign(document, "17", value); - sourcemeta::jsontoolkit::assign(document, "18", value); - sourcemeta::jsontoolkit::assign(document, "19", value); - - sourcemeta::jsontoolkit::assign(document, "20", value); - sourcemeta::jsontoolkit::assign(document, "21", value); - sourcemeta::jsontoolkit::assign(document, "22", value); - sourcemeta::jsontoolkit::assign(document, "23", value); - sourcemeta::jsontoolkit::assign(document, "24", value); - sourcemeta::jsontoolkit::assign(document, "25", value); - sourcemeta::jsontoolkit::assign(document, "26", value); - sourcemeta::jsontoolkit::assign(document, "27", value); - sourcemeta::jsontoolkit::assign(document, "28", value); - sourcemeta::jsontoolkit::assign(document, "29", value); - - sourcemeta::jsontoolkit::assign(document, "30", value); + sourcemeta::jsontoolkit::JSON::Object{}}; + const sourcemeta::jsontoolkit::JSON value{true}; + + document.assign("00", value); + document.assign("01", value); + document.assign("02", value); + document.assign("03", value); + document.assign("04", value); + document.assign("05", value); + document.assign("06", value); + document.assign("07", value); + document.assign("08", value); + document.assign("09", value); + + document.assign("10", value); + document.assign("11", value); + document.assign("12", value); + document.assign("13", value); + document.assign("14", value); + document.assign("15", value); + document.assign("16", value); + document.assign("17", value); + document.assign("18", value); + document.assign("19", value); + + document.assign("20", value); + document.assign("21", value); + document.assign("22", value); + document.assign("23", value); + document.assign("24", value); + document.assign("25", value); + document.assign("26", value); + document.assign("27", value); + document.assign("28", value); + document.assign("29", value); + + document.assign("30", value); OutputByteStream stream{}; @@ -913,45 +898,44 @@ TEST(Encoder, ANY_PACKED_TYPE_TAG_BYTE_PREFIX__object_31_entries) { TEST(Encoder, ANY_PACKED_TYPE_TAG_BYTE_PREFIX__object_32_entries) { using namespace sourcemeta::jsonbinpack; sourcemeta::jsontoolkit::JSON document{ - sourcemeta::jsontoolkit::make_object()}; - const sourcemeta::jsontoolkit::JSON value{ - sourcemeta::jsontoolkit::from(true)}; - - sourcemeta::jsontoolkit::assign(document, "00", value); - sourcemeta::jsontoolkit::assign(document, "01", value); - sourcemeta::jsontoolkit::assign(document, "02", value); - sourcemeta::jsontoolkit::assign(document, "03", value); - sourcemeta::jsontoolkit::assign(document, "04", value); - sourcemeta::jsontoolkit::assign(document, "05", value); - sourcemeta::jsontoolkit::assign(document, "06", value); - sourcemeta::jsontoolkit::assign(document, "07", value); - sourcemeta::jsontoolkit::assign(document, "08", value); - sourcemeta::jsontoolkit::assign(document, "09", value); - - sourcemeta::jsontoolkit::assign(document, "10", value); - sourcemeta::jsontoolkit::assign(document, "11", value); - sourcemeta::jsontoolkit::assign(document, "12", value); - sourcemeta::jsontoolkit::assign(document, "13", value); - sourcemeta::jsontoolkit::assign(document, "14", value); - sourcemeta::jsontoolkit::assign(document, "15", value); - sourcemeta::jsontoolkit::assign(document, "16", value); - sourcemeta::jsontoolkit::assign(document, "17", value); - sourcemeta::jsontoolkit::assign(document, "18", value); - sourcemeta::jsontoolkit::assign(document, "19", value); - - sourcemeta::jsontoolkit::assign(document, "20", value); - sourcemeta::jsontoolkit::assign(document, "21", value); - sourcemeta::jsontoolkit::assign(document, "22", value); - sourcemeta::jsontoolkit::assign(document, "23", value); - sourcemeta::jsontoolkit::assign(document, "24", value); - sourcemeta::jsontoolkit::assign(document, "25", value); - sourcemeta::jsontoolkit::assign(document, "26", value); - sourcemeta::jsontoolkit::assign(document, "27", value); - sourcemeta::jsontoolkit::assign(document, "28", value); - sourcemeta::jsontoolkit::assign(document, "29", value); - - sourcemeta::jsontoolkit::assign(document, "30", value); - sourcemeta::jsontoolkit::assign(document, "31", value); + sourcemeta::jsontoolkit::JSON::Object{}}; + const sourcemeta::jsontoolkit::JSON value{true}; + + document.assign("00", value); + document.assign("01", value); + document.assign("02", value); + document.assign("03", value); + document.assign("04", value); + document.assign("05", value); + document.assign("06", value); + document.assign("07", value); + document.assign("08", value); + document.assign("09", value); + + document.assign("10", value); + document.assign("11", value); + document.assign("12", value); + document.assign("13", value); + document.assign("14", value); + document.assign("15", value); + document.assign("16", value); + document.assign("17", value); + document.assign("18", value); + document.assign("19", value); + + document.assign("20", value); + document.assign("21", value); + document.assign("22", value); + document.assign("23", value); + document.assign("24", value); + document.assign("25", value); + document.assign("26", value); + document.assign("27", value); + document.assign("28", value); + document.assign("29", value); + + document.assign("30", value); + document.assign("31", value); OutputByteStream stream{}; @@ -1001,12 +985,11 @@ TEST(Encoder, ANY_PACKED_TYPE_TAG_BYTE_PREFIX__object_32_entries) { TEST(Encoder, ANY_PACKED_TYPE_TAG_BYTE_PREFIX__object_62_xs_shared) { using namespace sourcemeta::jsonbinpack; + sourcemeta::jsontoolkit::JSON document{ - sourcemeta::jsontoolkit::make_object()}; - sourcemeta::jsontoolkit::assign( - document, "foo", sourcemeta::jsontoolkit::from(std::string(62, 'x'))); - sourcemeta::jsontoolkit::assign( - document, "bar", sourcemeta::jsontoolkit::from(std::string(62, 'x'))); + sourcemeta::jsontoolkit::JSON::Object{}}; + document.assign("foo", sourcemeta::jsontoolkit::JSON{std::string(62, 'x')}); + document.assign("bar", sourcemeta::jsontoolkit::JSON{std::string(62, 'x')}); OutputByteStream stream{}; @@ -1015,7 +998,7 @@ TEST(Encoder, ANY_PACKED_TYPE_TAG_BYTE_PREFIX__object_62_xs_shared) { EXPECT_BYTES(stream, { 0x1b, // tag: object (with length 2) - 0x04, 0x66, 0x6f, 0x6f, // String length 3 + "foo" + 0x04, 0x62, 0x61, 0x72, // String tag + length + 'bar' 0x01, // tag: string 0x01, // length 62 (with uint5_max as minimum) @@ -1028,7 +1011,7 @@ TEST(Encoder, ANY_PACKED_TYPE_TAG_BYTE_PREFIX__object_62_xs_shared) { 0x78, 0x78, 0x78, 0x78, 0x78, 0x78, 0x78, 0x78, 0x78, 0x78, 0x78, 0x78, - 0x04, 0x62, 0x61, 0x72, // String tag + length + 'bar' + 0x04, 0x66, 0x6f, 0x6f, // String length 3 + "foo" 0x00, // tag: shared string 0x01, // length 62 (with uint5_max as minimum) 0x44 // pointer: 75 - 7 = 68 diff --git a/test/encoder/encode_array_test.cc b/test/encoder/encode_array_test.cc index 37550064b..2aeb439ea 100644 --- a/test/encoder/encode_array_test.cc +++ b/test/encoder/encode_array_test.cc @@ -1,15 +1,16 @@ -#include "encode_utils.h" -#include -#include -#include - #include + #include +#include "encode_utils.h" +#include +#include +#include + TEST(Encoder, FIXED_TYPED_ARRAY_0_1_2__no_prefix_encodings) { using namespace sourcemeta::jsonbinpack; - sourcemeta::jsontoolkit::JSON document{ - sourcemeta::jsontoolkit::parse("[ 0, 1, 2 ]")}; + sourcemeta::jsontoolkit::JSON document = + sourcemeta::jsontoolkit::parse("[ 0, 1, 2 ]"); OutputByteStream stream{}; Encoder encoder{stream}; @@ -20,13 +21,13 @@ TEST(Encoder, FIXED_TYPED_ARRAY_0_1_2__no_prefix_encodings) { TEST(Encoder, FIXED_TYPED_ARRAY_0_1_true__semityped) { using namespace sourcemeta::jsonbinpack; - sourcemeta::jsontoolkit::JSON document{ - sourcemeta::jsontoolkit::parse("[ 0, 1, true ]")}; + sourcemeta::jsontoolkit::JSON document = + sourcemeta::jsontoolkit::parse("[ 0, 1, true ]"); OutputByteStream stream{}; std::vector choices; - choices.push_back(sourcemeta::jsontoolkit::from(false)); - choices.push_back(sourcemeta::jsontoolkit::from(true)); + choices.push_back(sourcemeta::jsontoolkit::JSON(false)); + choices.push_back(sourcemeta::jsontoolkit::JSON(true)); Encoding first{BOUNDED_MULTIPLE_8BITS_ENUM_FIXED{0, 10, 1}}; Encoding second{BOUNDED_MULTIPLE_8BITS_ENUM_FIXED{0, 10, 1}}; @@ -40,7 +41,8 @@ TEST(Encoder, FIXED_TYPED_ARRAY_0_1_true__semityped) { TEST(Encoder, FIXED_TYPED_ARRAY_empty__no_prefix_encodings) { using namespace sourcemeta::jsonbinpack; - sourcemeta::jsontoolkit::JSON document{sourcemeta::jsontoolkit::make_array()}; + sourcemeta::jsontoolkit::JSON document{ + sourcemeta::jsontoolkit::JSON::Array{}}; OutputByteStream stream{}; Encoder encoder{stream}; @@ -51,13 +53,13 @@ TEST(Encoder, FIXED_TYPED_ARRAY_empty__no_prefix_encodings) { TEST(Encoder, BOUNDED_8BITS_TYPED_ARRAY_true_false_true__no_prefix_encodings) { using namespace sourcemeta::jsonbinpack; - sourcemeta::jsontoolkit::JSON document{ - sourcemeta::jsontoolkit::parse("[ true, false, true ]")}; + sourcemeta::jsontoolkit::JSON document = + sourcemeta::jsontoolkit::parse("[ true, false, true ]"); OutputByteStream stream{}; std::vector choices; - choices.push_back(sourcemeta::jsontoolkit::from(false)); - choices.push_back(sourcemeta::jsontoolkit::from(true)); + choices.push_back(sourcemeta::jsontoolkit::JSON(false)); + choices.push_back(sourcemeta::jsontoolkit::JSON(true)); Encoder encoder{stream}; encoder.BOUNDED_8BITS_TYPED_ARRAY( @@ -67,13 +69,13 @@ TEST(Encoder, BOUNDED_8BITS_TYPED_ARRAY_true_false_true__no_prefix_encodings) { TEST(Encoder, BOUNDED_8BITS_TYPED_ARRAY_true_false_true__same_max_min) { using namespace sourcemeta::jsonbinpack; - sourcemeta::jsontoolkit::JSON document{ - sourcemeta::jsontoolkit::parse("[ true, false, true ]")}; + sourcemeta::jsontoolkit::JSON document = + sourcemeta::jsontoolkit::parse("[ true, false, true ]"); OutputByteStream stream{}; std::vector choices; - choices.push_back(sourcemeta::jsontoolkit::from(false)); - choices.push_back(sourcemeta::jsontoolkit::from(true)); + choices.push_back(sourcemeta::jsontoolkit::JSON(false)); + choices.push_back(sourcemeta::jsontoolkit::JSON(true)); Encoder encoder{stream}; encoder.BOUNDED_8BITS_TYPED_ARRAY( @@ -83,51 +85,49 @@ TEST(Encoder, BOUNDED_8BITS_TYPED_ARRAY_true_false_true__same_max_min) { TEST(Encoder, BOUNDED_8BITS_TYPED_ARRAY_true_false_5__1_3) { using namespace sourcemeta::jsonbinpack; - sourcemeta::jsontoolkit::JSON document{ - sourcemeta::jsontoolkit::parse("[ true, false, 5 ]")}; + sourcemeta::jsontoolkit::JSON document = + sourcemeta::jsontoolkit::parse("[ true, false, 5 ]"); OutputByteStream stream{}; std::vector choices; - choices.push_back(sourcemeta::jsontoolkit::from(false)); - choices.push_back(sourcemeta::jsontoolkit::from(true)); + choices.push_back(sourcemeta::jsontoolkit::JSON(false)); + choices.push_back(sourcemeta::jsontoolkit::JSON(true)); Encoder encoder{stream}; encoder.BOUNDED_8BITS_TYPED_ARRAY( document, {1, 3, wrap(BOUNDED_MULTIPLE_8BITS_ENUM_FIXED{0, 255, 1}), - wrap({BYTE_CHOICE_INDEX{sourcemeta::jsontoolkit::copy(choices)}, - BYTE_CHOICE_INDEX{sourcemeta::jsontoolkit::copy(choices)}})}); + wrap({BYTE_CHOICE_INDEX{choices}, BYTE_CHOICE_INDEX{choices}})}); EXPECT_BYTES(stream, {0x02, 0x01, 0x00, 0x05}); } TEST(Encoder, BOUNDED_8BITS_TYPED_ARRAY_complex) { using namespace sourcemeta::jsonbinpack; - sourcemeta::jsontoolkit::JSON document{ - sourcemeta::jsontoolkit::parse("[ true, \"foo\", 1000 ]")}; + sourcemeta::jsontoolkit::JSON document = + sourcemeta::jsontoolkit::parse("[ true, \"foo\", 1000 ]"); OutputByteStream stream{}; std::vector choices; - choices.push_back(sourcemeta::jsontoolkit::from(false)); - choices.push_back(sourcemeta::jsontoolkit::from(true)); + choices.push_back(sourcemeta::jsontoolkit::JSON(false)); + choices.push_back(sourcemeta::jsontoolkit::JSON(true)); Encoder encoder{stream}; encoder.BOUNDED_8BITS_TYPED_ARRAY( - document, - {0, 10, wrap(FLOOR_MULTIPLE_ENUM_VARINT{-2, 4}), - wrap({BYTE_CHOICE_INDEX{sourcemeta::jsontoolkit::copy(choices)}, - FLOOR_VARINT_PREFIX_UTF8_STRING_SHARED{3}})}); + document, {0, 10, wrap(FLOOR_MULTIPLE_ENUM_VARINT{-2, 4}), + wrap({BYTE_CHOICE_INDEX{choices}, + FLOOR_VARINT_PREFIX_UTF8_STRING_SHARED{3}})}); EXPECT_BYTES(stream, {0x03, 0x01, 0x01, 0x66, 0x6f, 0x6f, 0xfa, 0x01}); } TEST(Encoder, FLOOR_TYPED_ARRAY_true_false_true__no_prefix_encodings) { using namespace sourcemeta::jsonbinpack; - sourcemeta::jsontoolkit::JSON document{ - sourcemeta::jsontoolkit::parse("[ true, false, true ]")}; + sourcemeta::jsontoolkit::JSON document = + sourcemeta::jsontoolkit::parse("[ true, false, true ]"); OutputByteStream stream{}; std::vector choices; - choices.push_back(sourcemeta::jsontoolkit::from(false)); - choices.push_back(sourcemeta::jsontoolkit::from(true)); + choices.push_back(sourcemeta::jsontoolkit::JSON(false)); + choices.push_back(sourcemeta::jsontoolkit::JSON(true)); Encoder encoder{stream}; encoder.FLOOR_TYPED_ARRAY( @@ -137,51 +137,49 @@ TEST(Encoder, FLOOR_TYPED_ARRAY_true_false_true__no_prefix_encodings) { TEST(Encoder, FLOOR_TYPED_ARRAY_true_false_5__1_3) { using namespace sourcemeta::jsonbinpack; - sourcemeta::jsontoolkit::JSON document{ - sourcemeta::jsontoolkit::parse("[ true, false, 5 ]")}; + sourcemeta::jsontoolkit::JSON document = + sourcemeta::jsontoolkit::parse("[ true, false, 5 ]"); OutputByteStream stream{}; std::vector choices; - choices.push_back(sourcemeta::jsontoolkit::from(false)); - choices.push_back(sourcemeta::jsontoolkit::from(true)); + choices.push_back(sourcemeta::jsontoolkit::JSON(false)); + choices.push_back(sourcemeta::jsontoolkit::JSON(true)); Encoder encoder{stream}; encoder.FLOOR_TYPED_ARRAY( document, {1, wrap(BOUNDED_MULTIPLE_8BITS_ENUM_FIXED{0, 255, 1}), - wrap({BYTE_CHOICE_INDEX{sourcemeta::jsontoolkit::copy(choices)}, - BYTE_CHOICE_INDEX{sourcemeta::jsontoolkit::copy(choices)}})}); + wrap({BYTE_CHOICE_INDEX{choices}, BYTE_CHOICE_INDEX{choices}})}); EXPECT_BYTES(stream, {0x02, 0x01, 0x00, 0x05}); } TEST(Encoder, FLOOR_TYPED_ARRAY_complex) { using namespace sourcemeta::jsonbinpack; - sourcemeta::jsontoolkit::JSON document{ - sourcemeta::jsontoolkit::parse("[ true, \"foo\", 1000 ]")}; + sourcemeta::jsontoolkit::JSON document = + sourcemeta::jsontoolkit::parse("[ true, \"foo\", 1000 ]"); OutputByteStream stream{}; std::vector choices; - choices.push_back(sourcemeta::jsontoolkit::from(false)); - choices.push_back(sourcemeta::jsontoolkit::from(true)); + choices.push_back(sourcemeta::jsontoolkit::JSON(false)); + choices.push_back(sourcemeta::jsontoolkit::JSON(true)); Encoder encoder{stream}; encoder.FLOOR_TYPED_ARRAY( - document, - {0, wrap(FLOOR_MULTIPLE_ENUM_VARINT{-2, 4}), - wrap({BYTE_CHOICE_INDEX{sourcemeta::jsontoolkit::copy(choices)}, - FLOOR_VARINT_PREFIX_UTF8_STRING_SHARED{3}})}); + document, {0, wrap(FLOOR_MULTIPLE_ENUM_VARINT{-2, 4}), + wrap({BYTE_CHOICE_INDEX{choices}, + FLOOR_VARINT_PREFIX_UTF8_STRING_SHARED{3}})}); EXPECT_BYTES(stream, {0x03, 0x01, 0x01, 0x66, 0x6f, 0x6f, 0xfa, 0x01}); } TEST(Encoder, ROOF_TYPED_ARRAY_true_false_true__no_prefix_encodings) { using namespace sourcemeta::jsonbinpack; - sourcemeta::jsontoolkit::JSON document{ - sourcemeta::jsontoolkit::parse("[ true, false, true ]")}; + sourcemeta::jsontoolkit::JSON document = + sourcemeta::jsontoolkit::parse("[ true, false, true ]"); OutputByteStream stream{}; std::vector choices; - choices.push_back(sourcemeta::jsontoolkit::from(false)); - choices.push_back(sourcemeta::jsontoolkit::from(true)); + choices.push_back(sourcemeta::jsontoolkit::JSON(false)); + choices.push_back(sourcemeta::jsontoolkit::JSON(true)); Encoder encoder{stream}; encoder.ROOF_TYPED_ARRAY( @@ -191,38 +189,36 @@ TEST(Encoder, ROOF_TYPED_ARRAY_true_false_true__no_prefix_encodings) { TEST(Encoder, ROOF_TYPED_ARRAY_true_false_5__1_3) { using namespace sourcemeta::jsonbinpack; - sourcemeta::jsontoolkit::JSON document{ - sourcemeta::jsontoolkit::parse("[ true, false, 5 ]")}; + sourcemeta::jsontoolkit::JSON document = + sourcemeta::jsontoolkit::parse("[ true, false, 5 ]"); OutputByteStream stream{}; std::vector choices; - choices.push_back(sourcemeta::jsontoolkit::from(false)); - choices.push_back(sourcemeta::jsontoolkit::from(true)); + choices.push_back(sourcemeta::jsontoolkit::JSON(false)); + choices.push_back(sourcemeta::jsontoolkit::JSON(true)); Encoder encoder{stream}; encoder.ROOF_TYPED_ARRAY( document, {5, wrap(BOUNDED_MULTIPLE_8BITS_ENUM_FIXED{0, 255, 1}), - wrap({BYTE_CHOICE_INDEX{sourcemeta::jsontoolkit::copy(choices)}, - BYTE_CHOICE_INDEX{sourcemeta::jsontoolkit::copy(choices)}})}); + wrap({BYTE_CHOICE_INDEX{choices}, BYTE_CHOICE_INDEX{choices}})}); EXPECT_BYTES(stream, {0x02, 0x01, 0x00, 0x05}); } TEST(Encoder, ROOF_TYPED_ARRAY_complex) { using namespace sourcemeta::jsonbinpack; - sourcemeta::jsontoolkit::JSON document{ - sourcemeta::jsontoolkit::parse("[ true, \"foo\", 1000 ]")}; + sourcemeta::jsontoolkit::JSON document = + sourcemeta::jsontoolkit::parse("[ true, \"foo\", 1000 ]"); OutputByteStream stream{}; std::vector choices; - choices.push_back(sourcemeta::jsontoolkit::from(false)); - choices.push_back(sourcemeta::jsontoolkit::from(true)); + choices.push_back(sourcemeta::jsontoolkit::JSON(false)); + choices.push_back(sourcemeta::jsontoolkit::JSON(true)); Encoder encoder{stream}; - encoder.ROOF_TYPED_ARRAY( - document, - {6, wrap(FLOOR_MULTIPLE_ENUM_VARINT{-2, 4}), - wrap({BYTE_CHOICE_INDEX{sourcemeta::jsontoolkit::copy(choices)}, - ROOF_VARINT_PREFIX_UTF8_STRING_SHARED{3}})}); + encoder.ROOF_TYPED_ARRAY(document, + {6, wrap(FLOOR_MULTIPLE_ENUM_VARINT{-2, 4}), + wrap({BYTE_CHOICE_INDEX{choices}, + ROOF_VARINT_PREFIX_UTF8_STRING_SHARED{3}})}); EXPECT_BYTES(stream, {0x03, 0x01, 0x01, 0x66, 0x6f, 0x6f, 0xfa, 0x01}); } diff --git a/test/encoder/context_test.cc b/test/encoder/encode_context_test.cc similarity index 99% rename from test/encoder/context_test.cc rename to test/encoder/encode_context_test.cc index 51c43f8b5..2ac279b68 100644 --- a/test/encoder/context_test.cc +++ b/test/encoder/encode_context_test.cc @@ -1,9 +1,11 @@ -#include #include -#include + +#include #include #include +#include + TEST(Encoder, context_record_string) { sourcemeta::jsonbinpack::encoder::Context context; using ContextType = sourcemeta::jsonbinpack::encoder::Context::Type; diff --git a/test/encoder/encode_enum_test.cc b/test/encoder/encode_enum_test.cc index 61f98282b..9adb97876 100644 --- a/test/encoder/encode_enum_test.cc +++ b/test/encoder/encode_enum_test.cc @@ -1,67 +1,63 @@ -#include "encode_utils.h" -#include -#include - #include #include // std::move #include // std::vector +#include "encode_utils.h" +#include +#include + TEST(Encoder, BYTE_CHOICE_INDEX_1__1_0_0) { - const sourcemeta::jsontoolkit::JSON document{ - sourcemeta::jsontoolkit::from(1)}; + const sourcemeta::jsontoolkit::JSON document{1}; OutputByteStream stream{}; sourcemeta::jsonbinpack::Encoder encoder{stream}; std::vector choices; - choices.push_back(sourcemeta::jsontoolkit::from(1)); - choices.push_back(sourcemeta::jsontoolkit::from(0)); - choices.push_back(sourcemeta::jsontoolkit::from(0)); + choices.push_back(sourcemeta::jsontoolkit::JSON{1}); + choices.push_back(sourcemeta::jsontoolkit::JSON{0}); + choices.push_back(sourcemeta::jsontoolkit::JSON{0}); encoder.BYTE_CHOICE_INDEX(document, {std::move(choices)}); EXPECT_BYTES(stream, {0x00}); } TEST(Encoder, BYTE_CHOICE_INDEX_1__0_1_0) { - const sourcemeta::jsontoolkit::JSON document{ - sourcemeta::jsontoolkit::from(1)}; + const sourcemeta::jsontoolkit::JSON document{1}; OutputByteStream stream{}; sourcemeta::jsonbinpack::Encoder encoder{stream}; std::vector choices; - choices.push_back(sourcemeta::jsontoolkit::from(0)); - choices.push_back(sourcemeta::jsontoolkit::from(1)); - choices.push_back(sourcemeta::jsontoolkit::from(0)); + choices.push_back(sourcemeta::jsontoolkit::JSON{0}); + choices.push_back(sourcemeta::jsontoolkit::JSON{1}); + choices.push_back(sourcemeta::jsontoolkit::JSON{0}); encoder.BYTE_CHOICE_INDEX(document, {std::move(choices)}); EXPECT_BYTES(stream, {0x01}); } TEST(Encoder, BYTE_CHOICE_INDEX_1__0_0_1) { - const sourcemeta::jsontoolkit::JSON document{ - sourcemeta::jsontoolkit::from(1)}; + const sourcemeta::jsontoolkit::JSON document{1}; OutputByteStream stream{}; sourcemeta::jsonbinpack::Encoder encoder{stream}; std::vector choices; - choices.push_back(sourcemeta::jsontoolkit::from(0)); - choices.push_back(sourcemeta::jsontoolkit::from(0)); - choices.push_back(sourcemeta::jsontoolkit::from(1)); + choices.push_back(sourcemeta::jsontoolkit::JSON{0}); + choices.push_back(sourcemeta::jsontoolkit::JSON{0}); + choices.push_back(sourcemeta::jsontoolkit::JSON{1}); encoder.BYTE_CHOICE_INDEX(document, {std::move(choices)}); EXPECT_BYTES(stream, {0x02}); } TEST(Encoder, BYTE_CHOICE_INDEX_bar__foo_bar_bar) { - const sourcemeta::jsontoolkit::JSON document{ - sourcemeta::jsontoolkit::from("bar")}; + const sourcemeta::jsontoolkit::JSON document{"bar"}; OutputByteStream stream{}; sourcemeta::jsonbinpack::Encoder encoder{stream}; std::vector choices; - choices.push_back(sourcemeta::jsontoolkit::from("foo")); - choices.push_back(sourcemeta::jsontoolkit::from("bar")); - choices.push_back(sourcemeta::jsontoolkit::from("bar")); + choices.push_back(sourcemeta::jsontoolkit::JSON{"foo"}); + choices.push_back(sourcemeta::jsontoolkit::JSON{"bar"}); + choices.push_back(sourcemeta::jsontoolkit::JSON{"bar"}); encoder.BYTE_CHOICE_INDEX(document, {std::move(choices)}); EXPECT_BYTES(stream, {0x01}); } TEST(Encoder, BYTE_CHOICE_INDEX_non_scalar_1) { - const sourcemeta::jsontoolkit::JSON document{ - sourcemeta::jsontoolkit::parse("{ \"foo\": 1 }")}; + const sourcemeta::jsontoolkit::JSON document = + sourcemeta::jsontoolkit::parse("{ \"foo\": 1 }"); OutputByteStream stream{}; sourcemeta::jsonbinpack::Encoder encoder{stream}; std::vector choices; @@ -77,60 +73,56 @@ TEST(Encoder, BYTE_CHOICE_INDEX_non_scalar_1) { } TEST(Encoder, LARGE_CHOICE_INDEX_1__1_0_0) { - const sourcemeta::jsontoolkit::JSON document{ - sourcemeta::jsontoolkit::from(1)}; + const sourcemeta::jsontoolkit::JSON document{1}; OutputByteStream stream{}; sourcemeta::jsonbinpack::Encoder encoder{stream}; std::vector choices; - choices.push_back(sourcemeta::jsontoolkit::from(1)); - choices.push_back(sourcemeta::jsontoolkit::from(0)); - choices.push_back(sourcemeta::jsontoolkit::from(0)); + choices.push_back(sourcemeta::jsontoolkit::JSON{1}); + choices.push_back(sourcemeta::jsontoolkit::JSON{0}); + choices.push_back(sourcemeta::jsontoolkit::JSON{0}); encoder.LARGE_CHOICE_INDEX(document, {std::move(choices)}); EXPECT_BYTES(stream, {0x00}); } TEST(Encoder, LARGE_CHOICE_INDEX_1__0_1_0) { - const sourcemeta::jsontoolkit::JSON document{ - sourcemeta::jsontoolkit::from(1)}; + const sourcemeta::jsontoolkit::JSON document{1}; OutputByteStream stream{}; sourcemeta::jsonbinpack::Encoder encoder{stream}; std::vector choices; - choices.push_back(sourcemeta::jsontoolkit::from(0)); - choices.push_back(sourcemeta::jsontoolkit::from(1)); - choices.push_back(sourcemeta::jsontoolkit::from(0)); + choices.push_back(sourcemeta::jsontoolkit::JSON{0}); + choices.push_back(sourcemeta::jsontoolkit::JSON{1}); + choices.push_back(sourcemeta::jsontoolkit::JSON{0}); encoder.LARGE_CHOICE_INDEX(document, {std::move(choices)}); EXPECT_BYTES(stream, {0x01}); } TEST(Encoder, LARGE_CHOICE_INDEX_1__0_0_1) { - const sourcemeta::jsontoolkit::JSON document{ - sourcemeta::jsontoolkit::from(1)}; + const sourcemeta::jsontoolkit::JSON document{1}; OutputByteStream stream{}; sourcemeta::jsonbinpack::Encoder encoder{stream}; std::vector choices; - choices.push_back(sourcemeta::jsontoolkit::from(0)); - choices.push_back(sourcemeta::jsontoolkit::from(0)); - choices.push_back(sourcemeta::jsontoolkit::from(1)); + choices.push_back(sourcemeta::jsontoolkit::JSON{0}); + choices.push_back(sourcemeta::jsontoolkit::JSON{0}); + choices.push_back(sourcemeta::jsontoolkit::JSON{1}); encoder.LARGE_CHOICE_INDEX(document, {std::move(choices)}); EXPECT_BYTES(stream, {0x02}); } TEST(Encoder, LARGE_CHOICE_INDEX_bar__foo_bar_bar) { - const sourcemeta::jsontoolkit::JSON document{ - sourcemeta::jsontoolkit::from("bar")}; + const sourcemeta::jsontoolkit::JSON document{"bar"}; OutputByteStream stream{}; sourcemeta::jsonbinpack::Encoder encoder{stream}; std::vector choices; - choices.push_back(sourcemeta::jsontoolkit::from("foo")); - choices.push_back(sourcemeta::jsontoolkit::from("bar")); - choices.push_back(sourcemeta::jsontoolkit::from("bar")); + choices.push_back(sourcemeta::jsontoolkit::JSON{"foo"}); + choices.push_back(sourcemeta::jsontoolkit::JSON{"bar"}); + choices.push_back(sourcemeta::jsontoolkit::JSON{"bar"}); encoder.LARGE_CHOICE_INDEX(document, {std::move(choices)}); EXPECT_BYTES(stream, {0x01}); } TEST(Encoder, LARGE_CHOICE_INDEX_non_scalar_1) { - const sourcemeta::jsontoolkit::JSON document{ - sourcemeta::jsontoolkit::parse("{ \"foo\": 1 }")}; + const sourcemeta::jsontoolkit::JSON document = + sourcemeta::jsontoolkit::parse("{ \"foo\": 1 }"); OutputByteStream stream{}; sourcemeta::jsonbinpack::Encoder encoder{stream}; std::vector choices; @@ -146,13 +138,12 @@ TEST(Encoder, LARGE_CHOICE_INDEX_non_scalar_1) { } TEST(Encoder, LARGE_CHOICE_INDEX_enum_250) { - const sourcemeta::jsontoolkit::JSON document{ - sourcemeta::jsontoolkit::from(250)}; + const sourcemeta::jsontoolkit::JSON document{250}; OutputByteStream stream{}; sourcemeta::jsonbinpack::Encoder encoder{stream}; std::vector choices; for (std::int64_t x = 0; x < 255; x++) { - choices.push_back(sourcemeta::jsontoolkit::from(x)); + choices.push_back(sourcemeta::jsontoolkit::JSON{x}); } encoder.LARGE_CHOICE_INDEX(document, {std::move(choices)}); @@ -160,60 +151,56 @@ TEST(Encoder, LARGE_CHOICE_INDEX_enum_250) { } TEST(Encoder, TOP_LEVEL_BYTE_CHOICE_INDEX_1__1_0_0) { - const sourcemeta::jsontoolkit::JSON document{ - sourcemeta::jsontoolkit::from(1)}; + const sourcemeta::jsontoolkit::JSON document{1}; OutputByteStream stream{}; sourcemeta::jsonbinpack::Encoder encoder{stream}; std::vector choices; - choices.push_back(sourcemeta::jsontoolkit::from(1)); - choices.push_back(sourcemeta::jsontoolkit::from(0)); - choices.push_back(sourcemeta::jsontoolkit::from(0)); + choices.push_back(sourcemeta::jsontoolkit::JSON{1}); + choices.push_back(sourcemeta::jsontoolkit::JSON{0}); + choices.push_back(sourcemeta::jsontoolkit::JSON{0}); encoder.TOP_LEVEL_BYTE_CHOICE_INDEX(document, {std::move(choices)}); EXPECT_BYTES(stream, {}); } TEST(Encoder, TOP_LEVEL_BYTE_CHOICE_INDEX_1__0_1_0) { - const sourcemeta::jsontoolkit::JSON document{ - sourcemeta::jsontoolkit::from(1)}; + const sourcemeta::jsontoolkit::JSON document{1}; OutputByteStream stream{}; sourcemeta::jsonbinpack::Encoder encoder{stream}; std::vector choices; - choices.push_back(sourcemeta::jsontoolkit::from(0)); - choices.push_back(sourcemeta::jsontoolkit::from(1)); - choices.push_back(sourcemeta::jsontoolkit::from(0)); + choices.push_back(sourcemeta::jsontoolkit::JSON{0}); + choices.push_back(sourcemeta::jsontoolkit::JSON{1}); + choices.push_back(sourcemeta::jsontoolkit::JSON{0}); encoder.TOP_LEVEL_BYTE_CHOICE_INDEX(document, {std::move(choices)}); EXPECT_BYTES(stream, {0x00}); } TEST(Encoder, TOP_LEVEL_BYTE_CHOICE_INDEX_1__0_0_1) { - const sourcemeta::jsontoolkit::JSON document{ - sourcemeta::jsontoolkit::from(1)}; + const sourcemeta::jsontoolkit::JSON document{1}; OutputByteStream stream{}; sourcemeta::jsonbinpack::Encoder encoder{stream}; std::vector choices; - choices.push_back(sourcemeta::jsontoolkit::from(0)); - choices.push_back(sourcemeta::jsontoolkit::from(0)); - choices.push_back(sourcemeta::jsontoolkit::from(1)); + choices.push_back(sourcemeta::jsontoolkit::JSON{0}); + choices.push_back(sourcemeta::jsontoolkit::JSON{0}); + choices.push_back(sourcemeta::jsontoolkit::JSON{1}); encoder.TOP_LEVEL_BYTE_CHOICE_INDEX(document, {std::move(choices)}); EXPECT_BYTES(stream, {0x01}); } TEST(Encoder, TOP_LEVEL_BYTE_CHOICE_INDEX_bar__foo_bar_bar) { - const sourcemeta::jsontoolkit::JSON document{ - sourcemeta::jsontoolkit::from("bar")}; + const sourcemeta::jsontoolkit::JSON document{"bar"}; OutputByteStream stream{}; sourcemeta::jsonbinpack::Encoder encoder{stream}; std::vector choices; - choices.push_back(sourcemeta::jsontoolkit::from("foo")); - choices.push_back(sourcemeta::jsontoolkit::from("bar")); - choices.push_back(sourcemeta::jsontoolkit::from("bar")); + choices.push_back(sourcemeta::jsontoolkit::JSON{"foo"}); + choices.push_back(sourcemeta::jsontoolkit::JSON{"bar"}); + choices.push_back(sourcemeta::jsontoolkit::JSON{"bar"}); encoder.TOP_LEVEL_BYTE_CHOICE_INDEX(document, {std::move(choices)}); EXPECT_BYTES(stream, {0x00}); } TEST(Encoder, TOP_LEVEL_BYTE_CHOICE_INDEX_non_scalar_1) { - const sourcemeta::jsontoolkit::JSON document{ - sourcemeta::jsontoolkit::parse("{ \"foo\": 1 }")}; + const sourcemeta::jsontoolkit::JSON document = + sourcemeta::jsontoolkit::parse("{ \"foo\": 1 }"); OutputByteStream stream{}; sourcemeta::jsonbinpack::Encoder encoder{stream}; std::vector choices; @@ -229,19 +216,18 @@ TEST(Encoder, TOP_LEVEL_BYTE_CHOICE_INDEX_non_scalar_1) { } TEST(Encoder, CONST_NONE_scalar) { - const sourcemeta::jsontoolkit::JSON document{ - sourcemeta::jsontoolkit::from(1)}; + const sourcemeta::jsontoolkit::JSON document{1}; OutputByteStream stream{}; sourcemeta::jsonbinpack::Encoder encoder{stream}; - encoder.CONST_NONE(document, {sourcemeta::jsontoolkit::from(document)}); + encoder.CONST_NONE(document, {document}); EXPECT_BYTES(stream, {}); } TEST(Encoder, CONST_NONE_complex) { - const sourcemeta::jsontoolkit::JSON document{ - sourcemeta::jsontoolkit::parse("{ \"foo\": 1 }")}; + const sourcemeta::jsontoolkit::JSON document = + sourcemeta::jsontoolkit::parse("{ \"foo\": 1 }"); OutputByteStream stream{}; sourcemeta::jsonbinpack::Encoder encoder{stream}; - encoder.CONST_NONE(document, {sourcemeta::jsontoolkit::from(document)}); + encoder.CONST_NONE(document, {document}); EXPECT_BYTES(stream, {}); } diff --git a/test/encoder/encode_integer_test.cc b/test/encoder/encode_integer_test.cc index 15378c25a..a68f85749 100644 --- a/test/encoder/encode_integer_test.cc +++ b/test/encoder/encode_integer_test.cc @@ -1,13 +1,13 @@ -#include "encode_utils.h" -#include -#include - #include + #include // std::numeric_limits +#include "encode_utils.h" +#include +#include + TEST(Encoder, BOUNDED_MULTIPLE_8BITS_ENUM_FIXED__minus_5_minus_5_minus_1_1) { - const sourcemeta::jsontoolkit::JSON document{ - sourcemeta::jsontoolkit::from(-5)}; + const sourcemeta::jsontoolkit::JSON document{-5}; OutputByteStream stream{}; sourcemeta::jsonbinpack::Encoder encoder{stream}; encoder.BOUNDED_MULTIPLE_8BITS_ENUM_FIXED(document, {-5, -1, 1}); @@ -15,8 +15,7 @@ TEST(Encoder, BOUNDED_MULTIPLE_8BITS_ENUM_FIXED__minus_5_minus_5_minus_1_1) { } TEST(Encoder, BOUNDED_MULTIPLE_8BITS_ENUM_FIXED__2_minus_5_5_1) { - const sourcemeta::jsontoolkit::JSON document{ - sourcemeta::jsontoolkit::from(2)}; + const sourcemeta::jsontoolkit::JSON document{2}; OutputByteStream stream{}; sourcemeta::jsonbinpack::Encoder encoder{stream}; encoder.BOUNDED_MULTIPLE_8BITS_ENUM_FIXED(document, {-5, 5, 1}); @@ -24,8 +23,7 @@ TEST(Encoder, BOUNDED_MULTIPLE_8BITS_ENUM_FIXED__2_minus_5_5_1) { } TEST(Encoder, BOUNDED_MULTIPLE_8BITS_ENUM_FIXED__5_2_8_1) { - const sourcemeta::jsontoolkit::JSON document{ - sourcemeta::jsontoolkit::from(5)}; + const sourcemeta::jsontoolkit::JSON document{5}; OutputByteStream stream{}; sourcemeta::jsonbinpack::Encoder encoder{stream}; encoder.BOUNDED_MULTIPLE_8BITS_ENUM_FIXED(document, {2, 8, 1}); @@ -33,8 +31,7 @@ TEST(Encoder, BOUNDED_MULTIPLE_8BITS_ENUM_FIXED__5_2_8_1) { } TEST(Encoder, BOUNDED_MULTIPLE_8BITS_ENUM_FIXED__5_1_19_5) { - const sourcemeta::jsontoolkit::JSON document{ - sourcemeta::jsontoolkit::from(5)}; + const sourcemeta::jsontoolkit::JSON document{5}; OutputByteStream stream{}; sourcemeta::jsonbinpack::Encoder encoder{stream}; encoder.BOUNDED_MULTIPLE_8BITS_ENUM_FIXED(document, {1, 19, 5}); @@ -42,8 +39,7 @@ TEST(Encoder, BOUNDED_MULTIPLE_8BITS_ENUM_FIXED__5_1_19_5) { } TEST(Encoder, BOUNDED_MULTIPLE_8BITS_ENUM_FIXED__15_1_19_5) { - const sourcemeta::jsontoolkit::JSON document{ - sourcemeta::jsontoolkit::from(15)}; + const sourcemeta::jsontoolkit::JSON document{15}; OutputByteStream stream{}; sourcemeta::jsonbinpack::Encoder encoder{stream}; encoder.BOUNDED_MULTIPLE_8BITS_ENUM_FIXED(document, {1, 19, 5}); @@ -51,8 +47,7 @@ TEST(Encoder, BOUNDED_MULTIPLE_8BITS_ENUM_FIXED__15_1_19_5) { } TEST(Encoder, BOUNDED_MULTIPLE_8BITS_ENUM_FIXED__255_0_255_1) { - const sourcemeta::jsontoolkit::JSON document{ - sourcemeta::jsontoolkit::from(255)}; + const sourcemeta::jsontoolkit::JSON document{255}; OutputByteStream stream{}; sourcemeta::jsonbinpack::Encoder encoder{stream}; encoder.BOUNDED_MULTIPLE_8BITS_ENUM_FIXED(document, {0, 255, 1}); @@ -60,8 +55,7 @@ TEST(Encoder, BOUNDED_MULTIPLE_8BITS_ENUM_FIXED__255_0_255_1) { } TEST(Encoder, FLOOR_MULTIPLE_ENUM_VARINT__minus_3_minus_10_1) { - const sourcemeta::jsontoolkit::JSON document{ - sourcemeta::jsontoolkit::from(-3)}; + const sourcemeta::jsontoolkit::JSON document{-3}; OutputByteStream stream{}; sourcemeta::jsonbinpack::Encoder encoder{stream}; encoder.FLOOR_MULTIPLE_ENUM_VARINT(document, {-10, 1}); @@ -69,8 +63,7 @@ TEST(Encoder, FLOOR_MULTIPLE_ENUM_VARINT__minus_3_minus_10_1) { } TEST(Encoder, FLOOR_MULTIPLE_ENUM_VARINT__5_2_1) { - const sourcemeta::jsontoolkit::JSON document{ - sourcemeta::jsontoolkit::from(5)}; + const sourcemeta::jsontoolkit::JSON document{5}; OutputByteStream stream{}; sourcemeta::jsonbinpack::Encoder encoder{stream}; encoder.FLOOR_MULTIPLE_ENUM_VARINT(document, {2, 1}); @@ -78,8 +71,7 @@ TEST(Encoder, FLOOR_MULTIPLE_ENUM_VARINT__5_2_1) { } TEST(Encoder, FLOOR_MULTIPLE_ENUM_VARINT__10_5_5) { - const sourcemeta::jsontoolkit::JSON document{ - sourcemeta::jsontoolkit::from(10)}; + const sourcemeta::jsontoolkit::JSON document{10}; OutputByteStream stream{}; sourcemeta::jsonbinpack::Encoder encoder{stream}; encoder.FLOOR_MULTIPLE_ENUM_VARINT(document, {5, 5}); @@ -87,8 +79,7 @@ TEST(Encoder, FLOOR_MULTIPLE_ENUM_VARINT__10_5_5) { } TEST(Encoder, FLOOR_MULTIPLE_ENUM_VARINT__10_2_5) { - const sourcemeta::jsontoolkit::JSON document{ - sourcemeta::jsontoolkit::from(10)}; + const sourcemeta::jsontoolkit::JSON document{10}; OutputByteStream stream{}; sourcemeta::jsonbinpack::Encoder encoder{stream}; encoder.FLOOR_MULTIPLE_ENUM_VARINT(document, {2, 5}); @@ -96,8 +87,7 @@ TEST(Encoder, FLOOR_MULTIPLE_ENUM_VARINT__10_2_5) { } TEST(Encoder, FLOOR_MULTIPLE_ENUM_VARINT__1000_minus_2_4) { - const sourcemeta::jsontoolkit::JSON document{ - sourcemeta::jsontoolkit::from(1000)}; + const sourcemeta::jsontoolkit::JSON document{1000}; OutputByteStream stream{}; sourcemeta::jsonbinpack::Encoder encoder{stream}; encoder.FLOOR_MULTIPLE_ENUM_VARINT(document, {-2, 4}); @@ -105,8 +95,7 @@ TEST(Encoder, FLOOR_MULTIPLE_ENUM_VARINT__1000_minus_2_4) { } TEST(Encoder, ROOF_MULTIPLE_MIRROR_ENUM_VARINT__minus_3_minus_2_1) { - const sourcemeta::jsontoolkit::JSON document{ - sourcemeta::jsontoolkit::from(-3)}; + const sourcemeta::jsontoolkit::JSON document{-3}; OutputByteStream stream{}; sourcemeta::jsonbinpack::Encoder encoder{stream}; encoder.ROOF_MULTIPLE_MIRROR_ENUM_VARINT(document, {-2, 1}); @@ -114,8 +103,7 @@ TEST(Encoder, ROOF_MULTIPLE_MIRROR_ENUM_VARINT__minus_3_minus_2_1) { } TEST(Encoder, ROOF_MULTIPLE_MIRROR_ENUM_VARINT__8_10_1) { - const sourcemeta::jsontoolkit::JSON document{ - sourcemeta::jsontoolkit::from(8)}; + const sourcemeta::jsontoolkit::JSON document{8}; OutputByteStream stream{}; sourcemeta::jsonbinpack::Encoder encoder{stream}; encoder.ROOF_MULTIPLE_MIRROR_ENUM_VARINT(document, {10, 1}); @@ -123,8 +111,7 @@ TEST(Encoder, ROOF_MULTIPLE_MIRROR_ENUM_VARINT__8_10_1) { } TEST(Encoder, ROOF_MULTIPLE_MIRROR_ENUM_VARINT__5_16_5) { - const sourcemeta::jsontoolkit::JSON document{ - sourcemeta::jsontoolkit::from(5)}; + const sourcemeta::jsontoolkit::JSON document{5}; OutputByteStream stream{}; sourcemeta::jsonbinpack::Encoder encoder{stream}; encoder.ROOF_MULTIPLE_MIRROR_ENUM_VARINT(document, {15, 5}); @@ -132,8 +119,7 @@ TEST(Encoder, ROOF_MULTIPLE_MIRROR_ENUM_VARINT__5_16_5) { } TEST(Encoder, ROOF_MULTIPLE_MIRROR_ENUM_VARINT__10_15_5) { - const sourcemeta::jsontoolkit::JSON document{ - sourcemeta::jsontoolkit::from(10)}; + const sourcemeta::jsontoolkit::JSON document{10}; OutputByteStream stream{}; sourcemeta::jsonbinpack::Encoder encoder{stream}; encoder.ROOF_MULTIPLE_MIRROR_ENUM_VARINT(document, {15, 5}); @@ -141,8 +127,7 @@ TEST(Encoder, ROOF_MULTIPLE_MIRROR_ENUM_VARINT__10_15_5) { } TEST(Encoder, ARBITRARY_MULTIPLE_ZIGZAG_VARINT__minus_25200_1) { - const sourcemeta::jsontoolkit::JSON document{ - sourcemeta::jsontoolkit::from(-25200)}; + const sourcemeta::jsontoolkit::JSON document{-25200}; OutputByteStream stream{}; sourcemeta::jsonbinpack::Encoder encoder{stream}; encoder.ARBITRARY_MULTIPLE_ZIGZAG_VARINT(document, {1}); @@ -150,8 +135,7 @@ TEST(Encoder, ARBITRARY_MULTIPLE_ZIGZAG_VARINT__minus_25200_1) { } TEST(Encoder, ARBITRARY_MULTIPLE_ZIGZAG_VARINT__10_5) { - const sourcemeta::jsontoolkit::JSON document{ - sourcemeta::jsontoolkit::from(10)}; + const sourcemeta::jsontoolkit::JSON document{10}; OutputByteStream stream{}; sourcemeta::jsonbinpack::Encoder encoder{stream}; encoder.ARBITRARY_MULTIPLE_ZIGZAG_VARINT(document, {5}); @@ -160,8 +144,7 @@ TEST(Encoder, ARBITRARY_MULTIPLE_ZIGZAG_VARINT__10_5) { TEST(Encoder, ARBITRARY_MULTIPLE_ZIGZAG_VARINT__int64_max_1) { const std::int64_t value = std::numeric_limits::max(); - const sourcemeta::jsontoolkit::JSON document{ - sourcemeta::jsontoolkit::from(value)}; + const sourcemeta::jsontoolkit::JSON document{value}; OutputByteStream stream{}; sourcemeta::jsonbinpack::Encoder encoder{stream}; encoder.ARBITRARY_MULTIPLE_ZIGZAG_VARINT(document, {1}); @@ -171,8 +154,7 @@ TEST(Encoder, ARBITRARY_MULTIPLE_ZIGZAG_VARINT__int64_max_1) { TEST(Encoder, ARBITRARY_MULTIPLE_ZIGZAG_VARINT__int64_min_1) { const std::int64_t value = std::numeric_limits::min() + 1; - const sourcemeta::jsontoolkit::JSON document{ - sourcemeta::jsontoolkit::from(value)}; + const sourcemeta::jsontoolkit::JSON document{value}; OutputByteStream stream{}; sourcemeta::jsonbinpack::Encoder encoder{stream}; encoder.ARBITRARY_MULTIPLE_ZIGZAG_VARINT(document, {1}); diff --git a/test/encoder/encode_number_test.cc b/test/encoder/encode_number_test.cc index e33633a31..11f8e8197 100644 --- a/test/encoder/encode_number_test.cc +++ b/test/encoder/encode_number_test.cc @@ -1,12 +1,11 @@ -#include "encode_utils.h" -#include -#include - #include +#include "encode_utils.h" +#include +#include + TEST(Encoder, DOUBLE_VARINT_TUPLE_5) { - const sourcemeta::jsontoolkit::JSON document{ - sourcemeta::jsontoolkit::from(5.0)}; + const sourcemeta::jsontoolkit::JSON document{5.0}; OutputByteStream stream{}; sourcemeta::jsonbinpack::Encoder encoder{stream}; encoder.DOUBLE_VARINT_TUPLE(document, {}); @@ -14,8 +13,7 @@ TEST(Encoder, DOUBLE_VARINT_TUPLE_5) { } TEST(Encoder, DOUBLE_VARINT_TUPLE_minus_3_point_14) { - const sourcemeta::jsontoolkit::JSON document{ - sourcemeta::jsontoolkit::from(-3.14)}; + const sourcemeta::jsontoolkit::JSON document{-3.14}; OutputByteStream stream{}; sourcemeta::jsonbinpack::Encoder encoder{stream}; encoder.DOUBLE_VARINT_TUPLE(document, {}); @@ -23,8 +21,7 @@ TEST(Encoder, DOUBLE_VARINT_TUPLE_minus_3_point_14) { } TEST(Encoder, DOUBLE_VARINT_TUPLE_minus_5) { - const sourcemeta::jsontoolkit::JSON document{ - sourcemeta::jsontoolkit::from(-5.0)}; + const sourcemeta::jsontoolkit::JSON document{-5.0}; OutputByteStream stream{}; sourcemeta::jsonbinpack::Encoder encoder{stream}; encoder.DOUBLE_VARINT_TUPLE(document, {}); @@ -32,8 +29,7 @@ TEST(Encoder, DOUBLE_VARINT_TUPLE_minus_5) { } TEST(Encoder, DOUBLE_VARINT_TUPLE_zero) { - const sourcemeta::jsontoolkit::JSON document{ - sourcemeta::jsontoolkit::from(0.0)}; + const sourcemeta::jsontoolkit::JSON document{0.0}; OutputByteStream stream{}; sourcemeta::jsonbinpack::Encoder encoder{stream}; encoder.DOUBLE_VARINT_TUPLE(document, {}); @@ -41,8 +37,7 @@ TEST(Encoder, DOUBLE_VARINT_TUPLE_zero) { } TEST(Encoder, DOUBLE_VARINT_TUPLE_1235) { - const sourcemeta::jsontoolkit::JSON document{ - sourcemeta::jsontoolkit::from(1235.0)}; + const sourcemeta::jsontoolkit::JSON document{1235.0}; OutputByteStream stream{}; sourcemeta::jsonbinpack::Encoder encoder{stream}; encoder.DOUBLE_VARINT_TUPLE(document, {}); @@ -50,8 +45,7 @@ TEST(Encoder, DOUBLE_VARINT_TUPLE_1235) { } TEST(Encoder, DOUBLE_VARINT_TUPLE_0_point_1235) { - const sourcemeta::jsontoolkit::JSON document{ - sourcemeta::jsontoolkit::from(0.1235)}; + const sourcemeta::jsontoolkit::JSON document{0.1235}; OutputByteStream stream{}; sourcemeta::jsonbinpack::Encoder encoder{stream}; encoder.DOUBLE_VARINT_TUPLE(document, {}); @@ -59,8 +53,7 @@ TEST(Encoder, DOUBLE_VARINT_TUPLE_0_point_1235) { } TEST(Encoder, DOUBLE_VARINT_TUPLE_1_point_235) { - const sourcemeta::jsontoolkit::JSON document{ - sourcemeta::jsontoolkit::from(1.235)}; + const sourcemeta::jsontoolkit::JSON document{1.235}; OutputByteStream stream{}; sourcemeta::jsonbinpack::Encoder encoder{stream}; encoder.DOUBLE_VARINT_TUPLE(document, {}); @@ -68,8 +61,7 @@ TEST(Encoder, DOUBLE_VARINT_TUPLE_1_point_235) { } TEST(Encoder, DOUBLE_VARINT_TUPLE_0_point_01235) { - const sourcemeta::jsontoolkit::JSON document{ - sourcemeta::jsontoolkit::from(0.01235)}; + const sourcemeta::jsontoolkit::JSON document{0.01235}; OutputByteStream stream{}; sourcemeta::jsonbinpack::Encoder encoder{stream}; encoder.DOUBLE_VARINT_TUPLE(document, {}); @@ -77,8 +69,7 @@ TEST(Encoder, DOUBLE_VARINT_TUPLE_0_point_01235) { } TEST(Encoder, DOUBLE_VARINT_TUPLE_12_35) { - const sourcemeta::jsontoolkit::JSON document{ - sourcemeta::jsontoolkit::from(12.35)}; + const sourcemeta::jsontoolkit::JSON document{12.35}; OutputByteStream stream{}; sourcemeta::jsonbinpack::Encoder encoder{stream}; encoder.DOUBLE_VARINT_TUPLE(document, {}); @@ -86,8 +77,7 @@ TEST(Encoder, DOUBLE_VARINT_TUPLE_12_35) { } TEST(Encoder, DOUBLE_VARINT_TUPLE_0_point_001235) { - const sourcemeta::jsontoolkit::JSON document{ - sourcemeta::jsontoolkit::from(0.001235)}; + const sourcemeta::jsontoolkit::JSON document{0.001235}; OutputByteStream stream{}; sourcemeta::jsonbinpack::Encoder encoder{stream}; encoder.DOUBLE_VARINT_TUPLE(document, {}); @@ -95,8 +85,7 @@ TEST(Encoder, DOUBLE_VARINT_TUPLE_0_point_001235) { } TEST(Encoder, DOUBLE_VARINT_TUPLE_123_point_5) { - const sourcemeta::jsontoolkit::JSON document{ - sourcemeta::jsontoolkit::from(123.5)}; + const sourcemeta::jsontoolkit::JSON document{123.5}; OutputByteStream stream{}; sourcemeta::jsonbinpack::Encoder encoder{stream}; encoder.DOUBLE_VARINT_TUPLE(document, {}); @@ -104,8 +93,7 @@ TEST(Encoder, DOUBLE_VARINT_TUPLE_123_point_5) { } TEST(Encoder, DOUBLE_VARINT_TUPLE_314) { - const sourcemeta::jsontoolkit::JSON document{ - sourcemeta::jsontoolkit::from(314.0)}; + const sourcemeta::jsontoolkit::JSON document{314.0}; OutputByteStream stream{}; sourcemeta::jsonbinpack::Encoder encoder{stream}; encoder.DOUBLE_VARINT_TUPLE(document, {}); @@ -113,8 +101,7 @@ TEST(Encoder, DOUBLE_VARINT_TUPLE_314) { } TEST(Encoder, DOUBLE_VARINT_TUPLE_0_point_314) { - const sourcemeta::jsontoolkit::JSON document{ - sourcemeta::jsontoolkit::from(0.314)}; + const sourcemeta::jsontoolkit::JSON document{0.314}; OutputByteStream stream{}; sourcemeta::jsonbinpack::Encoder encoder{stream}; encoder.DOUBLE_VARINT_TUPLE(document, {}); @@ -122,8 +109,7 @@ TEST(Encoder, DOUBLE_VARINT_TUPLE_0_point_314) { } TEST(Encoder, DOUBLE_VARINT_TUPLE_3_point_14) { - const sourcemeta::jsontoolkit::JSON document{ - sourcemeta::jsontoolkit::from(3.14)}; + const sourcemeta::jsontoolkit::JSON document{3.14}; OutputByteStream stream{}; sourcemeta::jsonbinpack::Encoder encoder{stream}; encoder.DOUBLE_VARINT_TUPLE(document, {}); @@ -131,8 +117,7 @@ TEST(Encoder, DOUBLE_VARINT_TUPLE_3_point_14) { } TEST(Encoder, DOUBLE_VARINT_TUPLE_0_point_0314) { - const sourcemeta::jsontoolkit::JSON document{ - sourcemeta::jsontoolkit::from(0.0314)}; + const sourcemeta::jsontoolkit::JSON document{0.0314}; OutputByteStream stream{}; sourcemeta::jsonbinpack::Encoder encoder{stream}; encoder.DOUBLE_VARINT_TUPLE(document, {}); @@ -140,8 +125,7 @@ TEST(Encoder, DOUBLE_VARINT_TUPLE_0_point_0314) { } TEST(Encoder, DOUBLE_VARINT_TUPLE_31_point_4) { - const sourcemeta::jsontoolkit::JSON document{ - sourcemeta::jsontoolkit::from(31.4)}; + const sourcemeta::jsontoolkit::JSON document{31.4}; OutputByteStream stream{}; sourcemeta::jsonbinpack::Encoder encoder{stream}; encoder.DOUBLE_VARINT_TUPLE(document, {}); diff --git a/test/encoder/encode_object_test.cc b/test/encoder/encode_object_test.cc index 0244868a3..cb9ecd5d9 100644 --- a/test/encoder/encode_object_test.cc +++ b/test/encoder/encode_object_test.cc @@ -1,14 +1,14 @@ -#include "encode_utils.h" -#include -#include -#include - #include +#include "encode_utils.h" +#include +#include +#include + TEST(Encoder, FIXED_TYPED_ARBITRARY_OBJECT__no_length_string__integer) { using namespace sourcemeta::jsonbinpack; - sourcemeta::jsontoolkit::JSON document{ - sourcemeta::jsontoolkit::parse("{\"foo\":1,\"bar\":2}")}; + const sourcemeta::jsontoolkit::JSON document = + sourcemeta::jsontoolkit::parse("{\"foo\":1,\"bar\":2}"); OutputByteStream stream{}; Encoder encoder{stream}; @@ -16,17 +16,17 @@ TEST(Encoder, FIXED_TYPED_ARBITRARY_OBJECT__no_length_string__integer) { document, {2, wrap(UTF8_STRING_NO_LENGTH{3}), wrap(BOUNDED_MULTIPLE_8BITS_ENUM_FIXED{0, 10, 1})}); EXPECT_BYTES(stream, { - 0x66, 0x6f, 0x6f, // "foo" - 0x01, // 1 0x62, 0x61, 0x72, // "bar" - 0x02 // 2 + 0x02, // 2 + 0x66, 0x6f, 0x6f, // "foo" + 0x01 // 1 }); } TEST(Encoder, VARINT_TYPED_ARBITRARY_OBJECT__no_length_string__integer) { using namespace sourcemeta::jsonbinpack; - sourcemeta::jsontoolkit::JSON document{ - sourcemeta::jsontoolkit::parse("{\"foo\":1,\"bar\":2}")}; + const sourcemeta::jsontoolkit::JSON document = + sourcemeta::jsontoolkit::parse("{\"foo\":1,\"bar\":2}"); OutputByteStream stream{}; Encoder encoder{stream}; @@ -35,9 +35,9 @@ TEST(Encoder, VARINT_TYPED_ARBITRARY_OBJECT__no_length_string__integer) { wrap(BOUNDED_MULTIPLE_8BITS_ENUM_FIXED{0, 10, 1})}); EXPECT_BYTES(stream, { 0x02, // length 2 - 0x66, 0x6f, 0x6f, // "foo" - 0x01, // 1 0x62, 0x61, 0x72, // "bar" - 0x02 // 2 + 0x02, // 2 + 0x66, 0x6f, 0x6f, // "foo" + 0x01 // 1 }); } diff --git a/test/encoder/encode_real_test.cc b/test/encoder/encode_real_test.cc index 63c60538e..b4173044d 100644 --- a/test/encoder/encode_real_test.cc +++ b/test/encoder/encode_real_test.cc @@ -1,5 +1,6 @@ #include -#include + +#include TEST(Encoder, real_digits_1) { const double input{3.14}; diff --git a/test/encoder/encode_string_test.cc b/test/encoder/encode_string_test.cc index e2dc3e9fe..bb301b65f 100644 --- a/test/encoder/encode_string_test.cc +++ b/test/encoder/encode_string_test.cc @@ -1,12 +1,11 @@ -#include "encode_utils.h" -#include -#include - #include +#include "encode_utils.h" +#include +#include + TEST(Encoder, UTF8_STRING_NO_LENGTH_foo_bar) { - const sourcemeta::jsontoolkit::JSON document{ - sourcemeta::jsontoolkit::from("foo bar")}; + const sourcemeta::jsontoolkit::JSON document{"foo bar"}; OutputByteStream stream{}; sourcemeta::jsonbinpack::Encoder encoder{stream}; encoder.UTF8_STRING_NO_LENGTH(document, {7}); @@ -14,8 +13,7 @@ TEST(Encoder, UTF8_STRING_NO_LENGTH_foo_bar) { } TEST(Encoder, FLOOR_VARINT_PREFIX_UTF8_STRING_SHARED_foo_3) { - const sourcemeta::jsontoolkit::JSON document{ - sourcemeta::jsontoolkit::from("foo")}; + const sourcemeta::jsontoolkit::JSON document{"foo"}; OutputByteStream stream{}; sourcemeta::jsonbinpack::Encoder encoder{stream}; encoder.FLOOR_VARINT_PREFIX_UTF8_STRING_SHARED(document, {3}); @@ -23,8 +21,7 @@ TEST(Encoder, FLOOR_VARINT_PREFIX_UTF8_STRING_SHARED_foo_3) { } TEST(Encoder, FLOOR_VARINT_PREFIX_UTF8_STRING_SHARED_foo_0_foo_3) { - const sourcemeta::jsontoolkit::JSON document{ - sourcemeta::jsontoolkit::from("foo")}; + const sourcemeta::jsontoolkit::JSON document{"foo"}; OutputByteStream stream{}; sourcemeta::jsonbinpack::Encoder encoder{stream}; encoder.FLOOR_VARINT_PREFIX_UTF8_STRING_SHARED(document, {0}); @@ -33,8 +30,7 @@ TEST(Encoder, FLOOR_VARINT_PREFIX_UTF8_STRING_SHARED_foo_0_foo_3) { } TEST(Encoder, ROOF_VARINT_PREFIX_UTF8_STRING_SHARED_foo_4) { - const sourcemeta::jsontoolkit::JSON document{ - sourcemeta::jsontoolkit::from("foo")}; + const sourcemeta::jsontoolkit::JSON document{"foo"}; OutputByteStream stream{}; sourcemeta::jsonbinpack::Encoder encoder{stream}; encoder.ROOF_VARINT_PREFIX_UTF8_STRING_SHARED(document, {4}); @@ -42,8 +38,7 @@ TEST(Encoder, ROOF_VARINT_PREFIX_UTF8_STRING_SHARED_foo_4) { } TEST(Encoder, ROOF_VARINT_PREFIX_UTF8_STRING_SHARED_foo_3_foo_5) { - const sourcemeta::jsontoolkit::JSON document{ - sourcemeta::jsontoolkit::from("foo")}; + const sourcemeta::jsontoolkit::JSON document{"foo"}; OutputByteStream stream{}; sourcemeta::jsonbinpack::Encoder encoder{stream}; encoder.ROOF_VARINT_PREFIX_UTF8_STRING_SHARED(document, {3}); @@ -52,8 +47,7 @@ TEST(Encoder, ROOF_VARINT_PREFIX_UTF8_STRING_SHARED_foo_3_foo_5) { } TEST(Encoder, BOUNDED_8BIT_PREFIX_UTF8_STRING_SHARED_foo_3_5) { - const sourcemeta::jsontoolkit::JSON document{ - sourcemeta::jsontoolkit::from("foo")}; + const sourcemeta::jsontoolkit::JSON document{"foo"}; OutputByteStream stream{}; sourcemeta::jsonbinpack::Encoder encoder{stream}; encoder.BOUNDED_8BIT_PREFIX_UTF8_STRING_SHARED(document, {3, 5}); @@ -61,8 +55,7 @@ TEST(Encoder, BOUNDED_8BIT_PREFIX_UTF8_STRING_SHARED_foo_3_5) { } TEST(Encoder, BOUNDED_8BIT_PREFIX_UTF8_STRING_SHARED_foo_3_3) { - const sourcemeta::jsontoolkit::JSON document{ - sourcemeta::jsontoolkit::from("foo")}; + const sourcemeta::jsontoolkit::JSON document{"foo"}; OutputByteStream stream{}; sourcemeta::jsonbinpack::Encoder encoder{stream}; encoder.BOUNDED_8BIT_PREFIX_UTF8_STRING_SHARED(document, {3, 3}); @@ -70,8 +63,7 @@ TEST(Encoder, BOUNDED_8BIT_PREFIX_UTF8_STRING_SHARED_foo_3_3) { } TEST(Encoder, BOUNDED_8BIT_PREFIX_UTF8_STRING_SHARED_foo_0_6_foo_3_100) { - const sourcemeta::jsontoolkit::JSON document{ - sourcemeta::jsontoolkit::from("foo")}; + const sourcemeta::jsontoolkit::JSON document{"foo"}; OutputByteStream stream{}; sourcemeta::jsonbinpack::Encoder encoder{stream}; encoder.BOUNDED_8BIT_PREFIX_UTF8_STRING_SHARED(document, {0, 6}); @@ -80,8 +72,7 @@ TEST(Encoder, BOUNDED_8BIT_PREFIX_UTF8_STRING_SHARED_foo_0_6_foo_3_100) { } TEST(Encoder, RFC3339_DATE_INTEGER_TRIPLET_2014_10_01) { - const sourcemeta::jsontoolkit::JSON document{ - sourcemeta::jsontoolkit::from("2014-10-01")}; + const sourcemeta::jsontoolkit::JSON document{"2014-10-01"}; OutputByteStream stream{}; sourcemeta::jsonbinpack::Encoder encoder{stream}; encoder.RFC3339_DATE_INTEGER_TRIPLET(document, {}); @@ -89,8 +80,7 @@ TEST(Encoder, RFC3339_DATE_INTEGER_TRIPLET_2014_10_01) { } TEST(Encoder, PREFIX_VARINT_LENGTH_STRING_SHARED_foo) { - const sourcemeta::jsontoolkit::JSON document{ - sourcemeta::jsontoolkit::from("foo")}; + const sourcemeta::jsontoolkit::JSON document{"foo"}; OutputByteStream stream{}; sourcemeta::jsonbinpack::Encoder encoder{stream}; encoder.PREFIX_VARINT_LENGTH_STRING_SHARED(document, {}); @@ -101,8 +91,7 @@ TEST(Encoder, PREFIX_VARINT_LENGTH_STRING_SHARED_foo) { } TEST(Encoder, PREFIX_VARINT_LENGTH_STRING_SHARED_foo_foo_foo_foo) { - const sourcemeta::jsontoolkit::JSON document{ - sourcemeta::jsontoolkit::from("foo")}; + const sourcemeta::jsontoolkit::JSON document{"foo"}; OutputByteStream stream{}; sourcemeta::jsonbinpack::Encoder encoder{stream}; encoder.PREFIX_VARINT_LENGTH_STRING_SHARED(document, {}); @@ -123,8 +112,7 @@ TEST(Encoder, PREFIX_VARINT_LENGTH_STRING_SHARED_foo_foo_foo_foo) { } TEST(Encoder, PREFIX_VARINT_LENGTH_STRING_SHARED_non_key_foo_key_foo) { - const sourcemeta::jsontoolkit::JSON document{ - sourcemeta::jsontoolkit::from("foo")}; + const sourcemeta::jsontoolkit::JSON document{"foo"}; OutputByteStream stream{}; sourcemeta::jsonbinpack::Encoder encoder{stream}; encoder.FLOOR_VARINT_PREFIX_UTF8_STRING_SHARED(document, {3}); @@ -139,8 +127,7 @@ TEST(Encoder, PREFIX_VARINT_LENGTH_STRING_SHARED_non_key_foo_key_foo) { } TEST(Encoder, PREFIX_VARINT_LENGTH_STRING_SHARED_key_foo_non_key_foo) { - const sourcemeta::jsontoolkit::JSON document{ - sourcemeta::jsontoolkit::from("foo")}; + const sourcemeta::jsontoolkit::JSON document{"foo"}; OutputByteStream stream{}; sourcemeta::jsonbinpack::Encoder encoder{stream}; encoder.PREFIX_VARINT_LENGTH_STRING_SHARED(document, {}); diff --git a/test/encoder/encode_test.cc b/test/encoder/encode_test.cc index 6811e3a58..bd19eeda9 100644 --- a/test/encoder/encode_test.cc +++ b/test/encoder/encode_test.cc @@ -1,13 +1,12 @@ -#include "encode_utils.h" -#include -#include - #include +#include "encode_utils.h" +#include +#include + TEST(Encoder, generic_encode_BOUNDED_MULTIPLE_8BITS_ENUM_FIXED) { using namespace sourcemeta::jsonbinpack; - const sourcemeta::jsontoolkit::JSON document{ - sourcemeta::jsontoolkit::from(-5)}; + const sourcemeta::jsontoolkit::JSON document{-5}; OutputByteStream stream{}; Encoder encoder{stream}; BOUNDED_MULTIPLE_8BITS_ENUM_FIXED options{-5, -1, 1}; diff --git a/test/encoder/encode_traits_test.cc b/test/encoder/encode_traits_test.cc index 1eb3701bc..037760a68 100644 --- a/test/encoder/encode_traits_test.cc +++ b/test/encoder/encode_traits_test.cc @@ -1,8 +1,10 @@ #include -#include + #include // std::char_traits #include +#include + using CharT = char; using Traits = std::char_traits; diff --git a/test/encoder/encode_varint_test.cc b/test/encoder/encode_varint_test.cc index 5580a8af5..535073267 100644 --- a/test/encoder/encode_varint_test.cc +++ b/test/encoder/encode_varint_test.cc @@ -1,10 +1,10 @@ -#include "encode_utils.h" -#include -#include - #include + #include // std::numeric_limits +#include "encode_utils.h" +#include + TEST(Encoder, varint_1) { OutputByteStream stream{}; sourcemeta::jsonbinpack::encoder::varint(stream, 1); diff --git a/test/encoder/encode_zigzag_test.cc b/test/encoder/encode_zigzag_test.cc index 502daca4b..057fa46df 100644 --- a/test/encoder/encode_zigzag_test.cc +++ b/test/encoder/encode_zigzag_test.cc @@ -1,9 +1,9 @@ -#include -#include - #include + #include // std::numeric_limits +#include + TEST(Encoder, zigzag_int_0_0) { const int value = 0; const std::uint64_t expected = 0; diff --git a/test/encoding/CMakeLists.txt b/test/encoding/CMakeLists.txt index 15dda3c96..d73fb9264 100644 --- a/test/encoding/CMakeLists.txt +++ b/test/encoding/CMakeLists.txt @@ -1,8 +1,16 @@ add_executable(sourcemeta_jsonbinpack_encoding_unit - encoding_traits_test.cc encoding_wrap_test.cc) -target_link_libraries(sourcemeta_jsonbinpack_encoding_unit PRIVATE gtest_main) -target_link_libraries(sourcemeta_jsonbinpack_encoding_unit PRIVATE sourcemeta_jsonbinpack_encoding) -target_link_libraries(sourcemeta_jsonbinpack_encoding_unit PRIVATE sourcemeta::jsontoolkit::json) + encoding_traits_test.cc + encoding_wrap_test.cc) +sourcemeta_jsonbinpack_add_compile_options( + PRIVATE sourcemeta_jsonbinpack_encoding_unit) + +target_link_libraries(sourcemeta_jsonbinpack_encoding_unit + PRIVATE GTest::gtest GTest::gtest_main) +target_link_libraries(sourcemeta_jsonbinpack_encoding_unit + PRIVATE sourcemeta::jsonbinpack::encoding) +target_link_libraries(sourcemeta_jsonbinpack_encoding_unit + PRIVATE sourcemeta::jsontoolkit::json) + gtest_discover_tests(sourcemeta_jsonbinpack_encoding_unit) set_target_properties(sourcemeta_jsonbinpack_encoding_unit - PROPERTIES FOLDER "Encoding") + PROPERTIES FOLDER "JSON BinPack/Encoding") diff --git a/test/encoding/encoding_traits_test.cc b/test/encoding/encoding_traits_test.cc index 0aef8a3ab..c2ba03576 100644 --- a/test/encoding/encoding_traits_test.cc +++ b/test/encoding/encoding_traits_test.cc @@ -1,5 +1,5 @@ -#include -#include +#include +#include #include #include diff --git a/test/encoding/encoding_wrap_test.cc b/test/encoding/encoding_wrap_test.cc index 3bc87122e..3a8bdebe9 100644 --- a/test/encoding/encoding_wrap_test.cc +++ b/test/encoding/encoding_wrap_test.cc @@ -1,6 +1,6 @@ -#include -#include -#include +#include +#include +#include #include #include @@ -18,8 +18,8 @@ TEST(Encoding, wrap_single_encoding) { TEST(Encoding, wrap_multiple_encodings) { using namespace sourcemeta::jsonbinpack; std::vector choices; - choices.push_back(sourcemeta::jsontoolkit::from(false)); - choices.push_back(sourcemeta::jsontoolkit::from(true)); + choices.push_back(sourcemeta::jsontoolkit::JSON{false}); + choices.push_back(sourcemeta::jsontoolkit::JSON{true}); const MultipleEncodings wrapper{ wrap({BYTE_CHOICE_INDEX{std::move(choices)}, BOUNDED_MULTIPLE_8BITS_ENUM_FIXED{0, 10, 1}})}; diff --git a/test/mapper/2020_12_states_integer_test.cc b/test/mapper/2020_12_states_integer_test.cc index c9d01e680..e02f9d0fb 100644 --- a/test/mapper/2020_12_states_integer_test.cc +++ b/test/mapper/2020_12_states_integer_test.cc @@ -1,19 +1,20 @@ -#include -#include - #include + +#include +#include + +#include #include -#include #include -static std::unordered_map test_vocabularies{ - {"https://json-schema.org/draft/2020-12/vocab/core", true}, - {"https://json-schema.org/draft/2020-12/vocab/applicator", true}, - {"https://json-schema.org/draft/2020-12/vocab/unevaluated", true}, - {"https://json-schema.org/draft/2020-12/vocab/validation", true}, - {"https://json-schema.org/draft/2020-12/vocab/meta-data", true}, - {"https://json-schema.org/draft/2020-12/vocab/format-annotation", true}, - {"https://json-schema.org/draft/2020-12/vocab/content", true}}; +static std::set test_vocabularies{ + "https://json-schema.org/draft/2020-12/vocab/core", + "https://json-schema.org/draft/2020-12/vocab/applicator", + "https://json-schema.org/draft/2020-12/vocab/unevaluated", + "https://json-schema.org/draft/2020-12/vocab/validation", + "https://json-schema.org/draft/2020-12/vocab/meta-data", + "https://json-schema.org/draft/2020-12/vocab/format-annotation", + "https://json-schema.org/draft/2020-12/vocab/content"}; TEST(MapperStates_2020_12, unbounded_infinity) { const sourcemeta::jsontoolkit::JSON schema{ @@ -66,9 +67,9 @@ TEST(MapperStates_2020_12, bounded_positive) { test_vocabularies)}; EXPECT_TRUE(states.has_value()); EXPECT_EQ(states->size(), 3); - EXPECT_EQ(sourcemeta::jsontoolkit::to_integer(states->at(0)), 3); - EXPECT_EQ(sourcemeta::jsontoolkit::to_integer(states->at(1)), 4); - EXPECT_EQ(sourcemeta::jsontoolkit::to_integer(states->at(2)), 5); + EXPECT_EQ(states->at(0).to_integer(), 3); + EXPECT_EQ(states->at(1).to_integer(), 4); + EXPECT_EQ(states->at(2).to_integer(), 5); } TEST(MapperStates_2020_12, bounded_negative) { @@ -84,9 +85,9 @@ TEST(MapperStates_2020_12, bounded_negative) { test_vocabularies)}; EXPECT_TRUE(states.has_value()); EXPECT_EQ(states->size(), 3); - EXPECT_EQ(sourcemeta::jsontoolkit::to_integer(states->at(0)), -10); - EXPECT_EQ(sourcemeta::jsontoolkit::to_integer(states->at(1)), -9); - EXPECT_EQ(sourcemeta::jsontoolkit::to_integer(states->at(2)), -8); + EXPECT_EQ(states->at(0).to_integer(), -10); + EXPECT_EQ(states->at(1).to_integer(), -9); + EXPECT_EQ(states->at(2).to_integer(), -8); } TEST(MapperStates_2020_12, bounded_negative_positive) { @@ -102,12 +103,12 @@ TEST(MapperStates_2020_12, bounded_negative_positive) { test_vocabularies)}; EXPECT_TRUE(states.has_value()); EXPECT_EQ(states->size(), 6); - EXPECT_EQ(sourcemeta::jsontoolkit::to_integer(states->at(0)), -3); - EXPECT_EQ(sourcemeta::jsontoolkit::to_integer(states->at(1)), -2); - EXPECT_EQ(sourcemeta::jsontoolkit::to_integer(states->at(2)), -1); - EXPECT_EQ(sourcemeta::jsontoolkit::to_integer(states->at(3)), 0); - EXPECT_EQ(sourcemeta::jsontoolkit::to_integer(states->at(4)), 1); - EXPECT_EQ(sourcemeta::jsontoolkit::to_integer(states->at(5)), 2); + EXPECT_EQ(states->at(0).to_integer(), -3); + EXPECT_EQ(states->at(1).to_integer(), -2); + EXPECT_EQ(states->at(2).to_integer(), -1); + EXPECT_EQ(states->at(3).to_integer(), 0); + EXPECT_EQ(states->at(4).to_integer(), 1); + EXPECT_EQ(states->at(5).to_integer(), 2); } TEST(MapperStates_2020_12, bounded_equal) { @@ -123,7 +124,7 @@ TEST(MapperStates_2020_12, bounded_equal) { test_vocabularies)}; EXPECT_TRUE(states.has_value()); EXPECT_EQ(states->size(), 1); - EXPECT_EQ(sourcemeta::jsontoolkit::to_integer(states->at(0)), 3); + EXPECT_EQ(states->at(0).to_integer(), 3); } TEST(MapperStates_2020_12, bounded_impossible) { @@ -156,9 +157,9 @@ TEST(MapperStates_2020_12, bounded_positive_with_matching_positive_multiplier) { test_vocabularies)}; EXPECT_TRUE(states.has_value()); EXPECT_EQ(states->size(), 3); - EXPECT_EQ(sourcemeta::jsontoolkit::to_integer(states->at(0)), 2); - EXPECT_EQ(sourcemeta::jsontoolkit::to_integer(states->at(1)), 4); - EXPECT_EQ(sourcemeta::jsontoolkit::to_integer(states->at(2)), 6); + EXPECT_EQ(states->at(0).to_integer(), 2); + EXPECT_EQ(states->at(1).to_integer(), 4); + EXPECT_EQ(states->at(2).to_integer(), 6); } TEST(MapperStates_2020_12, bounded_positive_with_matching_negative_multiplier) { @@ -175,9 +176,9 @@ TEST(MapperStates_2020_12, bounded_positive_with_matching_negative_multiplier) { test_vocabularies)}; EXPECT_TRUE(states.has_value()); EXPECT_EQ(states->size(), 3); - EXPECT_EQ(sourcemeta::jsontoolkit::to_integer(states->at(0)), 2); - EXPECT_EQ(sourcemeta::jsontoolkit::to_integer(states->at(1)), 4); - EXPECT_EQ(sourcemeta::jsontoolkit::to_integer(states->at(2)), 6); + EXPECT_EQ(states->at(0).to_integer(), 2); + EXPECT_EQ(states->at(1).to_integer(), 4); + EXPECT_EQ(states->at(2).to_integer(), 6); } TEST(MapperStates_2020_12, @@ -195,8 +196,8 @@ TEST(MapperStates_2020_12, test_vocabularies)}; EXPECT_TRUE(states.has_value()); EXPECT_EQ(states->size(), 2); - EXPECT_EQ(sourcemeta::jsontoolkit::to_integer(states->at(0)), 3); - EXPECT_EQ(sourcemeta::jsontoolkit::to_integer(states->at(1)), 6); + EXPECT_EQ(states->at(0).to_integer(), 3); + EXPECT_EQ(states->at(1).to_integer(), 6); } TEST(MapperStates_2020_12, bounded_positive_with_impossible_multiplier) { diff --git a/test/mapper/CMakeLists.txt b/test/mapper/CMakeLists.txt index e1ca539f5..ff729ab08 100644 --- a/test/mapper/CMakeLists.txt +++ b/test/mapper/CMakeLists.txt @@ -1,15 +1,23 @@ add_executable(sourcemeta_jsonbinpack_mapper_unit is_encoding_unit.cc make_encoding_unit.cc mapper_test.cc - 2020_12_mapper_any_test.cc - 2020_12_mapper_enum_test.cc - 2020_12_mapper_integer_test.cc - 2020_12_mapper_number_test.cc + # TODO: Re-enable these + # 2020_12_mapper_any_test.cc + # 2020_12_mapper_enum_test.cc + # 2020_12_mapper_integer_test.cc + # 2020_12_mapper_number_test.cc 2020_12_states_integer_test.cc) -target_link_libraries(sourcemeta_jsonbinpack_mapper_unit PRIVATE gtest_main) -target_link_libraries(sourcemeta_jsonbinpack_mapper_unit PRIVATE sourcemeta_jsonbinpack_mapper) -target_link_libraries(sourcemeta_jsonbinpack_mapper_unit PRIVATE sourcemeta_jsonbinpack_canonicalizer) -target_link_libraries(sourcemeta_jsonbinpack_mapper_unit PRIVATE sourcemeta::jsontoolkit::json) + +target_link_libraries(sourcemeta_jsonbinpack_mapper_unit + PRIVATE GTest::gtest GTest::gtest_main) +target_link_libraries(sourcemeta_jsonbinpack_mapper_unit + PRIVATE sourcemeta_jsonbinpack_mapper) +# target_link_libraries(sourcemeta_jsonbinpack_mapper_unit + # PRIVATE sourcemeta_jsonbinpack_canonicalizer) +target_link_libraries(sourcemeta_jsonbinpack_mapper_unit + PRIVATE sourcemeta::jsontoolkit::json) +target_link_libraries(sourcemeta_jsonbinpack_mapper_unit + PRIVATE sourcemeta::jsontoolkit::jsonschema) gtest_discover_tests(sourcemeta_jsonbinpack_mapper_unit) set_target_properties(sourcemeta_jsonbinpack_mapper_unit - PROPERTIES FOLDER "Mapper") + PROPERTIES FOLDER "JSON BinPack/Mapper") diff --git a/test/mapper/is_encoding_unit.cc b/test/mapper/is_encoding_unit.cc index eded6a767..75a7dbecd 100644 --- a/test/mapper/is_encoding_unit.cc +++ b/test/mapper/is_encoding_unit.cc @@ -1,8 +1,8 @@ -#include -#include - #include +#include +#include + TEST(MapperEncoding, is_encoding_false) { const sourcemeta::jsontoolkit::JSON schema{ sourcemeta::jsontoolkit::parse(R"JSON({ diff --git a/test/mapper/make_encoding_unit.cc b/test/mapper/make_encoding_unit.cc index 8ed5ccc7f..a5e448b8c 100644 --- a/test/mapper/make_encoding_unit.cc +++ b/test/mapper/make_encoding_unit.cc @@ -1,18 +1,19 @@ -#include -#include - #include +#include +#include +#include + TEST(MapperEncoding, make_encoding_1) { - auto result{sourcemeta::jsontoolkit::make_object()}; - auto options{sourcemeta::jsontoolkit::make_object()}; - sourcemeta::jsontoolkit::assign(options, "foo", - sourcemeta::jsontoolkit::from(1)); - sourcemeta::jsonbinpack::mapper::make_encoding(result, "FOO_BAR_BAZ", + auto result{sourcemeta::jsontoolkit::JSON::make_object()}; + auto options{sourcemeta::jsontoolkit::JSON::make_object()}; + options.assign("foo", sourcemeta::jsontoolkit::JSON{1}); + sourcemeta::jsontoolkit::SchemaTransformer transformer{result}; + sourcemeta::jsonbinpack::mapper::make_encoding(transformer, "FOO_BAR_BAZ", options); - EXPECT_TRUE(sourcemeta::jsontoolkit::is_object(result)); - EXPECT_TRUE(sourcemeta::jsontoolkit::defines(result, "$schema")); - EXPECT_TRUE(sourcemeta::jsontoolkit::defines(result, "name")); - EXPECT_TRUE(sourcemeta::jsontoolkit::defines(result, "options")); + EXPECT_TRUE(result.is_object()); + EXPECT_TRUE(result.defines("$schema")); + EXPECT_TRUE(result.defines("name")); + EXPECT_TRUE(result.defines("options")); EXPECT_TRUE(sourcemeta::jsonbinpack::mapper::is_encoding(result)); } diff --git a/test/mapper/mapper_test.cc b/test/mapper/mapper_test.cc index fdd5f7f9f..6e00ea7a8 100644 --- a/test/mapper/mapper_test.cc +++ b/test/mapper/mapper_test.cc @@ -1,30 +1,47 @@ -#include -#include - #include + +#include +#include +#include + #include +static auto test_resolver(std::string_view identifier) + -> std::future> { + std::promise> promise; + if (identifier == sourcemeta::jsonbinpack::schemas::encoding::v1::id) { + promise.set_value(sourcemeta::jsontoolkit::parse( + sourcemeta::jsonbinpack::schemas::encoding::v1::json)); + } else { + promise.set_value( + sourcemeta::jsontoolkit::official_resolver(identifier).get()); + } + + return promise.get_future(); +} + TEST(Mapper, unsupported_draft) { - sourcemeta::jsontoolkit::DefaultResolver resolver; - sourcemeta::jsonbinpack::Mapper mapper{resolver}; + sourcemeta::jsonbinpack::Mapper mapper; sourcemeta::jsontoolkit::JSON schema{sourcemeta::jsontoolkit::parse(R"JSON({ "$schema": "https://json-schema.org/draft/2019-09/schema", "type": "boolean" })JSON")}; - EXPECT_THROW( - mapper.apply(schema, "https://json-schema.org/draft/2020-12/schema"), - std::domain_error); + EXPECT_THROW(mapper.apply(schema, + sourcemeta::jsontoolkit::default_schema_walker, + test_resolver, + "https://json-schema.org/draft/2020-12/schema"), + std::domain_error); } TEST(Mapper, unknown_draft_default) { - sourcemeta::jsontoolkit::DefaultResolver resolver; - sourcemeta::jsonbinpack::Mapper mapper{resolver}; + sourcemeta::jsonbinpack::Mapper mapper; sourcemeta::jsontoolkit::JSON schema{sourcemeta::jsontoolkit::parse(R"JSON({ "type": "integer" })JSON")}; - mapper.apply(schema, "https://json-schema.org/draft/2020-12/schema"); + mapper.apply(schema, sourcemeta::jsontoolkit::default_schema_walker, + test_resolver, "https://json-schema.org/draft/2020-12/schema"); const sourcemeta::jsontoolkit::JSON expected{ sourcemeta::jsontoolkit::parse(R"JSON({ diff --git a/test/numeric/CMakeLists.txt b/test/numeric/CMakeLists.txt index 40e013565..3e8b38da5 100644 --- a/test/numeric/CMakeLists.txt +++ b/test/numeric/CMakeLists.txt @@ -1,8 +1,16 @@ add_executable(sourcemeta_jsonbinpack_numeric_unit - closest_smallest_exponent_test.cc - divide_ceil_test.cc divide_floor_test.cc uint_max_test.cc) -target_link_libraries(sourcemeta_jsonbinpack_numeric_unit PRIVATE gtest_main) -target_link_libraries(sourcemeta_jsonbinpack_numeric_unit PRIVATE sourcemeta_jsonbinpack_numeric) + numeric_closest_smallest_exponent_test.cc + numeric_divide_ceil_test.cc + numeric_divide_floor_test.cc + numeric_uint_max_test.cc) +sourcemeta_jsonbinpack_add_compile_options( + PRIVATE sourcemeta_jsonbinpack_numeric_unit) + +target_link_libraries(sourcemeta_jsonbinpack_numeric_unit + PRIVATE GTest::gtest GTest::gtest_main) +target_link_libraries(sourcemeta_jsonbinpack_numeric_unit + PRIVATE sourcemeta::jsonbinpack::numeric) + gtest_discover_tests(sourcemeta_jsonbinpack_numeric_unit) set_target_properties(sourcemeta_jsonbinpack_numeric_unit - PROPERTIES FOLDER "Numeric") + PROPERTIES FOLDER "JSON BinPack/Numeric") diff --git a/test/numeric/closest_smallest_exponent_test.cc b/test/numeric/numeric_closest_smallest_exponent_test.cc similarity index 54% rename from test/numeric/closest_smallest_exponent_test.cc rename to test/numeric/numeric_closest_smallest_exponent_test.cc index f66482181..8d95d466a 100644 --- a/test/numeric/closest_smallest_exponent_test.cc +++ b/test/numeric/numeric_closest_smallest_exponent_test.cc @@ -1,14 +1,14 @@ #include -#include +#include -TEST(Numeric, closest_smallest_exponent_2_2_1_2) { +TEST(JSONBinPack_numeric, closest_smallest_exponent_2_2_1_2) { EXPECT_EQ(sourcemeta::jsonbinpack::closest_smallest_exponent(2, 2, 1, 2), 1); } -TEST(Numeric, closest_smallest_exponent_20_2_1_6) { +TEST(JSONBinPack_numeric, closest_smallest_exponent_20_2_1_6) { EXPECT_EQ(sourcemeta::jsonbinpack::closest_smallest_exponent(20, 2, 1, 6), 4); } -TEST(Numeric, closest_smallest_exponent_20_3_1_6) { +TEST(JSONBinPack_numeric, closest_smallest_exponent_20_3_1_6) { EXPECT_EQ(sourcemeta::jsonbinpack::closest_smallest_exponent(20, 3, 1, 6), 2); } diff --git a/test/numeric/divide_ceil_test.cc b/test/numeric/numeric_divide_ceil_test.cc similarity index 78% rename from test/numeric/divide_ceil_test.cc rename to test/numeric/numeric_divide_ceil_test.cc index 14df4dd8f..f2e688fa8 100644 --- a/test/numeric/divide_ceil_test.cc +++ b/test/numeric/numeric_divide_ceil_test.cc @@ -1,65 +1,66 @@ #include #include -#include #include -TEST(Numeric, divide_ceil_simple_positive) { +#include + +TEST(JSONBinPack_numeric, divide_ceil_simple_positive) { const std::int64_t dividend{10}; const std::uint64_t divisor{5}; const std::int64_t result{2}; EXPECT_EQ(sourcemeta::jsonbinpack::divide_ceil(dividend, divisor), result); } -TEST(Numeric, divide_ceil_simple_negative) { +TEST(JSONBinPack_numeric, divide_ceil_simple_negative) { const std::int64_t dividend{-10}; const std::uint64_t divisor{5}; const std::int64_t result{-2}; EXPECT_EQ(sourcemeta::jsonbinpack::divide_ceil(dividend, divisor), result); } -TEST(Numeric, divide_ceil_small_positive) { +TEST(JSONBinPack_numeric, divide_ceil_small_positive) { const std::int64_t dividend{11}; const std::uint64_t divisor{5}; const std::int64_t result{3}; EXPECT_EQ(sourcemeta::jsonbinpack::divide_ceil(dividend, divisor), result); } -TEST(Numeric, divide_ceil_small_negative) { +TEST(JSONBinPack_numeric, divide_ceil_small_negative) { const std::int64_t dividend{-11}; const std::uint64_t divisor{5}; const std::int64_t result{-2}; EXPECT_EQ(sourcemeta::jsonbinpack::divide_ceil(dividend, divisor), result); } -TEST(Numeric, divide_ceil_positive_large_divisor) { +TEST(JSONBinPack_numeric, divide_ceil_positive_large_divisor) { const std::int64_t dividend{11}; const std::uint64_t divisor{20}; const std::int64_t result{1}; EXPECT_EQ(sourcemeta::jsonbinpack::divide_ceil(dividend, divisor), result); } -TEST(Numeric, divide_ceil_negative_large_divisor) { +TEST(JSONBinPack_numeric, divide_ceil_negative_large_divisor) { const std::int64_t dividend{-11}; const std::uint64_t divisor{20}; const std::int64_t result{0}; EXPECT_EQ(sourcemeta::jsonbinpack::divide_ceil(dividend, divisor), result); } -TEST(Numeric, divide_ceil_max_dividend_min_divisor) { +TEST(JSONBinPack_numeric, divide_ceil_max_dividend_min_divisor) { const std::int64_t dividend{std::numeric_limits::max()}; const std::uint64_t divisor{1}; const std::int64_t result{std::numeric_limits::max()}; EXPECT_EQ(sourcemeta::jsonbinpack::divide_ceil(dividend, divisor), result); } -TEST(Numeric, divide_ceil_min_dividend_min_divisor) { +TEST(JSONBinPack_numeric, divide_ceil_min_dividend_min_divisor) { const std::int64_t dividend{std::numeric_limits::min()}; const std::uint64_t divisor{1}; const std::int64_t result{std::numeric_limits::min()}; EXPECT_EQ(sourcemeta::jsonbinpack::divide_ceil(dividend, divisor), result); } -TEST(Numeric, divide_ceil_max_dividend_max_divisor) { +TEST(JSONBinPack_numeric, divide_ceil_max_dividend_max_divisor) { // This is 9223372036854775807 const std::int64_t dividend{std::numeric_limits::max()}; // This is 18446744073709551615 @@ -69,7 +70,7 @@ TEST(Numeric, divide_ceil_max_dividend_max_divisor) { EXPECT_EQ(sourcemeta::jsonbinpack::divide_ceil(dividend, divisor), result); } -TEST(Numeric, divide_ceil_min_dividend_max_divisor) { +TEST(JSONBinPack_numeric, divide_ceil_min_dividend_max_divisor) { // This is -9223372036854775808 const std::int64_t dividend{std::numeric_limits::min()}; // This is 18446744073709551615 diff --git a/test/numeric/divide_floor_test.cc b/test/numeric/numeric_divide_floor_test.cc similarity index 78% rename from test/numeric/divide_floor_test.cc rename to test/numeric/numeric_divide_floor_test.cc index 6fd258293..9a6d5cdd4 100644 --- a/test/numeric/divide_floor_test.cc +++ b/test/numeric/numeric_divide_floor_test.cc @@ -1,65 +1,66 @@ #include #include -#include #include -TEST(Numeric, divide_floor_simple_positive) { +#include + +TEST(JSONBinPack_numeric, divide_floor_simple_positive) { const std::int64_t dividend{10}; const std::uint64_t divisor{5}; const std::int64_t result{2}; EXPECT_EQ(sourcemeta::jsonbinpack::divide_floor(dividend, divisor), result); } -TEST(Numeric, divide_floor_simple_negative) { +TEST(JSONBinPack_numeric, divide_floor_simple_negative) { const std::int64_t dividend{-10}; const std::uint64_t divisor{5}; const std::int64_t result{-2}; EXPECT_EQ(sourcemeta::jsonbinpack::divide_floor(dividend, divisor), result); } -TEST(Numeric, divide_floor_small_positive) { +TEST(JSONBinPack_numeric, divide_floor_small_positive) { const std::int64_t dividend{11}; const std::uint64_t divisor{5}; const std::int64_t result{2}; EXPECT_EQ(sourcemeta::jsonbinpack::divide_floor(dividend, divisor), result); } -TEST(Numeric, divide_floor_small_negative) { +TEST(JSONBinPack_numeric, divide_floor_small_negative) { const std::int64_t dividend{-11}; const std::uint64_t divisor{5}; const std::int64_t result{-3}; EXPECT_EQ(sourcemeta::jsonbinpack::divide_floor(dividend, divisor), result); } -TEST(Numeric, divide_floor_positive_large_divisor) { +TEST(JSONBinPack_numeric, divide_floor_positive_large_divisor) { const std::int64_t dividend{11}; const std::uint64_t divisor{20}; const std::int64_t result{0}; EXPECT_EQ(sourcemeta::jsonbinpack::divide_floor(dividend, divisor), result); } -TEST(Numeric, divide_floor_negative_large_divisor) { +TEST(JSONBinPack_numeric, divide_floor_negative_large_divisor) { const std::int64_t dividend{-11}; const std::uint64_t divisor{20}; const std::int64_t result{-1}; EXPECT_EQ(sourcemeta::jsonbinpack::divide_floor(dividend, divisor), result); } -TEST(Numeric, divide_floor_max_dividend_min_divisor) { +TEST(JSONBinPack_numeric, divide_floor_max_dividend_min_divisor) { const std::int64_t dividend{std::numeric_limits::max()}; const std::uint64_t divisor{1}; const std::int64_t result{std::numeric_limits::max()}; EXPECT_EQ(sourcemeta::jsonbinpack::divide_floor(dividend, divisor), result); } -TEST(Numeric, divide_floor_min_dividend_min_divisor) { +TEST(JSONBinPack_numeric, divide_floor_min_dividend_min_divisor) { const std::int64_t dividend{std::numeric_limits::min()}; const std::uint64_t divisor{1}; const std::int64_t result{std::numeric_limits::min()}; EXPECT_EQ(sourcemeta::jsonbinpack::divide_floor(dividend, divisor), result); } -TEST(Numeric, divide_floor_max_dividend_max_divisor) { +TEST(JSONBinPack_numeric, divide_floor_max_dividend_max_divisor) { // This is 9223372036854775807 const std::int64_t dividend{std::numeric_limits::max()}; // This is 18446744073709551615 @@ -69,7 +70,7 @@ TEST(Numeric, divide_floor_max_dividend_max_divisor) { EXPECT_EQ(sourcemeta::jsonbinpack::divide_floor(dividend, divisor), result); } -TEST(Numeric, divide_floor_min_dividend_max_divisor) { +TEST(JSONBinPack_numeric, divide_floor_min_dividend_max_divisor) { // This is -9223372036854775808 const std::int64_t dividend{std::numeric_limits::min()}; // This is 18446744073709551615 diff --git a/test/numeric/uint_max_test.cc b/test/numeric/numeric_uint_max_test.cc similarity index 58% rename from test/numeric/uint_max_test.cc rename to test/numeric/numeric_uint_max_test.cc index d34d5d501..dc83f85c9 100644 --- a/test/numeric/uint_max_test.cc +++ b/test/numeric/numeric_uint_max_test.cc @@ -1,22 +1,22 @@ +#include // std::uint8_t #include -#include +#include // std::numeric_limits -#include // std::uint8_t -#include // std::numeric_limits +#include -TEST(Numeric, uint_max_8) { +TEST(JSONBinPack_numeric, uint_max_8) { EXPECT_EQ(sourcemeta::jsonbinpack::uint_max<8>, std::numeric_limits::max()); } -TEST(Numeric, uint_max_5) { +TEST(JSONBinPack_numeric, uint_max_5) { EXPECT_EQ(sourcemeta::jsonbinpack::uint_max<5>, 31); } -TEST(Numeric, uint_max_3) { +TEST(JSONBinPack_numeric, uint_max_3) { EXPECT_EQ(sourcemeta::jsonbinpack::uint_max<3>, 7); } -TEST(Numeric, uint_max_2) { +TEST(JSONBinPack_numeric, uint_max_2) { EXPECT_EQ(sourcemeta::jsonbinpack::uint_max<2>, 3); } diff --git a/test/parser/CMakeLists.txt b/test/parser/CMakeLists.txt index 0033d5824..e2c4d935e 100644 --- a/test/parser/CMakeLists.txt +++ b/test/parser/CMakeLists.txt @@ -5,10 +5,14 @@ add_executable(sourcemeta_jsonbinpack_parser_unit v1_integer_parser_test.cc v1_number_parser_test.cc v1_string_parser_test.cc) -target_link_libraries(sourcemeta_jsonbinpack_parser_unit PRIVATE gtest_main) -target_link_libraries(sourcemeta_jsonbinpack_parser_unit PRIVATE sourcemeta_jsonbinpack_parser) -target_link_libraries(sourcemeta_jsonbinpack_parser_unit PRIVATE sourcemeta_jsonbinpack_encoding) -target_link_libraries(sourcemeta_jsonbinpack_parser_unit PRIVATE sourcemeta::jsontoolkit::json) +target_link_libraries(sourcemeta_jsonbinpack_parser_unit + PRIVATE GTest::gtest GTest::gtest_main) +target_link_libraries(sourcemeta_jsonbinpack_parser_unit + PRIVATE sourcemeta::jsonbinpack::parser) +target_link_libraries(sourcemeta_jsonbinpack_parser_unit + PRIVATE sourcemeta::jsonbinpack::encoding) +target_link_libraries(sourcemeta_jsonbinpack_parser_unit + PRIVATE sourcemeta::jsontoolkit::json) gtest_discover_tests(sourcemeta_jsonbinpack_parser_unit) set_target_properties(sourcemeta_jsonbinpack_parser_unit - PROPERTIES FOLDER "Parser") + PROPERTIES FOLDER "JSON BinPack/Parser") diff --git a/test/parser/v1_any_parser_test.cc b/test/parser/v1_any_parser_test.cc index 2559a021b..6bae5f0fa 100644 --- a/test/parser/v1_any_parser_test.cc +++ b/test/parser/v1_any_parser_test.cc @@ -1,7 +1,8 @@ -#include -#include - #include + +#include +#include + #include TEST(Parser_v1, ANY_PACKED_TYPE_TAG_BYTE_PREFIX) { diff --git a/test/parser/v1_array_parser_test.cc b/test/parser/v1_array_parser_test.cc index ee06b54d1..635ee5f07 100644 --- a/test/parser/v1_array_parser_test.cc +++ b/test/parser/v1_array_parser_test.cc @@ -1,7 +1,8 @@ -#include -#include - #include + +#include +#include + #include TEST(Parser_v1, FIXED_TYPED_ARRAY_enum_integer_number) { @@ -52,12 +53,12 @@ TEST(Parser_v1, FIXED_TYPED_ARRAY_enum_integer_number) { std::get( std::get(result).prefix_encodings.at(0).value) .choices.at(0), - sourcemeta::jsontoolkit::from(1)); + sourcemeta::jsontoolkit::JSON{1}); EXPECT_EQ( std::get( std::get(result).prefix_encodings.at(0).value) .choices.at(1), - sourcemeta::jsontoolkit::from(2)); + sourcemeta::jsontoolkit::JSON{2}); EXPECT_TRUE(std::holds_alternative( std::get(result).prefix_encodings.at(1).value)); @@ -123,13 +124,13 @@ TEST(Parser_v1, BOUNDED_8BITS_TYPED_ARRAY_enum_integer_number) { .prefix_encodings.at(0) .value) .choices.at(0), - sourcemeta::jsontoolkit::from(1)); + sourcemeta::jsontoolkit::JSON{1}); EXPECT_EQ( std::get(std::get(result) .prefix_encodings.at(0) .value) .choices.at(1), - sourcemeta::jsontoolkit::from(2)); + sourcemeta::jsontoolkit::JSON{2}); EXPECT_TRUE(std::holds_alternative( std::get(result) @@ -191,12 +192,12 @@ TEST(Parser_v1, FLOOR_TYPED_ARRAY_enum_integer_number) { std::get( std::get(result).prefix_encodings.at(0).value) .choices.at(0), - sourcemeta::jsontoolkit::from(1)); + sourcemeta::jsontoolkit::JSON{1}); EXPECT_EQ( std::get( std::get(result).prefix_encodings.at(0).value) .choices.at(1), - sourcemeta::jsontoolkit::from(2)); + sourcemeta::jsontoolkit::JSON{2}); EXPECT_TRUE(std::holds_alternative( std::get(result).prefix_encodings.at(1).value)); @@ -253,11 +254,11 @@ TEST(Parser_v1, ROOF_TYPED_ARRAY_enum_integer_number) { EXPECT_EQ(std::get( std::get(result).prefix_encodings.at(0).value) .choices.at(0), - sourcemeta::jsontoolkit::from(1)); + sourcemeta::jsontoolkit::JSON{1}); EXPECT_EQ(std::get( std::get(result).prefix_encodings.at(0).value) .choices.at(1), - sourcemeta::jsontoolkit::from(2)); + sourcemeta::jsontoolkit::JSON{2}); EXPECT_TRUE(std::holds_alternative( std::get(result).prefix_encodings.at(1).value)); diff --git a/test/parser/v1_enum_parser_test.cc b/test/parser/v1_enum_parser_test.cc index 9a071721f..8d1f26f77 100644 --- a/test/parser/v1_enum_parser_test.cc +++ b/test/parser/v1_enum_parser_test.cc @@ -1,7 +1,8 @@ -#include -#include - #include + +#include +#include + #include TEST(Parser_v1, BYTE_CHOICE_INDEX_scalars) { @@ -20,11 +21,11 @@ TEST(Parser_v1, BYTE_CHOICE_INDEX_scalars) { EXPECT_TRUE(std::holds_alternative(result)); EXPECT_EQ(std::get(result).choices.size(), 3); EXPECT_EQ(std::get(result).choices.at(0), - sourcemeta::jsontoolkit::from(1)); + sourcemeta::jsontoolkit::JSON{1}); EXPECT_EQ(std::get(result).choices.at(1), - sourcemeta::jsontoolkit::from(2)); + sourcemeta::jsontoolkit::JSON{2}); EXPECT_EQ(std::get(result).choices.at(2), - sourcemeta::jsontoolkit::from(3)); + sourcemeta::jsontoolkit::JSON{3}); } TEST(Parser_v1, LARGE_CHOICE_INDEX_scalars) { @@ -43,11 +44,11 @@ TEST(Parser_v1, LARGE_CHOICE_INDEX_scalars) { EXPECT_TRUE(std::holds_alternative(result)); EXPECT_EQ(std::get(result).choices.size(), 3); EXPECT_EQ(std::get(result).choices.at(0), - sourcemeta::jsontoolkit::from(1)); + sourcemeta::jsontoolkit::JSON{1}); EXPECT_EQ(std::get(result).choices.at(1), - sourcemeta::jsontoolkit::from(2)); + sourcemeta::jsontoolkit::JSON{2}); EXPECT_EQ(std::get(result).choices.at(2), - sourcemeta::jsontoolkit::from(3)); + sourcemeta::jsontoolkit::JSON{3}); } TEST(Parser_v1, TOP_LEVEL_BYTE_CHOICE_INDEX_scalars) { @@ -66,11 +67,11 @@ TEST(Parser_v1, TOP_LEVEL_BYTE_CHOICE_INDEX_scalars) { EXPECT_TRUE(std::holds_alternative(result)); EXPECT_EQ(std::get(result).choices.size(), 3); EXPECT_EQ(std::get(result).choices.at(0), - sourcemeta::jsontoolkit::from(1)); + sourcemeta::jsontoolkit::JSON{1}); EXPECT_EQ(std::get(result).choices.at(1), - sourcemeta::jsontoolkit::from(2)); + sourcemeta::jsontoolkit::JSON{2}); EXPECT_EQ(std::get(result).choices.at(2), - sourcemeta::jsontoolkit::from(3)); + sourcemeta::jsontoolkit::JSON{3}); } TEST(Parser_v1, CONST_NONE_scalar) { @@ -88,5 +89,5 @@ TEST(Parser_v1, CONST_NONE_scalar) { using namespace sourcemeta::jsonbinpack; EXPECT_TRUE(std::holds_alternative(result)); EXPECT_EQ(std::get(result).value, - sourcemeta::jsontoolkit::from(1)); + sourcemeta::jsontoolkit::JSON{1}); } diff --git a/test/parser/v1_integer_parser_test.cc b/test/parser/v1_integer_parser_test.cc index b7f2cce31..49ae0f7d0 100644 --- a/test/parser/v1_integer_parser_test.cc +++ b/test/parser/v1_integer_parser_test.cc @@ -1,7 +1,8 @@ -#include -#include - #include + +#include +#include + #include TEST(Parser_v1, BOUNDED_MULTIPLE_8BITS_ENUM_FIXED_positive) { diff --git a/test/parser/v1_number_parser_test.cc b/test/parser/v1_number_parser_test.cc index da4beee5c..130f52188 100644 --- a/test/parser/v1_number_parser_test.cc +++ b/test/parser/v1_number_parser_test.cc @@ -1,7 +1,8 @@ -#include -#include - #include + +#include +#include + #include TEST(Parser_v1, DOUBLE_VARINT_TUPLE) { diff --git a/test/parser/v1_string_parser_test.cc b/test/parser/v1_string_parser_test.cc index 6997df7b7..c42bd78e9 100644 --- a/test/parser/v1_string_parser_test.cc +++ b/test/parser/v1_string_parser_test.cc @@ -1,7 +1,8 @@ -#include -#include - #include + +#include +#include + #include TEST(Parser_v1, UTF8_STRING_NO_LENGTH_3) { diff --git a/test/schemas/CMakeLists.txt b/test/schemas/CMakeLists.txt index 323c22808..7b5702f7e 100644 --- a/test/schemas/CMakeLists.txt +++ b/test/schemas/CMakeLists.txt @@ -2,9 +2,14 @@ # that exercise the encoding schemas against potential instances add_executable(sourcemeta_jsonbinpack_schemas_unit encoding_schema_test.cc) -target_link_libraries(sourcemeta_jsonbinpack_schemas_unit PRIVATE gtest_main) -target_link_libraries(sourcemeta_jsonbinpack_schemas_unit PRIVATE sourcemeta_jsonbinpack_schemas) -target_link_libraries(sourcemeta_jsonbinpack_schemas_unit PRIVATE sourcemeta::jsontoolkit::json) +target_link_libraries(sourcemeta_jsonbinpack_schemas_unit + PRIVATE GTest::gtest GTest::gtest_main) +target_link_libraries(sourcemeta_jsonbinpack_schemas_unit + PRIVATE sourcemeta_jsonbinpack_schemas) +target_link_libraries(sourcemeta_jsonbinpack_schemas_unit + PRIVATE sourcemeta::jsontoolkit::json) +target_link_libraries(sourcemeta_jsonbinpack_schemas_unit + PRIVATE sourcemeta::jsontoolkit::jsonschema) gtest_discover_tests(sourcemeta_jsonbinpack_schemas_unit) set_target_properties(sourcemeta_jsonbinpack_schemas_unit - PROPERTIES FOLDER "Schemas") + PROPERTIES FOLDER "JSON BinPack/Schemas") diff --git a/test/schemas/encoding_schema_test.cc b/test/schemas/encoding_schema_test.cc index f4d03052c..bbb44846f 100644 --- a/test/schemas/encoding_schema_test.cc +++ b/test/schemas/encoding_schema_test.cc @@ -1,15 +1,23 @@ -#include -#include - #include +#include +#include +#include + TEST(SchemasEncoding, v1) { const sourcemeta::jsontoolkit::JSON schema{sourcemeta::jsontoolkit::parse( sourcemeta::jsonbinpack::schemas::encoding::v1::json)}; - EXPECT_TRUE(sourcemeta::jsontoolkit::is_object(schema)); - EXPECT_TRUE(sourcemeta::jsontoolkit::defines(schema, "$id")); - const auto &identifier{sourcemeta::jsontoolkit::at(schema, "$id")}; - EXPECT_TRUE(sourcemeta::jsontoolkit::is_string(identifier)); - EXPECT_EQ(sourcemeta::jsontoolkit::to_string(identifier), - sourcemeta::jsonbinpack::schemas::encoding::v1::id); + EXPECT_TRUE(sourcemeta::jsontoolkit::is_schema(schema)); + + const std::optional dialect{ + sourcemeta::jsontoolkit::dialect(schema)}; + const std::optional id{ + sourcemeta::jsontoolkit::id(schema, + sourcemeta::jsontoolkit::official_resolver) + .get()}; + + EXPECT_TRUE(dialect.has_value()); + EXPECT_EQ(dialect.value(), "https://json-schema.org/draft/2020-12/schema"); + EXPECT_TRUE(id.has_value()); + EXPECT_EQ(id.value(), sourcemeta::jsonbinpack::schemas::encoding::v1::id); } diff --git a/vendor/googletest.mask b/vendor/googletest.mask index 2aa61fe78..97b080e57 100644 --- a/vendor/googletest.mask +++ b/vendor/googletest.mask @@ -1,4 +1,14 @@ googletest/test -googlemock/test +googletest/docs +googletest/samples +googletest/README.md +googlemock docs ci +.clang-format +BUILD.bazel +CONTRIBUTING.md +CONTRIBUTORS +googletest_deps.bzl +README.md +WORKSPACE diff --git a/vendor/googletest/.clang-format b/vendor/googletest/.clang-format deleted file mode 100644 index 5b9bfe6d2..000000000 --- a/vendor/googletest/.clang-format +++ /dev/null @@ -1,4 +0,0 @@ -# Run manually to reformat a file: -# clang-format -i --style=file -Language: Cpp -BasedOnStyle: Google diff --git a/vendor/googletest/BUILD.bazel b/vendor/googletest/BUILD.bazel deleted file mode 100644 index 965c518d7..000000000 --- a/vendor/googletest/BUILD.bazel +++ /dev/null @@ -1,190 +0,0 @@ -# Copyright 2017 Google Inc. -# All Rights Reserved. -# -# -# Redistribution and use in source and binary forms, with or without -# modification, are permitted provided that the following conditions are -# met: -# -# * Redistributions of source code must retain the above copyright -# notice, this list of conditions and the following disclaimer. -# * Redistributions in binary form must reproduce the above -# copyright notice, this list of conditions and the following disclaimer -# in the documentation and/or other materials provided with the -# distribution. -# * Neither the name of Google Inc. nor the names of its -# contributors may be used to endorse or promote products derived from -# this software without specific prior written permission. -# -# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -# -# Bazel Build for Google C++ Testing Framework(Google Test) - -load("@rules_cc//cc:defs.bzl", "cc_library", "cc_test") - -package(default_visibility = ["//visibility:public"]) - -licenses(["notice"]) - -exports_files(["LICENSE"]) - -config_setting( - name = "windows", - constraint_values = ["@platforms//os:windows"], -) - -config_setting( - name = "msvc_compiler", - flag_values = { - "@bazel_tools//tools/cpp:compiler": "msvc-cl", - }, - visibility = [":__subpackages__"], -) - -config_setting( - name = "has_absl", - values = {"define": "absl=1"}, -) - -# Library that defines the FRIEND_TEST macro. -cc_library( - name = "gtest_prod", - hdrs = ["googletest/include/gtest/gtest_prod.h"], - includes = ["googletest/include"], -) - -# Google Test including Google Mock -cc_library( - name = "gtest", - srcs = glob( - include = [ - "googletest/src/*.cc", - "googletest/src/*.h", - "googletest/include/gtest/**/*.h", - "googlemock/src/*.cc", - "googlemock/include/gmock/**/*.h", - ], - exclude = [ - "googletest/src/gtest-all.cc", - "googletest/src/gtest_main.cc", - "googlemock/src/gmock-all.cc", - "googlemock/src/gmock_main.cc", - ], - ), - hdrs = glob([ - "googletest/include/gtest/*.h", - "googlemock/include/gmock/*.h", - ]), - copts = select({ - ":windows": [], - "//conditions:default": ["-pthread"], - }), - defines = select({ - ":has_absl": ["GTEST_HAS_ABSL=1"], - "//conditions:default": [], - }), - features = select({ - ":windows": ["windows_export_all_symbols"], - "//conditions:default": [], - }), - includes = [ - "googlemock", - "googlemock/include", - "googletest", - "googletest/include", - ], - linkopts = select({ - ":windows": [], - "//conditions:default": ["-pthread"], - }), - deps = select({ - ":has_absl": [ - "@com_google_absl//absl/debugging:failure_signal_handler", - "@com_google_absl//absl/debugging:stacktrace", - "@com_google_absl//absl/debugging:symbolize", - "@com_google_absl//absl/strings", - "@com_google_absl//absl/types:any", - "@com_google_absl//absl/types:optional", - "@com_google_absl//absl/types:variant", - ], - "//conditions:default": [], - }), -) - -cc_library( - name = "gtest_main", - srcs = ["googlemock/src/gmock_main.cc"], - features = select({ - ":windows": ["windows_export_all_symbols"], - "//conditions:default": [], - }), - deps = [":gtest"], -) - -# The following rules build samples of how to use gTest. -cc_library( - name = "gtest_sample_lib", - srcs = [ - "googletest/samples/sample1.cc", - "googletest/samples/sample2.cc", - "googletest/samples/sample4.cc", - ], - hdrs = [ - "googletest/samples/prime_tables.h", - "googletest/samples/sample1.h", - "googletest/samples/sample2.h", - "googletest/samples/sample3-inl.h", - "googletest/samples/sample4.h", - ], - features = select({ - ":windows": ["windows_export_all_symbols"], - "//conditions:default": [], - }), -) - -cc_test( - name = "gtest_samples", - size = "small", - # All Samples except: - # sample9 (main) - # sample10 (main and takes a command line option and needs to be separate) - srcs = [ - "googletest/samples/sample1_unittest.cc", - "googletest/samples/sample2_unittest.cc", - "googletest/samples/sample3_unittest.cc", - "googletest/samples/sample4_unittest.cc", - "googletest/samples/sample5_unittest.cc", - "googletest/samples/sample6_unittest.cc", - "googletest/samples/sample7_unittest.cc", - "googletest/samples/sample8_unittest.cc", - ], - linkstatic = 0, - deps = [ - "gtest_sample_lib", - ":gtest_main", - ], -) - -cc_test( - name = "sample9_unittest", - size = "small", - srcs = ["googletest/samples/sample9_unittest.cc"], - deps = [":gtest"], -) - -cc_test( - name = "sample10_unittest", - size = "small", - srcs = ["googletest/samples/sample10_unittest.cc"], - deps = [":gtest"], -) diff --git a/vendor/googletest/CMakeLists.txt b/vendor/googletest/CMakeLists.txt index ea81ab129..089ac987f 100644 --- a/vendor/googletest/CMakeLists.txt +++ b/vendor/googletest/CMakeLists.txt @@ -1,19 +1,13 @@ # Note: CMake support is community-based. The maintainers do not use CMake # internally. -cmake_minimum_required(VERSION 2.8.12) - -if (POLICY CMP0048) - cmake_policy(SET CMP0048 NEW) -endif (POLICY CMP0048) +cmake_minimum_required(VERSION 3.13) project(googletest-distribution) -set(GOOGLETEST_VERSION 1.11.0) +set(GOOGLETEST_VERSION 1.14.0) -if (CMAKE_VERSION VERSION_GREATER "3.0.2") - if(NOT CYGWIN AND NOT MSYS AND NOT ${CMAKE_SYSTEM_NAME} STREQUAL QNX) - set(CMAKE_CXX_EXTENSIONS OFF) - endif() +if(NOT CYGWIN AND NOT MSYS AND NOT ${CMAKE_SYSTEM_NAME} STREQUAL QNX) + set(CMAKE_CXX_EXTENSIONS OFF) endif() enable_testing() @@ -24,6 +18,7 @@ include(GNUInstallDirs) #Note that googlemock target already builds googletest option(BUILD_GMOCK "Builds the googlemock subproject" ON) option(INSTALL_GTEST "Enable installation of googletest. (Projects embedding googletest may want to turn this OFF.)" ON) +option(GTEST_HAS_ABSL "Use Abseil and RE2. Requires Abseil and RE2 to be separately added to the build." OFF) if(BUILD_GMOCK) add_subdirectory( googlemock ) diff --git a/vendor/googletest/CONTRIBUTING.md b/vendor/googletest/CONTRIBUTING.md deleted file mode 100644 index da45e4450..000000000 --- a/vendor/googletest/CONTRIBUTING.md +++ /dev/null @@ -1,130 +0,0 @@ -# How to become a contributor and submit your own code - -## Contributor License Agreements - -We'd love to accept your patches! Before we can take them, we have to jump a -couple of legal hurdles. - -Please fill out either the individual or corporate Contributor License Agreement -(CLA). - -* If you are an individual writing original source code and you're sure you - own the intellectual property, then you'll need to sign an - [individual CLA](https://developers.google.com/open-source/cla/individual). -* If you work for a company that wants to allow you to contribute your work, - then you'll need to sign a - [corporate CLA](https://developers.google.com/open-source/cla/corporate). - -Follow either of the two links above to access the appropriate CLA and -instructions for how to sign and return it. Once we receive it, we'll be able to -accept your pull requests. - -## Are you a Googler? - -If you are a Googler, please make an attempt to submit an internal change rather -than a GitHub Pull Request. If you are not able to submit an internal change a -PR is acceptable as an alternative. - -## Contributing A Patch - -1. Submit an issue describing your proposed change to the - [issue tracker](https://github.com/google/googletest/issues). -2. Please don't mix more than one logical change per submittal, because it - makes the history hard to follow. If you want to make a change that doesn't - have a corresponding issue in the issue tracker, please create one. -3. Also, coordinate with team members that are listed on the issue in question. - This ensures that work isn't being duplicated and communicating your plan - early also generally leads to better patches. -4. If your proposed change is accepted, and you haven't already done so, sign a - Contributor License Agreement (see details above). -5. Fork the desired repo, develop and test your code changes. -6. Ensure that your code adheres to the existing style in the sample to which - you are contributing. -7. Ensure that your code has an appropriate set of unit tests which all pass. -8. Submit a pull request. - -## The Google Test and Google Mock Communities - -The Google Test community exists primarily through the -[discussion group](http://groups.google.com/group/googletestframework) and the -GitHub repository. Likewise, the Google Mock community exists primarily through -their own [discussion group](http://groups.google.com/group/googlemock). You are -definitely encouraged to contribute to the discussion and you can also help us -to keep the effectiveness of the group high by following and promoting the -guidelines listed here. - -### Please Be Friendly - -Showing courtesy and respect to others is a vital part of the Google culture, -and we strongly encourage everyone participating in Google Test development to -join us in accepting nothing less. Of course, being courteous is not the same as -failing to constructively disagree with each other, but it does mean that we -should be respectful of each other when enumerating the 42 technical reasons -that a particular proposal may not be the best choice. There's never a reason to -be antagonistic or dismissive toward anyone who is sincerely trying to -contribute to a discussion. - -Sure, C++ testing is serious business and all that, but it's also a lot of fun. -Let's keep it that way. Let's strive to be one of the friendliest communities in -all of open source. - -As always, discuss Google Test in the official GoogleTest discussion group. You -don't have to actually submit code in order to sign up. Your participation -itself is a valuable contribution. - -## Style - -To keep the source consistent, readable, diffable and easy to merge, we use a -fairly rigid coding style, as defined by the -[google-styleguide](https://github.com/google/styleguide) project. All patches -will be expected to conform to the style outlined -[here](https://google.github.io/styleguide/cppguide.html). Use -[.clang-format](https://github.com/google/googletest/blob/master/.clang-format) -to check your formatting. - -## Requirements for Contributors - -If you plan to contribute a patch, you need to build Google Test, Google Mock, -and their own tests from a git checkout, which has further requirements: - -* [Python](https://www.python.org/) v2.3 or newer (for running some of the - tests and re-generating certain source files from templates) -* [CMake](https://cmake.org/) v2.8.12 or newer - -## Developing Google Test and Google Mock - -This section discusses how to make your own changes to the Google Test project. - -### Testing Google Test and Google Mock Themselves - -To make sure your changes work as intended and don't break existing -functionality, you'll want to compile and run Google Test and GoogleMock's own -tests. For that you can use CMake: - - mkdir mybuild - cd mybuild - cmake -Dgtest_build_tests=ON -Dgmock_build_tests=ON ${GTEST_REPO_DIR} - -To choose between building only Google Test or Google Mock, you may modify your -cmake command to be one of each - - cmake -Dgtest_build_tests=ON ${GTEST_DIR} # sets up Google Test tests - cmake -Dgmock_build_tests=ON ${GMOCK_DIR} # sets up Google Mock tests - -Make sure you have Python installed, as some of Google Test's tests are written -in Python. If the cmake command complains about not being able to find Python -(`Could NOT find PythonInterp (missing: PYTHON_EXECUTABLE)`), try telling it -explicitly where your Python executable can be found: - - cmake -DPYTHON_EXECUTABLE=path/to/python ... - -Next, you can build Google Test and / or Google Mock and all desired tests. On -\*nix, this is usually done by - - make - -To run the tests, do - - make test - -All tests should pass. diff --git a/vendor/googletest/CONTRIBUTORS b/vendor/googletest/CONTRIBUTORS deleted file mode 100644 index 76db0b40f..000000000 --- a/vendor/googletest/CONTRIBUTORS +++ /dev/null @@ -1,63 +0,0 @@ -# This file contains a list of people who've made non-trivial -# contribution to the Google C++ Testing Framework project. People -# who commit code to the project are encouraged to add their names -# here. Please keep the list sorted by first names. - -Ajay Joshi -Balázs Dán -Benoit Sigoure -Bharat Mediratta -Bogdan Piloca -Chandler Carruth -Chris Prince -Chris Taylor -Dan Egnor -Dave MacLachlan -David Anderson -Dean Sturtevant -Eric Roman -Gene Volovich -Hady Zalek -Hal Burch -Jeffrey Yasskin -Jim Keller -Joe Walnes -Jon Wray -Jói Sigurðsson -Keir Mierle -Keith Ray -Kenton Varda -Kostya Serebryany -Krystian Kuzniarek -Lev Makhlis -Manuel Klimek -Mario Tanev -Mark Paskin -Markus Heule -Matthew Simmons -Mika Raento -Mike Bland -Miklós Fazekas -Neal Norwitz -Nermin Ozkiranartli -Owen Carlsen -Paneendra Ba -Pasi Valminen -Patrick Hanna -Patrick Riley -Paul Menage -Peter Kaminski -Piotr Kaminski -Preston Jackson -Rainer Klaffenboeck -Russ Cox -Russ Rufer -Sean Mcafee -Sigurður Ásgeirsson -Sverre Sundsdal -Takeshi Yoshino -Tracy Bialik -Vadim Berman -Vlad Losev -Wolfgang Klier -Zhanyong Wan diff --git a/vendor/googletest/README.md b/vendor/googletest/README.md deleted file mode 100644 index 7d872a57e..000000000 --- a/vendor/googletest/README.md +++ /dev/null @@ -1,140 +0,0 @@ -# GoogleTest - -### Announcements - -#### Live at Head - -GoogleTest now follows the -[Abseil Live at Head philosophy](https://abseil.io/about/philosophy#upgrade-support). -We recommend using the latest commit in the `master` branch in your projects. - -#### Documentation Updates - -Our documentation is now live on GitHub Pages at -https://google.github.io/googletest/. We recommend browsing the documentation on -GitHub Pages rather than directly in the repository. - -#### Release 1.10.x - -[Release 1.10.x](https://github.com/google/googletest/releases/tag/release-1.10.0) -is now available. - -#### Coming Soon - -* We are planning to take a dependency on - [Abseil](https://github.com/abseil/abseil-cpp). -* More documentation improvements are planned. - -## Welcome to **GoogleTest**, Google's C++ test framework! - -This repository is a merger of the formerly separate GoogleTest and GoogleMock -projects. These were so closely related that it makes sense to maintain and -release them together. - -### Getting Started - -See the [GoogleTest User's Guide](https://google.github.io/googletest/) for -documentation. We recommend starting with the -[GoogleTest Primer](https://google.github.io/googletest/primer.html). - -More information about building GoogleTest can be found at -[googletest/README.md](googletest/README.md). - -## Features - -* An [xUnit](https://en.wikipedia.org/wiki/XUnit) test framework. -* Test discovery. -* A rich set of assertions. -* User-defined assertions. -* Death tests. -* Fatal and non-fatal failures. -* Value-parameterized tests. -* Type-parameterized tests. -* Various options for running the tests. -* XML test report generation. - -## Supported Platforms - -GoogleTest requires a codebase and compiler compliant with the C++11 standard or -newer. - -The GoogleTest code is officially supported on the following platforms. -Operating systems or tools not listed below are community-supported. For -community-supported platforms, patches that do not complicate the code may be -considered. - -If you notice any problems on your platform, please file an issue on the -[GoogleTest GitHub Issue Tracker](https://github.com/google/googletest/issues). -Pull requests containing fixes are welcome! - -### Operating Systems - -* Linux -* macOS -* Windows - -### Compilers - -* gcc 5.0+ -* clang 5.0+ -* MSVC 2015+ - -**macOS users:** Xcode 9.3+ provides clang 5.0+. - -### Build Systems - -* [Bazel](https://bazel.build/) -* [CMake](https://cmake.org/) - -**Note:** Bazel is the build system used by the team internally and in tests. -CMake is supported on a best-effort basis and by the community. - -## Who Is Using GoogleTest? - -In addition to many internal projects at Google, GoogleTest is also used by the -following notable projects: - -* The [Chromium projects](http://www.chromium.org/) (behind the Chrome browser - and Chrome OS). -* The [LLVM](http://llvm.org/) compiler. -* [Protocol Buffers](https://github.com/google/protobuf), Google's data - interchange format. -* The [OpenCV](http://opencv.org/) computer vision library. - -## Related Open Source Projects - -[GTest Runner](https://github.com/nholthaus/gtest-runner) is a Qt5 based -automated test-runner and Graphical User Interface with powerful features for -Windows and Linux platforms. - -[GoogleTest UI](https://github.com/ospector/gtest-gbar) is a test runner that -runs your test binary, allows you to track its progress via a progress bar, and -displays a list of test failures. Clicking on one shows failure text. Google -Test UI is written in C#. - -[GTest TAP Listener](https://github.com/kinow/gtest-tap-listener) is an event -listener for GoogleTest that implements the -[TAP protocol](https://en.wikipedia.org/wiki/Test_Anything_Protocol) for test -result output. If your test runner understands TAP, you may find it useful. - -[gtest-parallel](https://github.com/google/gtest-parallel) is a test runner that -runs tests from your binary in parallel to provide significant speed-up. - -[GoogleTest Adapter](https://marketplace.visualstudio.com/items?itemName=DavidSchuldenfrei.gtest-adapter) -is a VS Code extension allowing to view GoogleTest in a tree view, and run/debug -your tests. - -[C++ TestMate](https://github.com/matepek/vscode-catch2-test-adapter) is a VS -Code extension allowing to view GoogleTest in a tree view, and run/debug your -tests. - -[Cornichon](https://pypi.org/project/cornichon/) is a small Gherkin DSL parser -that generates stub code for GoogleTest. - -## Contributing Changes - -Please read -[`CONTRIBUTING.md`](https://github.com/google/googletest/blob/master/CONTRIBUTING.md) -for details on how to contribute to this project. - -Happy testing! diff --git a/vendor/googletest/WORKSPACE b/vendor/googletest/WORKSPACE deleted file mode 100644 index 614f55778..000000000 --- a/vendor/googletest/WORKSPACE +++ /dev/null @@ -1,24 +0,0 @@ -workspace(name = "com_google_googletest") - -load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive") - -http_archive( - name = "com_google_absl", - urls = ["https://github.com/abseil/abseil-cpp/archive/7971fb358ae376e016d2d4fc9327aad95659b25e.zip"], # 2021-05-20T02:59:16Z - strip_prefix = "abseil-cpp-7971fb358ae376e016d2d4fc9327aad95659b25e", - sha256 = "aeba534f7307e36fe084b452299e49b97420667a8d28102cf9a0daeed340b859", -) - -http_archive( - name = "rules_cc", - urls = ["https://github.com/bazelbuild/rules_cc/archive/68cb652a71e7e7e2858c50593e5a9e3b94e5b9a9.zip"], # 2021-05-14T14:51:14Z - strip_prefix = "rules_cc-68cb652a71e7e7e2858c50593e5a9e3b94e5b9a9", - sha256 = "1e19e9a3bc3d4ee91d7fcad00653485ee6c798efbbf9588d40b34cbfbded143d", -) - -http_archive( - name = "rules_python", - urls = ["https://github.com/bazelbuild/rules_python/archive/ed6cc8f2c3692a6a7f013ff8bc185ba77eb9b4d2.zip"], # 2021-05-17T00:24:16Z - strip_prefix = "rules_python-ed6cc8f2c3692a6a7f013ff8bc185ba77eb9b4d2", - sha256 = "98b3c592faea9636ac8444bfd9de7f3fb4c60590932d6e6ac5946e3f8dbd5ff6", -) diff --git a/vendor/googletest/googlemock/CMakeLists.txt b/vendor/googletest/googlemock/CMakeLists.txt deleted file mode 100644 index e7df8ec53..000000000 --- a/vendor/googletest/googlemock/CMakeLists.txt +++ /dev/null @@ -1,218 +0,0 @@ -######################################################################## -# Note: CMake support is community-based. The maintainers do not use CMake -# internally. -# -# CMake build script for Google Mock. -# -# To run the tests for Google Mock itself on Linux, use 'make test' or -# ctest. You can select which tests to run using 'ctest -R regex'. -# For more options, run 'ctest --help'. - -option(gmock_build_tests "Build all of Google Mock's own tests." OFF) - -# A directory to find Google Test sources. -if (EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/gtest/CMakeLists.txt") - set(gtest_dir gtest) -else() - set(gtest_dir ../googletest) -endif() - -# Defines pre_project_set_up_hermetic_build() and set_up_hermetic_build(). -include("${gtest_dir}/cmake/hermetic_build.cmake" OPTIONAL) - -if (COMMAND pre_project_set_up_hermetic_build) - # Google Test also calls hermetic setup functions from add_subdirectory, - # although its changes will not affect things at the current scope. - pre_project_set_up_hermetic_build() -endif() - -######################################################################## -# -# Project-wide settings - -# Name of the project. -# -# CMake files in this project can refer to the root source directory -# as ${gmock_SOURCE_DIR} and to the root binary directory as -# ${gmock_BINARY_DIR}. -# Language "C" is required for find_package(Threads). -if (CMAKE_VERSION VERSION_LESS 3.0) - project(gmock CXX C) -else() - cmake_policy(SET CMP0048 NEW) - project(gmock VERSION ${GOOGLETEST_VERSION} LANGUAGES CXX C) -endif() -cmake_minimum_required(VERSION 2.8.12) - -if (COMMAND set_up_hermetic_build) - set_up_hermetic_build() -endif() - -# Instructs CMake to process Google Test's CMakeLists.txt and add its -# targets to the current scope. We are placing Google Test's binary -# directory in a subdirectory of our own as VC compilation may break -# if they are the same (the default). -add_subdirectory("${gtest_dir}" "${gmock_BINARY_DIR}/${gtest_dir}") - - -# These commands only run if this is the main project -if(CMAKE_PROJECT_NAME STREQUAL "gmock" OR CMAKE_PROJECT_NAME STREQUAL "googletest-distribution") - # BUILD_SHARED_LIBS is a standard CMake variable, but we declare it here to - # make it prominent in the GUI. - option(BUILD_SHARED_LIBS "Build shared libraries (DLLs)." OFF) -else() - mark_as_advanced(gmock_build_tests) -endif() - -# Although Google Test's CMakeLists.txt calls this function, the -# changes there don't affect the current scope. Therefore we have to -# call it again here. -config_compiler_and_linker() # from ${gtest_dir}/cmake/internal_utils.cmake - -# Adds Google Mock's and Google Test's header directories to the search path. -set(gmock_build_include_dirs - "${gmock_SOURCE_DIR}/include" - "${gmock_SOURCE_DIR}" - "${gtest_SOURCE_DIR}/include" - # This directory is needed to build directly from Google Test sources. - "${gtest_SOURCE_DIR}") -include_directories(${gmock_build_include_dirs}) - -######################################################################## -# -# Defines the gmock & gmock_main libraries. User tests should link -# with one of them. - -# Google Mock libraries. We build them using more strict warnings than what -# are used for other targets, to ensure that Google Mock can be compiled by -# a user aggressive about warnings. -if (MSVC) - cxx_library(gmock - "${cxx_strict}" - "${gtest_dir}/src/gtest-all.cc" - src/gmock-all.cc) - - cxx_library(gmock_main - "${cxx_strict}" - "${gtest_dir}/src/gtest-all.cc" - src/gmock-all.cc - src/gmock_main.cc) -else() - cxx_library(gmock "${cxx_strict}" src/gmock-all.cc) - target_link_libraries(gmock PUBLIC gtest) - set_target_properties(gmock PROPERTIES VERSION ${GOOGLETEST_VERSION}) - cxx_library(gmock_main "${cxx_strict}" src/gmock_main.cc) - target_link_libraries(gmock_main PUBLIC gmock) - set_target_properties(gmock_main PROPERTIES VERSION ${GOOGLETEST_VERSION}) -endif() -# If the CMake version supports it, attach header directory information -# to the targets for when we are part of a parent build (ie being pulled -# in via add_subdirectory() rather than being a standalone build). -if (DEFINED CMAKE_VERSION AND NOT "${CMAKE_VERSION}" VERSION_LESS "2.8.11") - target_include_directories(gmock SYSTEM INTERFACE - "$" - "$/${CMAKE_INSTALL_INCLUDEDIR}>") - target_include_directories(gmock_main SYSTEM INTERFACE - "$" - "$/${CMAKE_INSTALL_INCLUDEDIR}>") -endif() - -######################################################################## -# -# Install rules -install_project(gmock gmock_main) - -######################################################################## -# -# Google Mock's own tests. -# -# You can skip this section if you aren't interested in testing -# Google Mock itself. -# -# The tests are not built by default. To build them, set the -# gmock_build_tests option to ON. You can do it by running ccmake -# or specifying the -Dgmock_build_tests=ON flag when running cmake. - -if (gmock_build_tests) - # This must be set in the root directory for the tests to be run by - # 'make test' or ctest. - enable_testing() - - if (MINGW OR CYGWIN) - if (CMAKE_VERSION VERSION_LESS "2.8.12") - add_compile_options("-Wa,-mbig-obj") - else() - add_definitions("-Wa,-mbig-obj") - endif() - endif() - - ############################################################ - # C++ tests built with standard compiler flags. - - cxx_test(gmock-actions_test gmock_main) - cxx_test(gmock-cardinalities_test gmock_main) - cxx_test(gmock_ex_test gmock_main) - cxx_test(gmock-function-mocker_test gmock_main) - cxx_test(gmock-internal-utils_test gmock_main) - cxx_test(gmock-matchers_test gmock_main) - cxx_test(gmock-more-actions_test gmock_main) - cxx_test(gmock-nice-strict_test gmock_main) - cxx_test(gmock-port_test gmock_main) - cxx_test(gmock-spec-builders_test gmock_main) - cxx_test(gmock_link_test gmock_main test/gmock_link2_test.cc) - cxx_test(gmock_test gmock_main) - - if (DEFINED GTEST_HAS_PTHREAD) - cxx_test(gmock_stress_test gmock) - endif() - - # gmock_all_test is commented to save time building and running tests. - # Uncomment if necessary. - # cxx_test(gmock_all_test gmock_main) - - ############################################################ - # C++ tests built with non-standard compiler flags. - - if (MSVC) - cxx_library(gmock_main_no_exception "${cxx_no_exception}" - "${gtest_dir}/src/gtest-all.cc" src/gmock-all.cc src/gmock_main.cc) - - cxx_library(gmock_main_no_rtti "${cxx_no_rtti}" - "${gtest_dir}/src/gtest-all.cc" src/gmock-all.cc src/gmock_main.cc) - - else() - cxx_library(gmock_main_no_exception "${cxx_no_exception}" src/gmock_main.cc) - target_link_libraries(gmock_main_no_exception PUBLIC gmock) - - cxx_library(gmock_main_no_rtti "${cxx_no_rtti}" src/gmock_main.cc) - target_link_libraries(gmock_main_no_rtti PUBLIC gmock) - endif() - cxx_test_with_flags(gmock-more-actions_no_exception_test "${cxx_no_exception}" - gmock_main_no_exception test/gmock-more-actions_test.cc) - - cxx_test_with_flags(gmock_no_rtti_test "${cxx_no_rtti}" - gmock_main_no_rtti test/gmock-spec-builders_test.cc) - - cxx_shared_library(shared_gmock_main "${cxx_default}" - "${gtest_dir}/src/gtest-all.cc" src/gmock-all.cc src/gmock_main.cc) - - # Tests that a binary can be built with Google Mock as a shared library. On - # some system configurations, it may not possible to run the binary without - # knowing more details about the system configurations. We do not try to run - # this binary. To get a more robust shared library coverage, configure with - # -DBUILD_SHARED_LIBS=ON. - cxx_executable_with_flags(shared_gmock_test_ "${cxx_default}" - shared_gmock_main test/gmock-spec-builders_test.cc) - set_target_properties(shared_gmock_test_ - PROPERTIES - COMPILE_DEFINITIONS "GTEST_LINKED_AS_SHARED_LIBRARY=1") - - ############################################################ - # Python tests. - - cxx_executable(gmock_leak_test_ test gmock_main) - py_test(gmock_leak_test) - - cxx_executable(gmock_output_test_ test gmock) - py_test(gmock_output_test) -endif() diff --git a/vendor/googletest/googlemock/README.md b/vendor/googletest/googlemock/README.md deleted file mode 100644 index ead688325..000000000 --- a/vendor/googletest/googlemock/README.md +++ /dev/null @@ -1,44 +0,0 @@ -# Googletest Mocking (gMock) Framework - -### Overview - -Google's framework for writing and using C++ mock classes. It can help you -derive better designs of your system and write better tests. - -It is inspired by: - -* [jMock](http://www.jmock.org/) -* [EasyMock](http://www.easymock.org/) -* [Hamcrest](http://code.google.com/p/hamcrest/) - -It is designed with C++'s specifics in mind. - -gMock: - -- Provides a declarative syntax for defining mocks. -- Can define partial (hybrid) mocks, which are a cross of real and mock - objects. -- Handles functions of arbitrary types and overloaded functions. -- Comes with a rich set of matchers for validating function arguments. -- Uses an intuitive syntax for controlling the behavior of a mock. -- Does automatic verification of expectations (no record-and-replay needed). -- Allows arbitrary (partial) ordering constraints on function calls to be - expressed. -- Lets a user extend it by defining new matchers and actions. -- Does not use exceptions. -- Is easy to learn and use. - -Details and examples can be found here: - -* [gMock for Dummies](https://google.github.io/googletest/gmock_for_dummies.html) -* [Legacy gMock FAQ](https://google.github.io/googletest/gmock_faq.html) -* [gMock Cookbook](https://google.github.io/googletest/gmock_cook_book.html) -* [gMock Cheat Sheet](https://google.github.io/googletest/gmock_cheat_sheet.html) - -Please note that code under scripts/generator/ is from the -[cppclean project](http://code.google.com/p/cppclean/) and under the Apache -License, which is different from GoogleMock's license. - -GoogleMock is a part of -[GoogleTest C++ testing framework](http://github.com/google/googletest/) and a -subject to the same requirements. diff --git a/vendor/googletest/googlemock/cmake/gmock.pc.in b/vendor/googletest/googlemock/cmake/gmock.pc.in deleted file mode 100644 index 23c67b5c8..000000000 --- a/vendor/googletest/googlemock/cmake/gmock.pc.in +++ /dev/null @@ -1,10 +0,0 @@ -libdir=@CMAKE_INSTALL_FULL_LIBDIR@ -includedir=@CMAKE_INSTALL_FULL_INCLUDEDIR@ - -Name: gmock -Description: GoogleMock (without main() function) -Version: @PROJECT_VERSION@ -URL: https://github.com/google/googletest -Requires: gtest = @PROJECT_VERSION@ -Libs: -L${libdir} -lgmock @CMAKE_THREAD_LIBS_INIT@ -Cflags: -I${includedir} @GTEST_HAS_PTHREAD_MACRO@ diff --git a/vendor/googletest/googlemock/cmake/gmock_main.pc.in b/vendor/googletest/googlemock/cmake/gmock_main.pc.in deleted file mode 100644 index 66ffea7f4..000000000 --- a/vendor/googletest/googlemock/cmake/gmock_main.pc.in +++ /dev/null @@ -1,10 +0,0 @@ -libdir=@CMAKE_INSTALL_FULL_LIBDIR@ -includedir=@CMAKE_INSTALL_FULL_INCLUDEDIR@ - -Name: gmock_main -Description: GoogleMock (with main() function) -Version: @PROJECT_VERSION@ -URL: https://github.com/google/googletest -Requires: gmock = @PROJECT_VERSION@ -Libs: -L${libdir} -lgmock_main @CMAKE_THREAD_LIBS_INIT@ -Cflags: -I${includedir} @GTEST_HAS_PTHREAD_MACRO@ diff --git a/vendor/googletest/googlemock/docs/README.md b/vendor/googletest/googlemock/docs/README.md deleted file mode 100644 index 1bc57b799..000000000 --- a/vendor/googletest/googlemock/docs/README.md +++ /dev/null @@ -1,4 +0,0 @@ -# Content Moved - -We are working on updates to the GoogleTest documentation, which has moved to -the top-level [docs](../../docs) directory. diff --git a/vendor/googletest/googlemock/include/gmock/gmock-actions.h b/vendor/googletest/googlemock/include/gmock/gmock-actions.h deleted file mode 100644 index f2393bd3a..000000000 --- a/vendor/googletest/googlemock/include/gmock/gmock-actions.h +++ /dev/null @@ -1,1687 +0,0 @@ -// Copyright 2007, Google Inc. -// All rights reserved. -// -// Redistribution and use in source and binary forms, with or without -// modification, are permitted provided that the following conditions are -// met: -// -// * Redistributions of source code must retain the above copyright -// notice, this list of conditions and the following disclaimer. -// * Redistributions in binary form must reproduce the above -// copyright notice, this list of conditions and the following disclaimer -// in the documentation and/or other materials provided with the -// distribution. -// * Neither the name of Google Inc. nor the names of its -// contributors may be used to endorse or promote products derived from -// this software without specific prior written permission. -// -// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - - -// Google Mock - a framework for writing C++ mock classes. -// -// The ACTION* family of macros can be used in a namespace scope to -// define custom actions easily. The syntax: -// -// ACTION(name) { statements; } -// -// will define an action with the given name that executes the -// statements. The value returned by the statements will be used as -// the return value of the action. Inside the statements, you can -// refer to the K-th (0-based) argument of the mock function by -// 'argK', and refer to its type by 'argK_type'. For example: -// -// ACTION(IncrementArg1) { -// arg1_type temp = arg1; -// return ++(*temp); -// } -// -// allows you to write -// -// ...WillOnce(IncrementArg1()); -// -// You can also refer to the entire argument tuple and its type by -// 'args' and 'args_type', and refer to the mock function type and its -// return type by 'function_type' and 'return_type'. -// -// Note that you don't need to specify the types of the mock function -// arguments. However rest assured that your code is still type-safe: -// you'll get a compiler error if *arg1 doesn't support the ++ -// operator, or if the type of ++(*arg1) isn't compatible with the -// mock function's return type, for example. -// -// Sometimes you'll want to parameterize the action. For that you can use -// another macro: -// -// ACTION_P(name, param_name) { statements; } -// -// For example: -// -// ACTION_P(Add, n) { return arg0 + n; } -// -// will allow you to write: -// -// ...WillOnce(Add(5)); -// -// Note that you don't need to provide the type of the parameter -// either. If you need to reference the type of a parameter named -// 'foo', you can write 'foo_type'. For example, in the body of -// ACTION_P(Add, n) above, you can write 'n_type' to refer to the type -// of 'n'. -// -// We also provide ACTION_P2, ACTION_P3, ..., up to ACTION_P10 to support -// multi-parameter actions. -// -// For the purpose of typing, you can view -// -// ACTION_Pk(Foo, p1, ..., pk) { ... } -// -// as shorthand for -// -// template -// FooActionPk Foo(p1_type p1, ..., pk_type pk) { ... } -// -// In particular, you can provide the template type arguments -// explicitly when invoking Foo(), as in Foo(5, false); -// although usually you can rely on the compiler to infer the types -// for you automatically. You can assign the result of expression -// Foo(p1, ..., pk) to a variable of type FooActionPk. This can be useful when composing actions. -// -// You can also overload actions with different numbers of parameters: -// -// ACTION_P(Plus, a) { ... } -// ACTION_P2(Plus, a, b) { ... } -// -// While it's tempting to always use the ACTION* macros when defining -// a new action, you should also consider implementing ActionInterface -// or using MakePolymorphicAction() instead, especially if you need to -// use the action a lot. While these approaches require more work, -// they give you more control on the types of the mock function -// arguments and the action parameters, which in general leads to -// better compiler error messages that pay off in the long run. They -// also allow overloading actions based on parameter types (as opposed -// to just based on the number of parameters). -// -// CAVEAT: -// -// ACTION*() can only be used in a namespace scope as templates cannot be -// declared inside of a local class. -// Users can, however, define any local functors (e.g. a lambda) that -// can be used as actions. -// -// MORE INFORMATION: -// -// To learn more about using these macros, please search for 'ACTION' on -// https://github.com/google/googletest/blob/master/docs/gmock_cook_book.md - -// GOOGLETEST_CM0002 DO NOT DELETE - -#ifndef GOOGLEMOCK_INCLUDE_GMOCK_GMOCK_ACTIONS_H_ -#define GOOGLEMOCK_INCLUDE_GMOCK_GMOCK_ACTIONS_H_ - -#ifndef _WIN32_WCE -# include -#endif - -#include -#include -#include -#include -#include -#include -#include - -#include "gmock/internal/gmock-internal-utils.h" -#include "gmock/internal/gmock-port.h" -#include "gmock/internal/gmock-pp.h" - -#ifdef _MSC_VER -# pragma warning(push) -# pragma warning(disable:4100) -#endif - -namespace testing { - -// To implement an action Foo, define: -// 1. a class FooAction that implements the ActionInterface interface, and -// 2. a factory function that creates an Action object from a -// const FooAction*. -// -// The two-level delegation design follows that of Matcher, providing -// consistency for extension developers. It also eases ownership -// management as Action objects can now be copied like plain values. - -namespace internal { - -// BuiltInDefaultValueGetter::Get() returns a -// default-constructed T value. BuiltInDefaultValueGetter::Get() crashes with an error. -// -// This primary template is used when kDefaultConstructible is true. -template -struct BuiltInDefaultValueGetter { - static T Get() { return T(); } -}; -template -struct BuiltInDefaultValueGetter { - static T Get() { - Assert(false, __FILE__, __LINE__, - "Default action undefined for the function return type."); - return internal::Invalid(); - // The above statement will never be reached, but is required in - // order for this function to compile. - } -}; - -// BuiltInDefaultValue::Get() returns the "built-in" default value -// for type T, which is NULL when T is a raw pointer type, 0 when T is -// a numeric type, false when T is bool, or "" when T is string or -// std::string. In addition, in C++11 and above, it turns a -// default-constructed T value if T is default constructible. For any -// other type T, the built-in default T value is undefined, and the -// function will abort the process. -template -class BuiltInDefaultValue { - public: - // This function returns true if and only if type T has a built-in default - // value. - static bool Exists() { - return ::std::is_default_constructible::value; - } - - static T Get() { - return BuiltInDefaultValueGetter< - T, ::std::is_default_constructible::value>::Get(); - } -}; - -// This partial specialization says that we use the same built-in -// default value for T and const T. -template -class BuiltInDefaultValue { - public: - static bool Exists() { return BuiltInDefaultValue::Exists(); } - static T Get() { return BuiltInDefaultValue::Get(); } -}; - -// This partial specialization defines the default values for pointer -// types. -template -class BuiltInDefaultValue { - public: - static bool Exists() { return true; } - static T* Get() { return nullptr; } -}; - -// The following specializations define the default values for -// specific types we care about. -#define GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_(type, value) \ - template <> \ - class BuiltInDefaultValue { \ - public: \ - static bool Exists() { return true; } \ - static type Get() { return value; } \ - } - -GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_(void, ); // NOLINT -GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_(::std::string, ""); -GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_(bool, false); -GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_(unsigned char, '\0'); -GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_(signed char, '\0'); -GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_(char, '\0'); - -// There's no need for a default action for signed wchar_t, as that -// type is the same as wchar_t for gcc, and invalid for MSVC. -// -// There's also no need for a default action for unsigned wchar_t, as -// that type is the same as unsigned int for gcc, and invalid for -// MSVC. -#if GMOCK_WCHAR_T_IS_NATIVE_ -GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_(wchar_t, 0U); // NOLINT -#endif - -GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_(unsigned short, 0U); // NOLINT -GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_(signed short, 0); // NOLINT -GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_(unsigned int, 0U); -GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_(signed int, 0); -GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_(unsigned long, 0UL); // NOLINT -GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_(signed long, 0L); // NOLINT -GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_(unsigned long long, 0); // NOLINT -GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_(signed long long, 0); // NOLINT -GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_(float, 0); -GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_(double, 0); - -#undef GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_ - -// Simple two-arg form of std::disjunction. -template -using disjunction = typename ::std::conditional::type; - -} // namespace internal - -// When an unexpected function call is encountered, Google Mock will -// let it return a default value if the user has specified one for its -// return type, or if the return type has a built-in default value; -// otherwise Google Mock won't know what value to return and will have -// to abort the process. -// -// The DefaultValue class allows a user to specify the -// default value for a type T that is both copyable and publicly -// destructible (i.e. anything that can be used as a function return -// type). The usage is: -// -// // Sets the default value for type T to be foo. -// DefaultValue::Set(foo); -template -class DefaultValue { - public: - // Sets the default value for type T; requires T to be - // copy-constructable and have a public destructor. - static void Set(T x) { - delete producer_; - producer_ = new FixedValueProducer(x); - } - - // Provides a factory function to be called to generate the default value. - // This method can be used even if T is only move-constructible, but it is not - // limited to that case. - typedef T (*FactoryFunction)(); - static void SetFactory(FactoryFunction factory) { - delete producer_; - producer_ = new FactoryValueProducer(factory); - } - - // Unsets the default value for type T. - static void Clear() { - delete producer_; - producer_ = nullptr; - } - - // Returns true if and only if the user has set the default value for type T. - static bool IsSet() { return producer_ != nullptr; } - - // Returns true if T has a default return value set by the user or there - // exists a built-in default value. - static bool Exists() { - return IsSet() || internal::BuiltInDefaultValue::Exists(); - } - - // Returns the default value for type T if the user has set one; - // otherwise returns the built-in default value. Requires that Exists() - // is true, which ensures that the return value is well-defined. - static T Get() { - return producer_ == nullptr ? internal::BuiltInDefaultValue::Get() - : producer_->Produce(); - } - - private: - class ValueProducer { - public: - virtual ~ValueProducer() {} - virtual T Produce() = 0; - }; - - class FixedValueProducer : public ValueProducer { - public: - explicit FixedValueProducer(T value) : value_(value) {} - T Produce() override { return value_; } - - private: - const T value_; - GTEST_DISALLOW_COPY_AND_ASSIGN_(FixedValueProducer); - }; - - class FactoryValueProducer : public ValueProducer { - public: - explicit FactoryValueProducer(FactoryFunction factory) - : factory_(factory) {} - T Produce() override { return factory_(); } - - private: - const FactoryFunction factory_; - GTEST_DISALLOW_COPY_AND_ASSIGN_(FactoryValueProducer); - }; - - static ValueProducer* producer_; -}; - -// This partial specialization allows a user to set default values for -// reference types. -template -class DefaultValue { - public: - // Sets the default value for type T&. - static void Set(T& x) { // NOLINT - address_ = &x; - } - - // Unsets the default value for type T&. - static void Clear() { address_ = nullptr; } - - // Returns true if and only if the user has set the default value for type T&. - static bool IsSet() { return address_ != nullptr; } - - // Returns true if T has a default return value set by the user or there - // exists a built-in default value. - static bool Exists() { - return IsSet() || internal::BuiltInDefaultValue::Exists(); - } - - // Returns the default value for type T& if the user has set one; - // otherwise returns the built-in default value if there is one; - // otherwise aborts the process. - static T& Get() { - return address_ == nullptr ? internal::BuiltInDefaultValue::Get() - : *address_; - } - - private: - static T* address_; -}; - -// This specialization allows DefaultValue::Get() to -// compile. -template <> -class DefaultValue { - public: - static bool Exists() { return true; } - static void Get() {} -}; - -// Points to the user-set default value for type T. -template -typename DefaultValue::ValueProducer* DefaultValue::producer_ = nullptr; - -// Points to the user-set default value for type T&. -template -T* DefaultValue::address_ = nullptr; - -// Implement this interface to define an action for function type F. -template -class ActionInterface { - public: - typedef typename internal::Function::Result Result; - typedef typename internal::Function::ArgumentTuple ArgumentTuple; - - ActionInterface() {} - virtual ~ActionInterface() {} - - // Performs the action. This method is not const, as in general an - // action can have side effects and be stateful. For example, a - // get-the-next-element-from-the-collection action will need to - // remember the current element. - virtual Result Perform(const ArgumentTuple& args) = 0; - - private: - GTEST_DISALLOW_COPY_AND_ASSIGN_(ActionInterface); -}; - -// An Action is a copyable and IMMUTABLE (except by assignment) -// object that represents an action to be taken when a mock function -// of type F is called. The implementation of Action is just a -// std::shared_ptr to const ActionInterface. Don't inherit from Action! -// You can view an object implementing ActionInterface as a -// concrete action (including its current state), and an Action -// object as a handle to it. -template -class Action { - // Adapter class to allow constructing Action from a legacy ActionInterface. - // New code should create Actions from functors instead. - struct ActionAdapter { - // Adapter must be copyable to satisfy std::function requirements. - ::std::shared_ptr> impl_; - - template - typename internal::Function::Result operator()(Args&&... args) { - return impl_->Perform( - ::std::forward_as_tuple(::std::forward(args)...)); - } - }; - - template - using IsCompatibleFunctor = std::is_constructible, G>; - - public: - typedef typename internal::Function::Result Result; - typedef typename internal::Function::ArgumentTuple ArgumentTuple; - - // Constructs a null Action. Needed for storing Action objects in - // STL containers. - Action() {} - - // Construct an Action from a specified callable. - // This cannot take std::function directly, because then Action would not be - // directly constructible from lambda (it would require two conversions). - template < - typename G, - typename = typename std::enable_if, std::is_constructible, - G>>::value>::type> - Action(G&& fun) { // NOLINT - Init(::std::forward(fun), IsCompatibleFunctor()); - } - - // Constructs an Action from its implementation. - explicit Action(ActionInterface* impl) - : fun_(ActionAdapter{::std::shared_ptr>(impl)}) {} - - // This constructor allows us to turn an Action object into an - // Action, as long as F's arguments can be implicitly converted - // to Func's and Func's return type can be implicitly converted to F's. - template - explicit Action(const Action& action) : fun_(action.fun_) {} - - // Returns true if and only if this is the DoDefault() action. - bool IsDoDefault() const { return fun_ == nullptr; } - - // Performs the action. Note that this method is const even though - // the corresponding method in ActionInterface is not. The reason - // is that a const Action means that it cannot be re-bound to - // another concrete action, not that the concrete action it binds to - // cannot change state. (Think of the difference between a const - // pointer and a pointer to const.) - Result Perform(ArgumentTuple args) const { - if (IsDoDefault()) { - internal::IllegalDoDefault(__FILE__, __LINE__); - } - return internal::Apply(fun_, ::std::move(args)); - } - - private: - template - friend class Action; - - template - void Init(G&& g, ::std::true_type) { - fun_ = ::std::forward(g); - } - - template - void Init(G&& g, ::std::false_type) { - fun_ = IgnoreArgs::type>{::std::forward(g)}; - } - - template - struct IgnoreArgs { - template - Result operator()(const Args&...) const { - return function_impl(); - } - - FunctionImpl function_impl; - }; - - // fun_ is an empty function if and only if this is the DoDefault() action. - ::std::function fun_; -}; - -// The PolymorphicAction class template makes it easy to implement a -// polymorphic action (i.e. an action that can be used in mock -// functions of than one type, e.g. Return()). -// -// To define a polymorphic action, a user first provides a COPYABLE -// implementation class that has a Perform() method template: -// -// class FooAction { -// public: -// template -// Result Perform(const ArgumentTuple& args) const { -// // Processes the arguments and returns a result, using -// // std::get(args) to get the N-th (0-based) argument in the tuple. -// } -// ... -// }; -// -// Then the user creates the polymorphic action using -// MakePolymorphicAction(object) where object has type FooAction. See -// the definition of Return(void) and SetArgumentPointee(value) for -// complete examples. -template -class PolymorphicAction { - public: - explicit PolymorphicAction(const Impl& impl) : impl_(impl) {} - - template - operator Action() const { - return Action(new MonomorphicImpl(impl_)); - } - - private: - template - class MonomorphicImpl : public ActionInterface { - public: - typedef typename internal::Function::Result Result; - typedef typename internal::Function::ArgumentTuple ArgumentTuple; - - explicit MonomorphicImpl(const Impl& impl) : impl_(impl) {} - - Result Perform(const ArgumentTuple& args) override { - return impl_.template Perform(args); - } - - private: - Impl impl_; - }; - - Impl impl_; -}; - -// Creates an Action from its implementation and returns it. The -// created Action object owns the implementation. -template -Action MakeAction(ActionInterface* impl) { - return Action(impl); -} - -// Creates a polymorphic action from its implementation. This is -// easier to use than the PolymorphicAction constructor as it -// doesn't require you to explicitly write the template argument, e.g. -// -// MakePolymorphicAction(foo); -// vs -// PolymorphicAction(foo); -template -inline PolymorphicAction MakePolymorphicAction(const Impl& impl) { - return PolymorphicAction(impl); -} - -namespace internal { - -// Helper struct to specialize ReturnAction to execute a move instead of a copy -// on return. Useful for move-only types, but could be used on any type. -template -struct ByMoveWrapper { - explicit ByMoveWrapper(T value) : payload(std::move(value)) {} - T payload; -}; - -// Implements the polymorphic Return(x) action, which can be used in -// any function that returns the type of x, regardless of the argument -// types. -// -// Note: The value passed into Return must be converted into -// Function::Result when this action is cast to Action rather than -// when that action is performed. This is important in scenarios like -// -// MOCK_METHOD1(Method, T(U)); -// ... -// { -// Foo foo; -// X x(&foo); -// EXPECT_CALL(mock, Method(_)).WillOnce(Return(x)); -// } -// -// In the example above the variable x holds reference to foo which leaves -// scope and gets destroyed. If copying X just copies a reference to foo, -// that copy will be left with a hanging reference. If conversion to T -// makes a copy of foo, the above code is safe. To support that scenario, we -// need to make sure that the type conversion happens inside the EXPECT_CALL -// statement, and conversion of the result of Return to Action is a -// good place for that. -// -// The real life example of the above scenario happens when an invocation -// of gtl::Container() is passed into Return. -// -template -class ReturnAction { - public: - // Constructs a ReturnAction object from the value to be returned. - // 'value' is passed by value instead of by const reference in order - // to allow Return("string literal") to compile. - explicit ReturnAction(R value) : value_(new R(std::move(value))) {} - - // This template type conversion operator allows Return(x) to be - // used in ANY function that returns x's type. - template - operator Action() const { // NOLINT - // Assert statement belongs here because this is the best place to verify - // conditions on F. It produces the clearest error messages - // in most compilers. - // Impl really belongs in this scope as a local class but can't - // because MSVC produces duplicate symbols in different translation units - // in this case. Until MS fixes that bug we put Impl into the class scope - // and put the typedef both here (for use in assert statement) and - // in the Impl class. But both definitions must be the same. - typedef typename Function::Result Result; - GTEST_COMPILE_ASSERT_( - !std::is_reference::value, - use_ReturnRef_instead_of_Return_to_return_a_reference); - static_assert(!std::is_void::value, - "Can't use Return() on an action expected to return `void`."); - return Action(new Impl(value_)); - } - - private: - // Implements the Return(x) action for a particular function type F. - template - class Impl : public ActionInterface { - public: - typedef typename Function::Result Result; - typedef typename Function::ArgumentTuple ArgumentTuple; - - // The implicit cast is necessary when Result has more than one - // single-argument constructor (e.g. Result is std::vector) and R - // has a type conversion operator template. In that case, value_(value) - // won't compile as the compiler doesn't known which constructor of - // Result to call. ImplicitCast_ forces the compiler to convert R to - // Result without considering explicit constructors, thus resolving the - // ambiguity. value_ is then initialized using its copy constructor. - explicit Impl(const std::shared_ptr& value) - : value_before_cast_(*value), - value_(ImplicitCast_(value_before_cast_)) {} - - Result Perform(const ArgumentTuple&) override { return value_; } - - private: - GTEST_COMPILE_ASSERT_(!std::is_reference::value, - Result_cannot_be_a_reference_type); - // We save the value before casting just in case it is being cast to a - // wrapper type. - R value_before_cast_; - Result value_; - - GTEST_DISALLOW_COPY_AND_ASSIGN_(Impl); - }; - - // Partially specialize for ByMoveWrapper. This version of ReturnAction will - // move its contents instead. - template - class Impl, F> : public ActionInterface { - public: - typedef typename Function::Result Result; - typedef typename Function::ArgumentTuple ArgumentTuple; - - explicit Impl(const std::shared_ptr& wrapper) - : performed_(false), wrapper_(wrapper) {} - - Result Perform(const ArgumentTuple&) override { - GTEST_CHECK_(!performed_) - << "A ByMove() action should only be performed once."; - performed_ = true; - return std::move(wrapper_->payload); - } - - private: - bool performed_; - const std::shared_ptr wrapper_; - }; - - const std::shared_ptr value_; -}; - -// Implements the ReturnNull() action. -class ReturnNullAction { - public: - // Allows ReturnNull() to be used in any pointer-returning function. In C++11 - // this is enforced by returning nullptr, and in non-C++11 by asserting a - // pointer type on compile time. - template - static Result Perform(const ArgumentTuple&) { - return nullptr; - } -}; - -// Implements the Return() action. -class ReturnVoidAction { - public: - // Allows Return() to be used in any void-returning function. - template - static void Perform(const ArgumentTuple&) { - static_assert(std::is_void::value, "Result should be void."); - } -}; - -// Implements the polymorphic ReturnRef(x) action, which can be used -// in any function that returns a reference to the type of x, -// regardless of the argument types. -template -class ReturnRefAction { - public: - // Constructs a ReturnRefAction object from the reference to be returned. - explicit ReturnRefAction(T& ref) : ref_(ref) {} // NOLINT - - // This template type conversion operator allows ReturnRef(x) to be - // used in ANY function that returns a reference to x's type. - template - operator Action() const { - typedef typename Function::Result Result; - // Asserts that the function return type is a reference. This - // catches the user error of using ReturnRef(x) when Return(x) - // should be used, and generates some helpful error message. - GTEST_COMPILE_ASSERT_(std::is_reference::value, - use_Return_instead_of_ReturnRef_to_return_a_value); - return Action(new Impl(ref_)); - } - - private: - // Implements the ReturnRef(x) action for a particular function type F. - template - class Impl : public ActionInterface { - public: - typedef typename Function::Result Result; - typedef typename Function::ArgumentTuple ArgumentTuple; - - explicit Impl(T& ref) : ref_(ref) {} // NOLINT - - Result Perform(const ArgumentTuple&) override { return ref_; } - - private: - T& ref_; - }; - - T& ref_; -}; - -// Implements the polymorphic ReturnRefOfCopy(x) action, which can be -// used in any function that returns a reference to the type of x, -// regardless of the argument types. -template -class ReturnRefOfCopyAction { - public: - // Constructs a ReturnRefOfCopyAction object from the reference to - // be returned. - explicit ReturnRefOfCopyAction(const T& value) : value_(value) {} // NOLINT - - // This template type conversion operator allows ReturnRefOfCopy(x) to be - // used in ANY function that returns a reference to x's type. - template - operator Action() const { - typedef typename Function::Result Result; - // Asserts that the function return type is a reference. This - // catches the user error of using ReturnRefOfCopy(x) when Return(x) - // should be used, and generates some helpful error message. - GTEST_COMPILE_ASSERT_( - std::is_reference::value, - use_Return_instead_of_ReturnRefOfCopy_to_return_a_value); - return Action(new Impl(value_)); - } - - private: - // Implements the ReturnRefOfCopy(x) action for a particular function type F. - template - class Impl : public ActionInterface { - public: - typedef typename Function::Result Result; - typedef typename Function::ArgumentTuple ArgumentTuple; - - explicit Impl(const T& value) : value_(value) {} // NOLINT - - Result Perform(const ArgumentTuple&) override { return value_; } - - private: - T value_; - }; - - const T value_; -}; - -// Implements the polymorphic ReturnRoundRobin(v) action, which can be -// used in any function that returns the element_type of v. -template -class ReturnRoundRobinAction { - public: - explicit ReturnRoundRobinAction(std::vector values) { - GTEST_CHECK_(!values.empty()) - << "ReturnRoundRobin requires at least one element."; - state_->values = std::move(values); - } - - template - T operator()(Args&&...) const { - return state_->Next(); - } - - private: - struct State { - T Next() { - T ret_val = values[i++]; - if (i == values.size()) i = 0; - return ret_val; - } - - std::vector values; - size_t i = 0; - }; - std::shared_ptr state_ = std::make_shared(); -}; - -// Implements the polymorphic DoDefault() action. -class DoDefaultAction { - public: - // This template type conversion operator allows DoDefault() to be - // used in any function. - template - operator Action() const { return Action(); } // NOLINT -}; - -// Implements the Assign action to set a given pointer referent to a -// particular value. -template -class AssignAction { - public: - AssignAction(T1* ptr, T2 value) : ptr_(ptr), value_(value) {} - - template - void Perform(const ArgumentTuple& /* args */) const { - *ptr_ = value_; - } - - private: - T1* const ptr_; - const T2 value_; -}; - -#if !GTEST_OS_WINDOWS_MOBILE - -// Implements the SetErrnoAndReturn action to simulate return from -// various system calls and libc functions. -template -class SetErrnoAndReturnAction { - public: - SetErrnoAndReturnAction(int errno_value, T result) - : errno_(errno_value), - result_(result) {} - template - Result Perform(const ArgumentTuple& /* args */) const { - errno = errno_; - return result_; - } - - private: - const int errno_; - const T result_; -}; - -#endif // !GTEST_OS_WINDOWS_MOBILE - -// Implements the SetArgumentPointee(x) action for any function -// whose N-th argument (0-based) is a pointer to x's type. -template -struct SetArgumentPointeeAction { - A value; - - template - void operator()(const Args&... args) const { - *::std::get(std::tie(args...)) = value; - } -}; - -// Implements the Invoke(object_ptr, &Class::Method) action. -template -struct InvokeMethodAction { - Class* const obj_ptr; - const MethodPtr method_ptr; - - template - auto operator()(Args&&... args) const - -> decltype((obj_ptr->*method_ptr)(std::forward(args)...)) { - return (obj_ptr->*method_ptr)(std::forward(args)...); - } -}; - -// Implements the InvokeWithoutArgs(f) action. The template argument -// FunctionImpl is the implementation type of f, which can be either a -// function pointer or a functor. InvokeWithoutArgs(f) can be used as an -// Action as long as f's type is compatible with F. -template -struct InvokeWithoutArgsAction { - FunctionImpl function_impl; - - // Allows InvokeWithoutArgs(f) to be used as any action whose type is - // compatible with f. - template - auto operator()(const Args&...) -> decltype(function_impl()) { - return function_impl(); - } -}; - -// Implements the InvokeWithoutArgs(object_ptr, &Class::Method) action. -template -struct InvokeMethodWithoutArgsAction { - Class* const obj_ptr; - const MethodPtr method_ptr; - - using ReturnType = - decltype((std::declval()->*std::declval())()); - - template - ReturnType operator()(const Args&...) const { - return (obj_ptr->*method_ptr)(); - } -}; - -// Implements the IgnoreResult(action) action. -template -class IgnoreResultAction { - public: - explicit IgnoreResultAction(const A& action) : action_(action) {} - - template - operator Action() const { - // Assert statement belongs here because this is the best place to verify - // conditions on F. It produces the clearest error messages - // in most compilers. - // Impl really belongs in this scope as a local class but can't - // because MSVC produces duplicate symbols in different translation units - // in this case. Until MS fixes that bug we put Impl into the class scope - // and put the typedef both here (for use in assert statement) and - // in the Impl class. But both definitions must be the same. - typedef typename internal::Function::Result Result; - - // Asserts at compile time that F returns void. - static_assert(std::is_void::value, "Result type should be void."); - - return Action(new Impl(action_)); - } - - private: - template - class Impl : public ActionInterface { - public: - typedef typename internal::Function::Result Result; - typedef typename internal::Function::ArgumentTuple ArgumentTuple; - - explicit Impl(const A& action) : action_(action) {} - - void Perform(const ArgumentTuple& args) override { - // Performs the action and ignores its result. - action_.Perform(args); - } - - private: - // Type OriginalFunction is the same as F except that its return - // type is IgnoredValue. - typedef typename internal::Function::MakeResultIgnoredValue - OriginalFunction; - - const Action action_; - }; - - const A action_; -}; - -template -struct WithArgsAction { - InnerAction action; - - // The inner action could be anything convertible to Action. - // We use the conversion operator to detect the signature of the inner Action. - template - operator Action() const { // NOLINT - using TupleType = std::tuple; - Action::type...)> - converted(action); - - return [converted](Args... args) -> R { - return converted.Perform(std::forward_as_tuple( - std::get(std::forward_as_tuple(std::forward(args)...))...)); - }; - } -}; - -template -struct DoAllAction { - private: - template - using NonFinalType = - typename std::conditional::value, T, const T&>::type; - - template - std::vector Convert(IndexSequence) const { - return {ActionT(std::get(actions))...}; - } - - public: - std::tuple actions; - - template - operator Action() const { // NOLINT - struct Op { - std::vector...)>> converted; - Action last; - R operator()(Args... args) const { - auto tuple_args = std::forward_as_tuple(std::forward(args)...); - for (auto& a : converted) { - a.Perform(tuple_args); - } - return last.Perform(std::move(tuple_args)); - } - }; - return Op{Convert...)>>( - MakeIndexSequence()), - std::get(actions)}; - } -}; - -template -struct ReturnNewAction { - T* operator()() const { - return internal::Apply( - [](const Params&... unpacked_params) { - return new T(unpacked_params...); - }, - params); - } - std::tuple params; -}; - -template -struct ReturnArgAction { - template - auto operator()(const Args&... args) const -> - typename std::tuple_element>::type { - return std::get(std::tie(args...)); - } -}; - -template -struct SaveArgAction { - Ptr pointer; - - template - void operator()(const Args&... args) const { - *pointer = std::get(std::tie(args...)); - } -}; - -template -struct SaveArgPointeeAction { - Ptr pointer; - - template - void operator()(const Args&... args) const { - *pointer = *std::get(std::tie(args...)); - } -}; - -template -struct SetArgRefereeAction { - T value; - - template - void operator()(Args&&... args) const { - using argk_type = - typename ::std::tuple_element>::type; - static_assert(std::is_lvalue_reference::value, - "Argument must be a reference type."); - std::get(std::tie(args...)) = value; - } -}; - -template -struct SetArrayArgumentAction { - I1 first; - I2 last; - - template - void operator()(const Args&... args) const { - auto value = std::get(std::tie(args...)); - for (auto it = first; it != last; ++it, (void)++value) { - *value = *it; - } - } -}; - -template -struct DeleteArgAction { - template - void operator()(const Args&... args) const { - delete std::get(std::tie(args...)); - } -}; - -template -struct ReturnPointeeAction { - Ptr pointer; - template - auto operator()(const Args&...) const -> decltype(*pointer) { - return *pointer; - } -}; - -#if GTEST_HAS_EXCEPTIONS -template -struct ThrowAction { - T exception; - // We use a conversion operator to adapt to any return type. - template - operator Action() const { // NOLINT - T copy = exception; - return [copy](Args...) -> R { throw copy; }; - } -}; -#endif // GTEST_HAS_EXCEPTIONS - -} // namespace internal - -// An Unused object can be implicitly constructed from ANY value. -// This is handy when defining actions that ignore some or all of the -// mock function arguments. For example, given -// -// MOCK_METHOD3(Foo, double(const string& label, double x, double y)); -// MOCK_METHOD3(Bar, double(int index, double x, double y)); -// -// instead of -// -// double DistanceToOriginWithLabel(const string& label, double x, double y) { -// return sqrt(x*x + y*y); -// } -// double DistanceToOriginWithIndex(int index, double x, double y) { -// return sqrt(x*x + y*y); -// } -// ... -// EXPECT_CALL(mock, Foo("abc", _, _)) -// .WillOnce(Invoke(DistanceToOriginWithLabel)); -// EXPECT_CALL(mock, Bar(5, _, _)) -// .WillOnce(Invoke(DistanceToOriginWithIndex)); -// -// you could write -// -// // We can declare any uninteresting argument as Unused. -// double DistanceToOrigin(Unused, double x, double y) { -// return sqrt(x*x + y*y); -// } -// ... -// EXPECT_CALL(mock, Foo("abc", _, _)).WillOnce(Invoke(DistanceToOrigin)); -// EXPECT_CALL(mock, Bar(5, _, _)).WillOnce(Invoke(DistanceToOrigin)); -typedef internal::IgnoredValue Unused; - -// Creates an action that does actions a1, a2, ..., sequentially in -// each invocation. All but the last action will have a readonly view of the -// arguments. -template -internal::DoAllAction::type...> DoAll( - Action&&... action) { - return {std::forward_as_tuple(std::forward(action)...)}; -} - -// WithArg(an_action) creates an action that passes the k-th -// (0-based) argument of the mock function to an_action and performs -// it. It adapts an action accepting one argument to one that accepts -// multiple arguments. For convenience, we also provide -// WithArgs(an_action) (defined below) as a synonym. -template -internal::WithArgsAction::type, k> -WithArg(InnerAction&& action) { - return {std::forward(action)}; -} - -// WithArgs(an_action) creates an action that passes -// the selected arguments of the mock function to an_action and -// performs it. It serves as an adaptor between actions with -// different argument lists. -template -internal::WithArgsAction::type, k, ks...> -WithArgs(InnerAction&& action) { - return {std::forward(action)}; -} - -// WithoutArgs(inner_action) can be used in a mock function with a -// non-empty argument list to perform inner_action, which takes no -// argument. In other words, it adapts an action accepting no -// argument to one that accepts (and ignores) arguments. -template -internal::WithArgsAction::type> -WithoutArgs(InnerAction&& action) { - return {std::forward(action)}; -} - -// Creates an action that returns 'value'. 'value' is passed by value -// instead of const reference - otherwise Return("string literal") -// will trigger a compiler error about using array as initializer. -template -internal::ReturnAction Return(R value) { - return internal::ReturnAction(std::move(value)); -} - -// Creates an action that returns NULL. -inline PolymorphicAction ReturnNull() { - return MakePolymorphicAction(internal::ReturnNullAction()); -} - -// Creates an action that returns from a void function. -inline PolymorphicAction Return() { - return MakePolymorphicAction(internal::ReturnVoidAction()); -} - -// Creates an action that returns the reference to a variable. -template -inline internal::ReturnRefAction ReturnRef(R& x) { // NOLINT - return internal::ReturnRefAction(x); -} - -// Prevent using ReturnRef on reference to temporary. -template -internal::ReturnRefAction ReturnRef(R&&) = delete; - -// Creates an action that returns the reference to a copy of the -// argument. The copy is created when the action is constructed and -// lives as long as the action. -template -inline internal::ReturnRefOfCopyAction ReturnRefOfCopy(const R& x) { - return internal::ReturnRefOfCopyAction(x); -} - -// Modifies the parent action (a Return() action) to perform a move of the -// argument instead of a copy. -// Return(ByMove()) actions can only be executed once and will assert this -// invariant. -template -internal::ByMoveWrapper ByMove(R x) { - return internal::ByMoveWrapper(std::move(x)); -} - -// Creates an action that returns an element of `vals`. Calling this action will -// repeatedly return the next value from `vals` until it reaches the end and -// will restart from the beginning. -template -internal::ReturnRoundRobinAction ReturnRoundRobin(std::vector vals) { - return internal::ReturnRoundRobinAction(std::move(vals)); -} - -// Creates an action that returns an element of `vals`. Calling this action will -// repeatedly return the next value from `vals` until it reaches the end and -// will restart from the beginning. -template -internal::ReturnRoundRobinAction ReturnRoundRobin( - std::initializer_list vals) { - return internal::ReturnRoundRobinAction(std::vector(vals)); -} - -// Creates an action that does the default action for the give mock function. -inline internal::DoDefaultAction DoDefault() { - return internal::DoDefaultAction(); -} - -// Creates an action that sets the variable pointed by the N-th -// (0-based) function argument to 'value'. -template -internal::SetArgumentPointeeAction SetArgPointee(T value) { - return {std::move(value)}; -} - -// The following version is DEPRECATED. -template -internal::SetArgumentPointeeAction SetArgumentPointee(T value) { - return {std::move(value)}; -} - -// Creates an action that sets a pointer referent to a given value. -template -PolymorphicAction > Assign(T1* ptr, T2 val) { - return MakePolymorphicAction(internal::AssignAction(ptr, val)); -} - -#if !GTEST_OS_WINDOWS_MOBILE - -// Creates an action that sets errno and returns the appropriate error. -template -PolymorphicAction > -SetErrnoAndReturn(int errval, T result) { - return MakePolymorphicAction( - internal::SetErrnoAndReturnAction(errval, result)); -} - -#endif // !GTEST_OS_WINDOWS_MOBILE - -// Various overloads for Invoke(). - -// Legacy function. -// Actions can now be implicitly constructed from callables. No need to create -// wrapper objects. -// This function exists for backwards compatibility. -template -typename std::decay::type Invoke(FunctionImpl&& function_impl) { - return std::forward(function_impl); -} - -// Creates an action that invokes the given method on the given object -// with the mock function's arguments. -template -internal::InvokeMethodAction Invoke(Class* obj_ptr, - MethodPtr method_ptr) { - return {obj_ptr, method_ptr}; -} - -// Creates an action that invokes 'function_impl' with no argument. -template -internal::InvokeWithoutArgsAction::type> -InvokeWithoutArgs(FunctionImpl function_impl) { - return {std::move(function_impl)}; -} - -// Creates an action that invokes the given method on the given object -// with no argument. -template -internal::InvokeMethodWithoutArgsAction InvokeWithoutArgs( - Class* obj_ptr, MethodPtr method_ptr) { - return {obj_ptr, method_ptr}; -} - -// Creates an action that performs an_action and throws away its -// result. In other words, it changes the return type of an_action to -// void. an_action MUST NOT return void, or the code won't compile. -template -inline internal::IgnoreResultAction IgnoreResult(const A& an_action) { - return internal::IgnoreResultAction(an_action); -} - -// Creates a reference wrapper for the given L-value. If necessary, -// you can explicitly specify the type of the reference. For example, -// suppose 'derived' is an object of type Derived, ByRef(derived) -// would wrap a Derived&. If you want to wrap a const Base& instead, -// where Base is a base class of Derived, just write: -// -// ByRef(derived) -// -// N.B. ByRef is redundant with std::ref, std::cref and std::reference_wrapper. -// However, it may still be used for consistency with ByMove(). -template -inline ::std::reference_wrapper ByRef(T& l_value) { // NOLINT - return ::std::reference_wrapper(l_value); -} - -// The ReturnNew(a1, a2, ..., a_k) action returns a pointer to a new -// instance of type T, constructed on the heap with constructor arguments -// a1, a2, ..., and a_k. The caller assumes ownership of the returned value. -template -internal::ReturnNewAction::type...> ReturnNew( - Params&&... params) { - return {std::forward_as_tuple(std::forward(params)...)}; -} - -// Action ReturnArg() returns the k-th argument of the mock function. -template -internal::ReturnArgAction ReturnArg() { - return {}; -} - -// Action SaveArg(pointer) saves the k-th (0-based) argument of the -// mock function to *pointer. -template -internal::SaveArgAction SaveArg(Ptr pointer) { - return {pointer}; -} - -// Action SaveArgPointee(pointer) saves the value pointed to -// by the k-th (0-based) argument of the mock function to *pointer. -template -internal::SaveArgPointeeAction SaveArgPointee(Ptr pointer) { - return {pointer}; -} - -// Action SetArgReferee(value) assigns 'value' to the variable -// referenced by the k-th (0-based) argument of the mock function. -template -internal::SetArgRefereeAction::type> SetArgReferee( - T&& value) { - return {std::forward(value)}; -} - -// Action SetArrayArgument(first, last) copies the elements in -// source range [first, last) to the array pointed to by the k-th -// (0-based) argument, which can be either a pointer or an -// iterator. The action does not take ownership of the elements in the -// source range. -template -internal::SetArrayArgumentAction SetArrayArgument(I1 first, - I2 last) { - return {first, last}; -} - -// Action DeleteArg() deletes the k-th (0-based) argument of the mock -// function. -template -internal::DeleteArgAction DeleteArg() { - return {}; -} - -// This action returns the value pointed to by 'pointer'. -template -internal::ReturnPointeeAction ReturnPointee(Ptr pointer) { - return {pointer}; -} - -// Action Throw(exception) can be used in a mock function of any type -// to throw the given exception. Any copyable value can be thrown. -#if GTEST_HAS_EXCEPTIONS -template -internal::ThrowAction::type> Throw(T&& exception) { - return {std::forward(exception)}; -} -#endif // GTEST_HAS_EXCEPTIONS - -namespace internal { - -// A macro from the ACTION* family (defined later in gmock-generated-actions.h) -// defines an action that can be used in a mock function. Typically, -// these actions only care about a subset of the arguments of the mock -// function. For example, if such an action only uses the second -// argument, it can be used in any mock function that takes >= 2 -// arguments where the type of the second argument is compatible. -// -// Therefore, the action implementation must be prepared to take more -// arguments than it needs. The ExcessiveArg type is used to -// represent those excessive arguments. In order to keep the compiler -// error messages tractable, we define it in the testing namespace -// instead of testing::internal. However, this is an INTERNAL TYPE -// and subject to change without notice, so a user MUST NOT USE THIS -// TYPE DIRECTLY. -struct ExcessiveArg {}; - -// Builds an implementation of an Action<> for some particular signature, using -// a class defined by an ACTION* macro. -template struct ActionImpl; - -template -struct ImplBase { - struct Holder { - // Allows each copy of the Action<> to get to the Impl. - explicit operator const Impl&() const { return *ptr; } - std::shared_ptr ptr; - }; - using type = typename std::conditional::value, - Impl, Holder>::type; -}; - -template -struct ActionImpl : ImplBase::type { - using Base = typename ImplBase::type; - using function_type = R(Args...); - using args_type = std::tuple; - - ActionImpl() = default; // Only defined if appropriate for Base. - explicit ActionImpl(std::shared_ptr impl) : Base{std::move(impl)} { } - - R operator()(Args&&... arg) const { - static constexpr size_t kMaxArgs = - sizeof...(Args) <= 10 ? sizeof...(Args) : 10; - return Apply(MakeIndexSequence{}, - MakeIndexSequence<10 - kMaxArgs>{}, - args_type{std::forward(arg)...}); - } - - template - R Apply(IndexSequence, IndexSequence, - const args_type& args) const { - // Impl need not be specific to the signature of action being implemented; - // only the implementing function body needs to have all of the specific - // types instantiated. Up to 10 of the args that are provided by the - // args_type get passed, followed by a dummy of unspecified type for the - // remainder up to 10 explicit args. - static constexpr ExcessiveArg kExcessArg{}; - return static_cast(*this).template gmock_PerformImpl< - /*function_type=*/function_type, /*return_type=*/R, - /*args_type=*/args_type, - /*argN_type=*/typename std::tuple_element::type...>( - /*args=*/args, std::get(args)..., - ((void)excess_id, kExcessArg)...); - } -}; - -// Stores a default-constructed Impl as part of the Action<>'s -// std::function<>. The Impl should be trivial to copy. -template -::testing::Action MakeAction() { - return ::testing::Action(ActionImpl()); -} - -// Stores just the one given instance of Impl. -template -::testing::Action MakeAction(std::shared_ptr impl) { - return ::testing::Action(ActionImpl(std::move(impl))); -} - -#define GMOCK_INTERNAL_ARG_UNUSED(i, data, el) \ - , const arg##i##_type& arg##i GTEST_ATTRIBUTE_UNUSED_ -#define GMOCK_ACTION_ARG_TYPES_AND_NAMES_UNUSED_ \ - const args_type& args GTEST_ATTRIBUTE_UNUSED_ GMOCK_PP_REPEAT( \ - GMOCK_INTERNAL_ARG_UNUSED, , 10) - -#define GMOCK_INTERNAL_ARG(i, data, el) , const arg##i##_type& arg##i -#define GMOCK_ACTION_ARG_TYPES_AND_NAMES_ \ - const args_type& args GMOCK_PP_REPEAT(GMOCK_INTERNAL_ARG, , 10) - -#define GMOCK_INTERNAL_TEMPLATE_ARG(i, data, el) , typename arg##i##_type -#define GMOCK_ACTION_TEMPLATE_ARGS_NAMES_ \ - GMOCK_PP_TAIL(GMOCK_PP_REPEAT(GMOCK_INTERNAL_TEMPLATE_ARG, , 10)) - -#define GMOCK_INTERNAL_TYPENAME_PARAM(i, data, param) , typename param##_type -#define GMOCK_ACTION_TYPENAME_PARAMS_(params) \ - GMOCK_PP_TAIL(GMOCK_PP_FOR_EACH(GMOCK_INTERNAL_TYPENAME_PARAM, , params)) - -#define GMOCK_INTERNAL_TYPE_PARAM(i, data, param) , param##_type -#define GMOCK_ACTION_TYPE_PARAMS_(params) \ - GMOCK_PP_TAIL(GMOCK_PP_FOR_EACH(GMOCK_INTERNAL_TYPE_PARAM, , params)) - -#define GMOCK_INTERNAL_TYPE_GVALUE_PARAM(i, data, param) \ - , param##_type gmock_p##i -#define GMOCK_ACTION_TYPE_GVALUE_PARAMS_(params) \ - GMOCK_PP_TAIL(GMOCK_PP_FOR_EACH(GMOCK_INTERNAL_TYPE_GVALUE_PARAM, , params)) - -#define GMOCK_INTERNAL_GVALUE_PARAM(i, data, param) \ - , std::forward(gmock_p##i) -#define GMOCK_ACTION_GVALUE_PARAMS_(params) \ - GMOCK_PP_TAIL(GMOCK_PP_FOR_EACH(GMOCK_INTERNAL_GVALUE_PARAM, , params)) - -#define GMOCK_INTERNAL_INIT_PARAM(i, data, param) \ - , param(::std::forward(gmock_p##i)) -#define GMOCK_ACTION_INIT_PARAMS_(params) \ - GMOCK_PP_TAIL(GMOCK_PP_FOR_EACH(GMOCK_INTERNAL_INIT_PARAM, , params)) - -#define GMOCK_INTERNAL_FIELD_PARAM(i, data, param) param##_type param; -#define GMOCK_ACTION_FIELD_PARAMS_(params) \ - GMOCK_PP_FOR_EACH(GMOCK_INTERNAL_FIELD_PARAM, , params) - -#define GMOCK_INTERNAL_ACTION(name, full_name, params) \ - template \ - class full_name { \ - public: \ - explicit full_name(GMOCK_ACTION_TYPE_GVALUE_PARAMS_(params)) \ - : impl_(std::make_shared( \ - GMOCK_ACTION_GVALUE_PARAMS_(params))) { } \ - full_name(const full_name&) = default; \ - full_name(full_name&&) noexcept = default; \ - template \ - operator ::testing::Action() const { \ - return ::testing::internal::MakeAction(impl_); \ - } \ - private: \ - class gmock_Impl { \ - public: \ - explicit gmock_Impl(GMOCK_ACTION_TYPE_GVALUE_PARAMS_(params)) \ - : GMOCK_ACTION_INIT_PARAMS_(params) {} \ - template \ - return_type gmock_PerformImpl(GMOCK_ACTION_ARG_TYPES_AND_NAMES_) const; \ - GMOCK_ACTION_FIELD_PARAMS_(params) \ - }; \ - std::shared_ptr impl_; \ - }; \ - template \ - inline full_name name( \ - GMOCK_ACTION_TYPE_GVALUE_PARAMS_(params)) { \ - return full_name( \ - GMOCK_ACTION_GVALUE_PARAMS_(params)); \ - } \ - template \ - template \ - return_type full_name::gmock_Impl:: \ - gmock_PerformImpl(GMOCK_ACTION_ARG_TYPES_AND_NAMES_UNUSED_) const - -} // namespace internal - -// Similar to GMOCK_INTERNAL_ACTION, but no bound parameters are stored. -#define ACTION(name) \ - class name##Action { \ - public: \ - explicit name##Action() noexcept {} \ - name##Action(const name##Action&) noexcept {} \ - template \ - operator ::testing::Action() const { \ - return ::testing::internal::MakeAction(); \ - } \ - private: \ - class gmock_Impl { \ - public: \ - template \ - return_type gmock_PerformImpl(GMOCK_ACTION_ARG_TYPES_AND_NAMES_) const; \ - }; \ - }; \ - inline name##Action name() GTEST_MUST_USE_RESULT_; \ - inline name##Action name() { return name##Action(); } \ - template \ - return_type name##Action::gmock_Impl::gmock_PerformImpl( \ - GMOCK_ACTION_ARG_TYPES_AND_NAMES_UNUSED_) const - -#define ACTION_P(name, ...) \ - GMOCK_INTERNAL_ACTION(name, name##ActionP, (__VA_ARGS__)) - -#define ACTION_P2(name, ...) \ - GMOCK_INTERNAL_ACTION(name, name##ActionP2, (__VA_ARGS__)) - -#define ACTION_P3(name, ...) \ - GMOCK_INTERNAL_ACTION(name, name##ActionP3, (__VA_ARGS__)) - -#define ACTION_P4(name, ...) \ - GMOCK_INTERNAL_ACTION(name, name##ActionP4, (__VA_ARGS__)) - -#define ACTION_P5(name, ...) \ - GMOCK_INTERNAL_ACTION(name, name##ActionP5, (__VA_ARGS__)) - -#define ACTION_P6(name, ...) \ - GMOCK_INTERNAL_ACTION(name, name##ActionP6, (__VA_ARGS__)) - -#define ACTION_P7(name, ...) \ - GMOCK_INTERNAL_ACTION(name, name##ActionP7, (__VA_ARGS__)) - -#define ACTION_P8(name, ...) \ - GMOCK_INTERNAL_ACTION(name, name##ActionP8, (__VA_ARGS__)) - -#define ACTION_P9(name, ...) \ - GMOCK_INTERNAL_ACTION(name, name##ActionP9, (__VA_ARGS__)) - -#define ACTION_P10(name, ...) \ - GMOCK_INTERNAL_ACTION(name, name##ActionP10, (__VA_ARGS__)) - -} // namespace testing - -#ifdef _MSC_VER -# pragma warning(pop) -#endif - -#endif // GOOGLEMOCK_INCLUDE_GMOCK_GMOCK_ACTIONS_H_ diff --git a/vendor/googletest/googlemock/include/gmock/gmock-cardinalities.h b/vendor/googletest/googlemock/include/gmock/gmock-cardinalities.h deleted file mode 100644 index fc7f803a7..000000000 --- a/vendor/googletest/googlemock/include/gmock/gmock-cardinalities.h +++ /dev/null @@ -1,157 +0,0 @@ -// Copyright 2007, Google Inc. -// All rights reserved. -// -// Redistribution and use in source and binary forms, with or without -// modification, are permitted provided that the following conditions are -// met: -// -// * Redistributions of source code must retain the above copyright -// notice, this list of conditions and the following disclaimer. -// * Redistributions in binary form must reproduce the above -// copyright notice, this list of conditions and the following disclaimer -// in the documentation and/or other materials provided with the -// distribution. -// * Neither the name of Google Inc. nor the names of its -// contributors may be used to endorse or promote products derived from -// this software without specific prior written permission. -// -// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - - -// Google Mock - a framework for writing C++ mock classes. -// -// This file implements some commonly used cardinalities. More -// cardinalities can be defined by the user implementing the -// CardinalityInterface interface if necessary. - -// GOOGLETEST_CM0002 DO NOT DELETE - -#ifndef GOOGLEMOCK_INCLUDE_GMOCK_GMOCK_CARDINALITIES_H_ -#define GOOGLEMOCK_INCLUDE_GMOCK_GMOCK_CARDINALITIES_H_ - -#include -#include -#include // NOLINT -#include "gmock/internal/gmock-port.h" -#include "gtest/gtest.h" - -GTEST_DISABLE_MSC_WARNINGS_PUSH_(4251 \ -/* class A needs to have dll-interface to be used by clients of class B */) - -namespace testing { - -// To implement a cardinality Foo, define: -// 1. a class FooCardinality that implements the -// CardinalityInterface interface, and -// 2. a factory function that creates a Cardinality object from a -// const FooCardinality*. -// -// The two-level delegation design follows that of Matcher, providing -// consistency for extension developers. It also eases ownership -// management as Cardinality objects can now be copied like plain values. - -// The implementation of a cardinality. -class CardinalityInterface { - public: - virtual ~CardinalityInterface() {} - - // Conservative estimate on the lower/upper bound of the number of - // calls allowed. - virtual int ConservativeLowerBound() const { return 0; } - virtual int ConservativeUpperBound() const { return INT_MAX; } - - // Returns true if and only if call_count calls will satisfy this - // cardinality. - virtual bool IsSatisfiedByCallCount(int call_count) const = 0; - - // Returns true if and only if call_count calls will saturate this - // cardinality. - virtual bool IsSaturatedByCallCount(int call_count) const = 0; - - // Describes self to an ostream. - virtual void DescribeTo(::std::ostream* os) const = 0; -}; - -// A Cardinality is a copyable and IMMUTABLE (except by assignment) -// object that specifies how many times a mock function is expected to -// be called. The implementation of Cardinality is just a std::shared_ptr -// to const CardinalityInterface. Don't inherit from Cardinality! -class GTEST_API_ Cardinality { - public: - // Constructs a null cardinality. Needed for storing Cardinality - // objects in STL containers. - Cardinality() {} - - // Constructs a Cardinality from its implementation. - explicit Cardinality(const CardinalityInterface* impl) : impl_(impl) {} - - // Conservative estimate on the lower/upper bound of the number of - // calls allowed. - int ConservativeLowerBound() const { return impl_->ConservativeLowerBound(); } - int ConservativeUpperBound() const { return impl_->ConservativeUpperBound(); } - - // Returns true if and only if call_count calls will satisfy this - // cardinality. - bool IsSatisfiedByCallCount(int call_count) const { - return impl_->IsSatisfiedByCallCount(call_count); - } - - // Returns true if and only if call_count calls will saturate this - // cardinality. - bool IsSaturatedByCallCount(int call_count) const { - return impl_->IsSaturatedByCallCount(call_count); - } - - // Returns true if and only if call_count calls will over-saturate this - // cardinality, i.e. exceed the maximum number of allowed calls. - bool IsOverSaturatedByCallCount(int call_count) const { - return impl_->IsSaturatedByCallCount(call_count) && - !impl_->IsSatisfiedByCallCount(call_count); - } - - // Describes self to an ostream - void DescribeTo(::std::ostream* os) const { impl_->DescribeTo(os); } - - // Describes the given actual call count to an ostream. - static void DescribeActualCallCountTo(int actual_call_count, - ::std::ostream* os); - - private: - std::shared_ptr impl_; -}; - -// Creates a cardinality that allows at least n calls. -GTEST_API_ Cardinality AtLeast(int n); - -// Creates a cardinality that allows at most n calls. -GTEST_API_ Cardinality AtMost(int n); - -// Creates a cardinality that allows any number of calls. -GTEST_API_ Cardinality AnyNumber(); - -// Creates a cardinality that allows between min and max calls. -GTEST_API_ Cardinality Between(int min, int max); - -// Creates a cardinality that allows exactly n calls. -GTEST_API_ Cardinality Exactly(int n); - -// Creates a cardinality from its implementation. -inline Cardinality MakeCardinality(const CardinalityInterface* c) { - return Cardinality(c); -} - -} // namespace testing - -GTEST_DISABLE_MSC_WARNINGS_POP_() // 4251 - -#endif // GOOGLEMOCK_INCLUDE_GMOCK_GMOCK_CARDINALITIES_H_ diff --git a/vendor/googletest/googlemock/include/gmock/gmock-function-mocker.h b/vendor/googletest/googlemock/include/gmock/gmock-function-mocker.h deleted file mode 100644 index 0fc6f6f3f..000000000 --- a/vendor/googletest/googlemock/include/gmock/gmock-function-mocker.h +++ /dev/null @@ -1,479 +0,0 @@ -// Copyright 2007, Google Inc. -// All rights reserved. -// -// Redistribution and use in source and binary forms, with or without -// modification, are permitted provided that the following conditions are -// met: -// -// * Redistributions of source code must retain the above copyright -// notice, this list of conditions and the following disclaimer. -// * Redistributions in binary form must reproduce the above -// copyright notice, this list of conditions and the following disclaimer -// in the documentation and/or other materials provided with the -// distribution. -// * Neither the name of Google Inc. nor the names of its -// contributors may be used to endorse or promote products derived from -// this software without specific prior written permission. -// -// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - -// Google Mock - a framework for writing C++ mock classes. -// -// This file implements MOCK_METHOD. - -// GOOGLETEST_CM0002 DO NOT DELETE - -#ifndef GOOGLEMOCK_INCLUDE_GMOCK_INTERNAL_GMOCK_FUNCTION_MOCKER_H_ // NOLINT -#define GOOGLEMOCK_INCLUDE_GMOCK_INTERNAL_GMOCK_FUNCTION_MOCKER_H_ // NOLINT - -#include // IWYU pragma: keep -#include // IWYU pragma: keep - -#include "gmock/gmock-spec-builders.h" -#include "gmock/internal/gmock-internal-utils.h" -#include "gmock/internal/gmock-pp.h" - -namespace testing { -namespace internal { -template -using identity_t = T; - -template -struct ThisRefAdjuster { - template - using AdjustT = typename std::conditional< - std::is_const::type>::value, - typename std::conditional::value, - const T&, const T&&>::type, - typename std::conditional::value, T&, - T&&>::type>::type; - - template - static AdjustT Adjust(const MockType& mock) { - return static_cast>(const_cast(mock)); - } -}; - -} // namespace internal - -// The style guide prohibits "using" statements in a namespace scope -// inside a header file. However, the FunctionMocker class template -// is meant to be defined in the ::testing namespace. The following -// line is just a trick for working around a bug in MSVC 8.0, which -// cannot handle it if we define FunctionMocker in ::testing. -using internal::FunctionMocker; -} // namespace testing - -#define MOCK_METHOD(...) \ - GMOCK_PP_VARIADIC_CALL(GMOCK_INTERNAL_MOCK_METHOD_ARG_, __VA_ARGS__) - -#define GMOCK_INTERNAL_MOCK_METHOD_ARG_1(...) \ - GMOCK_INTERNAL_WRONG_ARITY(__VA_ARGS__) - -#define GMOCK_INTERNAL_MOCK_METHOD_ARG_2(...) \ - GMOCK_INTERNAL_WRONG_ARITY(__VA_ARGS__) - -#define GMOCK_INTERNAL_MOCK_METHOD_ARG_3(_Ret, _MethodName, _Args) \ - GMOCK_INTERNAL_MOCK_METHOD_ARG_4(_Ret, _MethodName, _Args, ()) - -#define GMOCK_INTERNAL_MOCK_METHOD_ARG_4(_Ret, _MethodName, _Args, _Spec) \ - GMOCK_INTERNAL_ASSERT_PARENTHESIS(_Args); \ - GMOCK_INTERNAL_ASSERT_PARENTHESIS(_Spec); \ - GMOCK_INTERNAL_ASSERT_VALID_SIGNATURE( \ - GMOCK_PP_NARG0 _Args, GMOCK_INTERNAL_SIGNATURE(_Ret, _Args)); \ - GMOCK_INTERNAL_ASSERT_VALID_SPEC(_Spec) \ - GMOCK_INTERNAL_MOCK_METHOD_IMPL( \ - GMOCK_PP_NARG0 _Args, _MethodName, GMOCK_INTERNAL_HAS_CONST(_Spec), \ - GMOCK_INTERNAL_HAS_OVERRIDE(_Spec), GMOCK_INTERNAL_HAS_FINAL(_Spec), \ - GMOCK_INTERNAL_GET_NOEXCEPT_SPEC(_Spec), \ - GMOCK_INTERNAL_GET_CALLTYPE(_Spec), GMOCK_INTERNAL_GET_REF_SPEC(_Spec), \ - (GMOCK_INTERNAL_SIGNATURE(_Ret, _Args))) - -#define GMOCK_INTERNAL_MOCK_METHOD_ARG_5(...) \ - GMOCK_INTERNAL_WRONG_ARITY(__VA_ARGS__) - -#define GMOCK_INTERNAL_MOCK_METHOD_ARG_6(...) \ - GMOCK_INTERNAL_WRONG_ARITY(__VA_ARGS__) - -#define GMOCK_INTERNAL_MOCK_METHOD_ARG_7(...) \ - GMOCK_INTERNAL_WRONG_ARITY(__VA_ARGS__) - -#define GMOCK_INTERNAL_WRONG_ARITY(...) \ - static_assert( \ - false, \ - "MOCK_METHOD must be called with 3 or 4 arguments. _Ret, " \ - "_MethodName, _Args and optionally _Spec. _Args and _Spec must be " \ - "enclosed in parentheses. If _Ret is a type with unprotected commas, " \ - "it must also be enclosed in parentheses.") - -#define GMOCK_INTERNAL_ASSERT_PARENTHESIS(_Tuple) \ - static_assert( \ - GMOCK_PP_IS_ENCLOSED_PARENS(_Tuple), \ - GMOCK_PP_STRINGIZE(_Tuple) " should be enclosed in parentheses.") - -#define GMOCK_INTERNAL_ASSERT_VALID_SIGNATURE(_N, ...) \ - static_assert( \ - std::is_function<__VA_ARGS__>::value, \ - "Signature must be a function type, maybe return type contains " \ - "unprotected comma."); \ - static_assert( \ - ::testing::tuple_size::ArgumentTuple>::value == _N, \ - "This method does not take " GMOCK_PP_STRINGIZE( \ - _N) " arguments. Parenthesize all types with unprotected commas.") - -#define GMOCK_INTERNAL_ASSERT_VALID_SPEC(_Spec) \ - GMOCK_PP_FOR_EACH(GMOCK_INTERNAL_ASSERT_VALID_SPEC_ELEMENT, ~, _Spec) - -#define GMOCK_INTERNAL_MOCK_METHOD_IMPL(_N, _MethodName, _Constness, \ - _Override, _Final, _NoexceptSpec, \ - _CallType, _RefSpec, _Signature) \ - typename ::testing::internal::Function::Result \ - GMOCK_INTERNAL_EXPAND(_CallType) \ - _MethodName(GMOCK_PP_REPEAT(GMOCK_INTERNAL_PARAMETER, _Signature, _N)) \ - GMOCK_PP_IF(_Constness, const, ) _RefSpec _NoexceptSpec \ - GMOCK_PP_IF(_Override, override, ) GMOCK_PP_IF(_Final, final, ) { \ - GMOCK_MOCKER_(_N, _Constness, _MethodName) \ - .SetOwnerAndName(this, #_MethodName); \ - return GMOCK_MOCKER_(_N, _Constness, _MethodName) \ - .Invoke(GMOCK_PP_REPEAT(GMOCK_INTERNAL_FORWARD_ARG, _Signature, _N)); \ - } \ - ::testing::MockSpec gmock_##_MethodName( \ - GMOCK_PP_REPEAT(GMOCK_INTERNAL_MATCHER_PARAMETER, _Signature, _N)) \ - GMOCK_PP_IF(_Constness, const, ) _RefSpec { \ - GMOCK_MOCKER_(_N, _Constness, _MethodName).RegisterOwner(this); \ - return GMOCK_MOCKER_(_N, _Constness, _MethodName) \ - .With(GMOCK_PP_REPEAT(GMOCK_INTERNAL_MATCHER_ARGUMENT, , _N)); \ - } \ - ::testing::MockSpec gmock_##_MethodName( \ - const ::testing::internal::WithoutMatchers&, \ - GMOCK_PP_IF(_Constness, const, )::testing::internal::Function< \ - GMOCK_PP_REMOVE_PARENS(_Signature)>*) const _RefSpec _NoexceptSpec { \ - return ::testing::internal::ThisRefAdjuster::Adjust(*this) \ - .gmock_##_MethodName(GMOCK_PP_REPEAT( \ - GMOCK_INTERNAL_A_MATCHER_ARGUMENT, _Signature, _N)); \ - } \ - mutable ::testing::FunctionMocker \ - GMOCK_MOCKER_(_N, _Constness, _MethodName) - -#define GMOCK_INTERNAL_EXPAND(...) __VA_ARGS__ - -// Five Valid modifiers. -#define GMOCK_INTERNAL_HAS_CONST(_Tuple) \ - GMOCK_PP_HAS_COMMA(GMOCK_PP_FOR_EACH(GMOCK_INTERNAL_DETECT_CONST, ~, _Tuple)) - -#define GMOCK_INTERNAL_HAS_OVERRIDE(_Tuple) \ - GMOCK_PP_HAS_COMMA( \ - GMOCK_PP_FOR_EACH(GMOCK_INTERNAL_DETECT_OVERRIDE, ~, _Tuple)) - -#define GMOCK_INTERNAL_HAS_FINAL(_Tuple) \ - GMOCK_PP_HAS_COMMA(GMOCK_PP_FOR_EACH(GMOCK_INTERNAL_DETECT_FINAL, ~, _Tuple)) - -#define GMOCK_INTERNAL_GET_NOEXCEPT_SPEC(_Tuple) \ - GMOCK_PP_FOR_EACH(GMOCK_INTERNAL_NOEXCEPT_SPEC_IF_NOEXCEPT, ~, _Tuple) - -#define GMOCK_INTERNAL_NOEXCEPT_SPEC_IF_NOEXCEPT(_i, _, _elem) \ - GMOCK_PP_IF( \ - GMOCK_PP_HAS_COMMA(GMOCK_INTERNAL_DETECT_NOEXCEPT(_i, _, _elem)), \ - _elem, ) - -#define GMOCK_INTERNAL_GET_REF_SPEC(_Tuple) \ - GMOCK_PP_FOR_EACH(GMOCK_INTERNAL_REF_SPEC_IF_REF, ~, _Tuple) - -#define GMOCK_INTERNAL_REF_SPEC_IF_REF(_i, _, _elem) \ - GMOCK_PP_IF(GMOCK_PP_HAS_COMMA(GMOCK_INTERNAL_DETECT_REF(_i, _, _elem)), \ - GMOCK_PP_CAT(GMOCK_INTERNAL_UNPACK_, _elem), ) - -#define GMOCK_INTERNAL_GET_CALLTYPE(_Tuple) \ - GMOCK_PP_FOR_EACH(GMOCK_INTERNAL_GET_CALLTYPE_IMPL, ~, _Tuple) - -#define GMOCK_INTERNAL_ASSERT_VALID_SPEC_ELEMENT(_i, _, _elem) \ - static_assert( \ - (GMOCK_PP_HAS_COMMA(GMOCK_INTERNAL_DETECT_CONST(_i, _, _elem)) + \ - GMOCK_PP_HAS_COMMA(GMOCK_INTERNAL_DETECT_OVERRIDE(_i, _, _elem)) + \ - GMOCK_PP_HAS_COMMA(GMOCK_INTERNAL_DETECT_FINAL(_i, _, _elem)) + \ - GMOCK_PP_HAS_COMMA(GMOCK_INTERNAL_DETECT_NOEXCEPT(_i, _, _elem)) + \ - GMOCK_PP_HAS_COMMA(GMOCK_INTERNAL_DETECT_REF(_i, _, _elem)) + \ - GMOCK_INTERNAL_IS_CALLTYPE(_elem)) == 1, \ - GMOCK_PP_STRINGIZE( \ - _elem) " cannot be recognized as a valid specification modifier."); - -// Modifiers implementation. -#define GMOCK_INTERNAL_DETECT_CONST(_i, _, _elem) \ - GMOCK_PP_CAT(GMOCK_INTERNAL_DETECT_CONST_I_, _elem) - -#define GMOCK_INTERNAL_DETECT_CONST_I_const , - -#define GMOCK_INTERNAL_DETECT_OVERRIDE(_i, _, _elem) \ - GMOCK_PP_CAT(GMOCK_INTERNAL_DETECT_OVERRIDE_I_, _elem) - -#define GMOCK_INTERNAL_DETECT_OVERRIDE_I_override , - -#define GMOCK_INTERNAL_DETECT_FINAL(_i, _, _elem) \ - GMOCK_PP_CAT(GMOCK_INTERNAL_DETECT_FINAL_I_, _elem) - -#define GMOCK_INTERNAL_DETECT_FINAL_I_final , - -#define GMOCK_INTERNAL_DETECT_NOEXCEPT(_i, _, _elem) \ - GMOCK_PP_CAT(GMOCK_INTERNAL_DETECT_NOEXCEPT_I_, _elem) - -#define GMOCK_INTERNAL_DETECT_NOEXCEPT_I_noexcept , - -#define GMOCK_INTERNAL_DETECT_REF(_i, _, _elem) \ - GMOCK_PP_CAT(GMOCK_INTERNAL_DETECT_REF_I_, _elem) - -#define GMOCK_INTERNAL_DETECT_REF_I_ref , - -#define GMOCK_INTERNAL_UNPACK_ref(x) x - -#define GMOCK_INTERNAL_GET_CALLTYPE_IMPL(_i, _, _elem) \ - GMOCK_PP_IF(GMOCK_INTERNAL_IS_CALLTYPE(_elem), \ - GMOCK_INTERNAL_GET_VALUE_CALLTYPE, GMOCK_PP_EMPTY) \ - (_elem) - -// TODO(iserna): GMOCK_INTERNAL_IS_CALLTYPE and -// GMOCK_INTERNAL_GET_VALUE_CALLTYPE needed more expansions to work on windows -// maybe they can be simplified somehow. -#define GMOCK_INTERNAL_IS_CALLTYPE(_arg) \ - GMOCK_INTERNAL_IS_CALLTYPE_I( \ - GMOCK_PP_CAT(GMOCK_INTERNAL_IS_CALLTYPE_HELPER_, _arg)) -#define GMOCK_INTERNAL_IS_CALLTYPE_I(_arg) GMOCK_PP_IS_ENCLOSED_PARENS(_arg) - -#define GMOCK_INTERNAL_GET_VALUE_CALLTYPE(_arg) \ - GMOCK_INTERNAL_GET_VALUE_CALLTYPE_I( \ - GMOCK_PP_CAT(GMOCK_INTERNAL_IS_CALLTYPE_HELPER_, _arg)) -#define GMOCK_INTERNAL_GET_VALUE_CALLTYPE_I(_arg) \ - GMOCK_PP_IDENTITY _arg - -#define GMOCK_INTERNAL_IS_CALLTYPE_HELPER_Calltype - -// Note: The use of `identity_t` here allows _Ret to represent return types that -// would normally need to be specified in a different way. For example, a method -// returning a function pointer must be written as -// -// fn_ptr_return_t (*method(method_args_t...))(fn_ptr_args_t...) -// -// But we only support placing the return type at the beginning. To handle this, -// we wrap all calls in identity_t, so that a declaration will be expanded to -// -// identity_t method(method_args_t...) -// -// This allows us to work around the syntactic oddities of function/method -// types. -#define GMOCK_INTERNAL_SIGNATURE(_Ret, _Args) \ - ::testing::internal::identity_t( \ - GMOCK_PP_FOR_EACH(GMOCK_INTERNAL_GET_TYPE, _, _Args)) - -#define GMOCK_INTERNAL_GET_TYPE(_i, _, _elem) \ - GMOCK_PP_COMMA_IF(_i) \ - GMOCK_PP_IF(GMOCK_PP_IS_BEGIN_PARENS(_elem), GMOCK_PP_REMOVE_PARENS, \ - GMOCK_PP_IDENTITY) \ - (_elem) - -#define GMOCK_INTERNAL_PARAMETER(_i, _Signature, _) \ - GMOCK_PP_COMMA_IF(_i) \ - GMOCK_INTERNAL_ARG_O(_i, GMOCK_PP_REMOVE_PARENS(_Signature)) \ - gmock_a##_i - -#define GMOCK_INTERNAL_FORWARD_ARG(_i, _Signature, _) \ - GMOCK_PP_COMMA_IF(_i) \ - ::std::forward(gmock_a##_i) - -#define GMOCK_INTERNAL_MATCHER_PARAMETER(_i, _Signature, _) \ - GMOCK_PP_COMMA_IF(_i) \ - GMOCK_INTERNAL_MATCHER_O(_i, GMOCK_PP_REMOVE_PARENS(_Signature)) \ - gmock_a##_i - -#define GMOCK_INTERNAL_MATCHER_ARGUMENT(_i, _1, _2) \ - GMOCK_PP_COMMA_IF(_i) \ - gmock_a##_i - -#define GMOCK_INTERNAL_A_MATCHER_ARGUMENT(_i, _Signature, _) \ - GMOCK_PP_COMMA_IF(_i) \ - ::testing::A() - -#define GMOCK_INTERNAL_ARG_O(_i, ...) \ - typename ::testing::internal::Function<__VA_ARGS__>::template Arg<_i>::type - -#define GMOCK_INTERNAL_MATCHER_O(_i, ...) \ - const ::testing::Matcher::template Arg<_i>::type>& - -#define MOCK_METHOD0(m, ...) GMOCK_INTERNAL_MOCK_METHODN(, , m, 0, __VA_ARGS__) -#define MOCK_METHOD1(m, ...) GMOCK_INTERNAL_MOCK_METHODN(, , m, 1, __VA_ARGS__) -#define MOCK_METHOD2(m, ...) GMOCK_INTERNAL_MOCK_METHODN(, , m, 2, __VA_ARGS__) -#define MOCK_METHOD3(m, ...) GMOCK_INTERNAL_MOCK_METHODN(, , m, 3, __VA_ARGS__) -#define MOCK_METHOD4(m, ...) GMOCK_INTERNAL_MOCK_METHODN(, , m, 4, __VA_ARGS__) -#define MOCK_METHOD5(m, ...) GMOCK_INTERNAL_MOCK_METHODN(, , m, 5, __VA_ARGS__) -#define MOCK_METHOD6(m, ...) GMOCK_INTERNAL_MOCK_METHODN(, , m, 6, __VA_ARGS__) -#define MOCK_METHOD7(m, ...) GMOCK_INTERNAL_MOCK_METHODN(, , m, 7, __VA_ARGS__) -#define MOCK_METHOD8(m, ...) GMOCK_INTERNAL_MOCK_METHODN(, , m, 8, __VA_ARGS__) -#define MOCK_METHOD9(m, ...) GMOCK_INTERNAL_MOCK_METHODN(, , m, 9, __VA_ARGS__) -#define MOCK_METHOD10(m, ...) \ - GMOCK_INTERNAL_MOCK_METHODN(, , m, 10, __VA_ARGS__) - -#define MOCK_CONST_METHOD0(m, ...) \ - GMOCK_INTERNAL_MOCK_METHODN(const, , m, 0, __VA_ARGS__) -#define MOCK_CONST_METHOD1(m, ...) \ - GMOCK_INTERNAL_MOCK_METHODN(const, , m, 1, __VA_ARGS__) -#define MOCK_CONST_METHOD2(m, ...) \ - GMOCK_INTERNAL_MOCK_METHODN(const, , m, 2, __VA_ARGS__) -#define MOCK_CONST_METHOD3(m, ...) \ - GMOCK_INTERNAL_MOCK_METHODN(const, , m, 3, __VA_ARGS__) -#define MOCK_CONST_METHOD4(m, ...) \ - GMOCK_INTERNAL_MOCK_METHODN(const, , m, 4, __VA_ARGS__) -#define MOCK_CONST_METHOD5(m, ...) \ - GMOCK_INTERNAL_MOCK_METHODN(const, , m, 5, __VA_ARGS__) -#define MOCK_CONST_METHOD6(m, ...) \ - GMOCK_INTERNAL_MOCK_METHODN(const, , m, 6, __VA_ARGS__) -#define MOCK_CONST_METHOD7(m, ...) \ - GMOCK_INTERNAL_MOCK_METHODN(const, , m, 7, __VA_ARGS__) -#define MOCK_CONST_METHOD8(m, ...) \ - GMOCK_INTERNAL_MOCK_METHODN(const, , m, 8, __VA_ARGS__) -#define MOCK_CONST_METHOD9(m, ...) \ - GMOCK_INTERNAL_MOCK_METHODN(const, , m, 9, __VA_ARGS__) -#define MOCK_CONST_METHOD10(m, ...) \ - GMOCK_INTERNAL_MOCK_METHODN(const, , m, 10, __VA_ARGS__) - -#define MOCK_METHOD0_T(m, ...) MOCK_METHOD0(m, __VA_ARGS__) -#define MOCK_METHOD1_T(m, ...) MOCK_METHOD1(m, __VA_ARGS__) -#define MOCK_METHOD2_T(m, ...) MOCK_METHOD2(m, __VA_ARGS__) -#define MOCK_METHOD3_T(m, ...) MOCK_METHOD3(m, __VA_ARGS__) -#define MOCK_METHOD4_T(m, ...) MOCK_METHOD4(m, __VA_ARGS__) -#define MOCK_METHOD5_T(m, ...) MOCK_METHOD5(m, __VA_ARGS__) -#define MOCK_METHOD6_T(m, ...) MOCK_METHOD6(m, __VA_ARGS__) -#define MOCK_METHOD7_T(m, ...) MOCK_METHOD7(m, __VA_ARGS__) -#define MOCK_METHOD8_T(m, ...) MOCK_METHOD8(m, __VA_ARGS__) -#define MOCK_METHOD9_T(m, ...) MOCK_METHOD9(m, __VA_ARGS__) -#define MOCK_METHOD10_T(m, ...) MOCK_METHOD10(m, __VA_ARGS__) - -#define MOCK_CONST_METHOD0_T(m, ...) MOCK_CONST_METHOD0(m, __VA_ARGS__) -#define MOCK_CONST_METHOD1_T(m, ...) MOCK_CONST_METHOD1(m, __VA_ARGS__) -#define MOCK_CONST_METHOD2_T(m, ...) MOCK_CONST_METHOD2(m, __VA_ARGS__) -#define MOCK_CONST_METHOD3_T(m, ...) MOCK_CONST_METHOD3(m, __VA_ARGS__) -#define MOCK_CONST_METHOD4_T(m, ...) MOCK_CONST_METHOD4(m, __VA_ARGS__) -#define MOCK_CONST_METHOD5_T(m, ...) MOCK_CONST_METHOD5(m, __VA_ARGS__) -#define MOCK_CONST_METHOD6_T(m, ...) MOCK_CONST_METHOD6(m, __VA_ARGS__) -#define MOCK_CONST_METHOD7_T(m, ...) MOCK_CONST_METHOD7(m, __VA_ARGS__) -#define MOCK_CONST_METHOD8_T(m, ...) MOCK_CONST_METHOD8(m, __VA_ARGS__) -#define MOCK_CONST_METHOD9_T(m, ...) MOCK_CONST_METHOD9(m, __VA_ARGS__) -#define MOCK_CONST_METHOD10_T(m, ...) MOCK_CONST_METHOD10(m, __VA_ARGS__) - -#define MOCK_METHOD0_WITH_CALLTYPE(ct, m, ...) \ - GMOCK_INTERNAL_MOCK_METHODN(, ct, m, 0, __VA_ARGS__) -#define MOCK_METHOD1_WITH_CALLTYPE(ct, m, ...) \ - GMOCK_INTERNAL_MOCK_METHODN(, ct, m, 1, __VA_ARGS__) -#define MOCK_METHOD2_WITH_CALLTYPE(ct, m, ...) \ - GMOCK_INTERNAL_MOCK_METHODN(, ct, m, 2, __VA_ARGS__) -#define MOCK_METHOD3_WITH_CALLTYPE(ct, m, ...) \ - GMOCK_INTERNAL_MOCK_METHODN(, ct, m, 3, __VA_ARGS__) -#define MOCK_METHOD4_WITH_CALLTYPE(ct, m, ...) \ - GMOCK_INTERNAL_MOCK_METHODN(, ct, m, 4, __VA_ARGS__) -#define MOCK_METHOD5_WITH_CALLTYPE(ct, m, ...) \ - GMOCK_INTERNAL_MOCK_METHODN(, ct, m, 5, __VA_ARGS__) -#define MOCK_METHOD6_WITH_CALLTYPE(ct, m, ...) \ - GMOCK_INTERNAL_MOCK_METHODN(, ct, m, 6, __VA_ARGS__) -#define MOCK_METHOD7_WITH_CALLTYPE(ct, m, ...) \ - GMOCK_INTERNAL_MOCK_METHODN(, ct, m, 7, __VA_ARGS__) -#define MOCK_METHOD8_WITH_CALLTYPE(ct, m, ...) \ - GMOCK_INTERNAL_MOCK_METHODN(, ct, m, 8, __VA_ARGS__) -#define MOCK_METHOD9_WITH_CALLTYPE(ct, m, ...) \ - GMOCK_INTERNAL_MOCK_METHODN(, ct, m, 9, __VA_ARGS__) -#define MOCK_METHOD10_WITH_CALLTYPE(ct, m, ...) \ - GMOCK_INTERNAL_MOCK_METHODN(, ct, m, 10, __VA_ARGS__) - -#define MOCK_CONST_METHOD0_WITH_CALLTYPE(ct, m, ...) \ - GMOCK_INTERNAL_MOCK_METHODN(const, ct, m, 0, __VA_ARGS__) -#define MOCK_CONST_METHOD1_WITH_CALLTYPE(ct, m, ...) \ - GMOCK_INTERNAL_MOCK_METHODN(const, ct, m, 1, __VA_ARGS__) -#define MOCK_CONST_METHOD2_WITH_CALLTYPE(ct, m, ...) \ - GMOCK_INTERNAL_MOCK_METHODN(const, ct, m, 2, __VA_ARGS__) -#define MOCK_CONST_METHOD3_WITH_CALLTYPE(ct, m, ...) \ - GMOCK_INTERNAL_MOCK_METHODN(const, ct, m, 3, __VA_ARGS__) -#define MOCK_CONST_METHOD4_WITH_CALLTYPE(ct, m, ...) \ - GMOCK_INTERNAL_MOCK_METHODN(const, ct, m, 4, __VA_ARGS__) -#define MOCK_CONST_METHOD5_WITH_CALLTYPE(ct, m, ...) \ - GMOCK_INTERNAL_MOCK_METHODN(const, ct, m, 5, __VA_ARGS__) -#define MOCK_CONST_METHOD6_WITH_CALLTYPE(ct, m, ...) \ - GMOCK_INTERNAL_MOCK_METHODN(const, ct, m, 6, __VA_ARGS__) -#define MOCK_CONST_METHOD7_WITH_CALLTYPE(ct, m, ...) \ - GMOCK_INTERNAL_MOCK_METHODN(const, ct, m, 7, __VA_ARGS__) -#define MOCK_CONST_METHOD8_WITH_CALLTYPE(ct, m, ...) \ - GMOCK_INTERNAL_MOCK_METHODN(const, ct, m, 8, __VA_ARGS__) -#define MOCK_CONST_METHOD9_WITH_CALLTYPE(ct, m, ...) \ - GMOCK_INTERNAL_MOCK_METHODN(const, ct, m, 9, __VA_ARGS__) -#define MOCK_CONST_METHOD10_WITH_CALLTYPE(ct, m, ...) \ - GMOCK_INTERNAL_MOCK_METHODN(const, ct, m, 10, __VA_ARGS__) - -#define MOCK_METHOD0_T_WITH_CALLTYPE(ct, m, ...) \ - MOCK_METHOD0_WITH_CALLTYPE(ct, m, __VA_ARGS__) -#define MOCK_METHOD1_T_WITH_CALLTYPE(ct, m, ...) \ - MOCK_METHOD1_WITH_CALLTYPE(ct, m, __VA_ARGS__) -#define MOCK_METHOD2_T_WITH_CALLTYPE(ct, m, ...) \ - MOCK_METHOD2_WITH_CALLTYPE(ct, m, __VA_ARGS__) -#define MOCK_METHOD3_T_WITH_CALLTYPE(ct, m, ...) \ - MOCK_METHOD3_WITH_CALLTYPE(ct, m, __VA_ARGS__) -#define MOCK_METHOD4_T_WITH_CALLTYPE(ct, m, ...) \ - MOCK_METHOD4_WITH_CALLTYPE(ct, m, __VA_ARGS__) -#define MOCK_METHOD5_T_WITH_CALLTYPE(ct, m, ...) \ - MOCK_METHOD5_WITH_CALLTYPE(ct, m, __VA_ARGS__) -#define MOCK_METHOD6_T_WITH_CALLTYPE(ct, m, ...) \ - MOCK_METHOD6_WITH_CALLTYPE(ct, m, __VA_ARGS__) -#define MOCK_METHOD7_T_WITH_CALLTYPE(ct, m, ...) \ - MOCK_METHOD7_WITH_CALLTYPE(ct, m, __VA_ARGS__) -#define MOCK_METHOD8_T_WITH_CALLTYPE(ct, m, ...) \ - MOCK_METHOD8_WITH_CALLTYPE(ct, m, __VA_ARGS__) -#define MOCK_METHOD9_T_WITH_CALLTYPE(ct, m, ...) \ - MOCK_METHOD9_WITH_CALLTYPE(ct, m, __VA_ARGS__) -#define MOCK_METHOD10_T_WITH_CALLTYPE(ct, m, ...) \ - MOCK_METHOD10_WITH_CALLTYPE(ct, m, __VA_ARGS__) - -#define MOCK_CONST_METHOD0_T_WITH_CALLTYPE(ct, m, ...) \ - MOCK_CONST_METHOD0_WITH_CALLTYPE(ct, m, __VA_ARGS__) -#define MOCK_CONST_METHOD1_T_WITH_CALLTYPE(ct, m, ...) \ - MOCK_CONST_METHOD1_WITH_CALLTYPE(ct, m, __VA_ARGS__) -#define MOCK_CONST_METHOD2_T_WITH_CALLTYPE(ct, m, ...) \ - MOCK_CONST_METHOD2_WITH_CALLTYPE(ct, m, __VA_ARGS__) -#define MOCK_CONST_METHOD3_T_WITH_CALLTYPE(ct, m, ...) \ - MOCK_CONST_METHOD3_WITH_CALLTYPE(ct, m, __VA_ARGS__) -#define MOCK_CONST_METHOD4_T_WITH_CALLTYPE(ct, m, ...) \ - MOCK_CONST_METHOD4_WITH_CALLTYPE(ct, m, __VA_ARGS__) -#define MOCK_CONST_METHOD5_T_WITH_CALLTYPE(ct, m, ...) \ - MOCK_CONST_METHOD5_WITH_CALLTYPE(ct, m, __VA_ARGS__) -#define MOCK_CONST_METHOD6_T_WITH_CALLTYPE(ct, m, ...) \ - MOCK_CONST_METHOD6_WITH_CALLTYPE(ct, m, __VA_ARGS__) -#define MOCK_CONST_METHOD7_T_WITH_CALLTYPE(ct, m, ...) \ - MOCK_CONST_METHOD7_WITH_CALLTYPE(ct, m, __VA_ARGS__) -#define MOCK_CONST_METHOD8_T_WITH_CALLTYPE(ct, m, ...) \ - MOCK_CONST_METHOD8_WITH_CALLTYPE(ct, m, __VA_ARGS__) -#define MOCK_CONST_METHOD9_T_WITH_CALLTYPE(ct, m, ...) \ - MOCK_CONST_METHOD9_WITH_CALLTYPE(ct, m, __VA_ARGS__) -#define MOCK_CONST_METHOD10_T_WITH_CALLTYPE(ct, m, ...) \ - MOCK_CONST_METHOD10_WITH_CALLTYPE(ct, m, __VA_ARGS__) - -#define GMOCK_INTERNAL_MOCK_METHODN(constness, ct, Method, args_num, ...) \ - GMOCK_INTERNAL_ASSERT_VALID_SIGNATURE( \ - args_num, ::testing::internal::identity_t<__VA_ARGS__>); \ - GMOCK_INTERNAL_MOCK_METHOD_IMPL( \ - args_num, Method, GMOCK_PP_NARG0(constness), 0, 0, , ct, , \ - (::testing::internal::identity_t<__VA_ARGS__>)) - -#define GMOCK_MOCKER_(arity, constness, Method) \ - GTEST_CONCAT_TOKEN_(gmock##constness##arity##_##Method##_, __LINE__) - -#endif // GOOGLEMOCK_INCLUDE_GMOCK_INTERNAL_GMOCK_FUNCTION_MOCKER_H_ diff --git a/vendor/googletest/googlemock/include/gmock/gmock-matchers.h b/vendor/googletest/googlemock/include/gmock/gmock-matchers.h deleted file mode 100644 index 86be9c176..000000000 --- a/vendor/googletest/googlemock/include/gmock/gmock-matchers.h +++ /dev/null @@ -1,5392 +0,0 @@ -// Copyright 2007, Google Inc. -// All rights reserved. -// -// Redistribution and use in source and binary forms, with or without -// modification, are permitted provided that the following conditions are -// met: -// -// * Redistributions of source code must retain the above copyright -// notice, this list of conditions and the following disclaimer. -// * Redistributions in binary form must reproduce the above -// copyright notice, this list of conditions and the following disclaimer -// in the documentation and/or other materials provided with the -// distribution. -// * Neither the name of Google Inc. nor the names of its -// contributors may be used to endorse or promote products derived from -// this software without specific prior written permission. -// -// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - - -// Google Mock - a framework for writing C++ mock classes. -// -// The MATCHER* family of macros can be used in a namespace scope to -// define custom matchers easily. -// -// Basic Usage -// =========== -// -// The syntax -// -// MATCHER(name, description_string) { statements; } -// -// defines a matcher with the given name that executes the statements, -// which must return a bool to indicate if the match succeeds. Inside -// the statements, you can refer to the value being matched by 'arg', -// and refer to its type by 'arg_type'. -// -// The description string documents what the matcher does, and is used -// to generate the failure message when the match fails. Since a -// MATCHER() is usually defined in a header file shared by multiple -// C++ source files, we require the description to be a C-string -// literal to avoid possible side effects. It can be empty, in which -// case we'll use the sequence of words in the matcher name as the -// description. -// -// For example: -// -// MATCHER(IsEven, "") { return (arg % 2) == 0; } -// -// allows you to write -// -// // Expects mock_foo.Bar(n) to be called where n is even. -// EXPECT_CALL(mock_foo, Bar(IsEven())); -// -// or, -// -// // Verifies that the value of some_expression is even. -// EXPECT_THAT(some_expression, IsEven()); -// -// If the above assertion fails, it will print something like: -// -// Value of: some_expression -// Expected: is even -// Actual: 7 -// -// where the description "is even" is automatically calculated from the -// matcher name IsEven. -// -// Argument Type -// ============= -// -// Note that the type of the value being matched (arg_type) is -// determined by the context in which you use the matcher and is -// supplied to you by the compiler, so you don't need to worry about -// declaring it (nor can you). This allows the matcher to be -// polymorphic. For example, IsEven() can be used to match any type -// where the value of "(arg % 2) == 0" can be implicitly converted to -// a bool. In the "Bar(IsEven())" example above, if method Bar() -// takes an int, 'arg_type' will be int; if it takes an unsigned long, -// 'arg_type' will be unsigned long; and so on. -// -// Parameterizing Matchers -// ======================= -// -// Sometimes you'll want to parameterize the matcher. For that you -// can use another macro: -// -// MATCHER_P(name, param_name, description_string) { statements; } -// -// For example: -// -// MATCHER_P(HasAbsoluteValue, value, "") { return abs(arg) == value; } -// -// will allow you to write: -// -// EXPECT_THAT(Blah("a"), HasAbsoluteValue(n)); -// -// which may lead to this message (assuming n is 10): -// -// Value of: Blah("a") -// Expected: has absolute value 10 -// Actual: -9 -// -// Note that both the matcher description and its parameter are -// printed, making the message human-friendly. -// -// In the matcher definition body, you can write 'foo_type' to -// reference the type of a parameter named 'foo'. For example, in the -// body of MATCHER_P(HasAbsoluteValue, value) above, you can write -// 'value_type' to refer to the type of 'value'. -// -// We also provide MATCHER_P2, MATCHER_P3, ..., up to MATCHER_P$n to -// support multi-parameter matchers. -// -// Describing Parameterized Matchers -// ================================= -// -// The last argument to MATCHER*() is a string-typed expression. The -// expression can reference all of the matcher's parameters and a -// special bool-typed variable named 'negation'. When 'negation' is -// false, the expression should evaluate to the matcher's description; -// otherwise it should evaluate to the description of the negation of -// the matcher. For example, -// -// using testing::PrintToString; -// -// MATCHER_P2(InClosedRange, low, hi, -// std::string(negation ? "is not" : "is") + " in range [" + -// PrintToString(low) + ", " + PrintToString(hi) + "]") { -// return low <= arg && arg <= hi; -// } -// ... -// EXPECT_THAT(3, InClosedRange(4, 6)); -// EXPECT_THAT(3, Not(InClosedRange(2, 4))); -// -// would generate two failures that contain the text: -// -// Expected: is in range [4, 6] -// ... -// Expected: is not in range [2, 4] -// -// If you specify "" as the description, the failure message will -// contain the sequence of words in the matcher name followed by the -// parameter values printed as a tuple. For example, -// -// MATCHER_P2(InClosedRange, low, hi, "") { ... } -// ... -// EXPECT_THAT(3, InClosedRange(4, 6)); -// EXPECT_THAT(3, Not(InClosedRange(2, 4))); -// -// would generate two failures that contain the text: -// -// Expected: in closed range (4, 6) -// ... -// Expected: not (in closed range (2, 4)) -// -// Types of Matcher Parameters -// =========================== -// -// For the purpose of typing, you can view -// -// MATCHER_Pk(Foo, p1, ..., pk, description_string) { ... } -// -// as shorthand for -// -// template -// FooMatcherPk -// Foo(p1_type p1, ..., pk_type pk) { ... } -// -// When you write Foo(v1, ..., vk), the compiler infers the types of -// the parameters v1, ..., and vk for you. If you are not happy with -// the result of the type inference, you can specify the types by -// explicitly instantiating the template, as in Foo(5, -// false). As said earlier, you don't get to (or need to) specify -// 'arg_type' as that's determined by the context in which the matcher -// is used. You can assign the result of expression Foo(p1, ..., pk) -// to a variable of type FooMatcherPk. This -// can be useful when composing matchers. -// -// While you can instantiate a matcher template with reference types, -// passing the parameters by pointer usually makes your code more -// readable. If, however, you still want to pass a parameter by -// reference, be aware that in the failure message generated by the -// matcher you will see the value of the referenced object but not its -// address. -// -// Explaining Match Results -// ======================== -// -// Sometimes the matcher description alone isn't enough to explain why -// the match has failed or succeeded. For example, when expecting a -// long string, it can be very helpful to also print the diff between -// the expected string and the actual one. To achieve that, you can -// optionally stream additional information to a special variable -// named result_listener, whose type is a pointer to class -// MatchResultListener: -// -// MATCHER_P(EqualsLongString, str, "") { -// if (arg == str) return true; -// -// *result_listener << "the difference: " -/// << DiffStrings(str, arg); -// return false; -// } -// -// Overloading Matchers -// ==================== -// -// You can overload matchers with different numbers of parameters: -// -// MATCHER_P(Blah, a, description_string1) { ... } -// MATCHER_P2(Blah, a, b, description_string2) { ... } -// -// Caveats -// ======= -// -// When defining a new matcher, you should also consider implementing -// MatcherInterface or using MakePolymorphicMatcher(). These -// approaches require more work than the MATCHER* macros, but also -// give you more control on the types of the value being matched and -// the matcher parameters, which may leads to better compiler error -// messages when the matcher is used wrong. They also allow -// overloading matchers based on parameter types (as opposed to just -// based on the number of parameters). -// -// MATCHER*() can only be used in a namespace scope as templates cannot be -// declared inside of a local class. -// -// More Information -// ================ -// -// To learn more about using these macros, please search for 'MATCHER' -// on -// https://github.com/google/googletest/blob/master/docs/gmock_cook_book.md -// -// This file also implements some commonly used argument matchers. More -// matchers can be defined by the user implementing the -// MatcherInterface interface if necessary. -// -// See googletest/include/gtest/gtest-matchers.h for the definition of class -// Matcher, class MatcherInterface, and others. - -// GOOGLETEST_CM0002 DO NOT DELETE - -#ifndef GOOGLEMOCK_INCLUDE_GMOCK_GMOCK_MATCHERS_H_ -#define GOOGLEMOCK_INCLUDE_GMOCK_GMOCK_MATCHERS_H_ - -#include -#include -#include -#include -#include -#include -#include // NOLINT -#include -#include -#include -#include -#include - -#include "gmock/internal/gmock-internal-utils.h" -#include "gmock/internal/gmock-port.h" -#include "gmock/internal/gmock-pp.h" -#include "gtest/gtest.h" - -// MSVC warning C5046 is new as of VS2017 version 15.8. -#if defined(_MSC_VER) && _MSC_VER >= 1915 -#define GMOCK_MAYBE_5046_ 5046 -#else -#define GMOCK_MAYBE_5046_ -#endif - -GTEST_DISABLE_MSC_WARNINGS_PUSH_( - 4251 GMOCK_MAYBE_5046_ /* class A needs to have dll-interface to be used by - clients of class B */ - /* Symbol involving type with internal linkage not defined */) - -namespace testing { - -// To implement a matcher Foo for type T, define: -// 1. a class FooMatcherImpl that implements the -// MatcherInterface interface, and -// 2. a factory function that creates a Matcher object from a -// FooMatcherImpl*. -// -// The two-level delegation design makes it possible to allow a user -// to write "v" instead of "Eq(v)" where a Matcher is expected, which -// is impossible if we pass matchers by pointers. It also eases -// ownership management as Matcher objects can now be copied like -// plain values. - -// A match result listener that stores the explanation in a string. -class StringMatchResultListener : public MatchResultListener { - public: - StringMatchResultListener() : MatchResultListener(&ss_) {} - - // Returns the explanation accumulated so far. - std::string str() const { return ss_.str(); } - - // Clears the explanation accumulated so far. - void Clear() { ss_.str(""); } - - private: - ::std::stringstream ss_; - - GTEST_DISALLOW_COPY_AND_ASSIGN_(StringMatchResultListener); -}; - -// Anything inside the 'internal' namespace IS INTERNAL IMPLEMENTATION -// and MUST NOT BE USED IN USER CODE!!! -namespace internal { - -// The MatcherCastImpl class template is a helper for implementing -// MatcherCast(). We need this helper in order to partially -// specialize the implementation of MatcherCast() (C++ allows -// class/struct templates to be partially specialized, but not -// function templates.). - -// This general version is used when MatcherCast()'s argument is a -// polymorphic matcher (i.e. something that can be converted to a -// Matcher but is not one yet; for example, Eq(value)) or a value (for -// example, "hello"). -template -class MatcherCastImpl { - public: - static Matcher Cast(const M& polymorphic_matcher_or_value) { - // M can be a polymorphic matcher, in which case we want to use - // its conversion operator to create Matcher. Or it can be a value - // that should be passed to the Matcher's constructor. - // - // We can't call Matcher(polymorphic_matcher_or_value) when M is a - // polymorphic matcher because it'll be ambiguous if T has an implicit - // constructor from M (this usually happens when T has an implicit - // constructor from any type). - // - // It won't work to unconditionally implicit_cast - // polymorphic_matcher_or_value to Matcher because it won't trigger - // a user-defined conversion from M to T if one exists (assuming M is - // a value). - return CastImpl(polymorphic_matcher_or_value, - std::is_convertible>{}, - std::is_convertible{}); - } - - private: - template - static Matcher CastImpl(const M& polymorphic_matcher_or_value, - std::true_type /* convertible_to_matcher */, - std::integral_constant) { - // M is implicitly convertible to Matcher, which means that either - // M is a polymorphic matcher or Matcher has an implicit constructor - // from M. In both cases using the implicit conversion will produce a - // matcher. - // - // Even if T has an implicit constructor from M, it won't be called because - // creating Matcher would require a chain of two user-defined conversions - // (first to create T from M and then to create Matcher from T). - return polymorphic_matcher_or_value; - } - - // M can't be implicitly converted to Matcher, so M isn't a polymorphic - // matcher. It's a value of a type implicitly convertible to T. Use direct - // initialization to create a matcher. - static Matcher CastImpl(const M& value, - std::false_type /* convertible_to_matcher */, - std::true_type /* convertible_to_T */) { - return Matcher(ImplicitCast_(value)); - } - - // M can't be implicitly converted to either Matcher or T. Attempt to use - // polymorphic matcher Eq(value) in this case. - // - // Note that we first attempt to perform an implicit cast on the value and - // only fall back to the polymorphic Eq() matcher afterwards because the - // latter calls bool operator==(const Lhs& lhs, const Rhs& rhs) in the end - // which might be undefined even when Rhs is implicitly convertible to Lhs - // (e.g. std::pair vs. std::pair). - // - // We don't define this method inline as we need the declaration of Eq(). - static Matcher CastImpl(const M& value, - std::false_type /* convertible_to_matcher */, - std::false_type /* convertible_to_T */); -}; - -// This more specialized version is used when MatcherCast()'s argument -// is already a Matcher. This only compiles when type T can be -// statically converted to type U. -template -class MatcherCastImpl > { - public: - static Matcher Cast(const Matcher& source_matcher) { - return Matcher(new Impl(source_matcher)); - } - - private: - class Impl : public MatcherInterface { - public: - explicit Impl(const Matcher& source_matcher) - : source_matcher_(source_matcher) {} - - // We delegate the matching logic to the source matcher. - bool MatchAndExplain(T x, MatchResultListener* listener) const override { - using FromType = typename std::remove_cv::type>::type>::type; - using ToType = typename std::remove_cv::type>::type>::type; - // Do not allow implicitly converting base*/& to derived*/&. - static_assert( - // Do not trigger if only one of them is a pointer. That implies a - // regular conversion and not a down_cast. - (std::is_pointer::type>::value != - std::is_pointer::type>::value) || - std::is_same::value || - !std::is_base_of::value, - "Can't implicitly convert from to "); - - // Do the cast to `U` explicitly if necessary. - // Otherwise, let implicit conversions do the trick. - using CastType = - typename std::conditional::value, - T&, U>::type; - - return source_matcher_.MatchAndExplain(static_cast(x), - listener); - } - - void DescribeTo(::std::ostream* os) const override { - source_matcher_.DescribeTo(os); - } - - void DescribeNegationTo(::std::ostream* os) const override { - source_matcher_.DescribeNegationTo(os); - } - - private: - const Matcher source_matcher_; - }; -}; - -// This even more specialized version is used for efficiently casting -// a matcher to its own type. -template -class MatcherCastImpl > { - public: - static Matcher Cast(const Matcher& matcher) { return matcher; } -}; - -// Template specialization for parameterless Matcher. -template -class MatcherBaseImpl { - public: - MatcherBaseImpl() = default; - - template - operator ::testing::Matcher() const { // NOLINT(runtime/explicit) - return ::testing::Matcher(new - typename Derived::template gmock_Impl()); - } -}; - -// Template specialization for Matcher with parameters. -template