| 1 |
# Copyright (C) Dnspython Contributors, see LICENSE for text of ISC license |
| 2 |
|
| 3 |
# Copyright (C) 2003-2007, 2009-2011 Nominum, Inc. |
| 4 |
# |
| 5 |
# Permission to use, copy, modify, and distribute this software and its |
| 6 |
# documentation for any purpose with or without fee is hereby granted, |
| 7 |
# provided that the above copyright notice and this permission notice |
| 8 |
# appear in all copies. |
| 9 |
# |
| 10 |
# THE SOFTWARE IS PROVIDED "AS IS" AND NOMINUM DISCLAIMS ALL WARRANTIES |
| 11 |
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
| 12 |
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL NOMINUM BE LIABLE FOR |
| 13 |
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 14 |
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 15 |
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT |
| 16 |
# OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 17 |
|
| 18 |
import sys |
| 19 |
|
| 20 |
sys.path.insert(0, "../") |
| 21 |
|
| 22 |
import unittest |
| 23 |
|
| 24 |
import dns |
| 25 |
import dns.exception |
| 26 |
import dns.grange |
| 27 |
|
| 28 |
|
| 29 |
class GRangeTestCase(unittest.TestCase): |
| 30 |
def testFromText1(self): |
| 31 |
start, stop, step = dns.grange.from_text("1-1") |
| 32 |
self.assertEqual(start, 1) |
| 33 |
self.assertEqual(stop, 1) |
| 34 |
self.assertEqual(step, 1) |
| 35 |
|
| 36 |
def testFromText2(self): |
| 37 |
start, stop, step = dns.grange.from_text("1-4") |
| 38 |
self.assertEqual(start, 1) |
| 39 |
self.assertEqual(stop, 4) |
| 40 |
self.assertEqual(step, 1) |
| 41 |
|
| 42 |
def testFromText3(self): |
| 43 |
start, stop, step = dns.grange.from_text("4-255") |
| 44 |
self.assertEqual(start, 4) |
| 45 |
self.assertEqual(stop, 255) |
| 46 |
self.assertEqual(step, 1) |
| 47 |
|
| 48 |
def testFromText4(self): |
| 49 |
start, stop, step = dns.grange.from_text("1-1/1") |
| 50 |
self.assertEqual(start, 1) |
| 51 |
self.assertEqual(stop, 1) |
| 52 |
self.assertEqual(step, 1) |
| 53 |
|
| 54 |
def testFromText5(self): |
| 55 |
start, stop, step = dns.grange.from_text("1-4/2") |
| 56 |
self.assertEqual(start, 1) |
| 57 |
self.assertEqual(stop, 4) |
| 58 |
self.assertEqual(step, 2) |
| 59 |
|
| 60 |
def testFromText6(self): |
| 61 |
start, stop, step = dns.grange.from_text("4-255/77") |
| 62 |
self.assertEqual(start, 4) |
| 63 |
self.assertEqual(stop, 255) |
| 64 |
self.assertEqual(step, 77) |
| 65 |
|
| 66 |
def testFailFromText1(self): |
| 67 |
with self.assertRaises(dns.exception.SyntaxError): |
| 68 |
start = 2 |
| 69 |
stop = 1 |
| 70 |
step = 1 |
| 71 |
dns.grange.from_text("%d-%d/%d" % (start, stop, step)) |
| 72 |
self.assertTrue(False) |
| 73 |
|
| 74 |
def testFailFromText2(self): |
| 75 |
with self.assertRaises(dns.exception.SyntaxError): |
| 76 |
start = "-1" |
| 77 |
stop = 3 |
| 78 |
step = 1 |
| 79 |
dns.grange.from_text("%s-%d/%d" % (start, stop, step)) |
| 80 |
|
| 81 |
def testFailFromText3(self): |
| 82 |
with self.assertRaises(dns.exception.SyntaxError): |
| 83 |
start = 1 |
| 84 |
stop = 4 |
| 85 |
step = "-2" |
| 86 |
dns.grange.from_text("%d-%d/%s" % (start, stop, step)) |
| 87 |
|
| 88 |
def testFailFromText4(self): |
| 89 |
with self.assertRaises(dns.exception.SyntaxError): |
| 90 |
dns.grange.from_text("1") |
| 91 |
|
| 92 |
|
| 93 |
if __name__ == "__main__": |
| 94 |
unittest.main() |