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 (C) 2024 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 <api/board/board_types.pb.h>
30#include <core/typeinfo.h>
32#include <layer_ids.h>
33#include <stroke_params.h>
34
35// Board-specific
36#include <padstack.h>
37#include <pcb_dimension.h>
38#include <pcb_track.h>
39#include <zones.h>
40#include <zone_settings.h>
41
42using namespace kiapi::common;
43
44BOOST_AUTO_TEST_SUITE( ApiEnums )
45
46
52template<typename KiCadEnum, typename ProtoEnum>
53void testEnums( bool aPartiallyMapped = false )
54{
55 boost::bimap<ProtoEnum, KiCadEnum> protoToKiCadSeen;
56 std::set<ProtoEnum> seenProtos;
57
58 for( ProtoEnum value : magic_enum::enum_values<ProtoEnum>() )
59 {
60 BOOST_TEST_CONTEXT( magic_enum::enum_type_name<ProtoEnum>() << "::"
61 << magic_enum::enum_name( value ) )
62 {
63 std::string name( magic_enum::enum_name( value ) );
64 auto splitPos = name.find_first_of( '_' );
65
66 // Protobuf enum names should be formatted as PREFIX_KEY
67 BOOST_REQUIRE_MESSAGE( splitPos != std::string::npos,
68 "Proto enum name doesn't have a prefix" );
69
70 std::string suffix = name.substr( splitPos );
71
72 // Protobuf enum with the value 0 should not map to anything
73 if( static_cast<int>( value ) == 0 )
74 {
75 BOOST_REQUIRE_MESSAGE( suffix.compare( "_UNKNOWN" ) == 0,
76 "Proto enum with value 0 must be named <PREFIX>_UNKNOWN" );
77 continue;
78 }
79
80 KiCadEnum result;
81 // Every non-unknown Proto value should map to a valid KiCad value
82 BOOST_REQUIRE_NO_THROW( result = ( FromProtoEnum<KiCadEnum, ProtoEnum>( value ) ) );
83
84 // There should be a 1:1 mapping
85 BOOST_REQUIRE( !protoToKiCadSeen.left.count( value ) );
86 protoToKiCadSeen.left.insert( { value, result } );
87 }
88 }
89
90 for( KiCadEnum value : magic_enum::enum_values<KiCadEnum>() )
91 {
92 BOOST_TEST_CONTEXT( magic_enum::enum_type_name<KiCadEnum>() << "::"
93 << magic_enum::enum_name( value ) )
94 {
95 ProtoEnum result;
96
97 if( aPartiallyMapped )
98 {
99 try
100 {
101 result = ToProtoEnum<KiCadEnum, ProtoEnum>( value );
102 }
104 {
105 // If it wasn't mapped from KiCad to Proto, it shouldn't be mapped the other way
106 BOOST_REQUIRE_MESSAGE( !protoToKiCadSeen.right.count( value ),
107 "Proto enum is mapped to this KiCad enum, but not vice versa" );
108 continue;
109 }
110 }
111 else
112 {
113 // Every KiCad enum value should map to a non-unknown Protobuf value
114 BOOST_REQUIRE_NO_THROW( result = ( ToProtoEnum<KiCadEnum, ProtoEnum>( value ) ) );
115 }
116
117 // Protobuf "unknown" should always be zero value by convention
118 BOOST_REQUIRE( result != static_cast<ProtoEnum>( 0 ) );
119
120 // There should be a 1:1 mapping
121 BOOST_REQUIRE( !seenProtos.count( result ) );
122 seenProtos.insert( result );
123
124 // Round-tripping should work
125 KiCadEnum roundTrip = FromProtoEnum<KiCadEnum, ProtoEnum>( result );
126 BOOST_REQUIRE( roundTrip == value );
127 }
128 }
129}
130
131BOOST_AUTO_TEST_CASE( HorizontalAlignment )
132{
133 testEnums<GR_TEXT_H_ALIGN_T, types::HorizontalAlignment>();
134}
135
136BOOST_AUTO_TEST_CASE( VerticalAlignment )
137{
138 testEnums<GR_TEXT_V_ALIGN_T, types::VerticalAlignment>();
139}
140
141BOOST_AUTO_TEST_CASE( StrokeLineStyle )
142{
143 testEnums<LINE_STYLE, types::StrokeLineStyle>();
144}
145
146BOOST_AUTO_TEST_CASE( KiCadObjectType )
147{
148 testEnums<KICAD_T, types::KiCadObjectType>( true );
149}
150
152{
153 testEnums<PCB_LAYER_ID, kiapi::board::types::BoardLayer>( true );
154}
155
156BOOST_AUTO_TEST_CASE( PadStackShape )
157{
158 testEnums<PAD_SHAPE, kiapi::board::types::PadStackShape>();
159}
160
161BOOST_AUTO_TEST_CASE( ZoneConnectionStyle )
162{
163 testEnums<ZONE_CONNECTION, kiapi::board::types::ZoneConnectionStyle>();
164}
165
167{
168 testEnums<PAD_ATTRIB, kiapi::board::types::PadType>();
169}
170
171BOOST_AUTO_TEST_CASE( PadStackType )
172{
173 testEnums<PADSTACK::MODE, kiapi::board::types::PadStackType>();
174}
175
177{
178 testEnums<PAD_DRILL_SHAPE, kiapi::board::types::DrillShape>();
179}
180
181BOOST_AUTO_TEST_CASE( UnconnectedLayerRemoval )
182{
183 testEnums<PADSTACK::UNCONNECTED_LAYER_MODE, kiapi::board::types::UnconnectedLayerRemoval>();
184}
185
187{
188 // VIATYPE::NOT_DEFINED is not mapped
189 testEnums<VIATYPE, kiapi::board::types::ViaType>( true );
190}
191
192BOOST_AUTO_TEST_CASE( IslandRemovalMode )
193{
194 testEnums<ISLAND_REMOVAL_MODE, kiapi::board::types::IslandRemovalMode>();
195}
196
197BOOST_AUTO_TEST_CASE( ZoneFillMode )
198{
199 testEnums<ZONE_FILL_MODE, kiapi::board::types::ZoneFillMode>();
200}
201
202BOOST_AUTO_TEST_CASE( ZoneBorderStyle )
203{
204 testEnums<ZONE_BORDER_DISPLAY_STYLE, kiapi::board::types::ZoneBorderStyle>();
205}
206
207BOOST_AUTO_TEST_CASE( PlacementRuleSourceType )
208{
209 testEnums<RULE_AREA_PLACEMENT_SOURCE_TYPE, kiapi::board::types::PlacementRuleSourceType>();
210}
211
212BOOST_AUTO_TEST_CASE( TeardropType )
213{
214 testEnums<TEARDROP_TYPE, kiapi::board::types::TeardropType>();
215}
216
217BOOST_AUTO_TEST_CASE( DimensionTextBorderStyle )
218{
219 testEnums<DIM_TEXT_BORDER, kiapi::board::types::DimensionTextBorderStyle>();
220}
221
222BOOST_AUTO_TEST_CASE( DimensionUnitFormat )
223{
224 testEnums<DIM_UNITS_FORMAT, kiapi::board::types::DimensionUnitFormat>();
225}
226
227BOOST_AUTO_TEST_CASE( DimensionArrowDirection )
228{
229 testEnums<DIM_ARROW_DIRECTION, kiapi::board::types::DimensionArrowDirection>();
230}
231
232BOOST_AUTO_TEST_CASE( DimensionPrecision )
233{
234 testEnums<DIM_PRECISION, kiapi::board::types::DimensionPrecision>();
235}
236
237BOOST_AUTO_TEST_CASE( DimensionTextPosition )
238{
239 testEnums<DIM_TEXT_POSITION, kiapi::board::types::DimensionTextPosition>();
240}
241
242BOOST_AUTO_TEST_CASE( DimensionUnit )
243{
244 testEnums<DIM_UNITS_MODE, kiapi::board::types::DimensionUnit>();
245}
246
const char * name
Definition: DXF_plotter.cpp:57
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.