KiCad PCB EDA Suite
Loading...
Searching...
No Matches
test_sch_sheet_path.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, see <https://www.gnu.org/licenses/>.
18 */
19
24
27#include "eeschema_test_utils.h"
28
29// Code under test
30#include <sch_sheet_path.h>
31
33#include <eeschema_helpers.h>
34#include <sch_screen.h>
35#include <sch_sheet.h>
36#include <schematic.h>
37
38#include <sstream>
39
41{
42public:
44 {
45 for( unsigned i = 0; i < 4; ++i )
46 {
47 m_sheets.emplace_back( nullptr, VECTOR2I( i, i ) );
48
49 std::ostringstream ss;
50 ss << "Sheet" << i;
51 m_sheets[i].GetField( FIELD_T::SHEET_NAME )->SetText( ss.str() );
52 m_sheets[i].SetParent( &m_schematic );
53 }
54
55 // 0->1->2
56 m_linear.push_back( &m_sheets[0] );
57 m_linear.push_back( &m_sheets[1] );
58 m_linear.push_back( &m_sheets[2] );
59 }
60
63
69
71 std::vector<SCH_SHEET> m_sheets;
72};
73
74
78BOOST_FIXTURE_TEST_SUITE( SchSheetPath, TEST_SCH_SHEET_PATH_FIXTURE )
79
80
81
85{
86 BOOST_CHECK_EQUAL( m_empty_path.size(), 0 );
87
88 BOOST_CHECK_THROW( m_empty_path.at( 0 ), std::out_of_range );
89
90 // Sheet paths with no SCH_SCHEET object are illegal.
91 // CHECK_WX_ASSERT( m_empty_path.GetPageNumber() );
92
93 // These accessors return nullptr when empty (i.e. they don't crash)
94 BOOST_CHECK_EQUAL( m_empty_path.Last(), nullptr );
95 BOOST_CHECK_EQUAL( m_empty_path.LastScreen(), nullptr );
96
97 BOOST_CHECK_EQUAL( m_empty_path.PathAsString(), "/" );
98 BOOST_CHECK_EQUAL( m_empty_path.PathHumanReadable(), "/" );
99}
100
101
106{
107 BOOST_CHECK_EQUAL( m_linear.size(), 3 );
108
109 BOOST_CHECK_EQUAL( m_linear.at( 0 ), &m_sheets[0] );
110 BOOST_CHECK_EQUAL( m_linear.at( 1 ), &m_sheets[1] );
111 BOOST_CHECK_EQUAL( m_linear.at( 2 ), &m_sheets[2] );
112
113 BOOST_CHECK_EQUAL( m_linear.Last(), &m_sheets[2] );
114 BOOST_CHECK_EQUAL( m_linear.LastScreen(), nullptr );
115
116 // don't know what the uuids will be, but we know the format: /<8-4-4-4-12>/<8-4-4-4-12>/
118 KI_TEST::IsUUIDPathWithLevels, ( m_linear.PathAsString().ToStdString() )( 2 ) );
119
120 // Sheet0 is the root sheet and isn't in the path
121 BOOST_CHECK_EQUAL( m_linear.PathHumanReadable(), "/Sheet1/Sheet2/" );
122}
123
124
126{
127 SCH_SHEET_PATH otherEmpty;
128
129 BOOST_CHECK( m_empty_path == otherEmpty );
130
131 BOOST_CHECK( m_empty_path != m_linear );
132}
133
134
135BOOST_AUTO_TEST_CASE( SheetListGetOrdinalPath )
136{
137 // The "complex_hierarchy" test project has a root sheet with two sheets that reference the
138 // same file.
139 std::unique_ptr<SCHEMATIC> schematic;
140 wxFileName fn( wxString::Format( wxS( "%snetlists/complex_hierarchy" ),
142 wxS( "complex_hierarchy" ), FILEEXT::ProjectFileExtension );
143
144 schematic.reset( EESCHEMA_HELPERS::LoadSchematic( fn.GetFullPath(), false, false, nullptr ) );
145
146 SCH_SHEET_LIST hierarchy = schematic->Hierarchy();
147 BOOST_CHECK_EQUAL( hierarchy.size(), 3 );
148
149 // A null pointer should always result in an empty return value.
150 BOOST_CHECK( !hierarchy.GetOrdinalPath( nullptr ) );
151
152 // The root sheet is a single instance. It's always ordinal.
153 BOOST_CHECK( hierarchy.GetOrdinalPath( schematic->RootScreen() ).value() == hierarchy.at( 0 ) );
154
155 // The shared schematic with the lowest page number is the ordinal sheet path.
156 SCH_SHEET* sheet = hierarchy.at( 1 ).Last();
157 BOOST_CHECK( hierarchy.GetOrdinalPath( sheet->GetScreen() ).value() == hierarchy.at( 1 ) );
158
159 // The shared sheet with a higher page number is not the ordinal sheet path.
160 sheet = hierarchy.at( 2 ).Last();
161 BOOST_CHECK( hierarchy.GetOrdinalPath( sheet->GetScreen() ).value() == hierarchy.at( 1 ) );
162}
163
164
168BOOST_AUTO_TEST_CASE( SheetPathPageProperties )
169{
170 // BOOST_CHECK_EQUAL( m_linear.GetPageNumber(), wxEmptyString );
171
172 // Add new instance to sheet object.
173 // BOOST_CHECK( m_linear.Last()->AddInstance( m_linear.Path() ) );
174 // m_linear.SetPageNumber( "1" );
175 // BOOST_CHECK_EQUAL( m_linear.GetPageNumber(), "1" );
176 // m_linear.SetPageNumber( "i" );
177 // BOOST_CHECK_EQUAL( m_linear.GetPageNumber(), "i" );
178}
179
180
186BOOST_AUTO_TEST_CASE( PathHumanReadableWithSlashes )
187{
188 SCH_SHEET_PATH pathWithSlash;
189 SCHEMATIC schematicLocal( nullptr );
190 std::vector<SCH_SHEET> sheets;
191
192 for( unsigned i = 0; i < 3; ++i )
193 {
194 sheets.emplace_back( nullptr, VECTOR2I( i, i ) );
195 sheets[i].SetParent( &schematicLocal );
196 }
197
198 sheets[0].GetField( FIELD_T::SHEET_NAME )->SetText( "Root" );
199 sheets[1].GetField( FIELD_T::SHEET_NAME )->SetText( "Power/Supply" );
200 sheets[2].GetField( FIELD_T::SHEET_NAME )->SetText( "SubSheet" );
201
202 pathWithSlash.push_back( &sheets[0] );
203 pathWithSlash.push_back( &sheets[1] );
204 pathWithSlash.push_back( &sheets[2] );
205
206 // Without escaping, the path contains the literal '/' in the sheet name
207 wxString unescaped = pathWithSlash.PathHumanReadable( true, false, false );
208 BOOST_CHECK_EQUAL( unescaped, "/Power/Supply/SubSheet/" );
209
210 // With escaping, the '/' in the sheet name becomes "{slash}"
211 wxString escaped = pathWithSlash.PathHumanReadable( true, false, true );
212 BOOST_CHECK_EQUAL( escaped, "/Power{slash}Supply/SubSheet/" );
213
214 // The escaped version should be unambiguous since '/' only means path separator
215 // and "{slash}" means a literal slash character in the sheet name.
216
217 // Test with stripping trailing separator
218 wxString escapedNoTrail = pathWithSlash.PathHumanReadable( true, true, true );
219 BOOST_CHECK_EQUAL( escapedNoTrail, "/Power{slash}Supply/SubSheet" );
220}
221
222
static SCHEMATIC * LoadSchematic(const wxString &aFileName, bool aSetActive, bool aForceDefaultProject, PROJECT *aProject=nullptr, bool aCalculateConnectivity=true)
Holds all the data relating to one schematic.
Definition schematic.h:90
A container for handling SCH_SHEET_PATH objects in a flattened hierarchy.
std::optional< SCH_SHEET_PATH > GetOrdinalPath(const SCH_SCREEN *aScreen) const
Return the ordinal sheet path of aScreen.
Handle access to a stack of flattened SCH_SHEET objects by way of a path for creating a flattened sch...
wxString PathHumanReadable(bool aUseShortRootName=true, bool aStripTrailingSeparator=false, bool aEscapeSheetNames=false) const
Return the sheet path in a human readable form made from the sheet names.
void push_back(SCH_SHEET *aSheet)
Forwarded method from std::vector.
Sheet symbol placed in a schematic, and is the entry point for a sub schematic.
Definition sch_sheet.h:44
SCH_SCREEN * GetScreen() const
Definition sch_sheet.h:139
std::vector< SCH_SHEET > m_sheets
handy store of SCH_SHEET objects
SCH_SHEET_PATH m_linear
We look at sheet 2 in the hierarchy: Sheets: 0 -> 1 -> 2.
static const std::string ProjectFileExtension
std::string GetEeschemaTestDataDir()
Get the configured location of Eeschema test data.
bool IsUUIDPathWithLevels(const std::string &aStr, unsigned aLevels)
Predicate to check a string is a UUID path format.
Definition of the SCH_SHEET_PATH and SCH_SHEET_LIST classes for Eeschema.
BOOST_AUTO_TEST_CASE(HorizontalAlignment)
BOOST_AUTO_TEST_SUITE_END()
BOOST_AUTO_TEST_CASE(Empty)
Declare the test suite.
BOOST_CHECK_PREDICATE(ArePolylineEndPointsNearCircle,(chain)(c.m_geom.m_center_point)(radius)(accuracy+epsilon))
BOOST_CHECK_EQUAL(result, "25.4")
Test utilities for timestamps.
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:683
Definition of file extensions used in Kicad.