diff options
| -rw-r--r-- | CMakeLists.txt | 5 | ||||
| -rw-r--r-- | CONTRIBUTING.rst | 9 | ||||
| -rw-r--r-- | cspell.json | 1 | ||||
| -rw-r--r-- | kernel/CMakeLists.txt | 4 | ||||
| -rw-r--r-- | kernel/kernel/test_support/string_makers.hpp | 132 | ||||
| -rw-r--r-- | kernel/kernel/test_support/string_makers.tests.cpp | 59 | ||||
| -rw-r--r-- | libs/kstd/CMakeLists.txt | 4 | ||||
| -rw-r--r-- | libs/kstd/kstd/test_support/os_panic.tests.cpp | 2 | ||||
| -rw-r--r-- | libs/kstd/kstd/test_support/string_makers.hpp | 180 | ||||
| -rw-r--r-- | libs/kstd/kstd/test_support/string_makers.tests.cpp | 94 |
10 files changed, 488 insertions, 2 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt index 1323efc3..a6950a3b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -32,7 +32,10 @@ if (BUILD_TESTING) FetchContent_MakeAvailable("Catch2") - add_compile_definitions("CATCH_CONFIG_NO_COUNTER") + add_compile_definitions( + "CATCH_CONFIG_NO_COUNTER" + "CATCH_CONFIG_ENABLE_ALL_STRINGMAKERS" + ) endif() #[============================================================================[ diff --git a/CONTRIBUTING.rst b/CONTRIBUTING.rst index 2dbd613c..dabc2552 100644 --- a/CONTRIBUTING.rst +++ b/CONTRIBUTING.rst @@ -54,6 +54,15 @@ For thread-safety and race condition analysis under ThreadSanitizer: cmake --build --preset bht-stress-dbg ctest --preset bht-stress-dbg +Printing Values in Test Failures +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +Catch2 prints values it cannot convert as ``{?}``. +To avoid that, every test translation unit force-includes ``kstd/test_support/string_makers.hpp`` (kstd tests) or ``kernel/test_support/string_makers.hpp`` (kernel tests), which teach Catch2 how to print our types without touching kernel code. +To make a new type printable in tests, add a ``Catch::StringMaker`` specialization to the ``string_makers.hpp`` of the library the type belongs to. +Do not add or change anything in kernel code (e.g. a ``kstd::formatter``) just to make a type printable in tests. +Types that already have a ``kstd::formatter`` are printed through it automatically. + Bootable Kernel Targets ~~~~~~~~~~~~~~~~~~~~~~~ diff --git a/cspell.json b/cspell.json index e288dfaf..94a85529 100644 --- a/cspell.json +++ b/cspell.json @@ -20,6 +20,7 @@ "hhdm", "idtr", "initializable", + "insertable", "interprocedural", "invlpg", "invokable", diff --git a/kernel/CMakeLists.txt b/kernel/CMakeLists.txt index 341f396a..2ed3ac62 100644 --- a/kernel/CMakeLists.txt +++ b/kernel/CMakeLists.txt @@ -184,6 +184,10 @@ if(BUILD_TESTING) ${KERNEL_TEST_SUPPORT_SOURCES} ) + target_compile_options("${TEST_TARGET}" PRIVATE + "$<$<CXX_COMPILER_ID:Clang,GNU>:SHELL:-include ${CMAKE_CURRENT_SOURCE_DIR}/kernel/test_support/string_makers.hpp>" + ) + target_compile_definitions("${TEST_TARGET}" PRIVATE KERNEL_TEST_ASSETS_DIR="${CMAKE_CURRENT_SOURCE_DIR}/kernel/test_support/vfs/test_assets" ) diff --git a/kernel/kernel/test_support/string_makers.hpp b/kernel/kernel/test_support/string_makers.hpp new file mode 100644 index 00000000..c158ac3a --- /dev/null +++ b/kernel/kernel/test_support/string_makers.hpp @@ -0,0 +1,132 @@ +#ifndef TEACHOS_KERNEL_TEST_SUPPORT_STRING_MAKERS_HPP +#define TEACHOS_KERNEL_TEST_SUPPORT_STRING_MAKERS_HPP + +//! Catch2 string conversions for kapi and kernel types. +//! +//! This header is force-included into every kernel test translation unit by the build system and must therefore never +//! be included by kernel code. See kstd/test_support/string_makers.hpp for the conversions of kstd types. + +#include <kapi/devices.hpp> +#include <kapi/filesystem.hpp> +#include <kapi/memory.hpp> + +#include <kstd/test_support/string_makers.hpp> // IWYU pragma: export + +#include <catch2/catch_tostring.hpp> + +#include <string> + +namespace Catch +{ + + template<> + struct StringMaker<kapi::filesystem::device_number> + { + auto static convert(kapi::filesystem::device_number number) -> std::string + { + return std::to_string(number.major) + ':' + std::to_string(number.minor); + } + }; + + template<> + struct StringMaker<kapi::filesystem::directory_cursor> + { + auto static convert(kapi::filesystem::directory_cursor cursor) -> std::string + { + return "directory_cursor{" + std::to_string(cursor.value) + '}'; + } + }; + + template<> + struct StringMaker<kapi::filesystem::file_status> + { + auto static convert(kapi::filesystem::file_status const & status) -> std::string + { + return "file_status{mode: " + Catch::Detail::stringify(status.mode) + + ", size: " + Catch::Detail::stringify(status.size) + + ", raw_device: " + Catch::Detail::stringify(status.raw_device) + + ", inode_number: " + Catch::Detail::stringify(status.inode_number) + + ", link_count: " + Catch::Detail::stringify(status.link_count) + '}'; + } + }; + + template<> + struct StringMaker<kapi::memory::frame> + { + auto static convert(kapi::memory::frame frame) -> std::string + { + return "frame{" + std::to_string(frame.number()) + '}'; + } + }; + + template<> + struct StringMaker<kapi::memory::page> + { + auto static convert(kapi::memory::page page) -> std::string + { + return "page{" + std::to_string(page.number()) + '}'; + } + }; + + template<> + struct StringMaker<kapi::devices::mmio_range> + { + auto static convert(kapi::devices::mmio_range const & range) -> std::string + { + return "mmio{" + Catch::Detail::stringify(range.start) + ", " + Catch::Detail::stringify(range.size) + '}'; + } + }; + + template<> + struct StringMaker<kapi::devices::io_port> + { + auto static convert(kapi::devices::io_port const & port) -> std::string + { + return "port{" + std::to_string(port.start) + ", " + std::to_string(port.count) + '}'; + } + }; + + template<> + struct StringMaker<kapi::devices::interrupt_line> + { + auto static convert(kapi::devices::interrupt_line const & line) -> std::string + { + return "irq{" + std::to_string(line.number) + '}'; + } + }; + + template<> + struct StringMaker<kapi::devices::dma_channel> + { + auto static convert(kapi::devices::dma_channel const & channel) -> std::string + { + return "dma{" + std::to_string(channel.number) + '}'; + } + }; + + template<> + struct StringMaker<kapi::devices::resource> + { + auto static convert(kapi::devices::resource const & resource) -> std::string + { + using kapi::devices::resource_type; + + switch (resource.type) + { + case resource_type::mmio: + return Catch::Detail::stringify(resource.get<resource_type::mmio>()); + case resource_type::port: + return Catch::Detail::stringify(resource.get<resource_type::port>()); + case resource_type::irq: + return Catch::Detail::stringify(resource.get<resource_type::irq>()); + case resource_type::dma: + return Catch::Detail::stringify(resource.get<resource_type::dma>()); + } + + return "resource{<unknown>}"; + } + }; + +} // namespace Catch + +#endif diff --git a/kernel/kernel/test_support/string_makers.tests.cpp b/kernel/kernel/test_support/string_makers.tests.cpp new file mode 100644 index 00000000..3c3cd791 --- /dev/null +++ b/kernel/kernel/test_support/string_makers.tests.cpp @@ -0,0 +1,59 @@ +#include <kapi/devices.hpp> +#include <kapi/filesystem.hpp> +#include <kapi/memory.hpp> + +#include <kstd/units.hpp> + +#include <catch2/catch_test_macros.hpp> +#include <catch2/catch_tostring.hpp> + +using namespace kstd::units_literals; + +SCENARIO("Catch2 string conversion of kapi types", "[test_support][string_makers]") +{ + GIVEN("filesystem types") + { + auto const number = kapi::filesystem::device_number{.major = 1, .minor = 2}; + auto const cursor = kapi::filesystem::directory_cursor{3}; + auto const status = + kapi::filesystem::file_status{.mode = 4, .size = 5_B, .raw_device = number, .inode_number = 6, .link_count = 7}; + + THEN("they are printed with their fields") + { + REQUIRE(Catch::Detail::stringify(number) == "1:2"); + REQUIRE(Catch::Detail::stringify(cursor) == "directory_cursor{3}"); + REQUIRE(Catch::Detail::stringify(status) == + "file_status{mode: 4, size: 5, raw_device: 1:2, inode_number: 6, link_count: 7}"); + } + } + + GIVEN("memory chunks") + { + THEN("they are printed with their number") + { + REQUIRE(Catch::Detail::stringify(kapi::memory::frame{8}) == "frame{8}"); + REQUIRE(Catch::Detail::stringify(kapi::memory::page{9}) == "page{9}"); + } + } + + GIVEN("device resources") + { + auto const port = kapi::devices::io_port{.start = 0x60, .count = 2}; + auto const line = kapi::devices::interrupt_line{.number = 1}; + auto const channel = kapi::devices::dma_channel{.number = 3}; + + THEN("they are printed with their kind and fields") + { + REQUIRE(Catch::Detail::stringify(port) == "port{96, 2}"); + REQUIRE(Catch::Detail::stringify(line) == "irq{1}"); + REQUIRE(Catch::Detail::stringify(channel) == "dma{3}"); + } + + THEN("a resource is printed like the value it holds") + { + REQUIRE(Catch::Detail::stringify(kapi::devices::resource{port}) == "port{96, 2}"); + REQUIRE(Catch::Detail::stringify(kapi::devices::resource{line}) == "irq{1}"); + REQUIRE(Catch::Detail::stringify(kapi::devices::resource{channel}) == "dma{3}"); + } + } +} diff --git a/libs/kstd/CMakeLists.txt b/libs/kstd/CMakeLists.txt index 1902d0d9..c6e02223 100644 --- a/libs/kstd/CMakeLists.txt +++ b/libs/kstd/CMakeLists.txt @@ -83,6 +83,10 @@ if(BUILD_TESTING) teachos_add_tests("kstd") + target_compile_options("kstd_tests" PRIVATE + "$<$<CXX_COMPILER_ID:Clang,GNU>:SHELL:-include ${CMAKE_CURRENT_SOURCE_DIR}/kstd/test_support/string_makers.hpp>" + ) + if(COMMAND "enable_coverage" AND KSTD_ENABLE_TEST_COVERAGE) enable_coverage("kstd") enable_coverage("kstd_tests") diff --git a/libs/kstd/kstd/test_support/os_panic.tests.cpp b/libs/kstd/kstd/test_support/os_panic.tests.cpp index c30411aa..17af74a7 100644 --- a/libs/kstd/kstd/test_support/os_panic.tests.cpp +++ b/libs/kstd/kstd/test_support/os_panic.tests.cpp @@ -7,7 +7,7 @@ namespace kstd::os { - auto panic(std::string_view message, std::source_location location) + auto panic(std::string_view message, std::source_location location) -> void { throw kstd::tests::os_panic{std::string{message}, location}; } diff --git a/libs/kstd/kstd/test_support/string_makers.hpp b/libs/kstd/kstd/test_support/string_makers.hpp new file mode 100644 index 00000000..03a7f8c8 --- /dev/null +++ b/libs/kstd/kstd/test_support/string_makers.hpp @@ -0,0 +1,180 @@ +#ifndef KSTD_TEST_SUPPORT_STRING_MAKERS_HPP +#define KSTD_TEST_SUPPORT_STRING_MAKERS_HPP + +//! Catch2 string conversions for kstd types. +//! +//! Catch2 prints values it does not know how to convert as "{?}". This header teaches Catch2 how to print kstd types, +//! so that failed assertions show the actual values. It is force-included into every test translation unit by the +//! build system and must therefore never be included by kernel code. +//! +//! Types that already have a kstd::formatter specialization are printed via kstd::format. All other types get a +//! Catch::StringMaker specialization here, or in the test support header of the library they belong to. Kernel code +//! must never be changed just to make a type printable in tests. + +#include <kstd/format.hpp> +#include <kstd/memory.hpp> +#include <kstd/string.hpp> +#include <kstd/system_error.hpp> + +#include <catch2/catch_tostring.hpp> + +#include <concepts> +#include <expected> +#include <ostream> +#include <ranges> +#include <string> +#include <type_traits> + +namespace kstd::tests +{ + + //! A type that can be printed via kstd::format. + template<typename T> + concept kstd_formattable = std::default_initializable<kstd::formatter<std::remove_cvref_t<T>>>; + + //! A type that can be written to a std::ostream. + template<typename T> + concept stream_insertable = requires(std::ostream & stream, T const & value) { stream << value; }; + + //! A type that Catch2 can not print on its own, but which is printable via kstd::format. + //! + //! Types that are stream insertable, pointers, arrays, and ranges are handled by Catch2 already. They are excluded + //! before checking for a formatter, since probing the formatter of a range with unformattable elements is a hard + //! error. + template<typename T> + concept catch_printable_via_kstd = + !std::ranges::range<T> && !Catch::is_range<T>::value && !std::is_pointer_v<T> && !std::is_array_v<T> && + !std::is_arithmetic_v<T> && !stream_insertable<T> && kstd_formattable<T>; + + //! Convert a kstd string to a std::string. + auto inline to_std_string(kstd::string const & string) -> std::string + { + return std::string{string.data(), string.size()}; + } + + //! Convert an error code or error condition to its textual representation, e.g. "generic:22:invalid argument". + template<typename ErrorType> + auto error_to_string(ErrorType const & error) -> std::string + { + return std::string{error.category().name()} + ':' + std::to_string(error.value()) + ':' + + std::string{error.message()}; + } + + //! Convert a smart pointer to the same textual representation Catch2 uses for raw pointers. + template<typename Pointer> + auto pointer_to_string(Pointer const & pointer) -> std::string + { + return Catch::Detail::stringify(pointer.get()); + } + +} // namespace kstd::tests + +namespace Catch +{ + + template<kstd::tests::catch_printable_via_kstd T> + struct StringMaker<T> + { + auto static convert(T const & value) -> std::string + { + return kstd::tests::to_std_string(kstd::format("{}", value)); + } + }; + + template<> + struct StringMaker<kstd::string> + { + auto static convert(kstd::string const & value) -> std::string + { + return Catch::Detail::stringify(kstd::tests::to_std_string(value)); + } + }; + + template<> + struct StringMaker<kstd::error_category> + { + auto static convert(kstd::error_category const & category) -> std::string + { + return std::string{category.name()}; + } + }; + + template<> + struct StringMaker<kstd::error_condition> + { + auto static convert(kstd::error_condition const & condition) -> std::string + { + return kstd::tests::error_to_string(condition); + } + }; + + template<kstd::error_code_enum Enum> + struct StringMaker<Enum> + { + auto static convert(Enum value) -> std::string + { + return kstd::tests::error_to_string(kstd::error_code{value}); + } + }; + + template<kstd::error_condition_enum Enum> + requires(!kstd::error_code_enum<Enum>) + struct StringMaker<Enum> + { + auto static convert(Enum value) -> std::string + { + return kstd::tests::error_to_string(make_error_condition(value)); + } + }; + + template<typename T> + struct StringMaker<kstd::shared_ptr<T>> + { + auto static convert(kstd::shared_ptr<T> const & pointer) -> std::string + { + return kstd::tests::pointer_to_string(pointer); + } + }; + + template<typename T> + struct StringMaker<kstd::unique_ptr<T>> + { + auto static convert(kstd::unique_ptr<T> const & pointer) -> std::string + { + return kstd::tests::pointer_to_string(pointer); + } + }; + + template<typename T> + struct StringMaker<kstd::observer_ptr<T>> + { + auto static convert(kstd::observer_ptr<T> const & pointer) -> std::string + { + return kstd::tests::pointer_to_string(pointer); + } + }; + + template<typename T, typename E> + struct StringMaker<std::expected<T, E>> + { + auto static convert(std::expected<T, E> const & expected) -> std::string + { + if (!expected) + { + return "error(" + Catch::Detail::stringify(expected.error()) + ')'; + } + + if constexpr (std::is_void_v<T>) + { + return "value()"; + } + else + { + return "value(" + Catch::Detail::stringify(*expected) + ')'; + } + } + }; + +} // namespace Catch + +#endif diff --git a/libs/kstd/kstd/test_support/string_makers.tests.cpp b/libs/kstd/kstd/test_support/string_makers.tests.cpp new file mode 100644 index 00000000..887bec37 --- /dev/null +++ b/libs/kstd/kstd/test_support/string_makers.tests.cpp @@ -0,0 +1,94 @@ +#include <kstd/test_support/string_makers.hpp> + +#include <kstd/memory.hpp> +#include <kstd/result.hpp> +#include <kstd/string.hpp> +#include <kstd/system_error.hpp> +#include <kstd/units.hpp> + +#include <catch2/catch_test_macros.hpp> +#include <catch2/catch_tostring.hpp> + +#include <compare> +#include <expected> + +using namespace kstd::units_literals; + +SCENARIO("Catch2 string conversion of kstd types", "[test_support][string_makers]") +{ + GIVEN("a kstd string") + { + auto const string = kstd::string{"hello"}; + + THEN("it is printed like a std::string") + { + REQUIRE(Catch::Detail::stringify(string) == "\"hello\""); + } + } + + GIVEN("a type with a kstd formatter") + { + THEN("it is printed via kstd::format") + { + REQUIRE(Catch::Detail::stringify(42_B) == "42"); + REQUIRE(Catch::Detail::stringify(std::strong_ordering::less) == "less"); + } + } + + GIVEN("an error code") + { + auto const error = kstd::make_error_code(kstd::errc::invalid_argument); + + THEN("it is printed with its category, value, and message") + { + REQUIRE(Catch::Detail::stringify(error) == kstd::tests::error_to_string(error)); + REQUIRE(Catch::Detail::stringify(error).starts_with(std::string{error.category().name()} + ':')); + } + } + + GIVEN("an error condition enumerator") + { + THEN("it is printed like the matching error condition") + { + REQUIRE(Catch::Detail::stringify(kstd::errc::invalid_argument) == + kstd::tests::error_to_string(kstd::make_error_condition(kstd::errc::invalid_argument))); + } + } + + GIVEN("a successful result") + { + auto const result = kstd::result<int>{42}; + auto const void_result = kstd::result<void>{}; + + THEN("it is printed as value(...)") + { + REQUIRE(Catch::Detail::stringify(result) == "value(42)"); + REQUIRE(Catch::Detail::stringify(void_result) == "value()"); + } + } + + GIVEN("a failed result") + { + auto const error = kstd::make_error_code(kstd::errc::invalid_argument); + auto const result = kstd::result<int>{std::unexpect, error}; + + THEN("it is printed as error(...)") + { + REQUIRE(Catch::Detail::stringify(result) == "error(" + Catch::Detail::stringify(error) + ')'); + } + } + + GIVEN("smart pointers") + { + auto const empty = kstd::shared_ptr<int>{}; + auto const shared = kstd::make_shared<int>(1); + auto const observer = kstd::observer_ptr<int>{shared.get()}; + + THEN("they are printed like raw pointers") + { + REQUIRE(Catch::Detail::stringify(empty) == "nullptr"); + REQUIRE(Catch::Detail::stringify(shared) == Catch::Detail::stringify(shared.get())); + REQUIRE(Catch::Detail::stringify(observer) == Catch::Detail::stringify(shared.get())); + } + } +} |
