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, see <https://www.gnu.org/licenses/>.
18 */
19
20#include <boost/test/unit_test.hpp>
21
22#include <connection_graph.h>
24#include <sch_netchain.h>
25#include <sch_screen.h>
26#include <sch_sheet.h>
27#include <schematic.h>
29#include <locale_io.h>
31
32#include <wx/ffile.h>
33#include <wx/filename.h>
34#include <wx/stdpaths.h>
35
36
37// Test backdoor to push a fully-formed committed net chain into the graph without
38// needing a real RebuildNetChains() pass. Friend-declared in connection_graph.h.
40 std::unique_ptr<SCH_NETCHAIN> aChain )
41{
42 aGraph.m_committedNetChains.push_back( std::move( aChain ) );
43}
44
45
47{
50 {
51 wxString tempDir = wxStandardPaths::Get().GetTempDir();
52 wxString projectPath =
53 tempDir + wxFileName::GetPathSeparator() + wxT( "test_netchain_save.kicad_pro" );
54 m_tempFiles.push_back( projectPath );
55
56 m_settingsManager.LoadProject( projectPath.ToStdString() );
57 m_schematic = std::make_unique<SCHEMATIC>( nullptr );
59 m_schematic->SetProject( m_project );
60 }
61
63 {
64 for( const wxString& file : m_tempFiles )
65 {
66 if( wxFileExists( file ) )
67 wxRemoveFile( file );
68 }
69
70 m_schematic.reset();
71 }
72
73 wxString GetTempFileName( const wxString& aPrefix )
74 {
75 wxString tempDir = wxStandardPaths::Get().GetTempDir();
76 wxString fileName = wxFileName::CreateTempFileName(
77 tempDir + wxFileName::GetPathSeparator() + aPrefix );
78 m_tempFiles.push_back( fileName );
79 return fileName;
80 }
81
83 std::unique_ptr<SCHEMATIC> m_schematic;
85 std::vector<wxString> m_tempFiles;
86};
87
88
96BOOST_FIXTURE_TEST_CASE( NetChainSavedOnlyOnRootSheetFile, NETCHAIN_SAVE_ROOT_FIXTURE )
97{
99
100 m_schematic->CreateDefaultScreens();
101
102 std::vector<SCH_SHEET*> topSheets = m_schematic->GetTopLevelSheets();
103 BOOST_REQUIRE( !topSheets.empty() );
104
105 SCH_SHEET* topSheet = topSheets[0];
106 SCH_SCREEN* topScreen = topSheet->GetScreen();
107 BOOST_REQUIRE( topScreen );
108
109 SCH_SHEET* subSheet = new SCH_SHEET( m_schematic.get() );
110 SCH_SCREEN* subScreen = new SCH_SCREEN( m_schematic.get() );
111 subSheet->SetName( "SubSheet" );
112 subSheet->SetScreen( subScreen );
113 subSheet->SetFileName( "subsheet.kicad_sch" );
114 subScreen->SetFileName( "subsheet.kicad_sch" );
115 topScreen->Append( subSheet );
116
117 m_schematic->RefreshHierarchy();
118
119 auto chain = std::make_unique<SCH_NETCHAIN>();
120 chain->SetName( wxT( "TEST_CHAIN" ) );
121 chain->AddNet( wxT( "/NET_A" ) );
122 chain->AddNet( wxT( "/NET_B" ) );
123 chain->SetTerminalRefs( wxT( "U1" ), wxT( "1" ), wxT( "U2" ), wxT( "2" ) );
124
125 boost_test_inject_committed_net_chain( *m_schematic->ConnectionGraph(), std::move( chain ) );
126
127 BOOST_REQUIRE_EQUAL( m_schematic->ConnectionGraph()->GetCommittedNetChains().size(), 1u );
128
129 wxString topFileName = GetTempFileName( "netchain_main" );
130 topFileName += ".kicad_sch";
131 m_tempFiles.push_back( topFileName );
132
133 wxString subFileName = GetTempFileName( "netchain_sub" );
134 subFileName += ".kicad_sch";
135 m_tempFiles.push_back( subFileName );
136
138 BOOST_CHECK_NO_THROW(
139 io.SaveSchematicFile( topFileName, topSheet, m_schematic.get() ) );
140 BOOST_CHECK_NO_THROW(
141 io.SaveSchematicFile( subFileName, subSheet, m_schematic.get() ) );
142
143 BOOST_REQUIRE( wxFileExists( topFileName ) );
144 BOOST_REQUIRE( wxFileExists( subFileName ) );
145
146 auto countOccurrences = []( const wxString& aHaystack, const wxString& aNeedle )
147 {
148 size_t count = 0;
149 size_t pos = 0;
150
151 while( ( pos = aHaystack.find( aNeedle, pos ) ) != wxString::npos )
152 {
153 ++count;
154 pos += aNeedle.length();
155 }
156
157 return count;
158 };
159
160 wxString topContents;
161 {
162 wxFFile readback( topFileName, "rb" );
163 BOOST_REQUIRE( readback.IsOpened() && readback.ReadAll( &topContents ) );
164 }
165
166 wxString subContents;
167 {
168 wxFFile readback( subFileName, "rb" );
169 BOOST_REQUIRE( readback.IsOpened() && readback.ReadAll( &subContents ) );
170 }
171
172 BOOST_CHECK_EQUAL( countOccurrences( topContents, wxT( "(net_chain" ) ), 1u );
173 BOOST_CHECK_MESSAGE( topContents.Contains( wxT( "TEST_CHAIN" ) ),
174 "Top-level sheet file is missing the chain name" );
175
176 BOOST_CHECK_EQUAL( countOccurrences( subContents, wxT( "(net_chain" ) ), 0u );
177 BOOST_CHECK_MESSAGE( !subContents.Contains( wxT( "TEST_CHAIN" ) ),
178 "Sub-sheet file unexpectedly contains the chain name" );
179}
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:37
Container for project specific data.
Definition project.h:62
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:44
void SetFileName(const wxString &aFilename)
Definition sch_sheet.h:376
void SetName(const wxString &aName)
Definition sch_sheet.h:137
SCH_SCREEN * GetScreen() const
Definition sch_sheet.h:139
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")