| // SPDX-License-Identifier: Apache-2.0 | |
| // SPDX-FileCopyrightText: See the NOTICE file distributed with this work for additional information regarding copyright ownership | |
| // SPDX-FileContributor: Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements | |
| #pragma once | |
| #include <chrono> | |
| #include <expected> | |
| #include <optional> | |
| #include <string> | |
| #include "fmt/format.h" | |
| #include "catch2/catch_test_macros.hpp" | |
| #include "catch2/catch_tostring.hpp" | |
| #include "catch2/matchers/catch_matchers.hpp" | |
| namespace Catch { | |
| template<typename T> | |
| struct StringMaker<std::optional<T>> { | |
| static std::string convert(const std::optional<T>& val) { | |
| if (val) { | |
| return fmt::format("std::optional({})", StringMaker<T>::convert(val.value())); | |
| } | |
| return "std::nullopt"; | |
| } | |
| }; | |
| template<> | |
| struct StringMaker<std::nullopt_t> { | |
| static std::string convert(const std::nullopt_t& /*val*/) { | |
| return "std::nullopt"; | |
| } | |
| }; | |
| template <> | |
| struct StringMaker<std::error_code> { | |
| static std::string convert(const std::error_code& error_code) { | |
| return fmt::format("std::error_code(category:{}, value:{}, message:{})", error_code.category().name(), error_code.value(), error_code.message()); | |
| } | |
| }; | |
| template <> | |
| struct StringMaker<std::chrono::file_clock::duration> { | |
| static std::string convert(const std::chrono::file_clock::duration& duration) { | |
| return fmt::format("{} {} s", duration.count(), Catch::ratio_string<std::chrono::file_clock::duration::period>::symbol()); | |
| } | |
| }; | |
| template <typename T, typename E> | |
| struct StringMaker<std::expected<T, E>> { | |
| static std::string convert(const std::expected<T, E>& expected) { | |
| if (!expected) { | |
| return fmt::format("unexpected({})", Catch::Detail::stringify(expected.error())); | |
| } | |
| if constexpr (!std::is_void_v<T>) { | |
| return fmt::format("expected({})", Catch::Detail::stringify(*expected)); | |
| } | |
| return "expected(void)"; | |
| } | |
| }; | |
| } // namespace Catch | |
| namespace org::apache::nifi::minifi::test { | |
| struct MatchesSuccess : Catch::Matchers::MatcherBase<std::error_code> { | |
| MatchesSuccess() = default; | |
| bool match(const std::error_code& err) const override { | |
| return err.value() == 0; | |
| } | |
| std::string describe() const override { | |
| return fmt::format("== {}", Catch::StringMaker<std::error_code>::convert(std::error_code{})); | |
| } | |
| }; | |
| struct MatchesError : Catch::Matchers::MatcherBase<std::error_code> { | |
| explicit MatchesError(std::optional<std::error_code> expected_error = std::nullopt) | |
| : Catch::Matchers::MatcherBase<std::error_code>(), | |
| expected_error_(expected_error) { | |
| } | |
| bool match(const std::error_code& err) const override { | |
| if (expected_error_) | |
| return err.value() == expected_error_->value(); | |
| return err.value() != 0; | |
| } | |
| std::string describe() const override { | |
| if (expected_error_) | |
| return fmt::format("== {}", Catch::StringMaker<std::error_code>::convert(*expected_error_)); | |
| return fmt::format("!= {}", Catch::StringMaker<std::error_code>::convert(std::error_code{})); | |
| } | |
| private: | |
| std::optional<std::error_code> expected_error_; | |
| }; | |
| } // namespace org::apache::nifi::minifi::test |