/* * This program source code file is part of KiCad, a free EDA CAD application. * * Copyright (C) 2024 Jon Evans * 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 #include #include #include #include #include #include BOOST_AUTO_TEST_SUITE( ApiProto ) struct PROTO_TEST_FIXTURE { PROTO_TEST_FIXTURE() : m_settingsManager( true /* headless */ ) { } SETTINGS_MANAGER m_settingsManager; std::unique_ptr m_board; }; template void testProtoFromKiCadObject( KiCadClass* aInput ) { BOOST_TEST_CONTEXT( aInput->GetFriendlyName() << ": " << aInput->m_Uuid.AsStdString() ) { google::protobuf::Any any; BOOST_REQUIRE_NO_THROW( aInput->Serialize( any ) ); ProtoClass proto; BOOST_REQUIRE_MESSAGE( any.UnpackTo( &proto ), "Any message did not unpack into the requested type" ); KiCadClass output( *static_cast( aInput->Clone() ) ); bool deserializeResult = false; BOOST_REQUIRE_NO_THROW( deserializeResult = output.Deserialize( any ) ); BOOST_REQUIRE_MESSAGE( deserializeResult, "Deserialize failed" ); // This round-trip checks that we can create an equivalent protobuf google::protobuf::Any outputAny; BOOST_REQUIRE_NO_THROW( output.Serialize( outputAny ) ); if( !( outputAny.SerializeAsString() == any.SerializeAsString() ) ) { BOOST_TEST_MESSAGE( "Input: " << any.Utf8DebugString() ); BOOST_TEST_MESSAGE( "Output: " << outputAny.Utf8DebugString() ); BOOST_TEST_FAIL( "Round-tripped protobuf does not match" ); } // This round-trip checks that we can create an equivalent KiCad object if( !( output == *aInput ) ) { if( ( output == *aInput ) ) BOOST_TEST_MESSAGE("ha"); BOOST_TEST_FAIL( "Round-tripped object does not match" ); } } } BOOST_FIXTURE_TEST_CASE( BoardTypes, PROTO_TEST_FIXTURE ) { KI_TEST::LoadBoard( m_settingsManager, "api_kitchen_sink", m_board ); for( PCB_TRACK* track : m_board->Tracks() ) { switch( track->Type() ) { case PCB_TRACE_T: testProtoFromKiCadObject( track ); break; case PCB_ARC_T: testProtoFromKiCadObject( track ); break; case PCB_VIA_T: testProtoFromKiCadObject( track ); break; default: wxFAIL; } } for( FOOTPRINT* footprint : m_board->Footprints() ) testProtoFromKiCadObject( footprint ); // Not yet // for( ZONE* zone : m_board->Zones() ) // testProtoFromKiCadObject( zone ); } BOOST_AUTO_TEST_SUITE_END()