KiCad PCB EDA Suite
Loading...
Searching...
No Matches
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 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#include "eeschema_test_utils.h"
21
23#include <sch_io/sch_io.h>
25
26#include <cstdlib>
27#include <memory>
28
31#include <eeschema/sch_screen.h>
32#include <eeschema/schematic.h>
35
36
38 m_schematic( nullptr ),
39 m_pi( SCH_IO_MGR::FindPlugin( SCH_IO_MGR::SCH_KICAD ) )
40{
41}
42
43
47
48
50{
51 wxFileName fn( aFn );
52
53 BOOST_TEST_CHECKPOINT( "Loading schematic " << fn.GetFullPath() );
55 wxFileName pro( fn );
57
58 // The plugin retains loaded root sheets for one schematic lifetime.
59 m_pi.reset( SCH_IO_MGR::FindPlugin( SCH_IO_MGR::SCH_KICAD ) );
60
61 // Schematic must be reset before a project is reloaded
62 m_schematic.reset();
63
64 m_manager.LoadProject( pro.GetFullPath() );
65
66 m_manager.Prj().SetElem( PROJECT::ELEM::LEGACY_SYMBOL_LIBS, nullptr );
67
68 m_schematic = std::make_unique<SCHEMATIC>( &m_manager.Prj() );
69 m_schematic->Reset();
70 SCH_SHEET* defaultSheet = m_schematic->GetTopLevelSheet( 0 );
71
72 SCH_SHEET* root = m_pi->LoadSchematicFile( fn.GetFullPath(), m_schematic.get() );
73 m_schematic->AddTopLevelSheet( root );
74
75 m_schematic->RemoveTopLevelSheet( defaultSheet );
76 delete defaultSheet;
78 BOOST_REQUIRE_EQUAL( m_pi->GetError().IsEmpty(), true );
80
81 SCH_SCREENS screens( m_schematic->Root() );
82
83 for( SCH_SCREEN* screen = screens.GetFirst(); screen; screen = screens.GetNext() )
84 screen->UpdateLocalLibSymbolLinks();
85
86 SCH_SHEET_LIST sheets = m_schematic->BuildSheetListSortedByPageNumbers();
87
88 // Restore all of the loaded symbol instances from the root sheet screen.
89 if( m_schematic->RootScreen()->GetFileFormatVersionAtLoad() < 20221002 )
90 sheets.UpdateSymbolInstanceData( m_schematic->RootScreen()->GetSymbolInstances() );
91
92 if( m_schematic->RootScreen()->GetFileFormatVersionAtLoad() < 20221110 )
93 sheets.UpdateSheetInstanceData( m_schematic->RootScreen()->GetSheetInstances() );
94
95 if( m_schematic->RootScreen()->GetFileFormatVersionAtLoad() < 20221206 )
96 {
97 for( SCH_SCREEN* screen = screens.GetFirst(); screen; screen = screens.GetNext() )
98 screen->MigrateSimModels();
99 }
100
101 if( m_schematic->RootScreen()->GetFileFormatVersionAtLoad() < 20230221 )
102 screens.FixLegacyPowerSymbolMismatches();
103
104 sheets.AnnotatePowerSymbols();
105
106 // NOTE: This is required for multi-unit symbols to be correct
107 for( SCH_SHEET_PATH& sheet : sheets )
108 sheet.UpdateAllScreenReferences();
109
110 // NOTE: SchematicCleanUp is not called; QA schematics must already be clean or else
111 // SchematicCleanUp must be freed from its UI dependencies.
112
113 std::unordered_set<SCH_SCREEN*> all_screens;
114
115 for( const SCH_SHEET_PATH& path : sheets )
116 all_screens.insert( path.LastScreen() );
117
118 SCH_RULE_AREA::UpdateRuleAreasInScreens( all_screens, nullptr );
119
120 m_schematic->ConnectionGraph()->Recalculate( sheets, true );
121}
122
123
124wxFileName KI_TEST::SCHEMATIC_TEST_FIXTURE::SchematicQAPath( const wxString& aBaseName )
125{
126 wxFileName fn( KI_TEST::GetEeschemaTestDataDir() );
127 fn.AppendDir( "netlists" );
128 fn.AppendDir( aBaseName );
129 fn.SetName( aBaseName );
131
132 return fn;
133}
134
135
136template <typename Exporter>
138{
139 wxFileName netFile = m_schematic->Project().GetProjectFullName();
140
141 if( aTest )
142 netFile.SetName( netFile.GetName() + "_test" );
143
144 netFile.SetExt( FILEEXT::NetlistFileExtension );
145
146 return netFile.GetFullPath();
147}
148
149
150template <typename Exporter>
152{
153 wxString netlistPath = GetNetlistPath( true );
154 BOOST_TEST_CHECKPOINT( "Writing netlist " << netlistPath );
155
156 // In case of a crash the file may not have been deleted.
157 if( wxFileExists( netlistPath ) )
158 wxRemoveFile( netlistPath );
159
161 std::unique_ptr<Exporter> exporter = std::make_unique<Exporter>( m_schematic.get() );
162
163 bool success = exporter->WriteNetlist( netlistPath, GetNetlistOptions(), reporter );
164
165 BOOST_REQUIRE( success && reporter.GetMessages().IsEmpty() );
166}
167
168
169template <typename Exporter>
171{
172 wxRemoveFile( GetNetlistPath( true ) );
173 m_schematic->Reset();
174}
175
176
177template <typename Exporter>
179{
180 LoadSchematic( SchematicQAPath( aBaseName ) );
181 WriteNetlist();
183 Cleanup();
184}
185
186
std::unique_ptr< SCHEMATIC > m_schematic
virtual void LoadSchematic(const wxFileName &aFn)
virtual wxFileName SchematicQAPath(const wxString &aBaseName)
@ LEGACY_SYMBOL_LIBS
Definition project.h:70
A factory which returns an instance of a SCH_IO.
Definition sch_io_mgr.h:50
static std::vector< std::pair< SCH_RULE_AREA *, SCH_SCREEN * > > UpdateRuleAreasInScreens(std::unordered_set< SCH_SCREEN * > &screens, KIGFX::SCH_VIEW *view)
Update all rule area connectvity / caches in the given sheet paths.
Container class that holds multiple SCH_SCREEN objects in a hierarchy.
Definition sch_screen.h:758
A container for handling SCH_SHEET_PATH objects in a flattened hierarchy.
void UpdateSheetInstanceData(const std::vector< SCH_SHEET_INSTANCE > &aSheetInstances)
Update all of the sheet instance information using aSheetInstances.
void AnnotatePowerSymbols()
Silently annotate the not yet annotated power symbols of the entire hierarchy of the sheet path list.
void UpdateSymbolInstanceData(const std::vector< SCH_SYMBOL_INSTANCE > &aSymbolInstances)
Update all of the symbol instance information using aSymbolInstances.
Handle access to a stack of flattened SCH_SHEET objects by way of a path for creating a flattened sch...
Sheet symbol placed in a schematic, and is the entry point for a sub schematic.
Definition sch_sheet.h:48
void TestNetlist(const wxString &aBaseName)
virtual void CompareNetlists()=0
virtual wxString GetNetlistPath(bool aTest=false)
A wrapper for reporting to a wxString object.
Definition reporter.h:242
static const std::string NetlistFileExtension
static const std::string ProjectFileExtension
static const std::string KiCadSchematicFileExtension
void LoadSchematic(SETTINGS_MANAGER &aSettingsManager, const wxString &aRelPath, std::unique_ptr< SCHEMATIC > &aSchematic)
std::string GetEeschemaTestDataDir()
Get the configured location of Eeschema test data.
BOOST_REQUIRE(intersection.has_value()==c.ExpectedIntersection.has_value())
std::string path
IbisParser parser & reporter
Definition of file extensions used in Kicad.