KiCad PCB EDA Suite
Loading...
Searching...
No Matches
test_api_enums.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 *
6 * This program is free software: you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation, either version 3 of the License, or (at your
9 * option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20#include <boost/test/unit_test.hpp>
21#include <boost/bimap.hpp>
22#include <magic_enum.hpp>
23#include <import_export.h>
25
26// Common
27#include <api/api_enums.h>
28#include <api/common/types/enums.pb.h>
29#include <core/typeinfo.h>
31#include <layer_ids.h>
32#include <stroke_params.h>
33
34// Board-specific
35#include <api/board/board_types.pb.h>
36#include <api/board/board_commands.pb.h>
38#include <padstack.h>
39#include <pcb_dimension.h>
40#include <pcb_track.h>
42#include <zones.h>
43#include <zone_settings.h>
44
45using namespace kiapi::common;
46
47BOOST_AUTO_TEST_SUITE( ApiEnums )
48
49
55template<typename KiCadEnum, typename ProtoEnum>
56void testEnums( bool aPartiallyMapped = false )
57{
58 boost::bimap<ProtoEnum, KiCadEnum> protoToKiCadSeen;
59 std::set<ProtoEnum> seenProtos;
60
61 for( ProtoEnum value : magic_enum::enum_values<ProtoEnum>() )
62 {
63 BOOST_TEST_CONTEXT( magic_enum::enum_type_name<ProtoEnum>() << "::"
64 << magic_enum::enum_name( value ) )
65 {
66 std::string name( magic_enum::enum_name( value ) );
67 auto splitPos = name.find_first_of( '_' );
68
69 // Protobuf enum names should be formatted as PREFIX_KEY
70 BOOST_REQUIRE_MESSAGE( splitPos != std::string::npos,
71 "Proto enum name doesn't have a prefix" );
72
73 std::string suffix = name.substr( splitPos );
74
75 // Protobuf enum with the value 0 should not map to anything
76 if( static_cast<int>( value ) == 0 )
77 {
78 BOOST_REQUIRE_MESSAGE( suffix.compare( "_UNKNOWN" ) == 0,
79 "Proto enum with value 0 must be named <PREFIX>_UNKNOWN" );
80 continue;
81 }
82
83 KiCadEnum result;
84 // Every non-unknown Proto value should map to a valid KiCad value
85 BOOST_REQUIRE_NO_THROW( result = ( FromProtoEnum<KiCadEnum, ProtoEnum>( value ) ) );
86
87 // There should be a 1:1 mapping
88 BOOST_REQUIRE( !protoToKiCadSeen.left.count( value ) );
89 protoToKiCadSeen.left.insert( { value, result } );
90 }
91 }
92
93 for( KiCadEnum value : magic_enum::enum_values<KiCadEnum>() )
94 {
95 BOOST_TEST_CONTEXT( magic_enum::enum_type_name<KiCadEnum>() << "::"
96 << magic_enum::enum_name( value ) )
97 {
98 ProtoEnum result;
99
100 if( aPartiallyMapped )
101 {
102 try
103 {
104 result = ToProtoEnum<KiCadEnum, ProtoEnum>( value );
105 }
107 {
108 // If it wasn't mapped from KiCad to Proto, it shouldn't be mapped the other way
109 BOOST_REQUIRE_MESSAGE( !protoToKiCadSeen.right.count( value ),
110 "Proto enum is mapped to this KiCad enum, but not vice versa" );
111 continue;
112 }
113 }
114 else
115 {
116 // Every KiCad enum value should map to a non-unknown Protobuf value
117 BOOST_REQUIRE_NO_THROW( result = ( ToProtoEnum<KiCadEnum, ProtoEnum>( value ) ) );
118 }
119
120 // Protobuf "unknown" should always be zero value by convention
121 BOOST_REQUIRE( result != static_cast<ProtoEnum>( 0 ) );
122
123 // There should be a 1:1 mapping
124 BOOST_REQUIRE( !seenProtos.count( result ) );
125 seenProtos.insert( result );
126
127 // Round-tripping should work
128 KiCadEnum roundTrip = FromProtoEnum<KiCadEnum, ProtoEnum>( result );
129 BOOST_REQUIRE( roundTrip == value );
130 }
131 }
132}
133
134BOOST_AUTO_TEST_CASE( HorizontalAlignment )
135{
136 testEnums<GR_TEXT_H_ALIGN_T, types::HorizontalAlignment>();
137}
138
139BOOST_AUTO_TEST_CASE( VerticalAlignment )
140{
141 testEnums<GR_TEXT_V_ALIGN_T, types::VerticalAlignment>();
142}
143
144BOOST_AUTO_TEST_CASE( StrokeLineStyle )
145{
146 testEnums<LINE_STYLE, types::StrokeLineStyle>();
147}
148
149BOOST_AUTO_TEST_CASE( KiCadObjectType )
150{
151 testEnums<KICAD_T, types::KiCadObjectType>( true );
152}
153
155{
156 testEnums<PCB_LAYER_ID, kiapi::board::types::BoardLayer>( true );
157}
158
159BOOST_AUTO_TEST_CASE( PadStackShape )
160{
161 testEnums<PAD_SHAPE, kiapi::board::types::PadStackShape>();
162}
163
164BOOST_AUTO_TEST_CASE( ZoneConnectionStyle )
165{
166 testEnums<ZONE_CONNECTION, kiapi::board::types::ZoneConnectionStyle>();
167}
168
170{
171 testEnums<PAD_ATTRIB, kiapi::board::types::PadType>();
172}
173
174BOOST_AUTO_TEST_CASE( PadStackType )
175{
176 testEnums<PADSTACK::MODE, kiapi::board::types::PadStackType>();
177}
178
180{
181 testEnums<PAD_DRILL_SHAPE, kiapi::board::types::DrillShape>();
182}
183
184BOOST_AUTO_TEST_CASE( UnconnectedLayerRemoval )
185{
186 testEnums<PADSTACK::UNCONNECTED_LAYER_MODE, kiapi::board::types::UnconnectedLayerRemoval>();
187}
188
190{
191 // VIATYPE::NOT_DEFINED is not mapped
192 testEnums<VIATYPE, kiapi::board::types::ViaType>( true );
193}
194
195BOOST_AUTO_TEST_CASE( IslandRemovalMode )
196{
197 testEnums<ISLAND_REMOVAL_MODE, kiapi::board::types::IslandRemovalMode>();
198}
199
200BOOST_AUTO_TEST_CASE( ZoneFillMode )
201{
202 testEnums<ZONE_FILL_MODE, kiapi::board::types::ZoneFillMode>();
203}
204
205BOOST_AUTO_TEST_CASE( ZoneBorderStyle )
206{
207 testEnums<ZONE_BORDER_DISPLAY_STYLE, kiapi::board::types::ZoneBorderStyle>();
208}
209
210BOOST_AUTO_TEST_CASE( PlacementRuleSourceType )
211{
212 testEnums<RULE_AREA_PLACEMENT_SOURCE_TYPE, kiapi::board::types::PlacementRuleSourceType>();
213}
214
215BOOST_AUTO_TEST_CASE( TeardropType )
216{
217 testEnums<TEARDROP_TYPE, kiapi::board::types::TeardropType>();
218}
219
220BOOST_AUTO_TEST_CASE( DimensionTextBorderStyle )
221{
222 testEnums<DIM_TEXT_BORDER, kiapi::board::types::DimensionTextBorderStyle>();
223}
224
225BOOST_AUTO_TEST_CASE( DimensionUnitFormat )
226{
227 testEnums<DIM_UNITS_FORMAT, kiapi::board::types::DimensionUnitFormat>();
228}
229
230BOOST_AUTO_TEST_CASE( DimensionArrowDirection )
231{
232 testEnums<DIM_ARROW_DIRECTION, kiapi::board::types::DimensionArrowDirection>();
233}
234
235BOOST_AUTO_TEST_CASE( DimensionPrecision )
236{
237 testEnums<DIM_PRECISION, kiapi::board::types::DimensionPrecision>();
238}
239
240BOOST_AUTO_TEST_CASE( DimensionTextPosition )
241{
242 testEnums<DIM_TEXT_POSITION, kiapi::board::types::DimensionTextPosition>();
243}
244
245BOOST_AUTO_TEST_CASE( DimensionUnit )
246{
247 testEnums<DIM_UNITS_MODE, kiapi::board::types::DimensionUnit>();
248}
249
250BOOST_AUTO_TEST_CASE( InactiveLayerDisplayMode )
251{
252 testEnums<HIGH_CONTRAST_MODE, kiapi::board::commands::InactiveLayerDisplayMode>();
253}
254
255BOOST_AUTO_TEST_CASE( NetColorDisplayMode )
256{
257 testEnums<NET_COLOR_MODE, kiapi::board::commands::NetColorDisplayMode>();
258}
259
260BOOST_AUTO_TEST_CASE( RatsnestDisplayMode )
261{
262 testEnums<RATSNEST_MODE, kiapi::board::commands::RatsnestDisplayMode>();
263}
264
265BOOST_AUTO_TEST_CASE( BoardStackupLayerType )
266{
267 testEnums<BOARD_STACKUP_ITEM_TYPE, kiapi::board::BoardStackupLayerType>();
268}
269
const char * name
Definition: DXF_plotter.cpp:59
An exception class to represent a WX assertion.
Definition: wx_assert.h:47
BOOST_AUTO_TEST_CASE(HorizontalAlignment)
void testEnums(bool aPartiallyMapped=false)
Checks if a KiCad enum has been properly mapped to a Protobuf enum.
BOOST_AUTO_TEST_SUITE(CadstarPartParser)
BOOST_AUTO_TEST_SUITE_END()
Class ZONE_SETTINGS used to handle zones parameters in dialogs.