test_yaml_represent_unicode_as_str.py 1.3 KB

12345678910111213141516171819202122232425262728293031
  1. # -*- coding: utf-8 -*-
  2. import pytest
  3. import ioex.datetimeex
  4. yaml = pytest.importorskip('yaml')
  5. @pytest.mark.parametrize(('dumper'), [yaml.Dumper, yaml.SafeDumper])
  6. @pytest.mark.parametrize(('unicode_string', 'expected_yaml_string', 'dump_params'), [
  7. [[u'item'], '[item]\n', {}],
  8. [[u'itäm'], '["it\\xE4m"]\n', {'allow_unicode': False}],
  9. [[u'itäm'], '[itäm]\n', {'allow_unicode': True}],
  10. [{u'key': u'value'}, '{key: value}\n', {}],
  11. [{u'kï': u'valü'}, '{"k\\xEF": "val\\xFC"}\n', {'allow_unicode': False}],
  12. [{u'kï': u'valü'}, '{kï: valü}\n', {'allow_unicode': True}],
  13. [{u'kĕyĭ': u'可以'}, '{kĕyĭ: 可以}\n', {'allow_unicode': True}],
  14. [{u'⚕': u'☤'}, '{⚕: ☤}\n', {'allow_unicode': True}],
  15. ])
  16. def test_to_yaml(unicode_string, expected_yaml_string, dump_params, dumper):
  17. # create subclass so call to class method does not interfere with other tests
  18. # see yaml.BaseRepresenter.add_representer()
  19. class TestDumper(dumper):
  20. pass
  21. ioex.register_yaml_unicode_as_str_representer(TestDumper)
  22. generated_yaml_string = yaml.dump(
  23. unicode_string,
  24. Dumper = TestDumper,
  25. default_flow_style = True,
  26. **dump_params
  27. )
  28. assert type(expected_yaml_string) == type(generated_yaml_string)
  29. assert expected_yaml_string == generated_yaml_string