From 38d7045685d4904d013ea4990a3aa7f5e78309cf Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Sat, 22 Oct 2022 17:57:47 +0100 Subject: Add magic support to printing/parsing/validating enumerations --- test/test-enumDetails.cpp | 50 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 test/test-enumDetails.cpp (limited to 'test/test-enumDetails.cpp') diff --git a/test/test-enumDetails.cpp b/test/test-enumDetails.cpp new file mode 100644 index 0000000..c803cf8 --- /dev/null +++ b/test/test-enumDetails.cpp @@ -0,0 +1,50 @@ +#define BOOST_TEST_MODULE test_enumDetails + +#include "enumDetailsData.hpp" +#include +#include +#include +#include + +constexpr std::array INVALID_NAMES {"", "missing", "GlobalScoped::aa", "GlobalScoped", "ns::aa", "a", "bb"}; +constexpr std::array VALID_NAMES {"aa", "b", "c"}; +template constexpr std::array VALID_VALUES {E::aa, E::b, E::c}; +// Not a template, else Boost test framework throws printing the context +constexpr std::array INVALID_VALUES {-1, 3, 20}; + +#define TESTS_FOR_TYPE(TYPE) \ + BOOST_DATA_TEST_CASE(invalid_check_##TYPE, INVALID_VALUES, in) \ + { \ + BOOST_CHECK(!EnumDetails::is_valid(static_cast(in))); \ + } \ + BOOST_DATA_TEST_CASE(invalid_parse_##TYPE, INVALID_NAMES, in) \ + { \ + BOOST_CHECK(!EnumDetails::parse(in).has_value()); \ + } \ + BOOST_DATA_TEST_CASE(invalid_to_string_##TYPE, INVALID_VALUES, in) \ + { \ + BOOST_CHECK(!EnumDetails::to_string(static_cast(in)).has_value()); \ + } \ + BOOST_DATA_TEST_CASE(valid_check_##TYPE, VALID_VALUES, in) \ + { \ + BOOST_CHECK(EnumDetails::is_valid(in)); \ + } \ + BOOST_DATA_TEST_CASE(valid_parse_##TYPE, VALID_NAMES ^ VALID_VALUES, in, out) \ + { \ + const auto v = EnumDetails::parse(in); \ + BOOST_REQUIRE(v.has_value()); \ + BOOST_CHECK_EQUAL(v.value(), out); \ + } \ + BOOST_DATA_TEST_CASE(valid_to_string_##TYPE, VALID_VALUES ^ VALID_NAMES, in, out) \ + { \ + const auto v = EnumDetails::to_string(in); \ + BOOST_CHECK(v.has_value()); \ + BOOST_CHECK_EQUAL(v.value(), out); \ + } + +TESTS_FOR_TYPE(GlobalScoped) +TESTS_FOR_TYPE(GlobalUnscoped) +using ns_unscoped = ns::Unscoped; +using ns_scoped = ns::Scoped; +TESTS_FOR_TYPE(ns_unscoped) +TESTS_FOR_TYPE(ns_scoped) -- cgit v1.2.3