From 695e3d9220be8773f6630bb5c512d122b8576742 Mon Sep 17 00:00:00 2001 From: Hamilton Kibbe Date: Sun, 28 Sep 2014 18:07:15 -0400 Subject: Added excellon support and refactored project --- gerber/tests/test_utils.py | 68 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 gerber/tests/test_utils.py (limited to 'gerber/tests/test_utils.py') diff --git a/gerber/tests/test_utils.py b/gerber/tests/test_utils.py new file mode 100644 index 0000000..50e2403 --- /dev/null +++ b/gerber/tests/test_utils.py @@ -0,0 +1,68 @@ +#! /usr/bin/env python +# -*- coding: utf-8 -*- + +# Author: Hamilton Kibbe + +from ..utils import decimal_string, parse_gerber_value, write_gerber_value + + +def test_zero_suppression(): + """ Test gerber value parser and writer handle zero suppression correctly. + """ + # Default format + format = (2, 5) + + # Test leading zero suppression + zero_suppression = 'leading' + test_cases = [('1', 0.00001), ('10', 0.0001), ('100', 0.001), + ('1000', 0.01), ('10000', 0.1), ('100000', 1.0),('1000000', 10.0), + ('-1', -0.00001), ('-10', -0.0001), ('-100', -0.001), + ('-1000', -0.01), ('-10000', -0.1), ('-100000', -1.0),('-1000000', -10.0),] + for string, value in test_cases: + assert(value == parse_gerber_value(string,format,zero_suppression)) + assert(string == write_gerber_value(value,format,zero_suppression)) + + # Test trailing zero suppression + zero_suppression = 'trailing' + test_cases = [('1', 10.0), ('01', 1.0), ('001', 0.1), ('0001', 0.01), + ('00001', 0.001), ('000001', 0.0001), ('0000001', 0.00001), + ('-1', -10.0), ('-01', -1.0), ('-001', -0.1), ('-0001', -0.01), + ('-00001', -0.001), ('-000001', -0.0001), ('-0000001', -0.00001)] + for string, value in test_cases: + assert(value == parse_gerber_value(string,format,zero_suppression)) + assert(string == write_gerber_value(value,format,zero_suppression)) + + + +def test_format(): + """ Test gerber value parser and writer handle format correctly + """ + zero_suppression = 'leading' + test_cases = [((2,7),'1',0.0000001), ((2,6),'1',0.000001), + ((2,5),'1',0.00001), ((2,4),'1',0.0001), ((2,3),'1',0.001), + ((2,2),'1',0.01), ((2,1),'1',0.1), ((2,7),'-1',-0.0000001), + ((2,6),'-1',-0.000001), ((2,5),'-1',-0.00001), ((2,4),'-1',-0.0001), + ((2,3),'-1',-0.001), ((2,2),'-1',-0.01), ((2,1),'-1',-0.1),] + for format, string, value in test_cases: + assert(value == parse_gerber_value(string,format,zero_suppression)) + assert(string == write_gerber_value(value,format,zero_suppression)) + + zero_suppression = 'trailing' + test_cases = [((6, 5), '1' , 100000.0), ((5, 5), '1', 10000.0), + ((4, 5), '1', 1000.0), ((3, 5), '1', 100.0),((2, 5), '1', 10.0), + ((1, 5), '1', 1.0), ((6, 5), '-1' , -100000.0), + ((5, 5), '-1', -10000.0), ((4, 5), '-1', -1000.0), + ((3, 5), '-1', -100.0),((2, 5), '-1', -10.0), ((1, 5), '-1', -1.0),] + for format, string, value in test_cases: + assert(value == parse_gerber_value(string,format,zero_suppression)) + assert(string == write_gerber_value(value,format,zero_suppression)) + + +def test_decimal_truncation(): + """ Test decimal string truncates value to the correct precision + """ + value = 1.123456789 + for x in range(10): + result = decimal_string(value, precision=x) + calculated = '1.' + ''.join(str(y) for y in range(1,x+1)) + assert(result == calculated) \ No newline at end of file -- cgit From af97dcf2a8200d9319e20d2789dbb0baa0611ba5 Mon Sep 17 00:00:00 2001 From: Hamilton Kibbe Date: Tue, 7 Oct 2014 22:44:08 -0400 Subject: fix excellon render --- gerber/tests/test_utils.py | 81 +++++++++++++++++++++++++++------------------- 1 file changed, 48 insertions(+), 33 deletions(-) (limited to 'gerber/tests/test_utils.py') diff --git a/gerber/tests/test_utils.py b/gerber/tests/test_utils.py index 50e2403..001a32f 100644 --- a/gerber/tests/test_utils.py +++ b/gerber/tests/test_utils.py @@ -3,6 +3,7 @@ # Author: Hamilton Kibbe +from .tests import assert_equal from ..utils import decimal_string, parse_gerber_value, write_gerber_value @@ -10,59 +11,73 @@ def test_zero_suppression(): """ Test gerber value parser and writer handle zero suppression correctly. """ # Default format - format = (2, 5) - + fmt = (2, 5) + # Test leading zero suppression zero_suppression = 'leading' test_cases = [('1', 0.00001), ('10', 0.0001), ('100', 0.001), - ('1000', 0.01), ('10000', 0.1), ('100000', 1.0),('1000000', 10.0), - ('-1', -0.00001), ('-10', -0.0001), ('-100', -0.001), - ('-1000', -0.01), ('-10000', -0.1), ('-100000', -1.0),('-1000000', -10.0),] + ('1000', 0.01), ('10000', 0.1), ('100000', 1.0), + ('1000000', 10.0), ('-1', -0.00001), ('-10', -0.0001), + ('-100', -0.001), ('-1000', -0.01), ('-10000', -0.1), + ('-100000', -1.0), ('-1000000', -10.0), ] for string, value in test_cases: - assert(value == parse_gerber_value(string,format,zero_suppression)) - assert(string == write_gerber_value(value,format,zero_suppression)) - + assert(value == parse_gerber_value(string, fmt, zero_suppression)) + assert(string == write_gerber_value(value, fmt, zero_suppression)) + # Test trailing zero suppression zero_suppression = 'trailing' test_cases = [('1', 10.0), ('01', 1.0), ('001', 0.1), ('0001', 0.01), - ('00001', 0.001), ('000001', 0.0001), ('0000001', 0.00001), - ('-1', -10.0), ('-01', -1.0), ('-001', -0.1), ('-0001', -0.01), - ('-00001', -0.001), ('-000001', -0.0001), ('-0000001', -0.00001)] + ('00001', 0.001), ('000001', 0.0001), + ('0000001', 0.00001), ('-1', -10.0), ('-01', -1.0), + ('-001', -0.1), ('-0001', -0.01), ('-00001', -0.001), + ('-000001', -0.0001), ('-0000001', -0.00001)] for string, value in test_cases: - assert(value == parse_gerber_value(string,format,zero_suppression)) - assert(string == write_gerber_value(value,format,zero_suppression)) - + assert(value == parse_gerber_value(string, fmt, zero_suppression)) + assert(string == write_gerber_value(value, fmt, zero_suppression)) def test_format(): """ Test gerber value parser and writer handle format correctly """ zero_suppression = 'leading' - test_cases = [((2,7),'1',0.0000001), ((2,6),'1',0.000001), - ((2,5),'1',0.00001), ((2,4),'1',0.0001), ((2,3),'1',0.001), - ((2,2),'1',0.01), ((2,1),'1',0.1), ((2,7),'-1',-0.0000001), - ((2,6),'-1',-0.000001), ((2,5),'-1',-0.00001), ((2,4),'-1',-0.0001), - ((2,3),'-1',-0.001), ((2,2),'-1',-0.01), ((2,1),'-1',-0.1),] - for format, string, value in test_cases: - assert(value == parse_gerber_value(string,format,zero_suppression)) - assert(string == write_gerber_value(value,format,zero_suppression)) - + test_cases = [((2, 7), '1', 0.0000001), ((2, 6), '1', 0.000001), + ((2, 5), '1', 0.00001), ((2, 4), '1', 0.0001), + ((2, 3), '1', 0.001), ((2, 2), '1', 0.01), + ((2, 1), '1', 0.1), ((2, 7), '-1', -0.0000001), + ((2, 6), '-1', -0.000001), ((2, 5), '-1', -0.00001), + ((2, 4), '-1', -0.0001), ((2, 3), '-1', -0.001), + ((2, 2), '-1', -0.01), ((2, 1), '-1', -0.1), ] + for fmt, string, value in test_cases: + assert(value == parse_gerber_value(string, fmt, zero_suppression)) + assert(string == write_gerber_value(value, fmt, zero_suppression)) + zero_suppression = 'trailing' - test_cases = [((6, 5), '1' , 100000.0), ((5, 5), '1', 10000.0), - ((4, 5), '1', 1000.0), ((3, 5), '1', 100.0),((2, 5), '1', 10.0), - ((1, 5), '1', 1.0), ((6, 5), '-1' , -100000.0), - ((5, 5), '-1', -10000.0), ((4, 5), '-1', -1000.0), - ((3, 5), '-1', -100.0),((2, 5), '-1', -10.0), ((1, 5), '-1', -1.0),] - for format, string, value in test_cases: - assert(value == parse_gerber_value(string,format,zero_suppression)) - assert(string == write_gerber_value(value,format,zero_suppression)) + test_cases = [((6, 5), '1', 100000.0), ((5, 5), '1', 10000.0), + ((4, 5), '1', 1000.0), ((3, 5), '1', 100.0), + ((2, 5), '1', 10.0), ((1, 5), '1', 1.0), + ((6, 5), '-1', -100000.0), ((5, 5), '-1', -10000.0), + ((4, 5), '-1', -1000.0), ((3, 5), '-1', -100.0), + ((2, 5), '-1', -10.0), ((1, 5), '-1', -1.0), ] + for fmt, string, value in test_cases: + assert(value == parse_gerber_value(string, fmt, zero_suppression)) + assert(string == write_gerber_value(value, fmt, zero_suppression)) def test_decimal_truncation(): - """ Test decimal string truncates value to the correct precision + """ Test decimal_string truncates value to the correct precision """ value = 1.123456789 for x in range(10): result = decimal_string(value, precision=x) calculated = '1.' + ''.join(str(y) for y in range(1,x+1)) - assert(result == calculated) \ No newline at end of file + assert(result == calculated) + + +def test_decimal_padding(): + """ Test decimal_string padding + """ + value = 1.123 + assert_equal(decimal_string(value, precision=3, padding=True), '1.123') + assert_equal(decimal_string(value, precision=4, padding=True), '1.1230') + assert_equal(decimal_string(value, precision=5, padding=True), '1.12300') + assert_equal(decimal_string(value, precision=6, padding=True), '1.123000') -- cgit From ab69ee0172353e64fbe5099a974341e88feaf24b Mon Sep 17 00:00:00 2001 From: Paulo Henrique Silva Date: Mon, 10 Nov 2014 12:24:09 -0200 Subject: Bunch of small fixes to improve Gerber read/write. --- gerber/tests/test_utils.py | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) (limited to 'gerber/tests/test_utils.py') diff --git a/gerber/tests/test_utils.py b/gerber/tests/test_utils.py index 001a32f..706fa65 100644 --- a/gerber/tests/test_utils.py +++ b/gerber/tests/test_utils.py @@ -19,7 +19,8 @@ def test_zero_suppression(): ('1000', 0.01), ('10000', 0.1), ('100000', 1.0), ('1000000', 10.0), ('-1', -0.00001), ('-10', -0.0001), ('-100', -0.001), ('-1000', -0.01), ('-10000', -0.1), - ('-100000', -1.0), ('-1000000', -10.0), ] + ('-100000', -1.0), ('-1000000', -10.0), + ('0', 0.0)] for string, value in test_cases: assert(value == parse_gerber_value(string, fmt, zero_suppression)) assert(string == write_gerber_value(value, fmt, zero_suppression)) @@ -30,7 +31,8 @@ def test_zero_suppression(): ('00001', 0.001), ('000001', 0.0001), ('0000001', 0.00001), ('-1', -10.0), ('-01', -1.0), ('-001', -0.1), ('-0001', -0.01), ('-00001', -0.001), - ('-000001', -0.0001), ('-0000001', -0.00001)] + ('-000001', -0.0001), ('-0000001', -0.00001), + ('0', 0.0)] for string, value in test_cases: assert(value == parse_gerber_value(string, fmt, zero_suppression)) assert(string == write_gerber_value(value, fmt, zero_suppression)) @@ -46,7 +48,8 @@ def test_format(): ((2, 1), '1', 0.1), ((2, 7), '-1', -0.0000001), ((2, 6), '-1', -0.000001), ((2, 5), '-1', -0.00001), ((2, 4), '-1', -0.0001), ((2, 3), '-1', -0.001), - ((2, 2), '-1', -0.01), ((2, 1), '-1', -0.1), ] + ((2, 2), '-1', -0.01), ((2, 1), '-1', -0.1), + ((2, 6), '0', 0) ] for fmt, string, value in test_cases: assert(value == parse_gerber_value(string, fmt, zero_suppression)) assert(string == write_gerber_value(value, fmt, zero_suppression)) @@ -57,7 +60,8 @@ def test_format(): ((2, 5), '1', 10.0), ((1, 5), '1', 1.0), ((6, 5), '-1', -100000.0), ((5, 5), '-1', -10000.0), ((4, 5), '-1', -1000.0), ((3, 5), '-1', -100.0), - ((2, 5), '-1', -10.0), ((1, 5), '-1', -1.0), ] + ((2, 5), '-1', -10.0), ((1, 5), '-1', -1.0), + ((2, 5), '0', 0)] for fmt, string, value in test_cases: assert(value == parse_gerber_value(string, fmt, zero_suppression)) assert(string == write_gerber_value(value, fmt, zero_suppression)) @@ -81,3 +85,6 @@ def test_decimal_padding(): assert_equal(decimal_string(value, precision=4, padding=True), '1.1230') assert_equal(decimal_string(value, precision=5, padding=True), '1.12300') assert_equal(decimal_string(value, precision=6, padding=True), '1.123000') + + assert_equal(decimal_string(0, precision=6, padding=True), '0.000000') + -- cgit From d98d23f8b5d61bb9d20e743a3c44bf04b6b2330a Mon Sep 17 00:00:00 2001 From: Hamilton Kibbe Date: Mon, 2 Feb 2015 00:43:08 -0500 Subject: More tests and bugfixes --- gerber/tests/test_utils.py | 43 +++++++++++++++++++++++++++++++------------ 1 file changed, 31 insertions(+), 12 deletions(-) (limited to 'gerber/tests/test_utils.py') diff --git a/gerber/tests/test_utils.py b/gerber/tests/test_utils.py index 706fa65..1077022 100644 --- a/gerber/tests/test_utils.py +++ b/gerber/tests/test_utils.py @@ -3,8 +3,8 @@ # Author: Hamilton Kibbe -from .tests import assert_equal -from ..utils import decimal_string, parse_gerber_value, write_gerber_value +from .tests import assert_equal, assert_raises +from ..utils import decimal_string, parse_gerber_value, write_gerber_value, detect_file_format def test_zero_suppression(): @@ -22,8 +22,8 @@ def test_zero_suppression(): ('-100000', -1.0), ('-1000000', -10.0), ('0', 0.0)] for string, value in test_cases: - assert(value == parse_gerber_value(string, fmt, zero_suppression)) - assert(string == write_gerber_value(value, fmt, zero_suppression)) + assert_equal(value, parse_gerber_value(string, fmt, zero_suppression)) + assert_equal(string, write_gerber_value(value, fmt, zero_suppression)) # Test trailing zero suppression zero_suppression = 'trailing' @@ -34,8 +34,8 @@ def test_zero_suppression(): ('-000001', -0.0001), ('-0000001', -0.00001), ('0', 0.0)] for string, value in test_cases: - assert(value == parse_gerber_value(string, fmt, zero_suppression)) - assert(string == write_gerber_value(value, fmt, zero_suppression)) + assert_equal(value, parse_gerber_value(string, fmt, zero_suppression)) + assert_equal(string, write_gerber_value(value, fmt, zero_suppression)) def test_format(): @@ -51,8 +51,8 @@ def test_format(): ((2, 2), '-1', -0.01), ((2, 1), '-1', -0.1), ((2, 6), '0', 0) ] for fmt, string, value in test_cases: - assert(value == parse_gerber_value(string, fmt, zero_suppression)) - assert(string == write_gerber_value(value, fmt, zero_suppression)) + assert_equal(value, parse_gerber_value(string, fmt, zero_suppression)) + assert_equal(string, write_gerber_value(value, fmt, zero_suppression)) zero_suppression = 'trailing' test_cases = [((6, 5), '1', 100000.0), ((5, 5), '1', 10000.0), @@ -63,8 +63,8 @@ def test_format(): ((2, 5), '-1', -10.0), ((1, 5), '-1', -1.0), ((2, 5), '0', 0)] for fmt, string, value in test_cases: - assert(value == parse_gerber_value(string, fmt, zero_suppression)) - assert(string == write_gerber_value(value, fmt, zero_suppression)) + assert_equal(value, parse_gerber_value(string, fmt, zero_suppression)) + assert_equal(string, write_gerber_value(value, fmt, zero_suppression)) def test_decimal_truncation(): @@ -74,7 +74,7 @@ def test_decimal_truncation(): for x in range(10): result = decimal_string(value, precision=x) calculated = '1.' + ''.join(str(y) for y in range(1,x+1)) - assert(result == calculated) + assert_equal(result, calculated) def test_decimal_padding(): @@ -85,6 +85,25 @@ def test_decimal_padding(): assert_equal(decimal_string(value, precision=4, padding=True), '1.1230') assert_equal(decimal_string(value, precision=5, padding=True), '1.12300') assert_equal(decimal_string(value, precision=6, padding=True), '1.123000') - assert_equal(decimal_string(0, precision=6, padding=True), '0.000000') + +def test_parse_format_validation(): + """ Test parse_gerber_value() format validation + """ + assert_raises(ValueError, parse_gerber_value, '00001111', (7, 5)) + assert_raises(ValueError, parse_gerber_value, '00001111', (5, 8)) + assert_raises(ValueError, parse_gerber_value, '00001111', (13,1)) + +def test_write_format_validation(): + """ Test write_gerber_value() format validation + """ + assert_raises(ValueError, write_gerber_value, 69.0, (7, 5)) + assert_raises(ValueError, write_gerber_value, 69.0, (5, 8)) + assert_raises(ValueError, write_gerber_value, 69.0, (13,1)) + + +def test_detect_format_with_short_file(): + """ Verify file format detection works with short files + """ + assert_equal('unknown', detect_file_format('gerber/tests/__init__.py')) -- cgit From 1cc20b351c10b1fa19817f29edd8c54a27aeee4b Mon Sep 17 00:00:00 2001 From: Hamilton Kibbe Date: Mon, 2 Feb 2015 11:42:47 -0500 Subject: tests --- gerber/tests/test_utils.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) (limited to 'gerber/tests/test_utils.py') diff --git a/gerber/tests/test_utils.py b/gerber/tests/test_utils.py index 1077022..1c3f1e5 100644 --- a/gerber/tests/test_utils.py +++ b/gerber/tests/test_utils.py @@ -4,7 +4,7 @@ # Author: Hamilton Kibbe from .tests import assert_equal, assert_raises -from ..utils import decimal_string, parse_gerber_value, write_gerber_value, detect_file_format +from ..utils import * def test_zero_suppression(): @@ -107,3 +107,11 @@ def test_detect_format_with_short_file(): """ Verify file format detection works with short files """ assert_equal('unknown', detect_file_format('gerber/tests/__init__.py')) + +def test_validate_coordinates(): + assert_raises(TypeError, validate_coordinates, 3) + assert_raises(TypeError, validate_coordinates, 3.1) + assert_raises(TypeError, validate_coordinates, '14') + assert_raises(TypeError, validate_coordinates, (0,)) + assert_raises(TypeError, validate_coordinates, (0,1,2)) + assert_raises(TypeError, validate_coordinates, (0,'string')) -- cgit From aea1f38597824085739aeed1fa6f33e264e23a4b Mon Sep 17 00:00:00 2001 From: Hamilton Kibbe Date: Sun, 8 Feb 2015 22:27:24 -0500 Subject: Fix write_gerber_value bug --- gerber/tests/test_utils.py | 3 +++ 1 file changed, 3 insertions(+) (limited to 'gerber/tests/test_utils.py') diff --git a/gerber/tests/test_utils.py b/gerber/tests/test_utils.py index 1c3f1e5..fe9b2e6 100644 --- a/gerber/tests/test_utils.py +++ b/gerber/tests/test_utils.py @@ -37,6 +37,9 @@ def test_zero_suppression(): assert_equal(value, parse_gerber_value(string, fmt, zero_suppression)) assert_equal(string, write_gerber_value(value, fmt, zero_suppression)) + assert_equal(write_gerber_value(0.000000001, fmt, 'leading'), '0') + assert_equal(write_gerber_value(0.000000001, fmt, 'trailing'), '0') + def test_format(): """ Test gerber value parser and writer handle format correctly -- cgit From 5476da8aa3f4ee424f56f4f2491e7af1c4b7b758 Mon Sep 17 00:00:00 2001 From: Hamilton Kibbe Date: Thu, 21 Jan 2016 03:57:44 -0500 Subject: Fix a bunch of rendering bugs. - 'clear' polarity primitives no longer erase background - Added aperture macro support for polygons - Added aperture macro rendring support - Renderer now creates a new surface for each layer and merges them instead of working directly on a single surface - Updated examples accordingly --- gerber/tests/test_utils.py | 25 +++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-) (limited to 'gerber/tests/test_utils.py') diff --git a/gerber/tests/test_utils.py b/gerber/tests/test_utils.py index fe9b2e6..35f6f47 100644 --- a/gerber/tests/test_utils.py +++ b/gerber/tests/test_utils.py @@ -52,7 +52,7 @@ def test_format(): ((2, 6), '-1', -0.000001), ((2, 5), '-1', -0.00001), ((2, 4), '-1', -0.0001), ((2, 3), '-1', -0.001), ((2, 2), '-1', -0.01), ((2, 1), '-1', -0.1), - ((2, 6), '0', 0) ] + ((2, 6), '0', 0)] for fmt, string, value in test_cases: assert_equal(value, parse_gerber_value(string, fmt, zero_suppression)) assert_equal(string, write_gerber_value(value, fmt, zero_suppression)) @@ -76,7 +76,7 @@ def test_decimal_truncation(): value = 1.123456789 for x in range(10): result = decimal_string(value, precision=x) - calculated = '1.' + ''.join(str(y) for y in range(1,x+1)) + calculated = '1.' + ''.join(str(y) for y in range(1, x + 1)) assert_equal(result, calculated) @@ -96,25 +96,34 @@ def test_parse_format_validation(): """ assert_raises(ValueError, parse_gerber_value, '00001111', (7, 5)) assert_raises(ValueError, parse_gerber_value, '00001111', (5, 8)) - assert_raises(ValueError, parse_gerber_value, '00001111', (13,1)) - + assert_raises(ValueError, parse_gerber_value, '00001111', (13, 1)) + + def test_write_format_validation(): """ Test write_gerber_value() format validation """ assert_raises(ValueError, write_gerber_value, 69.0, (7, 5)) assert_raises(ValueError, write_gerber_value, 69.0, (5, 8)) - assert_raises(ValueError, write_gerber_value, 69.0, (13,1)) + assert_raises(ValueError, write_gerber_value, 69.0, (13, 1)) def test_detect_format_with_short_file(): """ Verify file format detection works with short files """ assert_equal('unknown', detect_file_format('gerber/tests/__init__.py')) - + + def test_validate_coordinates(): assert_raises(TypeError, validate_coordinates, 3) assert_raises(TypeError, validate_coordinates, 3.1) assert_raises(TypeError, validate_coordinates, '14') assert_raises(TypeError, validate_coordinates, (0,)) - assert_raises(TypeError, validate_coordinates, (0,1,2)) - assert_raises(TypeError, validate_coordinates, (0,'string')) + assert_raises(TypeError, validate_coordinates, (0, 1, 2)) + assert_raises(TypeError, validate_coordinates, (0, 'string')) + + +def test_convex_hull(): + points = [(0, 0), (1, 0), (1, 1), (0.5, 0.5), (0, 1), (0, 0)] + expected = [(0, 0), (1, 0), (1, 1), (0, 1), (0, 0)] + assert_equal(set(convex_hull(points)), set(expected)) + \ No newline at end of file -- 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_utils.py | 168 +++++++++++++++++++++++++++------------------ 1 file changed, 103 insertions(+), 65 deletions(-) (limited to 'gerber/tests/test_utils.py') diff --git a/gerber/tests/test_utils.py b/gerber/tests/test_utils.py index 35f6f47..68484d1 100644 --- a/gerber/tests/test_utils.py +++ b/gerber/tests/test_utils.py @@ -3,7 +3,7 @@ # Author: Hamilton Kibbe -from .tests import assert_equal, assert_raises +import pytest from ..utils import * @@ -14,60 +14,99 @@ def test_zero_suppression(): fmt = (2, 5) # Test leading zero suppression - zero_suppression = 'leading' - test_cases = [('1', 0.00001), ('10', 0.0001), ('100', 0.001), - ('1000', 0.01), ('10000', 0.1), ('100000', 1.0), - ('1000000', 10.0), ('-1', -0.00001), ('-10', -0.0001), - ('-100', -0.001), ('-1000', -0.01), ('-10000', -0.1), - ('-100000', -1.0), ('-1000000', -10.0), - ('0', 0.0)] + zero_suppression = "leading" + test_cases = [ + ("1", 0.00001), + ("10", 0.0001), + ("100", 0.001), + ("1000", 0.01), + ("10000", 0.1), + ("100000", 1.0), + ("1000000", 10.0), + ("-1", -0.00001), + ("-10", -0.0001), + ("-100", -0.001), + ("-1000", -0.01), + ("-10000", -0.1), + ("-100000", -1.0), + ("-1000000", -10.0), + ("0", 0.0), + ] for string, value in test_cases: - assert_equal(value, parse_gerber_value(string, fmt, zero_suppression)) - assert_equal(string, write_gerber_value(value, fmt, zero_suppression)) + assert value == parse_gerber_value(string, fmt, zero_suppression) + assert string == write_gerber_value(value, fmt, zero_suppression) # Test trailing zero suppression - zero_suppression = 'trailing' - test_cases = [('1', 10.0), ('01', 1.0), ('001', 0.1), ('0001', 0.01), - ('00001', 0.001), ('000001', 0.0001), - ('0000001', 0.00001), ('-1', -10.0), ('-01', -1.0), - ('-001', -0.1), ('-0001', -0.01), ('-00001', -0.001), - ('-000001', -0.0001), ('-0000001', -0.00001), - ('0', 0.0)] + zero_suppression = "trailing" + test_cases = [ + ("1", 10.0), + ("01", 1.0), + ("001", 0.1), + ("0001", 0.01), + ("00001", 0.001), + ("000001", 0.0001), + ("0000001", 0.00001), + ("-1", -10.0), + ("-01", -1.0), + ("-001", -0.1), + ("-0001", -0.01), + ("-00001", -0.001), + ("-000001", -0.0001), + ("-0000001", -0.00001), + ("0", 0.0), + ] for string, value in test_cases: - assert_equal(value, parse_gerber_value(string, fmt, zero_suppression)) - assert_equal(string, write_gerber_value(value, fmt, zero_suppression)) + assert value == parse_gerber_value(string, fmt, zero_suppression) + assert string == write_gerber_value(value, fmt, zero_suppression) - assert_equal(write_gerber_value(0.000000001, fmt, 'leading'), '0') - assert_equal(write_gerber_value(0.000000001, fmt, 'trailing'), '0') + assert write_gerber_value(0.000000001, fmt, "leading") == "0" + assert write_gerber_value(0.000000001, fmt, "trailing") == "0" def test_format(): """ Test gerber value parser and writer handle format correctly """ - zero_suppression = 'leading' - test_cases = [((2, 7), '1', 0.0000001), ((2, 6), '1', 0.000001), - ((2, 5), '1', 0.00001), ((2, 4), '1', 0.0001), - ((2, 3), '1', 0.001), ((2, 2), '1', 0.01), - ((2, 1), '1', 0.1), ((2, 7), '-1', -0.0000001), - ((2, 6), '-1', -0.000001), ((2, 5), '-1', -0.00001), - ((2, 4), '-1', -0.0001), ((2, 3), '-1', -0.001), - ((2, 2), '-1', -0.01), ((2, 1), '-1', -0.1), - ((2, 6), '0', 0)] + zero_suppression = "leading" + test_cases = [ + ((2, 7), "1", 0.0000001), + ((2, 6), "1", 0.000001), + ((2, 5), "1", 0.00001), + ((2, 4), "1", 0.0001), + ((2, 3), "1", 0.001), + ((2, 2), "1", 0.01), + ((2, 1), "1", 0.1), + ((2, 7), "-1", -0.0000001), + ((2, 6), "-1", -0.000001), + ((2, 5), "-1", -0.00001), + ((2, 4), "-1", -0.0001), + ((2, 3), "-1", -0.001), + ((2, 2), "-1", -0.01), + ((2, 1), "-1", -0.1), + ((2, 6), "0", 0), + ] for fmt, string, value in test_cases: - assert_equal(value, parse_gerber_value(string, fmt, zero_suppression)) - assert_equal(string, write_gerber_value(value, fmt, zero_suppression)) - - zero_suppression = 'trailing' - test_cases = [((6, 5), '1', 100000.0), ((5, 5), '1', 10000.0), - ((4, 5), '1', 1000.0), ((3, 5), '1', 100.0), - ((2, 5), '1', 10.0), ((1, 5), '1', 1.0), - ((6, 5), '-1', -100000.0), ((5, 5), '-1', -10000.0), - ((4, 5), '-1', -1000.0), ((3, 5), '-1', -100.0), - ((2, 5), '-1', -10.0), ((1, 5), '-1', -1.0), - ((2, 5), '0', 0)] + assert value == parse_gerber_value(string, fmt, zero_suppression) + assert string == write_gerber_value(value, fmt, zero_suppression) + + zero_suppression = "trailing" + test_cases = [ + ((6, 5), "1", 100000.0), + ((5, 5), "1", 10000.0), + ((4, 5), "1", 1000.0), + ((3, 5), "1", 100.0), + ((2, 5), "1", 10.0), + ((1, 5), "1", 1.0), + ((6, 5), "-1", -100000.0), + ((5, 5), "-1", -10000.0), + ((4, 5), "-1", -1000.0), + ((3, 5), "-1", -100.0), + ((2, 5), "-1", -10.0), + ((1, 5), "-1", -1.0), + ((2, 5), "0", 0), + ] for fmt, string, value in test_cases: - assert_equal(value, parse_gerber_value(string, fmt, zero_suppression)) - assert_equal(string, write_gerber_value(value, fmt, zero_suppression)) + assert value == parse_gerber_value(string, fmt, zero_suppression) + assert string == write_gerber_value(value, fmt, zero_suppression) def test_decimal_truncation(): @@ -76,54 +115,53 @@ def test_decimal_truncation(): value = 1.123456789 for x in range(10): result = decimal_string(value, precision=x) - calculated = '1.' + ''.join(str(y) for y in range(1, x + 1)) - assert_equal(result, calculated) + calculated = "1." + "".join(str(y) for y in range(1, x + 1)) + assert result == calculated def test_decimal_padding(): """ Test decimal_string padding """ value = 1.123 - assert_equal(decimal_string(value, precision=3, padding=True), '1.123') - assert_equal(decimal_string(value, precision=4, padding=True), '1.1230') - assert_equal(decimal_string(value, precision=5, padding=True), '1.12300') - assert_equal(decimal_string(value, precision=6, padding=True), '1.123000') - assert_equal(decimal_string(0, precision=6, padding=True), '0.000000') + assert decimal_string(value, precision=3, padding=True) == "1.123" + assert decimal_string(value, precision=4, padding=True) == "1.1230" + assert decimal_string(value, precision=5, padding=True) == "1.12300" + assert decimal_string(value, precision=6, padding=True) == "1.123000" + assert decimal_string(0, precision=6, padding=True) == "0.000000" def test_parse_format_validation(): """ Test parse_gerber_value() format validation """ - assert_raises(ValueError, parse_gerber_value, '00001111', (7, 5)) - assert_raises(ValueError, parse_gerber_value, '00001111', (5, 8)) - assert_raises(ValueError, parse_gerber_value, '00001111', (13, 1)) + pytest.raises(ValueError, parse_gerber_value, "00001111", (7, 5)) + pytest.raises(ValueError, parse_gerber_value, "00001111", (5, 8)) + pytest.raises(ValueError, parse_gerber_value, "00001111", (13, 1)) def test_write_format_validation(): """ Test write_gerber_value() format validation """ - assert_raises(ValueError, write_gerber_value, 69.0, (7, 5)) - assert_raises(ValueError, write_gerber_value, 69.0, (5, 8)) - assert_raises(ValueError, write_gerber_value, 69.0, (13, 1)) + pytest.raises(ValueError, write_gerber_value, 69.0, (7, 5)) + pytest.raises(ValueError, write_gerber_value, 69.0, (5, 8)) + pytest.raises(ValueError, write_gerber_value, 69.0, (13, 1)) def test_detect_format_with_short_file(): """ Verify file format detection works with short files """ - assert_equal('unknown', detect_file_format('gerber/tests/__init__.py')) + assert "unknown" == detect_file_format("gerber/tests/__init__.py") def test_validate_coordinates(): - assert_raises(TypeError, validate_coordinates, 3) - assert_raises(TypeError, validate_coordinates, 3.1) - assert_raises(TypeError, validate_coordinates, '14') - assert_raises(TypeError, validate_coordinates, (0,)) - assert_raises(TypeError, validate_coordinates, (0, 1, 2)) - assert_raises(TypeError, validate_coordinates, (0, 'string')) + pytest.raises(TypeError, validate_coordinates, 3) + pytest.raises(TypeError, validate_coordinates, 3.1) + pytest.raises(TypeError, validate_coordinates, "14") + pytest.raises(TypeError, validate_coordinates, (0,)) + pytest.raises(TypeError, validate_coordinates, (0, 1, 2)) + pytest.raises(TypeError, validate_coordinates, (0, "string")) def test_convex_hull(): points = [(0, 0), (1, 0), (1, 1), (0.5, 0.5), (0, 1), (0, 0)] expected = [(0, 0), (1, 0), (1, 1), (0, 1), (0, 0)] - assert_equal(set(convex_hull(points)), set(expected)) - \ No newline at end of file + assert set(convex_hull(points)) == set(expected) -- cgit