KiCad PCB EDA Suite
Loading...
Searching...
No Matches
test_sch_footprint_field_reconciler.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
20#include <memory>
21#include <utility>
22#include <vector>
23
25
26#include <wx/filename.h>
27#include <wx/stdpaths.h>
28
29#include <lib_id.h>
30#include <pgm_base.h>
31#include <project.h>
32#include <schematic.h>
33#include <sch_screen.h>
34#include <sch_sheet.h>
35#include <sch_symbol.h>
38#include <sch_io/sch_io.h>
39#include <sch_io/sch_io_mgr.h>
40
42
43
44namespace
45{
47std::vector<std::pair<wxString, wxString>> collectFootprintFpids( SCHEMATIC& aSchematic )
48{
49 std::vector<std::pair<wxString, wxString>> out;
50 SCH_SCREENS screens( aSchematic.Root() );
51
52 for( SCH_SCREEN* screen = screens.GetFirst(); screen; screen = screens.GetNext() )
53 {
54 for( SCH_ITEM* item : screen->Items().OfType( SCH_SYMBOL_T ) )
55 {
56 SCH_SYMBOL* symbol = static_cast<SCH_SYMBOL*>( item );
57 wxString text = symbol->GetField( FIELD_T::FOOTPRINT )->GetText();
58
59 if( text.IsEmpty() )
60 continue;
61
62 LIB_ID fpid;
63
64 if( fpid.Parse( text ) >= 0 || fpid.GetUniStringLibItemName().IsEmpty() )
65 continue;
66
67 out.emplace_back( fpid.GetUniStringLibNickname(), fpid.GetUniStringLibItemName() );
68 }
69 }
70
71 return out;
72}
73} // namespace
74
75
76BOOST_AUTO_TEST_SUITE( SchFootprintFieldReconciler )
77
78
79// Eagle sets "<project>:<package>" footprint fields on a non-real lib; reconciler re-points nick to
80// cache, keeps package; fails on revert since fields keep the bogus nick
81BOOST_AUTO_TEST_CASE( EagleFootprintFieldsRelinkToCache )
82{
83 wxString sep = wxFileName::GetPathSeparator();
84 wxString dir = wxStandardPaths::Get().GetTempDir() + sep + wxT( "sch_fpfield_reconcile-qa" );
85
86 if( wxDirExists( dir ) )
87 wxFileName::Rmdir( dir, wxPATH_RMDIR_RECURSIVE );
88
89 wxFileName::Mkdir( dir, wxS_DIR_DEFAULT, wxPATH_MKDIR_FULL );
90
91 wxString projectPath = dir + sep + wxT( "sch_fpfield.kicad_pro" );
92 Pgm().GetSettingsManager().LoadProject( projectPath );
94 Pgm().GetLibraryManager().LoadProjectTables( project.GetProjectDirectory() );
95
96 wxFileName eagleFn( KI_TEST::GetEeschemaTestDataDir() );
97 eagleFn.AppendDir( wxS( "io" ) );
98 eagleFn.AppendDir( wxS( "eagle" ) );
99 eagleFn.SetFullName( wxS( "eagle-import-testfile.sch" ) );
100 BOOST_REQUIRE( eagleFn.FileExists() );
101
102 std::unique_ptr<SCHEMATIC> schematic = std::make_unique<SCHEMATIC>( &project );
103 schematic->Reset();
104
105 IO_RELEASER<SCH_IO> pi( SCH_IO_MGR::FindPlugin( SCH_IO_MGR::SCH_EAGLE ) );
106 BOOST_REQUIRE( pi.get() );
107
108 SCH_SHEET* loadedSheet = pi->LoadSchematicFile( eagleFn.GetFullPath(), schematic.get() );
109 BOOST_REQUIRE( loadedSheet );
110
111 std::vector<std::pair<wxString, wxString>> before = collectFootprintFpids( *schematic );
112 BOOST_REQUIRE_GT( before.size(), 0 );
113
114 const wxString cacheNick = wxS( "eagle_test-import-fps" );
115 SCH_FOOTPRINT_FIELD_RECONCILER reconciler( cacheNick, {} );
116
117 SCH_FP_FIELD_RECONCILE_RESULT result = reconciler.Reconcile( *schematic );
118
119 BOOST_CHECK_GT( result.m_relinkedToCache, 0 );
120
121 std::vector<std::pair<wxString, wxString>> after = collectFootprintFpids( *schematic );
122 BOOST_CHECK_EQUAL( after.size(), before.size() );
123
124 for( const auto& [nick, item] : after )
125 {
126 BOOST_CHECK_MESSAGE( nick == cacheNick,
127 wxString::Format( "Footprint field '%s:%s' not re-linked to cache",
128 nick, item ) );
129 }
130}
131
132
void LoadProjectTables(std::initializer_list< LIBRARY_TABLE_TYPE > aTablesToLoad={})
(Re)loads the project library tables in the given list, or all tables if no list is given
A logical library item identifier and consists of various portions much like a URI.
Definition lib_id.h:45
int Parse(const UTF8 &aId, bool aFix=false)
Parse LIB_ID with the information from aId.
Definition lib_id.cpp:65
const wxString GetUniStringLibItemName() const
Get strings for display messages in dialogs.
Definition lib_id.h:108
const wxString GetUniStringLibNickname() const
Definition lib_id.h:84
virtual SETTINGS_MANAGER & GetSettingsManager() const
Definition pgm_base.h:124
virtual LIBRARY_MANAGER & GetLibraryManager() const
Definition pgm_base.h:126
Container for project specific data.
Definition project.h:63
Holds all the data relating to one schematic.
Definition schematic.h:90
SCH_SHEET & Root() const
Definition schematic.h:134
virtual const wxString & GetText() const override
Return the string associated with the text object.
Definition sch_field.h:128
Frame-independent, non-interactive service that rewrites the library nickname of every schematic symb...
Base class for any item which can be embedded within the SCHEMATIC container class,...
Definition sch_item.h:162
Container class that holds multiple SCH_SCREEN objects in a hierarchy.
Definition sch_screen.h:746
Sheet symbol placed in a schematic, and is the entry point for a sub schematic.
Definition sch_sheet.h:44
Schematic symbol object.
Definition sch_symbol.h:69
SCH_FIELD * GetField(FIELD_T aFieldType)
Return a mandatory field in this symbol.
bool LoadProject(const wxString &aFullPath, bool aSetActive=true)
Load a project or sets up a new project with a specified path.
PROJECT & Prj() const
A helper while we are not MDI-capable – return the one and only project.
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.
PGM_BASE & Pgm()
The global program "get" accessor.
see class PGM_BASE
@ FOOTPRINT
Field Name Module PCB, i.e. "16DIP300".
BOOST_AUTO_TEST_CASE(HorizontalAlignment)
BOOST_AUTO_TEST_SUITE(CadstarPartParser)
BOOST_REQUIRE(intersection.has_value()==c.ExpectedIntersection.has_value())
BOOST_AUTO_TEST_SUITE_END()
wxString result
Test unit parsing edge cases and error handling.
BOOST_CHECK_EQUAL(result, "25.4")
@ SCH_SYMBOL_T
Definition typeinfo.h:169