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, you may find one at
18 * http://www.gnu.org/licenses/
19 */
20
23
24#include <connection_graph.h>
26#include <sch_netchain.h>
27#include <sch_sheet.h>
28#include <schematic.h>
30#include <locale_io.h>
31
32#include <wx/filename.h>
33#include <wx/xml/xml.h>
34
35
43
44
45static wxXmlNode* find_child( wxXmlNode* parent, const wxString& name )
46{
47 for( wxXmlNode* child = parent->GetChildren(); child; child = child->GetNext() )
48 {
49 if( child->GetName() == name )
50 return child;
51 }
52
53 return nullptr;
54}
55
56
57BOOST_FIXTURE_TEST_CASE( NetlistExporterXML_NetChainTerminalPinsRoundTrip,
59{
61 KI_TEST::LoadSchematic( m_settingsManager, wxT( "net_chains_four_nets" ), m_schematic );
62
63 SCH_SHEET_LIST sheets = m_schematic->BuildSheetListSortedByPageNumbers();
64 CONNECTION_GRAPH* graph = m_schematic->ConnectionGraph();
65 graph->Recalculate( sheets, /*aUnconditional=*/true );
66
67 SCH_NETCHAIN* candidate = nullptr;
68
69 for( const auto& sig : graph->GetPotentialNetChains() )
70 {
71 if( sig && sig->GetNets().size() == 4 )
72 {
73 candidate = sig.get();
74 break;
75 }
76 }
77
78 BOOST_REQUIRE_MESSAGE( candidate, "fixture must produce a 4-net potential chain" );
79 BOOST_REQUIRE_MESSAGE( !candidate->GetTerminalRef( 0 ).IsEmpty()
80 && !candidate->GetTerminalRef( 1 ).IsEmpty(),
81 "potential chain must have terminal refs populated" );
82
83 SCH_NETCHAIN* committed = graph->CreateNetChainFromPotential( candidate, wxT( "SIG_TEST" ) );
84 BOOST_REQUIRE( committed );
85
86 const wxString expectRefA = committed->GetTerminalRef( 0 );
87 const wxString expectPinA = committed->GetTerminalPinNum( 0 );
88 const wxString expectRefB = committed->GetTerminalRef( 1 );
89 const wxString expectPinB = committed->GetTerminalPinNum( 1 );
90
91 wxFileName netFile = m_schematic->Project().GetProjectFullName();
92 netFile.SetName( netFile.GetName() + wxT( "_terminal_pins_test" ) );
93 netFile.SetExt( wxT( "xml" ) );
94
95 if( wxFileExists( netFile.GetFullPath() ) )
96 wxRemoveFile( netFile.GetFullPath() );
97
98 WX_STRING_REPORTER reporter;
99 std::unique_ptr<NETLIST_EXPORTER_XML> exporter =
100 std::make_unique<NETLIST_EXPORTER_XML>( m_schematic.get() );
101
102 // Net chains are gated behind the KiCad-internal flag, so the public XML format
103 // does not leak them to schema-validating consumers.
104 BOOST_REQUIRE( exporter->WriteNetlist( netFile.GetFullPath(), GNL_OPT_KICAD, reporter )
105 && reporter.GetMessages().IsEmpty() );
106
107 wxXmlDocument xdoc;
108 BOOST_REQUIRE( xdoc.Load( netFile.GetFullPath() ) );
109
110 wxXmlNode* root = xdoc.GetRoot();
111 BOOST_REQUIRE( root );
112
113 wxXmlNode* netChains = find_child( root, wxT( "net_chains" ) );
114 BOOST_REQUIRE( netChains );
115
116 wxXmlNode* targetChain = nullptr;
117
118 for( wxXmlNode* xchain = netChains->GetChildren(); xchain; xchain = xchain->GetNext() )
119 {
120 if( xchain->GetName() != wxT( "net_chain" ) )
121 continue;
122
123 if( xchain->GetAttribute( wxT( "name" ), wxEmptyString ) == wxT( "SIG_TEST" ) )
124 {
125 targetChain = xchain;
126 break;
127 }
128 }
129
130 BOOST_REQUIRE_MESSAGE( targetChain, "expected committed chain SIG_TEST in netlist" );
131
132 wxXmlNode* xterminals = find_child( targetChain, wxT( "terminal_pins" ) );
133 BOOST_REQUIRE_MESSAGE( xterminals, "expected terminal_pins block under net_chain" );
134
135 std::vector<std::pair<wxString, wxString>> emittedTerms;
136
137 for( wxXmlNode* xt = xterminals->GetChildren(); xt; xt = xt->GetNext() )
138 {
139 if( xt->GetName() != wxT( "terminal_pin" ) )
140 continue;
141
142 emittedTerms.emplace_back( xt->GetAttribute( wxT( "ref" ), wxEmptyString ),
143 xt->GetAttribute( wxT( "pin" ), wxEmptyString ) );
144 }
145
146 BOOST_CHECK_EQUAL( emittedTerms.size(), 2u );
147
148 auto matches = []( const std::vector<std::pair<wxString, wxString>>& aTerms,
149 const wxString& aRef, const wxString& aPin )
150 {
151 for( const auto& t : aTerms )
152 {
153 if( t.first == aRef && t.second == aPin )
154 return true;
155 }
156
157 return false;
158 };
159
160 BOOST_CHECK( matches( emittedTerms, expectRefA, expectPinA ) );
161 BOOST_CHECK( matches( emittedTerms, expectRefB, expectPinB ) );
162
163 wxRemoveFile( netFile.GetFullPath() );
164}
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:41
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:193
const wxString & GetMessages() const
Definition reporter.cpp:105
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())
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")