KiCad PCB EDA Suite
Loading...
Searching...
No Matches
test_design_block_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
20#include <filesystem>
21#include <fstream>
22#include <memory>
23#include <vector>
24
26
27#include <design_block.h>
28#include <design_block_io.h>
30#include <ki_exception.h>
33
34
35namespace
36{
37
38class TEST_DESIGN_BLOCK_LIBRARY_ADAPTER : public DESIGN_BLOCK_LIBRARY_ADAPTER
39{
40public:
42
43 void SeedLoadedLibrary( const wxString& aNickname, const wxString& aUri )
44 {
45 LIBRARY_TABLE_ROW* row = m_rows.emplace_back( std::make_unique<LIBRARY_TABLE_ROW>() ).get();
46 row->SetNickname( aNickname );
47 row->SetType( wxS( "KiCad" ) );
48 row->SetURI( aUri );
49
50 LIB_DATA& data = m_libraries[aNickname];
52 data.plugin = std::make_unique<DESIGN_BLOCK_IO>();
53 data.row = row;
54 }
55
56private:
57 std::vector<std::unique_ptr<LIBRARY_TABLE_ROW>> m_rows;
58};
59
60
61std::filesystem::path makeBlockLibrary( const std::string& aDirName, const std::string& aBlockName,
62 const std::string& aJson )
63{
64 std::error_code ec;
65 std::filesystem::path lib = std::filesystem::temp_directory_path( ec ) / aDirName;
66
67 std::filesystem::remove_all( lib, ec );
68
69 std::filesystem::path block = lib / ( aBlockName + ".kicad_block" );
70 std::filesystem::create_directories( block, ec );
71
72 std::ofstream json( block / ( aBlockName + ".json" ) );
73 json << aJson;
74
75 return lib;
76}
77
78
79struct SCOPED_LIBRARY
80{
81 explicit SCOPED_LIBRARY( const std::filesystem::path& aPath ) :
82 path( aPath )
83 {
84 }
85
86 ~SCOPED_LIBRARY()
87 {
88 std::error_code ec;
89 std::filesystem::remove_all( path, ec );
90 }
91
92 std::filesystem::path path;
93};
94
95} // namespace
96
97
98BOOST_AUTO_TEST_SUITE( DesignBlockIoMessages )
99
100
101BOOST_AUTO_TEST_CASE( LoadNamesTheBlockNotThePath )
102{
103 SCOPED_LIBRARY lib( makeBlockLibrary( "kicad_qa_db_absent_item", "Amp", "{}" ) );
104
106 wxString what;
107
108 try
109 {
110 std::unique_ptr<DESIGN_BLOCK> loaded( io.DesignBlockLoad( lib.path.string(), wxS( "Missing" ) ) );
111 BOOST_FAIL( "loading a design block that does not exist must throw" );
112 }
113 catch( const IO_ERROR& ioe )
114 {
115 what = ioe.What();
116 }
117
118 BOOST_CHECK_MESSAGE( what.Contains( wxS( "Missing" ) ) && !what.Contains( wxS( ".kicad_block" ) ),
119 "the message must name the design block, not the assembled path, got: " + what );
120}
121
122
124
125
126BOOST_AUTO_TEST_SUITE( DesignBlockLibraryAdapter )
127
128
129BOOST_AUTO_TEST_CASE( LoadReportsUnreadableMetadata )
130{
131 SCOPED_LIBRARY lib( makeBlockLibrary( "kicad_qa_db_bad_metadata", "Amp", "{ \"description\":" ) );
132
133 LIBRARY_MANAGER manager;
134 TEST_DESIGN_BLOCK_LIBRARY_ADAPTER adapter( manager );
135
136 adapter.SeedLoadedLibrary( wxS( "blocks" ), lib.path.string() );
137
138 wxString error;
139 std::unique_ptr<DESIGN_BLOCK> block( adapter.LoadDesignBlock( wxS( "blocks" ), wxS( "Amp" ), false, &error ) );
140
141 BOOST_REQUIRE_MESSAGE( !block, "a design block with unreadable metadata must not load" );
142 BOOST_CHECK_MESSAGE( error.Contains( wxS( "Amp.json" ) ), "the reason must name the metadata file, got: " + error );
143}
144
145
146BOOST_AUTO_TEST_CASE( LoadReportsLibraryNotInTable )
147{
148 LIBRARY_MANAGER manager;
149 TEST_DESIGN_BLOCK_LIBRARY_ADAPTER adapter( manager );
150
151 wxString error;
152 std::unique_ptr<DESIGN_BLOCK> block( adapter.LoadDesignBlock( wxS( "nope" ), wxS( "Amp" ), false, &error ) );
153
154 BOOST_REQUIRE( !block );
155 BOOST_CHECK_MESSAGE( error.Contains( wxS( "nope" ) ), "the reason must name the missing library, got: " + error );
156}
157
158
DESIGN_BLOCK * DesignBlockLoad(const wxString &aLibraryPath, const wxString &aDesignBlockName, bool aKeepUUID=false, const std::map< std::string, UTF8 > *aProperties=nullptr)
DESIGN_BLOCK_LIBRARY_ADAPTER(LIBRARY_MANAGER &aManager)
Hold an error message and may be used when throwing exceptions containing meaningful error messages.
virtual const wxString What() const
A composite of Problem() and Where()
void SetNickname(const wxString &aNickname)
void SetType(const wxString &aType)
void SetURI(const wxString &aUri)
nlohmann::json json
Definition gerbview.cpp:50
LIB_STATUS status
std::unique_ptr< IO_BASE > plugin
const LIBRARY_TABLE_ROW * row
LOAD_STATUS load_status
BOOST_AUTO_TEST_CASE(HorizontalAlignment)
BOOST_AUTO_TEST_SUITE(CadstarPartParser)
BOOST_AUTO_TEST_CASE(LoadNamesTheBlockNotThePath)
BOOST_REQUIRE(intersection.has_value()==c.ExpectedIntersection.has_value())
BOOST_AUTO_TEST_SUITE_END()