test_yaml_construct_str_as_unicode.py 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. # -*- coding: utf-8 -*-
  2. import pytest
  3. import copy
  4. import ioex.datetimeex
  5. yaml = pytest.importorskip('yaml')
  6. @pytest.mark.parametrize(('loader'), [yaml.Loader, yaml.SafeLoader])
  7. @pytest.mark.parametrize(('yaml_string', 'expected_object'), [
  8. # ascii strings
  9. #
  10. # nota bene:
  11. # >>> yaml.dump('it\xc3\xa4m', allow_unicode = False) == '!!python/str "it\\xE4m"\n'
  12. # True
  13. # >>> yaml.dump('it\xc3\xa4m', allow_unicode = True) == "!!python/str 'it\xc3\xa4m'\n"
  14. # True
  15. ['item', u'item'],
  16. ['itäm', u'itäm'],
  17. ['it\xc3\xa4m', u'itäm'],
  18. ['"it\xc3\xa4m"', u'itäm'],
  19. [r'it\xE4m', ur'it\xE4m'],
  20. ['"itäm"', u'itäm'],
  21. [r'"it\xc3\xa4m"', u'it\xc3\xa4m'], # see comment above
  22. # unicode strings
  23. [r'"it\xE4m"', u'it\xE4m'],
  24. [r'"it\xE4m"', u'itäm'],
  25. [u'item', u'item'],
  26. [u'itäm', u'itäm'],
  27. ['{kĕyĭ: 可以}\n', {u'kĕyĭ': u'可以'}],
  28. ['{⚕: ☤}\n', {u'⚕': u'☤'}],
  29. # lists
  30. ['[item]', [u'item']],
  31. ['[itäm]', [u'itäm']],
  32. # dicts
  33. ['{key: value}', {u'key': u'value'}],
  34. ['{kï: valü}', {u'kï': u'valü'}],
  35. ])
  36. def test_to_yaml(yaml_string, expected_object, loader):
  37. loader_copy = copy.deepcopy(loader)
  38. ioex.register_yaml_str_as_unicode_constructor(loader_copy)
  39. generated_object = yaml.load(yaml_string, Loader = loader_copy)
  40. assert type(expected_object) == type(generated_object)
  41. assert expected_object == generated_object
  42. if type(expected_object) is list:
  43. assert [type(i) for i in expected_object] == [type(i) for i in generated_object]
  44. elif type(expected_object) is dict:
  45. assert [type(k) for k in expected_object.keys()] == [type(k) for k in generated_object.keys()]
  46. assert [type(v) for v in expected_object.values()] == [type(v) for v in generated_object.values()]