KiCad PCB EDA Suite
Loading...
Searching...
No Matches
test_symbol_library_adapter.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
17 * along with this program. If not, see <https://www.gnu.org/licenses/>.
18 */
19
21
24
25#include <wx/textfile.h>
26
27#include <filesystem>
28
29
30namespace
31{
32
38class TEST_SYMBOL_LIBRARY_ADAPTER : public SYMBOL_LIBRARY_ADAPTER
39{
40public:
42
43 void SeedLoadError( const wxString& aNickname )
44 {
45 m_libraries[aNickname].status.load_status = LOAD_STATUS::LOAD_ERROR;
46 }
47
49 void SeedLoaded( const wxString& aNickname )
50 {
51 m_libraries[aNickname].status.load_status = LOAD_STATUS::LOADED;
52 }
53};
54
55
57class SCOPED_SYM_LIB_TABLE
58{
59public:
60 SCOPED_SYM_LIB_TABLE( const std::vector<wxString>& aNicknamesInTableOrder )
61 {
62 std::error_code ec;
63 m_dir = std::filesystem::temp_directory_path( ec )
64 / std::filesystem::path( wxString::Format( wxS( "kicad_qa_symlibtbl_%p" ), (void*) this )
65 .ToStdString() );
66 std::filesystem::create_directories( m_dir, ec );
67
68 wxTextFile file( wxString( ( m_dir / "sym-lib-table" ).string() ) );
69 file.Create();
70 file.AddLine( wxS( "(sym_lib_table" ) );
71 file.AddLine( wxS( " (version 7)" ) );
72
73 for( const wxString& nickname : aNicknamesInTableOrder )
74 {
75 file.AddLine( wxString::Format(
76 wxS( " (lib (name \"%s\")(type \"KiCad\")(uri \"${KIPRJMOD}/%s.kicad_sym\")(options \"\")(descr \"\"))" ),
77 nickname, nickname ) );
78 }
79
80 file.AddLine( wxS( ")" ) );
81 file.Write();
82 file.Close();
83 }
84
85 ~SCOPED_SYM_LIB_TABLE()
86 {
87 std::error_code ec;
88 std::filesystem::remove_all( m_dir, ec );
89 }
90
91 wxString Path() const { return wxString( m_dir.string() ); }
92
93private:
94 std::filesystem::path m_dir;
95};
96
97} // namespace
98
99
100BOOST_AUTO_TEST_SUITE( SymbolLibraryAdapter )
101
102
103
111BOOST_AUTO_TEST_CASE( IsSymbolLibWritableHandlesFailedLoad )
112{
113 LIBRARY_MANAGER manager;
114 TEST_SYMBOL_LIBRARY_ADAPTER adapter( manager );
115
116 adapter.SeedLoadError( wxS( "BadLib" ) );
117
118 BOOST_CHECK_EQUAL( adapter.IsSymbolLibWritable( wxS( "BadLib" ) ), false );
119
120 // A library that was never even attempted must also be safe.
121 BOOST_CHECK_EQUAL( adapter.IsSymbolLibWritable( wxS( "NeverSeen" ) ), false );
122}
123
124
133BOOST_AUTO_TEST_CASE( GetLibraryNamesSortedRegardlessOfTableOrder )
134{
135 const std::vector<wxString> tableOrder = { wxS( "Zebra" ), wxS( "amplifier" ), wxS( "Device" ),
136 wxS( "Board_2" ), wxS( "Board_10" ) };
137
138 SCOPED_SYM_LIB_TABLE tableFile( tableOrder );
139
140 LIBRARY_MANAGER manager;
141 manager.LoadProjectTables( tableFile.Path(), { LIBRARY_TABLE_TYPE::SYMBOL } );
142
143 TEST_SYMBOL_LIBRARY_ADAPTER adapter( manager );
144
145 for( const wxString& nickname : tableOrder )
146 adapter.SeedLoaded( nickname );
147
148 const std::vector<wxString> names = adapter.GetLibraryNames();
149
150 const std::vector<wxString> expected = { wxS( "amplifier" ), wxS( "Board_2" ), wxS( "Board_10" ),
151 wxS( "Device" ), wxS( "Zebra" ) };
152
153 BOOST_CHECK_EQUAL_COLLECTIONS( names.begin(), names.end(), expected.begin(), expected.end() );
154}
155
156
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
An interface to the global shared library manager that is schematic-specific and linked to one projec...
SYMBOL_LIBRARY_ADAPTER(LIBRARY_MANAGER &aManager)
BOOST_AUTO_TEST_CASE(HorizontalAlignment)
BOOST_AUTO_TEST_SUITE(CadstarPartParser)
BOOST_AUTO_TEST_SUITE_END()
VECTOR3I expected(15, 30, 45)
BOOST_AUTO_TEST_CASE(IsSymbolLibWritableHandlesFailedLoad)
Regression test for a null-plugin dereference crash.
BOOST_CHECK_EQUAL(result, "25.4")