|
| 1 | +#include "pch.h" |
| 2 | + |
| 3 | +using namespace winrt; |
| 4 | +using namespace Windows::Foundation; |
| 5 | + |
| 6 | +namespace |
| 7 | +{ |
| 8 | + static bool s_loggerCalled = false; |
| 9 | + |
| 10 | + // Note that we are checking that the source line number matches expectations. If lines above this are changed |
| 11 | + // then this value needs to change as well. |
| 12 | + void FailOnLine15() |
| 13 | + { |
| 14 | + // Validate that handler translated exception |
| 15 | + REQUIRE_THROWS_AS(check_hresult(0x80000018), hresult_illegal_delegate_assignment); |
| 16 | + } |
| 17 | + |
| 18 | + void __stdcall logger(uint32_t lineNumber, char const* fileName, char const* functionName, void* returnAddress, winrt::hresult const result) noexcept |
| 19 | + { |
| 20 | + // In C++20 these fields should be filled in by std::source_location |
| 21 | + REQUIRE(lineNumber == 15); |
| 22 | + const auto fileNameSv = std::string_view(fileName); |
| 23 | + REQUIRE(!fileNameSv.empty()); |
| 24 | + REQUIRE(fileNameSv.find("custom_error.cpp") != std::string::npos); |
| 25 | + const auto functionNameSv = std::string_view(functionName); |
| 26 | + REQUIRE(!functionNameSv.empty()); |
| 27 | + REQUIRE(functionNameSv == "FailOnLine15"); |
| 28 | + |
| 29 | + REQUIRE(returnAddress); |
| 30 | + REQUIRE(result == 0x80000018); // E_ILLEGAL_DELEGATE_ASSIGNMENT) |
| 31 | + s_loggerCalled = true; |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +TEST_CASE("custom_error_logger") |
| 36 | +{ |
| 37 | + // Set up global handler |
| 38 | + REQUIRE(!s_loggerCalled); |
| 39 | + REQUIRE(!winrt_throw_hresult_handler); |
| 40 | + winrt_throw_hresult_handler = logger; |
| 41 | + |
| 42 | + FailOnLine15(); |
| 43 | + REQUIRE(s_loggerCalled); |
| 44 | + |
| 45 | + // Remove global handler |
| 46 | + winrt_throw_hresult_handler = nullptr; |
| 47 | + s_loggerCalled = false; |
| 48 | +} |
0 commit comments