summaryrefslogtreecommitdiffstats
path: root/test/test_text.py
blob: fa91feeb7d21af706553af93ef9d02b981e927a3 (plain)
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
"""Tests for text module."""
from typing import Any

import pytest

from ansiblelint.text import has_glob, has_jinja, strip_ansi_escape, toidentifier


@pytest.mark.parametrize(
    ("value", "expected"),
    (
        pytest.param("\x1b[1;31mHello", "Hello", id="0"),
        pytest.param("\x1b[2;37;41mExample_file.zip", "Example_file.zip", id="1"),
        pytest.param(b"ansible-lint", "ansible-lint", id="2"),
    ),
)
def test_strip_ansi_escape(value: Any, expected: str) -> None:
    """Tests for strip_ansi_escape()."""
    assert strip_ansi_escape(value) == expected


@pytest.mark.parametrize(
    ("value", "expected"),
    (
        pytest.param("foo-bar", "foo_bar", id="0"),
        pytest.param("foo--bar", "foo_bar", id="1"),
    ),
)
def test_toidentifier(value: Any, expected: str) -> None:
    """Tests for toidentifier()."""
    assert toidentifier(value) == expected


@pytest.mark.parametrize(
    ("value", "expected"),
    (pytest.param("example_test.zip", "Unable to convert role name", id="0"),),
)
def test_toidentifier_fail(value: Any, expected: str) -> None:
    """Tests for toidentifier()."""
    with pytest.raises(RuntimeError) as err:
        toidentifier(value)
    assert str(err.value).find(expected) > -1


@pytest.mark.parametrize(
    ("value", "expected"),
    (
        pytest.param("", False, id="0"),
        pytest.param("{{ }}", True, id="1"),
        pytest.param("foo {# #} bar", True, id="2"),
        pytest.param("foo \n{% %} bar", True, id="3"),
        pytest.param(None, False, id="4"),
        pytest.param(42, False, id="5"),
        pytest.param(True, False, id="6"),
    ),
)
def test_has_jinja(value: Any, expected: bool) -> None:
    """Tests for has_jinja()."""
    assert has_jinja(value) == expected


@pytest.mark.parametrize(
    ("value", "expected"),
    (
        pytest.param("", False, id="0"),
        pytest.param("*", True, id="1"),
        pytest.param("foo.*", True, id="2"),
        pytest.param(None, False, id="4"),
        pytest.param(42, False, id="5"),
        pytest.param(True, False, id="6"),
    ),
)
def test_has_glob(value: Any, expected: bool) -> None:
    """Tests for has_jinja()."""
    assert has_glob(value) == expected