File: test_quoting.py

package info (click to toggle)
python-graphviz 0.20.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,188 kB
  • sloc: python: 4,098; makefile: 13
file content (65 lines) | stat: -rw-r--r-- 1,916 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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import sys
import warnings

import pytest

import graphviz
from graphviz import quoting


@pytest.mark.parametrize(
    'char', ['G', 'E', 'T', 'H', 'L', 'l'])
def test_deprecated_escape(recwarn, char):
    warnings.simplefilter('always')

    escape = eval(rf'"\{char}"')

    assert len(recwarn) == 1
    w = recwarn.pop(DeprecationWarning if sys.version_info < (3, 12)
                    else SyntaxWarning)
    assert str(w.message).startswith('invalid escape sequence')

    assert escape == f'\\{char}'
    assert quoting.quote(escape) == f'"\\{char}"'


@pytest.mark.parametrize(
    'identifier, expected',
    [('"spam"', r'"\"spam\""'),
     ('node', '"node"'),
     ('EDGE', '"EDGE"'),
     ('Graph', '"Graph"'),
     ('\\G \\N \\E \\T \\H \\L', '"\\G \\N \\E \\T \\H \\L"'),
     ('\\n \\l \\r', '"\\n \\l \\r"'),
     ('\r\n', '"\r\n"'),
     ('\\\\n', r'"\\n"'),
     ('\u0665.\u0660', '"\u0665.\u0660"'),
     ('\\"spam', r'"\"spam"'),
     ('\\\\"spam', r'"\\\"spam"'),
     ('\\\\\\"spam', r'"\\\"spam"'),
     ('\\\\\\\\"spam', r'"\\\\\"spam"')])
def test_quote(identifier, expected):
    assert quoting.quote(identifier) == expected


@pytest.mark.parametrize(
    'attributes, expected',
    [([('spam', 'eggs')], ' [spam=eggs]'),
     ({'spam': 'eggs'}, ' [spam=eggs]')])
def test_attr_list(attributes, expected):
    assert quoting.attr_list(attributes=attributes) == expected


@pytest.mark.parametrize(
    'string, expected, expected_quoted',
    [('spam', 'spam', 'spam'),
     ('<>-*-<>', '<>-*-<>', '"<>-*-<>"')])
def test_nohtml(string, expected, expected_quoted):
    result = graphviz.nohtml(string)
    assert isinstance(result, str)
    assert isinstance(result, quoting.NoHtml)
    assert result == expected

    quoted = quoting.quote(result)
    assert isinstance(quoted, str)
    assert quoted == expected_quoted