← Back to team overview

kicad-developers team mailing list archive

[RFC] [PATCH] simple C++ tests

 

Hi,

This patch adds possibility to build C++ tests for pcbnew, using
Boost.Test framework. I intended it primarily to make tests for the P&S,
which I used to develop out-of-tree before.

Since Kicad's codebase is rather monolithic, testing/refactoring
anything drags in pretty much everything else in the code as a
dependency.  This causes long rebuild and linking times, which I find
very annoying. Also, while developing new features, most of the time is
spent linking pcbnew and clicking in the GUI to trigger the particular
feature.

For that reason I decided to link all the test cases directly to the
_pcbnew_kiface library and skip pcbnew's dependency scanning when
building tests. I'm also preparing a bare PCB test window for verifying
interactive/graphical operations without rebuilding pcbnew as a whole app.

I've attached the patch (set the KICAD_BUILD_TESTS=ON option to enable).
There's one trivial unit test included as an example. Let me know if
such solution would be acceptable.

Cheers,
Tom

>From fb308460da32cb7a30e66f0c114ac3a0a159ef60 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Tomasz=20W=C5=82ostowski?= <tomasz.wlostowski@xxxxxxx>
Date: Wed, 7 Sep 2016 17:53:20 +0200
Subject: [PATCH] Initial support for unit tests in pcbnew using Boost.Test

---
 CMakeLists.txt           | 34 +++++++++++++++++++++++-----------
 pcbnew/CMakeLists.txt    | 12 ++++++++++++
 tests/CMakeLists.txt     | 10 ++++++++++
 tests/pns/CMakeLists.txt |  3 +++
 tests/pns/test_node.cpp  | 21 +++++++++++++++++++++
 5 files changed, 69 insertions(+), 11 deletions(-)
 create mode 100644 tests/CMakeLists.txt
 create mode 100644 tests/pns/CMakeLists.txt
 create mode 100644 tests/pns/test_node.cpp

diff --git a/CMakeLists.txt b/CMakeLists.txt
index bfeaac5..0dbd735 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -49,6 +49,9 @@ option( KICAD_USE_OCE
     "Build tools and plugins related to OpenCascade Community Edition (default OFF)"
     )
 
+option( KICAD_BUILD_TESTS
+    "Builds C++ tests (default OFF)"
+    )
 # when option KICAD_SCRIPTING OR KICAD_SCRIPTING_MODULES is enabled:
 # PYTHON_EXECUTABLE can be defined when invoking cmake
 # ( use -DPYTHON_EXECUTABLE=<python path>/python.exe or python2 )
@@ -69,10 +72,14 @@ option( KICAD_SPICE "Build Kicad with internal Spice simulator." OFF )
 set( KICAD_REPO_NAME "product" CACHE STRING "Name of the tree from which this build came." )
 
 
-# Global setting: exports are explicit
-set( CMAKE_CXX_VISIBILITY_PRESET "hidden" )
-set( CMAKE_VISIBILITY_INLINES_HIDDEN ON )
-
+# Global setting: exports are explicit by default
+if ( KICAD_BUILD_TESTS )
+    set( CMAKE_CXX_VISIBILITY_PRESET "default" )
+    set( CMAKE_VISIBILITY_INLINES_HIDDEN OFF )
+else()
+    set( CMAKE_CXX_VISIBILITY_PRESET "hidden" )
+    set( CMAKE_VISIBILITY_INLINES_HIDDEN ON )
+endif()
 
 # Global setting: build everything position independent
 set( CMAKE_POSITION_INDEPENDENT_CODE ON )
@@ -93,12 +100,14 @@ if( POLICY CMP0063 )
         cmake_policy( SET CMP0063 NEW )
     endif()
 else()
