test_yaml.py 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. # -*- coding: utf-8 -*-
  2. # Import Python Libs
  3. from __future__ import absolute_import, print_function, unicode_literals
  4. import collections
  5. import textwrap
  6. # Import Salt Testing libs
  7. from tests.support.mixins import LoaderModuleMockMixin
  8. from tests.support.unit import TestCase
  9. from tests.support.mock import (
  10. patch
  11. )
  12. # Import Salt libs
  13. import salt.renderers.yaml as yaml
  14. from salt.ext import six
  15. class YAMLRendererTestCase(TestCase, LoaderModuleMockMixin):
  16. def setup_loader_modules(self):
  17. return {yaml: {}}
  18. def assert_unicode(self, value):
  19. '''
  20. Make sure the entire data structure is unicode
  21. '''
  22. if six.PY3:
  23. return
  24. if isinstance(value, six.string_types):
  25. if not isinstance(value, six.text_type):
  26. self.raise_error(value)
  27. elif isinstance(value, collections.Mapping):
  28. for k, v in six.iteritems(value):
  29. self.assert_unicode(k)
  30. self.assert_unicode(v)
  31. elif isinstance(value, collections.Iterable):
  32. for item in value:
  33. self.assert_unicode(item)
  34. def assert_matches(self, ret, expected):
  35. self.assertEqual(ret, expected)
  36. self.assert_unicode(ret)
  37. def test_yaml_render_string(self):
  38. data = 'string'
  39. result = yaml.render(data)
  40. self.assertEqual(result, data)
  41. def test_yaml_render_unicode(self):
  42. data = '!!python/unicode python unicode string'
  43. result = yaml.render(data)
  44. self.assertEqual(result, u'python unicode string')
  45. def test_yaml_render_old_unicode(self):
  46. config = {'use_yamlloader_old': True}
  47. with patch.dict(yaml.__opts__, config): # pylint: disable=no-member
  48. self.assert_matches(
  49. yaml.render(textwrap.dedent('''\
  50. foo:
  51. a: Д
  52. b: {'a': u'\\u0414'}''')),
  53. {'foo': {'a': u'\u0414', 'b': {'a': u'\u0414'}}}
  54. )