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/board/board.pb.h>
29#include <api/common/types/enums.pb.h>
30#include <core/typeinfo.h>
32#include <layer_ids.h>
33#include <pin_type.h>
34#include <stroke_params.h>
36
37// Board-specific
38#include <api/board/board_types.pb.h>
39#include <api/board/board_commands.pb.h>
40#include <api/board/board_jobs.pb.h>
41#include <api/schematic/schematic_jobs.pb.h>
55#include <jobs/job_pcb_render.h>
56#include <drc/drc_item.h>
57#include <drc/drc_rule.h>
58#include <padstack.h>
59#include <pcb_dimension.h>
60#include <pcb_track.h>
61#include <plotprint_opts.h>
63#include <zones.h>
64#include <zone_settings.h>
65
66using namespace kiapi::common;
67
68BOOST_AUTO_TEST_SUITE( ApiEnums )
69
70
76template<typename KiCadEnum, typename ProtoEnum>
77void testEnums( bool aPartiallyMapped = false )
78{
79 boost::bimap<ProtoEnum, KiCadEnum> protoToKiCadSeen;
80 std::set<ProtoEnum> seenProtos;
81
82 for( ProtoEnum value : magic_enum::enum_values<ProtoEnum>() )
83 {
84 BOOST_TEST_CONTEXT( magic_enum::enum_type_name<ProtoEnum>() << "::"
85 << magic_enum::enum_name( value ) )
86 {
87 std::string name( magic_enum::enum_name( value ) );
88 auto splitPos = name.find_first_of( '_' );
89
90 // Protobuf enum names should be formatted as PREFIX_KEY
91 BOOST_REQUIRE_MESSAGE( splitPos != std::string::npos,
92 "Proto enum name doesn't have a prefix" );
93
94 std::string suffix = name.substr( splitPos );
95
96 // Protobuf enum with the value 0 should not map to anything
97 if( static_cast<int>( value ) == 0 )
98 {
99 BOOST_REQUIRE_MESSAGE( suffix.compare( "_UNKNOWN" ) == 0,
100 "Proto enum with value 0 must be named <PREFIX>_UNKNOWN" );
101 continue;
102 }
103
104 KiCadEnum result;
105 // Every non-unknown Proto value should map to a valid KiCad value
106 BOOST_REQUIRE_NO_THROW( result = ( FromProtoEnum<KiCadEnum, ProtoEnum>( value ) ) );
107
108 // There should be a 1:1 mapping
109 BOOST_REQUIRE( !protoToKiCadSeen.left.count( value ) );
110 protoToKiCadSeen.left.insert( { value, result } );
111 }
112 }
113
114 for( KiCadEnum value : magic_enum::enum_values<KiCadEnum>() )
115 {
116 BOOST_TEST_CONTEXT( magic_enum::enum_type_name<KiCadEnum>() << "::"
117 << magic_enum::enum_name( value ) )
118 {
119 ProtoEnum result;
120
121 if( aPartiallyMapped )
122 {
123 try
124 {
126 }
128 {
129 // If it wasn't mapped from KiCad to Proto, it shouldn't be mapped the other way
130 BOOST_REQUIRE_MESSAGE( !protoToKiCadSeen.right.count( value ),
131 "Proto enum is mapped to this KiCad enum, but not vice versa" );
132 continue;
133 }
134 }
135 else
136 {
137 // Every KiCad enum value should map to a non-unknown Protobuf value
138 BOOST_REQUIRE_NO_THROW( result = ( ToProtoEnum<KiCadEnum, ProtoEnum>( value ) ) );
139 }
140
141 // Protobuf "unknown" should always be zero value by convention
142 BOOST_REQUIRE( result != static_cast<ProtoEnum>( 0 ) );
143
144 // There should be a 1:1 mapping
145 BOOST_REQUIRE( !seenProtos.count( result ) );
146 seenProtos.insert( result );
147
148 // Round-tripping should work
149 KiCadEnum roundTrip = FromProtoEnum<KiCadEnum, ProtoEnum>( result );
150 BOOST_REQUIRE( roundTrip == value );
151 }
152 }
153}
154
159
164
169
170BOOST_AUTO_TEST_CASE( KiCadObjectType )
171{
173}
174
179
184
189
194
199
204
209
214
216{
217 // VIATYPE::NOT_DEFINED is not mapped
219}
220
225
230
235
240
245
250
255
260
265
270
275
280
285
290
295
300
305
310
311BOOST_AUTO_TEST_CASE( DesignRuleType )
312{
313 using ProtoType = kiapi::board::DrcErrorType;
314
381 {
382 ProtoType proto = ToProtoEnum<PCB_DRC_CODE, ProtoType>( value );
383 BOOST_REQUIRE( proto != ProtoType::DRCET_UNKNOWN );
384 BOOST_CHECK( ( FromProtoEnum<PCB_DRC_CODE, ProtoType>( proto ) == value ) );
385 }
386
387 BOOST_CHECK( ( FromProtoEnum<PCB_DRC_CODE, ProtoType>( ProtoType::DRCET_UNKNOWN )
388 == static_cast<PCB_DRC_CODE>( 0 ) ) );
389}
390
391BOOST_AUTO_TEST_CASE( CustomRuleConstraintType )
392{
393 using ProtoType = kiapi::board::CustomRuleConstraintType;
394
395 for( DRC_CONSTRAINT_T value : magic_enum::enum_values<DRC_CONSTRAINT_T>() )
396 {
397 if( value == NULL_CONSTRAINT )
398 continue;
399
400 ProtoType proto = ToProtoEnum<DRC_CONSTRAINT_T, ProtoType>( value );
401 BOOST_REQUIRE( proto != ProtoType::CRCT_UNKNOWN );
402 BOOST_CHECK( ( FromProtoEnum<DRC_CONSTRAINT_T, ProtoType>( proto ) == value ) );
403 }
404
405 BOOST_CHECK( ( FromProtoEnum<DRC_CONSTRAINT_T, ProtoType>( ProtoType::CRCT_UNKNOWN )
406 == NULL_CONSTRAINT ) );
407}
408
409BOOST_AUTO_TEST_CASE( CustomRuleConstraintOption )
410{
411 using ProtoType = kiapi::board::CustomRuleConstraintOption;
412
413 for( DRC_CONSTRAINT::OPTIONS value : magic_enum::enum_values<DRC_CONSTRAINT::OPTIONS>() )
414 {
416 continue;
417
418 ProtoType proto = ToProtoEnum<DRC_CONSTRAINT::OPTIONS, ProtoType>( value );
419 BOOST_REQUIRE( proto != ProtoType::CRCO_UNKNOWN );
420 BOOST_CHECK( ( FromProtoEnum<DRC_CONSTRAINT::OPTIONS, ProtoType>( proto ) == value ) );
421 }
422}
423
424BOOST_AUTO_TEST_CASE( CustomRuleDisallowType )
425{
426 using ProtoType = kiapi::board::CustomRuleDisallowType;
427
434 {
435 ProtoType proto = ToProtoEnum<DRC_DISALLOW_T, ProtoType>( value );
436 BOOST_REQUIRE( proto != ProtoType::CRDT_UNKNOWN );
437 BOOST_CHECK( ( FromProtoEnum<DRC_DISALLOW_T, ProtoType>( proto ) == value ) );
438 }
439}
440
445
450
455
460
465
470
475
476BOOST_AUTO_TEST_CASE( SvgPaginationMode )
477{
478 using Mode = kiapi::board::jobs::BoardJobPaginationMode;
479
481 == Mode::BJPM_ALL_LAYERS_ONE_PAGE ) );
483 == Mode::BJPM_EACH_LAYER_OWN_FILE ) );
484
485 BOOST_CHECK( ( FromProtoEnum<JOB_EXPORT_PCB_SVG::GEN_MODE, Mode>( Mode::BJPM_ALL_LAYERS_ONE_PAGE )
487 BOOST_CHECK( ( FromProtoEnum<JOB_EXPORT_PCB_SVG::GEN_MODE, Mode>( Mode::BJPM_EACH_LAYER_OWN_FILE )
489 BOOST_CHECK( ( FromProtoEnum<JOB_EXPORT_PCB_SVG::GEN_MODE, Mode>( Mode::BJPM_EACH_LAYER_OWN_PAGE )
491}
492
493BOOST_AUTO_TEST_CASE( DxfPaginationMode )
494{
495 using Mode = kiapi::board::jobs::BoardJobPaginationMode;
496
498 == Mode::BJPM_ALL_LAYERS_ONE_PAGE ) );
500 == Mode::BJPM_EACH_LAYER_OWN_FILE ) );
501
502 BOOST_CHECK( ( FromProtoEnum<JOB_EXPORT_PCB_DXF::GEN_MODE, Mode>( Mode::BJPM_ALL_LAYERS_ONE_PAGE )
504 BOOST_CHECK( ( FromProtoEnum<JOB_EXPORT_PCB_DXF::GEN_MODE, Mode>( Mode::BJPM_EACH_LAYER_OWN_FILE )
506 BOOST_CHECK( ( FromProtoEnum<JOB_EXPORT_PCB_DXF::GEN_MODE, Mode>( Mode::BJPM_EACH_LAYER_OWN_PAGE )
508}
509
514
519
520BOOST_AUTO_TEST_CASE( PsPaginationMode )
521{
522 using Mode = kiapi::board::jobs::BoardJobPaginationMode;
523
525 == Mode::BJPM_ALL_LAYERS_ONE_PAGE ) );
527 == Mode::BJPM_EACH_LAYER_OWN_FILE ) );
528
529 BOOST_CHECK( ( FromProtoEnum<JOB_EXPORT_PCB_PS::GEN_MODE, Mode>( Mode::BJPM_ALL_LAYERS_ONE_PAGE )
531 BOOST_CHECK( ( FromProtoEnum<JOB_EXPORT_PCB_PS::GEN_MODE, Mode>( Mode::BJPM_EACH_LAYER_OWN_FILE )
533 BOOST_CHECK( ( FromProtoEnum<JOB_EXPORT_PCB_PS::GEN_MODE, Mode>( Mode::BJPM_EACH_LAYER_OWN_PAGE )
535}
536
541
546
551
556
561
566
571
576
581
586
591
596
601
606
611
616
const char * name
types::KiCadObjectType ToProtoEnum(KICAD_T aValue)
KICAD_T FromProtoEnum(types::KiCadObjectType aValue)
Definition api_enums.cpp:41
An exception class to represent a WX assertion.
Definition wx_assert.h:47
PCB_DRC_CODE
Definition drc_item.h:38
@ DRCE_DISABLED_LAYER_ITEM
Definition drc_item.h:72
@ DRCE_SKEW_OUT_OF_RANGE
Definition drc_item.h:105
@ DRCE_DIFF_PAIR_GAP_OUT_OF_RANGE
Definition drc_item.h:107
@ DRCE_CREEPAGE
Definition drc_item.h:45
@ DRCE_HOLE_CLEARANCE
Definition drc_item.h:55
@ DRCE_SILK_EDGE_CLEARANCE
Definition drc_item.h:99
@ DRCE_ZONES_INTERSECT
Definition drc_item.h:48
@ DRCE_FOOTPRINT_FILTERS
Definition drc_item.h:80
@ DRCE_SILK_MASK_CLEARANCE
Definition drc_item.h:97
@ DRCE_VIA_DIAMETER
Definition drc_item.h:62
@ DRCE_UNCONNECTED_ITEMS
Definition drc_item.h:40
@ DRCE_TRACK_WIDTH
Definition drc_item.h:56
@ DRCE_PADSTACK
Definition drc_item.h:63
@ DRCE_MIRRORED_TEXT_ON_FRONT_LAYER
Definition drc_item.h:110
@ DRCE_LIB_FOOTPRINT_ISSUES
Definition drc_item.h:83
@ DRCE_OVERLAPPING_FOOTPRINTS
Definition drc_item.h:66
@ DRCE_INVALID_OUTLINE
Definition drc_item.h:73
@ DRCE_TRACK_ON_POST_MACHINED_LAYER
Definition drc_item.h:116
@ DRCE_TEXT_ON_EDGECUTS
Definition drc_item.h:43
@ DRCE_DRILL_OUT_OF_RANGE
Definition drc_item.h:61
@ DRCE_EDGE_CLEARANCE
Definition drc_item.h:47
@ DRCE_STARVED_THERMAL
Definition drc_item.h:50
@ DRCE_TRACK_SEGMENT_LENGTH
Definition drc_item.h:58
@ DRCE_SCHEMATIC_FIELDS_PARITY
Definition drc_item.h:120
@ DRCE_MISSING_COURTYARD
Definition drc_item.h:67
@ DRCE_TRACK_ANGLE
Definition drc_item.h:57
@ DRCE_TRACK_NOT_CENTERED_ON_VIA
Definition drc_item.h:118
@ DRCE_CLEARANCE
Definition drc_item.h:44
@ DRCE_ISOLATED_COPPER
Definition drc_item.h:49
@ DRCE_GENERIC_ERROR
Definition drc_item.h:91
@ DRCE_MISSING_TUNING_PROFILE
Definition drc_item.h:113
@ DRCE_DRILLED_HOLES_TOO_CLOSE
Definition drc_item.h:53
@ DRCE_ALLOWED_ITEMS
Definition drc_item.h:42
@ DRCE_COPPER_SLIVER
Definition drc_item.h:93
@ DRCE_PTH_IN_COURTYARD
Definition drc_item.h:70
@ DRCE_DIFF_PAIR_UNCOUPLED_LENGTH_TOO_LONG
Definition drc_item.h:108
@ DRCE_MICROVIA_DRILL_OUT_OF_RANGE
Definition drc_item.h:65
@ DRCE_SHORTING_ITEMS
Definition drc_item.h:41
@ DRCE_MALFORMED_COURTYARD
Definition drc_item.h:68
@ DRCE_DANGLING_VIA
Definition drc_item.h:51
@ DRCE_PADSTACK_INVALID
Definition drc_item.h:64
@ DRCE_UNRESOLVED_VARIABLE
Definition drc_item.h:88
@ DRCE_FOOTPRINT_TYPE_MISMATCH
Definition drc_item.h:82
@ DRCE_NONMIRRORED_TEXT_ON_BACK_LAYER
Definition drc_item.h:111
@ DRCE_DUPLICATE_FOOTPRINT
Definition drc_item.h:76
@ DRCE_DANGLING_TRACK
Definition drc_item.h:52
@ DRCE_TEXT_HEIGHT
Definition drc_item.h:101
@ DRCE_ASSERTION_FAILURE
Definition drc_item.h:89
@ DRCE_SOLDERMASK_BRIDGE
Definition drc_item.h:94
@ DRCE_DRILLED_HOLES_COLOCATED
Definition drc_item.h:54
@ DRCE_EXTRA_FOOTPRINT
Definition drc_item.h:77
@ DRCE_SILK_CLEARANCE
Definition drc_item.h:100
@ DRCE_LENGTH_OUT_OF_RANGE
Definition drc_item.h:104
@ DRCE_LIB_FOOTPRINT_MISMATCH
Definition drc_item.h:84
@ DRCE_NET_CONFLICT
Definition drc_item.h:78
@ DRCE_MISSING_FOOTPRINT
Definition drc_item.h:75
@ DRCE_PAD_TH_WITH_NO_HOLE
Definition drc_item.h:85
@ DRCE_FOOTPRINT
Definition drc_item.h:86
@ DRCE_GENERIC_WARNING
Definition drc_item.h:90
@ DRCE_TEXT_THICKNESS
Definition drc_item.h:102
@ DRCE_NPTH_IN_COURTYARD
Definition drc_item.h:71
@ DRCE_CONNECTION_WIDTH
Definition drc_item.h:60
@ DRCE_SCHEMATIC_PARITY
Definition drc_item.h:79
@ DRCE_TRACKS_CROSSING
Definition drc_item.h:46
@ DRCE_VIA_COUNT_OUT_OF_RANGE
Definition drc_item.h:106
@ DRCE_ANNULAR_WIDTH
Definition drc_item.h:59
DRC_DISALLOW_T
Definition drc_rule.h:95
@ DRC_DISALLOW_PADS
Definition drc_rule.h:101
@ DRC_DISALLOW_BURIED_VIAS
Definition drc_rule.h:99
@ DRC_DISALLOW_BLIND_VIAS
Definition drc_rule.h:98
@ DRC_DISALLOW_TEXTS
Definition drc_rule.h:103
@ DRC_DISALLOW_ZONES
Definition drc_rule.h:102
@ DRC_DISALLOW_HOLES
Definition drc_rule.h:105
@ DRC_DISALLOW_GRAPHICS
Definition drc_rule.h:104
@ DRC_DISALLOW_THROUGH_VIAS
Definition drc_rule.h:96
@ DRC_DISALLOW_FOOTPRINTS
Definition drc_rule.h:106
@ DRC_DISALLOW_TRACKS
Definition drc_rule.h:100
@ DRC_DISALLOW_MICRO_VIAS
Definition drc_rule.h:97
DRC_CONSTRAINT_T
Definition drc_rule.h:53
@ NULL_CONSTRAINT
Definition drc_rule.h:54
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_REQUIRE(intersection.has_value()==c.ExpectedIntersection.has_value())
BOOST_AUTO_TEST_SUITE_END()
BOOST_TEST_CONTEXT("Test Clearance")
wxString result
Test unit parsing edge cases and error handling.
Class ZONE_SETTINGS used to handle zones parameters in dialogs.