diff --git a/include/indentlog/print/function.hpp b/include/indentlog/print/function.hpp index 0e5584f..2689ef1 100644 --- a/include/indentlog/print/function.hpp +++ b/include/indentlog/print/function.hpp @@ -24,6 +24,18 @@ namespace xo { simple }; + inline std::ostream & + operator<< (std::ostream & os, function_style x) { + switch(x) { + case function_style::literal: os << "literal"; break; + case function_style::pretty: os << "pretty"; break; + case function_style::streamlined: os << "streamlined"; break; + case function_style::simple: os << "simple"; break; + default: os << "???"; break; + } + return os; + } /*operator<<*/ + /* Tag to drive header-only expression */ template class function_name_impl { diff --git a/utest/CMakeLists.txt b/utest/CMakeLists.txt index 1513f8b..c6e6e59 100644 --- a/utest/CMakeLists.txt +++ b/utest/CMakeLists.txt @@ -3,7 +3,7 @@ set(SELF_EXECUTABLE_NAME utest.indentlog) set(SELF_SOURCE_FILES fixed.test.cpp quoted.test.cpp vector.test.cpp array.test.cpp timeutil.test.cpp tag.test.cpp - filename.test.cpp code_location.test.cpp + filename.test.cpp code_location.test.cpp function.test.cpp indentlog_utest_main.cpp) add_executable(${SELF_EXECUTABLE_NAME} ${SELF_SOURCE_FILES}) diff --git a/utest/function.test.cpp b/utest/function.test.cpp new file mode 100644 index 0000000..323a3f3 --- /dev/null +++ b/utest/function.test.cpp @@ -0,0 +1,59 @@ +/* @file function.test.cpp */ + +#include "indentlog/print/function.hpp" +#include "indentlog/print/tag.hpp" +#include +#include + +using namespace xo; + +namespace ut { + struct function_tcase { + function_tcase() = default; + function_tcase(function_style style, color_spec_type spec, std::string_view pretty, std::string_view output) + : style_{style}, spec_{spec}, pretty_{pretty}, output_{output} {} + + /* function style: literal|pretty|streamlined|simple*/ + function_style style_; + /* color spec for output */ + color_spec_type spec_; + /* function signature (as per __PRETTY_FUNCTION__) */ + std::string_view pretty_; + /* output text */ + std::string_view output_; + }; /*function_tcase*/ + + std::vector s_function_tcase_v( + { + function_tcase(function_style::pretty, color_spec_type::none(), "void foo() const", "[void foo() const]"), + function_tcase(function_style::streamlined, color_spec_type::none(), "void foo() const", "foo"), + function_tcase(function_style::simple, color_spec_type::none(), "void foo() const", "foo"), + + function_tcase(function_style::pretty, color_spec_type::none(), "void xo::class::foo() const", "[void xo::class::foo() const]"), + function_tcase(function_style::streamlined, color_spec_type::none(), "void xo::class::foo() const", "class::foo"), + function_tcase(function_style::simple, color_spec_type::none(), "void xo::class::foo() const", "foo"), + + function_tcase(function_style::pretty, color_spec_type::blue(), "void xo::class::foo() const", "[\033[31;34mvoid xo::class::foo() const\033[0m]"), + }); + + TEST_CASE("function", "[function]") { + tag_config::tag_color = color_spec_type::none(); + + for (std::uint32_t i_tc = 0, z_tc = s_function_tcase_v.size(); i_tc < z_tc; ++i_tc) { + function_tcase const & tc = s_function_tcase_v[i_tc]; + + INFO(tostr(xtag("i_tc", i_tc), xtag("style", tc.style_), xtag("spec", tc.spec_), xtag("pretty", tc.pretty_))); + + std::stringstream ss; + ss << function_name(tc.style_, tc.spec_, tc.pretty_); + + INFO(xtag("ss.str", ss.str())); + + REQUIRE(ss.str() == tc.output_); + } + + REQUIRE(s_function_tcase_v.size() > 1); + } +} /*namespace ut*/ + +/* end function.test.cpp */