KiCad PCB EDA Suite
Loading...
Searching...
No Matches
test_erc_netclasses.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
23#include <advanced_config.h>
24#include <connection_graph.h>
25#include <erc/erc.h>
26#include <locale_io.h>
27#include <sch_field.h>
28#include <sch_marker.h>
29#include <sch_sheet.h>
30#include <sch_sheet_pin.h>
31#include <sch_symbol.h>
32#include <schematic.h>
34#include <scoped_set_reset.h>
35
36BOOST_AUTO_TEST_CASE( ERCMissingNetclassesIncludeSheetsWithoutElectricalIslands )
37{
38 LOCALE_IO locale;
39 auto& enabled = const_cast<ADVANCED_CFG&>( ADVANCED_CFG::GetCfg() ).m_ConnectivityEngine;
40 SCOPED_SET_RESET restore( enabled, enabled );
41
42 for( bool backend : { false, true } )
43 {
44 enabled = backend;
45 SETTINGS_MANAGER settings;
46 std::unique_ptr<SCHEMATIC> schematic;
47 KI_TEST::LoadSchematic( settings, "legacy_hierarchy/legacy_hierarchy", schematic );
48 SCH_SCREEN* screen = schematic->RootScreen();
49 SCH_SHEET* child = nullptr;
50 std::vector<SCH_ITEM*> removed;
51
52 for( SCH_ITEM* item : screen->Items() )
53 {
54 if( !child && item->Type() == SCH_SHEET_T )
55 child = static_cast<SCH_SHEET*>( item );
56 else
57 removed.push_back( item );
58 }
59
60 BOOST_REQUIRE( child );
61
62 for( SCH_ITEM* item : removed )
63 screen->DeleteItem( item );
64
65 const auto pins = child->GetPins();
66
67 for( SCH_SHEET_PIN* pin : pins )
68 {
69 child->RemovePin( pin );
70 delete pin;
71 }
72
73 child->GetFields().emplace_back( child, FIELD_T::USER, "Netclass" );
74 child->GetFields().back().SetText( "MissingRootClass" );
75 screen->Update( child, false );
76 schematic->RebuildConnectivity();
77 const SCH_SHEET_PATH root = schematic->Hierarchy().front();
78 BOOST_REQUIRE( root.LastScreen() == screen );
79
80 if( backend )
81 schematic->ConnectionGraph()->Reset();
82
83 ERC_TESTER tester( schematic.get() );
85 size_t count = 0;
86
87 for( SCH_ITEM* item : screen->Items().OfType( SCH_MARKER_T ) )
88 {
89 const auto* marker = static_cast<SCH_MARKER*>( item );
90 const auto error = std::static_pointer_cast<ERC_ITEM>( marker->GetRCItem() );
91
92 if( error->GetErrorCode() != ERCE_UNDEFINED_NETCLASS )
93 continue;
94
95 BOOST_CHECK( error->GetMainItemID() == child->m_Uuid );
96 BOOST_CHECK( marker->GetPosition() == child->GetPosition() );
97 BOOST_REQUIRE( error->IsSheetSpecific() );
98 BOOST_CHECK( error->GetSpecificSheetPath().PathRef() == root.PathRef() );
99 BOOST_CHECK( error->GetErrorMessage( true ).Contains( "MissingRootClass" ) );
100 ++count;
101 }
102
103 BOOST_CHECK_EQUAL( count, 1 );
104 }
105}
106
107
108BOOST_AUTO_TEST_CASE( ERCMissingNetclassesUseActiveVariantPerInstance )
109{
110 LOCALE_IO locale;
111 auto& enabled = const_cast<ADVANCED_CFG&>( ADVANCED_CFG::GetCfg() ).m_ConnectivityEngine;
112 SCOPED_SET_RESET restore( enabled, enabled );
113
114 for( bool backend : { false, true } )
115 {
116 BOOST_TEST_CONTEXT( "new engine=" << backend )
117 {
118 enabled = backend;
119 SETTINGS_MANAGER settings;
120 std::unique_ptr<SCHEMATIC> schematic;
121 KI_TEST::LoadSchematic( settings, "legacy_hierarchy/legacy_hierarchy", schematic );
122 std::vector<SCH_SHEET_PATH> paths;
123
124 for( const SCH_SHEET_PATH& path : schematic->Hierarchy() )
125 {
126 if( path.LastScreen()->GetFileName().EndsWith( "ampli_ht.kicad_sch" ) )
127 paths.push_back( path );
128 }
129
130 BOOST_REQUIRE_EQUAL( paths.size(), 2 );
131 SCH_SCREEN* screen = paths[0].LastScreen();
132 SCH_SYMBOL* symbol = nullptr;
133
134 for( SCH_ITEM* item : screen->Items().OfType( SCH_SYMBOL_T ) )
135 {
136 symbol = static_cast<SCH_SYMBOL*>( item );
137 break;
138 }
139
140 BOOST_REQUIRE( symbol );
141 SCH_FIELD field( symbol, FIELD_T::USER, "Netclass" );
142 field.SetText( "Default" );
143 symbol->AddField( field );
144 const wxString variant( "Netclass variant" );
145 schematic->AddVariant( variant );
146 symbol->SetFieldText( "Netclass", "MissingVariantClass", &paths[0], variant );
147 screen->Update( symbol, false );
148 schematic->SetCurrentVariant( variant );
149 const SCH_FIELD* netclass = symbol->GetField( "Netclass" );
150 BOOST_REQUIRE( netclass );
151 BOOST_REQUIRE_EQUAL( netclass->GetShownText( &paths[0], FOR_NETNAME, variant ),
152 wxString( "MissingVariantClass" ) );
153 BOOST_REQUIRE_EQUAL( netclass->GetShownText( &paths[1], FOR_NETNAME, variant ), wxString( "Default" ) );
154 schematic->RebuildConnectivity();
155
156 if( backend )
157 schematic->ConnectionGraph()->Reset();
158
159 ERC_TESTER tester( schematic.get() );
161 std::vector<SCH_ITEM*> markers;
162
163 for( SCH_ITEM* item : screen->Items().OfType( SCH_MARKER_T ) )
164 {
165 const auto* marker = static_cast<SCH_MARKER*>( item );
166 const auto error = std::static_pointer_cast<ERC_ITEM>( marker->GetRCItem() );
167
168 if( error->GetErrorCode() != ERCE_UNDEFINED_NETCLASS )
169 continue;
170
171 BOOST_CHECK( error->GetMainItemID() == symbol->m_Uuid );
172 BOOST_REQUIRE( error->IsSheetSpecific() );
173 BOOST_CHECK( error->GetSpecificSheetPath().PathRef() == paths[0].PathRef() );
174 BOOST_CHECK( error->GetErrorMessage( true ).Contains( "MissingVariantClass" ) );
175 markers.push_back( item );
176 }
177
178 BOOST_CHECK_EQUAL( markers.size(), 1 );
179
180 for( SCH_ITEM* marker : markers )
181 screen->DeleteItem( marker );
182
183 schematic->SetCurrentVariant( wxString() );
184 schematic->RebuildConnectivity();
186 }
187 }
188}
static const ADVANCED_CFG & GetCfg()
Get the singleton instance's config, which is shared by all consumers.
const KIID m_Uuid
Definition eda_item.h:597
KICAD_T Type() const
Returns the type of object.
Definition eda_item.h:110
EE_TYPE OfType(KICAD_T aType) const
Definition sch_rtree.h:248
Runs the electrical rules checks and adds a SCH_MARKER for each violation.
Definition erc.h:60
int TestMissingNetclasses()
Tests for netclasses that are referenced but not defined.
Definition erc.cpp:1047
Instantiate the current locale within a scope in which you are expecting exceptions to be thrown.
Definition locale_io.h:37
wxString GetShownText(const SCH_SHEET_PATH *aPath, RESOLUTION_CONTEXT aContext, const wxString &aVariantName=wxEmptyString, int aDepth=0) const
void SetText(const wxString &aText) override
Base class for any item which can be embedded within the SCHEMATIC container class,...
Definition sch_item.h:170
EE_RTREE & Items()
Get the full RTree, usually for iterating.
Definition sch_screen.h:122
void Update(SCH_ITEM *aItem, bool aUpdateLibSymbol=true)
Refresh a changed item in the tree and invalidate connectivity.
void DeleteItem(SCH_ITEM *aItem)
Remove aItem from the linked list and deletes the object.
Handle access to a stack of flattened SCH_SHEET objects by way of a path for creating a flattened sch...
SCH_SCREEN * LastScreen()
const KIID_PATH & PathRef() const
Borrow the cached path until this sheet path is modified or destroyed.
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
std::vector< SCH_FIELD > & GetFields()
Return a reference to the vector holding the sheet's fields.
Definition sch_sheet.h:91
void RemovePin(const SCH_SHEET_PIN *aSheetPin)
Remove aSheetPin from the sheet.
VECTOR2I GetPosition() const override
Definition sch_sheet.h:504
std::vector< SCH_SHEET_PIN * > & GetPins()
Definition sch_sheet.h:241
Schematic symbol object.
Definition sch_symbol.h:74
void SetFieldText(const wxString &aFieldName, const wxString &aFieldText, const SCH_SHEET_PATH *aPath=nullptr, const wxString &aVariantName=wxEmptyString)
SCH_FIELD * AddField(const SCH_FIELD &aField)
Add a field to the symbol.
SCH_FIELD * GetField(FIELD_T aFieldType)
Return a mandatory field in this symbol.
RAII class that sets an value at construction and resets it to the original value at destruction.
@ FOR_NETNAME
Definition common.h:90
@ ERCE_UNDEFINED_NETCLASS
A netclass was referenced but not defined.
void LoadSchematic(SETTINGS_MANAGER &aSettingsManager, const wxString &aRelPath, std::unique_ptr< SCHEMATIC > &aSchematic)
@ USER
The field ID hasn't been set yet; field is invalid.
BOOST_AUTO_TEST_CASE(ERCMissingNetclassesIncludeSheetsWithoutElectricalIslands)
BOOST_REQUIRE(intersection.has_value()==c.ExpectedIntersection.has_value())
std::string path
KIBIS_PIN * pin
BOOST_TEST_CONTEXT("Test Clearance")
BOOST_CHECK_EQUAL(result, "25.4")
@ SCH_SYMBOL_T
Definition typeinfo.h:168
@ SCH_SHEET_T
Definition typeinfo.h:171
@ SCH_MARKER_T
Definition typeinfo.h:154