Unittests for Excellon number parser.
This commit is contained in:
14
camlib.py
14
camlib.py
@@ -1762,6 +1762,9 @@ class Excellon(Geometry):
|
|||||||
|
|
||||||
self.drills = []
|
self.drills = []
|
||||||
|
|
||||||
|
## IN|MM -> Units are inherited from Geometry
|
||||||
|
#self.units = units
|
||||||
|
|
||||||
# Trailing "T" or leading "L" (default)
|
# Trailing "T" or leading "L" (default)
|
||||||
#self.zeros = "T"
|
#self.zeros = "T"
|
||||||
self.zeros = zeros
|
self.zeros = zeros
|
||||||
@@ -2011,7 +2014,12 @@ class Excellon(Geometry):
|
|||||||
# If less than size digits, they are automatically added,
|
# If less than size digits, they are automatically added,
|
||||||
# 5 digits then are divided by 10^3 and so on.
|
# 5 digits then are divided by 10^3 and so on.
|
||||||
match = self.leadingzeros_re.search(number_str)
|
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
|
else: # Trailing
|
||||||
# You must show all zeros to the right of the number and can omit
|
# 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.
|
# of digits you typed and automatically fill in the missing zeros.
|
||||||
if self.units.lower() == "in": # Inches is 00.0000
|
if self.units.lower() == "in": # Inches is 00.0000
|
||||||
return float(number_str) / 10000
|
return float(number_str) / 10000
|
||||||
|
else:
|
||||||
return float(number_str) / 1000 # Metric is 000.000
|
return float(number_str) / 1000 # Metric is 000.000
|
||||||
|
|
||||||
def create_geometry(self):
|
def create_geometry(self):
|
||||||
"""
|
"""
|
||||||
|
|||||||
93
tests/test_excellon.py
Normal file
93
tests/test_excellon.py
Normal 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()
|
||||||
Reference in New Issue
Block a user