|
6 | 6 | #include <iris/requirements.hpp> |
7 | 7 | #include <iris/compare.hpp> |
8 | 8 | #include <iris/fixed_string.hpp> |
| 9 | +#include <iris/exception.hpp> |
9 | 10 |
|
10 | 11 | #include <concepts> |
11 | 12 | #include <utility> |
@@ -452,4 +453,73 @@ TEST_CASE("fixed_string") |
452 | 453 | CHECK((std::ranges::equal(str, "foobar"sv))); |
453 | 454 | } |
454 | 455 |
|
| 456 | + |
| 457 | +#define IRIS_TEST_THROWF(expected, E, ...) \ |
| 458 | + CHECK_THROWS_AS( \ |
| 459 | + ([]{ \ |
| 460 | + try { \ |
| 461 | + iris::throwf<E>(__VA_ARGS__); \ |
| 462 | + } catch (std::exception const& e) { \ |
| 463 | + CHECK(e.what() == std::string_view{expected}); \ |
| 464 | + throw; \ |
| 465 | + } \ |
| 466 | + })(), \ |
| 467 | + E \ |
| 468 | + ) |
| 469 | + |
| 470 | +TEST_CASE("throwf") |
| 471 | +{ |
| 472 | + class my_exception : public std::runtime_error |
| 473 | + { |
| 474 | + public: |
| 475 | + my_exception(std::string const& name, std::string const& message) |
| 476 | + : runtime_error(name + ": " + message) |
| 477 | + {} |
| 478 | + }; |
| 479 | + |
| 480 | + IRIS_TEST_THROWF("foo", std::runtime_error, "foo"); |
| 481 | + IRIS_TEST_THROWF("foo", std::runtime_error, std::string{"foo"}); |
| 482 | + IRIS_TEST_THROWF("foo", std::runtime_error, std::string{"foo"}.c_str()); |
| 483 | + IRIS_TEST_THROWF("foo", std::runtime_error, std::string_view{"foo"}); |
| 484 | + IRIS_TEST_THROWF("{}", std::runtime_error, "{}"); |
| 485 | + IRIS_TEST_THROWF("42", std::runtime_error, "{}", 42); |
| 486 | + IRIS_TEST_THROWF("foo", std::runtime_error, "{}", "foo"); |
| 487 | + IRIS_TEST_THROWF("foo", std::runtime_error, "{}", std::string{"foo"}); |
| 488 | + IRIS_TEST_THROWF("foo", std::runtime_error, "{}", std::string{"foo"}.c_str()); |
| 489 | + IRIS_TEST_THROWF("foo", std::runtime_error, "{}", std::string_view{"foo"}); |
| 490 | + |
| 491 | + IRIS_TEST_THROWF("{}: bar", my_exception, "{}", "bar"); |
| 492 | + IRIS_TEST_THROWF("foo: bar", my_exception, "foo", "bar"); |
| 493 | + |
| 494 | + // constructible with argument |
| 495 | + IRIS_TEST_THROWF("foobar", std::runtime_error, "foobar"); |
| 496 | + |
| 497 | + // constructible with format string |
| 498 | + IRIS_TEST_THROWF("33 - 4", std::runtime_error, "{} - {}", 33, 4); |
| 499 | + |
| 500 | + CHECK_THROWS_AS( |
| 501 | + ([]{ |
| 502 | + try { |
| 503 | + iris::throwf<std::system_error>(std::make_error_code(std::errc::invalid_argument), "{}", 42); |
| 504 | + } catch (std::system_error const& e) { |
| 505 | + CHECK(e.code() == std::make_error_code(std::errc::invalid_argument)); |
| 506 | + throw; |
| 507 | + } |
| 508 | + })(), |
| 509 | + std::system_error |
| 510 | + ); |
| 511 | + |
| 512 | + CHECK_THROWS_AS( |
| 513 | + ([]{ |
| 514 | + try { |
| 515 | + iris::throwf<std::system_error>(33 - 4, std::generic_category(), "{}", 42); |
| 516 | + } catch (std::system_error const& e) { |
| 517 | + CHECK((e.code() == std::error_code{33 - 4, std::generic_category()})); |
| 518 | + throw; |
| 519 | + } |
| 520 | + })(), |
| 521 | + std::system_error |
| 522 | + ); |
| 523 | +} |
| 524 | + |
455 | 525 | } // unit_test |
0 commit comments