You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

157 lines
5.1 KiB

  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2019-2023 KiCad Developers, see AUTHORS.TXT for contributors.
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version 2
  9. * of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, you may find one here:
  18. * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  19. * or you may search the http://www.gnu.org website for the version 2 license,
  20. * or you may write to the Free Software Foundation, Inc.,
  21. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  22. */
  23. #include "eeschema_test_utils.h"
  24. #include <qa_utils/wx_utils/unit_test_utils.h>
  25. #include <wildcards_and_files_ext.h>
  26. #include <cstdlib>
  27. #include <memory>
  28. #include <eeschema/sch_io_mgr.h>
  29. #include <eeschema/sch_screen.h>
  30. #include <eeschema/schematic.h>
  31. #include <eeschema/connection_graph.h>
  32. void KI_TEST::SCHEMATIC_TEST_FIXTURE::LoadSchematic( const wxString& aBaseName )
  33. {
  34. wxFileName fn = GetSchematicPath( aBaseName );
  35. BOOST_TEST_MESSAGE( fn.GetFullPath() );
  36. wxFileName pro( fn );
  37. pro.SetExt( ProjectFileExtension );
  38. // Schematic must be reset before a project is reloaded
  39. m_schematic.Reset();
  40. m_manager.LoadProject( pro.GetFullPath() );
  41. m_manager.Prj().SetElem( PROJECT::ELEM_SCH_SYMBOL_LIBS, nullptr );
  42. m_schematic.SetProject( &m_manager.Prj() );
  43. m_schematic.SetRoot( m_pi->Load( fn.GetFullPath(), &m_schematic ) );
  44. BOOST_REQUIRE_EQUAL( m_pi->GetError().IsEmpty(), true );
  45. m_schematic.CurrentSheet().push_back( &m_schematic.Root() );
  46. SCH_SCREENS screens( m_schematic.Root() );
  47. for( SCH_SCREEN* screen = screens.GetFirst(); screen; screen = screens.GetNext() )
  48. screen->UpdateLocalLibSymbolLinks();
  49. SCH_SHEET_LIST sheets = m_schematic.GetSheets();
  50. // Restore all of the loaded symbol instances from the root sheet screen.
  51. if( m_schematic.RootScreen()->GetFileFormatVersionAtLoad() < 20221002 )
  52. sheets.UpdateSymbolInstanceData( m_schematic.RootScreen()->GetSymbolInstances());
  53. if( m_schematic.RootScreen()->GetFileFormatVersionAtLoad() < 20221110 )
  54. sheets.UpdateSheetInstanceData( m_schematic.RootScreen()->GetSheetInstances());
  55. if( m_schematic.RootScreen()->GetFileFormatVersionAtLoad() < 20221206 )
  56. {
  57. for( SCH_SCREEN* screen = screens.GetFirst(); screen; screen = screens.GetNext() )
  58. screen->MigrateSimModels();
  59. }
  60. if( m_schematic.RootScreen()->GetFileFormatVersionAtLoad() < 20230221 )
  61. screens.FixLegacyPowerSymbolMismatches();
  62. sheets.AnnotatePowerSymbols();
  63. // NOTE: This is required for multi-unit symbols to be correct
  64. for( SCH_SHEET_PATH& sheet : sheets )
  65. sheet.UpdateAllScreenReferences();
  66. // NOTE: SchematicCleanUp is not called; QA schematics must already be clean or else
  67. // SchematicCleanUp must be freed from its UI dependencies.
  68. m_schematic.ConnectionGraph()->Recalculate( sheets, true );
  69. }
  70. wxFileName KI_TEST::SCHEMATIC_TEST_FIXTURE::GetSchematicPath( const wxString& aBaseName )
  71. {
  72. wxFileName fn( KI_TEST::GetEeschemaTestDataDir() );
  73. fn.AppendDir( "netlists" );
  74. fn.AppendDir( aBaseName );
  75. fn.SetName( aBaseName );
  76. fn.SetExt( KiCadSchematicFileExtension );
  77. return fn;
  78. }
  79. template <typename Exporter>
  80. wxString TEST_NETLIST_EXPORTER_FIXTURE<Exporter>::GetNetlistPath( bool aTest )
  81. {
  82. wxFileName netFile = m_schematic.Prj().GetProjectFullName();
  83. if( aTest )
  84. netFile.SetName( netFile.GetName() + "_test" );
  85. netFile.SetExt( NetlistFileExtension );
  86. return netFile.GetFullPath();
  87. }
  88. template <typename Exporter>
  89. void TEST_NETLIST_EXPORTER_FIXTURE<Exporter>::WriteNetlist()
  90. {
  91. // In case of a crash the file may not have been deleted.
  92. if( wxFileExists( GetNetlistPath( true ) ) )
  93. wxRemoveFile( GetNetlistPath( true ) );
  94. wxString errors;
  95. WX_STRING_REPORTER reporter( &errors );
  96. std::unique_ptr<Exporter> exporter = std::make_unique<Exporter>( &m_schematic );
  97. bool success = exporter->WriteNetlist( GetNetlistPath( true ), GetNetlistOptions(), reporter );
  98. BOOST_REQUIRE( success && errors.IsEmpty() );
  99. }
  100. template <typename Exporter>
  101. void TEST_NETLIST_EXPORTER_FIXTURE<Exporter>::Cleanup()
  102. {
  103. wxRemoveFile( GetNetlistPath( true ) );
  104. m_schematic.Reset();
  105. }
  106. template <typename Exporter>
  107. void TEST_NETLIST_EXPORTER_FIXTURE<Exporter>::TestNetlist( const wxString& aBaseName )
  108. {
  109. LoadSchematic( aBaseName );
  110. WriteNetlist();
  111. CompareNetlists();
  112. Cleanup();
  113. }
  114. template class TEST_NETLIST_EXPORTER_FIXTURE<NETLIST_EXPORTER_KICAD>;
  115. template class TEST_NETLIST_EXPORTER_FIXTURE<NETLIST_EXPORTER_SPICE>;