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 The 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_dimension.h>
33#include <pcb_track.h>
34#include <zone.h>
35
36
37BOOST_AUTO_TEST_SUITE( ApiProto )
38
40{
42 m_settingsManager( true /* headless */ )
43 { }
44
46 std::unique_ptr<BOARD> m_board;
47};
48
49
50template<typename ProtoClass, typename KiCadClass, typename ParentClass>
51void testProtoFromKiCadObject( KiCadClass* aInput, ParentClass* aParent, bool aStrict = true )
52{
53 BOOST_TEST_CONTEXT( aInput->GetFriendlyName() << ": " << aInput->m_Uuid.AsStdString() )
54 {
55 google::protobuf::Any any;
56 BOOST_REQUIRE_NO_THROW( aInput->Serialize( any ) );
57
58 BOOST_TEST_MESSAGE( "Input: " << any.Utf8DebugString() );
59
60 ProtoClass proto;
61 BOOST_REQUIRE_MESSAGE( any.UnpackTo( &proto ),
62 "Any message did not unpack into the requested type" );
63
64 std::unique_ptr<KiCadClass> output;
65
66 if( aStrict )
67 output = std::make_unique<KiCadClass>( aParent );
68 else
69 output = std::make_unique<KiCadClass>( *static_cast<KiCadClass*>( aInput->Clone() ) );
70
71 bool deserializeResult = false;
72 BOOST_REQUIRE_NO_THROW( deserializeResult = output->Deserialize( any ) );
73 BOOST_REQUIRE_MESSAGE( deserializeResult, "Deserialize failed" );
74
75 // This round-trip checks that we can create an equivalent protobuf
76 google::protobuf::Any outputAny;
77 BOOST_REQUIRE_NO_THROW( output->Serialize( outputAny ) );
78 BOOST_TEST_MESSAGE( "Output: " << outputAny.Utf8DebugString() );
79
80 if( !( outputAny.SerializeAsString() == any.SerializeAsString() ) )
81 {
82 BOOST_TEST_FAIL( "Round-tripped protobuf does not match" );
83 }
84
85 // This round-trip checks that we can create an equivalent KiCad object
86 if( !( *output == *aInput ) )
87 {
88 BOOST_TEST_FAIL( "Round-tripped object does not match" );
89 }
90 }
91}
92
93
95{
96 KI_TEST::LoadBoard( m_settingsManager, "api_kitchen_sink", m_board );
97
98 for( PCB_TRACK* track : m_board->Tracks() )
99 {
100 switch( track->Type() )
101 {
102 case PCB_TRACE_T:
103 testProtoFromKiCadObject<kiapi::board::types::Track>( track, m_board.get() );
104 break;
105
106 case PCB_ARC_T:
107 testProtoFromKiCadObject<kiapi::board::types::Arc>( track, m_board.get() );
108 break;
109
110 case PCB_VIA_T:
111 // Vias are not strict-checked at the moment because m_zoneLayerOverrides is not
112 // currently exposed to the API
113 // TODO(JE) enable strict when fixed
114 testProtoFromKiCadObject<kiapi::board::types::Via>( track, m_board.get(), false );
115 break;
116
117 default:
118 wxFAIL;
119 }
120 }
121
122 for( FOOTPRINT* footprint : m_board->Footprints() )
123 testProtoFromKiCadObject<kiapi::board::types::FootprintInstance>( footprint, m_board.get() );
124
125 for( ZONE* zone : m_board->Zones() )
126 testProtoFromKiCadObject<kiapi::board::types::Zone>( zone, m_board.get() );
127
128 for( BOARD_ITEM* item : m_board->Drawings() )
129 {
130 switch( item->Type() )
131 {
133 testProtoFromKiCadObject<kiapi::board::types::Dimension>(
134 static_cast<PCB_DIM_ALIGNED*>( item ), m_board.get() );
135 break;
136
138 testProtoFromKiCadObject<kiapi::board::types::Dimension>(
139 static_cast<PCB_DIM_ORTHOGONAL*>( item ), m_board.get() );
140 break;
141
142 case PCB_DIM_CENTER_T:
143 testProtoFromKiCadObject<kiapi::board::types::Dimension>(
144 static_cast<PCB_DIM_CENTER*>( item ), m_board.get() );
145 break;
146
147 case PCB_DIM_LEADER_T:
148 testProtoFromKiCadObject<kiapi::board::types::Dimension>(
149 static_cast<PCB_DIM_LEADER*>( item ), m_board.get() );
150 break;
151
152 case PCB_DIM_RADIAL_T:
153 testProtoFromKiCadObject<kiapi::board::types::Dimension>(
154 static_cast<PCB_DIM_RADIAL*>( item ), m_board.get() );
155 break;
156
157 default: break;
158 }
159 // TODO(JE) Shapes
160
161 // TODO(JE) Text
162 }
163}
164
165
167{
168 KI_TEST::LoadBoard( m_settingsManager, "padstacks", m_board );
169
170 for( PCB_TRACK* track : m_board->Tracks() )
171 {
172 switch( track->Type() )
173 {
174 case PCB_VIA_T:
175 // Vias are not strict-checked at the moment because m_zoneLayerOverrides is not
176 // currently exposed to the API
177 // TODO(JE) enable strict when fixed
178 testProtoFromKiCadObject<kiapi::board::types::Via>( static_cast<PCB_VIA*>( track ),
179 m_board.get(), false );
180 break;
181
182 default:
183 wxFAIL;
184 }
185 }
186
187 for( FOOTPRINT* footprint : m_board->Footprints() )
188 testProtoFromKiCadObject<kiapi::board::types::FootprintInstance>( footprint, m_board.get() );
189}
190
A base class for any item which can be embedded within the BOARD container class, and therefore insta...
Definition: board_item.h:79
For better understanding of the points that make a dimension:
Mark the center of a circle or arc with a cross shape.
A leader is a dimension-like object pointing to a specific point.
An orthogonal dimension is like an aligned dimension, but the extension lines are locked to the X or ...
A radial dimension indicates either the radius or diameter of an arc or circle.
Handle a list of polygons defining a copper zone.
Definition: zone.h:73
void LoadBoard(SETTINGS_MANAGER &aSettingsManager, const wxString &aRelPath, std::unique_ptr< BOARD > &aBoard)
std::unique_ptr< BOARD > m_board
SETTINGS_MANAGER m_settingsManager
void testProtoFromKiCadObject(KiCadClass *aInput, ParentClass *aParent, bool aStrict=true)
BOOST_FIXTURE_TEST_CASE(BoardTypes, PROTO_TEST_FIXTURE)
BOOST_AUTO_TEST_SUITE(CadstarPartParser)
BOOST_AUTO_TEST_SUITE_END()
BOOST_TEST_CONTEXT("Test Clearance")
BOOST_TEST_MESSAGE("Polyline has "<< chain.PointCount()<< " points")
@ PCB_DIM_ORTHOGONAL_T
class PCB_DIM_ORTHOGONAL, a linear dimension constrained to x/y
Definition: typeinfo.h:105
@ PCB_DIM_LEADER_T
class PCB_DIM_LEADER, a leader dimension (graphic item)
Definition: typeinfo.h:102
@ PCB_VIA_T
class PCB_VIA, a via (like a track segment on a copper layer)
Definition: typeinfo.h:97
@ PCB_DIM_CENTER_T
class PCB_DIM_CENTER, a center point marking (graphic item)
Definition: typeinfo.h:103
@ PCB_DIM_ALIGNED_T
class PCB_DIM_ALIGNED, a linear dimension (graphic item)
Definition: typeinfo.h:101
@ 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
@ PCB_DIM_RADIAL_T
class PCB_DIM_RADIAL, a radius or diameter dimension
Definition: typeinfo.h:104