summaryrefslogtreecommitdiff
path: root/tests/test_attrs.py
blob: c3b90c6e350481dad24d22c71bbbc8309c713ef9 (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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
from asciidoc import attrs
import pytest


testcases = {
    # these test cases fail under future mode
    "pure_legacy": (
        # In future mode, all values are always strings
        (
            'height=100,caption="",link="images/octocat.png"',
            {
                '0': 'height=100,caption="",link="images/octocat.png"',
                'height': 100,
                'caption': '',
                'link': 'images/octocat.png',
            },
        ),
        (
            "height=100,caption='',link='images/octocat.png'",
            {
                '0': "height=100,caption='',link='images/octocat.png'",
                'height': 100,
                'caption': '',
                'link': 'images/octocat.png',
            },
        ),
    ),
    # these test cases pass under both legacy and future modes
    "legacy": (
        # docstring tests
        ('', {}),
        ('hello,world', {'0': 'hello,world', '1': 'hello', '2': 'world'}),
        (
            '"hello", planet="earth"',
            {'0': '"hello", planet="earth"', '1': 'hello', 'planet': 'earth'}
        ),
        # tests taken from
        # https://github.com/asciidoctor/asciidoctor/blob/main/test/attribute_list_test.rb
        ('quote', {'0': 'quote', '1': 'quote'}),
        ('"quote"', {'0': '"quote"', '1': 'quote'}),
        ('""', {'0': '""', '1': ''}),
        ("'quote'", {'0': "'quote'", '1': 'quote'}),
        ("''", {'0': "''", '1': ''}),
        ('\'', {'0': '\'', '1': '\''}),
        ('\'ba\\\'zaar\'', {'0': '\'ba\\\'zaar\'', '1': 'ba\'zaar'}),
        (
            'first, second one, third',
            {
                '0': 'first, second one, third',
                '1': 'first',
                '2': 'second one', '3': 'third',
            },
        ),
        ('=foo=', {'0': '=foo=', '1': '=foo='}),
        ('foo="bar"', {'0': 'foo="bar"', 'foo': 'bar'}),

        ('foo=\'bar\'', {'0': 'foo=\'bar\'', 'foo': 'bar'}),

    ),
    # these tests only pass under future mode
    # tests taken from
    # https://github.com/asciidoctor/asciidoctor/blob/main/test/attribute_list_test.rb
    "future": (
        ('"ba\"zaar"', {'0': '"ba\"zaar"', '1': 'ba"zaar'}),
        ('name=\'', {'0': 'name=\'', 'name': '\''}),
        ('name=\'{val}', {'0': 'name=\'{val}', 'name': '\'{val}'}),
        ('quote , ', {'0': 'quote , ', '1': 'quote', '2': None}),
        (', John Smith', {'0': ', John Smith', '1': None, '2': 'John Smith'}),
        (
            'first,,third,',
            {'0': 'first,,third,', '1': 'first', '2': None, '3': 'third', '4': None}
        ),
        ('foo=bar', {'0': 'foo=bar', 'foo': 'bar'}),
        ('foo=', {'0': 'foo=', 'foo': ''}),
        ('foo=,bar=baz', {'0': 'foo=,bar=baz', 'foo': '', 'bar': 'baz'}),
        (
            'height=100,caption="",link="images/octocat.png"',
            {
                '0': 'height=100,caption="",link="images/octocat.png"',
                'height': '100',
                'caption': '',
                'link': 'images/octocat.png',
            },
        ),
        (
            "height=100,caption='',link='images/octocat.png'",
            {
                '0': "height=100,caption='',link='images/octocat.png'",
                'height': '100',
                'caption': '',
                'link': 'images/octocat.png',
            },
        ),
        (
            'first=value, second=two, third=3',
            {
                '0': 'first=value, second=two, third=3',
                'first': 'value',
                'second': 'two',
                'third': '3',
            },
        ),
        (
            'first=\'value\', second="value two", third=three',
            {
                '0': 'first=\'value\', second="value two", third=three',
                'first': 'value',
                'second': 'value two',
                'third': 'three',
            },
        ),
        (
            "     first    =     'value', second     =\"value two\"     , third=       three      ",  # noqa: E501
            {
                '0': "     first    =     'value', second     =\"value two\"     , third=       three      ",  # noqa: E501
                'first': 'value',
                'second': 'value two',
                'third': 'three',
            },
        ),
        (
            'first, second="value two", third=three, Sherlock Holmes',
            {
                '0': 'first, second="value two", third=three, Sherlock Holmes',
                '1': 'first',
                'second': 'value two',
                'third': 'three',
                '4': 'Sherlock Holmes',
            },
        ),
        (
            'first,,third=,,fifth=five',
            {
                '0': 'first,,third=,,fifth=five',
                '1': 'first',
                '2': None,
                'third': '',
                '4': None,
                'fifth': 'five',
            },
        ),
    )
}


@pytest.mark.parametrize(
    "input,expected",
    testcases["legacy"] + testcases["pure_legacy"],
)
def test_parse_attributes(input, expected):
    output = dict()
    attrs.parse_attributes(input, output)
    assert output == expected


@pytest.mark.parametrize(
    "input,expected",
    testcases['legacy'] + testcases["future"],
)
def test_parse_future_attributes(enable_future_compat, input, expected):
    output = dict()
    attrs.parse_attributes(input, output)
    assert output == expected