KiCad PCB EDA Suite
Loading...
Searching...
No Matches
test_pads_sch_import.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>
22
23#include <lib_symbol.h>
24#include <schematic.h>
26#include <sch_io/sch_io_mgr.h>
27#include <sch_screen.h>
28#include <sch_sheet.h>
29#include <sch_sheet_path.h>
30#include <sch_symbol.h>
32
33
34namespace
35{
36
37struct PADS_SCH_IMPORT_FIXTURE
38{
39 PADS_SCH_IMPORT_FIXTURE() : m_schematic( nullptr )
40 {
41 m_settingsManager.LoadProject( "" );
42 m_schematic.SetProject( &m_settingsManager.Prj() );
43 m_schematic.Reset();
44 }
45
46 ~PADS_SCH_IMPORT_FIXTURE()
47 {
48 m_schematic.Reset();
49 }
50
51 SETTINGS_MANAGER m_settingsManager;
52 SCHEMATIC m_schematic;
53};
54
55} // namespace
56
57
58BOOST_FIXTURE_TEST_SUITE( PadsSchImport, PADS_SCH_IMPORT_FIXTURE )
59
60
61BOOST_AUTO_TEST_CASE( CanReadSchematicFile )
62{
63 SCH_IO_PADS plugin;
64
65 wxString padsFile = wxString::FromUTF8(
66 KI_TEST::GetEeschemaTestDataDir() + "/plugins/pads/simple_schematic.txt" );
67
68 BOOST_CHECK( plugin.CanReadSchematicFile( padsFile ) );
69}
70
71
72BOOST_AUTO_TEST_CASE( CanReadSchematicFile_RejectNonPads )
73{
74 SCH_IO_PADS plugin;
75
76 wxString kicadFile = wxString::FromUTF8(
77 KI_TEST::GetEeschemaTestDataDir() + "/plugins/pads/simple_schematic.txt" );
78
79 BOOST_CHECK( plugin.CanReadSchematicFile( kicadFile ) );
80}
81
82
84{
85 IO_RELEASER<SCH_IO> pi( SCH_IO_MGR::FindPlugin( SCH_IO_MGR::SCH_PADS ) );
86 BOOST_CHECK_NE( pi.get(), nullptr );
87}
88
89
90BOOST_AUTO_TEST_CASE( MultiGateImport )
91{
92 SCH_IO_PADS plugin;
93
94 wxString padsFile = wxString::FromUTF8(
95 KI_TEST::GetEeschemaTestDataDir() + "/plugins/pads/multigate_schematic.txt" );
96
97 SCH_SHEET* rootSheet = plugin.LoadSchematicFile( padsFile, &m_schematic );
98 BOOST_REQUIRE( rootSheet );
99 BOOST_REQUIRE( rootSheet->GetScreen() );
100
101 SCH_SCREEN* screen = rootSheet->GetScreen();
102
103 // Collect U1 symbols
104 std::vector<SCH_SYMBOL*> u1Symbols;
105 SCH_SHEET_PATH rootPath;
106 rootPath.push_back( rootSheet );
107
108 for( SCH_ITEM* item : screen->Items().OfType( SCH_SYMBOL_T ) )
109 {
110 SCH_SYMBOL* sym = static_cast<SCH_SYMBOL*>( item );
111
112 if( sym->GetRef( &rootPath ) == wxT( "U1" ) )
113 u1Symbols.push_back( sym );
114 }
115
116 BOOST_REQUIRE_EQUAL( u1Symbols.size(), 2u );
117
118 // Sort by unit number for deterministic checks
119 std::sort( u1Symbols.begin(), u1Symbols.end(),
120 []( const SCH_SYMBOL* a, const SCH_SYMBOL* b )
121 {
122 return a->GetUnit() < b->GetUnit();
123 } );
124
125 // Unit 1 (gate A with TL082A decal) should have 5 pins
126 BOOST_CHECK_EQUAL( u1Symbols[0]->GetUnit(), 1 );
127 BOOST_CHECK_EQUAL( u1Symbols[0]->GetLibPins().size(), 5u );
128
129 // Unit 2 (gate B with TL082 decal) should have 3 pins
130 BOOST_CHECK_EQUAL( u1Symbols[1]->GetUnit(), 2 );
131 BOOST_CHECK_EQUAL( u1Symbols[1]->GetLibPins().size(), 3u );
132
133 // Both should share the same multi-unit LIB_SYMBOL with 2 units
134 BOOST_CHECK( u1Symbols[0]->IsMultiUnit() );
135 BOOST_CHECK_EQUAL( u1Symbols[0]->GetUnitCount(), 2 );
136
137 // Both references should be "U1" (not "U1-A" or "U1-B")
138 BOOST_CHECK_EQUAL( u1Symbols[0]->GetRef( &rootPath ), wxT( "U1" ) );
139 BOOST_CHECK_EQUAL( u1Symbols[1]->GetRef( &rootPath ), wxT( "U1" ) );
140}
141
142
143BOOST_AUTO_TEST_CASE( Issue23420_HeaderWithCodePageSuffix )
144{
145 // Regression test for https://gitlab.com/kicad/code/kicad/-/issues/23420
146 // PADS Logic schematics exported with a code page suffix in the header
147 // (e.g. *PADS-LOGIC-V9.0-CP1250*) must be detected and parsed.
148 SCH_IO_PADS plugin;
149
150 wxString padsFile = wxString::FromUTF8(
152 + "/plugins/pads/issue23420_codepage_schematic.txt" );
153
154 BOOST_CHECK( plugin.CanReadSchematicFile( padsFile ) );
155
156 SCH_SHEET* rootSheet = plugin.LoadSchematicFile( padsFile, &m_schematic );
157
158 BOOST_REQUIRE( rootSheet );
159 BOOST_REQUIRE( rootSheet->GetScreen() );
160}
161
162
163BOOST_AUTO_TEST_CASE( CanReadLibrary )
164{
165 SCH_IO_PADS plugin;
166
167 wxString padsFile = wxString::FromUTF8(
168 KI_TEST::GetEeschemaTestDataDir() + "/plugins/pads/symbols_schematic.txt" );
169
170 BOOST_CHECK( plugin.CanReadLibrary( padsFile ) );
171}
172
173
174BOOST_AUTO_TEST_CASE( EnumerateSymbolLib_NamesFromSchematic )
175{
176 SCH_IO_PADS plugin;
177
178 wxString padsFile = wxString::FromUTF8(
179 KI_TEST::GetEeschemaTestDataDir() + "/plugins/pads/symbols_schematic.txt" );
180
181 wxArrayString names;
182 BOOST_CHECK_NO_THROW( plugin.EnumerateSymbolLib( names, padsFile ) );
183 BOOST_CHECK_GT( names.GetCount(), 0u );
184}
185
186
187BOOST_AUTO_TEST_CASE( EnumerateSymbolLib_ReturnsLibSymbols )
188{
189 SCH_IO_PADS plugin;
190
191 wxString padsFile = wxString::FromUTF8(
192 KI_TEST::GetEeschemaTestDataDir() + "/plugins/pads/symbols_schematic.txt" );
193
194 std::vector<LIB_SYMBOL*> symbols;
195 BOOST_CHECK_NO_THROW( plugin.EnumerateSymbolLib( symbols, padsFile ) );
196 BOOST_CHECK_GT( symbols.size(), 0u );
197
198 for( LIB_SYMBOL* sym : symbols )
199 BOOST_REQUIRE( sym != nullptr );
200}
201
202
203BOOST_AUTO_TEST_CASE( LoadSymbol_ByName )
204{
205 SCH_IO_PADS plugin;
206
207 wxString padsFile = wxString::FromUTF8(
208 KI_TEST::GetEeschemaTestDataDir() + "/plugins/pads/symbols_schematic.txt" );
209
210 wxArrayString names;
211 plugin.EnumerateSymbolLib( names, padsFile );
212
213 BOOST_REQUIRE_GT( names.GetCount(), 0u );
214
215 LIB_SYMBOL* sym = plugin.LoadSymbol( padsFile, names.Item( 0 ) );
216 BOOST_REQUIRE( sym != nullptr );
217 BOOST_CHECK_EQUAL( sym->GetName(), names.Item( 0 ) );
218}
219
220
221BOOST_AUTO_TEST_CASE( LoadSymbol_UnknownReturnsNull )
222{
223 SCH_IO_PADS plugin;
224
225 wxString padsFile = wxString::FromUTF8(
226 KI_TEST::GetEeschemaTestDataDir() + "/plugins/pads/symbols_schematic.txt" );
227
228 LIB_SYMBOL* sym = plugin.LoadSymbol( padsFile, wxT( "NO_SUCH_SYMBOL_12345" ) );
229 BOOST_CHECK( sym == nullptr );
230}
231
232
233BOOST_AUTO_TEST_CASE( MultiGatePartTypeBecomesMultiUnitLibSymbol )
234{
235 SCH_IO_PADS plugin;
236
237 wxString padsFile = wxString::FromUTF8(
238 KI_TEST::GetEeschemaTestDataDir() + "/plugins/pads/multigate_schematic.txt" );
239
240 std::vector<LIB_SYMBOL*> symbols;
241 BOOST_CHECK_NO_THROW( plugin.EnumerateSymbolLib( symbols, padsFile ) );
242
243 bool foundMultiUnit = false;
244
245 for( LIB_SYMBOL* sym : symbols )
246 {
247 if( sym && sym->GetUnitCount() > 1 )
248 {
249 foundMultiUnit = true;
250 break;
251 }
252 }
253
254 BOOST_CHECK( foundMultiUnit );
255}
256
257
258BOOST_AUTO_TEST_CASE( IsLibraryNotWritable )
259{
260 SCH_IO_PADS plugin;
261
262 wxString padsFile = wxString::FromUTF8(
263 KI_TEST::GetEeschemaTestDataDir() + "/plugins/pads/symbols_schematic.txt" );
264
265 BOOST_CHECK( !plugin.IsLibraryWritable( padsFile ) );
266}
267
268
EE_TYPE OfType(KICAD_T aType) const
Definition sch_rtree.h:225
Define a library symbol object.
Definition lib_symbol.h:83
wxString GetName() const override
Definition lib_symbol.h:145
A SCH_IO derivation for loading PADS Logic schematic files.
Definition sch_io_pads.h:42
LIB_SYMBOL * LoadSymbol(const wxString &aLibraryPath, const wxString &aPartName, const std::map< std::string, UTF8 > *aProperties=nullptr) override
Load a LIB_SYMBOL object having aPartName from the aLibraryPath containing a library format that this...
void EnumerateSymbolLib(wxArrayString &aSymbolNameList, const wxString &aLibraryPath, const std::map< std::string, UTF8 > *aProperties=nullptr) override
Populate a list of LIB_SYMBOL alias names contained within the library aLibraryPath.
bool CanReadLibrary(const wxString &aFileName) const override
Checks if this IO object can read the specified library file/directory.
bool IsLibraryWritable(const wxString &aLibraryPath) override
Return true if the library at aLibraryPath is writable.
Definition sch_io_pads.h:75
bool CanReadSchematicFile(const wxString &aFileName) const override
Checks if this SCH_IO can read the specified schematic file.
SCH_SHEET * LoadSchematicFile(const wxString &aFileName, SCHEMATIC *aSchematic, SCH_SHEET *aAppendToMe=nullptr, const std::map< std::string, UTF8 > *aProperties=nullptr) override
Load information from some input file format that this SCH_IO implementation knows about,...
Base class for any item which can be embedded within the SCHEMATIC container class,...
Definition sch_item.h:168
EE_RTREE & Items()
Get the full RTree, usually for iterating.
Definition sch_screen.h:119
Handle access to a stack of flattened SCH_SHEET objects by way of a path for creating a flattened sch...
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:48
SCH_SCREEN * GetScreen() const
Definition sch_sheet.h:143
Schematic symbol object.
Definition sch_symbol.h:76
const wxString GetRef(const SCH_SHEET_PATH *aSheet, bool aIncludeUnit=false) const override
std::unique_ptr< T > IO_RELEASER
Helper to hold and release an IO_BASE object when exceptions are thrown.
Definition io_mgr.h:33
std::string GetEeschemaTestDataDir()
Get the configured location of Eeschema test data.
Definition of the SCH_SHEET_PATH and SCH_SHEET_LIST classes for Eeschema.
BOOST_AUTO_TEST_CASE(HorizontalAlignment)
BOOST_REQUIRE(intersection.has_value()==c.ExpectedIntersection.has_value())
BOOST_AUTO_TEST_SUITE_END()
BOOST_AUTO_TEST_CASE(CanReadSchematicFile)
BOOST_CHECK_EQUAL(result, "25.4")
@ SCH_SYMBOL_T
Definition typeinfo.h:173