Unittests for Excellon number parser.

This commit is contained in:
jpcaram 2015-01-11 19:39:01 -05:00
parent 1b154ab057
commit 3bedda2aeb
2 changed files with 104 additions and 3 deletions

View File

@ -1762,6 +1762,9 @@ class Excellon(Geometry):
self.drills = []
## IN|MM -> Units are inherited from Geometry
#self.units = units
# Trailing "T" or leading "L" (default)
#self.zeros = "T"
self.zeros = zeros
@ -2011,7 +2014,12 @@ class Excellon(Geometry):
# If less than size digits, they are automatically added,
# 5 digits then are divided by 10^3 and so on.
match = self.leadingzeros_re.search(number_str)
return float(number_str) / (10 ** (len(match.group(1)) + len(match.group(2)) - 2))
if self.units.lower() == "in":
return float(number_str) / \
(10 ** (len(match.group(1)) + len(match.group(2)) - 2))
else:
return float(number_str) / \
(10 ** (len(match.group(1)) + len(match.group(2)) - 3))
else: # Trailing
# You must show all zeros to the right of the number and can omit
@ -2019,8 +2027,8 @@ class Excellon(Geometry):
# of digits you typed and automatically fill in the missing zeros.
if self.units.lower() == "in": # Inches is 00.0000
return float(number_str) / 10000
return float(number_str) / 1000 # Metric is 000.000
else:
return float(number_str) / 1000 # Metric is 000.000
def create_geometry(self):
"""

93
tests/test_excellon.py Normal file
View File

@ -0,0 +1,93 @@
import unittest
import camlib
class ExcellonNumberParseTestInch(unittest.TestCase):
def test_inch_leading_6digit(self):
excellon = camlib.Excellon()
self.assertEqual(excellon.zeros, "L")
self.assertEqual(excellon.parse_number("123456"), 12.3456)
def test_inch_leading_5digit(self):
excellon = camlib.Excellon()
self.assertEqual(excellon.parse_number("12345"), 12.345)
def test_inch_leading_15digit(self):
excellon = camlib.Excellon()
self.assertEqual(excellon.parse_number("012345"), 1.2345)
def test_inch_leading_51digit(self):
excellon = camlib.Excellon()
self.assertEqual(excellon.parse_number("123450"), 12.345)
def test_inch_trailing_6digit(self):
excellon = camlib.Excellon()
excellon.zeros = "T"
self.assertEqual(excellon.parse_number("123456"), 12.3456)
def test_inch_trailing_5digit(self):
excellon = camlib.Excellon()
excellon.zeros = "T"
self.assertEqual(excellon.parse_number("12345"), 1.2345)
def test_inch_trailing_15digit(self):
excellon = camlib.Excellon()
excellon.zeros = "T"
self.assertEqual(excellon.parse_number("012345"), 1.2345)
def test_inch_trailing_51digit(self):
excellon = camlib.Excellon()
excellon.zeros = "T"
self.assertEqual(excellon.parse_number("123450"), 12.345)
class ExcellonNumberParseTestMetric(unittest.TestCase):
def test_inch_leading_6digit(self):
excellon = camlib.Excellon()
excellon.units = "mm"
self.assertEqual(excellon.parse_number("123456"), 123.456)
def test_inch_leading_5digit(self):
excellon = camlib.Excellon()
excellon.units = "mm"
self.assertEqual(excellon.parse_number("12345"), 123.45)
def test_inch_leading_15digit(self):
excellon = camlib.Excellon()
excellon.units = "mm"
self.assertEqual(excellon.parse_number("012345"), 12.345)
def test_inch_leading_51digit(self):
excellon = camlib.Excellon()
excellon.units = "mm"
self.assertEqual(excellon.parse_number("123450"), 123.45)
def test_inch_trailing_6digit(self):
excellon = camlib.Excellon()
excellon.units = "mm"
excellon.zeros = "T"
self.assertEqual(excellon.parse_number("123456"), 123.456)
def test_inch_trailing_5digit(self):
excellon = camlib.Excellon()
excellon.units = "mm"
excellon.zeros = "T"
self.assertEqual(excellon.parse_number("12345"), 12.345)
def test_inch_trailing_15digit(self):
excellon = camlib.Excellon()
excellon.units = "mm"
excellon.zeros = "T"
self.assertEqual(excellon.parse_number("012345"), 12.345)
def test_inch_trailing_51digit(self):
excellon = camlib.Excellon()
excellon.units = "mm"
excellon.zeros = "T"
self.assertEqual(excellon.parse_number("123450"), 123.45)
if __name__ == '__main__':
unittest.main()