From 81bac449d35db52c2b07ce09813e44f3f395b8c7 Mon Sep 17 00:00:00 2001
From: John Beard <john.j.beard@gmail.com>
Date: Wed, 12 Dec 2018 16:49:17 +0000
Subject: [PATCH] QA: Add unit tests on the Pcbnew library

This now allows code under /pcbnew to be tested.

A first test of the GRAPHICS_IMPORT_MGR is added.
---
 qa/CMakeLists.txt                      |   1 +
 qa/pcbnew/CMakeLists.txt               |  92 +++++++++++++++++++
 qa/pcbnew/test_graphics_import_mgr.cpp | 120 +++++++++++++++++++++++++
 qa/pcbnew/test_module.cpp              |  48 ++++++++++
 4 files changed, 261 insertions(+)
 create mode 100644 qa/pcbnew/CMakeLists.txt
 create mode 100644 qa/pcbnew/test_graphics_import_mgr.cpp
 create mode 100644 qa/pcbnew/test_module.cpp

diff --git a/qa/CMakeLists.txt b/qa/CMakeLists.txt
index b3acef7130..2118f0e9ed 100644
--- a/qa/CMakeLists.txt
+++ b/qa/CMakeLists.txt
@@ -24,6 +24,7 @@ add_subdirectory( shape_poly_set_refactor )
 add_subdirectory( common_tools )
 add_subdirectory( pcb_parse_input )
 
+add_subdirectory( pcbnew )
 # add_subdirectory( pcb_test_window )
 # add_subdirectory( polygon_triangulation )
 # add_subdirectory( polygon_generator )
