KiCad PCB EDA Suite
eeschema_test_utils.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) 2019-2022 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#include "eeschema_test_utils.h"
27
28#include <cstdlib>
29#include <memory>
30
31#include <eeschema/sch_io_mgr.h>
32#include <eeschema/sch_screen.h>
33#include <eeschema/schematic.h>
35
36
37#ifndef QA_EESCHEMA_DATA_LOCATION
38 #define QA_EESCHEMA_DATA_LOCATION "???"
39#endif
40
42{
43 const char* env = std::getenv( "KICAD_TEST_EESCHEMA_DATA_DIR" );
44 wxString fn;
45
46 if( !env )
47 {
48 // Use the compiled-in location of the data dir
49 // (i.e. where the files were at build time)
51 }
52 else
53 {
54 // Use whatever was given in the env var
55 fn << env;
56 }
57
58 // Ensure the string ends in / to force a directory interpretation
59 fn << "/";
60
61 return wxFileName{ fn };
62}
63
64
65void KI_TEST::SCHEMATIC_TEST_FIXTURE::LoadSchematic( const wxString& aBaseName )
66{
67 wxFileName fn = GetSchematicPath( aBaseName );
68
69 BOOST_TEST_MESSAGE( fn.GetFullPath() );
70
71 wxFileName pro( fn );
72 pro.SetExt( ProjectFileExtension );
73
74 // Schematic must be reset before a project is reloaded
76 m_manager.LoadProject( pro.GetFullPath() );
77
79
81 m_schematic.SetRoot( m_pi->Load( fn.GetFullPath(), &m_schematic ) );
82
83 BOOST_REQUIRE_EQUAL( m_pi->GetError().IsEmpty(), true );
84
86
87 SCH_SCREENS screens( m_schematic.Root() );
88
89 for( SCH_SCREEN* screen = screens.GetFirst(); screen; screen = screens.GetNext() )
90 screen->UpdateLocalLibSymbolLinks();
91
93
94 // Restore all of the loaded symbol instances from the root sheet screen.
97
100
101 sheets.AnnotatePowerSymbols();
102
103 // NOTE: This is required for multi-unit symbols to be correct
104 // Normally called from SCH_EDIT_FRAME::FixupJunctions() but could be refactored
105 for( SCH_SHEET_PATH& sheet : sheets )
106 sheet.UpdateAllScreenReferences();
107
108 // NOTE: SchematicCleanUp is not called; QA schematics must already be clean or else
109 // SchematicCleanUp must be freed from its UI dependencies.
110
111 m_schematic.ConnectionGraph()->Recalculate( sheets, true );
112}
113
114
115wxFileName KI_TEST::SCHEMATIC_TEST_FIXTURE::GetSchematicPath( const wxString& aBaseName )
116{
117 wxFileName fn = KI_TEST::GetEeschemaTestDataDir();
118 fn.AppendDir( "netlists" );
119 fn.AppendDir( aBaseName );
120 fn.SetName( aBaseName );
121 fn.SetExt( KiCadSchematicFileExtension );
122
123 return fn;
124}
125
126
127template <typename Exporter>
129{
130 wxFileName netFile = m_schematic.Prj().GetProjectFullName();
131
132 if( aTest )
133 netFile.SetName( netFile.GetName() + "_test" );
134
135 netFile.SetExt( NetlistFileExtension );
136
137 return netFile.GetFullPath();
138}
139
140
141template <typename Exporter>
143{
144 // In case of a crash the file may not have been deleted.
145 if( wxFileExists( GetNetlistPath( true ) ) )
146 wxRemoveFile( GetNetlistPath( true ) );
147
148 wxString errors;
149 WX_STRING_REPORTER reporter( &errors );
150 std::unique_ptr<Exporter> exporter = std::make_unique<Exporter>( &m_schematic );
151
152 bool success = exporter->WriteNetlist( GetNetlistPath( true ), GetNetlistOptions(), reporter );
153
154 BOOST_REQUIRE( success && errors.IsEmpty() );
155}
156
157
158template <typename Exporter>
160{
161 wxRemoveFile( GetNetlistPath( true ) );
162 m_schematic.Reset();
163}
164
165
166template <typename Exporter>
168{
169 LoadSchematic( aBaseName );
170 WriteNetlist();
171 CompareNetlists();
172 Cleanup();
173}
174
175
void Recalculate(const SCH_SHEET_LIST &aSheetList, bool aUnconditional=false, std::function< void(SCH_ITEM *)> *aChangedItemHandler=nullptr)
Updates the connection graph for the given list of sheets.
virtual void LoadSchematic(const wxString &aRelativePath)
SCHEMATIC m_schematic
‍Schematic to load
virtual wxFileName GetSchematicPath(const wxString &aBaseName)
virtual void SetElem(ELEM_T aIndex, _ELEM *aElem)
Definition: project.cpp:294
@ ELEM_SCH_SYMBOL_LIBS
Definition: project.h:207
void Reset()
Initialize this schematic to a blank one, unloading anything existing.
Definition: schematic.cpp:51
SCH_SHEET_PATH & CurrentSheet() const override
Definition: schematic.h:120
CONNECTION_GRAPH * ConnectionGraph() const override
Definition: schematic.h:130
SCH_SHEET_LIST GetSheets() const override
Builds and returns an updated schematic hierarchy TODO: can this be cached?
Definition: schematic.h:86
void SetRoot(SCH_SHEET *aRootSheet)
Initialize the schematic with a new root sheet.
Definition: schematic.cpp:104
void SetProject(PROJECT *aPrj)
Definition: schematic.cpp:76
SCH_SCREEN * RootScreen() const
Helper to retrieve the screen of the root sheet.
Definition: schematic.cpp:117
SCH_SHEET & Root() const
Definition: schematic.h:91
virtual const wxString & GetError() const
Return an error string to the caller.
Definition: sch_plugin.cpp:177
virtual SCH_SHEET * Load(const wxString &aFileName, SCHEMATIC *aSchematic, SCH_SHEET *aAppendToMe=nullptr, const STRING_UTF8_MAP *aProperties=nullptr)
Load information from some input file format that this SCH_PLUGIN implementation knows about,...
Definition: sch_plugin.cpp:51
Container class that holds multiple SCH_SCREEN objects in a hierarchy.
Definition: sch_screen.h:608
SCH_SCREEN * GetNext()
SCH_SCREEN * GetFirst()
int GetFileFormatVersionAtLoad() const
Definition: sch_screen.h:129
const std::vector< SYMBOL_INSTANCE_REFERENCE > & GetSymbolInstances() const
Definition: sch_screen.h:489
const std::vector< SCH_SHEET_INSTANCE > & GetSheetInstances() const
Definition: sch_screen.h:494
A container for handling SCH_SHEET_PATH objects in a flattened hierarchy.
void UpdateSheetInstances(const std::vector< SCH_SHEET_INSTANCE > &aSheetInstances)
Update all of the sheet instance information using aSheetInstances.
void UpdateSymbolInstances(const std::vector< SYMBOL_INSTANCE_REFERENCE > &aSymbolInstances)
Update all of the symbol instance information using aSymbolInstances.
void AnnotatePowerSymbols()
Silently annotate the not yet annotated power symbols of the entire hierarchy of the sheet path list.
Handle access to a stack of flattened SCH_SHEET objects by way of a path for creating a flattened sch...
void push_back(SCH_SHEET *aSheet)
Forwarded method from std::vector.
bool LoadProject(const wxString &aFullPath, bool aSetActive=true)
Loads a project or sets up a new project with a specified path.
PROJECT & Prj() const
A helper while we are not MDI-capable – return the one and only project.
void TestNetlist(const wxString &aBaseName)
virtual wxString GetNetlistPath(bool aTest=false)
A wrapper for reporting to a wxString object.
Definition: reporter.h:164
#define QA_EESCHEMA_DATA_LOCATION
const std::string ProjectFileExtension
const std::string NetlistFileExtension
const std::string KiCadSchematicFileExtension
wxFileName GetEeschemaTestDataDir()
Get the configured location of Eeschema test data.
void LoadSchematic(SETTINGS_MANAGER &aSettingsManager, const wxString &aRelPath, std::unique_ptr< SCHEMATIC > &aSchematic)
Definition of file extensions used in Kicad.