29#include <pegtl/contrib/analyze.hpp>
43 BOOST_REQUIRE( tao::pegtl::analyze< LIBRARY_TABLE_GRAMMAR::LIB_TABLE_FILE >( 1 ) == 0 );
50 tl::expected<LIBRARY_TABLE_IR, LIBRARY_PARSE_ERROR>
result = parser.
ParseBuffer(
"" );
57 std::vector<std::string> cases = {
63 p.append(
"libraries/" );
67 for(
const std::string&
path : cases )
84 wxString expected_error;
86 bool check_formatted =
true;
89 std::vector<TESTCASE> cases = {
90 { .filename =
"sym-lib-table", .expected_rows = 224 },
91 { .filename =
"fp-lib-table", .expected_rows = 146 },
92 { .filename =
"nested-symbols", .expected_rows = 6 },
93 { .filename =
"nested-disabled", .expected_rows = 4 },
94 { .filename =
"nested-hidden", .expected_rows = 4 },
95 { .filename =
"cycle", .expected_rows = 2 },
96 { .filename =
"sym-hand-edited", .expected_rows = 2, .check_formatted =
false },
97 { .filename =
"corrupted", .expected_error =
"Syntax error at line 6, column 9" },
98 { .filename =
"truncated", .expected_error =
"Syntax error at line 11, column 1" }
102 fn.AppendDir(
"libraries" );
104 for(
const auto& [filename, expected_error, expected_rows, check_formatted] : cases )
108 fn.SetName( filename );
113 BOOST_REQUIRE_MESSAGE(
table.Rows().size() == expected_rows,
114 wxString::Format(
"Expected %zu rows but got %zu",
115 expected_rows,
table.Rows().size() ) );
117 BOOST_REQUIRE_MESSAGE(
table.ErrorDescription() == expected_error,
118 wxString::Format(
"Expected error '%s' but got '%s'",
119 expected_error,
table.ErrorDescription() ) );
122 if( !
table.IsOk() || !check_formatted )
126 inFp.open( fn.GetFullPath().fn_str() );
129 std::stringstream inBuf;
130 inBuf << inFp.rdbuf();
131 std::string inData = inBuf.str();
134 table.Format( &formatter );
136 KICAD_FORMAT::FORMAT_MODE::LIBRARY_TABLE );
138 if( formatter.
GetString().compare( inData ) != 0 )
190 fn.AppendDir(
"libraries" );
193 fn.SetName(
"nested-disabled" );
196 BOOST_REQUIRE_MESSAGE( disabledTable.
Rows().size() == 4,
197 wxString::Format(
"Expected 4 rows but got %zu",
198 disabledTable.
Rows().size() ) );
201 bool foundDisabledRow =
false;
207 BOOST_REQUIRE_MESSAGE( row.Disabled(),
208 "Nested table row should have disabled flag set" );
209 foundDisabledRow =
true;
213 BOOST_REQUIRE_MESSAGE( foundDisabledRow,
214 "Disabled nested table row not found in parsed table" );
217 fn.SetName(
"nested-hidden" );
220 BOOST_REQUIRE_MESSAGE( hiddenTable.
Rows().size() == 4,
221 wxString::Format(
"Expected 4 rows but got %zu",
222 hiddenTable.
Rows().size() ) );
224 bool foundHiddenRow =
false;
230 BOOST_REQUIRE_MESSAGE( row.Hidden(),
231 "Nested table row should have hidden flag set" );
232 foundHiddenRow =
true;
236 BOOST_REQUIRE_MESSAGE( foundHiddenRow,
237 "Hidden nested table row not found in parsed table" );
256 std::vector<const LIBRARY_TABLE_ROW*> seededPointers;
257 std::vector<wxString> seededUris;
259 for(
int i = 0; i < 4; ++i )
262 row.
SetNickname( wxString::Format( wxS(
"seed_%d" ), i ) );
263 row.
SetURI( wxString::Format( wxS(
"${KIPRJMOD}/libs/seed_%d.kicad_sym" ), i ) );
266 seededPointers.push_back( &row );
267 seededUris.push_back( row.
URI() );
273 for(
int i = 0; i < 64; ++i )
276 row.
SetNickname( wxString::Format( wxS(
"extra_%d" ), i ) );
277 row.
SetURI( wxString::Format( wxS(
"${KIPRJMOD}/libs/extra_%d.kicad_sym" ), i ) );
280 for(
size_t j = 0; j < seededPointers.size(); ++j )
282 BOOST_REQUIRE_MESSAGE(
283 seededPointers[j]->URI() == seededUris[j],
285 wxS(
"Seed row %zu pointer was invalidated after inserting %d rows: "
286 "expected URI '%s', got '%s'" ),
287 j, i + 1, seededUris[j], seededPointers[j]->URI() ) );
311 bool expectedPcmManaged;
312 wxString description;
315 std::vector<CASE> cases = {
316 { wxS(
"${KICAD10_3RD_PARTY}/symbols/foo/foo.kicad_sym" ),
true,
317 wxS(
"Versioned 3RD_PARTY template should be recognised as PCM-managed" ) },
318 { wxS(
"${KICAD9_3RD_PARTY}/symbols/legacy/legacy.kicad_sym" ),
true,
319 wxS(
"Legacy versioned 3RD_PARTY template should still match the wildcard" ) },
320 { wxS(
"${KICAD10_3RD_PARTY}/footprints/bar/bar.pretty" ),
true,
321 wxS(
"Footprint library using 3RD_PARTY template should match" ) },
322 { wxS(
"${KICAD_USER_LIB}/symbols/test.kicad_sym" ),
false,
323 wxS(
"Row using a different env var must not be flagged as PCM-managed" ) },
324 { wxS(
"${KIPRJMOD}/libs/local.kicad_sym" ),
false,
325 wxS(
"Project-relative row must not be flagged as PCM-managed" ) },
326 { wxS(
"/abs/path/to/lib.kicad_sym" ),
false,
327 wxS(
"Absolute path row must not be flagged as PCM-managed" ) },
328 { wxS(
"${}" ),
false,
329 wxS(
"Malformed empty var name must not match" ) },
330 { wxS(
"${KICAD10_3RD_PARTY_EXTRA}/foo" ),
false,
331 wxS(
"Similar-but-different var name must not match" ) },
334 for(
const CASE& c : cases )
342 actual == c.expectedPcmManaged,
343 wxString::Format( wxS(
"%s: URI='%s' expected=%d actual=%d" ),
344 c.description, c.uri, c.expectedPcmManaged ? 1 : 0,
354 fn.AppendDir(
"libraries" );
355 fn.SetName(
"sym-lib-table" );
357 wxFileName tmpFn = wxFileName::CreateTempFileName(
"kicad_test_ro_" );
358 wxCopyFile( fn.GetFullPath(), tmpFn.GetFullPath() );
368 tmpFn.SetPermissions( wxS_IRUSR | wxS_IRGRP | wxS_IROTH );
374 if( wxFileName( tmpFn.GetFullPath() ).IsFileWritable() )
377 "read-only permissions (running as root?)" );
391 tmpFn.SetPermissions( wxS_IRUSR | wxS_IWUSR );
392 wxRemoveFile( tmpFn.GetFullPath() );
402 wxString tablePath = wxT(
"/some/read-only/path/sym-lib-table" );
403 wxString nickname1 = wxT(
"LibA" );
404 wxString nickname2 = wxT(
"LibB" );
std::map< wxString, std::map< wxString, LIB_OVERRIDE > > m_LibOverrides
Overrides for libraries in read-only nested tables.
void ClearLibOverride(const wxString &aTablePath, const wxString &aNickname)
Removes any override for a library that no longer needs one.
std::optional< LIBRARY_TABLE * > Table(LIBRARY_TABLE_TYPE aType, LIBRARY_TABLE_SCOPE aScope)
Retrieves a given table; creating a new empty project table if a valid project is loaded and the give...
void SetLibOverride(const wxString &aTablePath, const wxString &aNickname, bool aDisabled, bool aHidden)
Set a user override for a library in a read-only nested table.
static bool IsPcmManagedRow(const LIBRARY_TABLE_ROW &aRow)
Return true if a library table row was added by the Plugin and Content Manager.
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)
void SetNickname(const wxString &aNickname)
void SetType(const wxString &aType)
static const wxString TABLE_TYPE_NAME
void SetURI(const wxString &aUri)
const wxString & URI() const
LIBRARY_RESULT< void > Save()
bool IsReadOnly() const
Returns true if the underlying file exists but is not writable.
const std::deque< LIBRARY_TABLE_ROW > & Rows() const
virtual SETTINGS_MANAGER & GetSettingsManager() const
T * GetAppSettings(const char *aFilename)
Return a handle to the a given settings by type.
tl::expected< ResultType, LIBRARY_ERROR > LIBRARY_RESULT
std::string GetTestDataRootDir()
PGM_BASE & Pgm()
The global program "get" accessor.
BOOST_AUTO_TEST_CASE(HorizontalAlignment)
BOOST_AUTO_TEST_SUITE(CadstarPartParser)
BOOST_REQUIRE(intersection.has_value()==c.ExpectedIntersection.has_value())
BOOST_AUTO_TEST_SUITE_END()
BOOST_AUTO_TEST_CASE(Grammar)
BOOST_CHECK_MESSAGE(totalMismatches==0, std::to_string(totalMismatches)+" board(s) with strategy disagreements")
BOOST_TEST_MESSAGE("\n=== Real-World Polygon PIP Benchmark ===\n"<< formatTable(table))
std::vector< std::vector< std::string > > table
BOOST_TEST_CONTEXT("Test Clearance")
wxString result
Test unit parsing edge cases and error handling.