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 {
67 output = std::make_unique<KiCadClass>( aParent );
68 }
69 else
70 {
71 std::unique_ptr<KiCadClass> cloned( static_cast<KiCadClass*>( aInput->Clone() ) );
72 output = std::make_unique<KiCadClass>( *cloned );
73 }
74
75 bool deserializeResult = false;
76 BOOST_REQUIRE_NO_THROW( deserializeResult = output->Deserialize( any ) );
77 BOOST_REQUIRE_MESSAGE( deserializeResult, "Deserialize failed" );
78
79 // This round-trip checks that we can create an equivalent protobuf
80 google::protobuf::Any outputAny;
81 BOOST_REQUIRE_NO_THROW( output->Serialize( outputAny ) );
82 BOOST_TEST_MESSAGE( "Output: " << outputAny.Utf8DebugString() );
83
84 if( !( outputAny.SerializeAsString() == any.SerializeAsString() ) )
85 {
86 BOOST_TEST_FAIL( "Round-tripped protobuf does not match" );
87 }
88
89 // This round-trip checks that we can create an equivalent KiCad object
90 if( !( *output == *aInput ) )
91 {
92 BOOST_TEST_FAIL( "Round-tripped object does not match" );
93 }
94 }
95}
96
97
99{
100 KI_TEST::LoadBoard( m_settingsManager, "api_kitchen_sink", m_board );
101
102 for( PCB_TRACK* track : m_board->Tracks() )
103 {
104 switch( track->Type() )
105 {
106 case PCB_TRACE_T:
108 break;
109
110 case PCB_ARC_T:
112 m_board.get() );
113 break;
114
115 case PCB_VIA_T:
116 // Vias are not strict-checked at the moment because m_zoneLayerOverrides is not
117 // currently exposed to the API
118 // TODO(JE) enable strict when fixed
120 m_board.get(), false );
121 break;
122
123 default:
124 wxFAIL;
125 }
126 }
127
128 for( FOOTPRINT* footprint : m_board->Footprints() )
130
131 for( ZONE* zone : m_board->Zones() )
133
134 for( BOARD_ITEM* item : m_board->Drawings() )
135 {
136 switch( item->Type() )
137 {
140 static_cast<PCB_DIM_ALIGNED*>( item ), m_board.get() );
141 break;
142
145 static_cast<PCB_DIM_ORTHOGONAL*>( item ), m_board.get() );
146 break;
147
148 case PCB_DIM_CENTER_T:
150 static_cast<PCB_DIM_CENTER*>( item ), m_board.get() );
151 break;
152
153 case PCB_DIM_LEADER_T:
155 static_cast<PCB_DIM_LEADER*>( item ), m_board.get() );
156 break;
157
158 case PCB_DIM_RADIAL_T:
160 static_cast<PCB_DIM_RADIAL*>( item ), m_board.get() );
161 break;
162
163 default: break;
164 }
165 // TODO(JE) Shapes
166
167 // TODO(JE) Text
168
169 // TODO Barcodes
170 }
171}
172
173
175{
176 KI_TEST::LoadBoard( m_settingsManager, "padstacks", m_board );
177
178 for( PCB_TRACK* track : m_board->Tracks() )
179 {
180 switch( track->Type() )
181 {
182 case PCB_VIA_T:
183 // Vias are not strict-checked at the moment because m_zoneLayerOverrides is not
184 // currently exposed to the API
185 // TODO(JE) enable strict when fixed
187 m_board.get(), false );
188 break;
189
190 default:
191 wxFAIL;
192 }
193 }
194
195 for( FOOTPRINT* footprint : m_board->Footprints() )
197}
198
A base class for any item which can be embedded within the BOARD container class, and therefore insta...
Definition board_item.h:83
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