37#include <wx/filename.h>
56 std::unique_ptr<LIB_SYMBOL> symbol = std::make_unique<LIB_SYMBOL>( wxS(
"Symbol" ) );
57 std::unique_ptr<SCH_SCREEN> screen = std::make_unique<SCH_SCREEN>();
61 SYMBOL_BUFFER buffer( std::move( symbol ), std::move( screen ) );
64 BOOST_CHECK( &buffer.
GetSymbol() == &symbolRef );
70 std::unique_ptr<LIB_SYMBOL> originalSymbol =
71 std::make_unique<LIB_SYMBOL>( wxS(
"OriginalSymbol" ) );
72 LIB_SYMBOL& originalSymbolRef = *originalSymbol;
76 BOOST_CHECK( &buffer.
GetOriginal() == &originalSymbolRef );
77 BOOST_CHECK( buffer.
GetOriginal() == originalSymbolRef );
87 wxArrayString symbolNames;
93 auto parentSymbol1 = std::make_unique<LIB_SYMBOL>( wxS(
"Parent1" ) );
100 parentSymbol1->GetValueField().SetText( parentSymbol1->GetName() );
101 libBuffer.
CreateBuffer( std::move( parentSymbol1 ), std::make_unique<SCH_SCREEN>() );
102 BOOST_CHECK( libBuffer.
GetSymbol( parentSymbol1Ref.
GetName() ) == &parentSymbol1Ref );
119 auto childSymbol1 = std::make_unique<LIB_SYMBOL>( wxS(
"Child1" ) );
122 childSymbol1->
SetParent( &parentSymbol1Ref );
123 childSymbol1->GetValueField().SetText( childSymbol1->GetName() );
124 libBuffer.
CreateBuffer( std::move( childSymbol1 ), std::make_unique<SCH_SCREEN>() );
125 BOOST_CHECK( libBuffer.
GetSymbol( childSymbol1Ref.
GetName() ) == &childSymbol1Ref );
146 std::shared_ptr<SYMBOL_BUFFER> buf = libBuffer.
GetBuffer( parentSymbol1Ref.
GetName() );
156 BOOST_CHECK( deletedOk );
157 BOOST_CHECK( libBuffer.
GetBuffers().empty() );
168 props.
name = wxS(
"Standalone" );
181 std::unique_ptr<LIB_SYMBOL> standalone =
186 BOOST_CHECK( standalone->GetPinNameOffset() > 0 );
188 auto parent = std::make_unique<LIB_SYMBOL>( wxS(
"Parent" ) );
189 parent->GetValueField().SetText( parent->GetName() );
192 parent->AddField( user );
194 props.
name = wxS(
"Child" );
201 std::unique_ptr<LIB_SYMBOL> child =
204 BOOST_CHECK( child->GetParent().lock().get() == parent.get() );
205 BOOST_CHECK( child->GetFootprintField().GetText().IsEmpty() );
206 BOOST_CHECK( child->GetDatasheetField().GetText().IsEmpty() );
208 std::vector<SCH_FIELD*> childFields;
209 child->GetFields( childFields );
218 BOOST_CHECK( field->GetText().IsEmpty() );
222 BOOST_CHECK( found );
237 wxString tempDir = wxFileName::CreateTempFileName( wxS(
"kicad_test_" ) );
238 wxRemoveFile( tempDir );
239 wxFileName::Mkdir( tempDir );
240 wxString libPath = wxFileName( tempDir, wxS(
"test_lib.kicad_sym" ) ).GetFullPath();
245 plugin->CreateLibrary( libPath );
248 std::unique_ptr<LIB_SYMBOL> parentSymbol = std::make_unique<LIB_SYMBOL>( wxS(
"Parent" ) );
249 parentSymbol->GetValueField().SetText( wxS(
"Parent" ) );
250 parentSymbol->GetReferenceField().SetText( wxS(
"U" ) );
253 plugin->SaveSymbol( libPath,
new LIB_SYMBOL( *parentSymbol ) );
256 std::unique_ptr<LIB_SYMBOL> derivedSymbol = std::make_unique<LIB_SYMBOL>( wxS(
"Derived" ) );
257 derivedSymbol->GetValueField().SetText( wxS(
"Derived" ) );
258 derivedSymbol->SetParent( parentPtr );
260 plugin->SaveSymbol( libPath,
new LIB_SYMBOL( *derivedSymbol ) );
261 plugin->SaveLibrary( libPath );
267 LIB_SYMBOL* parent = plugin->LoadSymbol( libPath, wxS(
"Parent" ) );
268 LIB_SYMBOL* derived = plugin->LoadSymbol( libPath, wxS(
"Derived" ) );
280 LIB_SYMBOL* loadedParent = plugin->LoadSymbol( libPath, wxS(
"Parent" ) );
281 LIB_SYMBOL* loadedDerived = plugin->LoadSymbol( libPath, wxS(
"Derived" ) );
287 loadedDerived->
SetParent( loadedParent );
290 libBuffer.
CreateBuffer( std::make_unique<LIB_SYMBOL>( *loadedParent ),
291 std::make_unique<SCH_SCREEN>() );
293 std::unique_ptr<LIB_SYMBOL> derivedCopy = std::make_unique<LIB_SYMBOL>( *loadedDerived );
294 derivedCopy->SetParent( libBuffer.
GetSymbol( wxS(
"Parent" ) ) );
295 libBuffer.
CreateBuffer( std::move( derivedCopy ), std::make_unique<SCH_SCREEN>() );
300 BOOST_CHECK( libBuffer.
GetSymbol( wxS(
"Parent" ) ) !=
nullptr );
301 BOOST_CHECK( libBuffer.
GetSymbol( wxS(
"Derived" ) ) !=
nullptr );
304 std::shared_ptr<SYMBOL_BUFFER> derivedBuf = libBuffer.
GetBuffer( wxS(
"Derived" ) );
307 bool deleteResult = libBuffer.
DeleteBuffer( *derivedBuf );
308 BOOST_CHECK( deleteResult );
312 BOOST_CHECK( libBuffer.
GetSymbol( wxS(
"Parent" ) ) !=
nullptr );
313 BOOST_CHECK( libBuffer.
GetSymbol( wxS(
"Derived" ) ) ==
nullptr );
318 std::map<std::string, UTF8> properties;
322 for(
const std::shared_ptr<SYMBOL_BUFFER>& symbolBuf : libBuffer.
GetBuffers() )
324 libBuffer.
SaveBuffer( *symbolBuf, libPath, &*plugin,
true );
328 for(
const std::shared_ptr<SYMBOL_BUFFER>& deletedBuf : libBuffer.
GetDeletedBuffers() )
330 const wxString& originalName = deletedBuf->GetOriginal().GetName();
332 if( plugin->LoadSymbol( libPath, originalName ) )
333 plugin->DeleteSymbol( libPath, originalName, &properties );
336 plugin->SaveLibrary( libPath );
344 LIB_SYMBOL* parent = plugin->LoadSymbol( libPath, wxS(
"Parent" ) );
345 LIB_SYMBOL* derived = plugin->LoadSymbol( libPath, wxS(
"Derived" ) );
347 BOOST_CHECK( parent !=
nullptr );
350 "Derived symbol should have been deleted from the library file" );
354 if( wxFileName::DirExists( tempDir ) )
356 wxFileName::Rmdir( tempDir, wxPATH_RMDIR_RECURSIVE );
374 wxString tempDir = wxFileName::CreateTempFileName( wxS(
"kicad_test_" ) );
375 wxRemoveFile( tempDir );
376 wxFileName::Mkdir( tempDir );
378 wxString srcPath = wxFileName( tempDir, wxS(
"source.kicad_sym" ) ).GetFullPath();
379 wxString dstPath = wxFileName( tempDir, wxS(
"destination.kicad_sym" ) ).GetFullPath();
384 plugin->CreateLibrary( srcPath );
386 auto parent = std::make_unique<LIB_SYMBOL>( wxS(
"Parent" ) );
387 parent->GetValueField().SetText( wxS(
"Parent" ) );
388 parent->GetReferenceField().SetText( wxS(
"U" ) );
391 plugin->SaveSymbol( srcPath,
new LIB_SYMBOL( *parent ) );
393 auto derived = std::make_unique<LIB_SYMBOL>( wxS(
"Derived" ) );
394 derived->GetValueField().SetText( wxS(
"Derived" ) );
395 derived->SetParent( parentPtr );
397 plugin->SaveSymbol( srcPath,
new LIB_SYMBOL( *derived ) );
398 plugin->SaveLibrary( srcPath );
406 std::map<std::string, UTF8> properties;
409 LIB_SYMBOL* loadedParent = srcPlugin->LoadSymbol( srcPath, wxS(
"Parent" ) );
410 LIB_SYMBOL* loadedDerived = srcPlugin->LoadSymbol( srcPath, wxS(
"Derived" ) );
416 BOOST_CHECK_NO_THROW(
417 dstPlugin->SaveSymbol( dstPath,
new LIB_SYMBOL( *loadedParent ), &properties ) );
420 LIB_SYMBOL* dstParent = dstPlugin->LoadSymbol( dstPath, wxS(
"Parent" ), &properties );
424 BOOST_CHECK_NO_THROW(
425 dstPlugin->SaveSymbol( dstPath, newDerived, &properties ) );
427 BOOST_CHECK_NO_THROW( dstPlugin->SaveLibrary( dstPath ) );
434 LIB_SYMBOL* parent = plugin->LoadSymbol( dstPath, wxS(
"Parent" ) );
435 LIB_SYMBOL* derived = plugin->LoadSymbol( dstPath, wxS(
"Derived" ) );
438 "Parent symbol should exist in the saved-as library" );
440 "Derived symbol should exist in the saved-as library" );
446 if( wxFileName::DirExists( tempDir ) )
447 wxFileName::Rmdir( tempDir, wxPATH_RMDIR_RECURSIVE );
462 auto parent = std::make_unique<LIB_SYMBOL>( wxS(
"BaseSymbol" ) );
493 auto parent = std::make_unique<LIB_SYMBOL>( wxS(
"BaseSymbol" ) );
494 parent->GetValueField().SetText( wxS(
"BaseSymbol" ) );
495 parent->GetReferenceField().SetText( wxS(
"U" ) );
509 BOOST_REQUIRE_NO_THROW(
512 const std::string out = formatter.
GetString();
515 "Derived symbol should serialize (extends ...) using the recorded parent name" );
527 wxString tempDir = wxFileName::CreateTempFileName( wxS(
"kicad_test_" ) );
528 wxRemoveFile( tempDir );
529 wxFileName::Mkdir( tempDir );
531 wxString dstPath = wxFileName( tempDir, wxS(
"copy_target.kicad_sym" ) ).GetFullPath();
534 auto parent = std::make_unique<LIB_SYMBOL>( wxS(
"BaseSymbol" ) );
535 parent->GetValueField().SetText( wxS(
"BaseSymbol" ) );
536 parent->GetReferenceField().SetText( wxS(
"U" ) );
538 auto derived = std::make_unique<LIB_SYMBOL>( wxS(
"DerivedSymbol" ) );
539 derived->SetParent( parent.get() );
540 derived->SetParentName( parent->GetName() );
541 derived->GetValueField().SetText( wxS(
"DerivedSymbol" ) );
546 std::map<std::string, UTF8> properties;
549 BOOST_CHECK_NO_THROW(
550 plugin->SaveSymbol( dstPath,
new LIB_SYMBOL( *parent ), &properties ) );
552 LIB_SYMBOL* dstParent = plugin->LoadSymbol( dstPath, wxS(
"BaseSymbol" ), &properties );
559 BOOST_CHECK_NO_THROW( plugin->SaveSymbol( dstPath, newChild, &properties ) );
560 BOOST_CHECK_NO_THROW( plugin->SaveLibrary( dstPath ) );
567 LIB_SYMBOL* reloadedParent = plugin->LoadSymbol( dstPath, wxS(
"BaseSymbol" ) );
568 LIB_SYMBOL* reloadedChild = plugin->LoadSymbol( dstPath, wxS(
"DerivedSymbol" ) );
570 BOOST_CHECK( reloadedParent !=
nullptr );
572 BOOST_CHECK( reloadedChild->
IsDerived() );
576 if( wxFileName::DirExists( tempDir ) )
577 wxFileName::Rmdir( tempDir, wxPATH_RMDIR_RECURSIVE );
void SetContentModified(bool aModified=true)
Store a working copy of a library.
size_t GetDerivedSymbolNames(const wxString &aSymbolName, wxArrayString &aList)
Fetch all of the symbols derived from a aSymbolName into aList.
bool CreateBuffer(std::unique_ptr< LIB_SYMBOL > aCopy, std::unique_ptr< SCH_SCREEN > aScreen)
Create a new buffer to store a symbol. LIB_BUFFER takes ownership of aCopy.
bool DeleteBuffer(const SYMBOL_BUFFER &aSymbolBuf)
Delete the given symbol buffer from the library buffer.
void ClearDeletedBuffer()
void GetSymbolNames(wxArrayString &aSymbolNames, SYMBOL_NAME_FILTER aFilter=SYMBOL_NAME_FILTER::ALL)
Fetch a list of root symbols names from the library buffer.
const std::deque< std::shared_ptr< SYMBOL_BUFFER > > & GetDeletedBuffers() const
Return the deleted symbol buffers that need to be removed from the library file.
bool SaveBuffer(SYMBOL_BUFFER &aSymbolBuf, const wxString &aFileName, SCH_IO *aPlugin, bool aBuffer)
Save stored modifications using a plugin.
const std::deque< std::shared_ptr< SYMBOL_BUFFER > > & GetBuffers() const
Return all buffered symbols.
std::shared_ptr< SYMBOL_BUFFER > GetBuffer(const wxString &aAlias) const
Return a symbol buffer with LIB_SYMBOL holding a symbolic alias.
LIB_SYMBOL * GetSymbol(const wxString &aAlias) const
Return the working copy of a LIB_SYMBOL root object with specified alias.
bool HasDerivedSymbols(const wxString &aParentName) const
Check to see any symbols in the buffer are derived from a parent named aParentName.
bool UpdateBuffer(SYMBOL_BUFFER &aSymbolBuf, const LIB_SYMBOL &aCopy)
Update the buffered symbol with the contents of aCopy.
static std::unique_ptr< LIB_SYMBOL > CreateSymbol(const NEW_SYMBOL_PROPERTIES &aProps, LIB_SYMBOL *aParent)
Define a library symbol object.
SCH_FIELD & GetDescriptionField()
Return reference to the description field.
std::weak_ptr< LIB_SYMBOL > & GetParent()
void SetParent(LIB_SYMBOL *aParent=nullptr)
wxString GetName() const override
SCH_FIELD & GetValueField()
Return reference to the value field.
const wxString & GetParentName() const
void SetParentName(const wxString &aParentName)
void SetText(const wxString &aText) override
static void SaveSymbol(LIB_SYMBOL *aSymbol, OUTPUTFORMATTER &aFormatter, const wxString &aLibName=wxEmptyString, bool aIncludeData=true)
static const char * PropBuffering
The property used internally by the plugin to enable cache buffering which prevents the library file ...
LIB_SYMBOL & GetSymbol() const
SCH_SCREEN * GetScreen() const
LIB_SYMBOL & GetOriginal() const
void SetOriginal(std::unique_ptr< LIB_SYMBOL > aSymbol)
SYMBOL_LIBRARY_MANAGER_TEST_FIXTURE()
std::unique_ptr< T > IO_RELEASER
Helper to hold and release an IO_BASE object when exceptions are thrown.
wxString parentSymbolName
bool keepContentUserFields
bool unitsInterchangeable
@ USER
The field ID hasn't been set yet; field is invalid.
BOOST_AUTO_TEST_CASE(HorizontalAlignment)
BOOST_REQUIRE(intersection.has_value()==c.ExpectedIntersection.has_value())
BOOST_AUTO_TEST_SUITE_END()
BOOST_CHECK_MESSAGE(totalMismatches==0, std::to_string(totalMismatches)+" board(s) with strategy disagreements")
BOOST_AUTO_TEST_CASE(SymbolBuffer)
Test the SYMBOL_BUFFER object.
BOOST_CHECK_EQUAL(result, "25.4")