diff options
Diffstat (limited to 'nihil/tests')
| -rw-r--r-- | nihil/tests/CMakeLists.txt | 1 | ||||
| -rw-r--r-- | nihil/tests/error.cc | 99 | ||||
| -rw-r--r-- | nihil/tests/fd.cc | 3 | ||||
| -rw-r--r-- | nihil/tests/generic_error.cc | 32 |
4 files changed, 68 insertions, 67 deletions
diff --git a/nihil/tests/CMakeLists.txt b/nihil/tests/CMakeLists.txt index e3e7c06..1c4635f 100644 --- a/nihil/tests/CMakeLists.txt +++ b/nihil/tests/CMakeLists.txt @@ -6,7 +6,6 @@ add_executable(nihil.test error.cc fd.cc generator.cc - generic_error.cc getenv.cc guard.cc monad.cc diff --git a/nihil/tests/error.cc b/nihil/tests/error.cc index 7b079fd..31124e3 100644 --- a/nihil/tests/error.cc +++ b/nihil/tests/error.cc @@ -24,67 +24,77 @@ TEST_CASE("error: invariants", "[nihil]") TEST_CASE("error: construct from string", "[nihil]") { - auto e = nihil::error("an error"); - REQUIRE(e.str() == e.what()); - REQUIRE(e.what() == "an error"); - REQUIRE(std::format("{}", e) == e.what()); + using namespace nihil; + + auto e = error("an error"); + REQUIRE(e.str() == to_string(e)); + REQUIRE(to_string(e) == "an error"); + REQUIRE(std::format("{}", e) == to_string(e)); } TEST_CASE("error: construct from std::error_condition", "[nihil]") { + using namespace nihil; + auto code = std::make_error_condition(std::errc::invalid_argument); - auto e = nihil::error(code); + auto e = error(code); - REQUIRE(e.cause().has_value() == false); + REQUIRE(!e.cause()); REQUIRE(e.code().has_value() == false); REQUIRE(e.condition().has_value() == true); REQUIRE(e == std::errc::invalid_argument); REQUIRE(e != std::errc::no_such_file_or_directory); - REQUIRE(e.str() == e.what()); - REQUIRE(e.what() == std::strerror(EINVAL)); - REQUIRE(std::format("{}", e) == e.what()); + REQUIRE(e.str() == to_string(e)); + REQUIRE(to_string(e) == std::strerror(EINVAL)); + REQUIRE(std::format("{}", e) == to_string(e)); } TEST_CASE("error: construct from std::errc", "[nihil]") { - auto e = nihil::error(std::errc::invalid_argument); + using namespace nihil; - REQUIRE(e.cause().has_value() == false); + auto e = error(std::errc::invalid_argument); + + REQUIRE(!e.cause()); REQUIRE(e.code().has_value() == false); REQUIRE(e.condition().has_value() == true); REQUIRE(e == std::errc::invalid_argument); REQUIRE(e != std::errc::no_such_file_or_directory); - REQUIRE(e.str() == e.what()); - REQUIRE(e.what() == std::strerror(EINVAL)); - REQUIRE(std::format("{}", e) == e.what()); + REQUIRE(e.str() == to_string(e)); + REQUIRE(to_string(e) == std::strerror(EINVAL)); + REQUIRE(std::format("{}", e) == to_string(e)); } TEST_CASE("error: compound error", "[nihil]") { using namespace std::literals; + using namespace nihil; - auto e = nihil::error("cannot open file", - nihil::error(std::errc::no_such_file_or_directory)); + auto e = error("cannot open file", + error(std::errc::no_such_file_or_directory)); - REQUIRE(e.cause().has_value() == true); + REQUIRE(e.cause()); REQUIRE(e.code().has_value() == false); REQUIRE(e.condition().has_value() == false); - REQUIRE(e.cause() == std::errc::no_such_file_or_directory); + REQUIRE(*e.cause() == std::errc::no_such_file_or_directory); REQUIRE(e.str() == "cannot open file"); - REQUIRE(e.what() == ("cannot open file: "s + std::strerror(ENOENT))); - REQUIRE(std::format("{}", e) == e.what()); + REQUIRE(to_string(e) == ("cannot open file: "s + + std::strerror(ENOENT))); + REQUIRE(std::format("{}", e) == to_string(e)); } TEST_CASE("error: operator== with strings", "[nihil]") { - auto e1 = nihil::error("error"); - auto e2 = nihil::error("error"); - auto e3 = nihil::error("an error"); + using namespace nihil; + + auto e1 = error("error"); + auto e2 = error("error"); + auto e3 = error("an error"); REQUIRE(e1 == e2); REQUIRE(e1 != e3); @@ -92,25 +102,31 @@ TEST_CASE("error: operator== with strings", "[nihil]") TEST_CASE("error: operator< with strings", "[nihil]") { - auto e1 = nihil::error("aaa"); - auto e2 = nihil::error("zzz"); + using namespace nihil; + + auto e1 = error("aaa"); + auto e2 = error("zzz"); REQUIRE(e1 < e2); } TEST_CASE("error: operator== with a cause", "[nihil]") { - auto e1 = nihil::error("error", nihil::error("cause 1")); - auto e2 = nihil::error("error", nihil::error("cause 2")); + using namespace nihil; + + auto e1 = error("error", error("cause 1")); + auto e2 = error("error", error("cause 2")); REQUIRE(e1 == e2); } TEST_CASE("error: operator== with error_conditions", "[nihil]") { - auto e1 = nihil::error(std::errc::invalid_argument); - auto e2 = nihil::error(std::errc::invalid_argument); - auto e3 = nihil::error(std::errc::permission_denied); + using namespace nihil; + + auto e1 = error(std::errc::invalid_argument); + auto e2 = error(std::errc::invalid_argument); + auto e3 = error(std::errc::permission_denied); REQUIRE(e1 == e2); REQUIRE(e1 != e3); @@ -118,19 +134,36 @@ TEST_CASE("error: operator== with error_conditions", "[nihil]") TEST_CASE("error: std::format with string", "[nihil]") { - auto err = nihil::error("an error"); + using namespace nihil; + + auto err = error("an error"); REQUIRE(std::format("{}", err) == "an error"); } TEST_CASE("error: std::format with std::errc", "[nihil]") { - auto err = nihil::error(std::errc::invalid_argument); + using namespace nihil; + + auto err = error(std::errc::invalid_argument); REQUIRE(std::format("{}", err) == std::strerror(EINVAL)); } TEST_CASE("error: std::format with cause", "[nihil]") { - auto err = nihil::error("an error", std::errc::invalid_argument); + using namespace nihil; + + auto err = error("an error", std::errc::invalid_argument); REQUIRE(std::format("{}", err) == "an error: Invalid argument"); } +TEST_CASE("error: throw and catch", "[nihil]") +{ + using namespace std::literals; + using namespace nihil; + + try { + throw error("oh no", error(std::errc::invalid_argument)); + } catch (std::exception const &exc) { + REQUIRE(exc.what() == "oh no: Invalid argument"s); + } +} diff --git a/nihil/tests/fd.cc b/nihil/tests/fd.cc index d8acd35..59054fa 100644 --- a/nihil/tests/fd.cc +++ b/nihil/tests/fd.cc @@ -3,6 +3,7 @@ */ #include <span> +#include <stdexcept> #include <stdio.h> #include <fcntl.h> @@ -27,7 +28,7 @@ TEST_CASE("fd: construct empty", "[fd]") { nihil::fd fd; REQUIRE(!fd); - REQUIRE_THROWS_AS(fd.get(), nihil::fd_logic_error); + REQUIRE_THROWS_AS(fd.get(), std::logic_error); } TEST_CASE("fd: construct from fd", "[fd]") { diff --git a/nihil/tests/generic_error.cc b/nihil/tests/generic_error.cc deleted file mode 100644 index ee3eccd..0000000 --- a/nihil/tests/generic_error.cc +++ /dev/null @@ -1,32 +0,0 @@ -/* - * This source code is released into the public domain. - */ - -#include <catch2/catch_test_macros.hpp> - -import nihil; - -TEST_CASE("generic_error: basic", "[generic_error]") { - using namespace std::literals; - - // C string - try { - throw nihil::generic_error("test error"); - } catch (nihil::generic_error const &exc) { - REQUIRE(exc.what() == "test error"sv); - } - - // std::string - try { - throw nihil::generic_error("test error"s); - } catch (nihil::generic_error const &exc) { - REQUIRE(exc.what() == "test error"sv); - } - - // std::string_view - try { - throw nihil::generic_error("test error"sv); - } catch (nihil::generic_error const &exc) { - REQUIRE(exc.what() == "test error"sv); - } -} |
