| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
"""Unittest for the NameChecker.""" |
| 6 |
|
| 7 |
import re |
| 8 |
|
| 9 |
import astroid |
| 10 |
|
| 11 |
from pylint.checkers import base |
| 12 |
from pylint.interfaces import HIGH |
| 13 |
from pylint.testutils import CheckerTestCase, MessageTest, set_config |
| 14 |
|
| 15 |
|
| 16 |
class TestMultiNamingStyle(CheckerTestCase): |
| 17 |
CHECKER_CLASS = base.NameChecker |
| 18 |
|
| 19 |
MULTI_STYLE_RE = re.compile("(?:(?P<UP>[A-Z]+)|(?P<down>[a-z]+))$") |
| 20 |
|
| 21 |
@set_config(class_rgx=MULTI_STYLE_RE) |
| 22 |
def test_multi_name_detection_majority(self) -> None: |
| 23 |
classes = astroid.extract_node( |
| 24 |
""" |
| 25 |
class classb(object): #@ |
| 26 |
pass |
| 27 |
class CLASSA(object): #@ |
| 28 |
pass |
| 29 |
class CLASSC(object): #@ |
| 30 |
pass |
| 31 |
""" |
| 32 |
) |
| 33 |
message = MessageTest( |
| 34 |
"invalid-name", |
| 35 |
node=classes[0], |
| 36 |
args=( |
| 37 |
"Class", |
| 38 |
"classb", |
| 39 |
"the `UP` group in the '(?:(?P<UP>[A-Z]+)|(?P<down>[a-z]+))$' pattern", |
| 40 |
), |
| 41 |
confidence=HIGH, |
| 42 |
line=2, |
| 43 |
col_offset=0, |
| 44 |
end_line=2, |
| 45 |
end_col_offset=12, |
| 46 |
) |
| 47 |
with self.assertAddsMessages(message): |
| 48 |
cls = None |
| 49 |
for cls in classes: |
| 50 |
self.checker.visit_classdef(cls) |
| 51 |
if cls: |
| 52 |
self.checker.leave_module(cls.root) |
| 53 |
|
| 54 |
@set_config(class_rgx=MULTI_STYLE_RE) |
| 55 |
def test_multi_name_detection_first_invalid(self) -> None: |
| 56 |
classes = astroid.extract_node( |
| 57 |
""" |
| 58 |
class class_a(object): #@ |
| 59 |
pass |
| 60 |
class classb(object): #@ |
| 61 |
pass |
| 62 |
class CLASSC(object): #@ |
| 63 |
pass |
| 64 |
""" |
| 65 |
) |
| 66 |
messages = [ |
| 67 |
MessageTest( |
| 68 |
"invalid-name", |
| 69 |
node=classes[0], |
| 70 |
args=( |
| 71 |
"Class", |
| 72 |
"class_a", |
| 73 |
"'(?:(?P<UP>[A-Z]+)|(?P<down>[a-z]+))$' pattern", |
| 74 |
), |
| 75 |
confidence=HIGH, |
| 76 |
line=2, |
| 77 |
col_offset=0, |
| 78 |
end_line=2, |
| 79 |
end_col_offset=13, |
| 80 |
), |
| 81 |
MessageTest( |
| 82 |
"invalid-name", |
| 83 |
node=classes[2], |
| 84 |
args=( |
| 85 |
"Class", |
| 86 |
"CLASSC", |
| 87 |
"the `down` group in the '(?:(?P<UP>[A-Z]+)|(?P<down>[a-z]+))$' pattern", |
| 88 |
), |
| 89 |
confidence=HIGH, |
| 90 |
line=6, |
| 91 |
col_offset=0, |
| 92 |
end_line=6, |
| 93 |
end_col_offset=12, |
| 94 |
), |
| 95 |
] |
| 96 |
with self.assertAddsMessages(*messages): |
| 97 |
cls = None |
| 98 |
for cls in classes: |
| 99 |
self.checker.visit_classdef(cls) |
| 100 |
if cls: |
| 101 |
self.checker.leave_module(cls.root) |
| 102 |
|
| 103 |
@set_config( |
| 104 |
method_rgx=MULTI_STYLE_RE, |
| 105 |
function_rgx=MULTI_STYLE_RE, |
| 106 |
name_group=("function:method",), |
| 107 |
) |
| 108 |
def test_multi_name_detection_group(self) -> None: |
| 109 |
function_defs = astroid.extract_node( |
| 110 |
""" |
| 111 |
class First(object): |
| 112 |
def func(self): #@ |
| 113 |
pass |
| 114 |
|
| 115 |
def FUNC(): #@ |
| 116 |
pass |
| 117 |
""", |
| 118 |
module_name="test", |
| 119 |
) |
| 120 |
message = MessageTest( |
| 121 |
"invalid-name", |
| 122 |
node=function_defs[1], |
| 123 |
args=( |
| 124 |
"Function", |
| 125 |
"FUNC", |
| 126 |
"the `down` group in the '(?:(?P<UP>[A-Z]+)|(?P<down>[a-z]+))$' pattern", |
| 127 |
), |
| 128 |
confidence=HIGH, |
| 129 |
line=6, |
| 130 |
col_offset=0, |
| 131 |
end_line=6, |
| 132 |
end_col_offset=8, |
| 133 |
) |
| 134 |
with self.assertAddsMessages(message): |
| 135 |
func = None |
| 136 |
for func in function_defs: |
| 137 |
self.checker.visit_functiondef(func) |
| 138 |
if func: |
| 139 |
self.checker.leave_module(func.root) |
| 140 |
|
| 141 |
@set_config( |
| 142 |
function_rgx=re.compile("(?:(?P<ignore>FOO)|(?P<UP>[A-Z]+)|(?P<down>[a-z]+))$") |
| 143 |
) |
| 144 |
def test_multi_name_detection_exempt(self) -> None: |
| 145 |
function_defs = astroid.extract_node( |
| 146 |
""" |
| 147 |
def FOO(): #@ |
| 148 |
pass |
| 149 |
def lower(): #@ |
| 150 |
pass |
| 151 |
def FOO(): #@ |
| 152 |
pass |
| 153 |
def UPPER(): #@ |
| 154 |
pass |
| 155 |
""" |
| 156 |
) |
| 157 |
message = MessageTest( |
| 158 |
"invalid-name", |
| 159 |
node=function_defs[3], |
| 160 |
args=( |
| 161 |
"Function", |
| 162 |
"UPPER", |
| 163 |
"the `down` group in the '(?:(?P<ignore>FOO)|(?P<UP>[A-Z]+)|(?P<down>[a-z]+))$' pattern", |
| 164 |
), |
| 165 |
confidence=HIGH, |
| 166 |
line=8, |
| 167 |
col_offset=0, |
| 168 |
end_line=8, |
| 169 |
end_col_offset=9, |
| 170 |
) |
| 171 |
with self.assertAddsMessages(message): |
| 172 |
func = None |
| 173 |
for func in function_defs: |
| 174 |
self.checker.visit_functiondef(func) |
| 175 |
if func: |
| 176 |
self.checker.leave_module(func.root) |