|
| 1 | +using System.Linq; |
| 2 | +using System.Threading.Tasks; |
| 3 | +using FluentAssertions; |
| 4 | +using Microsoft.Python.Analysis.Tests.FluentAssertions; |
| 5 | +using Microsoft.Python.Core; |
| 6 | +using Microsoft.Python.Parsing.Tests; |
| 7 | +using Microsoft.VisualStudio.TestTools.UnitTesting; |
| 8 | +using TestUtilities; |
| 9 | + |
| 10 | +namespace Microsoft.Python.Analysis.Tests { |
| 11 | + |
| 12 | + [TestClass] |
| 13 | + public class LintTypeVarTests : AnalysisTestBase { |
| 14 | + public const string TypeVarImport = @" |
| 15 | +from typing import TypeVar |
| 16 | +"; |
| 17 | + |
| 18 | + public TestContext TestContext { get; set; } |
| 19 | + |
| 20 | + [TestInitialize] |
| 21 | + public void TestInitialize() |
| 22 | + => TestEnvironmentImpl.TestInitialize($"{TestContext.FullyQualifiedTestClassName}.{TestContext.TestName}"); |
| 23 | + |
| 24 | + [TestCleanup] |
| 25 | + public void Cleanup() => TestEnvironmentImpl.TestCleanup(); |
| 26 | + |
| 27 | + [DataRow(TypeVarImport + @" |
| 28 | +T = TypeVar(1, 2, 3)")] |
| 29 | + [DataRow(TypeVarImport + @" |
| 30 | +T = TypeVar(2.0, 3)")] |
| 31 | + [DataRow(TypeVarImport + @" |
| 32 | +class C: |
| 33 | + int: t |
| 34 | + __init__(t): |
| 35 | + self.x = t |
| 36 | +
|
| 37 | +test = C(5) |
| 38 | +T = TypeVar(C, 3) |
| 39 | +")] |
| 40 | + [DataRow(TypeVarImport + @" |
| 41 | +T = TypeVar(1f) |
| 42 | +")] |
| 43 | + [DataTestMethod, Priority(0)] |
| 44 | + public async Task TypeVarFirstArgumentNotString(string code) { |
| 45 | + var analysis = await GetAnalysisAsync(code); |
| 46 | + analysis.Diagnostics.Should().HaveCount(1); |
| 47 | + |
| 48 | + var diagnostic = analysis.Diagnostics.ElementAt(0); |
| 49 | + diagnostic.ErrorCode.Should().Be(Diagnostics.ErrorCodes.TypingTypeVarArguments); |
| 50 | + diagnostic.Message.Should().Be(Resources.TypeVarFirstArgumentNotString); |
| 51 | + } |
| 52 | + |
| 53 | + [TestMethod, Priority(0)] |
| 54 | + public async Task TypeVarFirstArgumentFunction() { |
| 55 | + const string code = @" |
| 56 | +from typing import TypeVar |
| 57 | +
|
| 58 | +def temp(): |
| 59 | + return 'str' |
| 60 | +
|
| 61 | +T = TypeVar(temp(), int, str) |
| 62 | +"; |
| 63 | + |
| 64 | + var analysis = await GetAnalysisAsync(code); |
| 65 | + analysis.Diagnostics.Should().BeEmpty(); |
| 66 | + } |
| 67 | + |
| 68 | + [DataRow(TypeVarImport + @" |
| 69 | +T = TypeVar('T', int, str)")] |
| 70 | + [DataRow(TypeVarImport + @" |
| 71 | +F = TypeVar('F',double, complex)")] |
| 72 | + [DataRow(TypeVarImport + @" |
| 73 | +T = TypeVar('T') |
| 74 | +")] |
| 75 | + [DataRow(TypeVarImport + @" |
| 76 | +T = TypeVar('T', float, int) |
| 77 | +")] |
| 78 | + [DataTestMethod, Priority(0)] |
| 79 | + public async Task TypeVarNoDiagnosticOnValidUse(string code) { |
| 80 | + var analysis = await GetAnalysisAsync(code); |
| 81 | + analysis.Diagnostics.Should().BeEmpty(); |
| 82 | + } |
| 83 | + |
| 84 | + |
| 85 | + [TestMethod, Priority(0)] |
| 86 | + public async Task TypeVarNoArguments() { |
| 87 | + const string code = @" |
| 88 | +from typing import TypeVar |
| 89 | +
|
| 90 | +T = TypeVar() |
| 91 | +"; |
| 92 | + var analysis = await GetAnalysisAsync(code); |
| 93 | + analysis.Diagnostics.Should().HaveCount(1); |
| 94 | + |
| 95 | + var diagnostic = analysis.Diagnostics.ElementAt(0); |
| 96 | + diagnostic.ErrorCode.Should().Be(Diagnostics.ErrorCodes.ParameterMissing); |
| 97 | + diagnostic.Message.Should().Be(Resources.Analysis_ParameterMissing.FormatInvariant("name")); |
| 98 | + diagnostic.SourceSpan.Should().Be(4, 5, 4, 14); |
| 99 | + } |
| 100 | + |
| 101 | + [DataRow("T = TypeVar('T', 'test_constraint')")] |
| 102 | + [DataRow("T = TypeVar('T', int)")] |
| 103 | + [DataRow("T = TypeVar('T', complex)")] |
| 104 | + [DataRow("T = TypeVar('T', str)")] |
| 105 | + [DataRow("T = TypeVar('T', 5)")] |
| 106 | + [DataTestMethod, Priority(0)] |
| 107 | + public async Task TypeVarOneConstraint(string decl) { |
| 108 | + string code = TypeVarImport + decl; |
| 109 | + var analysis = await GetAnalysisAsync(code); |
| 110 | + analysis.Diagnostics.Should().HaveCount(1); |
| 111 | + |
| 112 | + var diagnostic = analysis.Diagnostics.ElementAt(0); |
| 113 | + diagnostic.ErrorCode.Should().Be(Diagnostics.ErrorCodes.TypingTypeVarArguments); |
| 114 | + diagnostic.Message.Should().Be(Resources.TypeVarSingleConstraint); |
| 115 | + diagnostic.SourceSpan.Should().Be(3, 5, 3, decl.IndexOf(")") + 2); |
| 116 | + } |
| 117 | + } |
| 118 | +} |
0 commit comments