test_intranges.py 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. #!/usr/bin/env python
  2. import unittest
  3. from idna.intranges import intranges_from_list, intranges_contain, _encode_range
  4. class IntrangeTests(unittest.TestCase):
  5. def test_ranging(self):
  6. self.assertEqual(
  7. intranges_from_list(list(range(293, 499)) + list(range(4888, 9876))),
  8. (_encode_range(293, 499), _encode_range(4888, 9876),)
  9. )
  10. def test_ranging_2(self):
  11. self.assertEqual(
  12. intranges_from_list([111]),
  13. (_encode_range(111, 112),)
  14. )
  15. def test_skips(self):
  16. self.assertEqual(
  17. intranges_from_list([0, 2, 4, 6, 9, 10, 11, 13, 15,]),
  18. (
  19. _encode_range(0, 1),
  20. _encode_range(2, 3),
  21. _encode_range(4, 5),
  22. _encode_range(6, 7),
  23. _encode_range(9, 12),
  24. _encode_range(13, 14),
  25. _encode_range(15, 16),
  26. )
  27. )
  28. def test_empty_range(self):
  29. self.assertEqual(
  30. intranges_from_list([]),
  31. ()
  32. )
  33. class IntrangeContainsTests(unittest.TestCase):
  34. def _test_containment(self, ints, disjoint_ints):
  35. ranges = intranges_from_list(ints)
  36. for int_ in ints:
  37. assert intranges_contain(int_, ranges)
  38. for int_ in disjoint_ints:
  39. assert not intranges_contain(int_, ranges)
  40. def test_simple(self):
  41. self._test_containment(range(10, 20), [2, 3, 68, 3893])
  42. def test_skips(self):
  43. self._test_containment(
  44. [0, 2, 4, 6, 9, 10, 11, 13, 15,],
  45. [-1, 1, 3, 5, 7, 4898]
  46. )
  47. def test_singleton(self):
  48. self._test_containment([111], [110, 112])
  49. def test_empty(self):
  50. self._test_containment([], range(100))