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
47
48
49template<typename ProtoClass, typename KiCadClass, typename ParentClass>
50void testProtoFromKiCadObject( KiCadClass* aInput, ParentClass* aParent, bool aStrict = true )
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 std::unique_ptr<KiCadClass> output;
64
65 if( aStrict )
66 output = std::make_unique<KiCadClass>( aParent );
67 else
68 output = std::make_unique<KiCadClass>( *static_cast<KiCadClass*>( aInput->Clone() ) );
69
70 bool deserializeResult = false;
71 BOOST_REQUIRE_NO_THROW( deserializeResult = output->Deserialize( any ) );
72 BOOST_REQUIRE_MESSAGE( deserializeResult, "Deserialize failed" );
73
74 // This round-trip checks that we can create an equivalent protobuf
75 google::protobuf::Any outputAny;
76 BOOST_REQUIRE_NO_THROW( output->Serialize( outputAny ) );
77 BOOST_TEST_MESSAGE( "Output: " << outputAny.Utf8DebugString() );
78
79 if( !( outputAny.SerializeAsString() == any.SerializeAsString() ) )
80 {
81 BOOST_TEST_FAIL( "Round-tripped protobuf does not match" );
82 }
83
84 // This round-trip checks that we can create an equivalent KiCad object
85 if( !( *output == *aInput ) )
86 {
87 BOOST_TEST_FAIL( "Round-tripped object does not match" );
88 }
89 }
90}
91
92
94{
95 KI_TEST::LoadBoard( m_settingsManager, "api_kitchen_sink", m_board );
96
97 for( PCB_TRACK* track : m_board->Tracks() )
98 {
99 switch( track->Type() )
100 {
101 case PCB_TRACE_T:
103 break;
104
105 case PCB_ARC_T:
107 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
115 m_board.get(), false );
116 break;
117
118 default:
119 wxFAIL;
120 }
121 }
122
123 for( FOOTPRINT* footprint : m_board->Footprints() )
125
126 for( ZONE* zone : m_board->Zones() )
128
129 for( BOARD_ITEM* item : m_board->Drawings() )
130 {
131 switch( item->Type() )
132 {
135 static_cast<PCB_DIM_ALIGNED*>( item ), m_board.get() );
136 break;
137
140 static_cast<PCB_DIM_ORTHOGONAL*>( item ), m_board.get() );
141 break;
142
143 case PCB_DIM_CENTER_T:
145 static_cast<PCB_DIM_CENTER*>( item ), m_board.get() );
146 break;
147
148 case PCB_DIM_LEADER_T:
150 static_cast<PCB_DIM_LEADER*>( item ), m_board.get() );
151 break;
152
153 case PCB_DIM_RADIAL_T:
155 static_cast<PCB_DIM_RADIAL*>( item ), m_board.get() );
156 break;
157
158 default: break;
159 }
160 // TODO(JE) Shapes
161
162 // TODO(JE) Text
163
164 // TODO Barcodes
165 }
166}
167
168
170{
171 KI_TEST::LoadBoard( m_settingsManager, "padstacks", m_board );
172
173 for( PCB_TRACK* track : m_board->Tracks() )
174 {
175 switch( track->Type() )
176 {
177 case PCB_VIA_T:
178 // Vias are not strict-checked at the moment because m_zoneLayerOverrides is not
179 // currently exposed to the API
180 // TODO(JE) enable strict when fixed
182 m_board.get(), false );
183 break;
184
185 default:
186 wxFAIL;
187 }
188 }
189
190 for( FOOTPRINT* footprint : m_board->Footprints() )
192}
193
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:74
A type-safe container of any type.
Definition ki_any.h:93
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:106
@ PCB_DIM_LEADER_T
class PCB_DIM_LEADER, a leader dimension (graphic item)
Definition typeinfo.h:103
@ 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:104
@ PCB_DIM_ALIGNED_T
class PCB_DIM_ALIGNED, a linear dimension (graphic item)
Definition typeinfo.h:102
@ 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:105