test_yamlencoding.py 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. # -*- coding: utf-8 -*-
  2. """
  3. Tests for salt.utils.yamlencoding
  4. """
  5. # Import Python libs
  6. from __future__ import absolute_import, print_function, unicode_literals
  7. # Import Salt libs
  8. import salt.utils.yaml
  9. import salt.utils.yamlencoding
  10. from tests.support.unit import TestCase
  11. class YamlEncodingTestCase(TestCase):
  12. def test_yaml_dquote(self):
  13. for teststr in (r'"\ []{}"',):
  14. self.assertEqual(
  15. teststr,
  16. salt.utils.yaml.safe_load(salt.utils.yamlencoding.yaml_dquote(teststr)),
  17. )
  18. def test_yaml_dquote_doesNotAddNewLines(self):
  19. teststr = '"' * 100
  20. self.assertNotIn("\n", salt.utils.yamlencoding.yaml_dquote(teststr))
  21. def test_yaml_squote(self):
  22. ret = salt.utils.yamlencoding.yaml_squote(r'"')
  23. self.assertEqual(ret, r"""'"'""")
  24. def test_yaml_squote_doesNotAddNewLines(self):
  25. teststr = "'" * 100
  26. self.assertNotIn("\n", salt.utils.yamlencoding.yaml_squote(teststr))
  27. def test_yaml_encode(self):
  28. for testobj in (
  29. None,
  30. True,
  31. False,
  32. "[7, 5]",
  33. '"monkey"',
  34. 5,
  35. 7.5,
  36. "2014-06-02 15:30:29.7",
  37. ):
  38. self.assertEqual(
  39. testobj,
  40. salt.utils.yaml.safe_load(salt.utils.yamlencoding.yaml_encode(testobj)),
  41. )
  42. for testobj in ({}, [], set()):
  43. self.assertRaises(TypeError, salt.utils.yamlencoding.yaml_encode, testobj)