29#include <boost/test/unit_test.hpp>
42#include <wx/filename.h>
43#include <wx/stdpaths.h>
52 wxString tempDir = wxStandardPaths::Get().GetTempDir();
53 wxString projectPath = tempDir + wxFileName::GetPathSeparator() + wxT(
"test_project.kicad_pro");
57 m_schematic = std::make_unique<SCHEMATIC>(
nullptr );
67 if( wxFileExists( file ) )
76 wxString tempDir = wxStandardPaths::Get().GetTempDir();
77 wxString fileName = wxFileName::CreateTempFileName( tempDir + wxFileName::GetPathSeparator() + aPrefix );
99 m_schematic->CreateDefaultScreens();
102 std::vector<SCH_SHEET*> topSheets = m_schematic->GetTopLevelSheets();
105 SCH_SCREEN* screen = topSheets[0]->GetScreen();
110 topSheets[0]->SetName(
"Main Sheet" );
114 BOOST_CHECK( !currentSheet.
empty() );
115 if( !currentSheet.
empty() )
117 BOOST_CHECK( currentSheet.
Last() != &m_schematic->Root() );
122 wxString fileName = GetTempFileName(
"test_schematic" );
123 fileName +=
".kicad_sch";
124 m_tempFiles.push_back( fileName );
127 BOOST_CHECK_NO_THROW( io.
SaveSchematicFile( fileName, topSheets[0], m_schematic.get() ) );
130 BOOST_CHECK( wxFileExists( fileName ) );
133 m_schematic->Reset();
134 SCH_SHEET* defaultSheet = m_schematic->GetTopLevelSheet( 0 );
136 BOOST_CHECK_NO_THROW( loadedSheet = io.
LoadSchematicFile( fileName, m_schematic.get() ) );
137 BOOST_CHECK( loadedSheet !=
nullptr );
142 m_schematic->AddTopLevelSheet( loadedSheet );
143 m_schematic->RemoveTopLevelSheet( defaultSheet );
147 BOOST_CHECK( m_schematic->IsValid() );
151 BOOST_CHECK( !currentSheet2.
empty() );
152 if( !currentSheet2.
empty() )
154 BOOST_CHECK( currentSheet2.
Last() != &m_schematic->Root() );
159 wxString fileName2 = GetTempFileName(
"test_schematic_resave" );
160 fileName2 +=
".kicad_sch";
161 m_tempFiles.push_back( fileName2 );
163 std::vector<SCH_SHEET*> topSheets2 = m_schematic->GetTopLevelSheets();
168 BOOST_CHECK_NO_THROW( io.
SaveSchematicFile( fileName2, topSheets2[0], m_schematic.get() ) );
171 BOOST_CHECK( wxFileExists( fileName2 ) );
182 m_schematic->CreateDefaultScreens();
184 std::vector<SCH_SHEET*> topSheets = m_schematic->GetTopLevelSheets();
187 SCH_SCREEN* screen = topSheets[0]->GetScreen();
192 subSheet->
SetName(
"SubSheet" );
197 screen->
Append( subSheet );
200 wxString mainFileName = GetTempFileName(
"test_main" );
201 mainFileName +=
".kicad_sch";
202 m_tempFiles.push_back( mainFileName );
205 wxString subFileName = GetTempFileName(
"test_sub" );
206 subFileName +=
".kicad_sch";
207 m_tempFiles.push_back( subFileName );
210 BOOST_CHECK_NO_THROW( io.
SaveSchematicFile( mainFileName, topSheets[0], m_schematic.get() ) );
211 BOOST_CHECK_NO_THROW( io.
SaveSchematicFile( subFileName, subSheet, m_schematic.get() ) );
214 m_schematic->Reset();
215 SCH_SHEET* defaultSheet = m_schematic->GetTopLevelSheet( 0 );
217 BOOST_CHECK_NO_THROW( loadedSheet = io.
LoadSchematicFile( mainFileName, m_schematic.get() ) );
218 BOOST_CHECK( loadedSheet !=
nullptr );
222 m_schematic->AddTopLevelSheet( loadedSheet );
223 m_schematic->RemoveTopLevelSheet( defaultSheet );
227 m_schematic->RefreshHierarchy();
234 wxString mainFileName2 = GetTempFileName(
"test_main_resave" );
235 mainFileName2 +=
".kicad_sch";
236 m_tempFiles.push_back( mainFileName2 );
238 std::vector<SCH_SHEET*> topSheets2 = m_schematic->GetTopLevelSheets();
242 BOOST_CHECK_NO_THROW( io.
SaveSchematicFile( mainFileName2, topSheets2[0], m_schematic.get() ) );
245 BOOST_CHECK( wxFileExists( mainFileName2 ) );
259 fn.AppendDir(
"legacy_hierarchy" );
260 fn.SetName(
"legacy_hierarchy" );
262 wxString mainFile = fn.GetFullPath();
271 BOOST_CHECK_NO_THROW( loadedSheet = io.
LoadSchematicFile( mainFile, m_schematic.get() ) );
277 m_schematic->Reset();
278 SCH_SHEET* defaultSheet = m_schematic->GetTopLevelSheet( 0 );
279 m_schematic->AddTopLevelSheet( loadedSheet );
280 m_schematic->RemoveTopLevelSheet( defaultSheet );
284 m_schematic->RefreshHierarchy();
290 BOOST_REQUIRE_EQUAL( hierarchy.size(), 3 );
293 for(
size_t i = 0; i < hierarchy.size(); i++ )
296 BOOST_TEST_MESSAGE(
"\nSheet path [" + std::to_string(i) +
"]: " +
path.PathHumanReadable(
false ).ToStdString() );
302 BOOST_CHECK(
path.LastScreen() !=
nullptr );
304 if(
path.LastScreen() )
321 std::map<wxString, int> referenceCount;
322 int totalInstances = 0;
327 if( !
path.LastScreen() )
345 if( symbolInstance.
m_Path.empty() )
349 " has EMPTY instance path!" );
355 " -> " + symbolInstance.
m_Reference.ToStdString() +
356 " (path: " + symbolInstance.
m_Path.
AsString().ToStdString() +
")" );
362 " has NO instance for path " +
path.Path().AsString().ToStdString() );
370 BOOST_TEST_MESSAGE(
" Unique references: " + std::to_string( referenceCount.size() ) );
376 BOOST_CHECK( totalInstances > 0 );
380 for(
const auto& pair : referenceCount )
382 BOOST_TEST_MESSAGE(
" " + pair.first.ToStdString() +
": " + std::to_string( pair.second ) );
387 wxString saveFile = GetTempFileName(
"legacy_hierarchy_resave" );
389 m_tempFiles.push_back( saveFile );
391 std::vector<SCH_SHEET*> topSheets = m_schematic->GetTopLevelSheets();
395 BOOST_CHECK_NO_THROW( io.
SaveSchematicFile( saveFile, topSheets[0], m_schematic.get() ) );
396 BOOST_CHECK( wxFileExists( saveFile ) );
410 m_schematic->CreateDefaultScreens();
412 std::vector<SCH_SHEET*> topSheets = m_schematic->GetTopLevelSheets();
421 subSheet->
SetName(
"SubSheet" );
425 topScreen->
Append( subSheet );
427 m_schematic->RefreshHierarchy();
431 wxString wksPath = GetTempFileName(
"embed_wks" );
432 wksPath +=
".kicad_wks";
433 m_tempFiles.push_back( wksPath );
436 wxFFile wksFile( wksPath,
"wb" );
438 const wxString wksBody = wxT(
"(kicad_wks)" );
442 BOOST_REQUIRE( m_schematic->GetEmbeddedFiles()->AddFile( wxFileName( wksPath ),
false )
444 BOOST_REQUIRE( !m_schematic->GetEmbeddedFiles()->IsEmpty() );
446 wxString topFileName = GetTempFileName(
"embed_main" );
447 topFileName +=
".kicad_sch";
448 m_tempFiles.push_back( topFileName );
451 BOOST_CHECK_NO_THROW( io.
SaveSchematicFile( topFileName, topSheet, m_schematic.get() ) );
454 wxFFile readback( topFileName,
"rb" );
456 BOOST_REQUIRE( readback.IsOpened() && readback.ReadAll( &contents ) );
458 const wxString embeddedName = wxFileName( wksPath ).GetFullName();
461 "Top-level sheet file is missing (embedded_files) block" );
463 "Top-level sheet file is missing the embedded worksheet entry" );
465 "Top-level sheet file is missing (type worksheet) marker" );
wxString AsString() const
wxString AsString() const
Container for project specific data.
A SCH_IO derivation for loading schematic files using the new s-expression file format.
void SaveSchematicFile(const wxString &aFileName, SCH_SHEET *aSheet, SCHEMATIC *aSchematic, const std::map< std::string, UTF8 > *aProperties=nullptr) override
Write aSchematic to a storage file in a format that this SCH_IO implementation knows about,...
SCH_SHEET * LoadSchematicFile(const wxString &aFileName, SCHEMATIC *aSchematic, SCH_SHEET *aAppendToMe=nullptr, const std::map< std::string, UTF8 > *aProperties=nullptr) override
Load information from some input file format that this SCH_IO implementation knows about,...
Base class for any item which can be embedded within the SCHEMATIC container class,...
void Append(SCH_ITEM *aItem, bool aUpdateLibSymbol=true)
void SetFileName(const wxString &aFileName)
Set the file name for this screen to aFileName.
A container for handling SCH_SHEET_PATH objects in a flattened hierarchy.
void CheckForMissingSymbolInstances(const wxString &aProjectName)
Handle access to a stack of flattened SCH_SHEET objects by way of a path for creating a flattened sch...
bool empty() const
Forwarded method from std::vector.
SCH_SHEET * Last() const
Return a pointer to the last SCH_SHEET of the list.
Sheet symbol placed in a schematic, and is the entry point for a sub schematic.
void SetFileName(const wxString &aFilename)
void SetName(const wxString &aName)
SCH_SCREEN * GetScreen() const
void SetScreen(SCH_SCREEN *aScreen)
Set the SCH_SCREEN associated with this sheet to aScreen.
bool GetInstance(SCH_SYMBOL_INSTANCE &aInstance, const KIID_PATH &aSheetPath, bool aTestFromEnd=false) const
static const std::string KiCadSchematicFileExtension
std::string GetEeschemaTestDataDir()
Get the configured location of Eeschema test data.
std::vector< wxString > m_tempFiles
wxString GetTempFileName(const wxString &aPrefix)
SETTINGS_MANAGER m_settingsManager
std::unique_ptr< SCHEMATIC > m_schematic
A simple container for schematic symbol instance information.
BOOST_AUTO_TEST_CASE(HorizontalAlignment)
BOOST_REQUIRE(intersection.has_value()==c.ExpectedIntersection.has_value())
BOOST_AUTO_TEST_SUITE_END()
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))
BOOST_AUTO_TEST_CASE(TestSaveLoadSimpleSchematic)
Test that we can save and reload a schematic with the virtual root pattern and that symbol instances ...
BOOST_CHECK_EQUAL(result, "25.4")