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 child->RemovePin( pin );
69
70 child->GetFields().emplace_back( child, FIELD_T::SHEET_USER, "Netclass" );
71 child->GetFields().back().SetText( "MissingRootClass" );
72 screen->Update( child, false );
73 schematic->RebuildConnectivity();
74 const SCH_SHEET_PATH root = schematic->Hierarchy().front();
75 BOOST_REQUIRE( root.LastScreen() == screen );
76
77 if( backend )
78 schematic->ConnectionGraph()->Reset();
79
80 ERC_TESTER tester( schematic.get() );
82 size_t count = 0;
83
84 for( SCH_ITEM* item : screen->Items().OfType( SCH_MARKER_T ) )
85 {
86 const auto* marker = static_cast<SCH_MARKER*>( item );
87 const auto error = std::static_pointer_cast<ERC_ITEM>( marker->GetRCItem() );
88
89 if( error->GetErrorCode() != ERCE_UNDEFINED_NETCLASS )
90 continue;
91
92 BOOST_CHECK( error->GetMainItemID() == child->m_Uuid );
93 BOOST_CHECK( marker->GetPosition() == child->GetPosition() );
94 BOOST_REQUIRE( error->IsSheetSpecific() );
95 BOOST_CHECK( error->GetSpecificSheetPath().PathRef() == root.PathRef() );
96 BOOST_CHECK( error->GetErrorMessage( true ).Contains( "MissingRootClass" ) );
97 ++count;
98 }
99
100 BOOST_CHECK_EQUAL( count, 1 );
101 }
102}
103
104
105BOOST_AUTO_TEST_CASE( ERCMissingNetclassesUseActiveVariantPerInstance )
106{
107 LOCALE_IO locale;
108 auto& enabled = const_cast<ADVANCED_CFG&>( ADVANCED_CFG::GetCfg() ).m_ConnectivityEngine;
109 SCOPED_SET_RESET restore( enabled, enabled );
110
111 for( bool backend : { false, true } )
112 {
113 BOOST_TEST_CONTEXT( "new engine=" << backend )
114 {
115 enabled = backend;
116 SETTINGS_MANAGER settings;
117 std::unique_ptr<SCHEMATIC> schematic;
118 KI_TEST::LoadSchematic( settings, "legacy_hierarchy/legacy_hierarchy", schematic );
119 std::vector<SCH_SHEET_PATH> paths;
120
121 for( const SCH_SHEET_PATH& path : schematic->Hierarchy() )
122 {
123 if( path.LastScreen()->GetFileName().EndsWith( "ampli_ht.kicad_sch" ) )
124 paths.push_back( path );
125 }
126
127 BOOST_REQUIRE_EQUAL( paths.size(), 2 );
128 SCH_SCREEN* screen = paths[0].LastScreen();
129 SCH_SYMBOL* symbol = nullptr;
130
131 for( SCH_ITEM* item : screen->Items().OfType( SCH_SYMBOL_T ) )
132 {
133 symbol = static_cast<SCH_SYMBOL*>( item );
134 break;
135 }
136
137 BOOST_REQUIRE( symbol );
138 SCH_FIELD field( symbol, FIELD_T::USER, "Netclass" );
139 field.SetText( "Default" );
140 symbol->AddField( field );
141 const wxString variant( "Netclass variant" );
142 schematic->AddVariant( variant );
143 symbol->SetFieldText( "Netclass", "MissingVariantClass", &paths[0], variant );
144 screen->Update( symbol, false );
145 schematic->SetCurrentVariant( variant );
146 const SCH_FIELD* netclass = symbol->GetField( "Netclass" );
147 BOOST_REQUIRE( netclass );
148 BOOST_REQUIRE_EQUAL( netclass->GetShownText( &paths[0], FOR_NETNAME, variant ),
149 wxString( "MissingVariantClass" ) );
150 BOOST_REQUIRE_EQUAL( netclass->GetShownText( &paths[1], FOR_NETNAME, variant ), wxString( "Default" ) );
151 schematic->RebuildConnectivity();
152
153 if( backend )
154 schematic->ConnectionGraph()->Reset();
155
156 ERC_TESTER tester( schematic.get() );
158 std::vector<SCH_ITEM*> markers;
159
160 for( SCH_ITEM* item : screen->Items().OfType( SCH_MARKER_T ) )
161 {
162 const auto* marker = static_cast<SCH_MARKER*>( item );
163 const auto error = std::static_pointer_cast<ERC_ITEM>( marker->GetRCItem() );
164
165 if( error->GetErrorCode() != ERCE_UNDEFINED_NETCLASS )
166 continue;
167
168 BOOST_CHECK( error->GetMainItemID() == symbol->m_Uuid );
169 BOOST_REQUIRE( error->IsSheetSpecific() );
170 BOOST_CHECK( error->GetSpecificSheetPath().PathRef() == paths[0].PathRef() );
171 BOOST_CHECK( error->GetErrorMessage( true ).Contains( "MissingVariantClass" ) );
172 markers.push_back( item );
173 }
174
175 BOOST_CHECK_EQUAL( markers.size(), 1 );
176
177 for( SCH_ITEM* marker : markers )
178 screen->DeleteItem( marker );
179
180 schematic->SetCurrentVariant( wxString() );
181 schematic->RebuildConnectivity();
183 }
184 }
185}
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:599
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:1234
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:50
std::vector< SCH_FIELD > & GetFields()
Return a reference to the vector holding the sheet's fields.
Definition sch_sheet.h:93
void RemovePin(const SCH_SHEET_PIN *aSheetPin)
Remove aSheetPin from the sheet.
VECTOR2I GetPosition() const override
Definition sch_sheet.h:548
std::vector< SCH_SHEET_PIN * > & GetPins()
Definition sch_sheet.h:285
Schematic symbol object.
Definition sch_symbol.h:76
void SetFieldText(SCH_FIELD *aField, const wxString &aFieldName, const wxString &aFieldText, const SCH_SHEET_PATH *aPath, const wxString &aVariantName)
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:94
@ 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_CHECK(res.column_datatype(0)==SQL_NUMERIC||res.column_datatype(0)==SQL_DECIMAL)
BOOST_REQUIRE_EQUAL(rows.size(), 1)
BOOST_CHECK_EQUAL(std::any_cast< std::string >(rows[0].at("val")), "12345678901234567.89")
BOOST_REQUIRE(res.columns()==1)
BOOST_AUTO_TEST_CASE(ERCMissingNetclassesIncludeSheetsWithoutElectricalIslands)
std::string path
KIBIS_PIN * pin
BOOST_TEST_CONTEXT("Test Clearance")
@ SCH_SYMBOL_T
Definition typeinfo.h:168
@ SCH_SHEET_T
Definition typeinfo.h:171
@ SCH_MARKER_T
Definition typeinfo.h:154