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
|
# Licensed under the GPL: https://www.gnu.org/licenses/old-licenses/gpl-2.0.html
# For details: https://github.com/PyCQA/pylint/blob/main/LICENSE
# Copyright (c) https://github.com/PyCQA/pylint/blob/main/CONTRIBUTORS.txt
"""Unittest for the NameChecker."""
from __future__ import annotations
import unittest
from pylint.checkers import base
class TestNamePresets(unittest.TestCase):
SNAKE_CASE_NAMES = {"tést_snake_case", "test_snake_case11", "test_https_200"}
CAMEL_CASE_NAMES = {"téstCamelCase", "testCamelCase11", "testHTTP200"}
UPPER_CASE_NAMES = {"TÉST_UPPER_CASE", "TEST_UPPER_CASE11", "TEST_HTTP_200"}
PASCAL_CASE_NAMES = {"TéstPascalCase", "TestPascalCase11", "TestHTTP200"}
ALL_NAMES = (
SNAKE_CASE_NAMES | CAMEL_CASE_NAMES | UPPER_CASE_NAMES | PASCAL_CASE_NAMES
)
def _test_name_is_correct_for_all_name_types(
self, naming_style: type[base.NamingStyle], name: str
) -> None:
for name_type in base.KNOWN_NAME_TYPES_WITH_STYLE:
self._test_is_correct(naming_style, name, name_type)
def _test_name_is_incorrect_for_all_name_types(
self, naming_style: type[base.NamingStyle], name: str
) -> None:
for name_type in base.KNOWN_NAME_TYPES_WITH_STYLE:
self._test_is_incorrect(naming_style, name, name_type)
def _test_should_always_pass(self, naming_style: type[base.NamingStyle]) -> None:
always_pass_data = [
("__add__", "method"),
("__set_name__", "method"),
("__version__", "const"),
("__author__", "const"),
]
for name, name_type in always_pass_data:
self._test_is_correct(naming_style, name, name_type)
@staticmethod
def _test_is_correct(
naming_style: type[base.NamingStyle], name: str, name_type: str
) -> None:
rgx = naming_style.get_regex(name_type)
fail = f"{name!r} does not match pattern {rgx!r} (style: {naming_style}, type: {name_type})"
assert rgx.match(name), fail
@staticmethod
def _test_is_incorrect(
naming_style: type[base.NamingStyle], name: str, name_type: str
) -> None:
rgx = naming_style.get_regex(name_type)
fail = f"{name!r} not match pattern {rgx!r} (style: {naming_style}, type: {name_type})"
assert not rgx.match(name), fail
def test_snake_case(self) -> None:
naming_style = base.SnakeCaseStyle
for name in self.SNAKE_CASE_NAMES:
self._test_name_is_correct_for_all_name_types(naming_style, name)
for name in self.ALL_NAMES - self.SNAKE_CASE_NAMES:
self._test_name_is_incorrect_for_all_name_types(naming_style, name)
self._test_should_always_pass(naming_style)
def test_camel_case(self) -> None:
naming_style = base.CamelCaseStyle
for name in self.CAMEL_CASE_NAMES:
self._test_name_is_correct_for_all_name_types(naming_style, name)
for name in self.ALL_NAMES - self.CAMEL_CASE_NAMES:
self._test_name_is_incorrect_for_all_name_types(naming_style, name)
self._test_should_always_pass(naming_style)
def test_upper_case(self) -> None:
naming_style = base.UpperCaseStyle
for name in self.UPPER_CASE_NAMES:
self._test_name_is_correct_for_all_name_types(naming_style, name)
for name in self.ALL_NAMES - self.UPPER_CASE_NAMES:
self._test_name_is_incorrect_for_all_name_types(naming_style, name)
self._test_name_is_incorrect_for_all_name_types(naming_style, "UPPERcase")
self._test_should_always_pass(naming_style)
def test_pascal_case(self) -> None:
naming_style = base.PascalCaseStyle
for name in self.PASCAL_CASE_NAMES:
self._test_name_is_correct_for_all_name_types(naming_style, name)
for name in self.ALL_NAMES - self.PASCAL_CASE_NAMES:
self._test_name_is_incorrect_for_all_name_types(naming_style, name)
self._test_should_always_pass(naming_style)
|