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, 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"
25
27#include <sch_io/sch_io.h>
29
30#include <cstdlib>
31#include <memory>
32
35#include <eeschema/sch_screen.h>
36#include <eeschema/schematic.h>
39
40
42 m_schematic( nullptr ),
43 m_pi( SCH_IO_MGR::FindPlugin( SCH_IO_MGR::SCH_KICAD ) ),
44 m_manager( true )
45{
46}
47
48
50{
51}
52
53
54void KI_TEST::SCHEMATIC_TEST_FIXTURE::LoadSchematic( const wxString& aBaseName )
55{
56 wxFileName fn = GetSchematicPath( aBaseName );
57
58 BOOST_TEST_CHECKPOINT( "Loading schematic " << fn.GetFullPath() );
59
60 wxFileName pro( fn );
62
63 // Schematic must be reset before a project is reloaded
64 m_schematic.release();
65
66 m_manager.LoadProject( pro.GetFullPath() );
67
68 m_manager.Prj().SetElem( PROJECT::ELEM::SCH_SYMBOL_LIBS, nullptr );
69
70 m_schematic = std::make_unique<SCHEMATIC>( &m_manager.Prj() );
71 m_schematic->SetRoot( m_pi->LoadSchematicFile( fn.GetFullPath(), m_schematic.get() ) );
72
73 BOOST_REQUIRE_EQUAL( m_pi->GetError().IsEmpty(), true );
74
75 m_schematic->CurrentSheet().push_back( &m_schematic->Root() );
77 SCH_SCREENS screens( m_schematic->Root() );
78
79 for( SCH_SCREEN* screen = screens.GetFirst(); screen; screen = screens.GetNext() )
80 screen->UpdateLocalLibSymbolLinks();
81
82 SCH_SHEET_LIST sheets = m_schematic->BuildSheetListSortedByPageNumbers();
83
84 // Restore all of the loaded symbol instances from the root sheet screen.
85 if( m_schematic->RootScreen()->GetFileFormatVersionAtLoad() < 20221002 )
86 sheets.UpdateSymbolInstanceData( m_schematic->RootScreen()->GetSymbolInstances() );
87
88 if( m_schematic->RootScreen()->GetFileFormatVersionAtLoad() < 20221110 )
89 sheets.UpdateSheetInstanceData( m_schematic->RootScreen()->GetSheetInstances() );
90
91 if( m_schematic->RootScreen()->GetFileFormatVersionAtLoad() < 20221206 )
92 {
93 for( SCH_SCREEN* screen = screens.GetFirst(); screen; screen = screens.GetNext() )
94 screen->MigrateSimModels();
95 }
96
97 if( m_schematic->RootScreen()->GetFileFormatVersionAtLoad() < 20230221 )
98 screens.FixLegacyPowerSymbolMismatches();
99
100 sheets.AnnotatePowerSymbols();
101
102 // NOTE: This is required for multi-unit symbols to be correct
103 for( SCH_SHEET_PATH& sheet : sheets )
104 sheet.UpdateAllScreenReferences();
105
106 // NOTE: SchematicCleanUp is not called; QA schematics must already be clean or else
107 // SchematicCleanUp must be freed from its UI dependencies.
108
109 std::unordered_set<SCH_SCREEN*> all_screens;
110
111 for( const SCH_SHEET_PATH& path : sheets )
112 all_screens.insert( path.LastScreen() );
113
114 SCH_RULE_AREA::UpdateRuleAreasInScreens( all_screens, nullptr );
115
116 m_schematic->ConnectionGraph()->Recalculate( sheets, true );
117}
118
119
120wxFileName KI_TEST::SCHEMATIC_TEST_FIXTURE::GetSchematicPath( const wxString& aBaseName )
121{
122 wxFileName fn( KI_TEST::GetEeschemaTestDataDir() );
123 fn.AppendDir( "netlists" );
124 fn.AppendDir( aBaseName );
125 fn.SetName( aBaseName );
127
128 return fn;
129}
130
131
132template <typename Exporter>
134{
135 wxFileName netFile = m_schematic->Prj().GetProjectFullName();
136
137 if( aTest )
138 netFile.SetName( netFile.GetName() + "_test" );
139
140 netFile.SetExt( FILEEXT::NetlistFileExtension );
141
142 return netFile.GetFullPath();
143}
144
145
146template <typename Exporter>
148{
149 wxString netlistPath = GetNetlistPath( true );
150 BOOST_TEST_CHECKPOINT( "Writing netlist " << netlistPath );
151
152 // In case of a crash the file may not have been deleted.
153 if( wxFileExists( netlistPath ) )
154 wxRemoveFile( netlistPath );
155
156 WX_STRING_REPORTER reporter;
157 std::unique_ptr<Exporter> exporter = std::make_unique<Exporter>( m_schematic.get() );
158
159 bool success = exporter->WriteNetlist( netlistPath, GetNetlistOptions(), reporter );
160
161 BOOST_REQUIRE( success && reporter.GetMessages().IsEmpty() );
162}
163
164
165template <typename Exporter>
167{
168 wxRemoveFile( GetNetlistPath( true ) );
169 m_schematic->Reset();
170}
171
172
173template <typename Exporter>
175{
176 LoadSchematic( aBaseName );
177 WriteNetlist();
178 CompareNetlists();
179 Cleanup();
180}
181
182
virtual void LoadSchematic(const wxString &aRelativePath)
virtual wxFileName GetSchematicPath(const wxString &aBaseName)
A factory which returns an instance of a SCH_IO.
Definition: sch_io_mgr.h:51
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:753
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...
void TestNetlist(const wxString &aBaseName)
virtual wxString GetNetlistPath(bool aTest=false)
A wrapper for reporting to a wxString object.
Definition: reporter.h:190
const wxString & GetMessages() const
Definition: reporter.cpp:83
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())
Definition of file extensions used in Kicad.