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_track.h>
38#include <zones.h>
39
40using namespace kiapi::common;
41
42BOOST_AUTO_TEST_SUITE( ApiEnums )
43
44
50template<typename KiCadEnum, typename ProtoEnum>
51void testEnums( bool aPartiallyMapped = false )
52{
53 boost::bimap<ProtoEnum, KiCadEnum> protoToKiCadSeen;
54 std::set<ProtoEnum> seenProtos;
55
56 for( ProtoEnum value : magic_enum::enum_values<ProtoEnum>() )
57 {
58 BOOST_TEST_CONTEXT( magic_enum::enum_type_name<ProtoEnum>() << "::"
59 << magic_enum::enum_name( value ) )
60 {
61 std::string name( magic_enum::enum_name( value ) );
62 auto splitPos = name.find_first_of( '_' );
63
64 // Protobuf enum names should be formatted as PREFIX_KEY
65 BOOST_REQUIRE_MESSAGE( splitPos != std::string::npos,
66 "Proto enum name doesn't have a prefix" );
67
68 std::string suffix = name.substr( splitPos );
69
70 // Protobuf enum with the value 0 should not map to anything
71 if( static_cast<int>( value ) == 0 )
72 {
73 BOOST_REQUIRE_MESSAGE( suffix.compare( "_UNKNOWN" ) == 0,
74 "Proto enum with value 0 must be named <PREFIX>_UNKNOWN" );
75 continue;
76 }
77
78 KiCadEnum result;
79 // Every non-unknown Proto value should map to a valid KiCad value
80 BOOST_REQUIRE_NO_THROW( result = ( FromProtoEnum<KiCadEnum, ProtoEnum>( value ) ) );
81
82 // There should be a 1:1 mapping
83 BOOST_REQUIRE( !protoToKiCadSeen.left.count( value ) );
84 protoToKiCadSeen.left.insert( { value, result } );
85 }
86 }
87
88 for( KiCadEnum value : magic_enum::enum_values<KiCadEnum>() )
89 {
90 BOOST_TEST_CONTEXT( magic_enum::enum_type_name<KiCadEnum>() << "::"
91 << magic_enum::enum_name( value ) )
92 {
93 ProtoEnum result;
94
95 if( aPartiallyMapped )
96 {
97 try
98 {
99 result = ToProtoEnum<KiCadEnum, ProtoEnum>( value );
100 }
102 {
103 // If it wasn't mapped from KiCad to Proto, it shouldn't be mapped the other way
104 BOOST_REQUIRE_MESSAGE( !protoToKiCadSeen.right.count( value ),
105 "Proto enum is mapped to this KiCad enum, but not vice versa" );
106 continue;
107 }
108 }
109 else
110 {
111 // Every KiCad enum value should map to a non-unknown Protobuf value
112 BOOST_REQUIRE_NO_THROW( result = ( ToProtoEnum<KiCadEnum, ProtoEnum>( value ) ) );
113 }
114
115 // Protobuf "unknown" should always be zero value by convention
116 BOOST_REQUIRE( result != static_cast<ProtoEnum>( 0 ) );
117
118 // There should be a 1:1 mapping
119 BOOST_REQUIRE( !seenProtos.count( result ) );
120 seenProtos.insert( result );
121
122 // Round-tripping should work
123 KiCadEnum roundTrip = FromProtoEnum<KiCadEnum, ProtoEnum>( result );
124 BOOST_REQUIRE( roundTrip == value );
125 }
126 }
127}
128
129BOOST_AUTO_TEST_CASE( HorizontalAlignment )
130{
131 testEnums<GR_TEXT_H_ALIGN_T, types::HorizontalAlignment>();
132}
133
134BOOST_AUTO_TEST_CASE( VerticalAlignment )
135{
136 testEnums<GR_TEXT_V_ALIGN_T, types::VerticalAlignment>();
137}
138
139BOOST_AUTO_TEST_CASE( StrokeLineStyle )
140{
141 testEnums<LINE_STYLE, types::StrokeLineStyle>();
142}
143
144BOOST_AUTO_TEST_CASE( KiCadObjectType )
145{
146 testEnums<KICAD_T, types::KiCadObjectType>( true );
147}
148
150{
151 testEnums<PCB_LAYER_ID, kiapi::board::types::BoardLayer>( true );
152}
153
154BOOST_AUTO_TEST_CASE( PadStackShape )
155{
156 testEnums<PAD_SHAPE, kiapi::board::types::PadStackShape>();
157}
158
159BOOST_AUTO_TEST_CASE( ZoneConnectionStyle )
160{
161 testEnums<ZONE_CONNECTION, kiapi::board::types::ZoneConnectionStyle>();
162}
163
165{
166 testEnums<PAD_ATTRIB, kiapi::board::types::PadType>();
167}
168
169BOOST_AUTO_TEST_CASE( PadStackType )
170{
171 testEnums<PADSTACK::MODE, kiapi::board::types::PadStackType>();
172}
173
174BOOST_AUTO_TEST_CASE( UnconnectedLayerRemoval )
175{
176 testEnums<PADSTACK::UNCONNECTED_LAYER_MODE, kiapi::board::types::UnconnectedLayerRemoval>();
177}
178
180{
181 // VIATYPE::NOT_DEFINED is not mapped
182 testEnums<VIATYPE, kiapi::board::types::ViaType>( true );
183}
184
185
186BOOST_AUTO_TEST_SUITE_END()
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)
#define BOOST_TEST_CONTEXT(A)