diff --git a/tests/openscad_runner.py b/tests/openscad_runner.py index 2b4dea2..2a94b55 100644 --- a/tests/openscad_runner.py +++ b/tests/openscad_runner.py @@ -2,6 +2,9 @@ Helpful classes for running OpenScad from Python. @Copyright Arthur Moore 2024 MIT License """ + +import subprocess +from pathlib import Path from typing import NamedTuple class Vec3(NamedTuple): @@ -30,15 +33,42 @@ def set_variable_argument(var: str, val) -> [str, str]: """ return ['-D', f'{var}={str(val)}'] -openscad_binary_windows = 'C:\Program Files\OpenSCAD\openscad.exe' +class OpenScadRunner: + '''Helper to run the openscad binary''' + scad_file_path: Path + openscad_binary_path: Path + image_folder_base: Path -common_arguments = [ - #'--hardwarnings', // Does not work when setting variables by using functions - '--enable=fast-csg', - '--enable=predictible-output', - '--imgsize=1280,720', - '--view=axes', - '--projection=ortho', - ] + set_variable_argument('$fa', 8) + set_variable_argument('$fs', 0.25) + WINDOWS_DEFAULT_PATH = 'C:\Program Files\OpenSCAD\openscad.exe' + TOP_ANGLE_CAMERA = CameraArguments(Vec3(0,0,0),Vec3(45,0,45),50) -top_angle_camera = CameraArguments(Vec3(0,0,0),Vec3(45,0,45),50) + common_arguments = [ + #'--hardwarnings', // Does not work when setting variables by using functions + '--enable=fast-csg', + '--enable=predictible-output', + '--imgsize=1280,720', + '--view=axes', + '--projection=ortho', + ] + \ + set_variable_argument('$fa', 8) + set_variable_argument('$fs', 0.25) + + def __init__(self, file_path: Path): + self.openscad_binary_path = self.WINDOWS_DEFAULT_PATH + self.scad_file_path = file_path + self.image_folder_base = Path('.') + + def create_image(self, camera_args: CameraArguments, args: [str], image_file_name: str): + """ + Run the code, to create an image. + @Important The only verification is that no errors occured. + There is no verification if the image was created, or the image contents. + """ + assert(self.scad_file_path.exists()) + assert(self.image_folder_base.exists()) + + image_path = self.image_folder_base.joinpath(image_file_name) + command_arguments = self.common_arguments + \ + [camera_args.as_argument()] + args + \ + [f'-o{str(image_path)}', str(self.scad_file_path)] + #print(command_arguments) + return subprocess.run([self.openscad_binary_path]+command_arguments, check=True) diff --git a/tests/test_holes.py b/tests/test_holes.py index 4f13b75..a189173 100644 --- a/tests/test_holes.py +++ b/tests/test_holes.py @@ -1,36 +1,24 @@ """ -Functions for testing hole cutouts. +Tests for gridfinity-rebuilt-holes.scad @Copyright Arthur Moore 2024 MIT License """ from pathlib import Path from openscad_runner import * -import subprocess import unittest -class TestHoles(unittest.TestCase): +class TestHoleCutouts(unittest.TestCase): """ - Test Hole Cutouts. - + Test Hole Cutouts. The negatives used with `difference()` to create a hole. + Currently only makes sure code runs, and outputs pictures for manual verification. """ - scad_file_path = Path('../gridfinity-rebuilt-holes.scad') - image_folder_base = Path('../images/hole_cutouts/') - def run_image(self, camera_args: CameraArguments, test_args: [str], image_file_name: str): - """ - Run the code, to create an image. - @Important The only verification is that no errors occured. - There is no verification if the image was created, or the image contents. - """ - assert(self.scad_file_path.exists()) - image_path = self.image_folder_base.joinpath(image_file_name) - command_arguments = [openscad_binary_windows] + common_arguments + \ - [camera_args.as_argument()] + test_args + \ - [f'-o{str(image_path)}', str(self.scad_file_path)] - print(command_arguments) - return subprocess.run(command_arguments, check=True) + @classmethod + def setUpClass(cls): + cls.scad_runner = OpenScadRunner(Path('../gridfinity-rebuilt-holes.scad')) + cls.scad_runner.image_folder_base = Path('../images/hole_cutouts/') def test_refined_hole(self): """ @@ -39,47 +27,55 @@ class TestHoles(unittest.TestCase): camera_args = CameraArguments(Vec3(0,0,0),Vec3(225,0,225),50) test_args = set_variable_argument('test_options', 'bundle_hole_options(refined_hole=true, magnet_hole=false, screw_hole=false, crush_ribs=false, chamfer=false, supportless=false)') - self.run_image(camera_args, test_args, Path('refined_hole.png')) + self.scad_runner.create_image(camera_args, test_args, Path('refined_hole.png')) def test_plain_magnet_hole(self): test_args = set_variable_argument('test_options', 'bundle_hole_options(refined_hole=false, magnet_hole=true, screw_hole=false, crush_ribs=false, chamfer=false, supportless=false)') - self.run_image(top_angle_camera, test_args, Path('magnet_hole.png')) + self.scad_runner.create_image(self.scad_runner.TOP_ANGLE_CAMERA, + test_args, Path('magnet_hole.png')) def test_plain_screw_hole(self): test_args = set_variable_argument('test_options', 'bundle_hole_options(refined_hole=false, magnet_hole=false, screw_hole=true, crush_ribs=false, chamfer=false, supportless=false)') - self.run_image(top_angle_camera, test_args, Path('screw_hole.png')) + self.scad_runner.create_image(self.scad_runner.TOP_ANGLE_CAMERA, + test_args, Path('screw_hole.png')) def test_magnet_and_screw_hole(self): test_args = set_variable_argument('test_options', 'bundle_hole_options(refined_hole=false, magnet_hole=true, screw_hole=true, crush_ribs=false, chamfer=false, supportless=false)') - self.run_image(top_angle_camera, test_args, Path('magnet_and_screw_hole.png')) + self.scad_runner.create_image(self.scad_runner.TOP_ANGLE_CAMERA, + test_args, Path('magnet_and_screw_hole.png')) def test_chamfered_magnet_hole(self): test_args = set_variable_argument('test_options', 'bundle_hole_options(refined_hole=false, magnet_hole=true, screw_hole=false, crush_ribs=false, chamfer=true, supportless=false)') - self.run_image(top_angle_camera, test_args, Path('chamfered_magnet_hole.png')) + self.scad_runner.create_image(self.scad_runner.TOP_ANGLE_CAMERA, + test_args, Path('chamfered_magnet_hole.png')) def test_magnet_hole_crush_ribs(self): test_args = set_variable_argument('test_options', 'bundle_hole_options(refined_hole=false, magnet_hole=true, screw_hole=false, crush_ribs=true, chamfer=false, supportless=false)') - self.run_image(top_angle_camera, test_args, Path('magnet_hole_crush_ribs.png')) + self.scad_runner.create_image(self.scad_runner.TOP_ANGLE_CAMERA, + test_args, Path('magnet_hole_crush_ribs.png')) def test_magnet_hole_supportless(self): test_args = set_variable_argument('test_options', 'bundle_hole_options(refined_hole=false, magnet_hole=true, screw_hole=false, crush_ribs=false, chamfer=false, supportless=true)') - self.run_image(top_angle_camera, test_args, Path('magnet_hole_supportless.png')) + self.scad_runner.create_image(self.scad_runner.TOP_ANGLE_CAMERA, + test_args, Path('magnet_hole_supportless.png')) def test_magnet_and_screw_hole_supportless(self): test_args = set_variable_argument('test_options', 'bundle_hole_options(refined_hole=false, magnet_hole=true, screw_hole=true, crush_ribs=false, chamfer=false, supportless=true)') - self.run_image(top_angle_camera, test_args, Path('magnet_and_screw_hole_supportless.png')) + self.scad_runner.create_image(self.scad_runner.TOP_ANGLE_CAMERA, + test_args, Path('magnet_and_screw_hole_supportless.png')) def test_all_hole_options(self): test_args = set_variable_argument('test_options', 'bundle_hole_options(refined_hole=false, magnet_hole=true, screw_hole=true, crush_ribs=true, chamfer=true, supportless=true)') - self.run_image(top_angle_camera, test_args, Path('all_hole_options.png')) + self.scad_runner.create_image(self.scad_runner.TOP_ANGLE_CAMERA, + test_args, Path('all_hole_options.png')) if __name__ == '__main__': unittest.main()