From 5b772dde13a4833851cd0f42a2f99e505766f826 Mon Sep 17 00:00:00 2001 From: John Beard Date: Tue, 13 Aug 2024 10:02:08 +0100 Subject: [PATCH] QA: Use modern Boost test print customisation point Since Boost 1.64, you can use the boost_test_print_type customisation point to provide test printing for types. Move all test printing functions to this, and scrap the fiddly Boost version handling to deal with older Boosts (KiCad is now at minver 1.71). --- .../include/qa_utils/geometry/geometry.h | 5 +- .../qa_utils/wx_utils/unit_test_utils.h | 102 +++++------------- qa/qa_utils/wx_utils/unit_test_utils.cpp | 10 +- qa/tests/common/test_array_options.cpp | 4 +- qa/tests/common/test_coroutine.cpp | 4 +- qa/tests/common/wximage_test_utils.cpp | 7 +- qa/tests/common/wximage_test_utils.h | 10 +- qa/tests/eeschema/lib_field_test_utils.h | 24 ++--- qa/tests/libs/kimath/CMakeLists.txt | 1 + .../libs/kimath/geometry/geom_test_utils.cpp | 17 +++ .../libs/kimath/geometry/geom_test_utils.h | 20 +--- qa/tests/libs/sexpr/sexpr_test_utils.h | 17 ++- qa/tests/pcbnew/drc/drc_test_utils.cpp | 2 +- qa/tests/pcbnew/drc/drc_test_utils.h | 9 +- 14 files changed, 73 insertions(+), 159 deletions(-) create mode 100644 qa/tests/libs/kimath/geometry/geom_test_utils.cpp diff --git a/qa/qa_utils/include/qa_utils/geometry/geometry.h b/qa/qa_utils/include/qa_utils/geometry/geometry.h index 98aa605c0e..95d4324ec0 100644 --- a/qa/qa_utils/include/qa_utils/geometry/geometry.h +++ b/qa/qa_utils/include/qa_utils/geometry/geometry.h @@ -33,10 +33,9 @@ /** * Define a stream function for logging this type. - * - * TODO: convert to boost_test_print_type when Boost minver > 1.64 */ -inline std::ostream& operator<<( std::ostream& os, const BOX2I& aBox ) +template +std::ostream& boost_test_print_type( std::ostream& os, const BOX2& aBox ) { os << "BOX[ " << aBox.GetOrigin() << " + " << aBox.GetSize() << " ]"; return os; diff --git a/qa/qa_utils/include/qa_utils/wx_utils/unit_test_utils.h b/qa/qa_utils/include/qa_utils/wx_utils/unit_test_utils.h index a951010400..0ade7e2aa5 100644 --- a/qa/qa_utils/include/qa_utils/wx_utils/unit_test_utils.h +++ b/qa/qa_utils/include/qa_utils/wx_utils/unit_test_utils.h @@ -38,35 +38,6 @@ #include -/* - * Boost hides the configuration point for print_log_value in different - * namespaces between < 1.59 and >= 1.59. - * - * The macros can be used to open and close the right level of namespacing - * based on the version. - * - * We could just use a conditionally defined namespace alias, but that - * doesn't work in GCC <7 (GCC bug #56480) - * - * From Boost 1.64, this should be done with boost_test_print_type, - * and these defines can be removed once all logging functions use that. - */ -#if BOOST_VERSION >= 105900 -#define BOOST_TEST_PRINT_NAMESPACE_OPEN \ - boost \ - { \ - namespace test_tools \ - { \ - namespace tt_detail -#define BOOST_TEST_PRINT_NAMESPACE_CLOSE }} -#else -#define BOOST_TEST_PRINT_NAMESPACE_OPEN \ - boost \ - { \ - namespace test_tools -#define BOOST_TEST_PRINT_NAMESPACE_CLOSE } -#endif - template struct PRINTABLE_OPT @@ -114,79 +85,62 @@ inline bool operator!=( const PRINTABLE_OPT& aLhs, const PRINTABLE_OPT& aR } -namespace BOOST_TEST_PRINT_NAMESPACE_OPEN +// boost_test_print_type has to be in the same namespace as the printed type +namespace std { /** * Boost print helper for generic vectors */ template -struct print_log_value> +std::ostream& boost_test_print_type( std::ostream& os, std::vector const& aVec ) { - inline void operator()( std::ostream& os, std::vector const& aVec ) - { - os << "std::vector size " << aVec.size() << " ["; + os << "std::vector size " << aVec.size() << " ["; - for( const auto& i : aVec ) - { - os << "\n "; - print_log_value()( os, i ); - } - - os << "]"; + for( const auto& i : aVec ) + { + os << "\n " << i; } -}; + + os << "]"; + return os; +} /** * Boost print helper for generic maps */ template -struct print_log_value> +std::ostream& boost_test_print_type( std::ostream& os, std::map const& aMap ) { - inline void operator()( std::ostream& os, std::map const& aMap ) - { - os << "std::map size " << aMap.size() << " ["; + os << "std::map size " << aMap.size() << " ["; - for( const auto& [key, value] : aMap ) - { - os << "\n "; - print_log_value()( os, key ); - os << " = "; - print_log_value()( os, value ); - } - - os << "]"; + for( const auto& [key, value] : aMap ) + { + os << "\n " << key << " = " << value; } -}; + + os << "]"; + return os; +} /** * Boost print helper for generic pairs */ template -struct print_log_value> +std::ostream& boost_test_print_type( std::ostream& os, std::pair const& aPair ) { - inline void operator()( std::ostream& os, std::pair const& aPair ) - { - os << "["; - print_log_value()( os, aPair.first ); - os << ", "; - print_log_value()( os, aPair.second ); - os << "]"; - } -}; + os << "[" << aPair.first << ", " << aPair.second << "]"; + return os; +} + +} // namespace std + /** * Boost print helper for wxPoint. Note operator<< for this type doesn't * exist in non-DEBUG builds. */ -template <> -struct print_log_value -{ - void operator()( std::ostream& os, wxPoint const& aVec ); -}; - -} -BOOST_TEST_PRINT_NAMESPACE_CLOSE +std::ostream& boost_test_print_type( std::ostream& os, wxPoint const& aVec ); namespace KI_TEST diff --git a/qa/qa_utils/wx_utils/unit_test_utils.cpp b/qa/qa_utils/wx_utils/unit_test_utils.cpp index 1dc0c57509..4663975af5 100644 --- a/qa/qa_utils/wx_utils/unit_test_utils.cpp +++ b/qa/qa_utils/wx_utils/unit_test_utils.cpp @@ -23,18 +23,12 @@ #include -namespace BOOST_TEST_PRINT_NAMESPACE_OPEN -{ - -void print_log_value::operator()( std::ostream& os, wxPoint const& aPt ) +std::ostream& boost_test_print_type( std::ostream& os, wxPoint const& aPt ) { os << "WXPOINT[ x=\"" << aPt.x << "\" y=\"" << aPt.y << "\" ]"; + return os; } -} -BOOST_TEST_PRINT_NAMESPACE_CLOSE - - #ifndef QA_EESCHEMA_DATA_LOCATION #define QA_EESCHEMA_DATA_LOCATION "???" diff --git a/qa/tests/common/test_array_options.cpp b/qa/tests/common/test_array_options.cpp index 7c4d57f25c..a4c6ae64cb 100644 --- a/qa/tests/common/test_array_options.cpp +++ b/qa/tests/common/test_array_options.cpp @@ -36,10 +36,8 @@ /** * Define a stream function for logging this type. - * - * TODO: convert to boost_test_print_type when Boost minver > 1.64 */ -std::ostream& operator<<( std::ostream& os, const ARRAY_OPTIONS::TRANSFORM& aObj ) +std::ostream& boost_test_print_type( std::ostream& os, const ARRAY_OPTIONS::TRANSFORM& aObj ) { os << "TRANSFORM[ " << aObj.m_offset << " r " << aObj.m_rotation.AsDegrees() << "deg" << " ]"; diff --git a/qa/tests/common/test_coroutine.cpp b/qa/tests/common/test_coroutine.cpp index 6dd082eb75..94e7a0a6cd 100644 --- a/qa/tests/common/test_coroutine.cpp +++ b/qa/tests/common/test_coroutine.cpp @@ -67,10 +67,8 @@ struct COROUTINE_TEST_EVENT /** * Define a stream function for logging this type. - * - * TODO: convert to boost_test_print_type when Boost minver > 1.64 */ -std::ostream& operator<<( std::ostream& os, const COROUTINE_TEST_EVENT& aObj ) +std::ostream& boost_test_print_type( std::ostream& os, const COROUTINE_TEST_EVENT& aObj ) { os << "COROUTINE_TEST_EVENT[ type: " << (int) aObj.m_type << ", value: " << aObj.m_value << " ]"; diff --git a/qa/tests/common/wximage_test_utils.cpp b/qa/tests/common/wximage_test_utils.cpp index 48bdf2a175..53c08711c4 100644 --- a/qa/tests/common/wximage_test_utils.cpp +++ b/qa/tests/common/wximage_test_utils.cpp @@ -67,12 +67,9 @@ bool IsImagePixelOfColor( const wxImage& aImage, int aX, int aY, const KIGFX::CO } // namespace KI_TEST -namespace BOOST_TEST_PRINT_NAMESPACE_OPEN -{ -void print_log_value::operator()( std::ostream& os, wxImage const& aImage ) +std::ostream& boost_test_print_type( std::ostream& os, wxImage const& aImage ) { const wxSize size = aImage.GetSize(); os << "wxImage[" << size.x << "x" << size.y << "]"; + return os; } -} // namespace BOOST_TEST_PRINT_NAMESPACE_OPEN -BOOST_TEST_PRINT_NAMESPACE_CLOSE diff --git a/qa/tests/common/wximage_test_utils.h b/qa/tests/common/wximage_test_utils.h index d05551eb30..f60adca8f8 100644 --- a/qa/tests/common/wximage_test_utils.h +++ b/qa/tests/common/wximage_test_utils.h @@ -46,14 +46,6 @@ bool IsImagePixelOfColor( const wxImage& aImage, int aX, int aY, const KIGFX::CO } // namespace KI_TEST -namespace BOOST_TEST_PRINT_NAMESPACE_OPEN -{ -template <> -struct print_log_value -{ - void operator()( std::ostream& os, wxImage const& aImage ); -}; -} // namespace BOOST_TEST_PRINT_NAMESPACE_OPEN -BOOST_TEST_PRINT_NAMESPACE_CLOSE +std::ostream& boost_test_print_type( std::ostream& os, wxImage const& aImage ); #endif diff --git a/qa/tests/eeschema/lib_field_test_utils.h b/qa/tests/eeschema/lib_field_test_utils.h index 59c290206b..a2b3f9b313 100644 --- a/qa/tests/eeschema/lib_field_test_utils.h +++ b/qa/tests/eeschema/lib_field_test_utils.h @@ -35,27 +35,17 @@ #include -namespace BOOST_TEST_PRINT_NAMESPACE_OPEN +std::ostream& boost_test_print_type( std::ostream& os, SCH_FIELD const& f ) { -template <> -struct print_log_value -{ - inline void operator()( std::ostream& os, SCH_FIELD const& f ) - { - os << "SCH_FIELD[ " << f.GetCanonicalName() << " ]"; - } -}; + os << "SCH_FIELD[ " << f.GetCanonicalName() << " ]"; + return os; +} -template <> -struct print_log_value> +std::ostream& boost_test_print_type( std::ostream& os, std::vector const& f ) { - inline void operator()( std::ostream& os, std::vector const& f ) - { - os << "SCH_FIELDS[ " << f.size() << " ]"; - } -}; + os << "SCH_FIELDS[ " << f.size() << " ]"; + return os; } -BOOST_TEST_PRINT_NAMESPACE_CLOSE namespace KI_TEST diff --git a/qa/tests/libs/kimath/CMakeLists.txt b/qa/tests/libs/kimath/CMakeLists.txt index 03223b48ad..b05ac88851 100644 --- a/qa/tests/libs/kimath/CMakeLists.txt +++ b/qa/tests/libs/kimath/CMakeLists.txt @@ -27,6 +27,7 @@ set( QA_KIMATH_SRCS test_kimath.cpp + geometry/geom_test_utils.cpp geometry/test_chamfer.cpp geometry/test_distribute.cpp geometry/test_dogbone.cpp diff --git a/qa/tests/libs/kimath/geometry/geom_test_utils.cpp b/qa/tests/libs/kimath/geometry/geom_test_utils.cpp new file mode 100644 index 0000000000..748db7da53 --- /dev/null +++ b/qa/tests/libs/kimath/geometry/geom_test_utils.cpp @@ -0,0 +1,17 @@ + + +#include "geom_test_utils.h" + + +std::ostream& boost_test_print_type( std::ostream& os, const SHAPE_LINE_CHAIN& c ) +{ + os << "SHAPE_LINE_CHAIN: " << c.PointCount() << " points: [\n"; + + for( int i = 0; i < c.PointCount(); ++i ) + { + os << " " << i << ": " << c.CPoint( i ) << "\n"; + } + + os << "]"; + return os; +} diff --git a/qa/tests/libs/kimath/geometry/geom_test_utils.h b/qa/tests/libs/kimath/geometry/geom_test_utils.h index ddae2bc166..278177cbaf 100644 --- a/qa/tests/libs/kimath/geometry/geom_test_utils.h +++ b/qa/tests/libs/kimath/geometry/geom_test_utils.h @@ -347,26 +347,10 @@ inline bool SegmentsHaveSameEndPoints( const SEG& aSeg1, const SEG& aSeg2 ) } // namespace GEOM_TEST -namespace BOOST_TEST_PRINT_NAMESPACE_OPEN -{ -template <> -struct print_log_value -{ - inline void operator()( std::ostream& os, const SHAPE_LINE_CHAIN& c ) - { - os << "SHAPE_LINE_CHAIN: " << c.PointCount() << " points: [\n"; - for( int i = 0; i < c.PointCount(); ++i ) - { - os << " " << i << ": " << c.CPoint( i ) << "\n"; - } +// Stream printing for geometry types - os << "]"; - } -}; - -} -BOOST_TEST_PRINT_NAMESPACE_CLOSE +std::ostream& boost_test_print_type( std::ostream& os, const SHAPE_LINE_CHAIN& c ); #endif // GEOM_TEST_UTILS_H diff --git a/qa/tests/libs/sexpr/sexpr_test_utils.h b/qa/tests/libs/sexpr/sexpr_test_utils.h index 730957edb8..7c4273f1fc 100644 --- a/qa/tests/libs/sexpr/sexpr_test_utils.h +++ b/qa/tests/libs/sexpr/sexpr_test_utils.h @@ -224,21 +224,18 @@ inline bool SexprConvertsToString( const SEXPR::SEXPR& aSexpr, const std::string } // namespace KI_TEST -namespace BOOST_TEST_PRINT_NAMESPACE_OPEN -{ +namespace SEXPR +{ /** * Boost print helper for SEXPR objects */ -template <> -struct print_log_value +inline std::ostream& boost_test_print_type( std::ostream& os, const SEXPR& aSexpr ) { - inline void operator()( std::ostream& os, const SEXPR::SEXPR& aSexpr ) - { - os << "SEXPR [ " << KI_TEST::GetSexprDebugType( aSexpr ) << " ]\n " << aSexpr.AsString(); - } -}; + os << "SEXPR [ " << KI_TEST::GetSexprDebugType( aSexpr ) << " ]\n " << aSexpr.AsString(); + return os; } -BOOST_TEST_PRINT_NAMESPACE_CLOSE + +} // namespace SEXPR #endif // TEST_SEXPR_TEST_UTILS__H \ No newline at end of file diff --git a/qa/tests/pcbnew/drc/drc_test_utils.cpp b/qa/tests/pcbnew/drc/drc_test_utils.cpp index 70965e5289..71dff862d3 100644 --- a/qa/tests/pcbnew/drc/drc_test_utils.cpp +++ b/qa/tests/pcbnew/drc/drc_test_utils.cpp @@ -24,7 +24,7 @@ #include "drc_test_utils.h" -std::ostream& operator<<( std::ostream& os, const PCB_MARKER& aMarker ) +std::ostream& boost_test_print_type( std::ostream& os, const PCB_MARKER& aMarker ) { const auto& reporter = aMarker.GetRCItem(); os << "PCB_MARKER[\n"; diff --git a/qa/tests/pcbnew/drc/drc_test_utils.h b/qa/tests/pcbnew/drc/drc_test_utils.h index c2e1d875fc..53c35a7ba4 100644 --- a/qa/tests/pcbnew/drc/drc_test_utils.h +++ b/qa/tests/pcbnew/drc/drc_test_utils.h @@ -37,15 +37,8 @@ * Define a stream function for logging #PCB_MARKER test assertions. * * This has to be in the same namespace as #PCB_MARKER - * - * Note: this assumes there is not a operator<< for this type in the main - * Pcbnew library. If one is introduced there, this one should be removed. - * - * TODO: convert to boost_test_print_type when Boost minver > 1.64. This - * will keep testing logging and application-level operator<< implementations - * separate, as they should be. */ -std::ostream& operator<<( std::ostream& os, const PCB_MARKER& aMarker ); +std::ostream& boost_test_print_type( std::ostream& os, const PCB_MARKER& aMarker ); namespace KI_TEST