\ No newline at end of file
diff --git a/qa/pcbnew/CMakeLists.txt b/qa/pcbnew/CMakeLists.txt
new file mode 100644
index 0000000000..eecdb17677
--- /dev/null
+++ b/qa/pcbnew/CMakeLists.txt
@@ -0,0 +1,92 @@
+# This program source code file is part of KiCad, a free EDA CAD application.
+#
+# Copyright (C) 2018 KiCad Developers, see CHANGELOG.TXT for contributors.
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, you may find one here:
+# http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
+# or you may search the http://www.gnu.org website for the version 2 license,
+# or you may write to the Free Software Foundation, Inc.,
+# 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
+
+
+add_executable( qa_pcbnew
+    # A single top to load the pcnew kiface
+    # ../../common/single_top.cpp
+
+    # The main test entry points
+    test_module.cpp
+
+    # stuff from common due to...units?
+    ../../common/eda_text.cpp
+
+    # stuff from common which is needed...why?
+    ../../common/colors.cpp
+    ../../common/observable.cpp
+
+    test_graphics_import_mgr.cpp
+)
+
+if( BUILD_GITHUB_PLUGIN )
+    set( GITHUB_PLUGIN_LIBRARIES github_plugin )
+endif()
+
+set_source_files_properties( ../common/single_top.cpp pcbnew.cpp PROPERTIES
+    COMPILE_DEFINITIONS "TOP_FRAME=FRAME_PCB;PGM_DATA_FILE_EXT=\"kicad_pcb\";BUILD_KIWAY_DLL"
+)
+
+include_directories( BEFORE ${INC_BEFORE} )
+include_directories(
+    ${CMAKE_SOURCE_DIR}
+    ${CMAKE_SOURCE_DIR}/include
+    ${CMAKE_SOURCE_DIR}/polygon
+    ${CMAKE_SOURCE_DIR}/pcbnew
+    ${CMAKE_SOURCE_DIR}/common
+    ${CMAKE_SOURCE_DIR}/pcbnew/router
+    ${CMAKE_SOURCE_DIR}/pcbnew/tools
+    ${CMAKE_SOURCE_DIR}/pcbnew/dialogs
+    ${INC_AFTER}
+)
+
+target_link_libraries( qa_pcbnew
+    3d-viewer
+    connectivity
+    pcbcommon
+    pnsrouter
+    pcad2kicadpcb
+    common
+    legacy_wx
+    polygon
+    bitmaps
+    gal
+    qa_utils
+    lib_dxf
+    idf3
+    pcbnew_kiface_objects
+    unit_test_utils
+    ${wxWidgets_LIBRARIES}
+    ${GITHUB_PLUGIN_LIBRARIES}
+    ${GDI_PLUS_LIBRARIES}
+    ${PYTHON_LIBRARIES}
+    ${Boost_LIBRARIES}      # must follow GITHUB
+    ${PCBNEW_EXTRA_LIBS}    # -lrt must follow Boost
+)
+
+# we need to pretend to be something to appease the units code
+target_compile_definitions( qa_pcbnew
+    PRIVATE PCBNEW
+)
+
+add_test( NAME pcbnew
+    COMMAND qa_pcbnew
+)
\ No newline at end of file
diff --git a/qa/pcbnew/test_graphics_import_mgr.cpp b/qa/pcbnew/test_graphics_import_mgr.cpp
new file mode 100644
index 0000000000..ac6891cae2
--- /dev/null
+++ b/qa/pcbnew/test_graphics_import_mgr.cpp
@@ -0,0 +1,120 @@
+/*
+ * This program source code file is part of KiCad, a free EDA CAD application.
+ *
+ * Copyright (C) 2018 KiCad Developers, see CHANGELOG.TXT for contributors.
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, you may find one here:
+ * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
+ * or you may search the http://www.gnu.org website for the version 2 license,
+ * or you may write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
+ */
+
+#include <boost/test/test_case_template.hpp>
+#include <boost/test/unit_test.hpp>
+
+#include <unit_test_utils/unit_test_utils.h>
+
+#include <import_gfx/graphics_import_mgr.h>
+#include <import_gfx/graphics_import_plugin.h>
+
+/**
+ * Declares a struct as the Boost test fixture.
+ */
+BOOST_AUTO_TEST_SUITE( GraphicsImportMgr )
+
+static bool pluginHandlesExt( const GRAPHICS_IMPORT_PLUGIN& aPlugin, const std::string& aExt )
+{
+    const auto exts = aPlugin.GetFileExtensions();
+
+    return std::find( exts.begin(), exts.end(), wxString( aExt ) ) != exts.end();
+}
+
+struct TYPE_TO_EXTS
+{
+    // The type of the plugin
+    GRAPHICS_IMPORT_MGR::GFX_FILE_T m_type;
+
+    /// The list of extensions we expect this plugin to handle
+    std::vector<std::string> m_exts;
+
+    /// The name of the plugin
+    std::string m_name;
+};
+
+const static std::vector<TYPE_TO_EXTS> type_to_ext_cases = {
+    {
+            GRAPHICS_IMPORT_MGR::GFX_FILE_T::DXF,
+            { "dxf" },
+            "AutoCAD DXF",
+    },
+    {
+            GRAPHICS_IMPORT_MGR::GFX_FILE_T::SVG,
+            { "svg" },
+            "Scalable Vector Graphics",
+    },
+};
+
+/**
+ * Check we can look a plugin up by type and get the right one
+ */
+BOOST_AUTO_TEST_CASE( SelectByType )
+{
+    GRAPHICS_IMPORT_MGR mgr( {} );
+
+    for( const auto& c : type_to_ext_cases )
+    {
+        auto plugin = mgr.GetPlugin( c.m_type );
+
+        BOOST_CHECK( !!plugin );
+
+        if( plugin )
+        {
+            for( const auto& ext : c.m_exts )
+            {
+                BOOST_CHECK_MESSAGE( pluginHandlesExt( *plugin, ext ),
+                        "Plugin '" << plugin->GetName() << "' handles extension: " << ext );
+            }
+        }
+    }
+}
+
+/**
+ * Check we can look a plugin up by ext and get the right one
+ */
+BOOST_AUTO_TEST_CASE( SelectByExt )
+{
+    GRAPHICS_IMPORT_MGR mgr( {} );
+
+    for( const auto& c : type_to_ext_cases )
+    {
+        for( const auto& ext : c.m_exts )
+        {
+            auto plugin = mgr.GetPluginByExt( wxString( ext ) );
+
+            BOOST_CHECK( !!plugin );
+
+            if( plugin )
+            {
+                // This is an ugly way to check the right plugin,
+                // as we have to keep a list of expected strings (the plugins
+                // don't report any kind of other unique identifier).
+                // But it's quick and dirty and it's good enough!
+                BOOST_CHECK_EQUAL( c.m_name, plugin->GetName() );
+            }
+        }
+    }
+}
+
+BOOST_AUTO_TEST_SUITE_END()
diff --git a/qa/pcbnew/test_module.cpp b/qa/pcbnew/test_module.cpp
new file mode 100644
index 0000000000..5f593f6b27
--- /dev/null
+++ b/qa/pcbnew/test_module.cpp
@@ -0,0 +1,48 @@
+/*
+ * This program source code file is part of KiCad, a free EDA CAD application.
+ *
+ * Copyright (C) 2018 KiCad Developers, see CHANGELOG.TXT for contributors.
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, you may find one here:
+ * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
+ * or you may search the http://www.gnu.org website for the version 2 license,
+ * or you may write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
+ */
+
+/**
+ * Main file for the pcbnew tests to be compiled
+ */
+#include <boost/test/unit_test.hpp>
+
+#include <wx/init.h>
+
+
+bool init_unit_test()
+{
+    boost::unit_test::framework::master_test_suite().p_name.value = "Common Pcbnew module tests";
+    return wxInitialize();
+}
+
+
+int main( int argc, char* argv[] )
+{
+    int ret = boost::unit_test::unit_test_main( &init_unit_test, argc, argv );
+
+    // This causes some glib warnings on GTK3 (http://trac.wxwidgets.org/ticket/18274)
+    // but without it, Valgrind notices a lot of leaks from WX
+    wxUninitialize();
+
+    return ret;
+}
\ No newline at end of file