From 9cb85f83e22fed32bb2a05baf23d9a4435108ce6 Mon Sep 17 00:00:00 2001 From: Oleksandr Liakhevych Date: Tue, 31 Dec 2024 14:23:02 +0200 Subject: [PATCH 1/5] Fix DateOnly / TimeOnly parameters is TestCaseUsage --- global.json | 2 +- src/Directory.Build.props | 8 +-- ...uteArgumentTypedConstantExtensionsTests.cs | 6 ++ .../TestCaseUsageAnalyzerTests.cs | 25 ++++++++- .../nunit.analyzers.tests.csproj | 2 +- .../EqualConstraintUsageCodeFix.cs | 10 ++-- ...ttributeArgumentTypedConstantExtensions.cs | 55 ++++++++++--------- src/nunit.analyzers/nunit.analyzers.csproj | 1 - 8 files changed, 71 insertions(+), 38 deletions(-) diff --git a/global.json b/global.json index dffff818..c7e4561c 100644 --- a/global.json +++ b/global.json @@ -1,6 +1,6 @@ { "sdk": { - "version": "6.0.100", + "version": "8.0.100", "rollForward" : "latestFeature", "allowPrerelease": false } diff --git a/src/Directory.Build.props b/src/Directory.Build.props index c48ec898..df924d01 100644 --- a/src/Directory.Build.props +++ b/src/Directory.Build.props @@ -22,7 +22,7 @@ - + @@ -31,9 +31,9 @@ false - - - + + + diff --git a/src/nunit.analyzers.tests/Extensions/AttributeArgumentTypedConstantExtensionsTests.cs b/src/nunit.analyzers.tests/Extensions/AttributeArgumentTypedConstantExtensionsTests.cs index 923a5f34..22de52f6 100644 --- a/src/nunit.analyzers.tests/Extensions/AttributeArgumentTypedConstantExtensionsTests.cs +++ b/src/nunit.analyzers.tests/Extensions/AttributeArgumentTypedConstantExtensionsTests.cs @@ -99,6 +99,12 @@ private static IEnumerable GetTestData() SetName("CanAssignToWhenParameterIsVersionAndArgumentIsValidString"); yield return new TestCaseData("\"a.b.c.d\"", "Version", "object", Is.True). SetName("CanAssignToWhenParameterIsVersionAndArgumentIsInValidString"); +#if NET8_0_OR_GREATER + yield return new TestCaseData("\"00:03:00\"", "TimeOnly", "string", Is.True). + SetName("CanAssignToWhenParameterIsTimeSpanAndArgumentIsValidString"); + yield return new TestCaseData("\"2020-05-08\"", "DateOnly", "string", Is.True). + SetName("CanAssignToWhenParameterIsDateTimeAndArgumentIsValidString"); +#endif } private static async Task<(TypedConstant argumentConstant, ITypeSymbol typeSymbol, Compilation compilation)> diff --git a/src/nunit.analyzers.tests/TestCaseUsage/TestCaseUsageAnalyzerTests.cs b/src/nunit.analyzers.tests/TestCaseUsage/TestCaseUsageAnalyzerTests.cs index 499a8917..a11707ac 100644 --- a/src/nunit.analyzers.tests/TestCaseUsage/TestCaseUsageAnalyzerTests.cs +++ b/src/nunit.analyzers.tests/TestCaseUsage/TestCaseUsageAnalyzerTests.cs @@ -29,6 +29,17 @@ private static IEnumerable SpecialConversions } } +#if NET8_0_OR_GREATER + private static IEnumerable SpecialConversions_NET8 + { + get + { + yield return new TestCaseData("2019-10-10", typeof(DateOnly)); + yield return new TestCaseData("23:59:59", typeof(TimeOnly)); + } + } +#endif + [Test] public void VerifySupportedDiagnostics() { @@ -757,7 +768,7 @@ public void TestWithGenericParameter(T arg1) { } } #if NUNIT4 -#if NET6_0_OR_GREATER +#if NET8_0_OR_GREATER [Test] public void AnalyzeWhenArgumentIsCorrectGenericTypeParameter() { @@ -824,6 +835,18 @@ public void TestWithGenericParameter(T arg1) { } }"); RoslynAssert.Valid(this.analyzer, testCode); } + + [TestCaseSource(nameof(SpecialConversions_NET8))] + public void AnalyzeWhenArgumentIsSpecialConversionNET8(string value, Type targetType) + { + var testCode = TestUtility.WrapClassInNamespaceAndAddUsing($@" + public sealed class AnalyzeWhenArgumentIsSpecialConversion + {{ + [TestCase(""{value}"")] + public void Test({targetType.Name} a) {{ }} + }}"); + RoslynAssert.Valid(this.analyzer, testCode); + } #endif [Test] diff --git a/src/nunit.analyzers.tests/nunit.analyzers.tests.csproj b/src/nunit.analyzers.tests/nunit.analyzers.tests.csproj index 4ea04195..e9725bc2 100644 --- a/src/nunit.analyzers.tests/nunit.analyzers.tests.csproj +++ b/src/nunit.analyzers.tests/nunit.analyzers.tests.csproj @@ -1,7 +1,7 @@ NUnit.Analyzers.Tests - net6.0;net462 + net8.0;net462 4 diff --git a/src/nunit.analyzers/ConstraintUsage/EqualConstraintUsageCodeFix.cs b/src/nunit.analyzers/ConstraintUsage/EqualConstraintUsageCodeFix.cs index cc059520..bd1f3da7 100644 --- a/src/nunit.analyzers/ConstraintUsage/EqualConstraintUsageCodeFix.cs +++ b/src/nunit.analyzers/ConstraintUsage/EqualConstraintUsageCodeFix.cs @@ -26,7 +26,7 @@ protected override (ExpressionSyntax? actual, ExpressionSyntax? constraintExpres } else if (expected is PatternSyntax pattern) { - constraintExpression = this.ConvertPattern( + constraintExpression = ConvertPattern( SyntaxFactory.IdentifierName(NUnitFrameworkConstants.NameOfIs), pattern); } @@ -91,7 +91,7 @@ private static (ExpressionSyntax? actual, ExpressionOrPatternSyntax? expected) G /// supported-pattern or supported-pattern, /// supported-pattern and supported-pattern. /// - private InvocationExpressionSyntax? ConvertPattern(ExpressionSyntax member, PatternSyntax pattern) + private static InvocationExpressionSyntax? ConvertPattern(ExpressionSyntax member, PatternSyntax pattern) { if (pattern is ConstantPatternSyntax constantPattern) { @@ -125,7 +125,7 @@ private static (ExpressionSyntax? actual, ExpressionOrPatternSyntax? expected) G } else if (pattern is UnaryPatternSyntax unaryPattern && unaryPattern.IsKind(SyntaxKind.NotPattern)) { - return this.ConvertPattern( + return ConvertPattern( SyntaxFactory.MemberAccessExpression( SyntaxKind.SimpleMemberAccessExpression, member, @@ -143,11 +143,11 @@ private static (ExpressionSyntax? actual, ExpressionOrPatternSyntax? expected) G if (constraint is not null) { - InvocationExpressionSyntax? leftExpression = this.ConvertPattern(member, binaryPattern.Left); + InvocationExpressionSyntax? leftExpression = ConvertPattern(member, binaryPattern.Left); if (leftExpression is not null) { - return this.ConvertPattern( + return ConvertPattern( SyntaxFactory.MemberAccessExpression( SyntaxKind.SimpleMemberAccessExpression, leftExpression, diff --git a/src/nunit.analyzers/Extensions/AttributeArgumentTypedConstantExtensions.cs b/src/nunit.analyzers/Extensions/AttributeArgumentTypedConstantExtensions.cs index 60d3e035..9fa71f71 100644 --- a/src/nunit.analyzers/Extensions/AttributeArgumentTypedConstantExtensions.cs +++ b/src/nunit.analyzers/Extensions/AttributeArgumentTypedConstantExtensions.cs @@ -26,32 +26,37 @@ internal static class AttributeArgumentTypedConstantExtensions // Intrinsic type converters for types // https://github.com/dotnet/runtime/blob/master/src/libraries/System.ComponentModel.TypeConverter/src/System/ComponentModel/ReflectTypeDescriptionProvider.cs - // For converters that exist in .NET Standard 1.6 we use the converter. + // For converters that exist in .NET Standard 2.0 we use the converter. // Otherwise, we assume that the converter exists and it can convert the value. - private static readonly List<(Type type, Lazy? typeConverter)> IntrinsicTypeConverters = + private static readonly List<(string fullName, Lazy? typeConverter)> IntrinsicTypeConverters = new() { - (typeof(bool), new Lazy(() => new BooleanConverter())), - (typeof(byte), new Lazy(() => new ByteConverter())), - (typeof(sbyte), new Lazy(() => new SByteConverter())), - (typeof(char), new Lazy(() => new CharConverter())), - (typeof(double), new Lazy(() => new DoubleConverter())), - (typeof(string), new Lazy(() => new StringConverter())), - (typeof(int), new Lazy(() => new Int32Converter())), - (typeof(short), new Lazy(() => new Int16Converter())), - (typeof(long), new Lazy(() => new Int64Converter())), - (typeof(float), new Lazy(() => new SingleConverter())), - (typeof(ushort), new Lazy(() => new UInt16Converter())), - (typeof(uint), new Lazy(() => new UInt32Converter())), - (typeof(ulong), new Lazy(() => new UInt64Converter())), - (typeof(CultureInfo), null), - (typeof(DateTime), new Lazy(() => new DateTimeConverter())), - (typeof(DateTimeOffset), new Lazy(() => new DateTimeOffsetConverter())), - (typeof(decimal), new Lazy(() => new DecimalConverter())), - (typeof(TimeSpan), new Lazy(() => new TimeSpanConverter())), - (typeof(Guid), new Lazy(() => new GuidConverter())), - (typeof(Uri), new Lazy(() => new UriTypeConverter())), - (typeof(Version), null), + ("System.Boolean", new Lazy(() => new BooleanConverter())), + ("System.Byte", new Lazy(() => new ByteConverter())), + ("System.SByte", new Lazy(() => new SByteConverter())), + ("System.Char", new Lazy(() => new CharConverter())), + ("System.Double", new Lazy(() => new DoubleConverter())), + ("System.String", new Lazy(() => new StringConverter())), + ("System.Int32", new Lazy(() => new Int32Converter())), + ("System.Int16", new Lazy(() => new Int16Converter())), + ("System.Int64", new Lazy(() => new Int64Converter())), + ("System.Int128", null), + ("System.Half", null), + ("System.Single", new Lazy(() => new SingleConverter())), + ("System.UInt16", new Lazy(() => new UInt16Converter())), + ("System.UInt32", new Lazy(() => new UInt32Converter())), + ("System.UInt64", new Lazy(() => new UInt64Converter())), + ("System.UInt128", null), + ("System.Globalization.CultureInfo", null), + ("System.DateTime", new Lazy(() => new DateTimeConverter())), + ("System.DateTimeOffset", new Lazy(() => new DateTimeOffsetConverter())), + ("System.DateOnly", null), + ("System.Decimal", new Lazy(() => new DecimalConverter())), + ("System.TimeOnly", null), + ("System.TimeSpan", new Lazy(() => new TimeSpanConverter())), + ("System.Guid", new Lazy(() => new GuidConverter())), + ("System.Uri", new Lazy(() => new UriTypeConverter())), + ("System.Version", null) }; internal static bool CanAssignTo(this TypedConstant @this, ITypeSymbol target, Compilation compilation, @@ -214,7 +219,7 @@ private static bool CanBeTranslatedByTypeConverter( Compilation compilation) { var typeName = targetTypeSymbol.GetFullMetadataName(); - var targetType = IntrinsicTypeConverters.FirstOrDefault(t => t.type.FullName == typeName); + var targetType = IntrinsicTypeConverters.FirstOrDefault(t => t.fullName == typeName); if (targetType != default) { @@ -254,7 +259,7 @@ private static bool CanBeTranslatedByTypeConverter( return false; } -#endregion + #endregion #pragma warning disable SA1202 // Elements should be ordered by access. Prefer grouping internal static ImmutableArray AdjustArguments(this ImmutableArray attributePositionalArguments) diff --git a/src/nunit.analyzers/nunit.analyzers.csproj b/src/nunit.analyzers/nunit.analyzers.csproj index 993785a5..45f92b31 100644 --- a/src/nunit.analyzers/nunit.analyzers.csproj +++ b/src/nunit.analyzers/nunit.analyzers.csproj @@ -12,7 +12,6 @@ - From 824644b1e6f8f8aebbbb0ebadfa4313c5953cac0 Mon Sep 17 00:00:00 2001 From: Oleksandr Liakhevych Date: Tue, 31 Dec 2024 14:34:08 +0200 Subject: [PATCH 2/5] Fix ci build?... --- appveyor.yml | 2 +- .../AvoidUninstantiatedInternalClassesSuppressorTests.cs | 2 +- .../DiagnosticSuppressors/DisposableFieldsSuppressorTests.cs | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/appveyor.yml b/appveyor.yml index dd82465d..8e23fc2d 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -1,7 +1,7 @@ image: Visual Studio 2022 environment: - CLI_CHANNEL: 6.0.1xx + CLI_CHANNEL: 8.0.xxx install: - ps: $env:DOTNET_INSTALL_DIR = "$pwd\.dotnetsdk" diff --git a/src/nunit.analyzers.tests/DiagnosticSuppressors/AvoidUninstantiatedInternalClassesSuppressorTests.cs b/src/nunit.analyzers.tests/DiagnosticSuppressors/AvoidUninstantiatedInternalClassesSuppressorTests.cs index 8dbba997..1f44b734 100644 --- a/src/nunit.analyzers.tests/DiagnosticSuppressors/AvoidUninstantiatedInternalClassesSuppressorTests.cs +++ b/src/nunit.analyzers.tests/DiagnosticSuppressors/AvoidUninstantiatedInternalClassesSuppressorTests.cs @@ -59,7 +59,7 @@ public void OneTimeSetUp() { // Find the NetAnalyzers assembly (note version should match the one referenced) string netAnalyzersPath = Path.Combine(PathHelper.GetNuGetPackageDirectory(), - "microsoft.codeanalysis.netanalyzers/7.0.4/analyzers/dotnet/cs/Microsoft.CodeAnalysis.CSharp.NetAnalyzers.dll"); + "microsoft.codeanalysis.netanalyzers/8.0.0/analyzers/dotnet/cs/Microsoft.CodeAnalysis.CSharp.NetAnalyzers.dll"); Assembly netAnalyzerAssembly = Assembly.LoadFrom(netAnalyzersPath); Type analyzerType = netAnalyzerAssembly.GetType("Microsoft.CodeQuality.CSharp.Analyzers.Maintainability.CSharpAvoidUninstantiatedInternalClasses", true)!; this.analyzer = (DiagnosticAnalyzer)Activator.CreateInstance(analyzerType)!; diff --git a/src/nunit.analyzers.tests/DiagnosticSuppressors/DisposableFieldsSuppressorTests.cs b/src/nunit.analyzers.tests/DiagnosticSuppressors/DisposableFieldsSuppressorTests.cs index 6b1899f3..44ccd9a6 100644 --- a/src/nunit.analyzers.tests/DiagnosticSuppressors/DisposableFieldsSuppressorTests.cs +++ b/src/nunit.analyzers.tests/DiagnosticSuppressors/DisposableFieldsSuppressorTests.cs @@ -19,7 +19,7 @@ public void OneTimeSetUp() { // Find the NetAnalyzers assembly (note version should match the one referenced) string netAnalyzersPath = Path.Combine(PathHelper.GetNuGetPackageDirectory(), - "microsoft.codeanalysis.netanalyzers/7.0.4/analyzers/dotnet/cs/Microsoft.CodeAnalysis.CSharp.NetAnalyzers.dll"); + "microsoft.codeanalysis.netanalyzers/8.0.0/analyzers/dotnet/cs/Microsoft.CodeAnalysis.CSharp.NetAnalyzers.dll"); Assembly netAnalyzerAssembly = Assembly.LoadFrom(netAnalyzersPath); Type analyzerType = netAnalyzerAssembly.GetType("Microsoft.CodeQuality.CSharp.Analyzers.ApiDesignGuidelines.CSharpTypesThatOwnDisposableFieldsShouldBeDisposableAnalyzer", true)!; this.analyzer = (DiagnosticAnalyzer)Activator.CreateInstance(analyzerType)!; From 04ad8334e77449683933a2a7695e39526c90951b Mon Sep 17 00:00:00 2001 From: Oleksandr Liakhevych Date: Tue, 31 Dec 2024 14:45:17 +0200 Subject: [PATCH 3/5] Fix tests --- appveyor.yml | 2 +- .../DiagnosticSuppressors/DisposableFieldsSuppressorTests.cs | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/appveyor.yml b/appveyor.yml index 8e23fc2d..b8ace0fc 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -1,7 +1,7 @@ image: Visual Studio 2022 environment: - CLI_CHANNEL: 8.0.xxx + CLI_CHANNEL: 8.0 install: - ps: $env:DOTNET_INSTALL_DIR = "$pwd\.dotnetsdk" diff --git a/src/nunit.analyzers.tests/DiagnosticSuppressors/DisposableFieldsSuppressorTests.cs b/src/nunit.analyzers.tests/DiagnosticSuppressors/DisposableFieldsSuppressorTests.cs index 44ccd9a6..0466dd11 100644 --- a/src/nunit.analyzers.tests/DiagnosticSuppressors/DisposableFieldsSuppressorTests.cs +++ b/src/nunit.analyzers.tests/DiagnosticSuppressors/DisposableFieldsSuppressorTests.cs @@ -19,9 +19,9 @@ public void OneTimeSetUp() { // Find the NetAnalyzers assembly (note version should match the one referenced) string netAnalyzersPath = Path.Combine(PathHelper.GetNuGetPackageDirectory(), - "microsoft.codeanalysis.netanalyzers/8.0.0/analyzers/dotnet/cs/Microsoft.CodeAnalysis.CSharp.NetAnalyzers.dll"); + "microsoft.codeanalysis.netanalyzers/8.0.0/analyzers/dotnet/cs/Microsoft.CodeAnalysis.NetAnalyzers.dll"); Assembly netAnalyzerAssembly = Assembly.LoadFrom(netAnalyzersPath); - Type analyzerType = netAnalyzerAssembly.GetType("Microsoft.CodeQuality.CSharp.Analyzers.ApiDesignGuidelines.CSharpTypesThatOwnDisposableFieldsShouldBeDisposableAnalyzer", true)!; + Type analyzerType = netAnalyzerAssembly.GetType("Microsoft.CodeQuality.Analyzers.ApiDesignGuidelines.TypesThatOwnDisposableFieldsShouldBeDisposableAnalyzer", true)!; this.analyzer = (DiagnosticAnalyzer)Activator.CreateInstance(analyzerType)!; this.analyzer = new DefaultEnabledAnalyzer(this.analyzer); From fc1f56897635670e560fa9657ddd52b9db27a940 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9E=D0=BB=D0=B5=D0=BA=D1=81=D0=B0=D0=BD=D0=B4=D1=80=20?= =?UTF-8?q?=D0=9B=D1=8F=D1=85=D0=B5=D0=B2=D0=B8=D1=87?= Date: Mon, 6 Jan 2025 14:50:16 +0200 Subject: [PATCH 4/5] Revert to dotnet 6.0 --- appveyor.yml | 2 +- global.json | 2 +- src/Directory.Build.props | 8 ++++---- ...voidUninstantiatedInternalClassesSuppressorTests.cs | 2 +- .../DisposableFieldsSuppressorTests.cs | 4 ++-- .../AttributeArgumentTypedConstantExtensionsTests.cs | 2 +- .../TestCaseUsage/TestCaseUsageAnalyzerTests.cs | 10 +++++----- src/nunit.analyzers.tests/nunit.analyzers.tests.csproj | 2 +- .../ConstraintUsage/EqualConstraintUsageCodeFix.cs | 10 +++++----- 9 files changed, 21 insertions(+), 21 deletions(-) diff --git a/appveyor.yml b/appveyor.yml index b8ace0fc..dd82465d 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -1,7 +1,7 @@ image: Visual Studio 2022 environment: - CLI_CHANNEL: 8.0 + CLI_CHANNEL: 6.0.1xx install: - ps: $env:DOTNET_INSTALL_DIR = "$pwd\.dotnetsdk" diff --git a/global.json b/global.json index c7e4561c..dffff818 100644 --- a/global.json +++ b/global.json @@ -1,6 +1,6 @@ { "sdk": { - "version": "8.0.100", + "version": "6.0.100", "rollForward" : "latestFeature", "allowPrerelease": false } diff --git a/src/Directory.Build.props b/src/Directory.Build.props index df924d01..c48ec898 100644 --- a/src/Directory.Build.props +++ b/src/Directory.Build.props @@ -22,7 +22,7 @@ - + @@ -31,9 +31,9 @@ false - - - + + + diff --git a/src/nunit.analyzers.tests/DiagnosticSuppressors/AvoidUninstantiatedInternalClassesSuppressorTests.cs b/src/nunit.analyzers.tests/DiagnosticSuppressors/AvoidUninstantiatedInternalClassesSuppressorTests.cs index 1f44b734..8dbba997 100644 --- a/src/nunit.analyzers.tests/DiagnosticSuppressors/AvoidUninstantiatedInternalClassesSuppressorTests.cs +++ b/src/nunit.analyzers.tests/DiagnosticSuppressors/AvoidUninstantiatedInternalClassesSuppressorTests.cs @@ -59,7 +59,7 @@ public void OneTimeSetUp() { // Find the NetAnalyzers assembly (note version should match the one referenced) string netAnalyzersPath = Path.Combine(PathHelper.GetNuGetPackageDirectory(), - "microsoft.codeanalysis.netanalyzers/8.0.0/analyzers/dotnet/cs/Microsoft.CodeAnalysis.CSharp.NetAnalyzers.dll"); + "microsoft.codeanalysis.netanalyzers/7.0.4/analyzers/dotnet/cs/Microsoft.CodeAnalysis.CSharp.NetAnalyzers.dll"); Assembly netAnalyzerAssembly = Assembly.LoadFrom(netAnalyzersPath); Type analyzerType = netAnalyzerAssembly.GetType("Microsoft.CodeQuality.CSharp.Analyzers.Maintainability.CSharpAvoidUninstantiatedInternalClasses", true)!; this.analyzer = (DiagnosticAnalyzer)Activator.CreateInstance(analyzerType)!; diff --git a/src/nunit.analyzers.tests/DiagnosticSuppressors/DisposableFieldsSuppressorTests.cs b/src/nunit.analyzers.tests/DiagnosticSuppressors/DisposableFieldsSuppressorTests.cs index 0466dd11..6b1899f3 100644 --- a/src/nunit.analyzers.tests/DiagnosticSuppressors/DisposableFieldsSuppressorTests.cs +++ b/src/nunit.analyzers.tests/DiagnosticSuppressors/DisposableFieldsSuppressorTests.cs @@ -19,9 +19,9 @@ public void OneTimeSetUp() { // Find the NetAnalyzers assembly (note version should match the one referenced) string netAnalyzersPath = Path.Combine(PathHelper.GetNuGetPackageDirectory(), - "microsoft.codeanalysis.netanalyzers/8.0.0/analyzers/dotnet/cs/Microsoft.CodeAnalysis.NetAnalyzers.dll"); + "microsoft.codeanalysis.netanalyzers/7.0.4/analyzers/dotnet/cs/Microsoft.CodeAnalysis.CSharp.NetAnalyzers.dll"); Assembly netAnalyzerAssembly = Assembly.LoadFrom(netAnalyzersPath); - Type analyzerType = netAnalyzerAssembly.GetType("Microsoft.CodeQuality.Analyzers.ApiDesignGuidelines.TypesThatOwnDisposableFieldsShouldBeDisposableAnalyzer", true)!; + Type analyzerType = netAnalyzerAssembly.GetType("Microsoft.CodeQuality.CSharp.Analyzers.ApiDesignGuidelines.CSharpTypesThatOwnDisposableFieldsShouldBeDisposableAnalyzer", true)!; this.analyzer = (DiagnosticAnalyzer)Activator.CreateInstance(analyzerType)!; this.analyzer = new DefaultEnabledAnalyzer(this.analyzer); diff --git a/src/nunit.analyzers.tests/Extensions/AttributeArgumentTypedConstantExtensionsTests.cs b/src/nunit.analyzers.tests/Extensions/AttributeArgumentTypedConstantExtensionsTests.cs index 22de52f6..71d2d019 100644 --- a/src/nunit.analyzers.tests/Extensions/AttributeArgumentTypedConstantExtensionsTests.cs +++ b/src/nunit.analyzers.tests/Extensions/AttributeArgumentTypedConstantExtensionsTests.cs @@ -99,7 +99,7 @@ private static IEnumerable GetTestData() SetName("CanAssignToWhenParameterIsVersionAndArgumentIsValidString"); yield return new TestCaseData("\"a.b.c.d\"", "Version", "object", Is.True). SetName("CanAssignToWhenParameterIsVersionAndArgumentIsInValidString"); -#if NET8_0_OR_GREATER +#if NET6_0_OR_GREATER yield return new TestCaseData("\"00:03:00\"", "TimeOnly", "string", Is.True). SetName("CanAssignToWhenParameterIsTimeSpanAndArgumentIsValidString"); yield return new TestCaseData("\"2020-05-08\"", "DateOnly", "string", Is.True). diff --git a/src/nunit.analyzers.tests/TestCaseUsage/TestCaseUsageAnalyzerTests.cs b/src/nunit.analyzers.tests/TestCaseUsage/TestCaseUsageAnalyzerTests.cs index a11707ac..29be2a44 100644 --- a/src/nunit.analyzers.tests/TestCaseUsage/TestCaseUsageAnalyzerTests.cs +++ b/src/nunit.analyzers.tests/TestCaseUsage/TestCaseUsageAnalyzerTests.cs @@ -29,8 +29,8 @@ private static IEnumerable SpecialConversions } } -#if NET8_0_OR_GREATER - private static IEnumerable SpecialConversions_NET8 +#if NET6_0_OR_GREATER + private static IEnumerable SpecialConversions_NET6 { get { @@ -768,7 +768,7 @@ public void TestWithGenericParameter(T arg1) { } } #if NUNIT4 -#if NET8_0_OR_GREATER +#if NET6_0_OR_GREATER [Test] public void AnalyzeWhenArgumentIsCorrectGenericTypeParameter() { @@ -836,8 +836,8 @@ public void TestWithGenericParameter(T arg1) { } RoslynAssert.Valid(this.analyzer, testCode); } - [TestCaseSource(nameof(SpecialConversions_NET8))] - public void AnalyzeWhenArgumentIsSpecialConversionNET8(string value, Type targetType) + [TestCaseSource(nameof(SpecialConversions_NET6))] + public void AnalyzeWhenArgumentIsSpecialConversionNET6(string value, Type targetType) { var testCode = TestUtility.WrapClassInNamespaceAndAddUsing($@" public sealed class AnalyzeWhenArgumentIsSpecialConversion diff --git a/src/nunit.analyzers.tests/nunit.analyzers.tests.csproj b/src/nunit.analyzers.tests/nunit.analyzers.tests.csproj index 45b5b851..402afc2f 100644 --- a/src/nunit.analyzers.tests/nunit.analyzers.tests.csproj +++ b/src/nunit.analyzers.tests/nunit.analyzers.tests.csproj @@ -1,7 +1,7 @@ NUnit.Analyzers.Tests - net8.0;net462 + net6.0;net462 4 diff --git a/src/nunit.analyzers/ConstraintUsage/EqualConstraintUsageCodeFix.cs b/src/nunit.analyzers/ConstraintUsage/EqualConstraintUsageCodeFix.cs index bd1f3da7..cc059520 100644 --- a/src/nunit.analyzers/ConstraintUsage/EqualConstraintUsageCodeFix.cs +++ b/src/nunit.analyzers/ConstraintUsage/EqualConstraintUsageCodeFix.cs @@ -26,7 +26,7 @@ protected override (ExpressionSyntax? actual, ExpressionSyntax? constraintExpres } else if (expected is PatternSyntax pattern) { - constraintExpression = ConvertPattern( + constraintExpression = this.ConvertPattern( SyntaxFactory.IdentifierName(NUnitFrameworkConstants.NameOfIs), pattern); } @@ -91,7 +91,7 @@ private static (ExpressionSyntax? actual, ExpressionOrPatternSyntax? expected) G /// supported-pattern or supported-pattern, /// supported-pattern and supported-pattern. /// - private static InvocationExpressionSyntax? ConvertPattern(ExpressionSyntax member, PatternSyntax pattern) + private InvocationExpressionSyntax? ConvertPattern(ExpressionSyntax member, PatternSyntax pattern) { if (pattern is ConstantPatternSyntax constantPattern) { @@ -125,7 +125,7 @@ private static (ExpressionSyntax? actual, ExpressionOrPatternSyntax? expected) G } else if (pattern is UnaryPatternSyntax unaryPattern && unaryPattern.IsKind(SyntaxKind.NotPattern)) { - return ConvertPattern( + return this.ConvertPattern( SyntaxFactory.MemberAccessExpression( SyntaxKind.SimpleMemberAccessExpression, member, @@ -143,11 +143,11 @@ private static (ExpressionSyntax? actual, ExpressionOrPatternSyntax? expected) G if (constraint is not null) { - InvocationExpressionSyntax? leftExpression = ConvertPattern(member, binaryPattern.Left); + InvocationExpressionSyntax? leftExpression = this.ConvertPattern(member, binaryPattern.Left); if (leftExpression is not null) { - return ConvertPattern( + return this.ConvertPattern( SyntaxFactory.MemberAccessExpression( SyntaxKind.SimpleMemberAccessExpression, leftExpression, From 31dcac42cd9133ac67c056576452e06389a1c54d Mon Sep 17 00:00:00 2001 From: Manfred Brands Date: Wed, 8 Jan 2025 16:04:36 +0800 Subject: [PATCH 5/5] DateTime and TimeOnly also work with NUnit 3 --- .../TestCaseUsageAnalyzerTests.cs | 26 ++++++++++--------- 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/src/nunit.analyzers.tests/TestCaseUsage/TestCaseUsageAnalyzerTests.cs b/src/nunit.analyzers.tests/TestCaseUsage/TestCaseUsageAnalyzerTests.cs index 29be2a44..f394673b 100644 --- a/src/nunit.analyzers.tests/TestCaseUsage/TestCaseUsageAnalyzerTests.cs +++ b/src/nunit.analyzers.tests/TestCaseUsage/TestCaseUsageAnalyzerTests.cs @@ -767,6 +767,20 @@ public void TestWithGenericParameter(T arg1) { } RoslynAssert.Valid(this.analyzer, testCode); } +#if NET6_0_OR_GREATER + [TestCaseSource(nameof(SpecialConversions_NET6))] + public void AnalyzeWhenArgumentIsSpecialConversionNET6(string value, Type targetType) + { + var testCode = TestUtility.WrapClassInNamespaceAndAddUsing($@" + public sealed class AnalyzeWhenArgumentIsSpecialConversion + {{ + [TestCase(""{value}"")] + public void Test({targetType.Name} a) {{ }} + }}"); + RoslynAssert.Valid(this.analyzer, testCode); + } +#endif + #if NUNIT4 #if NET6_0_OR_GREATER [Test] @@ -835,18 +849,6 @@ public void TestWithGenericParameter(T arg1) { } }"); RoslynAssert.Valid(this.analyzer, testCode); } - - [TestCaseSource(nameof(SpecialConversions_NET6))] - public void AnalyzeWhenArgumentIsSpecialConversionNET6(string value, Type targetType) - { - var testCode = TestUtility.WrapClassInNamespaceAndAddUsing($@" - public sealed class AnalyzeWhenArgumentIsSpecialConversion - {{ - [TestCase(""{value}"")] - public void Test({targetType.Name} a) {{ }} - }}"); - RoslynAssert.Valid(this.analyzer, testCode); - } #endif [Test]