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.

127 lines
3.5 KiB

  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2018 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 <qa_utils/wx_utils/unit_test_utils.h>
  24. #include <import_gfx/graphics_import_mgr.h>
  25. #include <import_gfx/graphics_import_plugin.h>
  26. #include <regex>
  27. /**
  28. * Declares a struct as the Boost test fixture.
  29. */
  30. BOOST_AUTO_TEST_SUITE( GraphicsImportMgr )
  31. static bool pluginHandlesExt( const GRAPHICS_IMPORT_PLUGIN& aPlugin, const std::string& aExt )
  32. {
  33. const auto exts = aPlugin.GetFileExtensions();
  34. for( const auto& ext : exts )
  35. {
  36. std::regex ext_reg( ext );
  37. if( std::regex_match( aExt, ext_reg ) )
  38. return true;
  39. }
  40. return false;
  41. }
  42. struct TYPE_TO_EXTS
  43. {
  44. // The type of the plugin
  45. GRAPHICS_IMPORT_MGR::GFX_FILE_T m_type;
  46. /// The list of extensions we expect this plugin to handle
  47. std::vector<std::string> m_exts;
  48. /// The name of the plugin
  49. std::string m_name;
  50. };
  51. const static std::vector<TYPE_TO_EXTS> type_to_ext_cases = {
  52. {
  53. GRAPHICS_IMPORT_MGR::GFX_FILE_T::DXF,
  54. { "dxf" },
  55. "AutoCAD DXF",
  56. },
  57. {
  58. GRAPHICS_IMPORT_MGR::GFX_FILE_T::SVG,
  59. { "svg" },
  60. "Scalable Vector Graphics",
  61. },
  62. };
  63. /**
  64. * Check we can look a plugin up by type and get the right one
  65. */
  66. BOOST_AUTO_TEST_CASE( SelectByType )
  67. {
  68. GRAPHICS_IMPORT_MGR mgr( {} );
  69. for( const auto& c : type_to_ext_cases )
  70. {
  71. auto plugin = mgr.GetPlugin( c.m_type );
  72. BOOST_CHECK( !!plugin );
  73. if( plugin )
  74. {
  75. for( const auto& ext : c.m_exts )
  76. {
  77. BOOST_CHECK_MESSAGE( pluginHandlesExt( *plugin, ext ),
  78. "Plugin '" << plugin->GetName() << "' handles extension: " << ext );
  79. }
  80. }
  81. }
  82. }
  83. /**
  84. * Check we can look a plugin up by ext and get the right one
  85. */
  86. BOOST_AUTO_TEST_CASE( SelectByExt )
  87. {
  88. GRAPHICS_IMPORT_MGR mgr( {} );
  89. for( const auto& c : type_to_ext_cases )
  90. {
  91. for( const auto& ext : c.m_exts )
  92. {
  93. auto plugin = mgr.GetPluginByExt( wxString( ext ) );
  94. BOOST_CHECK( !!plugin );
  95. if( plugin )
  96. {
  97. // This is an ugly way to check the right plugin,
  98. // as we have to keep a list of expected strings (the plugins
  99. // don't report any kind of other unique identifier).
  100. // But it's quick and dirty and it's good enough!
  101. BOOST_CHECK_EQUAL( c.m_name, plugin->GetName() );
  102. }
  103. }
  104. }
  105. }
  106. BOOST_AUTO_TEST_SUITE_END()