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, see <https://www.gnu.org/licenses/>.
18 */
19
20
21#pragma once
22
23#include <filesystem>
24#include <map>
25#include <memory>
26#include <mutex>
27#include <functional>
28#include <optional>
29#include <set>
30#include <string>
31#include <system_error>
32
33#include <wx/string.h>
34
35#include <boost/test/unit_test.hpp>
36
37#include <reporter.h>
38#include <core/typeinfo.h>
39#include <tool/tool_manager.h>
40#include <pcb_io/pcb_io.h>
41#include <pcb_track_types.h>
42
44
45class BOARD;
46class BOARD_ITEM;
47class FOOTPRINT;
48class KIID;
49class PCB_TEXT;
50class PCB_SHAPE;
51class ZONE;
52class PAD;
53class SHAPE_POLY_SET;
55
56
57/*
58 * Boost test printers
59 */
60std::ostream& boost_test_print_type( std::ostream& os, const VIATYPE& aViaType );
61
62
63namespace KI_TEST
64{
65class DUMMY_TOOL : public TOOL_BASE
66{
67public:
69 TOOL_BASE( BATCH, TOOL_MANAGER::MakeToolId( "" ), "testframework.dummytool" )
70 {};
71
72 void Reset( RESET_REASON aReason ) override {}
73};
74
75
85{
86public:
91 TEMPORARY_DIRECTORY( const std::string& aNamePrefix, const std::string aSuffix )
92 {
93 int i = 0;
94
95 // Find a unique directory name
96 while( true )
97 {
98 m_path = std::filesystem::temp_directory_path()
99 / ( aNamePrefix + std::to_string( i ) + aSuffix );
100
101 if( !std::filesystem::exists( m_path ) )
102 break;
103
104 i++;
105 }
106
107 wxASSERT( !std::filesystem::exists( m_path ) );
108 std::filesystem::create_directories( m_path );
109 }
110
111 ~TEMPORARY_DIRECTORY() { std::filesystem::remove_all( m_path ); }
112
113 const std::filesystem::path& GetPath() const { return m_path; }
114
115private:
116 std::filesystem::path m_path;
117};
118
119
129{
130public:
132
134 {
135 std::error_code ec;
136
137 for( const std::string& path : scan() )
138 {
139 if( m_preexisting.count( path ) == 0 )
140 std::filesystem::remove( path, ec );
141 }
142 }
143
144private:
145 static std::set<std::string> scan()
146 {
147 std::set<std::string> found;
148 std::error_code ec;
149
150 for( auto it = std::filesystem::recursive_directory_iterator( GetPcbnewTestDataDir(), ec );
151 !ec && it != std::filesystem::recursive_directory_iterator(); it.increment( ec ) )
152 {
153 if( it->path().extension() == ".kicad_dru" )
154 found.insert( it->path().string() );
155 }
156
157 return found;
158 }
159
160 std::set<std::string> m_preexisting;
161};
162
163
176{
177public:
178 BOARD_DUMPER();
179
180 void DumpBoardToFile( BOARD& aBoard, const std::string& aName ) const;
181
182 const bool m_dump_boards;
183};
184
185
187{
188public:
189 enum COLOR
190 {
191 RED = 0,
194 };
195
197
198 const wxString& GetLogContents() const
199 {
200 return m_logContents;
201 }
202
203 void PrintProgress( const wxString& aMessage )
204 {
206 return;
207
210
211 printf( "%s", (const char*) aMessage.c_str() );
212 fflush( stdout );
213
215 }
216
217
218 void Print( const wxString& aMessage )
219 {
222
224 {
225 printf( "%s", (const char*) aMessage.c_str() );
226 fflush( stdout );
227 }
228
229 m_logContents.append( aMessage );
231 }
232
233
234 void SetColor( COLOR color )
235 {
237 return;
238
239 std::map<COLOR, wxString> colorMap =
240 {
241 { RED, "\033[0;31m" },
242 { GREEN, "\033[0;32m" },
243 { DEFAULT, "\033[0;37m" }
244 };
245
246 printf( "%s", (const char*) colorMap[color].c_str() );
247 fflush( stdout );
248 }
249
250
251private:
253 {
255 return;
256
257 printf( "\r\033[K" );
258 fflush( stdout );
259 }
260
263 std::mutex m_lock;
265};
266
267
269{
270public:
273
274
275 virtual REPORTER& Report( const wxString& aText,
276 SEVERITY aSeverity = RPT_SEVERITY_UNDEFINED ) override
277 {
278 switch( aSeverity )
279 {
281 m_log->SetColor( CONSOLE_LOG::RED );
282 m_log->Print( "ERROR | " );
283 break;
284
285 default: m_log->SetColor( CONSOLE_LOG::DEFAULT ); m_log->Print( " | " );
286 }
287
288 m_log->SetColor( CONSOLE_LOG::DEFAULT );
289 m_log->Print( aText + "\n" );
290 return *this;
291 }
292
293 virtual bool HasMessage() const override { return true; }
294
295private:
297};
298
299
301{
303
304 // These tests may well test specific versions of the board file format,
305 // so don't let it change accidentally in the files (e.g. by resaving in newer KiCad)!
306 std::optional<int> m_ExpectedBoardVersion;
307
308 // Be default, printing the board file is sufficent to identify the test case
309 friend std::ostream& operator<<( std::ostream& os, const BOARD_LOAD_TEST_CASE& aTestCase )
310 {
311 os << aTestCase.m_BoardFileRelativePath;
312 return os;
313 }
314};
315
316
317void LoadBoard( SETTINGS_MANAGER& aSettingsManager, const wxString& aRelPath,
318 std::unique_ptr<BOARD>& aBoard );
319
330BOARD_ITEM& RequireBoardItemWithTypeAndId( const BOARD& aBoard, KICAD_T aItemType,
331 const KIID& aID );
332
344void LoadAndTestBoardFile( const wxString aRelativePath, bool aRoundtrip,
345 std::function<void( BOARD& )> aBoardTestFunction,
346 std::optional<int> aExpectedBoardVersion = std::nullopt );
347
351void LoadAndTestFootprintFile( const wxString& aLibRelativePath, const wxString& aFpName,
352 bool aRoundtrip,
353 std::function<void( FOOTPRINT& )> aFootprintTestFunction,
354 std::optional<int> aExpectedFootprintVersion );
355
356
357void FillZones( BOARD* m_board );
358
359
363void CheckFootprint( const FOOTPRINT* expected, const FOOTPRINT* fp );
364
365void CheckFpPad( const PAD* expected, const PAD* pad );
366
367void CheckFpText( const PCB_TEXT* expected, const PCB_TEXT* text );
368
369void CheckFpShape( const PCB_SHAPE* expected, const PCB_SHAPE* shape );
370
371void CheckFpZone( const ZONE* expected, const ZONE* zone );
372
373void CheckShapePolySet( const SHAPE_POLY_SET* expected, const SHAPE_POLY_SET* polyset );
374
378void PrintBoardStats( const BOARD* aBoard, const std::string& aBoardName );
379
380
385{
386public:
392
394 m_errorCount( 0 ),
395 m_warningCount( 0 ),
396 m_infoCount( 0 )
397 {
398 }
399
400 REPORTER& Report( const wxString& aText, SEVERITY aSeverity = RPT_SEVERITY_UNDEFINED ) override;
401
402 bool HasMessage() const override { return !m_messages.empty(); }
403
404 EDA_UNITS GetUnits() const override { return EDA_UNITS::MM; }
405
406 void Clear() override
407 {
408 m_messages.clear();
409 m_errorCount = 0;
410 m_warningCount = 0;
411 m_infoCount = 0;
412 }
413
414 void PrintAllMessages( const std::string& aContext ) const;
415
416 int GetErrorCount() const { return m_errorCount; }
417 int GetWarningCount() const { return m_warningCount; }
418 const std::vector<MESSAGE>& GetMessages() const { return m_messages; }
419
420private:
421 std::vector<MESSAGE> m_messages;
425};
426
427
432std::unique_ptr<BOARD> LoadBoardWithCapture( PCB_IO& aIoPlugin, const std::string& aFilePath, REPORTER* aReporter );
433
434
443{
444public:
452 BOARD* LoadAndCache( const std::string& aFilePath, REPORTER* aReporter );
453
458 BOARD* GetCachedBoard( const std::string& aFilePath );
459
460protected:
461 BOARD* getCachedBoard( PCB_IO& aIoPlugin, const std::string& aFilePath, bool aForceReload, REPORTER* aReporter );
462
466 virtual BOARD* getCachedBoard( const std::string& aFilePath, bool aForceReload, REPORTER* aReporter ) = 0;
467
468private:
469 std::map<std::string, std::unique_ptr<BOARD>> m_boardCache;
470};
471
472} // namespace KI_TEST
General utilities for PCB file IO for QA programs.
std::ostream & boost_test_print_type(std::ostream &os, const VIATYPE &aViaType)
A base class for any item which can be embedded within the BOARD container class, and therefore insta...
Definition board_item.h:81
Information pertinent to a Pcbnew printed circuit board.
Definition board.h:372
Definition kiid.h:44
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)
const wxString & GetLogContents() const
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.
static std::set< std::string > scan()
std::set< std::string > m_preexisting
const std::filesystem::path & GetPath() const
TEMPORARY_DIRECTORY(const std::string &aNamePrefix, const std::string aSuffix)
Create a temporary directory with a given prefix and suffix.
std::filesystem::path m_path
Definition pad.h:61
A base class that BOARD loading and saving plugins should derive from.
Definition pcb_io.h:75
A pure virtual class used to derive REPORTER objects from.
Definition reporter.h:71
REPORTER()
Definition reporter.h:73
Represent a set of closed polygons.
friend class TOOL_MANAGER
Definition tool_base.h:152
RESET_REASON
Determine the reason of reset for a tool.
Definition tool_base.h:74
TOOL_BASE(TOOL_TYPE aType, TOOL_ID aId, const std::string &aName=std::string(""))
Definition tool_base.h:64
Handle a list of polygons defining a copper zone.
Definition zone.h:70
EDA_UNITS
Definition eda_units.h:44
std::string GetPcbnewTestDataDir()
Utility which returns a path to the data directory where the test board files are stored.
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.
Declarations of types for tracks and vias.
VIATYPE
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)
std::string path
VECTOR3I expected(15, 30, 45)
@ BATCH
Tool that runs in the background without any user intervention.
Definition tool_base.h:48
KICAD_T
The set of class identification values stored in EDA_ITEM::m_structType.
Definition typeinfo.h:71