File: test_module_validation.py

package info (click to toggle)
python-aristaproto 1.2%2Breally0.1.4-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 1,600 kB
  • sloc: python: 6,521; java: 106; xml: 84; makefile: 6
file content (111 lines) | stat: -rw-r--r-- 3,110 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
from typing import (
    List,
    Optional,
    Set,
)

import pytest

from aristaproto.plugin.module_validation import ModuleValidator


@pytest.mark.parametrize(
    ["text", "expected_collisions"],
    [
        pytest.param(
            ["import os"],
            None,
            id="single import",
        ),
        pytest.param(
            ["import os", "import sys"],
            None,
            id="multiple imports",
        ),
        pytest.param(
            ["import os", "import os"],
            {"os"},
            id="duplicate imports",
        ),
        pytest.param(
            ["from os import path", "import os"],
            None,
            id="duplicate imports with alias",
        ),
        pytest.param(
            ["from os import path", "import os as os_alias"],
            None,
            id="duplicate imports with alias",
        ),
        pytest.param(
            ["from os import path", "import os as path"],
            {"path"},
            id="duplicate imports with alias",
        ),
        pytest.param(
            ["import os", "class os:"],
            {"os"},
            id="duplicate import with class",
        ),
        pytest.param(
            ["import os", "class os:", "  pass", "import sys"],
            {"os"},
            id="duplicate import with class and another",
        ),
        pytest.param(
            ["def test(): pass", "class test:"],
            {"test"},
            id="duplicate class and function",
        ),
        pytest.param(
            ["def test(): pass", "def test(): pass"],
            {"test"},
            id="duplicate functions",
        ),
        pytest.param(
            ["def test(): pass", "test = 100"],
            {"test"},
            id="function and variable",
        ),
        pytest.param(
            ["def test():", "    test = 3"],
            None,
            id="function and variable in function",
        ),
        pytest.param(
            [
                "def test(): pass",
                "'''",
                "def test(): pass",
                "'''",
                "def test_2(): pass",
            ],
            None,
            id="duplicate functions with multiline string",
        ),
        pytest.param(
            ["def test(): pass", "# def test(): pass"],
            None,
            id="duplicate functions with comments",
        ),
        pytest.param(
            ["from test import (", "    A", "   B", "   C", ")"],
            None,
            id="multiline import",
        ),
        pytest.param(
            ["from test import (", "    A", "   B", "   C", ")", "from test import A"],
            {"A"},
            id="multiline import with duplicate",
        ),
    ],
)
def test_module_validator(text: List[str], expected_collisions: Optional[Set[str]]):
    line_iterator = iter(text)
    validator = ModuleValidator(line_iterator)
    valid = validator.validate()
    if expected_collisions is None:
        assert valid
    else:
        assert set(validator.collisions.keys()) == expected_collisions
        assert not valid