|
| 1 | +// Copyright(c) Microsoft Corporation |
| 2 | +// All rights reserved. |
| 3 | +// |
| 4 | +// Licensed under the Apache License, Version 2.0 (the License); you may not use |
| 5 | +// this file except in compliance with the License. You may obtain a copy of the |
| 6 | +// License at http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | +// |
| 8 | +// THIS CODE IS PROVIDED ON AN *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS |
| 9 | +// OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY |
| 10 | +// IMPLIED WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE, |
| 11 | +// MERCHANTABILITY OR NON-INFRINGEMENT. |
| 12 | +// |
| 13 | +// See the Apache Version 2.0 License for specific language governing |
| 14 | +// permissions and limitations under the License. |
| 15 | + |
| 16 | +using System.Linq; |
| 17 | +using System.Threading.Tasks; |
| 18 | +using FluentAssertions; |
| 19 | +using Microsoft.Python.Analysis.Tests.FluentAssertions; |
| 20 | +using Microsoft.Python.Parsing; |
| 21 | +using Microsoft.Python.Parsing.Tests; |
| 22 | +using Microsoft.VisualStudio.TestTools.UnitTesting; |
| 23 | +using TestUtilities; |
| 24 | + |
| 25 | +namespace Microsoft.Python.Analysis.Tests { |
| 26 | + [TestClass] |
| 27 | + public class LintReturnInInitTests : AnalysisTestBase { |
| 28 | + public TestContext TestContext { get; set; } |
| 29 | + |
| 30 | + [TestInitialize] |
| 31 | + public void TestInitialize() |
| 32 | + => TestEnvironmentImpl.TestInitialize($"{TestContext.FullyQualifiedTestClassName}.{TestContext.TestName}"); |
| 33 | + |
| 34 | + [TestCleanup] |
| 35 | + public void Cleanup() => TestEnvironmentImpl.TestCleanup(); |
| 36 | + |
| 37 | + [TestMethod, Priority(0)] |
| 38 | + public async Task ReturnInInit() { |
| 39 | + const string code = @" |
| 40 | +class Rectangle: |
| 41 | + def __init__(self, width, height): |
| 42 | + self.width = width |
| 43 | + self.height = height |
| 44 | + self.area = width * height |
| 45 | + return self.area |
| 46 | +
|
| 47 | +r = Rectangle(10, 10) |
| 48 | +"; |
| 49 | + var analysis = await GetAnalysisAsync(code); |
| 50 | + analysis.Diagnostics.Should().HaveCount(1); |
| 51 | + |
| 52 | + var diagnostic = analysis.Diagnostics.ElementAt(0); |
| 53 | + diagnostic.SourceSpan.Should().Be(7, 9, 7, 25); |
| 54 | + diagnostic.Severity.Should().Be(Severity.Warning); |
| 55 | + diagnostic.ErrorCode.Should().Be(Diagnostics.ErrorCodes.ReturnInInit); |
| 56 | + diagnostic.Message.Should().Be(Resources.ReturnInInit); |
| 57 | + } |
| 58 | + |
| 59 | + [TestMethod, Priority(0)] |
| 60 | + public async Task ReturnInitBasic() { |
| 61 | + const string code = @" |
| 62 | +class Rectangle: |
| 63 | + def __init__(self, width, height): |
| 64 | + return 2 |
| 65 | +
|
| 66 | +r = Rectangle(10, 10) |
| 67 | +"; |
| 68 | + var analysis = await GetAnalysisAsync(code); |
| 69 | + analysis.Diagnostics.Should().HaveCount(1); |
| 70 | + |
| 71 | + var diagnostic = analysis.Diagnostics.ElementAt(0); |
| 72 | + diagnostic.SourceSpan.Should().Be(4, 9, 4, 17); |
| 73 | + diagnostic.Severity.Should().Be(Severity.Warning); |
| 74 | + diagnostic.ErrorCode.Should().Be(Diagnostics.ErrorCodes.ReturnInInit); |
| 75 | + diagnostic.Message.Should().Be(Resources.ReturnInInit); |
| 76 | + } |
| 77 | + |
| 78 | + [TestMethod, Priority(0)] |
| 79 | + public async Task ReturnInInitConditional() { |
| 80 | + const string code = @" |
| 81 | +class A: |
| 82 | + def __init__(self, x): |
| 83 | + self.x = x |
| 84 | + if x > 0: |
| 85 | + return 10 |
| 86 | +
|
| 87 | +a = A(1) |
| 88 | +"; |
| 89 | + var analysis = await GetAnalysisAsync(code); |
| 90 | + analysis.Diagnostics.Should().HaveCount(1); |
| 91 | + |
| 92 | + var diagnostic = analysis.Diagnostics.ElementAt(0); |
| 93 | + diagnostic.SourceSpan.Should().Be(6, 13, 6, 22); |
| 94 | + diagnostic.Severity.Should().Be(Severity.Warning); |
| 95 | + diagnostic.ErrorCode.Should().Be(Diagnostics.ErrorCodes.ReturnInInit); |
| 96 | + diagnostic.Message.Should().Be(Resources.ReturnInInit); |
| 97 | + } |
| 98 | + |
| 99 | + [TestMethod, Priority(0)] |
| 100 | + public async Task ReturnNoneInInit() { |
| 101 | + const string code = @" |
| 102 | +class A: |
| 103 | + def __init__(self, x): |
| 104 | + self.x = x |
| 105 | + self.x += 1 |
| 106 | + return None |
| 107 | +
|
| 108 | +a = A(1) |
| 109 | +"; |
| 110 | + var analysis = await GetAnalysisAsync(code); |
| 111 | + analysis.Diagnostics.Should().BeEmpty(); |
| 112 | + } |
| 113 | + |
| 114 | + [TestMethod, Priority(0)] |
| 115 | + public async Task EmptyReturnInInit() { |
| 116 | + const string code = @" |
| 117 | +class A: |
| 118 | + def __init__(self, x): |
| 119 | + self.x = x |
| 120 | + return |
| 121 | +a = A(1) |
| 122 | +"; |
| 123 | + var analysis = await GetAnalysisAsync(code); |
| 124 | + analysis.Diagnostics.Should().BeEmpty(); |
| 125 | + } |
| 126 | + } |
| 127 | +} |
0 commit comments