diff --git a/pygcode/words.py b/pygcode/words.py index e8729fb..7f38c5e 100644 --- a/pygcode/words.py +++ b/pygcode/words.py @@ -4,9 +4,11 @@ import six from .exceptions import GCodeBlockFormatError -FLOAT_REGEX = re.compile(r'^-?\d+(\.\d+)?') +FLOAT_REGEX = re.compile(r'^-?(\d+\.?\d*|\.\d+)') # testcase: ..tests.test_words.WordValueMatchTests.test_float INT_REGEX = re.compile(r'^-?\d+') POSITIVEINT_REGEX = re.compile(r'^\d+') +CODE_REGEX = re.compile(r'^\d+(\.\d)?') # similar + WORD_MAP = { # Descriptions copied from wikipedia: @@ -47,7 +49,7 @@ WORD_MAP = { # G-Codes 'G': { 'class': float, - 'value_regex': re.compile(r'^\d+(\.\d)?'), + 'value_regex': CODE_REGEX, 'description': "Address for preparatory commands", }, # Tool Offsets @@ -81,7 +83,7 @@ WORD_MAP = { # Miscellaneous Function 'M': { 'class': float, - 'value_regex': re.compile(r'^\d+(\.\d)?'), + 'value_regex': CODE_REGEX, 'description': "Miscellaneous function", }, # Line Number diff --git a/tests/test_words.py b/tests/test_words.py index a5ffe95..e42e8ef 100644 --- a/tests/test_words.py +++ b/tests/test_words.py @@ -40,3 +40,44 @@ class WordIterTests(unittest.TestCase): self.assertEqual([w[3].letter, w[3].value], ['I', -0.11]) self.assertEqual([w[4].letter, w[4].value], ['J', -1.26]) self.assertEqual([w[5].letter, w[5].value], ['F', 70]) + + +class WordValueMatchTests(unittest.TestCase): + + def regex_assertions(self, regex, positive_list, negative_list): + # Assert all elements of positive_list match regex + for (value_str, expected_match) in positive_list: + match = regex.search(value_str) + self.assertIsNotNone(match, "failed to match '%s'" % value_str) + self.assertEqual(match.group(), expected_match) + + # Asesrt all elements of negative_list do not match regex + for value_str in negative_list: + match = regex.search(value_str) + self.assertIsNone(match, "matched for '%s'" % value_str) + + def test_float(self): + self.regex_assertions( + regex=words.FLOAT_REGEX, + positive_list=[ + ('1.2', '1.2'), ('1', '1'), ('200', '200'), ('0092', '0092'), + ('1.', '1.'), ('.2', '.2'), ('-1.234', '-1.234'), + ('-1.', '-1.'), ('-.289', '-.289'), + # error cases (only detectable in gcode context) + ('1.2e3', '1.2'), + ], + negative_list=['.', ' 1.2'] + ) + + def test_code(self): + self.regex_assertions( + regex=words.CODE_REGEX, + positive_list=[ + ('1.2', '1.2'), ('1', '1'), ('10', '10'), + ('02', '02'), ('02.3', '02.3'), + ('1.', '1'), ('03 ', '03'), + # error cases (only detectable in gcode context) + ('30.12', '30.1'), + ], + negative_list=['.2', '.', ' 2'] + )