KiCad PCB EDA Suite
Loading...
Searching...
No Matches
test_issue22286_pin_alternate.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 2
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 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
19 * or you may search the http://www.gnu.org website for the version 2 license,
20 * or you may write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22 */
23
32
34#include "eeschema_test_utils.h"
35
36#include <lib_symbol.h>
37#include <pin_type.h>
38#include <sch_pin.h>
39#include <sch_symbol.h>
41
42
44{
45public:
46 SCH_SYMBOL* FindSymbolByRef( const wxString& aRef )
47 {
48 if( !m_schematic )
49 return nullptr;
50
51 SCH_SCREEN* screen = m_schematic->RootScreen();
52
53 if( !screen )
54 return nullptr;
55
56 for( SCH_ITEM* item : screen->Items().OfType( SCH_SYMBOL_T ) )
57 {
58 SCH_SYMBOL* symbol = static_cast<SCH_SYMBOL*>( item );
59
60 if( symbol && symbol->GetRef( &m_schematic->Hierarchy()[0], false ) == aRef )
61 return symbol;
62 }
63
64 return nullptr;
65 }
66
67 SCH_PIN* FindPinByNumber( SCH_SYMBOL* aSymbol, const wxString& aNumber )
68 {
69 if( !aSymbol )
70 return nullptr;
71
72 for( SCH_PIN* pin : aSymbol->GetPins( &m_schematic->Hierarchy()[0] ) )
73 {
74 if( pin->GetNumber() == aNumber )
75 return pin;
76 }
77
78 return nullptr;
79 }
80};
81
82
83BOOST_FIXTURE_TEST_SUITE( Issue22286, TEST_ISSUE22286_FIXTURE )
84
85
86
94BOOST_AUTO_TEST_CASE( PinAlternateTypeAfterSchematicLoad )
95{
96 wxFileName fn;
97 fn.SetPath( KI_TEST::GetEeschemaTestDataDir() );
98 fn.AppendDir( wxS( "issue22286" ) );
99 fn.SetName( wxS( "bugtest" ) );
101
102 LoadSchematic( fn.GetFullPath() );
103
104 // Find symbol J1 (the MOLEX connector)
105 SCH_SYMBOL* j1 = FindSymbolByRef( wxS( "J1" ) );
106 BOOST_REQUIRE_MESSAGE( j1 != nullptr, "Symbol J1 not found in schematic" );
107
108 // Find pin 8
109 SCH_PIN* pin8 = FindPinByNumber( j1, wxS( "8" ) );
110 BOOST_REQUIRE_MESSAGE( pin8 != nullptr, "Pin 8 not found on symbol J1" );
111
112 // Check that the alternate is set
113 BOOST_CHECK_MESSAGE( pin8->GetAlt() == wxS( "8.pow" ),
114 "Pin 8 should have alternate '8.pow' set, but has '"
115 + pin8->GetAlt() + "'" );
116
117 // Check that m_libPin is set
118 BOOST_CHECK_MESSAGE( pin8->GetLibPin() != nullptr,
119 "Pin 8 should have m_libPin set after schematic load" );
120
121 if( pin8->GetLibPin() )
122 {
123 // Check that the library pin has alternates
124 const auto& alternates = pin8->GetLibPin()->GetAlternates();
125 BOOST_CHECK_MESSAGE( !alternates.empty(),
126 "Library pin should have alternates populated" );
127 BOOST_CHECK_MESSAGE( alternates.count( wxS( "8.pow" ) ) > 0,
128 "Library pin should have '8.pow' in alternates map" );
129
130 // Check the alternate's type in the library pin
131 if( alternates.count( wxS( "8.pow" ) ) > 0 )
132 {
133 const SCH_PIN::ALT& alt = alternates.at( wxS( "8.pow" ) );
134 BOOST_CHECK_MESSAGE( alt.m_Type == ELECTRICAL_PINTYPE::PT_POWER_IN,
135 "Library pin alternate '8.pow' should have type PT_POWER_IN" );
136 }
137 }
138
139 // The critical test: GetType() should return the alternate's type, not the default
140 ELECTRICAL_PINTYPE pinType = pin8->GetType();
141 BOOST_CHECK_MESSAGE( pinType == ELECTRICAL_PINTYPE::PT_POWER_IN,
142 "Pin 8 GetType() should return PT_POWER_IN (7) for alternate '8.pow', "
143 "but returned " + std::to_string( static_cast<int>( pinType ) )
144 + " (" + ElectricalPinTypeGetText( pinType ) + ")" );
145
146 // Verify the shown name is the alternate name
147 BOOST_CHECK_MESSAGE( pin8->GetShownName() == wxS( "8.pow" ),
148 "Pin 8 GetShownName() should return '8.pow', but returned '"
149 + pin8->GetShownName() + "'" );
150}
151
152
161BOOST_AUTO_TEST_CASE( ConnectionGraphHandlesPinAlternateType )
162{
163 wxFileName fn;
164 fn.SetPath( KI_TEST::GetEeschemaTestDataDir() );
165 fn.AppendDir( wxS( "issue22286" ) );
166 fn.SetName( wxS( "bugtest" ) );
168
169 LoadSchematic( fn.GetFullPath() );
170
171 SCH_SYMBOL* j1 = FindSymbolByRef( wxS( "J1" ) );
172 BOOST_REQUIRE( j1 != nullptr );
173
174 SCH_PIN* pin8 = FindPinByNumber( j1, wxS( "8" ) );
175 BOOST_REQUIRE( pin8 != nullptr );
176
177 // Get the net name for pin 8
178 SCH_SHEET_PATH path = m_schematic->Hierarchy()[0];
179 wxString netName = pin8->GetDefaultNetName( path );
180
181 // With the fix, pin 8 should NOT get an "unconnected-(...)" name because
182 // it's a power pin (PT_POWER_IN), not a no-connect pin (PT_NC).
183 // It will still get a "Net-(...)" name because it's on a regular connector.
184 BOOST_CHECK_MESSAGE( !netName.StartsWith( wxS( "unconnected-(" ) ),
185 "Pin 8 (power_in) should not get an 'unconnected-(...)' name. "
186 "Got: '" + netName + "'" );
187}
188
189
EE_TYPE OfType(KICAD_T aType) const
Definition sch_rtree.h:241
A generic fixture for loading schematics and associated settings for qa tests.
std::unique_ptr< SCHEMATIC > m_schematic
Base class for any item which can be embedded within the SCHEMATIC container class,...
Definition sch_item.h:167
const std::map< wxString, ALT > & GetAlternates() const
Definition sch_pin.h:160
ALT GetAlt(const wxString &aAlt)
Definition sch_pin.h:174
SCH_PIN * GetLibPin() const
Definition sch_pin.h:89
const wxString & GetShownName() const
Definition sch_pin.cpp:567
wxString GetDefaultNetName(const SCH_SHEET_PATH &aPath, bool aForceNoConnect=false)
Definition sch_pin.cpp:1417
ELECTRICAL_PINTYPE GetType() const
Definition sch_pin.cpp:313
EE_RTREE & Items()
Get the full RTree, usually for iterating.
Definition sch_screen.h:118
Handle access to a stack of flattened SCH_SHEET objects by way of a path for creating a flattened sch...
Schematic symbol object.
Definition sch_symbol.h:76
std::vector< const SCH_PIN * > GetPins(const SCH_SHEET_PATH *aSheet) const
Retrieve a list of the SCH_PINs for the given sheet path.
const wxString GetRef(const SCH_SHEET_PATH *aSheet, bool aIncludeUnit=false) const override
SCH_SYMBOL * FindSymbolByRef(const wxString &aRef)
SCH_PIN * FindPinByNumber(SCH_SYMBOL *aSymbol, const wxString &aNumber)
static const std::string KiCadSchematicFileExtension
std::string GetEeschemaTestDataDir()
Get the configured location of Eeschema test data.
ELECTRICAL_PINTYPE
The symbol library pin object electrical types used in ERC tests.
Definition pin_type.h:36
@ PT_POWER_IN
power input (GND, VCC for ICs). Must be connected to a power output.
Definition pin_type.h:46
wxString ElectricalPinTypeGetText(ELECTRICAL_PINTYPE)
Definition pin_type.cpp:207
static void LoadSchematic(SCHEMATIC *aSchematic, SCH_SHEET *aRootSheet, const wxString &aFileName)
ELECTRICAL_PINTYPE m_Type
Definition sch_pin.h:47
BOOST_AUTO_TEST_CASE(HorizontalAlignment)
BOOST_REQUIRE(intersection.has_value()==c.ExpectedIntersection.has_value())
BOOST_AUTO_TEST_SUITE_END()
BOOST_AUTO_TEST_CASE(PinAlternateTypeAfterSchematicLoad)
Test that pin alternates are correctly applied after loading a schematic from disk.
std::string path
KIBIS_PIN * pin
@ SCH_SYMBOL_T
Definition typeinfo.h:176
Definition of file extensions used in Kicad.