File: conftest.py

package info (click to toggle)
mwparserfromhell 0.6.6-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,176 kB
  • sloc: python: 7,518; ansic: 4,462; sh: 139; makefile: 127
file content (158 lines) | stat: -rw-r--r-- 6,147 bytes parent folder | download | duplicates (2)
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
# Copyright (C) 2012-2021 Ben Kurtovic <ben.kurtovic@gmail.com>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

from mwparserfromhell.nodes import (
    Argument,
    Comment,
    ExternalLink,
    Heading,
    HTMLEntity,
    Tag,
    Template,
    Text,
    Wikilink,
)
from mwparserfromhell.smart_list import SmartList
from mwparserfromhell.wikicode import Wikicode

wrap = lambda L: Wikicode(SmartList(L))
wraptext = lambda *args: wrap([Text(t) for t in args])


def _assert_node_equal(expected, actual):
    """Assert that two Nodes have the same type and have the same data."""
    registry = {
        Argument: _assert_argument_node_equal,
        Comment: _assert_comment_node_equal,
        ExternalLink: _assert_external_link_node_equal,
        Heading: _assert_heading_node_equal,
        HTMLEntity: _assert_html_entity_node_equal,
        Tag: _assert_tag_node_equal,
        Template: _assert_template_node_equal,
        Text: _assert_text_node_equal,
        Wikilink: _assert_wikilink_node_equal,
    }
    # pylint: disable=unidiomatic-typecheck
    assert type(expected) == type(actual)
    registry[type(expected)](expected, actual)


def _assert_argument_node_equal(expected, actual):
    """Assert that two Argument nodes have the same data."""
    assert_wikicode_equal(expected.name, actual.name)
    if expected.default is not None:
        assert_wikicode_equal(expected.default, actual.default)
    else:
        assert actual.default is None


def _assert_comment_node_equal(expected, actual):
    """Assert that two Comment nodes have the same data."""
    assert expected.contents == actual.contents


def _assert_external_link_node_equal(expected, actual):
    """Assert that two ExternalLink nodes have the same data."""
    assert_wikicode_equal(expected.url, actual.url)
    if expected.title is not None:
        assert_wikicode_equal(expected.title, actual.title)
    else:
        assert actual.title is None
    assert expected.brackets is actual.brackets
    assert expected.suppress_space is actual.suppress_space


def _assert_heading_node_equal(expected, actual):
    """Assert that two Heading nodes have the same data."""
    assert_wikicode_equal(expected.title, actual.title)
    assert expected.level == actual.level


def _assert_html_entity_node_equal(expected, actual):
    """Assert that two HTMLEntity nodes have the same data."""
    assert expected.value == actual.value
    assert expected.named is actual.named
    assert expected.hexadecimal is actual.hexadecimal
    assert expected.hex_char == actual.hex_char


def _assert_tag_node_equal(expected, actual):
    """Assert that two Tag nodes have the same data."""
    assert_wikicode_equal(expected.tag, actual.tag)
    if expected.contents is not None:
        assert_wikicode_equal(expected.contents, actual.contents)
    else:
        assert actual.contents is None
    length = len(expected.attributes)
    assert length == len(actual.attributes)
    for i in range(length):
        exp_attr = expected.attributes[i]
        act_attr = actual.attributes[i]
        assert_wikicode_equal(exp_attr.name, act_attr.name)
        if exp_attr.value is not None:
            assert_wikicode_equal(exp_attr.value, act_attr.value)
            assert exp_attr.quotes == act_attr.quotes
        else:
            assert act_attr.value is None
        assert exp_attr.pad_first == act_attr.pad_first
        assert exp_attr.pad_before_eq == act_attr.pad_before_eq
        assert exp_attr.pad_after_eq == act_attr.pad_after_eq
    assert expected.wiki_markup == actual.wiki_markup
    assert expected.self_closing is actual.self_closing
    assert expected.invalid is actual.invalid
    assert expected.implicit is actual.implicit
    assert expected.padding == actual.padding
    assert_wikicode_equal(expected.closing_tag, actual.closing_tag)


def _assert_template_node_equal(expected, actual):
    """Assert that two Template nodes have the same data."""
    assert_wikicode_equal(expected.name, actual.name)
    length = len(expected.params)
    assert length == len(actual.params)
    for i in range(length):
        exp_param = expected.params[i]
        act_param = actual.params[i]
        assert_wikicode_equal(exp_param.name, act_param.name)
        assert_wikicode_equal(exp_param.value, act_param.value)
        assert exp_param.showkey is act_param.showkey


def _assert_text_node_equal(expected, actual):
    """Assert that two Text nodes have the same data."""
    assert expected.value == actual.value


def _assert_wikilink_node_equal(expected, actual):
    """Assert that two Wikilink nodes have the same data."""
    assert_wikicode_equal(expected.title, actual.title)
    if expected.text is not None:
        assert_wikicode_equal(expected.text, actual.text)
    else:
        assert actual.text is None


def assert_wikicode_equal(expected, actual):
    """Assert that two Wikicode objects have the same data."""
    assert isinstance(actual, Wikicode)
    length = len(expected.nodes)
    assert length == len(actual.nodes)
    for i in range(length):
        _assert_node_equal(expected.get(i), actual.get(i))