test__compat.py 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. # -*- coding: utf-8 -*-
  2. """
  3. Unit tests for salt._compat
  4. """
  5. # Import Python libs
  6. from __future__ import absolute_import, print_function, unicode_literals
  7. import logging
  8. import sys
  9. # Import Salt libs
  10. import salt._compat as compat
  11. # Import 3rd Party libs
  12. from salt.ext.six import binary_type, text_type
  13. # Import Salt Testing libs
  14. from tests.support.unit import TestCase
  15. log = logging.getLogger(__name__)
  16. PY3 = sys.version_info.major == 3
  17. class CompatTestCase(TestCase):
  18. def test_text(self):
  19. ret = compat.text_("test string")
  20. self.assertTrue(isinstance(ret, text_type))
  21. def test_text_binary(self):
  22. ret = compat.text_(b"test string")
  23. self.assertTrue(isinstance(ret, text_type))
  24. def test_bytes(self):
  25. ret = compat.bytes_("test string")
  26. self.assertTrue(isinstance(ret, binary_type))
  27. def test_bytes_binary(self):
  28. ret = compat.bytes_(b"test string")
  29. self.assertTrue(isinstance(ret, binary_type))
  30. def test_ascii_native(self):
  31. ret = compat.ascii_native_("test string")
  32. self.assertTrue(isinstance(ret, str))
  33. def test_ascii_native_binary(self):
  34. ret = compat.ascii_native_(b"test string")
  35. self.assertTrue(isinstance(ret, str))
  36. def test_native(self):
  37. ret = compat.native_("test string")
  38. self.assertTrue(isinstance(ret, str))
  39. def test_native_binary(self):
  40. ret = compat.native_(b"test string")
  41. self.assertTrue(isinstance(ret, str))
  42. def test_string_io(self):
  43. ret = compat.string_io("test string")
  44. if PY3:
  45. expected = "io.StringIO object"
  46. else:
  47. expected = "cStringIO.StringI object"
  48. self.assertTrue(expected in repr(ret))
  49. def test_string_io_unicode(self):
  50. ret = compat.string_io("test string \xf8")
  51. if PY3:
  52. expected = "io.StringIO object"
  53. else:
  54. expected = "StringIO.StringIO instance"
  55. self.assertTrue(expected in repr(ret))
  56. def test_ipv6_class__is_packed_binary(self):
  57. ipv6 = compat.IPv6AddressScoped("2001:db8::")
  58. self.assertEqual(str(ipv6), "2001:db8::")
  59. def test_ipv6_class__is_packed_binary_integer(self):
  60. ipv6 = compat.IPv6AddressScoped(42540766411282592856903984951653826560)
  61. self.assertEqual(str(ipv6), "2001:db8::")
  62. def test_ipv6_class__is_packed_binary__issue_51831(self):
  63. ipv6 = compat.IPv6AddressScoped(b"sixteen.digit.bn")
  64. self.assertEqual(str(ipv6), "7369:7874:6565:6e2e:6469:6769:742e:626e")