From f0585baefa54c5cd891ba04c81053956b1a59977 Mon Sep 17 00:00:00 2001 From: Garret Fick Date: Sun, 17 Jul 2016 13:14:54 +0800 Subject: [PATCH] Create first test that renders and validates the the rendered PNG is correct. --- gerber/render/cairo_backend.py | 5 +- .../tests/golden/example_two_square_boxes.png | Bin 0 -> 18247 bytes .../resources/example_two_square_boxes.gbr | 19 ++++++ gerber/tests/test_cairo_backend.py | 59 ++++++++++++++++++ gerber/tests/test_primitives.py | 12 +++- 5 files changed, 90 insertions(+), 5 deletions(-) create mode 100644 gerber/tests/golden/example_two_square_boxes.png create mode 100644 gerber/tests/resources/example_two_square_boxes.gbr create mode 100644 gerber/tests/test_cairo_backend.py diff --git a/gerber/render/cairo_backend.py b/gerber/render/cairo_backend.py index 5a3c7a1..3c4a395 100644 --- a/gerber/render/cairo_backend.py +++ b/gerber/render/cairo_backend.py @@ -292,8 +292,7 @@ class GerberCairoContext(GerberContext): self.ctx.paint() def dump(self, filename): - is_svg = filename.lower().endswith(".svg") - if is_svg: + if filename and filename.lower().endswith(".svg"): self.surface.finish() self.surface_buffer.flush() with open(filename, "w") as f: @@ -301,7 +300,7 @@ class GerberCairoContext(GerberContext): f.write(self.surface_buffer.read()) f.flush() else: - self.surface.write_to_png(filename) + return self.surface.write_to_png(filename) def dump_svg_str(self): self.surface.finish() diff --git a/gerber/tests/golden/example_two_square_boxes.png b/gerber/tests/golden/example_two_square_boxes.png new file mode 100644 index 0000000000000000000000000000000000000000..4732995a9bcc73cc53215ab6d2f71bc1cfeb03e8 GIT binary patch literal 18247 zcmeAS@N?(olHy`uVBq!ia0y~y;CaHpzjIq`yzs@fEh z&29N{kLGS}ioRcCT|Ql%nSp_!VeRhve|z@T&Z}ZQbMETThc_?&yQ0m&z`*bWxZZP0JetPfYr}rctmwvVVDmG*B^sDF9!ajF{E@qByMxbz3{g<3=>A#;Gh@{2XHWq zrUytcjE2K#IEFz&0?bZOM~uO3n0yr)q6`c-PG-T`Z&FO*?6OT#aJF?MFO1zVcMThi zeZVgaGy(|bjv5ad`5X-#1_p-Fl*2HZtwsw4a0rYR3eaE}Efhuz1xQjDZ7GZv3XqgA z+EM_81Mv;;_3C{eVQT?EIvGYAL1hMynP`N*JwLK;b}q)dEsI zTAhGHfp*o2LfP90kQE6N_J7s*Ch87ij}FB#Fffb`B!ZH{=qM`#1Hbx2v00l`*Fs& zpPPY!;mHxi`nXXxC^$#M1QH6P;V>Ew1C|EPbtcs_Ft9xVoeMBpMnMzeX!#5Yh|zEu z4TsS*Fq#GiMH*OUcx>Of`QIPEQ3XZF^bPAj-xO{z{NoBb5r*%}?{2^3p6lE8Yzopr0LFr$3IG5A literal 0 HcmV?d00001 diff --git a/gerber/tests/resources/example_two_square_boxes.gbr b/gerber/tests/resources/example_two_square_boxes.gbr new file mode 100644 index 0000000..54a8ac1 --- /dev/null +++ b/gerber/tests/resources/example_two_square_boxes.gbr @@ -0,0 +1,19 @@ +G04 Ucamco ex. 1: Two square boxes* +%FSLAX25Y25*% +%MOMM*% +%TF.Part,Other*% +%LPD*% +%ADD10C,0.010*% +D10* +X0Y0D02* +G01* +X500000Y0D01* +Y500000D01* +X0D01* +Y0D01* +X600000D02* +X1100000D01* +Y500000D01* +X600000D01* +Y0D01* +M02* \ No newline at end of file diff --git a/gerber/tests/test_cairo_backend.py b/gerber/tests/test_cairo_backend.py new file mode 100644 index 0000000..d7ec7b3 --- /dev/null +++ b/gerber/tests/test_cairo_backend.py @@ -0,0 +1,59 @@ +#! /usr/bin/env python +# -*- coding: utf-8 -*- + +# Author: Garret Fick +import os +import io + +from ..render.cairo_backend import GerberCairoContext +from ..rs274x import read, GerberFile +from .tests import * + + + +TWO_BOXES_FILE = os.path.join(os.path.dirname(__file__), + 'resources/example_two_square_boxes.gbr') +TWO_BOXES_EXPECTED = os.path.join(os.path.dirname(__file__), + 'golden/example_two_square_boxes.png') + +def test_render_polygon(): + + _test_render(TWO_BOXES_FILE, TWO_BOXES_EXPECTED) + +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 = read(gerber_path) + + # Create PNG image to the memory stream + ctx = GerberCairoContext() + gerber.render(ctx) + + actual_bytes = ctx.dump(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: + out_file.write(actual_bytes) + # 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, 'rb') as expected_file: + expected_bytes = expected_file.read() + + assert_equal(expected_bytes, actual_bytes) diff --git a/gerber/tests/test_primitives.py b/gerber/tests/test_primitives.py index 0f13a80..a88497c 100644 --- a/gerber/tests/test_primitives.py +++ b/gerber/tests/test_primitives.py @@ -9,10 +9,18 @@ from operator import add def test_primitive_smoketest(): p = Primitive() - assert_raises(NotImplementedError, p.bounding_box) + try: + p.bounding_box + assert_false(True, 'should have thrown the exception') + except NotImplementedError: + pass p.to_metric() p.to_inch() - p.offset(1, 1) + try: + p.offset(1, 1) + assert_false(True, 'should have thrown the exception') + except NotImplementedError: + pass def test_line_angle(): """ Test Line primitive angle calculation