KiCad PCB EDA Suite
Loading...
Searching...
No Matches
test_library_tables.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 * @author Jon Evans <[email protected]>
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
21#include <filesystem>
22#include <fstream>
23
24#include <mock_pgm_base.h>
25#include <richio.h>
29#include <pegtl/contrib/analyze.hpp>
30
35
36
37BOOST_AUTO_TEST_SUITE( LibraryTables )
38
39
41{
42 BOOST_REQUIRE( tao::pegtl::analyze< LIBRARY_TABLE_GRAMMAR::LIB_TABLE_FILE >( 1 ) == 0 );
43}
44
45
47{
49 tl::expected<LIBRARY_TABLE_IR, LIBRARY_PARSE_ERROR> result = parser.ParseBuffer( "" );
50 BOOST_REQUIRE( !result.has_value() );
51}
52
53
54BOOST_AUTO_TEST_CASE( ParseFromFile )
55{
56 std::vector<std::string> cases = {
57 "sym-lib-table",
58 "fp-lib-table"
59 };
60
61 std::filesystem::path p( KI_TEST::GetTestDataRootDir() );
62 p.append( "libraries/" );
63
65
66 for( const std::string& path : cases )
67 {
68 p.remove_filename();
69 p.append( path );
70
71 auto result = parser.Parse( p );
72
73 BOOST_REQUIRE( result.has_value() );
74 }
75}
76
77
78BOOST_AUTO_TEST_CASE( ParseAndConstruct )
79{
80 struct TESTCASE
81 {
82 wxString filename;
83 wxString expected_error;
84 size_t expected_rows;
85 bool check_formatted = true;
86 };
87
88 std::vector<TESTCASE> cases = {
89 { .filename = "sym-lib-table", .expected_rows = 224 },
90 { .filename = "fp-lib-table", .expected_rows = 146 },
91 { .filename = "nested-symbols", .expected_rows = 6 },
92 { .filename = "cycle", .expected_rows = 2 },
93 { .filename = "sym-hand-edited", .expected_rows = 2, .check_formatted = false },
94 { .filename = "corrupted", .expected_error = "Syntax error at line 6, column 9" },
95 { .filename = "truncated", .expected_error = "Syntax error at line 11, column 1" }
96 };
97
98 wxFileName fn( KI_TEST::GetTestDataRootDir(), wxEmptyString );
99 fn.AppendDir( "libraries" );
100
101 for( const auto& [filename, expected_error, expected_rows, check_formatted] : cases )
102 {
103 BOOST_TEST_CONTEXT( filename )
104 {
105 fn.SetName( filename );
107
108 BOOST_REQUIRE( table.IsOk() == ( expected_error.IsEmpty() ) );
109
110 BOOST_REQUIRE_MESSAGE( table.Rows().size() == expected_rows,
111 wxString::Format( "Expected %zu rows but got %zu",
112 expected_rows, table.Rows().size() ) );
113
114 BOOST_REQUIRE_MESSAGE( table.ErrorDescription() == expected_error,
115 wxString::Format( "Expected error '%s' but got '%s'",
116 expected_error, table.ErrorDescription() ) );
117
118 // Non-parsed tables can't be formatted
119 if( !table.IsOk() || !check_formatted )
120 continue;
121
122 std::ifstream inFp;
123 inFp.open( fn.GetFullPath().fn_str() );
124 BOOST_REQUIRE( inFp.is_open() );
125
126 std::stringstream inBuf;
127 inBuf << inFp.rdbuf();
128 std::string inData = inBuf.str();
129
130 STRING_FORMATTER formatter;
131 table.Format( &formatter );
133 KICAD_FORMAT::FORMAT_MODE::LIBRARY_TABLE );
134
135 if( formatter.GetString().compare( inData ) != 0 )
136 {
137 BOOST_TEST_MESSAGE( "--- original ---" );
138 BOOST_TEST_MESSAGE( inData );
139 BOOST_TEST_MESSAGE( "--- formatted ---" );
140 BOOST_TEST_MESSAGE( formatter.GetString() );
141 }
142
143 BOOST_REQUIRE( formatter.GetString().compare( inData ) == 0 );
144 }
145 }
146}
147
149{
150 LIBRARY_MANAGER manager;
151 manager.LoadGlobalTables();
152
153 BOOST_REQUIRE( manager.Rows( LIBRARY_TABLE_TYPE::SYMBOL ).size() == 3 );
154 BOOST_REQUIRE( manager.Rows( LIBRARY_TABLE_TYPE::FOOTPRINT ).size() == 146 );
155}
156
std::vector< LIBRARY_TABLE_ROW * > Rows(LIBRARY_TABLE_TYPE aType, LIBRARY_TABLE_SCOPE aScope=LIBRARY_TABLE_SCOPE::BOTH, bool aIncludeInvalid=false) const
Returns a flattened list of libraries of the given type.
void LoadGlobalTables(std::initializer_list< LIBRARY_TABLE_TYPE > aTablesToLoad={})
(Re)loads the global library tables in the given list, or all tables if no list is given
tl::expected< LIBRARY_TABLE_IR, LIBRARY_PARSE_ERROR > ParseBuffer(const std::string &aBuffer)
tl::expected< LIBRARY_TABLE_IR, LIBRARY_PARSE_ERROR > Parse(const std::filesystem::path &aPath)
Implement an OUTPUTFORMATTER to a memory buffer.
Definition richio.h:422
std::string & MutableString()
Definition richio.h:450
const std::string & GetString()
Definition richio.h:445
void Prettify(std::string &aSource, FORMAT_MODE aMode)
Pretty-prints s-expression text according to KiCad format rules.
std::string GetTestDataRootDir()
BOOST_AUTO_TEST_CASE(HorizontalAlignment)
BOOST_AUTO_TEST_SUITE(CadstarPartParser)
BOOST_REQUIRE(intersection.has_value()==c.ExpectedIntersection.has_value())
BOOST_AUTO_TEST_SUITE_END()
std::string path
BOOST_AUTO_TEST_CASE(Grammar)
BOOST_TEST_CONTEXT("Test Clearance")
BOOST_TEST_MESSAGE("Polyline has "<< chain.PointCount()<< " points")
wxString result
Test unit parsing edge cases and error handling.