From c1408f130cdcf11cef1093e14f363a76aaccb055 Mon Sep 17 00:00:00 2001 From: Chris Hennes Date: Fri, 26 Feb 2021 18:55:55 -0600 Subject: [PATCH] [OpenSCAD] Add unit test framework to module The OpenSCAD module did not have any unit tests. This commit adds the basic framework (and two very simple tests). The framework is modeled after the unit tests in the FEM module. --- src/Mod/OpenSCAD/CMakeLists.txt | 46 ++++++++++++ src/Mod/OpenSCAD/Init.py | 2 + src/Mod/OpenSCAD/InitGui.py | 5 ++ src/Mod/OpenSCAD/OpenSCADTest/__init__.py | 0 src/Mod/OpenSCAD/OpenSCADTest/app/__init__.py | 0 .../OpenSCADTest/app/test_importCSG.py | 72 +++++++++++++++++++ src/Mod/OpenSCAD/OpenSCADTest/data/CSG.csg | 18 +++++ src/Mod/OpenSCAD/OpenSCADTest/data/CSG.scad | 32 +++++++++ .../OpenSCAD/OpenSCADTest/data/__init__.py | 0 src/Mod/OpenSCAD/OpenSCADTest/gui/__init__.py | 0 .../OpenSCAD/OpenSCADTest/gui/test_dummy.py | 38 ++++++++++ .../OpenSCAD/OpenSCADTest/test_information.md | 3 + src/Mod/OpenSCAD/TestOpenSCADApp.py | 28 ++++++++ src/Mod/OpenSCAD/TestOpenSCADGui.py | 29 ++++++++ 14 files changed, 273 insertions(+) create mode 100644 src/Mod/OpenSCAD/OpenSCADTest/__init__.py create mode 100644 src/Mod/OpenSCAD/OpenSCADTest/app/__init__.py create mode 100644 src/Mod/OpenSCAD/OpenSCADTest/app/test_importCSG.py create mode 100644 src/Mod/OpenSCAD/OpenSCADTest/data/CSG.csg create mode 100644 src/Mod/OpenSCAD/OpenSCADTest/data/CSG.scad create mode 100644 src/Mod/OpenSCAD/OpenSCADTest/data/__init__.py create mode 100644 src/Mod/OpenSCAD/OpenSCADTest/gui/__init__.py create mode 100644 src/Mod/OpenSCAD/OpenSCADTest/gui/test_dummy.py create mode 100644 src/Mod/OpenSCAD/OpenSCADTest/test_information.md create mode 100644 src/Mod/OpenSCAD/TestOpenSCADApp.py create mode 100644 src/Mod/OpenSCAD/TestOpenSCADGui.py diff --git a/src/Mod/OpenSCAD/CMakeLists.txt b/src/Mod/OpenSCAD/CMakeLists.txt index b7a3873d56..f77a9d599b 100644 --- a/src/Mod/OpenSCAD/CMakeLists.txt +++ b/src/Mod/OpenSCAD/CMakeLists.txt @@ -15,9 +15,45 @@ SET(OpenSCAD_SRCS colorcodeshapes.py expandplacements.py replaceobj.py + TestOpenSCADApp.py ) +IF (BUILD_GUI) + LIST(APPEND OpenSCAD_SRCS TestOpenSCADGui.py) +ENDIF (BUILD_GUI) SOURCE_GROUP("" FILES ${OpenSCAD_SRCS}) +SET(OpenSCADTests_SRCS + OpenSCADTest/__init__.py + OpenSCADTest/test_information.md +) + +SET(OpenSCADTestsApp_SRCS + OpenSCADTest/app/__init__.py + OpenSCADTest/app/test_importCSG.py +) + +SET(OpenSCADTestsGui_SRCS + OpenSCADTest/gui/__init__.py + OpenSCADTest/gui/test_dummy.py +) + +SET(OpenSCADTestsFiles_SRCS + OpenSCADTest/data/__init__.py + OpenSCADTest/data/CSG.scad + OpenSCADTest/data/CSG.csg +) + +SET(OpenSCADTests_ALL + ${OpenSCADTests_SRCS} + ${OpenSCADTestsApp_SRCS} + ${OpenSCADTestsFiles_SRCS} + ) + +IF (BUILD_GUI) + LIST(APPEND OpenSCADTests_ALL ${OpenSCADTestsGui_SRCS}) +ENDIF (BUILD_GUI) + + set(all_files ${OpenSCAD_SRCS}) SET(OpenSCADGuiIcon_SVG @@ -28,10 +64,16 @@ ADD_CUSTOM_TARGET(OpenSCAD ALL SOURCES ${all_files} ${OpenSCAD_QRC_SRCS} ${OpenSCADGuiIcon_SVG} ) +ADD_CUSTOM_TARGET(OpenSCADTests ALL + SOURCES ${OpenSCADTests_ALL} +) + fc_copy_sources(OpenSCAD "${CMAKE_BINARY_DIR}/Mod/OpenSCAD" ${all_files}) fc_copy_sources(OpenSCAD "${CMAKE_BINARY_DIR}/${CMAKE_INSTALL_DATADIR}/Mod/OpenSCAD" ${OpenSCADGuiIcon_SVG}) +fc_copy_sources(OpenSCADTests "${CMAKE_BINARY_DIR}/Mod/OpenSCAD" ${OpenSCADTests_ALL}) + IF (BUILD_GUI) fc_target_copy_resource(OpenSCAD ${CMAKE_CURRENT_BINARY_DIR} @@ -59,3 +101,7 @@ INSTALL( "${CMAKE_INSTALL_DATADIR}/Mod/OpenSCAD/Resources/icons" ) +INSTALL(FILES ${OpenSCADTests_SRCS} DESTINATION Mod/OpenSCAD/OpenSCADTest) +INSTALL(FILES ${OpenSCADTestsApp_SRCS} DESTINATION Mod/OpenSCAD/OpenSCADTest/app) +INSTALL(FILES ${OpenSCADTestsGui_SRCS} DESTINATION Mod/OpenSCAD/OpenSCADTest/gui) +INSTALL(FILES ${OpenSCADTestsFiles_SRCS} DESTINATION Mod/OpenSCAD/OpenSCADTest/data) diff --git a/src/Mod/OpenSCAD/Init.py b/src/Mod/OpenSCAD/Init.py index 9f3d6d0df2..6d1c27ded9 100644 --- a/src/Mod/OpenSCAD/Init.py +++ b/src/Mod/OpenSCAD/Init.py @@ -32,6 +32,8 @@ openscadfilename = param.GetString('openscadexecutable') openscadbin = openscadfilename and os.path.isfile(openscadfilename) if openscadbin: FreeCAD.addImportType("OpenSCAD Format (*.scad)","importCSG") + FreeCAD.__unit_test__ += ["TestOpenSCADApp"] + FreeCAD.addExportType("OpenSCAD CSG Format (*.csg)","exportCSG") FreeCAD.addExportType("OpenSCAD Format (*.scad)","exportCSG") diff --git a/src/Mod/OpenSCAD/InitGui.py b/src/Mod/OpenSCAD/InitGui.py index 0883071d19..0ce25ccad9 100644 --- a/src/Mod/OpenSCAD/InitGui.py +++ b/src/Mod/OpenSCAD/InitGui.py @@ -29,6 +29,7 @@ #***************************************************************************/ import FreeCAD +import OpenSCADUtils param = FreeCAD.ParamGet("User parameter:BaseApp/Preferences/Mod/OpenSCAD") openscadfilename = param.GetString('openscadexecutable') @@ -91,3 +92,7 @@ class OpenSCADWorkbench ( Workbench ): Gui.addWorkbench(OpenSCADWorkbench()) + +openscadfilename = OpenSCADUtils.searchforopenscadexe() +if openscadfilename: #automatic search was succsessful + FreeCAD.__unit_test__ += ["TestOpenSCADGui"] diff --git a/src/Mod/OpenSCAD/OpenSCADTest/__init__.py b/src/Mod/OpenSCAD/OpenSCADTest/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/src/Mod/OpenSCAD/OpenSCADTest/app/__init__.py b/src/Mod/OpenSCAD/OpenSCADTest/app/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/src/Mod/OpenSCAD/OpenSCADTest/app/test_importCSG.py b/src/Mod/OpenSCAD/OpenSCADTest/app/test_importCSG.py new file mode 100644 index 0000000000..015846e6cf --- /dev/null +++ b/src/Mod/OpenSCAD/OpenSCADTest/app/test_importCSG.py @@ -0,0 +1,72 @@ +#*************************************************************************** +#* * +#* Copyright (c) 2021 Chris Hennes * +#* * +#* This program is free software; you can redistribute it and/or modify * +#* it under the terms of the GNU Lesser General Public License (LGPL) * +#* as published by the Free Software Foundation; either version 2 of * +#* the License, or (at your option) any later version. * +#* for detail see the LICENSE text file. * +#* * +#* 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 Library General Public License for more details. * +#* * +#* You should have received a copy of the GNU Library General Public * +#* License along with this program; if not, write to the Free Software * +#* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 * +#* USA * +#* * +#*************************************************************************** + +import unittest +import FreeCAD +import OpenSCAD +import importCSG + +from os.path import join + +__title__ = "ImportCSG OpenSCAD App unit tests" +__author__ = "Chris Hennes" +__url__ = "https://www.freecadweb.org" + + +class TestImportCSG(unittest.TestCase): + + MODULE = 'test_importCSG' # file name without extension + + + def setUp(self): + self.test_dir = join(FreeCAD.getHomePath(), "Mod", "OpenSCAD", "OpenSCADTest", "data") + pass + + def test_open_scad(self): + testfile = join(self.test_dir, "CSG.scad") + doc = importCSG.open(testfile) + + # Doc should now contain three solids: a union, an intersection, and a difference + union = doc.getObject("union") + intersection = doc.getObject("intersection") + difference = doc.getObject("difference") + + self.assertTrue (union is not None) + self.assertTrue (intersection is not None) + self.assertTrue (difference is not None) + + FreeCAD.closeDocument("CSG") + + def test_open_csg(self): + testfile = join(self.test_dir, "CSG.csg") + doc = importCSG.open(testfile) + + # Doc should now contain three solids: a union, an intersection, and a difference + union = doc.getObject("union") + intersection = doc.getObject("intersection") + difference = doc.getObject("difference") + + self.assertTrue (union is not None) + self.assertTrue (intersection is not None) + self.assertTrue (difference is not None) + + FreeCAD.closeDocument("CSG") \ No newline at end of file diff --git a/src/Mod/OpenSCAD/OpenSCADTest/data/CSG.csg b/src/Mod/OpenSCAD/OpenSCADTest/data/CSG.csg new file mode 100644 index 0000000000..61ca6c132b --- /dev/null +++ b/src/Mod/OpenSCAD/OpenSCADTest/data/CSG.csg @@ -0,0 +1,18 @@ +multmatrix([[1, 0, 0, -24], [0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1]]) { + union() { + cube(size = [15, 15, 15], center = true); + sphere($fn = 0, $fa = 12, $fs = 2, r = 10); + } +} +intersection() { + cube(size = [15, 15, 15], center = true); + sphere($fn = 0, $fa = 12, $fs = 2, r = 10); +} +multmatrix([[1, 0, 0, 24], [0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1]]) { + difference() { + cube(size = [15, 15, 15], center = true); + sphere($fn = 0, $fa = 12, $fs = 2, r = 10); + } +} +group(); + diff --git a/src/Mod/OpenSCAD/OpenSCADTest/data/CSG.scad b/src/Mod/OpenSCAD/OpenSCADTest/data/CSG.scad new file mode 100644 index 0000000000..199526ec41 --- /dev/null +++ b/src/Mod/OpenSCAD/OpenSCADTest/data/CSG.scad @@ -0,0 +1,32 @@ +// CSG.scad - Basic example of CSG usage + +translate([-24,0,0]) { + union() { + cube(15, center=true); + sphere(10); + } +} + +intersection() { + cube(15, center=true); + sphere(10); +} + +translate([24,0,0]) { + difference() { + cube(15, center=true); + sphere(10); + } +} + +echo(version=version()); +// Written by Marius Kintel +// +// To the extent possible under law, the author(s) have dedicated all +// copyright and related and neighboring rights to this software to the +// public domain worldwide. This software is distributed without any +// warranty. +// +// You should have received a copy of the CC0 Public Domain +// Dedication along with this software. +// If not, see . diff --git a/src/Mod/OpenSCAD/OpenSCADTest/data/__init__.py b/src/Mod/OpenSCAD/OpenSCADTest/data/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/src/Mod/OpenSCAD/OpenSCADTest/gui/__init__.py b/src/Mod/OpenSCAD/OpenSCADTest/gui/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/src/Mod/OpenSCAD/OpenSCADTest/gui/test_dummy.py b/src/Mod/OpenSCAD/OpenSCADTest/gui/test_dummy.py new file mode 100644 index 0000000000..1b7d729ff0 --- /dev/null +++ b/src/Mod/OpenSCAD/OpenSCADTest/gui/test_dummy.py @@ -0,0 +1,38 @@ +#*************************************************************************** +#* * +#* Copyright (c) 2021 Chris Hennes * +#* * +#* This program is free software; you can redistribute it and/or modify * +#* it under the terms of the GNU Lesser General Public License (LGPL) * +#* as published by the Free Software Foundation; either version 2 of * +#* the License, or (at your option) any later version. * +#* for detail see the LICENSE text file. * +#* * +#* 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 Library General Public License for more details. * +#* * +#* You should have received a copy of the GNU Library General Public * +#* License along with this program; if not, write to the Free Software * +#* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 * +#* USA * +#* * +#*************************************************************************** + +# OpenSCAD Module GUI Tests + +import unittest +import FreeCAD +import OpenSCAD + +class TestDummy(unittest.TestCase): + + MODULE = 'test_dummy' # file name without extension + + + def setUp(self): + pass + + def test_tests(self): + self.assertTrue(True) \ No newline at end of file diff --git a/src/Mod/OpenSCAD/OpenSCADTest/test_information.md b/src/Mod/OpenSCAD/OpenSCADTest/test_information.md new file mode 100644 index 0000000000..1108004d88 --- /dev/null +++ b/src/Mod/OpenSCAD/OpenSCADTest/test_information.md @@ -0,0 +1,3 @@ +# OpenSCAD Unit Test Information + +The OpenSCAD testing framework is based on the tests in the FEM Module. diff --git a/src/Mod/OpenSCAD/TestOpenSCADApp.py b/src/Mod/OpenSCAD/TestOpenSCADApp.py new file mode 100644 index 0000000000..7398486c6d --- /dev/null +++ b/src/Mod/OpenSCAD/TestOpenSCADApp.py @@ -0,0 +1,28 @@ +# *************************************************************************** +# * Copyright (c) 2021 Chris Hennes * +# * * +# * This file is part of the FreeCAD CAx development system. * +# * * +# * This program is free software; you can redistribute it and/or modify * +# * it under the terms of the GNU Lesser General Public License (LGPL) * +# * as published by the Free Software Foundation; either version 2 of * +# * the License, or (at your option) any later version. * +# * for detail see the LICENCE text file. * +# * * +# * 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 Library General Public License for more details. * +# * * +# * You should have received a copy of the GNU Library General Public * +# * License along with this program; if not, write to the Free Software * +# * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 * +# * USA * +# * * +# *************************************************************************** + +# Unit test for the OpenSCAD module +from OpenSCADTest.app.test_importCSG import TestImportCSG as OpenSCADTestImportCSG + +# dummy usage to get flake8 and lgtm quiet +False if OpenSCADTestImportCSG.__name__ else True diff --git a/src/Mod/OpenSCAD/TestOpenSCADGui.py b/src/Mod/OpenSCAD/TestOpenSCADGui.py new file mode 100644 index 0000000000..d0165e9810 --- /dev/null +++ b/src/Mod/OpenSCAD/TestOpenSCADGui.py @@ -0,0 +1,29 @@ +# *************************************************************************** +# * Copyright (c) 2021 Chris Hennes * +# * * +# * This file is part of the FreeCAD CAx development system. * +# * * +# * This program is free software; you can redistribute it and/or modify * +# * it under the terms of the GNU Lesser General Public License (LGPL) * +# * as published by the Free Software Foundation; either version 2 of * +# * the License, or (at your option) any later version. * +# * for detail see the LICENCE text file. * +# * * +# * 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 Library General Public License for more details. * +# * * +# * You should have received a copy of the GNU Library General Public * +# * License along with this program; if not, write to the Free Software * +# * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 * +# * USA * +# * * +# *************************************************************************** + +# Gui Unit tests for the FEM module +from OpenSCADTest.gui.test_dummy import TestDummy as OpenSCADGuiTestDummy + + +# dummy usage to get flake8 and lgtm quiet +False if OpenSCADGuiTestDummy.__name__ else True