KiCad PCB EDA Suite
Loading...
Searching...
No Matches
test_issue24044_expression_net_names.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, you may find one here:
18 * https://www.gnu.org/licenses/gpl-3.0.en.html
19 * or you may search the http://www.gnu.org website for the version 32 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
26
27#include <schematic.h>
28#include <sch_sheet.h>
29#include <sch_sheet_path.h>
30#include <sch_sheet_pin.h>
31#include <sch_screen.h>
32#include <sch_label.h>
34#include <locale_io.h>
35
36
38{
40 std::unique_ptr<SCHEMATIC> m_schematic;
41};
42
43
68BOOST_FIXTURE_TEST_CASE( Issue24044ExpressionNetNames, ISSUE_24044_FIXTURE )
69{
71
72 KI_TEST::LoadSchematic( m_settingsManager, "issue24044/issue24044", m_schematic );
73
74 SCH_SHEET_LIST hierarchy = m_schematic->Hierarchy();
75
76 // Collect the two sub-sheet instance paths (both reference channels.kicad_sch but on
77 // different pages).
78 std::vector<SCH_SHEET_PATH> childPaths;
79
80 for( const SCH_SHEET_PATH& path : hierarchy )
81 {
82 if( path.size() == 2 )
83 childPaths.push_back( path );
84 }
85
86 BOOST_REQUIRE_EQUAL( childPaths.size(), 2U );
87
88 for( const SCH_SHEET_PATH& childPath : childPaths )
89 {
90 SCH_SHEET* childSheet = childPath.Last();
91 BOOST_REQUIRE( childSheet );
92
93 // Build the lookup on the child sheet's hierarchical labels
94 std::set<wxString> labelTexts;
95
96 for( SCH_ITEM* item : childSheet->GetScreen()->Items() )
97 {
98 if( item->Type() == SCH_HIER_LABEL_T )
99 {
100 SCH_HIERLABEL* label = static_cast<SCH_HIERLABEL*>( item );
101 labelTexts.insert( label->GetShownText( &childPath, false ) );
102 }
103 }
104
105 wxString labelList;
106
107 for( const wxString& t : labelTexts )
108 {
109 if( !labelList.IsEmpty() )
110 labelList += wxT( "," );
111
112 labelList += t;
113 }
114
115 // Every sheet pin's resolved name must appear in the child's hierarchical labels
116 for( SCH_SHEET_PIN* pin : childSheet->GetPins() )
117 {
118 wxString pinText = pin->GetShownText( &childPath, false );
119
120 BOOST_CHECK_MESSAGE( !pinText.Contains( wxT( "@{" ) )
121 && !pinText.Contains( wxT( "${" ) ),
122 wxString::Format( "Sheet pin '%s' on path '%s' did not fully "
123 "resolve expression variables",
124 pinText,
125 childPath.PathHumanReadable() ) );
126
127 BOOST_CHECK_MESSAGE( labelTexts.count( pinText ) == 1,
128 wxString::Format( "Sheet pin '%s' on path '%s' has no matching "
129 "hierarchical label inside the child sheet. "
130 "Labels: [%s]",
131 pinText,
132 childPath.PathHumanReadable(),
133 labelList ) );
134 }
135 }
136
137 // Each sheet instance should produce a different resolved name (the expression depends
138 // on the page number, so the two instances must disambiguate).
139 std::set<wxString> allPinTexts;
140
141 for( const SCH_SHEET_PATH& childPath : childPaths )
142 {
143 for( SCH_SHEET_PIN* pin : childPath.Last()->GetPins() )
144 allPinTexts.insert( pin->GetShownText( &childPath, false ) );
145 }
146
147 BOOST_CHECK_EQUAL( allPinTexts.size(), 4U );
148
149 // Verify the computed names match the expected formula for each page number
150 for( const SCH_SHEET_PATH& childPath : childPaths )
151 {
152 long page = 0;
153 BOOST_REQUIRE( childPath.GetPageNumber().ToLong( &page ) );
154
155 std::set<wxString> expected = {
156 wxString::Format( "Ch%ld", ( page - 2 ) * 2 + 0 ),
157 wxString::Format( "Ch%ld", ( page - 2 ) * 2 + 1 )
158 };
159
160 std::set<wxString> actual;
161
162 for( SCH_SHEET_PIN* pin : childPath.Last()->GetPins() )
163 actual.insert( pin->GetShownText( &childPath, false ) );
164
165 wxString actualList;
166
167 for( const wxString& t : actual )
168 {
169 if( !actualList.IsEmpty() )
170 actualList += wxT( "," );
171
172 actualList += t;
173 }
174
176 wxString::Format( "Page %ld expected {Ch%ld, Ch%ld} but got {%s}",
177 page,
178 ( page - 2 ) * 2 + 0,
179 ( page - 2 ) * 2 + 1,
180 actualList ) );
181 }
182}
183
184
198BOOST_FIXTURE_TEST_CASE( Issue24044PathFormEquivalence, ISSUE_24044_FIXTURE )
199{
201
202 KI_TEST::LoadSchematic( m_settingsManager, "issue24044/issue24044", m_schematic );
203
204 std::vector<SCH_SHEET_PATH> childPaths;
205
206 for( const SCH_SHEET_PATH& path : m_schematic->Hierarchy() )
207 {
208 if( path.size() == 2 )
209 childPaths.push_back( path );
210 }
211
212 BOOST_REQUIRE_EQUAL( childPaths.size(), 2U );
213
214 for( const SCH_SHEET_PATH& childPath : childPaths )
215 {
216 SCH_SHEET* childSheet = childPath.Last();
217 BOOST_REQUIRE( childSheet );
218
219 // Build the parent-screen path (drop the trailing child sheet). This is the path
220 // shape that callers such as sch_selection_tool pass in.
221 SCH_SHEET_PATH parentPath = childPath;
222 parentPath.pop_back();
223
224 BOOST_REQUIRE_EQUAL( parentPath.size() + 1U, childPath.size() );
225
226 for( SCH_SHEET_PIN* pin : childSheet->GetPins() )
227 {
228 wxString fromChildPath = pin->GetShownText( &childPath, false );
229 wxString fromParentPath = pin->GetShownText( &parentPath, false );
230
231 BOOST_CHECK_MESSAGE( fromChildPath == fromParentPath,
232 wxString::Format( "Sheet pin resolution diverged between "
233 "path forms: child='%s' parent='%s'",
234 fromChildPath,
235 fromParentPath ) );
236 }
237 }
238}
Instantiate the current locale within a scope in which you are expecting exceptions to be thrown.
Definition locale_io.h:41
Base class for any item which can be embedded within the SCHEMATIC container class,...
Definition sch_item.h:168
wxString GetShownText(const SCH_SHEET_PATH *aPath, bool aAllowExtraText, int aDepth=0) const override
EE_RTREE & Items()
Get the full RTree, usually for iterating.
Definition sch_screen.h:119
A container for handling SCH_SHEET_PATH objects in a flattened hierarchy.
Handle access to a stack of flattened SCH_SHEET objects by way of a path for creating a flattened sch...
size_t size() const
Forwarded method from std::vector.
void pop_back()
Forwarded method from std::vector.
Define a sheet pin (label) used in sheets to create hierarchical schematics.
Sheet symbol placed in a schematic, and is the entry point for a sub schematic.
Definition sch_sheet.h:48
SCH_SCREEN * GetScreen() const
Definition sch_sheet.h:143
std::vector< SCH_SHEET_PIN * > & GetPins()
Definition sch_sheet.h:231
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.
std::vector< FAB_LAYER_COLOR > dummy
std::unique_ptr< SCHEMATIC > m_schematic
BOOST_REQUIRE(intersection.has_value()==c.ExpectedIntersection.has_value())
BOOST_FIXTURE_TEST_CASE(Issue24044ExpressionNetNames, ISSUE_24044_FIXTURE)
Test for issue #24044: Inconsistent internal evaluation of net names when using expressions.
std::string path
KIBIS_PIN * pin
VECTOR3I expected(15, 30, 45)
BOOST_CHECK_MESSAGE(totalMismatches==0, std::to_string(totalMismatches)+" board(s) with strategy disagreements")
int actual
BOOST_CHECK_EQUAL(result, "25.4")
@ SCH_HIER_LABEL_T
Definition typeinfo.h:170