KiCad PCB EDA Suite
Loading...
Searching...
No Matches
test_symbol_library_manager.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 (C) 2023 Wayne Stambaugh <[email protected]>
5 * Copyright (C) 2023 KiCad Developers, see AUTHORS.txt for contributors.
6 *
7 * This program is free software: you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation, either version 3 of the License, or (at your
10 * option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program. If not, see <http://www.gnu.org/licenses/>.
19 */
20
27
28// Code under test
30
32{
33public:
35 {
36 }
37};
38
39
40BOOST_FIXTURE_TEST_SUITE( SymbolLibraryManager, SYMBOL_LIBRARY_MANAGER_TEST_FIXTURE )
41
42
43
46BOOST_AUTO_TEST_CASE( SymbolBuffer )
47{
48 std::unique_ptr<LIB_SYMBOL> symbol = std::make_unique<LIB_SYMBOL>( wxS( "Symbol" ) );
49 std::unique_ptr<SCH_SCREEN> screen = std::make_unique<SCH_SCREEN>();
50
51 LIB_SYMBOL& symbolRef = *symbol;
52
53 SYMBOL_BUFFER buffer( std::move( symbol ), std::move( screen ) );
54
55 BOOST_CHECK( !buffer.IsModified() );
56 BOOST_CHECK( &buffer.GetSymbol() == &symbolRef );
57 BOOST_CHECK( buffer.GetOriginal() == symbolRef );
58
59 buffer.GetScreen()->SetContentModified();
60 BOOST_CHECK( buffer.IsModified() );
61
62 std::unique_ptr<LIB_SYMBOL> originalSymbol =
63 std::make_unique<LIB_SYMBOL>( wxS( "OriginalSymbol" ) );
64 LIB_SYMBOL& originalSymbolRef = *originalSymbol;
65
66 buffer.SetOriginal( std::move( originalSymbol ) );
67
68 BOOST_CHECK( &buffer.GetOriginal() == &originalSymbolRef );
69 BOOST_CHECK( buffer.GetOriginal() == originalSymbolRef );
70 BOOST_CHECK( &buffer.GetSymbol() != &buffer.GetOriginal() );
71}
72
73
78{
79 wxArrayString symbolNames;
80 LIB_BUFFER libBuffer( wxS( "TestLibrary" ) );
81
82 BOOST_CHECK( !libBuffer.IsModified() );
83 BOOST_CHECK_EQUAL( libBuffer.GetHash(), 1 );
84
85 auto parentSymbol1 = std::make_unique<LIB_SYMBOL>( wxS( "Parent1" ) );
86 // A but clunky, but real code would get symbol from the LIB_BUFFER
87 // via GetSymbol etc, rather than retaining a reference after construction.
88 LIB_SYMBOL& parentSymbol1Ref = *parentSymbol1;
89
90 BOOST_CHECK_EQUAL( libBuffer.GetSymbol( parentSymbol1->GetName() ), nullptr );
91
92 parentSymbol1->GetValueField().SetText( parentSymbol1->GetName() );
93 libBuffer.CreateBuffer( std::move( parentSymbol1 ), std::make_unique<SCH_SCREEN>() );
94 BOOST_CHECK( libBuffer.GetSymbol( parentSymbol1Ref.GetName() ) == &parentSymbol1Ref );
95 BOOST_CHECK( !libBuffer.HasDerivedSymbols( parentSymbol1Ref.GetName() ) );
96 BOOST_CHECK_EQUAL( libBuffer.GetHash(), 2 );
97
98 libBuffer.GetSymbolNames( symbolNames );
99 BOOST_CHECK_EQUAL( symbolNames.GetCount(), 1 );
100 BOOST_CHECK_EQUAL( symbolNames[0], parentSymbol1Ref.GetName() );
101
102 symbolNames.Clear();
103 libBuffer.GetSymbolNames( symbolNames, SYMBOL_NAME_FILTER::ROOT_ONLY );
104 BOOST_CHECK_EQUAL( symbolNames.GetCount(), 1 );
105 BOOST_CHECK_EQUAL( symbolNames[0], parentSymbol1Ref.GetName() );
106
107 symbolNames.Clear();
108 libBuffer.GetSymbolNames( symbolNames, SYMBOL_NAME_FILTER::DERIVED_ONLY );
109 BOOST_CHECK_EQUAL( symbolNames.GetCount(), 0 );
110
111 auto childSymbol1 = std::make_unique<LIB_SYMBOL>( wxS( "Child1" ) );
112 LIB_SYMBOL& childSymbol1Ref = *childSymbol1;
113
114 childSymbol1->SetParent( &parentSymbol1Ref );
115 childSymbol1->GetValueField().SetText( childSymbol1->GetName() );
116 libBuffer.CreateBuffer( std::move( childSymbol1 ), std::make_unique<SCH_SCREEN>() );
117 BOOST_CHECK( libBuffer.GetSymbol( childSymbol1Ref.GetName() ) == &childSymbol1Ref );
118 BOOST_CHECK( libBuffer.HasDerivedSymbols( parentSymbol1Ref.GetName() ) );
119 BOOST_CHECK_EQUAL( libBuffer.GetHash(), 3 );
120
121 symbolNames.Clear();
122 libBuffer.GetSymbolNames( symbolNames );
123 BOOST_CHECK_EQUAL( symbolNames.GetCount(), 2 );
124 BOOST_CHECK_EQUAL( symbolNames[0], parentSymbol1Ref.GetName() );
125 BOOST_CHECK_EQUAL( symbolNames[1], childSymbol1Ref.GetName() );
126
127 symbolNames.Clear();
128 libBuffer.GetSymbolNames( symbolNames, SYMBOL_NAME_FILTER::DERIVED_ONLY );
129 BOOST_CHECK_EQUAL( symbolNames.GetCount(), 1 );
130 BOOST_CHECK_EQUAL( symbolNames[0], childSymbol1Ref.GetName() );
131
132 symbolNames.Clear();
133 BOOST_CHECK_EQUAL( libBuffer.GetDerivedSymbolNames( parentSymbol1Ref.GetName(), symbolNames ),
134 1 );
135 BOOST_CHECK_EQUAL( symbolNames[0], childSymbol1Ref.GetName() );
136
137 std::shared_ptr<SYMBOL_BUFFER> buf = libBuffer.GetBuffer( parentSymbol1Ref.GetName() );
138
139 LIB_SYMBOL tmp( parentSymbol1Ref );
140 tmp.GetDescriptionField().SetText( wxS( "Description" ) );
141
142 libBuffer.UpdateBuffer( *buf, tmp );
143 BOOST_CHECK_EQUAL( libBuffer.GetHash(), 4 );
144 BOOST_CHECK( *libBuffer.GetSymbol( parentSymbol1Ref.GetName() ) == tmp );
145
146 const bool deletedOk = libBuffer.DeleteBuffer( *buf );
147 BOOST_CHECK( deletedOk );
148 BOOST_CHECK( libBuffer.GetBuffers().empty() );
149}
150
151
void SetContentModified(bool aModified=true)
Definition: base_screen.h:59
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)
Update the buffered symbol with the contents of aCopy.
bool IsModified() const
bool DeleteBuffer(const SYMBOL_BUFFER &aSymbolBuf)
Delete the given symbol buffer from the library buffer.
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 > > & GetBuffers() const
std::shared_ptr< SYMBOL_BUFFER > GetBuffer(const wxString &aAlias) const
Return all buffered symbols.
LIB_SYMBOL * GetSymbol(const wxString &aAlias) const
Create a new buffer to store a symbol. LIB_BUFFER takes ownership of aCopy.
int GetHash() 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)
Define a library symbol object.
Definition: lib_symbol.h:78
void SetParent(LIB_SYMBOL *aParent=nullptr)
Definition: lib_symbol.cpp:295
wxString GetName() const override
Definition: lib_symbol.h:137
SCH_FIELD & GetDescriptionField() const
Return reference to the description field.
void SetText(const wxString &aText) override
Definition: sch_field.cpp:1212
Store a working copy of a library.
LIB_SYMBOL & GetSymbol() const
SCH_SCREEN * GetScreen() const
LIB_SYMBOL & GetOriginal() const
void SetOriginal(std::unique_ptr< LIB_SYMBOL > aSymbol)
BOOST_AUTO_TEST_SUITE_END()
BOOST_AUTO_TEST_CASE(SymbolBuffer)
Test the SYMBOL_BUFFER object.