From 1db822906867f11fec615bfe3943a56bcb8a35fe Mon Sep 17 00:00:00 2001 From: Roberto Fernandez Bautista Date: Mon, 11 Apr 2022 21:49:26 +0100 Subject: [PATCH] qa_eeschema refactor: Create generic SCHEMATIC_TEST_FIXTURE --- qa/unittests/eeschema/eeschema_test_utils.cpp | 52 +++++++++--- qa/unittests/eeschema/eeschema_test_utils.h | 36 +++++++- qa/unittests/eeschema/test_netlists.cpp | 83 +------------------ qa/unittests/eeschema/test_sch_sheet_list.cpp | 80 ++---------------- 4 files changed, 82 insertions(+), 169 deletions(-) diff --git a/qa/unittests/eeschema/eeschema_test_utils.cpp b/qa/unittests/eeschema/eeschema_test_utils.cpp index 6955abbf37..f9173ea271 100644 --- a/qa/unittests/eeschema/eeschema_test_utils.cpp +++ b/qa/unittests/eeschema/eeschema_test_utils.cpp @@ -30,6 +30,8 @@ #include #include #include +#include +#include #ifndef QA_EESCHEMA_DATA_LOCATION @@ -60,24 +62,39 @@ wxFileName KI_TEST::GetEeschemaTestDataDir() } -std::unique_ptr ReadSchematicFromFile( const std::string& aFilename ) +void KI_TEST::SCHEMATIC_TEST_FIXTURE::loadSchematic( const wxString& aRelativePath ) { - auto pi = SCH_IO_MGR::FindPlugin( SCH_IO_MGR::SCH_KICAD ); - std::unique_ptr schematic = std::make_unique( nullptr ); + wxFileName fn = getSchematicFile( aRelativePath ); - schematic->Reset(); - schematic->SetRoot( pi->Load( aFilename, schematic.get() ) ); - schematic->CurrentSheet().push_back( &schematic->Root() ); + BOOST_TEST_MESSAGE( fn.GetFullPath() ); - SCH_SCREENS screens( schematic->Root() ); + wxFileName pro( fn ); + pro.SetExt( ProjectFileExtension ); + + m_schematic.Reset(); + m_schematic.CurrentSheet().clear(); + + m_manager.LoadProject( pro.GetFullPath() ); + m_manager.Prj().SetElem( PROJECT::ELEM_SCH_SYMBOL_LIBS, nullptr ); + + m_schematic.SetProject( &m_manager.Prj() ); + + m_schematic.SetRoot( m_pi->Load( fn.GetFullPath(), &m_schematic ) ); + + BOOST_REQUIRE_EQUAL( m_pi->GetError().IsEmpty(), true ); + + m_schematic.CurrentSheet().push_back( &m_schematic.Root() ); + + SCH_SCREENS screens( m_schematic.Root() ); for( SCH_SCREEN* screen = screens.GetFirst(); screen; screen = screens.GetNext() ) screen->UpdateLocalLibSymbolLinks(); - SCH_SHEET_LIST sheets = schematic->GetSheets(); + SCH_SHEET_LIST sheets = m_schematic.GetSheets(); // Restore all of the loaded symbol instances from the root sheet screen. - sheets.UpdateSymbolInstances( schematic->RootScreen()->GetSymbolInstances() ); + sheets.UpdateSymbolInstances( m_schematic.RootScreen()->GetSymbolInstances() ); + sheets.UpdateSheetInstances( m_schematic.RootScreen()->GetSheetInstances() ); sheets.AnnotatePowerSymbols(); @@ -89,7 +106,18 @@ std::unique_ptr ReadSchematicFromFile( const std::string& aFilename ) // NOTE: SchematicCleanUp is not called; QA schematics must already be clean or else // SchematicCleanUp must be freed from its UI dependencies. - schematic->ConnectionGraph()->Recalculate( sheets, true ); - - return schematic; + m_schematic.ConnectionGraph()->Recalculate( sheets, true ); } + + +wxFileName KI_TEST::SCHEMATIC_TEST_FIXTURE::getSchematicFile( const wxString& aBaseName ) +{ + wxFileName fn = KI_TEST::GetEeschemaTestDataDir(); + fn.AppendDir( "netlists" ); + fn.AppendDir( aBaseName ); + fn.SetName( aBaseName ); + fn.SetExt( KiCadSchematicFileExtension ); + + return fn; +} + diff --git a/qa/unittests/eeschema/eeschema_test_utils.h b/qa/unittests/eeschema/eeschema_test_utils.h index 8fe2249353..ea40d0f871 100644 --- a/qa/unittests/eeschema/eeschema_test_utils.h +++ b/qa/unittests/eeschema/eeschema_test_utils.h @@ -24,11 +24,14 @@ #ifndef QA_EESCHEMA_EESCHEMA_TEST_UTILS__H #define QA_EESCHEMA_EESCHEMA_TEST_UTILS__H + +#include +#include +#include #include namespace KI_TEST { - /** * Get the configured location of Eeschema test data. * @@ -39,6 +42,37 @@ namespace KI_TEST */ wxFileName GetEeschemaTestDataDir(); +/** + * A generic fixture for loading schematics and associated settings for qa tests + */ +class SCHEMATIC_TEST_FIXTURE +{ +public: + SCHEMATIC_TEST_FIXTURE() : m_schematic( nullptr ), m_manager( true ) + { + m_pi = SCH_IO_MGR::FindPlugin( SCH_IO_MGR::SCH_KICAD ); + } + + virtual ~SCHEMATIC_TEST_FIXTURE() + { + m_schematic.Reset(); + delete m_pi; + } + +protected: + void loadSchematic( const wxString& aRelativePath ); + + virtual wxFileName getSchematicFile( const wxString& aBaseName ); + + ///> Schematic to load + SCHEMATIC m_schematic; + + SCH_PLUGIN* m_pi; + + SETTINGS_MANAGER m_manager; +}; + + } // namespace KI_TEST #endif // QA_EESCHEMA_EESCHEMA_TEST_UTILS__H \ No newline at end of file diff --git a/qa/unittests/eeschema/test_netlists.cpp b/qa/unittests/eeschema/test_netlists.cpp index ff3ab3a1e8..f70e08ec9b 100644 --- a/qa/unittests/eeschema/test_netlists.cpp +++ b/qa/unittests/eeschema/test_netlists.cpp @@ -32,24 +32,9 @@ #include -class TEST_NETLISTS_FIXTURE +class TEST_NETLISTS_FIXTURE : public KI_TEST::SCHEMATIC_TEST_FIXTURE { -public: - TEST_NETLISTS_FIXTURE() : - m_schematic( nullptr ), - m_manager( true ) - { - m_pi = SCH_IO_MGR::FindPlugin( SCH_IO_MGR::SCH_KICAD ); - } - - virtual ~TEST_NETLISTS_FIXTURE() - { - m_schematic.Reset(); - SCH_IO_MGR::ReleasePlugin( m_pi ); - } - - void loadSchematic( const wxString& aBaseName ); - +protected: wxString getNetlistFileName( bool aTest = false ); void writeNetlist(); @@ -59,73 +44,9 @@ public: void cleanup(); void doNetlistTest( const wxString& aBaseName ); - - ///> Schematic to load - SCHEMATIC m_schematic; - - SCH_PLUGIN* m_pi; - - SETTINGS_MANAGER m_manager; }; -static wxString getSchematicFile( const wxString& aBaseName ) -{ - wxFileName fn = KI_TEST::GetEeschemaTestDataDir(); - fn.AppendDir( "netlists" ); - fn.AppendDir( aBaseName ); - fn.SetName( aBaseName ); - fn.SetExt( KiCadSchematicFileExtension ); - - return fn.GetFullPath(); -} - - -void TEST_NETLISTS_FIXTURE::loadSchematic( const wxString& aBaseName ) -{ - wxString fn = getSchematicFile( aBaseName ); - - BOOST_TEST_MESSAGE( fn ); - - wxFileName pro( fn ); - pro.SetExt( ProjectFileExtension ); - - m_manager.LoadProject( pro.GetFullPath() ); - - m_manager.Prj().SetElem( PROJECT::ELEM_SCH_SYMBOL_LIBS, nullptr ); - - m_schematic.Reset(); - m_schematic.SetProject( &m_manager.Prj() ); - m_schematic.SetRoot( m_pi->Load( fn, &m_schematic ) ); - - BOOST_REQUIRE_EQUAL( m_pi->GetError().IsEmpty(), true ); - - m_schematic.CurrentSheet().push_back( &m_schematic.Root() ); - - SCH_SCREENS screens( m_schematic.Root() ); - - for( SCH_SCREEN* screen = screens.GetFirst(); screen; screen = screens.GetNext() ) - screen->UpdateLocalLibSymbolLinks(); - - SCH_SHEET_LIST sheets = m_schematic.GetSheets(); - - // Restore all of the loaded symbol instances from the root sheet screen. - sheets.UpdateSymbolInstances( m_schematic.RootScreen()->GetSymbolInstances() ); - - sheets.AnnotatePowerSymbols(); - - // NOTE: This is required for multi-unit symbols to be correct - // Normally called from SCH_EDIT_FRAME::FixupJunctions() but could be refactored - for( SCH_SHEET_PATH& sheet : sheets ) - sheet.UpdateAllScreenReferences(); - - // NOTE: SchematicCleanUp is not called; QA schematics must already be clean or else - // SchematicCleanUp must be freed from its UI dependencies. - - m_schematic.ConnectionGraph()->Recalculate( sheets, true ); -} - - wxString TEST_NETLISTS_FIXTURE::getNetlistFileName( bool aTest ) { wxFileName netFile = m_schematic.Prj().GetProjectFullName(); diff --git a/qa/unittests/eeschema/test_sch_sheet_list.cpp b/qa/unittests/eeschema/test_sch_sheet_list.cpp index e306a98677..05123386fb 100644 --- a/qa/unittests/eeschema/test_sch_sheet_list.cpp +++ b/qa/unittests/eeschema/test_sch_sheet_list.cpp @@ -20,87 +20,17 @@ #include #include "eeschema_test_utils.h" -#include -#include #include -#include -#include #include - -class TEST_SCH_SHEET_LIST_FIXTURE +class TEST_SCH_SHEET_LIST_FIXTURE : public KI_TEST::SCHEMATIC_TEST_FIXTURE { -public: - TEST_SCH_SHEET_LIST_FIXTURE() : - m_schematic( nullptr ), - m_manager( true ) - { - m_pi = SCH_IO_MGR::FindPlugin( SCH_IO_MGR::SCH_KICAD ); - } - - virtual ~TEST_SCH_SHEET_LIST_FIXTURE() - { - m_schematic.Reset(); - delete m_pi; - } - - void loadSchematic( const wxString& aRelativePath ); - - wxFileName buildFullPath( const wxString& aRelativePath ); - - ///> Schematic to load - SCHEMATIC m_schematic; - - SCH_PLUGIN* m_pi; - - SETTINGS_MANAGER m_manager; +protected: + wxFileName getSchematicFile( const wxString& aRelativePath ) override; }; -void TEST_SCH_SHEET_LIST_FIXTURE::loadSchematic( const wxString& aRelativePath ) -{ - wxFileName fn = buildFullPath( aRelativePath ); - - BOOST_TEST_MESSAGE( fn.GetFullPath() ); - - wxFileName pro( fn ); - pro.SetExt( ProjectFileExtension ); - - m_schematic.Reset(); - m_schematic.CurrentSheet().clear(); - - m_manager.LoadProject( pro.GetFullPath() ); - m_manager.Prj().SetElem( PROJECT::ELEM_SCH_SYMBOL_LIBS, nullptr ); - - m_schematic.SetProject( &m_manager.Prj() ); - - m_schematic.SetRoot( m_pi->Load( fn.GetFullPath(), &m_schematic ) ); - - BOOST_REQUIRE_EQUAL( m_pi->GetError().IsEmpty(), true ); - - m_schematic.CurrentSheet().push_back( &m_schematic.Root() ); - - SCH_SCREENS screens( m_schematic.Root() ); - - for( SCH_SCREEN* screen = screens.GetFirst(); screen; screen = screens.GetNext() ) - screen->UpdateLocalLibSymbolLinks(); - - SCH_SHEET_LIST sheets = m_schematic.GetSheets(); - - // Restore all of the loaded symbol instances from the root sheet screen. - sheets.UpdateSymbolInstances( m_schematic.RootScreen()->GetSymbolInstances() ); - sheets.UpdateSheetInstances( m_schematic.RootScreen()->GetSheetInstances() ); - - sheets.AnnotatePowerSymbols(); - - // NOTE: This is required for multi-unit symbols to be correct - // Normally called from SCH_EDIT_FRAME::FixupJunctions() but could be refactored - for( SCH_SHEET_PATH& sheet : sheets ) - sheet.UpdateAllScreenReferences(); -} - - -wxFileName TEST_SCH_SHEET_LIST_FIXTURE::buildFullPath( const wxString& aRelativePath ) +wxFileName TEST_SCH_SHEET_LIST_FIXTURE::getSchematicFile( const wxString& aRelativePath ) { wxFileName fn = KI_TEST::GetEeschemaTestDataDir(); fn.AppendDir( "netlists" ); @@ -174,7 +104,7 @@ BOOST_AUTO_TEST_CASE( TestEditPageNumbersInSharedDesign ) // Save and reload wxString tempName = "complex_hierarchy_shared/complex_hierarchy_modified"; - wxFileName tempFn = buildFullPath( tempName ); + wxFileName tempFn = getSchematicFile( tempName ); m_pi->Save( tempFn.GetFullPath(), &m_schematic.Root(), &m_schematic ); loadSchematic( tempName );