Debian Codegraph @

test/units/module_utils/common/test_yaml.py

raw
1 from __future__ import annotations
2
3 import typing as t
4
5 import pytest
6
7 from ansible.module_utils.common.yaml import yaml_dump, yaml_dump_all
8 from ansible.module_utils._internal._datatag import Tripwire
9 from ansible.module_utils._internal._datatag._tags import Deprecated
10 from units.mock.custom_types import CustomMapping, CustomSequence
11
12 _test_tag = Deprecated(msg="test")
13
14
15 @pytest.mark.parametrize("value, expected", (
16 (CustomMapping(dict(a=1)), "a: 1"),
17 (CustomSequence([1]), "- 1"),
18 (_test_tag.tag(dict(a=1)), "a: 1"),
19 (_test_tag.tag([1]), "- 1"),
20 (_test_tag.tag(1), "1"),
21 (_test_tag.tag("Ansible"), "Ansible"),
22 ))
23 def test_dump(value: t.Any, expected: str) -> None:
24 """Verify supported types can be dumped."""
25 result = yaml_dump(value).strip()
26
27 assert result == expected
28
29 result = yaml_dump_all([value]).strip()
30
31 assert result == expected
32
33
34 def test_dump_tripwire() -> None:
35 """Verify dumping a tripwire trips it."""
36 class Tripped(Exception):
37 pass
38
39 class CustomTripwire(Tripwire):
40 def trip(self) -> t.NoReturn:
41 raise Tripped()
42
43 with pytest.raises(Tripped):
44 yaml_dump(CustomTripwire())
45
46 with pytest.raises(Tripped):
47 yaml_dump_all([CustomTripwire()])