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 <pin_type.h>
33#include <stroke_params.h>
35
36// Board-specific
37#include <api/board/board_types.pb.h>
38#include <api/board/board_commands.pb.h>
40#include <padstack.h>
41#include <pcb_dimension.h>
42#include <pcb_track.h>
44#include <zones.h>
45#include <zone_settings.h>
46
47using namespace kiapi::common;
48
49BOOST_AUTO_TEST_SUITE( ApiEnums )
50
51
57template<typename KiCadEnum, typename ProtoEnum>
58void testEnums( bool aPartiallyMapped = false )
59{
60 boost::bimap<ProtoEnum, KiCadEnum> protoToKiCadSeen;
61 std::set<ProtoEnum> seenProtos;
62
63 for( ProtoEnum value : magic_enum::enum_values<ProtoEnum>() )
64 {
65 BOOST_TEST_CONTEXT( magic_enum::enum_type_name<ProtoEnum>() << "::"
66 << magic_enum::enum_name( value ) )
67 {
68 std::string name( magic_enum::enum_name( value ) );
69 auto splitPos = name.find_first_of( '_' );
70
71 // Protobuf enum names should be formatted as PREFIX_KEY
72 BOOST_REQUIRE_MESSAGE( splitPos != std::string::npos,
73 "Proto enum name doesn't have a prefix" );
74
75 std::string suffix = name.substr( splitPos );
76
77 // Protobuf enum with the value 0 should not map to anything
78 if( static_cast<int>( value ) == 0 )
79 {
80 BOOST_REQUIRE_MESSAGE( suffix.compare( "_UNKNOWN" ) == 0,
81 "Proto enum with value 0 must be named <PREFIX>_UNKNOWN" );
82 continue;
83 }
84
85 KiCadEnum result;
86 // Every non-unknown Proto value should map to a valid KiCad value
87 BOOST_REQUIRE_NO_THROW( result = ( FromProtoEnum<KiCadEnum, ProtoEnum>( value ) ) );
88
89 // There should be a 1:1 mapping
90 BOOST_REQUIRE( !protoToKiCadSeen.left.count( value ) );
91 protoToKiCadSeen.left.insert( { value, result } );
92 }
93 }
94
95 for( KiCadEnum value : magic_enum::enum_values<KiCadEnum>() )
96 {
97 BOOST_TEST_CONTEXT( magic_enum::enum_type_name<KiCadEnum>() << "::"
98 << magic_enum::enum_name( value ) )
99 {
100 ProtoEnum result;
101
102 if( aPartiallyMapped )
103 {
104 try
105 {
107 }
109 {
110 // If it wasn't mapped from KiCad to Proto, it shouldn't be mapped the other way
111 BOOST_REQUIRE_MESSAGE( !protoToKiCadSeen.right.count( value ),
112 "Proto enum is mapped to this KiCad enum, but not vice versa" );
113 continue;
114 }
115 }
116 else
117 {
118 // Every KiCad enum value should map to a non-unknown Protobuf value
119 BOOST_REQUIRE_NO_THROW( result = ( ToProtoEnum<KiCadEnum, ProtoEnum>( value ) ) );
120 }
121
122 // Protobuf "unknown" should always be zero value by convention
123 BOOST_REQUIRE( result != static_cast<ProtoEnum>( 0 ) );
124
125 // There should be a 1:1 mapping
126 BOOST_REQUIRE( !seenProtos.count( result ) );
127 seenProtos.insert( result );
128
129 // Round-tripping should work
130 KiCadEnum roundTrip = FromProtoEnum<KiCadEnum, ProtoEnum>( result );
131 BOOST_REQUIRE( roundTrip == value );
132 }
133 }
134}
135
140
145
150
151BOOST_AUTO_TEST_CASE( KiCadObjectType )
152{
154}
155
160
165
170
175
180
185
190
195
197{
198 // VIATYPE::NOT_DEFINED is not mapped
200}
201
206
211
216
221
226
231
236
241
246
251
256
261
266
271
276
281
const char * name
types::KiCadObjectType ToProtoEnum(KICAD_T aValue)
Definition api_enums.cpp:97
KICAD_T FromProtoEnum(types::KiCadObjectType aValue)
Definition api_enums.cpp:36
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_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.