File: test_constructors.py

package info (click to toggle)
python-annotatedyaml 1.0.2-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,088 kB
  • sloc: python: 1,303; makefile: 18
file content (41 lines) | stat: -rw-r--r-- 1,544 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
from __future__ import annotations

import io
from unittest.mock import Mock

from yaml.nodes import ScalarNode

from annotatedyaml.constructors import _handle_scalar_tag
from annotatedyaml.loader import PythonSafeLoader
from annotatedyaml.objects import NodeStrClass


def test__handle_scalar_tag_non_str():
    """Test _handle_scalar_tag with a non-str scalar."""
    loader = PythonSafeLoader(io.BytesIO(b""))
    node = ScalarNode("tag:yaml.org,2002:int", 123)
    node_str_class = _handle_scalar_tag(loader, node)
    assert not isinstance(node_str_class, NodeStrClass)
    assert node_str_class == 123


def test__handle_scalar_tag_str():
    """Test _handle_scalar_tag with a str scalar."""
    loader = PythonSafeLoader(io.BytesIO(b""))
    node = ScalarNode("tag:yaml.org,2002:str", "123", Mock(line=3))
    node_str_class = _handle_scalar_tag(loader, node)
    assert isinstance(node_str_class, NodeStrClass)
    assert node_str_class == "123"
    assert node_str_class.__config_file__ == "<file>"
    assert node_str_class.__line__ == node.start_mark.line + 1


def test__handle_scalar_tag_str_missing_startmark():
    """Test _handle_scalar_tag with a str scalar with no startmark."""
    loader = PythonSafeLoader(io.BytesIO(b""))
    node = ScalarNode("tag:yaml.org,2002:str", "123")
    node_str_class = _handle_scalar_tag(loader, node)
    assert isinstance(node_str_class, NodeStrClass)
    assert node_str_class == "123"
    assert node_str_class.__config_file__ == "<file>"
    assert not hasattr(node_str_class, "__line__")