KiCad PCB EDA Suite
Loading...
Searching...
No Matches
test_api_proto.cpp
Go to the documentation of this file.
1/*
2 * This program source code file is part of KiCad, a free EDA CAD application.
3 *
4 * Copyright (C) 2024 Jon Evans <[email protected]>
5 * Copyright (C) 2024 KiCad Developers, see AUTHORS.txt for contributors.
6 *
7 * This program is free software: you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation, either version 3 of the License, or (at your
10 * option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program. If not, see <http://www.gnu.org/licenses/>.
19 */
20
21#include <boost/test/unit_test.hpp>
22#include <import_export.h>
26#include <google/protobuf/any.pb.h>
27
28#include <api/board/board_types.pb.h>
29
30#include <board.h>
31#include <footprint.h>
32#include <pcb_track.h>
33#include <zone.h>
34
35
36BOOST_AUTO_TEST_SUITE( ApiProto )
37
39{
41 m_settingsManager( true /* headless */ )
42 { }
43
45 std::unique_ptr<BOARD> m_board;
46};
47
48
49template<typename ProtoClass, typename KiCadClass>
50void testProtoFromKiCadObject( KiCadClass* aInput )
51{
52 BOOST_TEST_CONTEXT( aInput->GetFriendlyName() << ": " << aInput->m_Uuid.AsStdString() )
53 {
54 google::protobuf::Any any;
55 BOOST_REQUIRE_NO_THROW( aInput->Serialize( any ) );
56
57 BOOST_TEST_MESSAGE( "Input: " << any.Utf8DebugString() );
58
59 ProtoClass proto;
60 BOOST_REQUIRE_MESSAGE( any.UnpackTo( &proto ),
61 "Any message did not unpack into the requested type" );
62
63 KiCadClass output( *static_cast<KiCadClass*>( aInput->Clone() ) );
64
65 bool deserializeResult = false;
66 BOOST_REQUIRE_NO_THROW( deserializeResult = output.Deserialize( any ) );
67 BOOST_REQUIRE_MESSAGE( deserializeResult, "Deserialize failed" );
68
69 // This round-trip checks that we can create an equivalent protobuf
70 google::protobuf::Any outputAny;
71 BOOST_REQUIRE_NO_THROW( output.Serialize( outputAny ) );
72 BOOST_TEST_MESSAGE( "Output: " << outputAny.Utf8DebugString() );
73
74 if( !( outputAny.SerializeAsString() == any.SerializeAsString() ) )
75 {
76 BOOST_TEST_FAIL( "Round-tripped protobuf does not match" );
77 }
78
79 // This round-trip checks that we can create an equivalent KiCad object
80 if( !( output == *aInput ) )
81 {
82 BOOST_TEST_FAIL( "Round-tripped object does not match" );
83 }
84 }
85}
86
87
89{
90 KI_TEST::LoadBoard( m_settingsManager, "api_kitchen_sink", m_board );
91
92 for( PCB_TRACK* track : m_board->Tracks() )
93 {
94 switch( track->Type() )
95 {
96 case PCB_TRACE_T:
97 testProtoFromKiCadObject<kiapi::board::types::Track>( track );
98 break;
99
100 case PCB_ARC_T:
101 testProtoFromKiCadObject<kiapi::board::types::Arc>( track );
102 break;
103
104 case PCB_VIA_T:
105 testProtoFromKiCadObject<kiapi::board::types::Via>( track );
106 break;
107
108 default:
109 wxFAIL;
110 }
111 }
112
113 for( FOOTPRINT* footprint : m_board->Footprints() )
114 testProtoFromKiCadObject<kiapi::board::types::FootprintInstance>( footprint );
115
116 // Not yet
117// for( ZONE* zone : m_board->Zones() )
118// testProtoFromKiCadObject<kiapi::board::types::Zone>( zone );
119}
120
121
123{
124 KI_TEST::LoadBoard( m_settingsManager, "padstacks", m_board );
125
126 for( PCB_TRACK* track : m_board->Tracks() )
127 {
128 switch( track->Type() )
129 {
130 case PCB_VIA_T:
131 testProtoFromKiCadObject<kiapi::board::types::Via>( static_cast<PCB_VIA*>( track ) );
132 break;
133
134 default:
135 wxFAIL;
136 }
137 }
138
139 for( FOOTPRINT* footprint : m_board->Footprints() )
140 testProtoFromKiCadObject<kiapi::board::types::FootprintInstance>( footprint );
141}
142
void LoadBoard(SETTINGS_MANAGER &aSettingsManager, const wxString &aRelPath, std::unique_ptr< BOARD > &aBoard)
std::unique_ptr< BOARD > m_board
SETTINGS_MANAGER m_settingsManager
BOOST_FIXTURE_TEST_CASE(BoardTypes, PROTO_TEST_FIXTURE)
void testProtoFromKiCadObject(KiCadClass *aInput)
BOOST_AUTO_TEST_SUITE(CadstarPartParser)
BOOST_AUTO_TEST_SUITE_END()
@ PCB_VIA_T
class PCB_VIA, a via (like a track segment on a copper layer)
Definition: typeinfo.h:97
@ PCB_ARC_T
class PCB_ARC, an arc track segment on a copper layer
Definition: typeinfo.h:98
@ PCB_TRACE_T
class PCB_TRACK, a track segment (segment on a copper layer)
Definition: typeinfo.h:96