From 965d3ce23b92f8aff1063debd6d3364de15791fe Mon Sep 17 00:00:00 2001 From: Garret Fick Date: Sun, 24 Jul 2016 22:08:31 +0800 Subject: Add more tests for rendering to PNG. Start adding tests for rendering to Gerber format. Changed definition of no hole to use None instead of 0 so we can differentiate when writing to Gerber format. Makde polygon use hole diameter instead of hole radius to match other primitives --- gerber/tests/test_rs274x_backend.py | 185 ++++++++++++++++++++++++++++++++++++ 1 file changed, 185 insertions(+) create mode 100644 gerber/tests/test_rs274x_backend.py (limited to 'gerber/tests/test_rs274x_backend.py') diff --git a/gerber/tests/test_rs274x_backend.py b/gerber/tests/test_rs274x_backend.py new file mode 100644 index 0000000..89512f0 --- /dev/null +++ b/gerber/tests/test_rs274x_backend.py @@ -0,0 +1,185 @@ +#! /usr/bin/env python +# -*- coding: utf-8 -*- + +# Author: Garret Fick +import io +import os + +from ..render.rs274x_backend import Rs274xContext +from ..rs274x import read +from .tests import * + +def test_render_two_boxes(): + """Umaco exapmle of two boxes""" + _test_render('resources/example_two_square_boxes.gbr', 'golden/example_two_square_boxes.gbr') + + +def _test_render_single_quadrant(): + """Umaco exapmle of a single quadrant arc""" + + # TODO there is probably a bug here + _test_render('resources/example_single_quadrant.gbr', 'golden/example_single_quadrant.gbr') + + +def _test_render_simple_contour(): + """Umaco exapmle of a simple arrow-shaped contour""" + _test_render('resources/example_simple_contour.gbr', 'golden/example_simple_contour.gbr') + + +def _test_render_single_contour_1(): + """Umaco example of a single contour + + The resulting image for this test is used by other tests because they must generate the same output.""" + _test_render('resources/example_single_contour_1.gbr', 'golden/example_single_contour.gbr') + + +def _test_render_single_contour_2(): + """Umaco exapmle of a single contour, alternate contour end order + + The resulting image for this test is used by other tests because they must generate the same output.""" + _test_render('resources/example_single_contour_2.gbr', 'golden/example_single_contour.gbr') + + +def _test_render_single_contour_3(): + """Umaco exapmle of a single contour with extra line""" + _test_render('resources/example_single_contour_3.gbr', 'golden/example_single_contour_3.gbr') + + +def _test_render_not_overlapping_contour(): + """Umaco example of D02 staring a second contour""" + _test_render('resources/example_not_overlapping_contour.gbr', 'golden/example_not_overlapping_contour.gbr') + + +def _test_render_not_overlapping_touching(): + """Umaco example of D02 staring a second contour""" + _test_render('resources/example_not_overlapping_touching.gbr', 'golden/example_not_overlapping_touching.gbr') + + +def _test_render_overlapping_touching(): + """Umaco example of D02 staring a second contour""" + _test_render('resources/example_overlapping_touching.gbr', 'golden/example_overlapping_touching.gbr') + + +def _test_render_overlapping_contour(): + """Umaco example of D02 staring a second contour""" + _test_render('resources/example_overlapping_contour.gbr', 'golden/example_overlapping_contour.gbr') + + +def _DISABLED_test_render_level_holes(): + """Umaco example of using multiple levels to create multiple holes""" + + # TODO This is clearly rendering wrong. I'm temporarily checking this in because there are more + # rendering fixes in the related repository that may resolve these. + _test_render('resources/example_level_holes.gbr', 'golden/example_overlapping_contour.gbr') + + +def _DISABLED_test_render_cutin(): + """Umaco example of using a cutin""" + + # TODO This is clearly rendering wrong. + _test_render('resources/example_cutin.gbr', 'golden/example_cutin.gbr') + + +def _test_render_fully_coincident(): + """Umaco example of coincident lines rendering two contours""" + + _test_render('resources/example_fully_coincident.gbr', 'golden/example_fully_coincident.gbr') + + +def _test_render_coincident_hole(): + """Umaco example of coincident lines rendering a hole in the contour""" + + _test_render('resources/example_coincident_hole.gbr', 'golden/example_coincident_hole.gbr') + + +def _test_render_cutin_multiple(): + """Umaco example of a region with multiple cutins""" + + _test_render('resources/example_cutin_multiple.gbr', 'golden/example_cutin_multiple.gbr') + + +def _test_flash_circle(): + """Umaco example a simple circular flash with and without a hole""" + + _test_render('resources/example_flash_circle.gbr', 'golden/example_flash_circle.gbr') + + +def _test_flash_rectangle(): + """Umaco example a simple rectangular flash with and without a hole""" + + _test_render('resources/example_flash_rectangle.gbr', 'golden/example_flash_rectangle.gbr') + + +def _test_flash_obround(): + """Umaco example a simple obround flash with and without a hole""" + + _test_render('resources/example_flash_obround.gbr', 'golden/example_flash_obround.gbr') + + +def _test_flash_polygon(): + """Umaco example a simple polygon flash with and without a hole""" + + _test_render('resources/example_flash_polygon.gbr', 'golden/example_flash_polygon.gbr') + + +def _test_holes_dont_clear(): + """Umaco example that an aperture with a hole does not clear the area""" + + _test_render('resources/example_holes_dont_clear.gbr', 'golden/example_holes_dont_clear.gbr') + + +def _test_render_am_exposure_modifier(): + """Umaco example that an aperture macro with a hole does not clear the area""" + + _test_render('resources/example_am_exposure_modifier.gbr', 'golden/example_am_exposure_modifier.gbr') + + +def _resolve_path(path): + return os.path.join(os.path.dirname(__file__), + path) + + +def _test_render(gerber_path, png_expected_path, create_output_path = None): + """Render the gerber file and compare to the expected PNG output. + + Parameters + ---------- + gerber_path : string + Path to Gerber file to open + png_expected_path : string + Path to the PNG file to compare to + create_output : string|None + If not None, write the generated PNG to the specified path. + This is primarily to help with + """ + + gerber_path = _resolve_path(gerber_path) + png_expected_path = _resolve_path(png_expected_path) + if create_output_path: + create_output_path = _resolve_path(create_output_path) + + gerber = read(gerber_path) + + # Create GBR output from the input file + ctx = Rs274xContext(gerber.settings) + gerber.render(ctx) + + actual_contents = ctx.dump() + + # If we want to write the file bytes, do it now. This happens + if create_output_path: + with open(create_output_path, 'wb') as out_file: + out_file.write(actual_contents.getvalue()) + # Creating the output is dangerous - it could overwrite the expected result. + # So if we are creating the output, we make the test fail on purpose so you + # won't forget to disable this + assert_false(True, 'Test created the output %s. This needs to be disabled to make sure the test behaves correctly' % (create_output_path,)) + + # Read the expected PNG file + + with open(png_expected_path, 'r') as expected_file: + expected_contents = expected_file.read() + + assert_equal(expected_contents, actual_contents.getvalue()) + + return gerber -- cgit From 389c273a8787a20f3e6ea5fdb951f62d7d5d4999 Mon Sep 17 00:00:00 2001 From: Hamilton Kibbe Date: Fri, 18 Nov 2016 08:12:55 -0500 Subject: Clean up rs274x output tests --- gerber/tests/test_rs274x_backend.py | 38 ++++++++++++++++++------------------- 1 file changed, 19 insertions(+), 19 deletions(-) (limited to 'gerber/tests/test_rs274x_backend.py') diff --git a/gerber/tests/test_rs274x_backend.py b/gerber/tests/test_rs274x_backend.py index 89512f0..e128841 100644 --- a/gerber/tests/test_rs274x_backend.py +++ b/gerber/tests/test_rs274x_backend.py @@ -2,7 +2,7 @@ # -*- coding: utf-8 -*- # Author: Garret Fick -import io + import os from ..render.rs274x_backend import Rs274xContext @@ -16,7 +16,7 @@ def test_render_two_boxes(): def _test_render_single_quadrant(): """Umaco exapmle of a single quadrant arc""" - + # TODO there is probably a bug here _test_render('resources/example_single_quadrant.gbr', 'golden/example_single_quadrant.gbr') @@ -25,17 +25,17 @@ def _test_render_simple_contour(): """Umaco exapmle of a simple arrow-shaped contour""" _test_render('resources/example_simple_contour.gbr', 'golden/example_simple_contour.gbr') - + def _test_render_single_contour_1(): """Umaco example of a single contour - + The resulting image for this test is used by other tests because they must generate the same output.""" _test_render('resources/example_single_contour_1.gbr', 'golden/example_single_contour.gbr') def _test_render_single_contour_2(): """Umaco exapmle of a single contour, alternate contour end order - + The resulting image for this test is used by other tests because they must generate the same output.""" _test_render('resources/example_single_contour_2.gbr', 'golden/example_single_contour.gbr') @@ -43,12 +43,12 @@ def _test_render_single_contour_2(): def _test_render_single_contour_3(): """Umaco exapmle of a single contour with extra line""" _test_render('resources/example_single_contour_3.gbr', 'golden/example_single_contour_3.gbr') - - + + def _test_render_not_overlapping_contour(): """Umaco example of D02 staring a second contour""" _test_render('resources/example_not_overlapping_contour.gbr', 'golden/example_not_overlapping_contour.gbr') - + def _test_render_not_overlapping_touching(): """Umaco example of D02 staring a second contour""" @@ -67,7 +67,7 @@ def _test_render_overlapping_contour(): def _DISABLED_test_render_level_holes(): """Umaco example of using multiple levels to create multiple holes""" - + # TODO This is clearly rendering wrong. I'm temporarily checking this in because there are more # rendering fixes in the related repository that may resolve these. _test_render('resources/example_level_holes.gbr', 'golden/example_overlapping_contour.gbr') @@ -96,7 +96,7 @@ def _test_render_cutin_multiple(): """Umaco example of a region with multiple cutins""" _test_render('resources/example_cutin_multiple.gbr', 'golden/example_cutin_multiple.gbr') - + def _test_flash_circle(): """Umaco example a simple circular flash with and without a hole""" @@ -141,7 +141,7 @@ def _resolve_path(path): def _test_render(gerber_path, png_expected_path, create_output_path = None): """Render the gerber file and compare to the expected PNG output. - + Parameters ---------- gerber_path : string @@ -150,14 +150,14 @@ def _test_render(gerber_path, png_expected_path, create_output_path = None): Path to the PNG file to compare to create_output : string|None If not None, write the generated PNG to the specified path. - This is primarily to help with + This is primarily to help with """ - + gerber_path = _resolve_path(gerber_path) png_expected_path = _resolve_path(png_expected_path) if create_output_path: create_output_path = _resolve_path(create_output_path) - + gerber = read(gerber_path) # Create GBR output from the input file @@ -165,7 +165,7 @@ def _test_render(gerber_path, png_expected_path, create_output_path = None): gerber.render(ctx) actual_contents = ctx.dump() - + # If we want to write the file bytes, do it now. This happens if create_output_path: with open(create_output_path, 'wb') as out_file: @@ -174,12 +174,12 @@ def _test_render(gerber_path, png_expected_path, create_output_path = None): # So if we are creating the output, we make the test fail on purpose so you # won't forget to disable this assert_false(True, 'Test created the output %s. This needs to be disabled to make sure the test behaves correctly' % (create_output_path,)) - + # Read the expected PNG file - + with open(png_expected_path, 'r') as expected_file: expected_contents = expected_file.read() - + assert_equal(expected_contents, actual_contents.getvalue()) - + return gerber -- cgit From ef589a064015de3a1ce6487dbb56b99332673e9d Mon Sep 17 00:00:00 2001 From: Paulo Henrique Silva Date: Tue, 26 Nov 2019 00:37:41 -0300 Subject: Migrate to pytest (#111) * Migrate to pytest All tests were update to use pytest. Tests were alse black formatted. Eventually all code will be black formatted but need to merge some PRs first. --- gerber/tests/test_rs274x_backend.py | 105 ++++++++++++++++++++++++++---------- 1 file changed, 76 insertions(+), 29 deletions(-) (limited to 'gerber/tests/test_rs274x_backend.py') diff --git a/gerber/tests/test_rs274x_backend.py b/gerber/tests/test_rs274x_backend.py index e128841..13347c5 100644 --- a/gerber/tests/test_rs274x_backend.py +++ b/gerber/tests/test_rs274x_backend.py @@ -7,62 +7,86 @@ import os from ..render.rs274x_backend import Rs274xContext from ..rs274x import read -from .tests import * + def test_render_two_boxes(): """Umaco exapmle of two boxes""" - _test_render('resources/example_two_square_boxes.gbr', 'golden/example_two_square_boxes.gbr') + _test_render( + "resources/example_two_square_boxes.gbr", "golden/example_two_square_boxes.gbr" + ) def _test_render_single_quadrant(): """Umaco exapmle of a single quadrant arc""" # TODO there is probably a bug here - _test_render('resources/example_single_quadrant.gbr', 'golden/example_single_quadrant.gbr') + _test_render( + "resources/example_single_quadrant.gbr", "golden/example_single_quadrant.gbr" + ) def _test_render_simple_contour(): """Umaco exapmle of a simple arrow-shaped contour""" - _test_render('resources/example_simple_contour.gbr', 'golden/example_simple_contour.gbr') + _test_render( + "resources/example_simple_contour.gbr", "golden/example_simple_contour.gbr" + ) def _test_render_single_contour_1(): """Umaco example of a single contour The resulting image for this test is used by other tests because they must generate the same output.""" - _test_render('resources/example_single_contour_1.gbr', 'golden/example_single_contour.gbr') + _test_render( + "resources/example_single_contour_1.gbr", "golden/example_single_contour.gbr" + ) def _test_render_single_contour_2(): """Umaco exapmle of a single contour, alternate contour end order The resulting image for this test is used by other tests because they must generate the same output.""" - _test_render('resources/example_single_contour_2.gbr', 'golden/example_single_contour.gbr') + _test_render( + "resources/example_single_contour_2.gbr", "golden/example_single_contour.gbr" + ) def _test_render_single_contour_3(): """Umaco exapmle of a single contour with extra line""" - _test_render('resources/example_single_contour_3.gbr', 'golden/example_single_contour_3.gbr') + _test_render( + "resources/example_single_contour_3.gbr", "golden/example_single_contour_3.gbr" + ) def _test_render_not_overlapping_contour(): """Umaco example of D02 staring a second contour""" - _test_render('resources/example_not_overlapping_contour.gbr', 'golden/example_not_overlapping_contour.gbr') + _test_render( + "resources/example_not_overlapping_contour.gbr", + "golden/example_not_overlapping_contour.gbr", + ) def _test_render_not_overlapping_touching(): """Umaco example of D02 staring a second contour""" - _test_render('resources/example_not_overlapping_touching.gbr', 'golden/example_not_overlapping_touching.gbr') + _test_render( + "resources/example_not_overlapping_touching.gbr", + "golden/example_not_overlapping_touching.gbr", + ) def _test_render_overlapping_touching(): """Umaco example of D02 staring a second contour""" - _test_render('resources/example_overlapping_touching.gbr', 'golden/example_overlapping_touching.gbr') + _test_render( + "resources/example_overlapping_touching.gbr", + "golden/example_overlapping_touching.gbr", + ) def _test_render_overlapping_contour(): """Umaco example of D02 staring a second contour""" - _test_render('resources/example_overlapping_contour.gbr', 'golden/example_overlapping_contour.gbr') + _test_render( + "resources/example_overlapping_contour.gbr", + "golden/example_overlapping_contour.gbr", + ) def _DISABLED_test_render_level_holes(): @@ -70,76 +94,96 @@ def _DISABLED_test_render_level_holes(): # TODO This is clearly rendering wrong. I'm temporarily checking this in because there are more # rendering fixes in the related repository that may resolve these. - _test_render('resources/example_level_holes.gbr', 'golden/example_overlapping_contour.gbr') + _test_render( + "resources/example_level_holes.gbr", "golden/example_overlapping_contour.gbr" + ) def _DISABLED_test_render_cutin(): """Umaco example of using a cutin""" # TODO This is clearly rendering wrong. - _test_render('resources/example_cutin.gbr', 'golden/example_cutin.gbr') + _test_render("resources/example_cutin.gbr", "golden/example_cutin.gbr") def _test_render_fully_coincident(): """Umaco example of coincident lines rendering two contours""" - _test_render('resources/example_fully_coincident.gbr', 'golden/example_fully_coincident.gbr') + _test_render( + "resources/example_fully_coincident.gbr", "golden/example_fully_coincident.gbr" + ) def _test_render_coincident_hole(): """Umaco example of coincident lines rendering a hole in the contour""" - _test_render('resources/example_coincident_hole.gbr', 'golden/example_coincident_hole.gbr') + _test_render( + "resources/example_coincident_hole.gbr", "golden/example_coincident_hole.gbr" + ) def _test_render_cutin_multiple(): """Umaco example of a region with multiple cutins""" - _test_render('resources/example_cutin_multiple.gbr', 'golden/example_cutin_multiple.gbr') + _test_render( + "resources/example_cutin_multiple.gbr", "golden/example_cutin_multiple.gbr" + ) def _test_flash_circle(): """Umaco example a simple circular flash with and without a hole""" - _test_render('resources/example_flash_circle.gbr', 'golden/example_flash_circle.gbr') + _test_render( + "resources/example_flash_circle.gbr", "golden/example_flash_circle.gbr" + ) def _test_flash_rectangle(): """Umaco example a simple rectangular flash with and without a hole""" - _test_render('resources/example_flash_rectangle.gbr', 'golden/example_flash_rectangle.gbr') + _test_render( + "resources/example_flash_rectangle.gbr", "golden/example_flash_rectangle.gbr" + ) def _test_flash_obround(): """Umaco example a simple obround flash with and without a hole""" - _test_render('resources/example_flash_obround.gbr', 'golden/example_flash_obround.gbr') + _test_render( + "resources/example_flash_obround.gbr", "golden/example_flash_obround.gbr" + ) def _test_flash_polygon(): """Umaco example a simple polygon flash with and without a hole""" - _test_render('resources/example_flash_polygon.gbr', 'golden/example_flash_polygon.gbr') + _test_render( + "resources/example_flash_polygon.gbr", "golden/example_flash_polygon.gbr" + ) def _test_holes_dont_clear(): """Umaco example that an aperture with a hole does not clear the area""" - _test_render('resources/example_holes_dont_clear.gbr', 'golden/example_holes_dont_clear.gbr') + _test_render( + "resources/example_holes_dont_clear.gbr", "golden/example_holes_dont_clear.gbr" + ) def _test_render_am_exposure_modifier(): """Umaco example that an aperture macro with a hole does not clear the area""" - _test_render('resources/example_am_exposure_modifier.gbr', 'golden/example_am_exposure_modifier.gbr') + _test_render( + "resources/example_am_exposure_modifier.gbr", + "golden/example_am_exposure_modifier.gbr", + ) def _resolve_path(path): - return os.path.join(os.path.dirname(__file__), - path) + return os.path.join(os.path.dirname(__file__), path) -def _test_render(gerber_path, png_expected_path, create_output_path = None): +def _test_render(gerber_path, png_expected_path, create_output_path=None): """Render the gerber file and compare to the expected PNG output. Parameters @@ -168,18 +212,21 @@ def _test_render(gerber_path, png_expected_path, create_output_path = None): # If we want to write the file bytes, do it now. This happens if create_output_path: - with open(create_output_path, 'wb') as out_file: + with open(create_output_path, "wb") as out_file: out_file.write(actual_contents.getvalue()) # Creating the output is dangerous - it could overwrite the expected result. # So if we are creating the output, we make the test fail on purpose so you # won't forget to disable this - assert_false(True, 'Test created the output %s. This needs to be disabled to make sure the test behaves correctly' % (create_output_path,)) + assert not True, ( + "Test created the output %s. This needs to be disabled to make sure the test behaves correctly" + % (create_output_path,) + ) # Read the expected PNG file - with open(png_expected_path, 'r') as expected_file: + with open(png_expected_path, "r") as expected_file: expected_contents = expected_file.read() - assert_equal(expected_contents, actual_contents.getvalue()) + assert expected_contents == actual_contents.getvalue() return gerber -- cgit