KiCad PCB EDA Suite
Loading...
Searching...
No Matches
test_sch_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 The KiCad Developers, see AUTHORS.txt for contributors.
5 * @author Jon Evans <[email protected]>
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>
25
26#include <api/schematic/schematic_types.pb.h>
27#include <api/api_sch_utils.h>
28
29#include <eeschema_test_utils.h>
30
31#include <sch_bitmap.h>
32#include <sch_bus_entry.h>
33#include <sch_group.h>
34#include <sch_junction.h>
35#include <sch_label.h>
36#include <sch_line.h>
37#include <sch_no_connect.h>
38#include <sch_shape.h>
39#include <sch_sheet.h>
40#include <sch_symbol.h>
41#include <sch_text.h>
42#include <sch_textbox.h>
43#include <wx/filename.h>
44
45
46BOOST_FIXTURE_TEST_SUITE( ApiSchProto, KI_TEST::SCHEMATIC_TEST_FIXTURE )
47
49{
50 wxFileName fn( KI_TEST::GetEeschemaTestDataDir(), wxS( "api_kitchen_sink.kicad_sch" ) );
51 LoadSchematic( fn );
52 SCH_SHEET_PATH path = m_schematic->CurrentSheet();
53
54 for( SCH_ITEM* item : m_schematic->RootScreen()->Items() )
55 {
56 switch( item->Type() )
57 {
58 case SCH_JUNCTION_T:
60 static_cast<SCH_JUNCTION*>( item ),
61 []()
62 {
63 return std::make_unique<SCH_JUNCTION>();
64 } );
65 break;
66
69 static_cast<SCH_NO_CONNECT*>( item ),
70 []()
71 {
72 return std::make_unique<SCH_NO_CONNECT>();
73 } );
74 break;
75
78 static_cast<SCH_BUS_WIRE_ENTRY*>( item ),
79 []()
80 {
81 return std::make_unique<SCH_BUS_WIRE_ENTRY>();
82 } );
83 break;
84
87 static_cast<SCH_BUS_BUS_ENTRY*>( item ),
88 []()
89 {
90 return std::make_unique<SCH_BUS_BUS_ENTRY>();
91 } );
92 break;
93
94 case SCH_LINE_T:
96 static_cast<SCH_LINE*>( item ),
97 []()
98 {
99 return std::make_unique<SCH_LINE>();
100 } );
101 break;
102
103 case SCH_SHAPE_T:
105 static_cast<SCH_SHAPE*>( item ),
106 []()
107 {
108 return std::make_unique<SCH_SHAPE>();
109 } );
110 break;
111
112 case SCH_BITMAP_T:
114 static_cast<SCH_BITMAP*>( item ),
115 []()
116 {
117 return std::make_unique<SCH_BITMAP>();
118 } );
119 break;
120
121 case SCH_TEXT_T:
123 static_cast<SCH_TEXT*>( item ),
124 []()
125 {
126 return std::make_unique<SCH_TEXT>();
127 } );
128 break;
129
130 case SCH_TEXTBOX_T:
132 static_cast<SCH_TEXTBOX*>( item ),
133 []()
134 {
135 return std::make_unique<SCH_TEXTBOX>();
136 } );
137 break;
138
139 case SCH_LABEL_T:
141 static_cast<SCH_LABEL*>( item ),
142 []()
143 {
144 return std::make_unique<SCH_LABEL>();
145 } );
146 break;
147
150 static_cast<SCH_GLOBALLABEL*>( item ),
151 []()
152 {
153 return std::make_unique<SCH_GLOBALLABEL>();
154 } );
155 break;
156
157 case SCH_HIER_LABEL_T:
159 static_cast<SCH_HIERLABEL*>( item ),
160 []()
161 {
162 return std::make_unique<SCH_HIERLABEL>();
163 } );
164 break;
165
168 static_cast<SCH_DIRECTIVE_LABEL*>( item ),
169 []()
170 {
171 return std::make_unique<SCH_DIRECTIVE_LABEL>();
172 } );
173 break;
174
175 case SCH_GROUP_T:
177 static_cast<SCH_GROUP*>( item ),
178 [this]()
179 {
180 return std::make_unique<SCH_GROUP>( m_schematic->RootScreen() );
181 } );
182 break;
183
184 case SCH_SHEET_T:
186 static_cast<SCH_SHEET*>( item ),
187 [this]()
188 {
189 return std::make_unique<SCH_SHEET>( m_schematic->RootScreen() );
190 } );
191 break;
192
193 case SCH_SYMBOL_T:
194 {
195 SCH_SYMBOL* symbol = static_cast<SCH_SYMBOL*>( item );
196
197 BOOST_TEST_CONTEXT( "symbol: " << symbol->m_Uuid.AsStdString() )
198 {
199 bool result = false;
200 kiapi::schematic::types::SchematicSymbolInstance symbolProto;
201 BOOST_REQUIRE_NO_THROW( result = PackSymbol( &symbolProto, symbol, path ) );
202 BOOST_REQUIRE_MESSAGE( result, "Serialization failed" );
203
204 std::unique_ptr<SCH_SYMBOL> output = std::make_unique<SCH_SYMBOL>();
205
206 BOOST_REQUIRE_NO_THROW( result = UnpackSymbol( output.get(), symbolProto ) );
207 BOOST_REQUIRE_MESSAGE( result, "Deserialization failed" );
208
209 kiapi::schematic::types::SchematicSymbolInstance outputProto;
210 BOOST_REQUIRE_NO_THROW( result = PackSymbol( &outputProto, output.get(), path ) );
211 BOOST_REQUIRE_MESSAGE( result, "Second serialization failed" );
212
213 if( !( outputProto.SerializeAsString() == symbolProto.SerializeAsString() ) )
214 {
215 BOOST_TEST_MESSAGE( "Input: " << symbolProto.Utf8DebugString() );
216 BOOST_TEST_MESSAGE( "Output: " << outputProto.Utf8DebugString() );
217 BOOST_TEST_FAIL( "Round-tripped protobuf does not match" );
218 }
219
220 if( !output->operator==( *symbol ) )
221 BOOST_TEST_FAIL( "Round-tripped object does not match" );
222 }
223
224 break;
225 }
226
227 default:
228 break;
229 }
230 }
231}
232
bool PackSymbol(kiapi::schematic::types::SchematicSymbolInstance *aOutput, const SCH_SYMBOL *aInput, const SCH_SHEET_PATH &aPath)
bool UnpackSymbol(SCH_SYMBOL *aOutput, const kiapi::schematic::types::SchematicSymbolInstance &aInput)
void testProtoFromKiCadObject(KiCadClass *aInput, Factory &&aCreateOutput)
const KIID m_Uuid
Definition eda_item.h:528
std::string AsStdString() const
Definition kiid.cpp:250
A generic fixture for loading schematics and associated settings for qa tests.
Object to handle a bitmap image that can be inserted in a schematic.
Definition sch_bitmap.h:40
Class for a bus to bus entry.
Class for a wire to bus entry.
A set of SCH_ITEMs (i.e., without duplicates).
Definition sch_group.h:52
Base class for any item which can be embedded within the SCHEMATIC container class,...
Definition sch_item.h:168
Segment description base class to describe items which have 2 end points (track, wire,...
Definition sch_line.h:42
Handle access to a stack of flattened SCH_SHEET objects by way of a path for creating a flattened sch...
Sheet symbol placed in a schematic, and is the entry point for a sub schematic.
Definition sch_sheet.h:48
Schematic symbol object.
Definition sch_symbol.h:76
std::string GetEeschemaTestDataDir()
Get the configured location of Eeschema test data.
Class to handle a set of SCH_ITEMs.
static void LoadSchematic(SCHEMATIC *aSchematic, SCH_SHEET *aRootSheet, const wxString &aFileName)
BOOST_AUTO_TEST_CASE(HorizontalAlignment)
BOOST_AUTO_TEST_SUITE_END()
std::string path
nlohmann::json output
BOOST_TEST_MESSAGE("\n=== Real-World Polygon PIP Benchmark ===\n"<< formatTable(table))
BOOST_TEST_CONTEXT("Test Clearance")
wxString result
Test unit parsing edge cases and error handling.
@ SCH_GROUP_T
Definition typeinfo.h:174
@ SCH_LINE_T
Definition typeinfo.h:164
@ SCH_NO_CONNECT_T
Definition typeinfo.h:161
@ SCH_SYMBOL_T
Definition typeinfo.h:173
@ SCH_DIRECTIVE_LABEL_T
Definition typeinfo.h:172
@ SCH_LABEL_T
Definition typeinfo.h:168
@ SCH_SHEET_T
Definition typeinfo.h:176
@ SCH_SHAPE_T
Definition typeinfo.h:150
@ SCH_HIER_LABEL_T
Definition typeinfo.h:170
@ SCH_BUS_BUS_ENTRY_T
Definition typeinfo.h:163
@ SCH_TEXT_T
Definition typeinfo.h:152
@ SCH_BUS_WIRE_ENTRY_T
Definition typeinfo.h:162
@ SCH_BITMAP_T
Definition typeinfo.h:165
@ SCH_TEXTBOX_T
Definition typeinfo.h:153
@ SCH_GLOBAL_LABEL_T
Definition typeinfo.h:169
@ SCH_JUNCTION_T
Definition typeinfo.h:160