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>
34
35// Board-specific
36#include <api/board/board_types.pb.h>
37#include <api/board/board_commands.pb.h>
39#include <padstack.h>
40#include <pcb_dimension.h>
41#include <pcb_track.h>
43#include <zones.h>
44#include <zone_settings.h>
45
46using namespace kiapi::common;
47
48BOOST_AUTO_TEST_SUITE( ApiEnums )
49
50
56template<typename KiCadEnum, typename ProtoEnum>
57void testEnums( bool aPartiallyMapped = false )
58{
59 boost::bimap<ProtoEnum, KiCadEnum> protoToKiCadSeen;
60 std::set<ProtoEnum> seenProtos;
61
62 for( ProtoEnum value : magic_enum::enum_values<ProtoEnum>() )
63 {
64 BOOST_TEST_CONTEXT( magic_enum::enum_type_name<ProtoEnum>() << "::"
65 << magic_enum::enum_name( value ) )
66 {
67 std::string name( magic_enum::enum_name( value ) );
68 auto splitPos = name.find_first_of( '_' );
69
70 // Protobuf enum names should be formatted as PREFIX_KEY
71 BOOST_REQUIRE_MESSAGE( splitPos != std::string::npos,
72 "Proto enum name doesn't have a prefix" );
73
74 std::string suffix = name.substr( splitPos );
75
76 // Protobuf enum with the value 0 should not map to anything
77 if( static_cast<int>( value ) == 0 )
78 {
79 BOOST_REQUIRE_MESSAGE( suffix.compare( "_UNKNOWN" ) == 0,
80 "Proto enum with value 0 must be named <PREFIX>_UNKNOWN" );
81 continue;
82 }
83
84 KiCadEnum result;
85 // Every non-unknown Proto value should map to a valid KiCad value
86 BOOST_REQUIRE_NO_THROW( result = ( FromProtoEnum<KiCadEnum, ProtoEnum>( value ) ) );
87
88 // There should be a 1:1 mapping
89 BOOST_REQUIRE( !protoToKiCadSeen.left.count( value ) );
90 protoToKiCadSeen.left.insert( { value, result } );
91 }
92 }
93
94 for( KiCadEnum value : magic_enum::enum_values<KiCadEnum>() )
95 {
96 BOOST_TEST_CONTEXT( magic_enum::enum_type_name<KiCadEnum>() << "::"
97 << magic_enum::enum_name( value ) )
98 {
99 ProtoEnum result;
100
101 if( aPartiallyMapped )
102 {
103 try
104 {
106 }
108 {
109 // If it wasn't mapped from KiCad to Proto, it shouldn't be mapped the other way
110 BOOST_REQUIRE_MESSAGE( !protoToKiCadSeen.right.count( value ),
111 "Proto enum is mapped to this KiCad enum, but not vice versa" );
112 continue;
113 }
114 }
115 else
116 {
117 // Every KiCad enum value should map to a non-unknown Protobuf value
118 BOOST_REQUIRE_NO_THROW( result = ( ToProtoEnum<KiCadEnum, ProtoEnum>( value ) ) );
119 }
120
121 // Protobuf "unknown" should always be zero value by convention
122 BOOST_REQUIRE( result != static_cast<ProtoEnum>( 0 ) );
123
124 // There should be a 1:1 mapping
125 BOOST_REQUIRE( !seenProtos.count( result ) );
126 seenProtos.insert( result );
127
128 // Round-tripping should work
129 KiCadEnum roundTrip = FromProtoEnum<KiCadEnum, ProtoEnum>( result );
130 BOOST_REQUIRE( roundTrip == value );
131 }
132 }
133}
134
139
144
149
150BOOST_AUTO_TEST_CASE( KiCadObjectType )
151{
153}
154
159
164
169
174
179
184
189
191{
192 // VIATYPE::NOT_DEFINED is not mapped
194}
195
200
205
210
215
220
225
230
235
240
245
250
255
260
265
270
275
const char * name
types::KiCadObjectType ToProtoEnum(KICAD_T aValue)
Definition api_enums.cpp:96
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.