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 {
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
138
143
148
149BOOST_AUTO_TEST_CASE( KiCadObjectType )
150{
152}
153
158
163
168
173
178
183
188
190{
191 // VIATYPE::NOT_DEFINED is not mapped
193}
194
199
204
209
214
219
224
229
234
239
244
249
254
259
264
269
const char * name
types::KiCadObjectType ToProtoEnum(KICAD_T aValue)
Definition api_enums.cpp:95
KICAD_T FromProtoEnum(types::KiCadObjectType aValue)
Definition api_enums.cpp:35
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.