KiCad PCB EDA Suite
Loading...
Searching...
No Matches
test_netlist_exporter_xml_terminal_pins.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
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
22
23#include <connection_graph.h>
25#include <sch_netchain.h>
26#include <sch_sheet.h>
27#include <schematic.h>
29#include <locale_io.h>
30
31#include <wx/filename.h>
32#include <wx/xml/xml.h>
33
34
42
43
44static wxXmlNode* find_child( wxXmlNode* parent, const wxString& name )
45{
46 for( wxXmlNode* child = parent->GetChildren(); child; child = child->GetNext() )
47 {
48 if( child->GetName() == name )
49 return child;
50 }
51
52 return nullptr;
53}
54
55
56BOOST_FIXTURE_TEST_CASE( NetlistExporterXML_NetChainTerminalPinsRoundTrip,
58{
60 KI_TEST::LoadSchematic( m_settingsManager, wxT( "net_chains_four_nets" ), m_schematic );
61
62 SCH_SHEET_LIST sheets = m_schematic->BuildSheetListSortedByPageNumbers();
63 CONNECTION_GRAPH* graph = m_schematic->ConnectionGraph();
64 graph->Recalculate( sheets, /*aUnconditional=*/true );
65
66 SCH_NETCHAIN* candidate = nullptr;
67
68 for( const auto& sig : graph->GetPotentialNetChains() )
69 {
70 if( sig && sig->GetNets().size() == 4 )
71 {
72 candidate = sig.get();
73 break;
74 }
75 }
76
77 BOOST_REQUIRE_MESSAGE( candidate, "fixture must produce a 4-net potential chain" );
78 BOOST_REQUIRE_MESSAGE( !candidate->GetTerminalRef( 0 ).IsEmpty()
79 && !candidate->GetTerminalRef( 1 ).IsEmpty(),
80 "potential chain must have terminal refs populated" );
81
82 SCH_NETCHAIN* committed = graph->CreateNetChainFromPotential( candidate, wxT( "SIG_TEST" ) );
83 BOOST_REQUIRE( committed );
84
85 const wxString expectRefA = committed->GetTerminalRef( 0 );
86 const wxString expectPinA = committed->GetTerminalPinNum( 0 );
87 const wxString expectRefB = committed->GetTerminalRef( 1 );
88 const wxString expectPinB = committed->GetTerminalPinNum( 1 );
89
90 wxFileName netFile = m_schematic->Project().GetProjectFullName();
91 netFile.SetName( netFile.GetName() + wxT( "_terminal_pins_test" ) );
92 netFile.SetExt( wxT( "xml" ) );
93
94 if( wxFileExists( netFile.GetFullPath() ) )
95 wxRemoveFile( netFile.GetFullPath() );
96
98 std::unique_ptr<NETLIST_EXPORTER_XML> exporter =
99 std::make_unique<NETLIST_EXPORTER_XML>( m_schematic.get() );
100
101 // Net chains are gated behind the KiCad-internal flag, so the public XML format
102 // does not leak them to schema-validating consumers.
103 BOOST_REQUIRE( exporter->WriteNetlist( netFile.GetFullPath(), GNL_OPT_KICAD, reporter )
104 && reporter.GetMessages().IsEmpty() );
105
106 wxXmlDocument xdoc;
107 BOOST_REQUIRE( xdoc.Load( netFile.GetFullPath() ) );
108
109 wxXmlNode* root = xdoc.GetRoot();
110 BOOST_REQUIRE( root );
111
112 wxXmlNode* netChains = find_child( root, wxT( "net_chains" ) );
113 BOOST_REQUIRE( netChains );
114
115 wxXmlNode* targetChain = nullptr;
116
117 for( wxXmlNode* xchain = netChains->GetChildren(); xchain; xchain = xchain->GetNext() )
118 {
119 if( xchain->GetName() != wxT( "net_chain" ) )
120 continue;
121
122 if( xchain->GetAttribute( wxT( "name" ), wxEmptyString ) == wxT( "SIG_TEST" ) )
123 {
124 targetChain = xchain;
125 break;
126 }
127 }
128
129 BOOST_REQUIRE_MESSAGE( targetChain, "expected committed chain SIG_TEST in netlist" );
130
131 wxXmlNode* xterminals = find_child( targetChain, wxT( "terminal_pins" ) );
132 BOOST_REQUIRE_MESSAGE( xterminals, "expected terminal_pins block under net_chain" );
133
134 std::vector<std::pair<wxString, wxString>> emittedTerms;
135
136 for( wxXmlNode* xt = xterminals->GetChildren(); xt; xt = xt->GetNext() )
137 {
138 if( xt->GetName() != wxT( "terminal_pin" ) )
139 continue;
140
141 emittedTerms.emplace_back( xt->GetAttribute( wxT( "ref" ), wxEmptyString ),
142 xt->GetAttribute( wxT( "pin" ), wxEmptyString ) );
143 }
144
145 BOOST_CHECK_EQUAL( emittedTerms.size(), 2u );
146
147 auto matches = []( const std::vector<std::pair<wxString, wxString>>& aTerms,
148 const wxString& aRef, const wxString& aPin )
149 {
150 for( const auto& t : aTerms )
151 {
152 if( t.first == aRef && t.second == aPin )
153 return true;
154 }
155
156 return false;
157 };
158
159 BOOST_CHECK( matches( emittedTerms, expectRefA, expectPinA ) );
160 BOOST_CHECK( matches( emittedTerms, expectRefB, expectPinB ) );
161
162 wxRemoveFile( netFile.GetFullPath() );
163}
const char * name
Calculate the connectivity of a schematic and generates netlists.
SCH_NETCHAIN * CreateNetChainFromPotential(SCH_NETCHAIN *aPotential, const wxString &aName)
Promote a potential net chain to an actual user net chain with the provided name.
void Recalculate(const SCH_SHEET_LIST &aSheetList, bool aUnconditional=false, std::function< void(SCH_ITEM *)> *aChangedItemHandler=nullptr, PROGRESS_REPORTER *aProgressReporter=nullptr)
Update the connection graph for the given list of sheets.
const std::vector< std::unique_ptr< SCH_NETCHAIN > > & GetPotentialNetChains() const
Potential net chains are inferred groupings produced by RebuildNetChains() but not yet user-committed...
Instantiate the current locale within a scope in which you are expecting exceptions to be thrown.
Definition locale_io.h:37
A net chain is a collection of nets that are connected together through passive components.
const wxString & GetTerminalRef(int aIdx) const
const wxString & GetTerminalPinNum(int aIdx) const
A container for handling SCH_SHEET_PATH objects in a flattened hierarchy.
A wrapper for reporting to a wxString object.
Definition reporter.h:189
void LoadSchematic(SETTINGS_MANAGER &aSettingsManager, const wxString &aRelPath, std::unique_ptr< SCHEMATIC > &aSchematic)
@ GNL_OPT_KICAD
std::vector< FAB_LAYER_COLOR > dummy
BOOST_REQUIRE(intersection.has_value()==c.ExpectedIntersection.has_value())
IbisParser parser & reporter
BOOST_FIXTURE_TEST_CASE(NetlistExporterXML_NetChainTerminalPinsRoundTrip, XML_NETCHAIN_TERMINALS_FIXTURE)
static wxXmlNode * find_child(wxXmlNode *parent, const wxString &name)
BOOST_CHECK_EQUAL(result, "25.4")