20#include <boost/test/unit_test.hpp>
21#include <boost/bimap.hpp>
22#include <magic_enum.hpp>
28#include <api/common/types/enums.pb.h>
29#include <api/board/board_types.pb.h>
50template<
typename KiCadEnum,
typename ProtoEnum>
53 boost::bimap<ProtoEnum, KiCadEnum> protoToKiCadSeen;
54 std::set<ProtoEnum> seenProtos;
56 for( ProtoEnum value : magic_enum::enum_values<ProtoEnum>() )
58 BOOST_TEST_CONTEXT( magic_enum::enum_type_name<ProtoEnum>() <<
"::"
59 << magic_enum::enum_name( value ) )
61 std::string
name( magic_enum::enum_name( value ) );
62 auto splitPos =
name.find_first_of(
'_' );
65 BOOST_REQUIRE_MESSAGE( splitPos != std::string::npos,
66 "Proto enum name doesn't have a prefix" );
68 std::string suffix =
name.substr( splitPos );
71 if(
static_cast<int>( value ) == 0 )
73 BOOST_REQUIRE_MESSAGE( suffix.compare(
"_UNKNOWN" ) == 0,
74 "Proto enum with value 0 must be named <PREFIX>_UNKNOWN" );
80 BOOST_REQUIRE_NO_THROW( result = ( FromProtoEnum<KiCadEnum, ProtoEnum>( value ) ) );
83 BOOST_REQUIRE( !protoToKiCadSeen.left.count( value ) );
84 protoToKiCadSeen.left.insert( { value, result } );
88 for( KiCadEnum value : magic_enum::enum_values<KiCadEnum>() )
90 BOOST_TEST_CONTEXT( magic_enum::enum_type_name<KiCadEnum>() <<
"::"
91 << magic_enum::enum_name( value ) )
95 if( aPartiallyMapped )
99 result = ToProtoEnum<KiCadEnum, ProtoEnum>( value );
104 BOOST_REQUIRE_MESSAGE( !protoToKiCadSeen.right.count( value ),
105 "Proto enum is mapped to this KiCad enum, but not vice versa" );
112 BOOST_REQUIRE_NO_THROW( result = ( ToProtoEnum<KiCadEnum, ProtoEnum>( value ) ) );
116 BOOST_REQUIRE( result !=
static_cast<ProtoEnum
>( 0 ) );
119 BOOST_REQUIRE( !seenProtos.count( result ) );
120 seenProtos.insert( result );
123 KiCadEnum roundTrip = FromProtoEnum<KiCadEnum, ProtoEnum>( result );
124 BOOST_REQUIRE( roundTrip == value );
131 testEnums<GR_TEXT_H_ALIGN_T, types::HorizontalAlignment>();
136 testEnums<GR_TEXT_V_ALIGN_T, types::VerticalAlignment>();
141 testEnums<LINE_STYLE, types::StrokeLineStyle>();
146 testEnums<KICAD_T, types::KiCadObjectType>(
true );
151 testEnums<PCB_LAYER_ID, kiapi::board::types::BoardLayer>(
true );
156 testEnums<PAD_SHAPE, kiapi::board::types::PadStackShape>();
161 testEnums<ZONE_CONNECTION, kiapi::board::types::ZoneConnectionStyle>();
166 testEnums<PAD_ATTRIB, kiapi::board::types::PadType>();
171 testEnums<PADSTACK::MODE, kiapi::board::types::PadStackType>();
176 testEnums<PAD_DRILL_SHAPE, kiapi::board::types::DrillShape>();
181 testEnums<PADSTACK::UNCONNECTED_LAYER_MODE, kiapi::board::types::UnconnectedLayerRemoval>();
187 testEnums<VIATYPE, kiapi::board::types::ViaType>(
true );
An exception class to represent a WX assertion.
BOOST_AUTO_TEST_CASE(HorizontalAlignment)
void testEnums(bool aPartiallyMapped=false)
Checks if a KiCad enum has been properly mapped to a Protobuf enum.
BOOST_AUTO_TEST_SUITE(CadstarPartParser)
BOOST_AUTO_TEST_SUITE_END()