-    if( CMAKE_CXX_COMPILE_OPTIONS_VISIBILITY AND NOT APPLE )
-        set( CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${CMAKE_CXX_COMPILE_OPTIONS_VISIBILITY}hidden" )
-    endif()
-    if( CMAKE_CXX_COMPILE_OPTIONS_VISIBILITY_INLINES_HIDDEN AND NOT APPLE )
-        set( CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${CMAKE_CXX_COMPILE_OPTIONS_VISIBILITY_INLINES_HIDDEN}" )
-    endif()
+    if( CMAKE_CXX_VISIBILITY_PRESET STREQUAL "hidden" )
+		if( CMAKE_CXX_COMPILE_OPTIONS_VISIBILITY AND NOT APPLE )
+    	    set( CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${CMAKE_CXX_COMPILE_OPTIONS_VISIBILITY}hidden" )
+		endif()
+		if( CMAKE_CXX_COMPILE_OPTIONS_VISIBILITY_INLINES_HIDDEN AND NOT APPLE )
+    		set( CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${CMAKE_CXX_COMPILE_OPTIONS_VISIBILITY_INLINES_HIDDEN}" )
+		endif()
+	endif()
 endif()
 
 
@@ -503,7 +512,7 @@ find_package( Cairo 1.8.8 REQUIRED )
 #
 # Note: Prior to Boost 1.59, the Boost context library is not built when compiling on windows
 #       with GCC.  You must patch the Boost sources.
-find_package( Boost 1.54.0 REQUIRED COMPONENTS context system thread )
+find_package( Boost 1.54.0 REQUIRED COMPONENTS context system thread unit_test_framework )
 
 # Include MinGW resource compiler.
 include( MinGWResourceCompiler )
@@ -804,6 +813,9 @@ add_subdirectory( kicad )               # should follow pcbnew, eeschema
 add_subdirectory( tools )
 add_subdirectory( utils )
 add_subdirectory( qa )
+if ( KICAD_BUILD_TESTS )
+    add_subdirectory( tests )
+endif()
 
 # Resources
 add_subdirectory( demos )
diff --git a/pcbnew/CMakeLists.txt b/pcbnew/CMakeLists.txt
index c4aff51..5ce31a0 100644
--- a/pcbnew/CMakeLists.txt
+++ b/pcbnew/CMakeLists.txt
@@ -578,6 +578,18 @@ set_target_properties( pcbnew_kiface PROPERTIES
     SUFFIX          ${KIFACE_SUFFIX}
     )
 
+if ( KICAD_BUILD_TESTS )
+    if ( UNIX )
+	add_custom_command(TARGET pcbnew_kiface POST_BUILD
+	 COMMAND ln -sf _pcbnew.kiface lib_pcbnew_kiface.so )
+    else()
+	add_custom_command(TARGET pcbnew_kiface POST_BUILD
+	 COMMAND copy _pcbnew.kiface lib_pcbnew_kiface.dll )
+    endif()
+
+
+endif ()
+
 if( ${OPENMP_FOUND} )
     set_target_properties( pcbnew_kiface PROPERTIES
         COMPILE_FLAGS   ${OpenMP_CXX_FLAGS}
diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt
new file mode 100644
index 0000000..e8e59ad
--- /dev/null
+++ b/tests/CMakeLists.txt
@@ -0,0 +1,10 @@
+include_directories ( .. ../polygon ../include ../pcbnew ../pcbnew/router )
+link_directories ( ../pcbnew )
+
+function( add_test_case name )
+    add_executable ( ${name} ${name}.cpp )
+    target_link_libraries ( ${name} ${wxWidgets_LIBRARIES} _pcbnew_kiface ${Boost_LIBRARIES} )
+endfunction( add_test_case )
+
+add_subdirectory ( pns )
+
diff --git a/tests/pns/CMakeLists.txt b/tests/pns/CMakeLists.txt
new file mode 100644
index 0000000..b305e1e
--- /dev/null
+++ b/tests/pns/CMakeLists.txt
@@ -0,0 +1,3 @@
+add_test_case( test_node )
+
+
diff --git a/tests/pns/test_node.cpp b/tests/pns/test_node.cpp
new file mode 100644
index 0000000..eea11c1
--- /dev/null
+++ b/tests/pns/test_node.cpp
@@ -0,0 +1,21 @@
+#define BOOST_TEST_DYN_LINK
+#define BOOST_TEST_MODULE PNS_NODE
+
+#include <boost/test/unit_test.hpp>
+
+#include "pns_item.h"
+#include "pns_node.h"
+
+using namespace PNS;
+using namespace std;
+
+BOOST_AUTO_TEST_CASE( PnsNode )
+{
+    unique_ptr<NODE> node ( new NODE );
+    BOOST_REQUIRE( node->JointCount() == 0 );
+
+    unique_ptr<SEGMENT> segment( new SEGMENT ( SEG ( VECTOR2I (0, 0), VECTOR2I( 1000, 1000) ), 0 ) );
+    node->Add( move ( segment ) );
+
+    BOOST_REQUIRE( node->JointCount() == 2 );
+}
-- 
1.9.1


Follow ups