SFSExceptionMatcher.h (2079B)
1 // Copyright (c) Microsoft Corporation. 2 // Licensed under the MIT License. 3 4 #pragma once 5 6 #include "Result.h" 7 #include "SFSException.h" 8 9 #include <catch2/catch_test_macros.hpp> 10 #include <catch2/matchers/catch_matchers_templated.hpp> 11 12 #include <optional> 13 #include <string> 14 15 #define REQUIRE_THROWS_CODE(call, code) \ 16 REQUIRE_THROWS_MATCHES(call, SFS::details::SFSException, SFS::test::SFSExceptionMatcher(SFS::Result::code)) 17 #define REQUIRE_THROWS_CODE_MSG(call, code, message) \ 18 REQUIRE_THROWS_MATCHES(call, SFS::details::SFSException, SFS::test::SFSExceptionMatcher(SFS::Result::code, message)) 19 #define REQUIRE_THROWS_CODE_MSG_MATCHES(call, code, matcher) \ 20 REQUIRE_THROWS_MATCHES(call, \ 21 SFS::details::SFSException, \ 22 SFS::test::SFSExceptionGenericMatcher(SFS::Result::code, matcher)) 23 24 namespace SFS::test 25 { 26 struct SFSExceptionMatcher : Catch::Matchers::MatcherGenericBase 27 { 28 SFSExceptionMatcher(const Result::Code& code); 29 SFSExceptionMatcher(const Result::Code& code, std::string message); 30 31 bool match(const SFS::details::SFSException& other) const; 32 std::string describe() const override; 33 34 private: 35 Result::Code m_code{Result::Success}; 36 std::optional<std::string> m_message; 37 }; 38 39 template <typename Matcher> 40 struct SFSExceptionGenericMatcher : Catch::Matchers::MatcherGenericBase 41 { 42 SFSExceptionGenericMatcher(const Result::Code& code, Matcher messageMatcher); 43 44 bool match(const SFS::details::SFSException& other) const; 45 std::string describe() const override; 46 47 private: 48 Result::Code m_code{Result::Success}; 49 Matcher m_messageMatcher; 50 }; 51 } // namespace SFS::test