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_barcode.h>
33#include <pcb_dimension.h>
34#include <pcb_reference_image.h>
35#include <pcb_track.h>
36#include <zone.h>
37
38
39BOOST_AUTO_TEST_SUITE( ApiProto )
40
49
50
51template<typename ProtoClass, typename KiCadClass, typename ParentClass>
52void testProtoFromKiCadObject( KiCadClass* aInput, ParentClass* aParent, bool aStrict = true )
53{
54 BOOST_TEST_CONTEXT( aInput->GetFriendlyName() << ": " << aInput->m_Uuid.AsStdString() )
55 {
56 google::protobuf::Any any;
57 BOOST_REQUIRE_NO_THROW( aInput->Serialize( any ) );
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
83 if( !( outputAny.SerializeAsString() == any.SerializeAsString() ) )
84 {
85 BOOST_TEST_MESSAGE( "Input: " << any.Utf8DebugString() );
86 BOOST_TEST_MESSAGE( "Output: " << outputAny.Utf8DebugString() );
87 BOOST_TEST_FAIL( "Round-tripped protobuf does not match" );
88 }
89
90 // This round-trip checks that we can create an equivalent KiCad object
91 if( !( *output == *aInput ) )
92 {
93 BOOST_TEST_FAIL( "Round-tripped object does not match" );
94 }
95 }
96}
97
98
100{
101 KI_TEST::LoadBoard( m_settingsManager, "api_kitchen_sink", m_board );
102
103 int barcodeCount = 0;
104 int referenceImageCount = 0;
105
106 for( PCB_TRACK* track : m_board->Tracks() )
107 {
108 switch( track->Type() )
109 {
110 case PCB_TRACE_T:
112 break;
113
114 case PCB_ARC_T:
116 m_board.get() );
117 break;
118
119 case PCB_VIA_T:
120 // Vias are not strict-checked at the moment because m_zoneLayerOverrides is not
121 // currently exposed to the API
122 // TODO(JE) enable strict when fixed
124 m_board.get(), false );
125 break;
126
127 default:
128 wxFAIL;
129 }
130 }
131
132 for( FOOTPRINT* footprint : m_board->Footprints() )
134
135 for( ZONE* zone : m_board->Zones() )
137
138 for( BOARD_ITEM* item : m_board->Drawings() )
139 {
140 switch( item->Type() )
141 {
144 static_cast<PCB_DIM_ALIGNED*>( item ), m_board.get() );
145 break;
146
149 static_cast<PCB_DIM_ORTHOGONAL*>( item ), m_board.get() );
150 break;
151
152 case PCB_DIM_CENTER_T:
154 static_cast<PCB_DIM_CENTER*>( item ), m_board.get() );
155 break;
156
157 case PCB_DIM_LEADER_T:
159 static_cast<PCB_DIM_LEADER*>( item ), m_board.get() );
160 break;
161
162 case PCB_DIM_RADIAL_T:
164 static_cast<PCB_DIM_RADIAL*>( item ), m_board.get() );
165 break;
166
167 case PCB_BARCODE_T:
169 static_cast<PCB_BARCODE*>( item ), m_board.get() );
170 ++barcodeCount;
171 break;
172
175 static_cast<PCB_REFERENCE_IMAGE*>( item ), m_board.get() );
176 ++referenceImageCount;
177 break;
178
179 default: break;
180 }
181 // TODO(JE) Shapes
182
183 // TODO(JE) Text
184 }
185
186 BOOST_CHECK_GT( barcodeCount, 0 );
187 BOOST_CHECK_GT( referenceImageCount, 0 );
188}
189
190
192{
193 KI_TEST::LoadBoard( m_settingsManager, "padstacks", m_board );
194
195 for( PCB_TRACK* track : m_board->Tracks() )
196 {
197 switch( track->Type() )
198 {
199 case PCB_VIA_T:
200 // Vias are not strict-checked at the moment because m_zoneLayerOverrides is not
201 // currently exposed to the API
202 // TODO(JE) enable strict when fixed
204 m_board.get(), false );
205 break;
206
207 default:
208 wxFAIL;
209 }
210 }
211
212 for( FOOTPRINT* footprint : m_board->Footprints() )
214}
215
A base class for any item which can be embedded within the BOARD container class, and therefore insta...
Definition board_item.h:84
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.
Object to handle a bitmap image that can be inserted in a PCB.
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)
BARCODE class definition.
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()
nlohmann::json output
BOOST_TEST_MESSAGE("\n=== Real-World Polygon PIP Benchmark ===\n"<< formatTable(table))
BOOST_TEST_CONTEXT("Test Clearance")
@ PCB_DIM_ORTHOGONAL_T
class PCB_DIM_ORTHOGONAL, a linear dimension constrained to x/y
Definition typeinfo.h:103
@ PCB_DIM_LEADER_T
class PCB_DIM_LEADER, a leader dimension (graphic item)
Definition typeinfo.h:100
@ PCB_VIA_T
class PCB_VIA, a via (like a track segment on a copper layer)
Definition typeinfo.h:94
@ PCB_DIM_CENTER_T
class PCB_DIM_CENTER, a center point marking (graphic item)
Definition typeinfo.h:101
@ PCB_REFERENCE_IMAGE_T
class PCB_REFERENCE_IMAGE, bitmap on a layer
Definition typeinfo.h:86
@ PCB_BARCODE_T
class PCB_BARCODE, a barcode (graphic item)
Definition typeinfo.h:98
@ PCB_DIM_ALIGNED_T
class PCB_DIM_ALIGNED, a linear dimension (graphic item)
Definition typeinfo.h:99
@ PCB_ARC_T
class PCB_ARC, an arc track segment on a copper layer
Definition typeinfo.h:95
@ PCB_TRACE_T
class PCB_TRACK, a track segment (segment on a copper layer)
Definition typeinfo.h:93
@ PCB_DIM_RADIAL_T
class PCB_DIM_RADIAL, a radius or diameter dimension
Definition typeinfo.h:102