KiCad PCB EDA Suite
Loading...
Searching...
No Matches
api_test_utils.h
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 * @author Jon Evans <[email protected]>
6 *
7 * This program is free software: you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation, either version 3 of the License, or (at your
10 * option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
19 */
20
21
22#ifndef KICAD_API_TEST_UTILS_H
23#define KICAD_API_TEST_UTILS_H
24
25#include <boost/test/unit_test.hpp>
26#include <boost/bimap.hpp>
27#include <google/protobuf/any.pb.h>
28#include <memory>
29#include <set>
30#include <magic_enum.hpp>
31#include <type_traits>
32#include <unordered_set>
33
34#include <api/api_enums.h>
35#include <eda_group.h>
36#include <eda_item.h>
38
46template <typename KiCadEnum, typename ProtoEnum>
47void testEnums( bool aPartiallyMapped = false, std::optional<KiCadEnum> aNullMapping = std::nullopt )
48{
49 boost::bimap<ProtoEnum, KiCadEnum> protoToKiCadSeen;
50 std::set<ProtoEnum> seenProtos;
51
52 for( ProtoEnum value : magic_enum::enum_values<ProtoEnum>() )
53 {
54 BOOST_TEST_CONTEXT( magic_enum::enum_type_name<ProtoEnum>() << "::" << magic_enum::enum_name( value ) )
55 {
56 std::string name( magic_enum::enum_name( value ) );
57 auto splitPos = name.find_first_of( '_' );
58
59 // Protobuf enum names should be formatted as PREFIX_KEY
60 BOOST_REQUIRE_MESSAGE( splitPos != std::string::npos, "Proto enum name doesn't have a prefix" );
61
62 std::string suffix = name.substr( splitPos );
63
64 // Protobuf enum with the value 0 should not map to anything
65 if( static_cast<int>( value ) == 0 )
66 {
67 BOOST_REQUIRE_MESSAGE( suffix.compare( "_UNKNOWN" ) == 0,
68 "Proto enum with value 0 must be named <PREFIX>_UNKNOWN" );
69 continue;
70 }
71
72 KiCadEnum result;
73 // Every non-unknown Proto value should map to a valid KiCad value
74 BOOST_REQUIRE_NO_THROW( result = ( FromProtoEnum<KiCadEnum, ProtoEnum>( value ) ) );
75
76 // There should be a 1:1 mapping
77 BOOST_REQUIRE( !protoToKiCadSeen.left.count( value ) );
78 protoToKiCadSeen.left.insert( { value, result } );
79 }
80 }
81
82 for( KiCadEnum value : magic_enum::enum_values<KiCadEnum>() )
83 {
84 BOOST_TEST_CONTEXT( magic_enum::enum_type_name<KiCadEnum>() << "::" << magic_enum::enum_name( value ) )
85 {
86 ProtoEnum result;
87
88 if( aPartiallyMapped )
89 {
90 try
91 {
93 }
95 {
96 // If it wasn't mapped from KiCad to Proto, it shouldn't be mapped the other way
97 BOOST_REQUIRE_MESSAGE( !protoToKiCadSeen.right.count( value ),
98 "Proto enum is mapped to this KiCad enum, but not vice versa" );
99 continue;
100 }
101 }
102 else
103 {
104 // Every KiCad enum value should map to a non-unknown Protobuf value
105 BOOST_REQUIRE_NO_THROW( result = ( ToProtoEnum<KiCadEnum, ProtoEnum>( value ) ) );
106 }
107
108 // Protobuf "unknown" should always be zero value by convention
109 BOOST_REQUIRE( ( result != static_cast<ProtoEnum>( 0 ) )
110 || ( aNullMapping && *aNullMapping == value ) );
111
112 // There should be a 1:1 mapping
113 BOOST_REQUIRE( !seenProtos.count( result ) );
114 seenProtos.insert( result );
115
116 // Round-tripping should work
118 BOOST_REQUIRE( roundTrip == value );
119 }
120 }
121}
122
123
124template<typename ProtoClass, typename KiCadClass, typename Factory>
125void testProtoFromKiCadObject( KiCadClass* aInput, Factory&& aCreateOutput )
126{
127 BOOST_TEST_CONTEXT( aInput->GetFriendlyName() << ": " << aInput->m_Uuid.AsStdString() )
128 {
129 constexpr bool isGroup = std::is_base_of_v<EDA_GROUP, std::decay_t<KiCadClass>>;
130
131 std::unordered_set<EDA_ITEM*> cachedItems;
132
133 if constexpr( isGroup )
134 cachedItems = aInput->GetItems();
135
136 google::protobuf::Any any;
137 BOOST_REQUIRE_NO_THROW( aInput->Serialize( any ) );
138
139 ProtoClass proto;
140 BOOST_REQUIRE_MESSAGE( any.UnpackTo( &proto ),
141 "Any message did not unpack into the requested type" );
142
143 std::unique_ptr<KiCadClass> output = aCreateOutput();
144
145 bool deserializeResult = false;
146 BOOST_REQUIRE_NO_THROW( deserializeResult = output->Deserialize( any ) );
147 BOOST_REQUIRE_MESSAGE( deserializeResult, "Deserialize failed" );
148
149 google::protobuf::Any outputAny;
150 BOOST_REQUIRE_NO_THROW( output->Serialize( outputAny ) );
151
152 if( !( outputAny.SerializeAsString() == any.SerializeAsString() ) )
153 {
154 BOOST_TEST_MESSAGE( "Input: " << any.Utf8DebugString() );
155 BOOST_TEST_MESSAGE( "Output: " << outputAny.Utf8DebugString() );
156 BOOST_TEST_FAIL( "Round-tripped protobuf does not match" );
157 }
158
159 if constexpr( isGroup )
160 {
161 if( output->GetItems() != cachedItems )
162 BOOST_TEST_FAIL( "Round-tripped group does not match" );
163
164 // Restore cached items into original group
165 for( EDA_ITEM* item : cachedItems )
166 aInput->AddItem( item );
167 }
168 else if( !output->operator==( *aInput ) )
169 {
170 BOOST_TEST_FAIL( "Round-tripped object does not match" );
171 }
172 }
173}
174
175
176template<typename ProtoClass, typename KiCadClass, typename ParentClass>
177void testProtoFromKiCadObject( KiCadClass* aInput, ParentClass* aParent, bool aStrict = true )
178{
179 if( aStrict )
180 {
182 [aParent]() { return std::make_unique<KiCadClass>( aParent ); } );
183 }
184 else
185 {
187 [aInput]()
188 {
189 std::unique_ptr<KiCadClass> cloned( static_cast<KiCadClass*>( aInput->Clone() ) );
190 return std::make_unique<KiCadClass>( *cloned );
191 } );
192 }
193}
194
195#endif //KICAD_API_TEST_UTILS_H
const char * name
KICOMMON_API types::KiCadObjectType ToProtoEnum(KICAD_T aValue)
KICOMMON_API KICAD_T FromProtoEnum(types::KiCadObjectType aValue)
Definition api_enums.cpp:55
void testEnums(bool aPartiallyMapped=false, std::optional< KiCadEnum > aNullMapping=std::nullopt)
Checks if a KiCad enum has been properly mapped to a Protobuf enum.
void testProtoFromKiCadObject(KiCadClass *aInput, Factory &&aCreateOutput)
A base class for most all the KiCad significant classes used in schematics and boards.
Definition eda_item.h:98
An exception class to represent a WX assertion.
Definition wx_assert.h:44
A type-safe container of any type.
Definition ki_any.h:92
static void roundTrip(const DIFF_VALUE &aValue)
BOOST_REQUIRE(intersection.has_value()==c.ExpectedIntersection.has_value())
BOOST_TEST_CONTEXT("Test Clearance")
BOOST_TEST_MESSAGE("Polyline has "<< chain.PointCount()<< " points")
wxString result
Test unit parsing edge cases and error handling.