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-2020 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
98 sheets.AnnotatePowerSymbols();
100 // NOTE: This is required for multi-unit symbols to be correct
101 // Normally called from SCH_EDIT_FRAME::FixupJunctions() but could be refactored
102 for( SCH_SHEET_PATH& sheet : sheets )
103 sheet.UpdateAllScreenReferences();
104
105 // NOTE: SchematicCleanUp is not called; QA schematics must already be clean or else
106 // SchematicCleanUp must be freed from its UI dependencies.
107
108 m_schematic.ConnectionGraph()->Recalculate( sheets, true );
109}
110
111
112wxFileName KI_TEST::SCHEMATIC_TEST_FIXTURE::GetSchematicPath( const wxString& aBaseName )
113{
114 wxFileName fn = KI_TEST::GetEeschemaTestDataDir();
115 fn.AppendDir( "netlists" );
116 fn.AppendDir( aBaseName );
117 fn.SetName( aBaseName );
118 fn.SetExt( KiCadSchematicFileExtension );
119
120 return fn;
121}
122
123
124template <typename Exporter>
126{
127 wxFileName netFile = m_schematic.Prj().GetProjectFullName();
128
129 if( aTest )
130 netFile.SetName( netFile.GetName() + "_test" );
131
132 netFile.SetExt( NetlistFileExtension );
133
134 return netFile.GetFullPath();
135}
136
137
138template <typename Exporter>
140{
141 // In case of a crash the file may not have been deleted.
142 if( wxFileExists( GetNetlistPath( true ) ) )
143 wxRemoveFile( GetNetlistPath( true ) );
144
145 auto exporter = std::make_unique<Exporter>( &m_schematic );
146 BOOST_REQUIRE_EQUAL( exporter->WriteNetlist( GetNetlistPath( true ), GetNetlistOptions() ),
147 true );
148}
149
150
151template <typename Exporter>
153{
154 wxRemoveFile( GetNetlistPath( true ) );
155 m_schematic.Reset();
156}
157
158
159template <typename Exporter>
161{
162 LoadSchematic( aBaseName );
163 WriteNetlist();
164 CompareNetlists();
165 Cleanup();
166}
167
168
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:252
@ ELEM_SCH_SYMBOL_LIBS
Definition: project.h:204
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:121
CONNECTION_GRAPH * ConnectionGraph() const override
Definition: schematic.h:131
SCH_SHEET_LIST GetSheets() const override
Builds and returns an updated schematic hierarchy TODO: can this be cached?
Definition: schematic.h:87
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:92
virtual SCH_SHEET * Load(const wxString &aFileName, SCHEMATIC *aSchematic, SCH_SHEET *aAppendToMe=nullptr, const PROPERTIES *aProperties=nullptr)
Load information from some input file format that this SCH_PLUGIN implementation knows about,...
Definition: sch_plugin.cpp:51
virtual const wxString & GetError() const
Return an error string to the caller.
Definition: sch_plugin.cpp:177
Container class that holds multiple SCH_SCREEN objects in a hierarchy.
Definition: sch_screen.h:595
SCH_SCREEN * GetNext()
SCH_SCREEN * GetFirst()
const std::vector< SYMBOL_INSTANCE_REFERENCE > & GetSymbolInstances() const
Definition: sch_screen.h:487
const std::vector< SCH_SHEET_INSTANCE > & GetSheetInstances() const
Definition: sch_screen.h:492
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)
#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.
Definition of file extensions used in Kicad.