|
| 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.Diagnostics; |
| 20 | +using Microsoft.Python.Analysis.Tests.FluentAssertions; |
| 21 | +using Microsoft.Python.Core; |
| 22 | +using Microsoft.Python.Parsing.Tests; |
| 23 | +using Microsoft.VisualStudio.TestTools.UnitTesting; |
| 24 | +using TestUtilities; |
| 25 | + |
| 26 | +namespace Microsoft.Python.Analysis.Tests { |
| 27 | + [TestClass] |
| 28 | + public class InheritNonClassTests : AnalysisTestBase { |
| 29 | + public TestContext TestContext { get; set; } |
| 30 | + |
| 31 | + [TestInitialize] |
| 32 | + public void TestInitialize() |
| 33 | + => TestEnvironmentImpl.TestInitialize($"{TestContext.FullyQualifiedTestClassName}.{TestContext.TestName}"); |
| 34 | + |
| 35 | + [TestCleanup] |
| 36 | + public void Cleanup() => TestEnvironmentImpl.TestCleanup(); |
| 37 | + |
| 38 | + [TestMethod, Priority(0)] |
| 39 | + public async Task InheritFromRenamedBuiltin() { |
| 40 | + const string code = @" |
| 41 | +tmp = str |
| 42 | +
|
| 43 | +class C(tmp): |
| 44 | + def method(self): |
| 45 | + return 'test' |
| 46 | +"; |
| 47 | + var analysis = await GetAnalysisAsync(code); |
| 48 | + analysis.Diagnostics.Should().BeEmpty(); |
| 49 | + } |
| 50 | + |
| 51 | + |
| 52 | + [TestMethod, Priority(0)] |
| 53 | + public async Task InheritFromBuiltin() { |
| 54 | + const string code = @" |
| 55 | +class C(str): |
| 56 | + def method(self): |
| 57 | + return 'test' |
| 58 | +"; |
| 59 | + var analysis = await GetAnalysisAsync(code); |
| 60 | + analysis.Diagnostics.Should().BeEmpty(); |
| 61 | + } |
| 62 | + |
| 63 | + |
| 64 | + [TestMethod, Priority(0)] |
| 65 | + public async Task InheritFromUserClass() { |
| 66 | + const string code = @" |
| 67 | +class D: |
| 68 | + def hello(self): |
| 69 | + pass |
| 70 | +
|
| 71 | +class C(D): |
| 72 | + def method(self): |
| 73 | + return 'test' |
| 74 | +"; |
| 75 | + var analysis = await GetAnalysisAsync(code); |
| 76 | + analysis.Diagnostics.Should().BeEmpty(); |
| 77 | + } |
| 78 | + |
| 79 | + |
| 80 | + [TestMethod, Priority(0)] |
| 81 | + public async Task InheritFromConstant() { |
| 82 | + const string code = @" |
| 83 | +class C(5): |
| 84 | + def method(self): |
| 85 | + return 'test' |
| 86 | +"; |
| 87 | + var analysis = await GetAnalysisAsync(code); |
| 88 | + analysis.Diagnostics.Should().HaveCount(1); |
| 89 | + |
| 90 | + var diagnostic = analysis.Diagnostics.ElementAt(0); |
| 91 | + diagnostic.SourceSpan.Should().Be(2, 7, 2, 8); |
| 92 | + diagnostic.Message.Should().Be(Resources.InheritNonClass.FormatInvariant("5")); |
| 93 | + diagnostic.ErrorCode.Should().Be(ErrorCodes.InheritNonClass); |
| 94 | + } |
| 95 | + |
| 96 | + |
| 97 | + [TestMethod, Priority(0)] |
| 98 | + public async Task InheritFromConstantVar() { |
| 99 | + const string code = @" |
| 100 | +x = 'str' |
| 101 | +
|
| 102 | +class C(x): |
| 103 | + def method(self): |
| 104 | + return 'test' |
| 105 | +
|
| 106 | +x = 5 |
| 107 | +
|
| 108 | +class D(x): |
| 109 | + def method(self): |
| 110 | + return 'test' |
| 111 | +"; |
| 112 | + var analysis = await GetAnalysisAsync(code); |
| 113 | + analysis.Diagnostics.Should().HaveCount(2); |
| 114 | + |
| 115 | + var diagnostic = analysis.Diagnostics.ElementAt(0); |
| 116 | + diagnostic.SourceSpan.Should().Be(4, 7, 4, 8); |
| 117 | + diagnostic.Message.Should().Be(Resources.InheritNonClass.FormatInvariant("str")); |
| 118 | + diagnostic.ErrorCode.Should().Be(ErrorCodes.InheritNonClass); |
| 119 | + |
| 120 | + diagnostic = analysis.Diagnostics.ElementAt(1); |
| 121 | + diagnostic.SourceSpan.Should().Be(10, 7, 10, 8); |
| 122 | + diagnostic.Message.Should().Be(Resources.InheritNonClass.FormatInvariant("5")); |
| 123 | + diagnostic.ErrorCode.Should().Be(ErrorCodes.InheritNonClass); |
| 124 | + } |
| 125 | + |
| 126 | + [Ignore] |
| 127 | + [TestMethod, Priority(0)] |
| 128 | + public async Task InheritFromBinaryOp() { |
| 129 | + const string code = @" |
| 130 | +x = 5 |
| 131 | +
|
| 132 | +class C(x + 2): |
| 133 | + def method(self): |
| 134 | + return 'test' |
| 135 | +"; |
| 136 | + var analysis = await GetAnalysisAsync(code); |
| 137 | + analysis.Diagnostics.Should().HaveCount(1); |
| 138 | + |
| 139 | + var diagnostic = analysis.Diagnostics.ElementAt(0); |
| 140 | + diagnostic.SourceSpan.Should().Be(4, 7, 4, 8); |
| 141 | + diagnostic.Message.Should().Be(Resources.InheritNonClass.FormatInvariant("x + 2")); |
| 142 | + diagnostic.ErrorCode.Should().Be(ErrorCodes.InheritNonClass); |
| 143 | + } |
| 144 | + |
| 145 | + |
| 146 | + [TestMethod, Priority(0)] |
| 147 | + public async Task InheritFromOtherModule() { |
| 148 | + const string code = @" |
| 149 | +import typing |
| 150 | +
|
| 151 | +class C(typing.TypeVar): |
| 152 | + def method(self): |
| 153 | + return 'test' |
| 154 | +"; |
| 155 | + var analysis = await GetAnalysisAsync(code); |
| 156 | + analysis.Diagnostics.Should().BeEmpty(); |
| 157 | + } |
| 158 | + |
| 159 | + [TestMethod, Priority(0)] |
| 160 | + public async Task InheritFromRenamedOtherModule() { |
| 161 | + const string code = @" |
| 162 | +import typing |
| 163 | +
|
| 164 | +tmp = typing.TypeVar |
| 165 | +class C(tmp): |
| 166 | + def method(self): |
| 167 | + return 'test' |
| 168 | +"; |
| 169 | + var analysis = await GetAnalysisAsync(code); |
| 170 | + analysis.Diagnostics.Should().BeEmpty(); |
| 171 | + } |
| 172 | + } |
| 173 | +} |
0 commit comments