forked from TartanLlama/expected
* Add assertions for cases where the API documents undefined behaviour. This affects operator->, operator*, and error() on a tl::expected instance. The performance impact is likely negligible for typical uses. refs #113 Signed-off-by: Daira Hopwood <daira@jacaranda.org> * Add tests for assertions. Signed-off-by: Daira Hopwood <daira@jacaranda.org> * Add documentation for assertion behaviour. Signed-off-by: Daira Hopwood <daira@jacaranda.org> --------- Signed-off-by: Daira Hopwood <daira@jacaranda.org>
19 lines
543 B
C++
19 lines
543 B
C++
#include <catch2/catch.hpp>
|
|
#include <stdexcept>
|
|
|
|
#define TL_ASSERT(cond) if (!(cond)) { throw std::runtime_error(std::string("assertion failure")); }
|
|
|
|
#include <tl/expected.hpp>
|
|
|
|
TEST_CASE("Assertions", "[assertions]") {
|
|
tl::expected<int,int> o1 = 42;
|
|
REQUIRE_THROWS_WITH(o1.error(), "assertion failure");
|
|
|
|
tl::expected<int,int> o2 {tl::unexpect, 0};
|
|
REQUIRE_THROWS_WITH(*o2, "assertion failure");
|
|
|
|
struct foo { int bar; };
|
|
tl::expected<struct foo,int> o3 {tl::unexpect, 0};
|
|
REQUIRE_THROWS_WITH(o3->bar, "assertion failure");
|
|
}
|