]> git.ipfire.org Git - thirdparty/dnspython.git/commitdiff
Simplify $GENERATE range code, add some error checks, and increase test coverage.
authorBob Halley <halley@dnspython.org>
Sun, 9 Aug 2020 01:32:55 +0000 (18:32 -0700)
committerBob Halley <halley@dnspython.org>
Sun, 9 Aug 2020 01:32:55 +0000 (18:32 -0700)
dns/grange.py
tests/test_grange.py

index ffe8be7c4690ef1f9afdf82eddaad129a6ef4c2e..112ede47c432b40d927a880a69c99309576aa54d 100644 (file)
@@ -28,11 +28,12 @@ def from_text(text):
     Returns a tuple of three ``int`` values ``(start, stop, step)``.
     """
 
-    # TODO, figure out the bounds on start, stop and step.
+    start = -1
+    stop = -1
     step = 1
     cur = ''
     state = 0
-    # state   0 1 2 3 4
+    # state   0   1   2
     #         x - y / z
 
     if text and text[0] == '-':
@@ -42,28 +43,27 @@ def from_text(text):
         if c == '-' and state == 0:
             start = int(cur)
             cur = ''
-            state = 2
+            state = 1
         elif c == '/':
             stop = int(cur)
             cur = ''
-            state = 4
+            state = 2
         elif c.isdigit():
             cur += c
         else:
             raise dns.exception.SyntaxError("Could not parse %s" % (c))
 
-    if state in (1, 3):
-        raise dns.exception.SyntaxError()
-
-    if state == 2:
+    if state == 0:
+        raise dns.exception.SyntaxError("no stop value specified")
+    elif state == 1:
         stop = int(cur)
-
-    if state == 4:
+    else:
+        assert state == 2
         step = int(cur)
 
     assert step >= 1
     assert start >= 0
-    assert start <= stop
-    # TODO, can start == stop?
+    if start > stop:
+        raise dns.exception.SyntaxError('start must be <= stop')
 
     return (start, stop, step)
index d52b8556c8153ecc891fd930c2e1c4a44f5daa88..9b5ddd2469f35a6e04313ee382bfb9102ed01260 100644 (file)
@@ -64,28 +64,30 @@ class GRangeTestCase(unittest.TestCase):
         self.assertEqual(step, 77)
 
     def testFailFromText1(self):
-        def bad():
+        with self.assertRaises(dns.exception.SyntaxError):
             start = 2
             stop = 1
             step = 1
             dns.grange.from_text('%d-%d/%d' % (start, stop, step))
-        self.assertRaises(AssertionError, bad)
+            self.assertTrue(False)
 
     def testFailFromText2(self):
-        def bad():
+        with self.assertRaises(dns.exception.SyntaxError):
             start = '-1'
             stop = 3
             step = 1
             dns.grange.from_text('%s-%d/%d' % (start, stop, step))
-        self.assertRaises(dns.exception.SyntaxError, bad)
 
     def testFailFromText3(self):
-        def bad():
+        with self.assertRaises(dns.exception.SyntaxError):
             start = 1
             stop = 4
             step = '-2'
             dns.grange.from_text('%d-%d/%s' % (start, stop, step))
-        self.assertRaises(dns.exception.SyntaxError, bad)
+
+    def testFailFromText4(self):
+        with self.assertRaises(dns.exception.SyntaxError):
+            dns.grange.from_text('1')
 
 if __name__ == '__main__':
     unittest.main()