KiCad PCB EDA Suite
Loading...
Searching...
No Matches
test_sch_io_database_cycle.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 modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation, either version 3 of the License, or (at your
9 * option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20#include <boost/test/unit_test.hpp>
21
23#include <lib_symbol.h>
26#include <reporter.h>
27
28// We need access to private members of SCH_IO_DATABASE to drive the cycle-detection
29// state directly. The plugin declares SCH_IO_DATABASE_CYCLE_DETECTION_FIXTURE as a friend.
31
32
41{
42public:
44 {
45 // s_inProgressLoads is static per thread, make sure any prior tests tidied up
46 // after themselves.
47 BOOST_REQUIRE_MESSAGE( SCH_IO_DATABASE::s_inProgressLoads.empty(),
48 "s_inProgressLoads is not empty at test start" );
49 }
50
52 {
53 // Do not leak our in-progress loads into subsequent tests.
55 }
56
59 std::unique_ptr<LIB_SYMBOL> InvokeWithCycle( SCH_IO_DATABASE& aPlugin,
60 const wxString& aSymbolName,
61 const DATABASE_LIB_TABLE& aTable,
62 const DATABASE_CONNECTION::ROW& aRow,
63 const wxString& aInProgressKey )
64 {
65 aPlugin.s_inProgressLoads.insert( aInProgressKey );
66 return aPlugin.loadSymbolFromRow( aSymbolName, aTable, aRow );
67 }
68
71 std::unique_ptr<LIB_SYMBOL> Invoke( SCH_IO_DATABASE& aPlugin,
72 const wxString& aSymbolName,
73 const DATABASE_LIB_TABLE& aTable,
74 const DATABASE_CONNECTION::ROW& aRow )
75 {
76 return aPlugin.loadSymbolFromRow( aSymbolName, aTable, aRow );
77 }
78
79 bool IsInProgressEmpty( const SCH_IO_DATABASE& aPlugin ) const
80 {
81 return aPlugin.s_inProgressLoads.empty();
82 }
83
84 std::size_t InProgressSize( const SCH_IO_DATABASE& aPlugin ) const
85 {
86 return aPlugin.s_inProgressLoads.size();
87 }
88};
89
90
91BOOST_AUTO_TEST_SUITE( SchIoDatabaseCycle )
92
93
94// m_adapter is deliberately left null: if the cycle short-circuit fails, loadSymbolFromRow
95// dereferences the null adapter and crashes the test. So this fails on revert of the fix.
96BOOST_AUTO_TEST_CASE( DetectsAlreadyInProgressLibId )
97{
98 SCH_IO_DATABASE plugin;
99
101 table.name = "SelfRefDB";
102 table.table = "Parts";
103 table.key_col = "Part ID";
104 table.symbols_col = "Symbols";
105
107 row[table.symbols_col] = std::string( "SelfRefDB:R-001" );
108
110
111 std::unique_ptr<LIB_SYMBOL> symbol =
112 fixture.InvokeWithCycle( plugin, wxS( "R-001" ), table, row, wxS( "SelfRefDB:R-001" ) );
113
114 BOOST_REQUIRE( symbol );
115 BOOST_CHECK( symbol->GetName() == wxS( "R-001" ) );
116
117 // The re-insert of the already-seeded key must observe it as present and so must not erase
118 // the fixture's entry on the way out.
119 BOOST_CHECK_EQUAL( fixture.InProgressSize( plugin ), 1u );
120}
121
122
123// A leaked in-progress entry would make a later load of the same LIB_ID a false positive cycle.
124// Drive two loads through a real (empty) adapter and require the set to be empty after each, so
125// a broken RAII removal is caught.
126BOOST_AUTO_TEST_CASE( RaiiGuardBalancesInProgressSet )
127{
128 LIBRARY_MANAGER libManager;
129 SYMBOL_LIBRARY_ADAPTER adapter( libManager );
130
131 SCH_IO_DATABASE plugin;
132 plugin.SetLibraryManagerAdapter( &adapter );
133
135 table.name = "SelfRefDB";
136 table.table = "Parts";
137 table.key_col = "Part ID";
138 table.symbols_col = "Symbols";
139
141 // Valid LIB_ID that will be tracked. The adapter has no libraries loaded so its LoadSymbol
142 // returns null without ever re-entering SCH_IO_DATABASE.
143 row[table.symbols_col] = std::string( "SomeLib:R-001" );
144
146
147 std::unique_ptr<LIB_SYMBOL> symbol = fixture.Invoke( plugin, wxS( "R-001" ), table, row );
148
149 BOOST_REQUIRE( symbol );
150 BOOST_CHECK( fixture.IsInProgressEmpty( plugin ) );
151
152 // A second call on the same row must not see leftover state from the first.
153 symbol = fixture.Invoke( plugin, wxS( "R-001" ), table, row );
154 BOOST_REQUIRE( symbol );
155 BOOST_CHECK( fixture.IsInProgressEmpty( plugin ) );
156}
157
158
159// Tokens of the Symbols column that are not Library:Symbol must be reported, not silently dropped.
160BOOST_AUTO_TEST_CASE( ReportsMalformedSymbolReference )
161{
162 LIBRARY_MANAGER libManager;
163 SYMBOL_LIBRARY_ADAPTER adapter( libManager );
164
165 SCH_IO_DATABASE plugin;
166 plugin.SetLibraryManagerAdapter( &adapter );
167
169 plugin.SetReporter( &reporter );
170
172 table.name = "Parts";
173 table.table = "Parts";
174 table.key_col = "Part ID";
175 table.symbols_col = "Symbols";
176
178
180 row[table.symbols_col] = std::string( "SomeLib:R-001" );
181
182 fixture.Invoke( plugin, wxS( "R-001" ), table, row );
183 BOOST_CHECK( reporter.GetMessages().IsEmpty() );
184
185 row[table.symbols_col] = std::string( "NotASymbol;SomeLib:R-001" );
186
187 fixture.Invoke( plugin, wxS( "R-001" ), table, row );
188 BOOST_CHECK_EQUAL( reporter.GetMessages(),
189 wxString( "'NotASymbol' is not a symbol; expected Library:Symbol.\n" ) );
190}
191
192
std::map< std::string, std::any > ROW
virtual void SetReporter(REPORTER *aReporter)
Set an optional reporter for warnings/errors.
Definition io_base.h:89
Regression test for https://gitlab.com/kicad/code/kicad/-/issues/24249.
std::unique_ptr< LIB_SYMBOL > InvokeWithCycle(SCH_IO_DATABASE &aPlugin, const wxString &aSymbolName, const DATABASE_LIB_TABLE &aTable, const DATABASE_CONNECTION::ROW &aRow, const wxString &aInProgressKey)
Drive loadSymbolFromRow with aInProgressKey already marked in-progress, simulating the second entry o...
std::size_t InProgressSize(const SCH_IO_DATABASE &aPlugin) const
bool IsInProgressEmpty(const SCH_IO_DATABASE &aPlugin) const
std::unique_ptr< LIB_SYMBOL > Invoke(SCH_IO_DATABASE &aPlugin, const wxString &aSymbolName, const DATABASE_LIB_TABLE &aTable, const DATABASE_CONNECTION::ROW &aRow)
Drive loadSymbolFromRow without pre-seeding the in-progress set; the plugin inserts and (via its RAII...
A KiCad database library provides both symbol and footprint metadata, so there are "shim" plugins on ...
static thread_local std::unordered_set< wxString > s_inProgressLoads
LIB_IDs whose resolution is in flight on the calling thread, used to break self-referential cycles wh...
void SetLibraryManagerAdapter(SYMBOL_LIBRARY_ADAPTER *aAdapter) override
Some library plugins need to interface with other loaded libraries.
std::unique_ptr< LIB_SYMBOL > loadSymbolFromRow(const wxString &aSymbolName, const DATABASE_LIB_TABLE &aTable, const DATABASE_CONNECTION::ROW &aRow)
An interface to the global shared library manager that is schematic-specific and linked to one projec...
A wrapper for reporting to a wxString object.
Definition reporter.h:242
static bool empty(const wxTextEntryBase *aCtrl)
A database library table will be mapped to a sub-library provided by the database library entry in th...
BOOST_AUTO_TEST_CASE(HorizontalAlignment)
BOOST_AUTO_TEST_SUITE(CadstarPartParser)
BOOST_REQUIRE(intersection.has_value()==c.ExpectedIntersection.has_value())
BOOST_AUTO_TEST_SUITE_END()
IbisParser parser & reporter
BOOST_AUTO_TEST_CASE(DetectsAlreadyInProgressLibId)
BOOST_CHECK_EQUAL(result, "25.4")