summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBob Halley <halley@dnspython.org>2020-08-08 18:32:55 -0700
committerBob Halley <halley@dnspython.org>2020-08-08 18:32:55 -0700
commit26fd19690c44a01c84c27a2d4244d2e5dc7b7a19 (patch)
tree360b56b8f680499588e76af121ab241547c069b1
parent9eeb0aa296c3a95ab78cf9abce9768aa19cdb71b (diff)
downloaddnspython-26fd19690c44a01c84c27a2d4244d2e5dc7b7a19.tar.gz
Simplify $GENERATE range code, add some error checks, and increase test coverage.
-rw-r--r--dns/grange.py24
-rw-r--r--tests/test_grange.py14
2 files changed, 20 insertions, 18 deletions
diff --git a/dns/grange.py b/dns/grange.py
index ffe8be7..112ede4 100644
--- a/dns/grange.py
+++ b/dns/grange.py
@@ -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)
diff --git a/tests/test_grange.py b/tests/test_grange.py
index d52b855..9b5ddd2 100644
--- a/tests/test_grange.py
+++ b/tests/test_grange.py
@@ -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()