From 6830e5c8c1e7d45c2a7b46236e47a0cf170d45f1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gy=C3=B6rgy=20K=C5=91szeg?= Date: Sun, 22 Oct 2023 22:08:50 +0200 Subject: [PATCH] Fixing unit tests and analyzer issues for every target --- .../KGySoft.CoreLibraries.UnitTest.csproj | 13 ++++++++----- .../UnitTests/CoreLibraries/EnumTest.cs | 4 +--- .../UnitTests/Reflection/ReflectorTest.cs | 2 +- .../UnitTests/Resources/ResXDataNodeTest.cs | 1 + .../Binary/BinarySerializerTest.TestData.cs | 2 ++ .../Binary/BinarySerializerTest.Tests.cs | 2 -- .../Serialization/Xml/XmlSerializerTest.TestData.cs | 2 ++ KGySoft.CoreLibraries/KGySoft.CoreLibraries.csproj | 8 ++++---- .../Reflection/CreateInstanceAccessor.cs | 9 ++++++--- KGySoft.CoreLibraries/Reflection/FieldAccessor.cs | 2 -- KGySoft.CoreLibraries/Reflection/IndexerAccessor.cs | 6 +++--- .../Reflection/SimplePropertyAccessor.cs | 6 +++--- KGySoft.CoreLibraries/Res.cs | 2 +- .../Binary/BinarySerializationFormatter.cs | 6 +++++- .../Serialization/Xml/XmlSerializerBase.cs | 8 ++++++++ 15 files changed, 45 insertions(+), 28 deletions(-) diff --git a/KGySoft.CoreLibraries.UnitTest/KGySoft.CoreLibraries.UnitTest.csproj b/KGySoft.CoreLibraries.UnitTest/KGySoft.CoreLibraries.UnitTest.csproj index 87acb915..e1f61968 100644 --- a/KGySoft.CoreLibraries.UnitTest/KGySoft.CoreLibraries.UnitTest.csproj +++ b/KGySoft.CoreLibraries.UnitTest/KGySoft.CoreLibraries.UnitTest.csproj @@ -1,8 +1,8 @@  - - net7.0 + net35;net40;net45;net46;net47;net472;netcoreapp2.0;netcoreapp2.1;netcoreapp3.0;net5.0;net6.0;net7.0;net8.0 + false @@ -28,10 +28,13 @@ false - $(NoWarn);MSB3568;NETSDK1138 + $(NoWarn);MSB3568;MSB3825;NETSDK1138;NU1902;NU1903 diff --git a/KGySoft.CoreLibraries.UnitTest/UnitTests/CoreLibraries/EnumTest.cs b/KGySoft.CoreLibraries.UnitTest/UnitTests/CoreLibraries/EnumTest.cs index 66fb058e..1618f85f 100644 --- a/KGySoft.CoreLibraries.UnitTest/UnitTests/CoreLibraries/EnumTest.cs +++ b/KGySoft.CoreLibraries.UnitTest/UnitTests/CoreLibraries/EnumTest.cs @@ -418,9 +418,7 @@ public void GetFlagsCountTest() public void TestEnum_PartiallyTrusted() { var domain = CreateSandboxDomain( -#if NET35 - new SecurityPermission(SecurityPermissionFlag.ControlEvidence), -#endif + new SecurityPermission(SecurityPermissionFlag.ControlEvidence | SecurityPermissionFlag.UnmanagedCode), new ReflectionPermission(ReflectionPermissionFlag.MemberAccess)); var handle = Activator.CreateInstance(domain, Assembly.GetExecutingAssembly().FullName, typeof(Sandbox).FullName!); var sandbox = (Sandbox)handle.Unwrap(); diff --git a/KGySoft.CoreLibraries.UnitTest/UnitTests/Reflection/ReflectorTest.cs b/KGySoft.CoreLibraries.UnitTest/UnitTests/Reflection/ReflectorTest.cs index 0ee35b31..3116f91b 100644 --- a/KGySoft.CoreLibraries.UnitTest/UnitTests/Reflection/ReflectorTest.cs +++ b/KGySoft.CoreLibraries.UnitTest/UnitTests/Reflection/ReflectorTest.cs @@ -1010,7 +1010,7 @@ public void ClassStaticComplexFunctionMethodInvoke() parameters = (object[])args.Clone(); inv.Invoke(null, parameters.AsSpan()); Assert.AreEqual(args[0], result); - Assert.AreEqual(args[0], test.IntProp); + Assert.AreEqual(args[0], TestClass.StaticIntProp); Assert.AreNotEqual(args[2], parameters[2]); #endif diff --git a/KGySoft.CoreLibraries.UnitTest/UnitTests/Resources/ResXDataNodeTest.cs b/KGySoft.CoreLibraries.UnitTest/UnitTests/Resources/ResXDataNodeTest.cs index c4f797a7..838cfb22 100644 --- a/KGySoft.CoreLibraries.UnitTest/UnitTests/Resources/ResXDataNodeTest.cs +++ b/KGySoft.CoreLibraries.UnitTest/UnitTests/Resources/ResXDataNodeTest.cs @@ -147,6 +147,7 @@ public void SafeModeWithFileRefTest() } [Test] + [Obsolete] public void SafeModeWithFileRefTestWithResolver() { var fileRef = new ResXFileRef("fileName", "MyNamespace.DangerousType, DangerousAssembly, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null", null); diff --git a/KGySoft.CoreLibraries.UnitTest/UnitTests/Serialization/Binary/BinarySerializerTest.TestData.cs b/KGySoft.CoreLibraries.UnitTest/UnitTests/Serialization/Binary/BinarySerializerTest.TestData.cs index c13f952b..e9596d53 100644 --- a/KGySoft.CoreLibraries.UnitTest/UnitTests/Serialization/Binary/BinarySerializerTest.TestData.cs +++ b/KGySoft.CoreLibraries.UnitTest/UnitTests/Serialization/Binary/BinarySerializerTest.TestData.cs @@ -1782,11 +1782,13 @@ private sealed record ClassRecord(string StringProp, int IntProp) #region Methods #if NET35 // Compiler bug: CS0656: Missing compiler required member 'System.Type.op_Equality' +#pragma warning disable CS8851 // Record defines 'Equals' but not 'GetHashCode'. public bool Equals(ClassRecord other) => ReferenceEquals(this, other) || (other is not null && EqualityContract == other.EqualityContract && StringProp == other.StringProp && IntProp == other.IntProp); +#pragma warning restore CS8851 // Record defines 'Equals' but not 'GetHashCode'. #endif #endregion diff --git a/KGySoft.CoreLibraries.UnitTest/UnitTests/Serialization/Binary/BinarySerializerTest.Tests.cs b/KGySoft.CoreLibraries.UnitTest/UnitTests/Serialization/Binary/BinarySerializerTest.Tests.cs index 9c4c1efa..776b0542 100644 --- a/KGySoft.CoreLibraries.UnitTest/UnitTests/Serialization/Binary/BinarySerializerTest.Tests.cs +++ b/KGySoft.CoreLibraries.UnitTest/UnitTests/Serialization/Binary/BinarySerializerTest.Tests.cs @@ -66,10 +66,8 @@ #region Suppressions -#if NET5_0_OR_GREATER #pragma warning disable SYSLIB0011 // Type or member is obsolete - this class uses BinaryFormatter for comparisons. It's safe because both serialization and deserialization is in the same process. #pragma warning disable CS0618 // Use of obsolete symbol - as above, as well as indicating some obsolete types as expected ones when deserializing a Hashtable or other non-generic collections -#endif #endregion diff --git a/KGySoft.CoreLibraries.UnitTest/UnitTests/Serialization/Xml/XmlSerializerTest.TestData.cs b/KGySoft.CoreLibraries.UnitTest/UnitTests/Serialization/Xml/XmlSerializerTest.TestData.cs index 82b63bb1..ad8b0cb2 100644 --- a/KGySoft.CoreLibraries.UnitTest/UnitTests/Serialization/Xml/XmlSerializerTest.TestData.cs +++ b/KGySoft.CoreLibraries.UnitTest/UnitTests/Serialization/Xml/XmlSerializerTest.TestData.cs @@ -931,11 +931,13 @@ public ClassRecord() : this(default, default) #region Methods #if NET35 // Compiler bug: CS0656: Missing compiler required member 'System.Type.op_Equality' +#pragma warning disable CS8851 // Record defines 'Equals' but not 'GetHashCode'. public bool Equals(ClassRecord other) => ReferenceEquals(this, other) || (other is not null && EqualityContract == other.EqualityContract && StringProp == other.StringProp && IntProp == other.IntProp); +#pragma warning restore CS8851 // Record defines 'Equals' but not 'GetHashCode'. #endif #endregion diff --git a/KGySoft.CoreLibraries/KGySoft.CoreLibraries.csproj b/KGySoft.CoreLibraries/KGySoft.CoreLibraries.csproj index 2e90bcdc..1e2f7a30 100644 --- a/KGySoft.CoreLibraries/KGySoft.CoreLibraries.csproj +++ b/KGySoft.CoreLibraries/KGySoft.CoreLibraries.csproj @@ -3,8 +3,8 @@ - - net48;net8.0 + net35;net40;net45;net46;net47;net472;netcoreapp2.0;netcoreapp2.1;netcoreapp3.0;netstandard2.0;netstandard2.1;net5.0;net6.0;net7.0;net8.0 + @@ -29,13 +29,13 @@ $(NoWarn);NETSDK1138;NU1902;NU1903 - + all runtime; build; native; contentfiles; analyzers diff --git a/KGySoft.CoreLibraries/Reflection/CreateInstanceAccessor.cs b/KGySoft.CoreLibraries/Reflection/CreateInstanceAccessor.cs index e35c2c9b..4d7f83c1 100644 --- a/KGySoft.CoreLibraries/Reflection/CreateInstanceAccessor.cs +++ b/KGySoft.CoreLibraries/Reflection/CreateInstanceAccessor.cs @@ -23,11 +23,14 @@ using System.Runtime.ExceptionServices; using KGySoft.Annotations; - using KGySoft.CoreLibraries; -#if !NET6_0_OR_GREATER -using KGySoft.CoreLibraries; +#endregion + +#region Suppressions + +#if !(NETCOREAPP3_0_OR_GREATER || NETSTANDARD2_1_OR_GREATER) +#pragma warning disable CS8763 // A method marked [DoesNotReturn] should not return - false alarm, ExceptionDispatchInfo.Throw() does not return either. #endif #endregion diff --git a/KGySoft.CoreLibraries/Reflection/FieldAccessor.cs b/KGySoft.CoreLibraries/Reflection/FieldAccessor.cs index e112f734..4b41eb8d 100644 --- a/KGySoft.CoreLibraries/Reflection/FieldAccessor.cs +++ b/KGySoft.CoreLibraries/Reflection/FieldAccessor.cs @@ -17,9 +17,7 @@ using System; using System.Diagnostics.CodeAnalysis; -#if NET471_OR_GREATER || NETSTANDARD || NETCOREAPP using System.Linq; -#endif using System.Linq.Expressions; using System.Reflection; #if !NETSTANDARD2_0 diff --git a/KGySoft.CoreLibraries/Reflection/IndexerAccessor.cs b/KGySoft.CoreLibraries/Reflection/IndexerAccessor.cs index ddf52349..821d418c 100644 --- a/KGySoft.CoreLibraries/Reflection/IndexerAccessor.cs +++ b/KGySoft.CoreLibraries/Reflection/IndexerAccessor.cs @@ -55,7 +55,7 @@ internal IndexerAccessor(PropertyInfo pi) if (Property.PropertyType.IsPointer) Throw.NotSupportedException(Res.ReflectionPointerTypeNotSupported(Property.PropertyType)); - if (!CanWrite) + if (!Property.CanWrite) { if (Property.PropertyType.IsByRef) { @@ -161,7 +161,7 @@ private protected override Delegate CreateNonGenericSetter() if (ParameterTypes.Length > 1) Throw.NotSupportedException(); // Will be handled in PostValidate - if (!CanWrite) + if (!Property.CanWrite) { if (Property.PropertyType.IsByRef) { @@ -272,7 +272,7 @@ private protected override Delegate CreateGenericSetter() Type delegateType = (isValueType ? typeof(ValueTypeAction<,,>) : typeof(ReferenceTypeAction<,,>)) .GetGenericType(declaringType, Property.PropertyType.IsByRef ? Property.PropertyType.GetElementType()! : Property.PropertyType, ParameterTypes[0]); - if (!CanWrite) + if (!Property.CanWrite) { if (Property.PropertyType.IsByRef) { diff --git a/KGySoft.CoreLibraries/Reflection/SimplePropertyAccessor.cs b/KGySoft.CoreLibraries/Reflection/SimplePropertyAccessor.cs index bca50ed3..27404484 100644 --- a/KGySoft.CoreLibraries/Reflection/SimplePropertyAccessor.cs +++ b/KGySoft.CoreLibraries/Reflection/SimplePropertyAccessor.cs @@ -53,7 +53,7 @@ internal SimplePropertyAccessor(PropertyInfo pi) if (Property.PropertyType.IsPointer) Throw.NotSupportedException(Res.ReflectionPointerTypeNotSupported(Property.PropertyType)); - if (!CanWrite) + if (!Property.CanWrite) { if (Property.PropertyType.IsByRef) { @@ -146,7 +146,7 @@ private protected override Delegate CreateNonGenericSetter() if (Property.PropertyType.IsPointer) Throw.NotSupportedException(Res.ReflectionPointerTypeNotSupported(Property.PropertyType)); - if (!CanWrite) + if (!Property.CanWrite) { if (Property.PropertyType.IsByRef) { @@ -245,7 +245,7 @@ private protected override Delegate CreateGenericSetter() : declaringType!.IsValueType ? typeof(ValueTypeAction<,>).GetGenericType(declaringType, propertyType) : typeof(ReferenceTypeAction<,>).GetGenericType(declaringType, propertyType); - if (!CanWrite) + if (!Property.CanWrite) { if (isByRef) { diff --git a/KGySoft.CoreLibraries/Res.cs b/KGySoft.CoreLibraries/Res.cs index 6722203c..5c51299a 100644 --- a/KGySoft.CoreLibraries/Res.cs +++ b/KGySoft.CoreLibraries/Res.cs @@ -827,7 +827,7 @@ internal static void Initialize() internal static string PerformanceTestCallingGcCollect(bool value) => Get("PerformanceTest_CallingGcCollectFormat", FormatBool(value)); /// Forced CPU Affinity: {0} - internal static string PerformanceTestCpuAffinity(int? affinity) => Get("PerformanceTest_CpuAffinityFormat", affinity == null ? No : affinity.ToString()); + internal static string PerformanceTestCpuAffinity(int? affinity) => Get("PerformanceTest_CpuAffinityFormat", affinity == null ? No : affinity); /// Cases are sorted by {0} internal static string PerformanceTestSortOfCases(string sort) => Get("PerformanceTest_SortOfCasesFormat", sort); diff --git a/KGySoft.CoreLibraries/Serialization/Binary/BinarySerializationFormatter.cs b/KGySoft.CoreLibraries/Serialization/Binary/BinarySerializationFormatter.cs index 43d93d4c..1218b987 100644 --- a/KGySoft.CoreLibraries/Serialization/Binary/BinarySerializationFormatter.cs +++ b/KGySoft.CoreLibraries/Serialization/Binary/BinarySerializationFormatter.cs @@ -53,7 +53,7 @@ #region Suppressions -#if !NET7_0_OR_GREATER +#if !NET8_0_OR_GREATER #pragma warning disable CS1574 // the documentation contains types that are not available in every target #endif #if NET5_0_OR_GREATER @@ -317,6 +317,10 @@ namespace KGySoft.Serialization.Binary /// (in .NET Core 2.0 and above) /// (in .NET Core 2.0 and above) /// (in .NET Core 2.0 and above) + /// (in .NET Core 3.0 and above) + /// (in .NET Core 3.0 and above) + /// (in .NET Core 3.0 and above) + /// (in .NET 8.0 and above) /// /// /// diff --git a/KGySoft.CoreLibraries/Serialization/Xml/XmlSerializerBase.cs b/KGySoft.CoreLibraries/Serialization/Xml/XmlSerializerBase.cs index 77717cb4..1c19ccee 100644 --- a/KGySoft.CoreLibraries/Serialization/Xml/XmlSerializerBase.cs +++ b/KGySoft.CoreLibraries/Serialization/Xml/XmlSerializerBase.cs @@ -49,6 +49,14 @@ #endregion +#region Suppressions + +#if !(NETSTANDARD2_1_OR_GREATER && NETCOREAPP3_0_OR_GREATER) +#pragma warning disable CS8602 // Dereference of a possibly null reference +#endif + +#endregion + namespace KGySoft.Serialization.Xml { internal abstract class XmlSerializerBase