-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathpanic.cpp
More file actions
47 lines (38 loc) · 1.18 KB
/
panic.cpp
File metadata and controls
47 lines (38 loc) · 1.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <stdx/ct_string.hpp>
#include <stdx/panic.hpp>
#include <catch2/catch_test_macros.hpp>
#include <cstdint>
#include <string_view>
namespace {
int runtime_calls{};
int compile_time_calls{};
struct injected_handler {
template <typename Why, typename... Ts>
static auto panic(Why why, Ts &&...) noexcept -> void {
CHECK(std::string_view{why} == "uh-oh");
++runtime_calls;
}
template <stdx::ct_string Why, typename... Ts>
static auto panic(Ts &&...) noexcept -> void {
STATIC_REQUIRE(std::string_view{Why} == "uh-oh");
++compile_time_calls;
}
};
} // namespace
template <> inline auto stdx::panic_handler<> = injected_handler{};
TEST_CASE("panic called with runtime arguments", "[panic]") {
runtime_calls = 0;
stdx::panic("uh-oh");
CHECK(runtime_calls == 1);
}
TEST_CASE("panic called with compile-time strings", "[panic]") {
compile_time_calls = 0;
using namespace stdx::ct_string_literals;
stdx::panic<"uh-oh"_cts>();
CHECK(compile_time_calls == 1);
}
TEST_CASE("compile-time panic called through macro", "[panic]") {
compile_time_calls = 0;
STDX_PANIC("uh-oh");
CHECK(compile_time_calls == 1);
}