KiCad PCB EDA Suite
Loading...
Searching...
No Matches
board_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 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, you may find one here:
18 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
19 * or you may search the http://www.gnu.org website for the version 2 license,
20 * or you may write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22 */
23
24
25#pragma once
26
27#include <map>
28#include <memory>
29#include <mutex>
30#include <functional>
31#include <optional>
32#include <string>
33
34#include <wx/string.h>
35
36#include <reporter.h>
37#include <core/typeinfo.h>
38#include <tool/tool_manager.h>
39#include <pcb_io/pcb_io.h>
40
41class BOARD;
42class BOARD_ITEM;
43class FOOTPRINT;
44class KIID;
45class PCB_TEXT;
46class PCB_SHAPE;
47class ZONE;
48class PAD;
49class SHAPE_POLY_SET;
51
52namespace KI_TEST
53{
54class DUMMY_TOOL : public TOOL_BASE
55{
56public:
58 TOOL_BASE( BATCH, TOOL_MANAGER::MakeToolId( "" ), "testframework.dummytool" )
59 {};
60
61 void Reset( RESET_REASON aReason ) override {}
62};
63
64
77{
78public:
80
81 void DumpBoardToFile( BOARD& aBoard, const std::string& aName ) const;
82
83 const bool m_dump_boards;
84};
85
86
88{
89public:
90 enum COLOR
91 {
92 RED = 0,
95 };
96
98
99 void PrintProgress( const wxString& aMessage )
100 {
103
104 printf( "%s", (const char*) aMessage.c_str() );
105 fflush( stdout );
106
108 }
109
110
111 void Print( const wxString& aMessage )
112 {
115
116 printf( "%s", (const char*) aMessage.c_str() );
117 fflush( stdout );
118
120 }
121
122
123 void SetColor( COLOR color )
124 {
125 std::map<COLOR, wxString> colorMap = { { RED, "\033[0;31m" },
126 { GREEN, "\033[0;32m" },
127 { DEFAULT, "\033[0;37m" } };
128
129 printf( "%s", (const char*) colorMap[color].c_str() );
130 fflush( stdout );
131 }
132
133
134private:
136 {
137 printf( "\r\033[K" );
138 fflush( stdout );
139 }
140
142 std::mutex m_lock;
143};
144
145
147{
148public:
151
152
153 virtual REPORTER& Report( const wxString& aText,
154 SEVERITY aSeverity = RPT_SEVERITY_UNDEFINED ) override
155 {
156 switch( aSeverity )
157 {
159 m_log->SetColor( CONSOLE_LOG::RED );
160 m_log->Print( "ERROR | " );
161 break;
162
163 default: m_log->SetColor( CONSOLE_LOG::DEFAULT ); m_log->Print( " | " );
164 }
165
166 m_log->SetColor( CONSOLE_LOG::DEFAULT );
167 m_log->Print( aText + "\n" );
168 return *this;
169 }
170
171 virtual bool HasMessage() const override { return true; }
172
173private:
175};
176
177
179{
181
182 // These tests may well test specific versions of the board file format,
183 // so don't let it change accidentally in the files (e.g. by resaving in newer KiCad)!
184 std::optional<int> m_ExpectedBoardVersion;
185
186 // Be default, printing the board file is sufficent to identify the test case
187 friend std::ostream& operator<<( std::ostream& os, const BOARD_LOAD_TEST_CASE& aTestCase )
188 {
189 os << aTestCase.m_BoardFileRelativePath;
190 return os;
191 }
192};
193
194
195void LoadBoard( SETTINGS_MANAGER& aSettingsManager, const wxString& aRelPath,
196 std::unique_ptr<BOARD>& aBoard );
197
208BOARD_ITEM& RequireBoardItemWithTypeAndId( const BOARD& aBoard, KICAD_T aItemType,
209 const KIID& aID );
210
222void LoadAndTestBoardFile( const wxString aRelativePath, bool aRoundtrip,
223 std::function<void( BOARD& )> aBoardTestFunction,
224 std::optional<int> aExpectedBoardVersion = std::nullopt );
225
229void LoadAndTestFootprintFile( const wxString& aLibRelativePath, const wxString& aFpName,
230 bool aRoundtrip,
231 std::function<void( FOOTPRINT& )> aFootprintTestFunction,
232 std::optional<int> aExpectedFootprintVersion );
233
234
235void FillZones( BOARD* m_board );
236
237
241void CheckFootprint( const FOOTPRINT* expected, const FOOTPRINT* fp );
242
243void CheckFpPad( const PAD* expected, const PAD* pad );
244
245void CheckFpText( const PCB_TEXT* expected, const PCB_TEXT* text );
246
247void CheckFpShape( const PCB_SHAPE* expected, const PCB_SHAPE* shape );
248
249void CheckFpZone( const ZONE* expected, const ZONE* zone );
250
251void CheckShapePolySet( const SHAPE_POLY_SET* expected, const SHAPE_POLY_SET* polyset );
252
256void PrintBoardStats( const BOARD* aBoard, const std::string& aBoardName );
257
258
263{
264public:
270
272 m_errorCount( 0 ),
273 m_warningCount( 0 ),
274 m_infoCount( 0 )
275 {
276 }
277
278 REPORTER& Report( const wxString& aText, SEVERITY aSeverity = RPT_SEVERITY_UNDEFINED ) override;
279
280 bool HasMessage() const override { return !m_messages.empty(); }
281
282 EDA_UNITS GetUnits() const override { return EDA_UNITS::MM; }
283
284 void Clear() override
285 {
286 m_messages.clear();
287 m_errorCount = 0;
288 m_warningCount = 0;
289 m_infoCount = 0;
290 }
291
292 void PrintAllMessages( const std::string& aContext ) const;
293
294 int GetErrorCount() const { return m_errorCount; }
295 int GetWarningCount() const { return m_warningCount; }
296 const std::vector<MESSAGE>& GetMessages() const { return m_messages; }
297
298private:
299 std::vector<MESSAGE> m_messages;
303};
304
305
310std::unique_ptr<BOARD> LoadBoardWithCapture( PCB_IO& aIoPlugin, const std::string& aFilePath, REPORTER* aReporter );
311
312
321{
322public:
330 BOARD* LoadAndCache( const std::string& aFilePath, REPORTER* aReporter );
331
336 BOARD* GetCachedBoard( const std::string& aFilePath );
337
338protected:
339 BOARD* getCachedBoard( PCB_IO& aIoPlugin, const std::string& aFilePath, bool aForceReload, REPORTER* aReporter );
340
344 virtual BOARD* getCachedBoard( const std::string& aFilePath, bool aForceReload, REPORTER* aReporter ) = 0;
345
346private:
347 std::map<std::string, std::unique_ptr<BOARD>> m_boardCache;
348};
349
350} // namespace KI_TEST
A base class for any item which can be embedded within the BOARD container class, and therefore insta...
Definition board_item.h:84
Information pertinent to a Pcbnew printed circuit board.
Definition board.h:322
Definition kiid.h:49
void DumpBoardToFile(BOARD &aBoard, const std::string &aName) const
Manager for caching loaded boards in memory, to avoid repeatedly loading and parsing the same board.
std::map< std::string, std::unique_ptr< BOARD > > m_boardCache
BOARD * getCachedBoard(PCB_IO &aIoPlugin, const std::string &aFilePath, bool aForceReload, REPORTER *aReporter)
BOARD * LoadAndCache(const std::string &aFilePath, REPORTER *aReporter)
Load (or reload) board for the given file path and send the load messages to the given reporter.
BOARD * GetCachedBoard(const std::string &aFilePath)
Get a cached board for the given file path, or load it if not already cached, without forcing a reloa...
virtual BOARD * getCachedBoard(const std::string &aFilePath, bool aForceReload, REPORTER *aReporter)=0
Implementation of the board loader (probably plug in the PCB_IO plugin here)
bool HasMessage() const override
Returns true if any messages were reported.
void PrintAllMessages(const std::string &aContext) const
const std::vector< MESSAGE > & GetMessages() const
REPORTER & Report(const wxString &aText, SEVERITY aSeverity=RPT_SEVERITY_UNDEFINED) override
Report a string with a given severity.
EDA_UNITS GetUnits() const override
std::vector< MESSAGE > m_messages
void PrintProgress(const wxString &aMessage)
void SetColor(COLOR color)
void Print(const wxString &aMessage)
virtual bool HasMessage() const override
Returns true if any messages were reported.
CONSOLE_MSG_REPORTER(CONSOLE_LOG *log)
virtual REPORTER & Report(const wxString &aText, SEVERITY aSeverity=RPT_SEVERITY_UNDEFINED) override
Report a string with a given severity.
void Reset(RESET_REASON aReason) override
Bring the tool to a known, initial state.
Definition pad.h:55
A base class that BOARD loading and saving plugins should derive from.
Definition pcb_io.h:70
A pure virtual class used to derive REPORTER objects from.
Definition reporter.h:73
REPORTER()
Definition reporter.h:75
Represent a set of closed polygons.
friend class TOOL_MANAGER
Definition tool_base.h:156
RESET_REASON
Determine the reason of reset for a tool.
Definition tool_base.h:78
TOOL_BASE(TOOL_TYPE aType, TOOL_ID aId, const std::string &aName=std::string(""))
Definition tool_base.h:68
Handle a list of polygons defining a copper zone.
Definition zone.h:73
EDA_UNITS
Definition eda_units.h:48
void LoadBoard(SETTINGS_MANAGER &aSettingsManager, const wxString &aRelPath, std::unique_ptr< BOARD > &aBoard)
void CheckFootprint(const FOOTPRINT *expected, const FOOTPRINT *fp)
Helper method to check if two footprints are semantically the same.
void PrintBoardStats(const BOARD *aBoard, const std::string &aBoardName)
Print detailed board statistics for debugging using test-framework logging.
void FillZones(BOARD *m_board)
std::unique_ptr< BOARD > LoadBoardWithCapture(PCB_IO &aIoPlugin, const std::string &aFilePath, REPORTER *aReporter)
Attempt to load an board with a given IO plugin, capturing all reporter messages.
void CheckFpShape(const PCB_SHAPE *expected, const PCB_SHAPE *shape)
void LoadAndTestFootprintFile(const wxString &aLibRelativePath, const wxString &aFpName, bool aRoundtrip, std::function< void(FOOTPRINT &)> aFootprintTestFunction, std::optional< int > aExpectedFootprintVersion)
Same as LoadAndTestBoardFile, but for footprints.
void CheckFpPad(const PAD *expected, const PAD *pad)
void CheckFpZone(const ZONE *expected, const ZONE *zone)
void CheckFpText(const PCB_TEXT *expected, const PCB_TEXT *text)
void LoadAndTestBoardFile(const wxString aRelativePath, bool aRoundtrip, std::function< void(BOARD &)> aBoardTestFunction, std::optional< int > aExpectedBoardVersion)
Perform "some test" on a board file loaded from the path, then optionally save and reload and run the...
void CheckShapePolySet(const SHAPE_POLY_SET *expected, const SHAPE_POLY_SET *polyset)
BOARD_ITEM & RequireBoardItemWithTypeAndId(const BOARD &aBoard, KICAD_T aItemType, const KIID &aID)
Get an item from the given board with a certain type and UUID.
SEVERITY
@ RPT_SEVERITY_ERROR
@ RPT_SEVERITY_UNDEFINED
std::optional< int > m_ExpectedBoardVersion
friend std::ostream & operator<<(std::ostream &os, const BOARD_LOAD_TEST_CASE &aTestCase)
VECTOR3I expected(15, 30, 45)
@ BATCH
Tool that runs in the background without any user intervention.
Definition tool_base.h:52
KICAD_T
The set of class identification values stored in EDA_ITEM::m_structType.
Definition typeinfo.h:78