test__compat.py 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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 Testing libs
  10. from tests.support.unit import TestCase
  11. # Import Salt libs
  12. import salt._compat as compat
  13. # Import 3rd Party libs
  14. from salt.ext.six import binary_type, text_type
  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(u'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')