| 1 | from __future__ import annotations |
| 2 | |
| 3 | import io |
| 4 | from unittest.mock import Mock |
| 5 | |
| 6 | from yaml.nodes import ScalarNode |
| 7 | |
| 8 | from annotatedyaml.constructors import _handle_scalar_tag |
| 9 | from annotatedyaml.loader import PythonSafeLoader |
| 10 | from annotatedyaml.objects import NodeStrClass |
| 11 | |
| 12 | |
| 13 | def test__handle_scalar_tag_non_str(): |
| 14 | """Test _handle_scalar_tag with a non-str scalar.""" |
| 15 | loader = PythonSafeLoader(io.BytesIO(b"")) |
| 16 | node = ScalarNode("tag:yaml.org,2002:int", 123) |
| 17 | node_str_class = _handle_scalar_tag(loader, node) |
| 18 | assert not isinstance(node_str_class, NodeStrClass) |
| 19 | assert node_str_class == 123 |
| 20 | |
| 21 | |
| 22 | def test__handle_scalar_tag_str(): |
| 23 | """Test _handle_scalar_tag with a str scalar.""" |
| 24 | loader = PythonSafeLoader(io.BytesIO(b"")) |
| 25 | node = ScalarNode("tag:yaml.org,2002:str", "123", Mock(line=3)) |
| 26 | node_str_class = _handle_scalar_tag(loader, node) |
| 27 | assert isinstance(node_str_class, NodeStrClass) |
| 28 | assert node_str_class == "123" |
| 29 | assert node_str_class.__config_file__ == "<file>" |
| 30 | assert node_str_class.__line__ == node.start_mark.line + 1 |
| 31 | |
| 32 | |
| 33 | def test__handle_scalar_tag_str_missing_startmark(): |
| 34 | """Test _handle_scalar_tag with a str scalar with no startmark.""" |
| 35 | loader = PythonSafeLoader(io.BytesIO(b"")) |
| 36 | node = ScalarNode("tag:yaml.org,2002:str", "123") |
| 37 | node_str_class = _handle_scalar_tag(loader, node) |
| 38 | assert isinstance(node_str_class, NodeStrClass) |
| 39 | assert node_str_class == "123" |
| 40 | assert node_str_class.__config_file__ == "<file>" |
| 41 | assert not hasattr(node_str_class, "__line__") |