This repository was archived by the owner on Apr 14, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 132
Expand file tree
/
Copy pathLintNoSelfArgumentTests.cs
More file actions
182 lines (160 loc) · 5.83 KB
/
LintNoSelfArgumentTests.cs
File metadata and controls
182 lines (160 loc) · 5.83 KB
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
// Copyright(c) Microsoft Corporation
// All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the License); you may not use
// this file except in compliance with the License. You may obtain a copy of the
// License at http://www.apache.org/licenses/LICENSE-2.0
//
// THIS CODE IS PROVIDED ON AN *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS
// OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY
// IMPLIED WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
// MERCHANTABILITY OR NON-INFRINGEMENT.
//
// See the Apache Version 2.0 License for specific language governing
// permissions and limitations under the License.
using System.Linq;
using System.Threading.Tasks;
using FluentAssertions;
using Microsoft.Python.Analysis.Diagnostics;
using Microsoft.Python.Analysis.Tests.FluentAssertions;
using Microsoft.Python.Core;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using TestUtilities;
namespace Microsoft.Python.Analysis.Tests {
[TestClass]
public class LintNoSelfArgumentTests : AnalysisTestBase {
public TestContext TestContext { get; set; }
[TestInitialize]
public void TestInitialize()
=> TestEnvironmentImpl.TestInitialize($"{TestContext.FullyQualifiedTestClassName}.{TestContext.TestName}");
[TestCleanup]
public void Cleanup() => TestEnvironmentImpl.TestCleanup();
[TestMethod, Priority(0)]
public async Task FirstArgumentMethodNotSelf() {
const string code = @"
class Test:
def test(x, y, z):
pass
";
var analysis = await GetAnalysisAsync(code);
analysis.Diagnostics.Should().HaveCount(1);
var diagnostic = analysis.Diagnostics.ElementAt(0);
diagnostic.ErrorCode.Should().Be(ErrorCodes.NoSelfArgument);
diagnostic.SourceSpan.Should().Be(3, 14, 3, 15);
diagnostic.Message.Should().Be(Resources.NoSelfArgument.FormatInvariant("test"));
}
[TestMethod, Priority(0)]
public async Task FirstArgumentNotSelfMultiple() {
const string code = @"
class Test:
def test(x, y, z):
pass
def test2(x, y, z):
pass
";
var analysis = await GetAnalysisAsync(code);
analysis.Diagnostics.Should().HaveCount(2);
var diagnostic = analysis.Diagnostics.ElementAt(0);
diagnostic.ErrorCode.Should().Be(ErrorCodes.NoSelfArgument);
diagnostic.SourceSpan.Should().Be(3, 14, 3, 15);
diagnostic.Message.Should().Be(Resources.NoSelfArgument.FormatInvariant("test"));
diagnostic = analysis.Diagnostics.ElementAt(1);
diagnostic.ErrorCode.Should().Be(ErrorCodes.NoSelfArgument);
diagnostic.SourceSpan.Should().Be(6, 15, 6, 16);
diagnostic.Message.Should().Be(Resources.NoSelfArgument.FormatInvariant("test2"));
}
[TestMethod, Priority(0)]
public async Task NestedClassFuncNoSelfArg() {
const string code = @"
class Test:
class Test2:
def hello(x, y, z):
pass
def test(x, y, z): ...
";
var analysis = await GetAnalysisAsync(code);
analysis.Diagnostics.Should().HaveCount(2);
var diagnostic = analysis.Diagnostics.ElementAt(0);
diagnostic.ErrorCode.Should().Be(ErrorCodes.NoSelfArgument);
diagnostic.SourceSpan.Should().Be(4, 19, 4, 20);
diagnostic.Message.Should().Be(Resources.NoSelfArgument.FormatInvariant("hello"));
diagnostic = analysis.Diagnostics.ElementAt(1);
diagnostic.ErrorCode.Should().Be(ErrorCodes.NoSelfArgument);
diagnostic.SourceSpan.Should().Be(7, 14, 7, 15);
diagnostic.Message.Should().Be(Resources.NoSelfArgument.FormatInvariant("test"));
}
[TestMethod, Priority(0)]
public async Task FirstArgumentIsSelf() {
const string code = @"
class Test:
def test(self):
pass
";
var analysis = await GetAnalysisAsync(code);
analysis.Diagnostics.Should().BeEmpty();
}
[TestMethod, Priority(0)]
public async Task FirstArgumentIsSelfManyParams() {
const string code = @"
class Test:
def test(self, a, b, c, d, e):
pass
";
var analysis = await GetAnalysisAsync(code);
analysis.Diagnostics.Should().BeEmpty();
}
[TestMethod, Priority(0)]
public async Task StaticMethodNoSelfValid() {
const string code = @"
class C:
@staticmethod
def test(a, b):
pass
";
var analysis = await GetAnalysisAsync(code);
analysis.Diagnostics.Should().BeEmpty();
}
[TestMethod, Priority(0)]
public async Task NormalFunction() {
const string code = @"
def test():
pass
class C:
def test1(self):
pass
";
var analysis = await GetAnalysisAsync(code);
analysis.Diagnostics.Should().BeEmpty();
}
[TestMethod, Priority(0)]
public async Task NormalProperty() {
const string code = @"
class C:
@property
def test(self):
pass
";
var analysis = await GetAnalysisAsync(code);
analysis.Diagnostics.Should().BeEmpty();
}
[TestMethod, Priority(0)]
public async Task NoDiagnosticInMetaclass() {
const string code = @"
class Test(type):
def test(a):
pass
";
var analysis = await GetAnalysisAsync(code);
analysis.Diagnostics.Should().BeEmpty();
}
[TestMethod, Priority(0)]
public async Task LambdaInClassScope() {
const string code = @"
class C:
[lambda x, y: x+y+z for z in range(10)]
";
var analysis = await GetAnalysisAsync(code);
analysis.Diagnostics.Should().BeEmpty();
}
}
}