29#include <boost/test/unit_test.hpp>
41#include <wx/filename.h>
42#include <wx/stdpaths.h>
51 wxString tempDir = wxStandardPaths::Get().GetTempDir();
52 wxString projectPath = tempDir + wxFileName::GetPathSeparator() + wxT(
"test_project.kicad_pro");
56 m_schematic = std::make_unique<SCHEMATIC>(
nullptr );
66 if( wxFileExists( file ) )
75 wxString tempDir = wxStandardPaths::Get().GetTempDir();
76 wxString fileName = wxFileName::CreateTempFileName( tempDir + wxFileName::GetPathSeparator() + aPrefix );
98 m_schematic->CreateDefaultScreens();
101 std::vector<SCH_SHEET*> topSheets = m_schematic->GetTopLevelSheets();
104 SCH_SCREEN* screen = topSheets[0]->GetScreen();
109 topSheets[0]->SetName(
"Main Sheet" );
113 BOOST_CHECK( !currentSheet.
empty() );
114 if( !currentSheet.
empty() )
116 BOOST_CHECK( currentSheet.
Last() != &m_schematic->Root() );
121 wxString fileName = GetTempFileName(
"test_schematic" );
122 fileName +=
".kicad_sch";
123 m_tempFiles.push_back( fileName );
126 BOOST_CHECK_NO_THROW( io.
SaveSchematicFile( fileName, topSheets[0], m_schematic.get() ) );
129 BOOST_CHECK( wxFileExists( fileName ) );
132 m_schematic->Reset();
133 SCH_SHEET* defaultSheet = m_schematic->GetTopLevelSheet( 0 );
135 BOOST_CHECK_NO_THROW( loadedSheet = io.
LoadSchematicFile( fileName, m_schematic.get() ) );
136 BOOST_CHECK( loadedSheet !=
nullptr );
141 m_schematic->AddTopLevelSheet( loadedSheet );
142 m_schematic->RemoveTopLevelSheet( defaultSheet );
146 BOOST_CHECK( m_schematic->IsValid() );
150 BOOST_CHECK( !currentSheet2.
empty() );
151 if( !currentSheet2.
empty() )
153 BOOST_CHECK( currentSheet2.
Last() != &m_schematic->Root() );
158 wxString fileName2 = GetTempFileName(
"test_schematic_resave" );
159 fileName2 +=
".kicad_sch";
160 m_tempFiles.push_back( fileName2 );
162 std::vector<SCH_SHEET*> topSheets2 = m_schematic->GetTopLevelSheets();
167 BOOST_CHECK_NO_THROW( io.
SaveSchematicFile( fileName2, topSheets2[0], m_schematic.get() ) );
170 BOOST_CHECK( wxFileExists( fileName2 ) );
181 m_schematic->CreateDefaultScreens();
183 std::vector<SCH_SHEET*> topSheets = m_schematic->GetTopLevelSheets();
186 SCH_SCREEN* screen = topSheets[0]->GetScreen();
191 subSheet->
SetName(
"SubSheet" );
196 screen->
Append( subSheet );
199 wxString mainFileName = GetTempFileName(
"test_main" );
200 mainFileName +=
".kicad_sch";
201 m_tempFiles.push_back( mainFileName );
204 wxString subFileName = GetTempFileName(
"test_sub" );
205 subFileName +=
".kicad_sch";
206 m_tempFiles.push_back( subFileName );
209 BOOST_CHECK_NO_THROW( io.
SaveSchematicFile( mainFileName, topSheets[0], m_schematic.get() ) );
210 BOOST_CHECK_NO_THROW( io.
SaveSchematicFile( subFileName, subSheet, m_schematic.get() ) );
213 m_schematic->Reset();
214 SCH_SHEET* defaultSheet = m_schematic->GetTopLevelSheet( 0 );
216 BOOST_CHECK_NO_THROW( loadedSheet = io.
LoadSchematicFile( mainFileName, m_schematic.get() ) );
217 BOOST_CHECK( loadedSheet !=
nullptr );
221 m_schematic->AddTopLevelSheet( loadedSheet );
222 m_schematic->RemoveTopLevelSheet( defaultSheet );
226 m_schematic->RefreshHierarchy();
233 wxString mainFileName2 = GetTempFileName(
"test_main_resave" );
234 mainFileName2 +=
".kicad_sch";
235 m_tempFiles.push_back( mainFileName2 );
237 std::vector<SCH_SHEET*> topSheets2 = m_schematic->GetTopLevelSheets();
241 BOOST_CHECK_NO_THROW( io.
SaveSchematicFile( mainFileName2, topSheets2[0], m_schematic.get() ) );
244 BOOST_CHECK( wxFileExists( mainFileName2 ) );
258 fn.AppendDir(
"legacy_hierarchy" );
259 fn.SetName(
"legacy_hierarchy" );
261 wxString mainFile = fn.GetFullPath();
270 BOOST_CHECK_NO_THROW( loadedSheet = io.
LoadSchematicFile( mainFile, m_schematic.get() ) );
276 m_schematic->Reset();
277 SCH_SHEET* defaultSheet = m_schematic->GetTopLevelSheet( 0 );
278 m_schematic->AddTopLevelSheet( loadedSheet );
279 m_schematic->RemoveTopLevelSheet( defaultSheet );
283 m_schematic->RefreshHierarchy();
289 BOOST_REQUIRE_EQUAL( hierarchy.size(), 3 );
292 for(
size_t i = 0; i < hierarchy.size(); i++ )
295 BOOST_TEST_MESSAGE(
"\nSheet path [" + std::to_string(i) +
"]: " +
path.PathHumanReadable(
false ).ToStdString() );
301 BOOST_CHECK(
path.LastScreen() !=
nullptr );
303 if(
path.LastScreen() )
320 std::map<wxString, int> referenceCount;
321 int totalInstances = 0;
326 if( !
path.LastScreen() )
344 if( symbolInstance.
m_Path.empty() )
348 " has EMPTY instance path!" );
354 " -> " + symbolInstance.
m_Reference.ToStdString() +
355 " (path: " + symbolInstance.
m_Path.
AsString().ToStdString() +
")" );
361 " has NO instance for path " +
path.Path().AsString().ToStdString() );
369 BOOST_TEST_MESSAGE(
" Unique references: " + std::to_string( referenceCount.size() ) );
375 BOOST_CHECK( totalInstances > 0 );
379 for(
const auto& pair : referenceCount )
381 BOOST_TEST_MESSAGE(
" " + pair.first.ToStdString() +
": " + std::to_string( pair.second ) );
386 wxString saveFile = GetTempFileName(
"legacy_hierarchy_resave" );
388 m_tempFiles.push_back( saveFile );
390 std::vector<SCH_SHEET*> topSheets = m_schematic->GetTopLevelSheets();
394 BOOST_CHECK_NO_THROW( io.
SaveSchematicFile( saveFile, topSheets[0], m_schematic.get() ) );
395 BOOST_CHECK( wxFileExists( saveFile ) );
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)
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_AUTO_TEST_CASE(TestSaveLoadSimpleSchematic)
Test that we can save and reload a schematic with the virtual root pattern and that symbol instances ...
BOOST_TEST_MESSAGE("Polyline has "<< chain.PointCount()<< " points")
BOOST_CHECK_EQUAL(result, "25.4")