KiCad PCB EDA Suite
Loading...
Searching...
No Matches
test_netlist_exporter_xml_unit_fields.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
22
24#include <sch_field.h>
25#include <sch_screen.h>
26#include <sch_sheet_path.h>
27#include <sch_symbol.h>
28#include <schematic.h>
30
31#include <wx/filename.h>
32#include <wx/xml/xml.h>
33
34
35namespace
36{
37
38struct UNIT_FIELDS_FIXTURE
39{
40 UNIT_FIELDS_FIXTURE()
41 {
42 // U4 is split over three sheets, with unit 3 alone on the oscillator sheet
43 KI_TEST::LoadSchematic( m_settingsManager, wxT( "issue19646/issue19646" ), m_schematic );
44 }
45
46 void AddField( int aUnit, const wxString& aName, const wxString& aText )
47 {
48 SCH_SYMBOL* unit = nullptr;
49
50 for( const SCH_SHEET_PATH& sheet : m_schematic->Hierarchy() )
51 {
52 for( SCH_ITEM* item : sheet.LastScreen()->Items().OfType( SCH_SYMBOL_T ) )
53 {
54 SCH_SYMBOL* symbol = static_cast<SCH_SYMBOL*>( item );
55
56 if( symbol->GetRef( &sheet ) == wxS( "U4" ) && symbol->GetUnitSelection( &sheet ) == aUnit )
57 unit = symbol;
58 }
59 }
60
61 BOOST_REQUIRE( unit );
62
63 SCH_FIELD field( unit, FIELD_T::USER, aName );
64 field.SetText( aText );
65 unit->AddField( field );
66 }
67
68 wxString ExportedField( const wxString& aName )
69 {
70 wxFileName netFile = m_schematic->Project().GetProjectFullName();
71 netFile.SetName( netFile.GetName() + wxT( "_unit_fields" ) );
72 netFile.SetExt( wxT( "xml" ) );
73
74 WX_STRING_REPORTER reporter;
75 NETLIST_EXPORTER_XML exporter( m_schematic.get(), nullptr );
76
77 BOOST_REQUIRE( exporter.WriteNetlist( netFile.GetFullPath(), 0, reporter ) );
78
79 wxXmlDocument doc;
80 BOOST_REQUIRE( doc.Load( netFile.GetFullPath() ) );
81 wxRemoveFile( netFile.GetFullPath() );
82
83 wxXmlNode* comp = doc.GetRoot()->GetChildren();
84
85 while( comp && comp->GetName() != wxT( "components" ) )
86 comp = comp->GetNext();
87
89
90 for( comp = comp->GetChildren(); comp; comp = comp->GetNext() )
91 {
92 if( comp->GetAttribute( wxT( "ref" ) ) != wxT( "U4" ) )
93 continue;
94
95 for( wxXmlNode* fields = comp->GetChildren(); fields; fields = fields->GetNext() )
96 {
97 if( fields->GetName() != wxT( "fields" ) )
98 continue;
99
100 for( wxXmlNode* field = fields->GetChildren(); field; field = field->GetNext() )
101 {
102 if( field->GetAttribute( wxT( "name" ) ) == aName )
103 return field->GetNodeContent();
104 }
105 }
106 }
107
108 BOOST_FAIL( "U4 exports no " + aName.ToStdString() );
109 return wxEmptyString;
110 }
111
112 SETTINGS_MANAGER m_settingsManager;
113 std::unique_ptr<SCHEMATIC> m_schematic;
114};
115
116} // namespace
117
118
119BOOST_FIXTURE_TEST_CASE( NetlistExporterXML_BlankUnitFieldDoesNotHideText, UNIT_FIELDS_FIXTURE )
120{
121 AddField( 1, wxS( "Maker" ), wxEmptyString );
122 AddField( 3, wxS( "Maker" ), wxS( "Acme" ) );
123
124 BOOST_CHECK_EQUAL( ExportedField( wxS( "Maker" ) ), wxS( "Acme" ) );
125}
126
127
128BOOST_FIXTURE_TEST_CASE( NetlistExporterXML_UnitFieldUsesItsOwnSheet, UNIT_FIELDS_FIXTURE )
129{
130 AddField( 3, wxS( "Page" ), wxS( "${SHEETNAME}" ) );
131
132 BOOST_CHECK_EQUAL( ExportedField( wxS( "Page" ) ), wxS( "F28P65x OSC, RESET AND DEBUG" ) );
133}
void SetName(const wxString &aName)
SCH_FIELD * AddField(const SCH_FIELD &aField)
Add a field to the symbol.
int GetUnitSelection(const SCH_SHEET_PATH *aSheet) const
Return the instance-specific unit selection for the given sheet path.
const wxString GetRef(const SCH_SHEET_PATH *aSheet, bool aIncludeUnit=false) const override
@ SCH_SYMBOL
one item stream of a Schematic Symbol Library
void LoadSchematic(SETTINGS_MANAGER &aSettingsManager, const wxString &aRelPath, std::unique_ptr< SCHEMATIC > &aSchematic)
Definition of the SCH_SHEET_PATH and SCH_SHEET_LIST classes for Eeschema.
@ USER
The field ID hasn't been set yet; field is invalid.
BOOST_REQUIRE(intersection.has_value()==c.ExpectedIntersection.has_value())
IbisParser parser & reporter
KIBIS_COMPONENT * comp
BOOST_FIXTURE_TEST_CASE(NetlistExporterXML_BlankUnitFieldDoesNotHideText, UNIT_FIELDS_FIXTURE)
BOOST_CHECK_EQUAL(result, "25.4")
@ SCH_SYMBOL_T
Definition typeinfo.h:168