/*
* This program source code file is part of KiCad, a free EDA CAD application.
*
* Copyright (C) 2024 KiCad Developers, see AUTHORS.txt for contributors.
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation, either version 3 of the License, or (at your
* option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program. If not, see .
*/
#include
#include
#include
#include
#include
// Common
#include
#include
#include
#include
#include
#include
#include
// Board-specific
#include
#include
#include
using namespace kiapi::common;
BOOST_AUTO_TEST_SUITE( ApiEnums )
/**
* Checks if a KiCad enum has been properly mapped to a Protobuf enum
* @tparam KiCadEnum is an enum type
* @tparam ProtoEnum is a Protobuf enum type
* @param aPartiallyMapped is true if only some of the KiCad enum values are exposed to the API
*/
template
void testEnums( bool aPartiallyMapped = false )
{
boost::bimap protoToKiCadSeen;
std::set seenProtos;
for( ProtoEnum value : magic_enum::enum_values() )
{
BOOST_TEST_CONTEXT( magic_enum::enum_type_name() << "::"
<< magic_enum::enum_name( value ) )
{
std::string name( magic_enum::enum_name( value ) );
auto splitPos = name.find_first_of( '_' );
// Protobuf enum names should be formatted as PREFIX_KEY
BOOST_REQUIRE_MESSAGE( splitPos != std::string::npos,
"Proto enum name doesn't have a prefix" );
std::string suffix = name.substr( splitPos );
// Protobuf enum with the value 0 should not map to anything
if( static_cast( value ) == 0 )
{
BOOST_REQUIRE_MESSAGE( suffix.compare( "_UNKNOWN" ) == 0,
"Proto enum with value 0 must be named _UNKNOWN" );
continue;
}
KiCadEnum result;
// Every non-unknown Proto value should map to a valid KiCad value
BOOST_REQUIRE_NO_THROW( result = ( FromProtoEnum( value ) ) );
// There should be a 1:1 mapping
BOOST_REQUIRE( !protoToKiCadSeen.left.count( value ) );
protoToKiCadSeen.left.insert( { value, result } );
}
}
for( KiCadEnum value : magic_enum::enum_values() )
{
BOOST_TEST_CONTEXT( magic_enum::enum_type_name() << "::"
<< magic_enum::enum_name( value ) )
{
ProtoEnum result;
if( aPartiallyMapped )
{
try
{
result = ToProtoEnum( value );
}
catch( KI_TEST::WX_ASSERT_ERROR )
{
// If it wasn't mapped from KiCad to Proto, it shouldn't be mapped the other way
BOOST_REQUIRE_MESSAGE( !protoToKiCadSeen.right.count( value ),
"Proto enum is mapped to this KiCad enum, but not vice versa" );
continue;
}
}
else
{
// Every KiCad enum value should map to a non-unknown Protobuf value
BOOST_REQUIRE_NO_THROW( result = ( ToProtoEnum( value ) ) );
}
// Protobuf "unknown" should always be zero value by convention
BOOST_REQUIRE( result != static_cast( 0 ) );
// There should be a 1:1 mapping
BOOST_REQUIRE( !seenProtos.count( result ) );
seenProtos.insert( result );
// Round-tripping should work
KiCadEnum roundTrip = FromProtoEnum( result );
BOOST_REQUIRE( roundTrip == value );
}
}
}
BOOST_AUTO_TEST_CASE( HorizontalAlignment )
{
testEnums();
}
BOOST_AUTO_TEST_CASE( VerticalAlignment )
{
testEnums();
}
BOOST_AUTO_TEST_CASE( StrokeLineStyle )
{
testEnums();
}
BOOST_AUTO_TEST_CASE( KiCadObjectType )
{
testEnums( true );
}
BOOST_AUTO_TEST_CASE( BoardLayer )
{
testEnums( true );
}
BOOST_AUTO_TEST_CASE( PadStackShape )
{
testEnums();
}
BOOST_AUTO_TEST_CASE( ZoneConnectionStyle )
{
testEnums();
}
BOOST_AUTO_TEST_CASE( PadType )
{
testEnums();
}
BOOST_AUTO_TEST_CASE( PadStackType )
{
testEnums();
}
BOOST_AUTO_TEST_CASE( UnconnectedLayerRemoval )
{
testEnums();
}
BOOST_AUTO_TEST_CASE( ViaType )
{
// VIATYPE::NOT_DEFINED is not mapped
testEnums( true );
}
BOOST_AUTO_TEST_SUITE_END()