20
20
package org .sonar .python .checks ;
21
21
22
22
import org .junit .Test ;
23
+ import org .sonar .python .checks .quickfix .PythonQuickFixVerifier ;
23
24
import org .sonar .python .checks .utils .PythonCheckVerifier ;
24
25
26
+ import static org .sonar .python .checks .BuiltinGenericsOverTypingModuleCheck .MESSAGE ;
27
+ import static org .sonar .python .checks .utils .CodeTestUtils .code ;
28
+
25
29
public class BuiltinGenericsOverTypingModuleCheckTest {
26
30
27
31
@ Test
@@ -36,4 +40,135 @@ public void checkCollections() {
36
40
new BuiltinGenericsOverTypingModuleCheck ());
37
41
}
38
42
43
+ @ Test
44
+ public void quick_fix_generics_in_param () {
45
+ String codeWithIssue = code (
46
+ "from typing import List" ,
47
+ "def foo(test: List[int]) -> str:" ,
48
+ " return \" bar\" " );
49
+ String codeFixed = code (
50
+ "from typing import List" ,
51
+ "def foo(test: list[int]) -> str:" ,
52
+ " return \" bar\" " );
53
+ BuiltinGenericsOverTypingModuleCheck check = new BuiltinGenericsOverTypingModuleCheck ();
54
+ PythonQuickFixVerifier .verify (check , codeWithIssue , codeFixed );
55
+ String specificMessage = String .format (MESSAGE , "list" );
56
+ PythonQuickFixVerifier .verifyQuickFixMessages (check , codeWithIssue , specificMessage );
57
+ }
58
+
59
+ @ Test
60
+ public void quick_fix_generics_in_return_type () {
61
+ String codeWithIssue = code (
62
+ "from typing import Dict" ,
63
+ "def foo(test: list[int]) -> Dict[str, int]:" ,
64
+ " return {}" );
65
+ String codeFixed = code (
66
+ "from typing import Dict" ,
67
+ "def foo(test: list[int]) -> dict[str, int]:" ,
68
+ " return {}" );
69
+ BuiltinGenericsOverTypingModuleCheck check = new BuiltinGenericsOverTypingModuleCheck ();
70
+ PythonQuickFixVerifier .verify (check , codeWithIssue , codeFixed );
71
+ String specificMessage = String .format (MESSAGE , "dict" );
72
+ PythonQuickFixVerifier .verifyQuickFixMessages (check , codeWithIssue , specificMessage );
73
+ }
74
+
75
+ @ Test
76
+ public void quick_fix_in_return_type_multiline () {
77
+ String codeWithIssue = code (
78
+ "from typing import Dict" ,
79
+ "def foo(test: list[int]) -> Dict[str," ,
80
+ " int]:" ,
81
+ " return {}" );
82
+ String codeFixed = code (
83
+ "from typing import Dict" ,
84
+ "def foo(test: list[int]) -> dict[str," ,
85
+ " int]:" ,
86
+ " return {}" );
87
+ BuiltinGenericsOverTypingModuleCheck check = new BuiltinGenericsOverTypingModuleCheck ();
88
+ PythonQuickFixVerifier .verify (check , codeWithIssue , codeFixed );
89
+ String specificMessage = String .format (MESSAGE , "dict" );
90
+ PythonQuickFixVerifier .verifyQuickFixMessages (check , codeWithIssue , specificMessage );
91
+ }
92
+
93
+ @ Test
94
+ public void quick_fix_generics_in_variable () {
95
+ String codeWithIssue = code (
96
+ "from typing import Set" ,
97
+ "def foo():" ,
98
+ " my_var: Set[str]" ,
99
+ " return my_var" );
100
+ String codeFixed = code (
101
+ "from typing import Set" ,
102
+ "def foo():" ,
103
+ " my_var: set[str]" ,
104
+ " return my_var" );
105
+ BuiltinGenericsOverTypingModuleCheck check = new BuiltinGenericsOverTypingModuleCheck ();
106
+ PythonQuickFixVerifier .verify (check , codeWithIssue , codeFixed );
107
+ String specificMessage = String .format (MESSAGE , "set" );
108
+ PythonQuickFixVerifier .verifyQuickFixMessages (check , codeWithIssue , specificMessage );
109
+ }
110
+
111
+ @ Test
112
+ public void quick_fix_fully_qualified () {
113
+ String codeWithIssue = code (
114
+ "import typing" ,
115
+ "def foo():" ,
116
+ " my_var: typing.Set[int]" ,
117
+ " return my_var" );
118
+ String codeFixed = code (
119
+ "import typing" ,
120
+ "def foo():" ,
121
+ " my_var: set[int]" ,
122
+ " return my_var" );
123
+ BuiltinGenericsOverTypingModuleCheck check = new BuiltinGenericsOverTypingModuleCheck ();
124
+ PythonQuickFixVerifier .verify (check , codeWithIssue , codeFixed );
125
+ String specificMessage = String .format (MESSAGE , "set" );
126
+ PythonQuickFixVerifier .verifyQuickFixMessages (check , codeWithIssue , specificMessage );
127
+ }
128
+
129
+ @ Test
130
+ public void no_quick_fix_for_change_requiring_import () {
131
+ String codeWithIssue = code (
132
+ "from typing import Iterable" ,
133
+ "def foo(test: Iterable[int]) -> dict[str, int]:" ,
134
+ " return {}" );
135
+ BuiltinGenericsOverTypingModuleCheck check = new BuiltinGenericsOverTypingModuleCheck ();
136
+ PythonQuickFixVerifier .verifyNoQuickFixes (check , codeWithIssue );
137
+ }
138
+
139
+ @ Test
140
+ public void quick_fix_nested_types () {
141
+ String codeWithIssue = code (
142
+ "from typing import Dict" ,
143
+ "def foo() -> tuple[str, Dict[int]]:" ,
144
+ " return (\" foo\" , list())" );
145
+ String codeFixed = code (
146
+ "from typing import Dict" ,
147
+ "def foo() -> tuple[str, dict[int]]:" ,
148
+ " return (\" foo\" , list())" );
149
+ BuiltinGenericsOverTypingModuleCheck check = new BuiltinGenericsOverTypingModuleCheck ();
150
+ PythonQuickFixVerifier .verify (check , codeWithIssue , codeFixed );
151
+ String specificMessage = String .format (MESSAGE , "dict" );
152
+ PythonQuickFixVerifier .verifyQuickFixMessages (check , codeWithIssue , specificMessage );
153
+ }
154
+
155
+ @ Test
156
+ public void quick_fix_nested_function () {
157
+ String codeWithIssue = code (
158
+ "from typing import Tuple" ,
159
+ "def foo() -> tuple[str, int]:" ,
160
+ " def bar(test: Tuple[str, int]) -> str:" ,
161
+ " return \" bar\" " ,
162
+ " return (\" foo\" , list())" );
163
+ String codeFixed = code (
164
+ "from typing import Tuple" ,
165
+ "def foo() -> tuple[str, int]:" ,
166
+ " def bar(test: tuple[str, int]) -> str:" ,
167
+ " return \" bar\" " ,
168
+ " return (\" foo\" , list())" );
169
+ BuiltinGenericsOverTypingModuleCheck check = new BuiltinGenericsOverTypingModuleCheck ();
170
+ PythonQuickFixVerifier .verify (check , codeWithIssue , codeFixed );
171
+ String specificMessage = String .format (MESSAGE , "tuple" );
172
+ PythonQuickFixVerifier .verifyQuickFixMessages (check , codeWithIssue , specificMessage );
173
+ }
39
174
}
0 commit comments