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).
This commit is contained in:
John Beard 2024-08-13 10:02:08 +01:00
parent 27c0c7e0e9
commit 5b772dde13
14 changed files with 75 additions and 161 deletions

View File

@ -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 <typename T>
std::ostream& boost_test_print_type( std::ostream& os, const BOX2<T>& aBox )
{
os << "BOX[ " << aBox.GetOrigin() << " + " << aBox.GetSize() << " ]";
return os;

View File

@ -38,35 +38,6 @@
#include <wx/gdicmn.h>
/*
* 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<class T>
struct PRINTABLE_OPT
@ -114,79 +85,62 @@ inline bool operator!=( const PRINTABLE_OPT<L>& aLhs, const PRINTABLE_OPT<R>& 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 <typename T>
struct print_log_value<std::vector<T>>
std::ostream& boost_test_print_type( std::ostream& os, std::vector<T> const& aVec )
{
inline void operator()( std::ostream& os, std::vector<T> const& aVec )
os << "std::vector size " << aVec.size() << " [";
for( const auto& i : aVec )
{
os << "std::vector size " << aVec.size() << " [";
for( const auto& i : aVec )
{
os << "\n ";
print_log_value<T>()( os, i );
}
os << "]";
os << "\n " << i;
}
};
os << "]";
return os;
}
/**
* Boost print helper for generic maps
*/
template <typename K, typename V>
struct print_log_value<std::map<K, V>>
std::ostream& boost_test_print_type( std::ostream& os, std::map<K, V> const& aMap )
{
inline void operator()( std::ostream& os, std::map<K, V> const& aMap )
os << "std::map size " << aMap.size() << " [";
for( const auto& [key, value] : aMap )
{
os << "std::map size " << aMap.size() << " [";
for( const auto& [key, value] : aMap )
{
os << "\n ";
print_log_value<K>()( os, key );
os << " = ";
print_log_value<K>()( os, value );
}
os << "]";
os << "\n " << key << " = " << value;
}
};
os << "]";
return os;
}
/**
* Boost print helper for generic pairs
*/
template <typename K, typename V>
struct print_log_value<std::pair<K, V>>
std::ostream& boost_test_print_type( std::ostream& os, std::pair<K, V> const& aPair )
{
inline void operator()( std::ostream& os, std::pair<K, V> const& aPair )
{
os << "[";
print_log_value<K>()( os, aPair.first );
os << ", ";
print_log_value<K>()( 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<wxPoint>
{
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

View File

@ -23,18 +23,12 @@
#include <qa_utils/wx_utils/unit_test_utils.h>
namespace BOOST_TEST_PRINT_NAMESPACE_OPEN
{
void print_log_value<wxPoint>::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 "???"

View File

@ -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"
<< " ]";

View File

@ -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
<< " ]";

View File

@ -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<wxImage>::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

View File

@ -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<wxImage>
{
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

View File

@ -35,27 +35,17 @@
#include <sch_field.h>
namespace BOOST_TEST_PRINT_NAMESPACE_OPEN
std::ostream& boost_test_print_type( std::ostream& os, SCH_FIELD const& f )
{
template <>
struct print_log_value<SCH_FIELD>
{
inline void operator()( std::ostream& os, SCH_FIELD const& f )
{
os << "SCH_FIELD[ " << f.GetCanonicalName() << " ]";
}
};
template <>
struct print_log_value<std::vector<SCH_FIELD>>
{
inline void operator()( std::ostream& os, std::vector<SCH_FIELD> const& f )
{
os << "SCH_FIELDS[ " << f.size() << " ]";
}
};
os << "SCH_FIELD[ " << f.GetCanonicalName() << " ]";
return os;
}
std::ostream& boost_test_print_type( std::ostream& os, std::vector<SCH_FIELD> const& f )
{
os << "SCH_FIELDS[ " << f.size() << " ]";
return os;
}
BOOST_TEST_PRINT_NAMESPACE_CLOSE
namespace KI_TEST

View File

@ -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

View File

@ -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;
}

View File

@ -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<SHAPE_LINE_CHAIN>
{
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

View File

@ -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<SEXPR::SEXPR>
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

View File

@ -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";

View File

@ -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