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