KiCad PCB EDA Suite
Loading...
Searching...
No Matches
test_net_chain_save_root_only.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 3
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 * https://www.gnu.org/licenses/gpl-3.0.en.html
19 */
20
21#include <boost/test/unit_test.hpp>
22
23#include <connection_graph.h>
25#include <sch_netchain.h>
26#include <sch_screen.h>
27#include <sch_sheet.h>
28#include <schematic.h>
30#include <locale_io.h>
32
33#include <wx/ffile.h>
34#include <wx/filename.h>
35#include <wx/stdpaths.h>
36
37
38// Test backdoor to push a fully-formed committed net chain into the graph without
39// needing a real RebuildNetChains() pass. Friend-declared in connection_graph.h.
41 std::unique_ptr<SCH_NETCHAIN> aChain )
42{
43 aGraph.m_committedNetChains.push_back( std::move( aChain ) );
44}
45
46
48{
51 {
52 wxString tempDir = wxStandardPaths::Get().GetTempDir();
53 wxString projectPath =
54 tempDir + wxFileName::GetPathSeparator() + wxT( "test_netchain_save.kicad_pro" );
55 m_tempFiles.push_back( projectPath );
56
57 m_settingsManager.LoadProject( projectPath.ToStdString() );
58 m_schematic = std::make_unique<SCHEMATIC>( nullptr );
60 m_schematic->SetProject( m_project );
61 }
62
64 {
65 for( const wxString& file : m_tempFiles )
66 {
67 if( wxFileExists( file ) )
68 wxRemoveFile( file );
69 }
70
71 m_schematic.reset();
72 }
73
74 wxString GetTempFileName( const wxString& aPrefix )
75 {
76 wxString tempDir = wxStandardPaths::Get().GetTempDir();
77 wxString fileName = wxFileName::CreateTempFileName(
78 tempDir + wxFileName::GetPathSeparator() + aPrefix );
79 m_tempFiles.push_back( fileName );
80 return fileName;
81 }
82
84 std::unique_ptr<SCHEMATIC> m_schematic;
86 std::vector<wxString> m_tempFiles;
87};
88
89
97BOOST_FIXTURE_TEST_CASE( NetChainSavedOnlyOnRootSheetFile, NETCHAIN_SAVE_ROOT_FIXTURE )
98{
100
101 m_schematic->CreateDefaultScreens();
102
103 std::vector<SCH_SHEET*> topSheets = m_schematic->GetTopLevelSheets();
104 BOOST_REQUIRE( !topSheets.empty() );
105
106 SCH_SHEET* topSheet = topSheets[0];
107 SCH_SCREEN* topScreen = topSheet->GetScreen();
108 BOOST_REQUIRE( topScreen );
109
110 SCH_SHEET* subSheet = new SCH_SHEET( m_schematic.get() );
111 SCH_SCREEN* subScreen = new SCH_SCREEN( m_schematic.get() );
112 subSheet->SetName( "SubSheet" );
113 subSheet->SetScreen( subScreen );
114 subSheet->SetFileName( "subsheet.kicad_sch" );
115 subScreen->SetFileName( "subsheet.kicad_sch" );
116 topScreen->Append( subSheet );
117
118 m_schematic->RefreshHierarchy();
119
120 auto chain = std::make_unique<SCH_NETCHAIN>();
121 chain->SetName( wxT( "TEST_CHAIN" ) );
122 chain->AddNet( wxT( "/NET_A" ) );
123 chain->AddNet( wxT( "/NET_B" ) );
124 chain->SetTerminalRefs( wxT( "U1" ), wxT( "1" ), wxT( "U2" ), wxT( "2" ) );
125
126 boost_test_inject_committed_net_chain( *m_schematic->ConnectionGraph(), std::move( chain ) );
127
128 BOOST_REQUIRE_EQUAL( m_schematic->ConnectionGraph()->GetCommittedNetChains().size(), 1u );
129
130 wxString topFileName = GetTempFileName( "netchain_main" );
131 topFileName += ".kicad_sch";
132 m_tempFiles.push_back( topFileName );
133
134 wxString subFileName = GetTempFileName( "netchain_sub" );
135 subFileName += ".kicad_sch";
136 m_tempFiles.push_back( subFileName );
137
139 BOOST_CHECK_NO_THROW(
140 io.SaveSchematicFile( topFileName, topSheet, m_schematic.get() ) );
141 BOOST_CHECK_NO_THROW(
142 io.SaveSchematicFile( subFileName, subSheet, m_schematic.get() ) );
143
144 BOOST_REQUIRE( wxFileExists( topFileName ) );
145 BOOST_REQUIRE( wxFileExists( subFileName ) );
146
147 auto countOccurrences = []( const wxString& aHaystack, const wxString& aNeedle )
148 {
149 size_t count = 0;
150 size_t pos = 0;
151
152 while( ( pos = aHaystack.find( aNeedle, pos ) ) != wxString::npos )
153 {
154 ++count;
155 pos += aNeedle.length();
156 }
157
158 return count;
159 };
160
161 wxString topContents;
162 {
163 wxFFile readback( topFileName, "rb" );
164 BOOST_REQUIRE( readback.IsOpened() && readback.ReadAll( &topContents ) );
165 }
166
167 wxString subContents;
168 {
169 wxFFile readback( subFileName, "rb" );
170 BOOST_REQUIRE( readback.IsOpened() && readback.ReadAll( &subContents ) );
171 }
172
173 BOOST_CHECK_EQUAL( countOccurrences( topContents, wxT( "(net_chain" ) ), 1u );
174 BOOST_CHECK_MESSAGE( topContents.Contains( wxT( "TEST_CHAIN" ) ),
175 "Top-level sheet file is missing the chain name" );
176
177 BOOST_CHECK_EQUAL( countOccurrences( subContents, wxT( "(net_chain" ) ), 0u );
178 BOOST_CHECK_MESSAGE( !subContents.Contains( wxT( "TEST_CHAIN" ) ),
179 "Sub-sheet file unexpectedly contains the chain name" );
180}
std::vector< std::unique_ptr< SCH_NETCHAIN > > m_committedNetChains
CONNECTION_GRAPH(SCHEMATIC *aSchematic=nullptr)
Instantiate the current locale within a scope in which you are expecting exceptions to be thrown.
Definition locale_io.h:41
Container for project specific data.
Definition project.h:66
A SCH_IO derivation for loading schematic files using the new s-expression file format.
void SaveSchematicFile(const wxString &aFileName, SCH_SHEET *aSheet, SCHEMATIC *aSchematic, const std::map< std::string, UTF8 > *aProperties=nullptr) override
Write aSchematic to a storage file in a format that this SCH_IO implementation knows about,...
void Append(SCH_ITEM *aItem, bool aUpdateLibSymbol=true)
void SetFileName(const wxString &aFileName)
Set the file name for this screen to aFileName.
Sheet symbol placed in a schematic, and is the entry point for a sub schematic.
Definition sch_sheet.h:48
void SetFileName(const wxString &aFilename)
Definition sch_sheet.h:380
void SetName(const wxString &aName)
Definition sch_sheet.h:141
SCH_SCREEN * GetScreen() const
Definition sch_sheet.h:143
void SetScreen(SCH_SCREEN *aScreen)
Set the SCH_SCREEN associated with this sheet to aScreen.
std::vector< FAB_LAYER_COLOR > dummy
std::unique_ptr< SCHEMATIC > m_schematic
wxString GetTempFileName(const wxString &aPrefix)
BOOST_REQUIRE(intersection.has_value()==c.ExpectedIntersection.has_value())
void boost_test_inject_committed_net_chain(CONNECTION_GRAPH &aGraph, std::unique_ptr< SCH_NETCHAIN > aChain)
BOOST_FIXTURE_TEST_CASE(NetChainSavedOnlyOnRootSheetFile, NETCHAIN_SAVE_ROOT_FIXTURE)
Regression: net_chain emission is schematic-level state owned by the connection graph.
BOOST_CHECK_MESSAGE(totalMismatches==0, std::to_string(totalMismatches)+" board(s) with strategy disagreements")
const SHAPE_LINE_CHAIN chain
BOOST_CHECK_EQUAL(result, "25.4")