From 069c3a8e34a2a3d4abda338acd6389af2f1e447d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Eduardo=20C=C3=A1ceres?= Date: Mon, 11 Nov 2024 17:39:58 +0100 Subject: [PATCH] Address most linter warnings (#184) * Solve 200 warnings * Address intermitent error about same node id --- .../MoqGenericLoggerExtensions.cs | 6 +- src/SheepTools.Moq/MoqLoggerExtensions.cs | 6 +- .../Extensions/AssemblyExtensions.cs | 14 +- .../Extensions/DateTimeExtensions.cs | 2 +- .../Extensions/EnumerableExtensions.cs | 2 +- src/SheepTools/Extensions/StringExtensions.cs | 4 +- src/SheepTools/Model/IntPointWithValue.cs | 2 - src/SheepTools/Model/Point.cs | 4 +- src/SheepTools/Model/TreeNode.cs | 12 +- src/SheepTools/SheepToolsExceptions.cs | 14 +- .../MoqGenericLoggerExtensionsTest.cs | 4 + .../MoqLoggerExtensionsTest.cs | 4 + tests/SheepTools.Test/BitArrayComparerTest.cs | 10 +- tests/SheepTools.Test/EnsureTest.cs | 16 +- .../Extensions/AttributeFixtures.cs | 20 +- .../Extensions/CollectionExtensionsTest.cs | 10 +- .../Extensions/DateTimeExtensionsTest.cs | 8 +- .../Extensions/EnumerableExtensionsTest.cs | 12 +- .../Extensions/NumericExtensionsTest.cs | 10 +- .../Extensions/Vector2ExtensionsTest.cs | 2 +- tests/SheepTools.Test/MathsTest.cs | 8 +- tests/SheepTools.Test/Model/BitMatrixTest.cs | 287 +++++++++--------- tests/SheepTools.Test/Model/IntPointTest.cs | 2 +- .../Model/IntPointWithValueTest.cs | 2 +- tests/SheepTools.Test/Model/NodeTest.cs | 26 +- tests/SheepTools.Test/Model/Point3DTest.cs | 2 +- tests/SheepTools.Test/Model/PointTest.cs | 6 +- tests/SheepTools.XUnit.Test/AsssertTest.cs | 4 +- 28 files changed, 253 insertions(+), 246 deletions(-) diff --git a/src/SheepTools.Moq/MoqGenericLoggerExtensions.cs b/src/SheepTools.Moq/MoqGenericLoggerExtensions.cs index 288d0a6..3e713ca 100644 --- a/src/SheepTools.Moq/MoqGenericLoggerExtensions.cs +++ b/src/SheepTools.Moq/MoqGenericLoggerExtensions.cs @@ -1,4 +1,6 @@ -using Microsoft.Extensions.Logging; +#pragma warning disable RCS1036 // Remove unnecessary blank line + +using Microsoft.Extensions.Logging; using Moq; namespace SheepTools.Moq; @@ -142,3 +144,5 @@ public static void VerifyLog(this Mock> loggerMock, Lo #endregion } + +#pragma warning restore RCS1036 // Remove unnecessary blank line diff --git a/src/SheepTools.Moq/MoqLoggerExtensions.cs b/src/SheepTools.Moq/MoqLoggerExtensions.cs index c04e2ab..2451beb 100644 --- a/src/SheepTools.Moq/MoqLoggerExtensions.cs +++ b/src/SheepTools.Moq/MoqLoggerExtensions.cs @@ -1,4 +1,6 @@ -using Microsoft.Extensions.Logging; +#pragma warning disable RCS1036 // Remove unnecessary blank line + +using Microsoft.Extensions.Logging; using Moq; namespace SheepTools.Moq; @@ -142,3 +144,5 @@ public static void VerifyLog(this Mock loggerMock, LogLevel #endregion } + +#pragma warning restore RCS1036 // Remove unnecessary blank line diff --git a/src/SheepTools/Extensions/AssemblyExtensions.cs b/src/SheepTools/Extensions/AssemblyExtensions.cs index c2c6e26..67981f4 100644 --- a/src/SheepTools/Extensions/AssemblyExtensions.cs +++ b/src/SheepTools/Extensions/AssemblyExtensions.cs @@ -23,7 +23,7 @@ public static IEnumerable GetTypes() /// public static IEnumerable GetTypes(this Assembly? assembly) { - return (assembly?.GetTypes() ?? Array.Empty()) + return (assembly?.GetTypes() ?? []) .Where(type => type.IsDefined(typeof(TAttribute), true)); } @@ -47,7 +47,7 @@ public static IEnumerable> GetTypesAndAttributes> GetTypesAndAttributes(this Assembly? assembly) where TAttribute : Attribute { - return (assembly?.GetTypes() ?? Array.Empty()) + return (assembly?.GetTypes() ?? []) .Where(type => type.IsDefined(typeof(TAttribute), true)) .SelectMany(type => type.GetCustomAttributes() .Select(attribute => Tuple.Create(type, attribute))); @@ -61,10 +61,12 @@ public static IEnumerable> GetTypesAndAttributesstring Collection public static IEnumerable GetAssemblies() { - IList validAssemblies = new List(); - IList assemblies = new List(); - assemblies.AddRange(Assembly.GetCallingAssembly().GetReferencedAssemblies()); - assemblies.Add(Assembly.GetCallingAssembly().GetName()); + IList validAssemblies = []; + IList assemblies = + [ + .. Assembly.GetCallingAssembly().GetReferencedAssemblies(), + Assembly.GetCallingAssembly().GetName(), + ]; foreach (var candidate in from assemblyName in assemblies let candidate = Assembly.Load(assemblyName) where candidate is not null diff --git a/src/SheepTools/Extensions/DateTimeExtensions.cs b/src/SheepTools/Extensions/DateTimeExtensions.cs index 9fb6aa8..c4080cf 100644 --- a/src/SheepTools/Extensions/DateTimeExtensions.cs +++ b/src/SheepTools/Extensions/DateTimeExtensions.cs @@ -27,7 +27,7 @@ public static bool IsAfter(this DateTime dateTime, DateTime other) /// public static double MillisecondsFromEpoch(this DateTime dateTime) { - return (dateTime - new DateTime(1970, 1, 1, 0, 0, 0)).TotalMilliseconds; + return (dateTime - DateTime.UnixEpoch).TotalMilliseconds; } /// diff --git a/src/SheepTools/Extensions/EnumerableExtensions.cs b/src/SheepTools/Extensions/EnumerableExtensions.cs index 283760a..170dfc0 100644 --- a/src/SheepTools/Extensions/EnumerableExtensions.cs +++ b/src/SheepTools/Extensions/EnumerableExtensions.cs @@ -42,7 +42,7 @@ public static HashSet IntersectAll(this IEnumerable> enumer hashSet.IntersectWith(enumerable); } } - return hashSet ?? new HashSet(); + return hashSet ?? []; } public static HashSet IntersectAll(this IEnumerable enumerableOfStrings) diff --git a/src/SheepTools/Extensions/StringExtensions.cs b/src/SheepTools/Extensions/StringExtensions.cs index 98fe4f7..98d5251 100644 --- a/src/SheepTools/Extensions/StringExtensions.cs +++ b/src/SheepTools/Extensions/StringExtensions.cs @@ -31,7 +31,7 @@ public static bool IsWhiteSpace(this string? str) /// public static bool HasWhiteSpaces(this string? str) { - return str?.Contains(" ") == true; + return str?.Contains(' ') == true; } /// @@ -43,7 +43,7 @@ public static bool HasWhiteSpaces(this string? str) public static string? Truncate(this string? str, int maxLength) { return (str?.Length > maxLength) - ? str.Substring(0, maxLength) + ? str[..maxLength] : str; } diff --git a/src/SheepTools/Model/IntPointWithValue.cs b/src/SheepTools/Model/IntPointWithValue.cs index 3b173de..23fcaf7 100644 --- a/src/SheepTools/Model/IntPointWithValue.cs +++ b/src/SheepTools/Model/IntPointWithValue.cs @@ -16,7 +16,5 @@ public IntPointWithValue(T value, int x, int y) : base(x, y) Value = value; } -#pragma warning disable RCS1132 // Remove redundant overriding member - not redundant, https://github.com/JosefPihrt/Roslynator/issues/744 public override string ToString() => base.ToString(); -#pragma warning restore RCS1132 // Remove redundant overriding member. } diff --git a/src/SheepTools/Model/Point.cs b/src/SheepTools/Model/Point.cs index dcc5589..d1f927c 100644 --- a/src/SheepTools/Model/Point.cs +++ b/src/SheepTools/Model/Point.cs @@ -116,7 +116,7 @@ public virtual Point RotateClockwise(Point pivot, double angle, bool isRadians = public virtual Point CalculateClosestManhattanPoint(ICollection candidatePoints) { - Dictionary pointDistanceDictionary = new(); + Dictionary pointDistanceDictionary = []; foreach (Point point in candidatePoints) { @@ -134,7 +134,7 @@ public virtual Point CalculateClosestManhattanPoint(ICollection candidate /// public virtual Point? CalculateClosestManhattanPointNotTied(ICollection candidatePoints) { - Dictionary pointDistanceDictionary = new(); + Dictionary pointDistanceDictionary = []; foreach (Point point in candidatePoints) { diff --git a/src/SheepTools/Model/TreeNode.cs b/src/SheepTools/Model/TreeNode.cs index 194ea45..d34cc4e 100644 --- a/src/SheepTools/Model/TreeNode.cs +++ b/src/SheepTools/Model/TreeNode.cs @@ -26,7 +26,7 @@ public record TreeNode : GenericNode /// public TreeNode(TKey id) : base(id) { - Children = new HashSet>(); + Children = []; } /// @@ -42,7 +42,7 @@ public TreeNode(TKey id, TreeNode child) : base(id) throw new ArgumentException("A node cannot be its own child"); } - Children = new HashSet> { child }; + Children = [child]; } /// @@ -76,7 +76,7 @@ public TreeNode(TreeNode parent, TKey id) : base(id) Parent = parent; ParentId = parent.Id; - Children = new HashSet>(); + Children = []; } /// @@ -105,8 +105,8 @@ public virtual int DescendantsCount() public virtual int RelationshipsCount() { return Children.Count - + Children.Select(child => child.DescendantsCount()).Sum() - + Children.Select(child => child.RelationshipsCount()).Sum(); + + Children.Sum(child => child.DescendantsCount()) + + Children.Sum(child => child.RelationshipsCount()); } /// @@ -181,9 +181,7 @@ void transverseBackwards(IEnumerable> nodes, ref HashSet id public override bool Equals(TreeNode? other) => base.Equals(other); -#pragma warning disable RCS1132 // Remove redundant overriding member. - https://github.com/JosefPihrt/Roslynator/issues/744 public override int GetHashCode() => base.GetHashCode(); -#pragma warning restore RCS1132 // Remove redundant overriding member. #endregion } diff --git a/src/SheepTools/SheepToolsExceptions.cs b/src/SheepTools/SheepToolsExceptions.cs index 9266351..8dd5df9 100644 --- a/src/SheepTools/SheepToolsExceptions.cs +++ b/src/SheepTools/SheepToolsExceptions.cs @@ -1,8 +1,5 @@ -using System.Runtime.Serialization; +namespace SheepTools; -namespace SheepTools; - -[Serializable] public class ValidationException : Exception { public ValidationException() @@ -16,13 +13,8 @@ public ValidationException(string message) : base(message) public ValidationException(string message, Exception innerException) : base(message, innerException) { } - - protected ValidationException(SerializationInfo info, StreamingContext context) : base(info, context) - { - } } -[Serializable] public class NotFoundException : Exception { public NotFoundException() @@ -36,8 +28,4 @@ public NotFoundException(string message) : base(message) public NotFoundException(string message, Exception innerException) : base(message, innerException) { } - - protected NotFoundException(SerializationInfo info, StreamingContext context) : base(info, context) - { - } } diff --git a/tests/SheepTools.Moq.Test/MoqGenericLoggerExtensionsTest.cs b/tests/SheepTools.Moq.Test/MoqGenericLoggerExtensionsTest.cs index 7a533a1..6eb67fc 100644 --- a/tests/SheepTools.Moq.Test/MoqGenericLoggerExtensionsTest.cs +++ b/tests/SheepTools.Moq.Test/MoqGenericLoggerExtensionsTest.cs @@ -94,6 +94,8 @@ public void VerifyLogException() } } +#pragma warning disable CA2254 // Template should be a static expression + public class FixtureServiceGenericLogger { private readonly ILogger _logger; @@ -113,3 +115,5 @@ public void LogException(LogLevel level, Exception e, string message) _logger.Log(level, e, message); } } + +#pragma warning restore CA2254 // Template should be a static expression diff --git a/tests/SheepTools.Moq.Test/MoqLoggerExtensionsTest.cs b/tests/SheepTools.Moq.Test/MoqLoggerExtensionsTest.cs index 5bac43c..2213146 100644 --- a/tests/SheepTools.Moq.Test/MoqLoggerExtensionsTest.cs +++ b/tests/SheepTools.Moq.Test/MoqLoggerExtensionsTest.cs @@ -94,6 +94,8 @@ public void VerifyLogException() } } +#pragma warning disable CA2254 // Template should be a static expression + internal class FixtureService { private readonly ILogger _logger; @@ -113,3 +115,5 @@ public void LogException(LogLevel level, Exception e, string message) _logger.Log(level, e, message); } } + +#pragma warning restore CA2254 // Template should be a static expression diff --git a/tests/SheepTools.Test/BitArrayComparerTest.cs b/tests/SheepTools.Test/BitArrayComparerTest.cs index a46cb72..a2dccf7 100644 --- a/tests/SheepTools.Test/BitArrayComparerTest.cs +++ b/tests/SheepTools.Test/BitArrayComparerTest.cs @@ -9,17 +9,19 @@ public class BitArrayComparerTest [Fact] public void BitArrayComparer() { +#pragma warning disable CA1861 // Avoid constant arrays as arguments var set = new HashSet { - new BitArray(new[] { true, false }), - new BitArray(new[] { false, true }), + new(new[] { true, false }), + new(new[] { false, true }), }; var otherSet = new HashSet { - new BitArray(new[] { true, false }), - new BitArray(new[] { false, true }), + new(new[] { true, false }), + new(new[] { false, true }), }; +#pragma warning restore CA1861 // Avoid constant arrays as arguments // Without the comparer Assert.NotEqual(set, otherSet); diff --git a/tests/SheepTools.Test/EnsureTest.cs b/tests/SheepTools.Test/EnsureTest.cs index cf7d214..acb3677 100644 --- a/tests/SheepTools.Test/EnsureTest.cs +++ b/tests/SheepTools.Test/EnsureTest.cs @@ -13,8 +13,8 @@ public class EnsureTest [Fact] public void Equal() { - var date = new DateTime(1111, 1, 1); - DateTime otherDate(int n = 0) => new(date.Ticks + n); + var date = new DateTime(1111, 1, 1, 0, 0, 0, DateTimeKind.Unspecified); + DateTime otherDate(int n = 0) => new(date.Ticks + n, DateTimeKind.Unspecified); Asssert.DoesNotThrow(() => Ensure.Equal(date, otherDate())); Assert.Throws(() => Ensure.Equal(date, otherDate(1))); @@ -26,8 +26,8 @@ public void Equal() [Fact] public void EqualsTest() { - var date = new DateTime(1111, 1, 1); - DateTime otherDate(int n = 0) => new(date.Ticks + n); + var date = new DateTime(1111, 1, 1, 0, 0, 0, DateTimeKind.Unspecified); + DateTime otherDate(int n = 0) => new(date.Ticks + n, DateTimeKind.Unspecified); Asssert.DoesNotThrow(() => Ensure.Equals(date, otherDate())); Assert.Throws(() => Ensure.Equals(date, otherDate(1))); @@ -39,8 +39,8 @@ public void EqualsTest() [Fact] public void NotEqual() { - var date = new DateTime(2222, 1, 1); - DateTime otherDate(int n = 0) => new(date.Ticks + n); + var date = new DateTime(2222, 1, 1, 0, 0, 0, DateTimeKind.Unspecified); + DateTime otherDate(int n = 0) => new(date.Ticks + n, DateTimeKind.Unspecified); Asssert.DoesNotThrow(() => Ensure.NotEqual(date, otherDate(-1))); Assert.Throws(() => Ensure.NotEqual(date, otherDate())); @@ -52,8 +52,8 @@ public void NotEqual() [Fact] public void NotEquals() { - var date = new DateTime(2222, 1, 1); - DateTime otherDate(int n = 0) => new(date.Ticks + n); + var date = new DateTime(2222, 1, 1, 0, 0, 0, DateTimeKind.Unspecified); + DateTime otherDate(int n = 0) => new(date.Ticks + n, DateTimeKind.Unspecified); Asssert.DoesNotThrow(() => Ensure.NotEquals(date, otherDate(-1))); Assert.Throws(() => Ensure.NotEquals(date, otherDate())); diff --git a/tests/SheepTools.Test/Extensions/AttributeFixtures.cs b/tests/SheepTools.Test/Extensions/AttributeFixtures.cs index c2736a0..a8dadf2 100644 --- a/tests/SheepTools.Test/Extensions/AttributeFixtures.cs +++ b/tests/SheepTools.Test/Extensions/AttributeFixtures.cs @@ -16,24 +16,24 @@ public FooAttribute(Type classInterface, ServiceLifetime serviceLifetime) } } -internal interface IScopedBar { } +internal interface IScopedBar; [Foo(typeof(IScopedBar), ServiceLifetime.Scoped)] -internal class ScopedBar : IScopedBar { } +internal class ScopedBar : IScopedBar; -internal interface ITransientBar { } +internal interface ITransientBar; [Foo(typeof(ITransientBar), ServiceLifetime.Transient)] -internal class TransientBar : ITransientBar { } +internal class TransientBar : ITransientBar; -internal interface ISingletonBar { } +internal interface ISingletonBar; -internal interface IYetAnotherInterface { } +internal interface IYetAnotherInterface; [Foo(typeof(ISingletonBar), ServiceLifetime.Singleton)] [Foo(typeof(IYetAnotherInterface), ServiceLifetime.Scoped)] -internal class SingletonBar : ISingletonBar, IYetAnotherInterface { } +internal class SingletonBar : ISingletonBar, IYetAnotherInterface; -internal interface IAssemblyExtensionTestInterface { } -internal interface IAssemblyExtensionTestInterfaceNotImplemented { } -internal class AssemblyExtensionTest : IAssemblyExtensionTestInterface { } +internal interface IAssemblyExtensionTestInterface; +internal interface IAssemblyExtensionTestInterfaceNotImplemented; +internal class AssemblyExtensionTest : IAssemblyExtensionTestInterface; diff --git a/tests/SheepTools.Test/Extensions/CollectionExtensionsTest.cs b/tests/SheepTools.Test/Extensions/CollectionExtensionsTest.cs index a6c7379..f2b919b 100644 --- a/tests/SheepTools.Test/Extensions/CollectionExtensionsTest.cs +++ b/tests/SheepTools.Test/Extensions/CollectionExtensionsTest.cs @@ -17,14 +17,14 @@ public void AddRangeCollection() existingCollection.AddRange(itemsToAdd); // Assert - Assert.Equal(initialCollection.Concat(itemsToAdd).ToList(), existingCollection); + Assert.Equal([.. initialCollection, .. itemsToAdd], [.. existingCollection]); } [Fact] public void AddRangeList() { // Arrange - ICollection existingCollection = new List { 1, 2, 3 }; + ICollection existingCollection = [1, 2, 3]; var initialCollection = existingCollection.ToList(); var itemsToAdd = new List { 4, 5, 6 }; @@ -32,14 +32,14 @@ public void AddRangeList() existingCollection.AddRange(itemsToAdd); // Assert - Assert.Equal(initialCollection.Concat(itemsToAdd).ToList(), existingCollection); + Assert.Equal([.. initialCollection, .. itemsToAdd], existingCollection); } [Fact] public void RemoveAllList() { // Arrange - ICollection existingList = new List { 1, 2, 3, 4, 5, 6 }; + ICollection existingList = [1, 2, 3, 4, 5, 6]; var initialList = existingList.ToList(); var evens = existingList.Where(n => n % 2 == 0).ToList(); @@ -62,6 +62,6 @@ public void RemoveAllCollection() existingCollection.RemoveAll(n => n % 2 == 0); // Assert - Assert.Equal(initialCollection.Except(evens).ToList(), existingCollection); + Assert.Equal(initialCollection.Except(evens).ToList(), [.. existingCollection]); } } diff --git a/tests/SheepTools.Test/Extensions/DateTimeExtensionsTest.cs b/tests/SheepTools.Test/Extensions/DateTimeExtensionsTest.cs index 5805f34..051d3c3 100644 --- a/tests/SheepTools.Test/Extensions/DateTimeExtensionsTest.cs +++ b/tests/SheepTools.Test/Extensions/DateTimeExtensionsTest.cs @@ -16,8 +16,8 @@ public void IsAfterNow() [Fact] public void IsAfter() { - var octoberRevolution = new DateTime(1917, 10, 25); - var frenchRevolution = new DateTime(1789, 5, 5); + var octoberRevolution = new DateTime(1917, 10, 25, 0, 0, 0, DateTimeKind.Unspecified); + var frenchRevolution = new DateTime(1789, 5, 5, 0, 0, 0, DateTimeKind.Unspecified); Assert.True(octoberRevolution.IsBetter(frenchRevolution)); } @@ -25,10 +25,10 @@ public void IsAfter() [Fact] public void GetMillisecondsFromEpoch() { - var epoch = new DateTime(1970, 1, 1, 0, 0, 0); + var epoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Unspecified); Assert.Equal(0, epoch.MillisecondsFromEpoch()); - var epochPlusOneHour = new DateTime(1970, 1, 1, 1, 0, 0); + var epochPlusOneHour = new DateTime(1970, 1, 1, 1, 0, 0, DateTimeKind.Unspecified); Assert.Equal(3600 * 1_000, epochPlusOneHour.MillisecondsFromEpoch()); } diff --git a/tests/SheepTools.Test/Extensions/EnumerableExtensionsTest.cs b/tests/SheepTools.Test/Extensions/EnumerableExtensionsTest.cs index 74baef2..98e8950 100644 --- a/tests/SheepTools.Test/Extensions/EnumerableExtensionsTest.cs +++ b/tests/SheepTools.Test/Extensions/EnumerableExtensionsTest.cs @@ -13,7 +13,7 @@ public void ForEachList() { // Arrange var mock = new Mock(); - IEnumerable list = new List { mock.Object, mock.Object, mock.Object }; + IEnumerable list = [mock.Object, mock.Object, mock.Object]; // Act list.ForEach(str => str.Foo()); @@ -54,9 +54,9 @@ public void IntersectAll() { var listOfListsOfChars = new List> { - new List { 'a', 'b', 'c' }, - new List { 'a', 'a', 'c' }, - new List { 'a' } + new() { 'a', 'b', 'c' }, + new() { 'a', 'a', 'c' }, + new() { 'a' } }; var charIntersection = listOfListsOfChars.IntersectAll(); Assert.Single(charIntersection); @@ -64,8 +64,8 @@ public void IntersectAll() var listOfListsOfInt = new List> { - new List { 1 }, - new List { 2 }, + new() { 1 }, + new() { 2 }, }; var intIntersection = listOfListsOfInt.IntersectAll(); Assert.Empty(intIntersection); diff --git a/tests/SheepTools.Test/Extensions/NumericExtensionsTest.cs b/tests/SheepTools.Test/Extensions/NumericExtensionsTest.cs index 62860f0..8cd480c 100644 --- a/tests/SheepTools.Test/Extensions/NumericExtensionsTest.cs +++ b/tests/SheepTools.Test/Extensions/NumericExtensionsTest.cs @@ -127,15 +127,15 @@ public void ClampDouble(double n, double result) [Fact] public void ClampDateTime() { - DateTime min = new(2019, 10, 25); - DateTime max = new(2019, 10, 30); + DateTime min = new(2019, 10, 25, 0, 0, 0, DateTimeKind.Unspecified); + DateTime max = new(2019, 10, 30, 0, 0, 0, DateTimeKind.Unspecified); Dictionary numberExpectedClampedValuePair = new() { [DateTime.MinValue] = min, - [new DateTime(2019, 10, 24, 23, 0, 0)] = min, - [new DateTime(2019, 10, 25, 1, 0, 0)] = new DateTime(2019, 10, 25, 1, 0, 0), - [new DateTime(2019, 10, 30, 0, 0, 1)] = max, + [new DateTime(2019, 10, 24, 23, 0, 0, DateTimeKind.Unspecified)] = min, + [new DateTime(2019, 10, 25, 1, 0, 0, DateTimeKind.Unspecified)] = new DateTime(2019, 10, 25, 1, 0, 0, DateTimeKind.Unspecified), + [new DateTime(2019, 10, 30, 0, 0, 1, DateTimeKind.Unspecified)] = max, [DateTime.MaxValue] = max }; diff --git a/tests/SheepTools.Test/Extensions/Vector2ExtensionsTest.cs b/tests/SheepTools.Test/Extensions/Vector2ExtensionsTest.cs index d67988b..7976e2c 100644 --- a/tests/SheepTools.Test/Extensions/Vector2ExtensionsTest.cs +++ b/tests/SheepTools.Test/Extensions/Vector2ExtensionsTest.cs @@ -20,7 +20,7 @@ public void Equal() Assert.True(a == b); Assert.True(a != c); - HashSet set = new() { a }; + HashSet set = [a]; Assert.False(set.Add(b)); Assert.True(set.Add(c)); } diff --git a/tests/SheepTools.Test/MathsTest.cs b/tests/SheepTools.Test/MathsTest.cs index 127fb22..626db5a 100644 --- a/tests/SheepTools.Test/MathsTest.cs +++ b/tests/SheepTools.Test/MathsTest.cs @@ -23,7 +23,7 @@ public void LeastCommonMultiple(ulong a, ulong b, ulong result) [InlineData(new ulong[] { 240, 492, 768 }, 157440)] public void LeastCommonMultipleEnumerable(ulong[] input, ulong result) { - Assert.Equal(result, Maths.LeastCommonMultiple(input)); + Assert.Equal(result, input.LeastCommonMultiple()); } [Fact] @@ -31,7 +31,7 @@ public void ShouldNotAttemptLcdWith0AsArgument() { Assert.Throws(() => Maths.LeastCommonMultiple(0, 0)); Assert.Throws(() => Maths.LeastCommonMultiple(3, 0)); - Assert.Throws(() => Maths.LeastCommonMultiple(new ulong[] { 0, 3 })); + Assert.Throws(() => Maths.LeastCommonMultiple([0, 3])); } [Theory] @@ -51,7 +51,7 @@ public void GreatestCommonDivisor(ulong a, ulong b, ulong result) [InlineData(new ulong[] { 240, 492, 768 }, 12)] public void GreatestCommonDivisorEnumerable(ulong[] input, ulong result) { - Assert.Equal(result, Maths.GreatestCommonDivisor(input)); + Assert.Equal(result, input.GreatestCommonDivisor()); } [Fact] @@ -59,6 +59,6 @@ public void ShouldNotAttemptGcdWith0AsArgument() { Assert.Throws(() => Maths.GreatestCommonDivisor(0, 3)); Assert.Throws(() => Maths.GreatestCommonDivisor(3, 0)); - Assert.Throws(() => Maths.GreatestCommonDivisor(new ulong[] { 0, 3 })); + Assert.Throws(() => Maths.GreatestCommonDivisor([0, 3])); } } diff --git a/tests/SheepTools.Test/Model/BitMatrixTest.cs b/tests/SheepTools.Test/Model/BitMatrixTest.cs index abf25ef..54ed0da 100644 --- a/tests/SheepTools.Test/Model/BitMatrixTest.cs +++ b/tests/SheepTools.Test/Model/BitMatrixTest.cs @@ -1,5 +1,6 @@ -using SheepTools.Model; -using System.Collections; +#pragma warning disable CA1861 // Avoid constant arrays as arguments + +using SheepTools.Model; using Xunit; namespace SheepTools.Test.Model; @@ -70,38 +71,38 @@ public static IEnumerable FlipUpsideDownData() { yield return new object[] { - new BitMatrix(new List - { - new BitArray( new [] { true, true, false }), - new BitArray( new [] { false, false, false }), - new BitArray( new [] { true, true, true }), - new BitArray( new [] { false, false, true }) - }), - - new BitMatrix(new List - { - new BitArray( new [] { false, false, true }), - new BitArray( new [] { true, true, true }), - new BitArray( new [] { false, false, false }), - new BitArray( new [] { true, true, false }) - }) + new BitMatrix( + [ + new( new [] { true, true, false }), + new( new [] { false, false, false }), + new( new [] { true, true, true }), + new( new [] { false, false, true }) + ]), + + new BitMatrix( + [ + new( new [] { false, false, true }), + new( new [] { true, true, true }), + new( new [] { false, false, false }), + new( new [] { true, true, false }) + ]) }; yield return new object[] { - new BitMatrix(new List - { - new BitArray( new [] { true, true, false }), - new BitArray( new [] { false, false, false }), - new BitArray( new [] { false, false, true }) - }), - - new BitMatrix(new List - { - new BitArray( new [] { false, false, true }), - new BitArray( new [] { false, false, false }), - new BitArray( new [] { true, true, false }) - }) + new BitMatrix( + [ + new( new [] { true, true, false }), + new( new [] { false, false, false }), + new( new [] { false, false, true }) + ]), + + new BitMatrix( + [ + new( new [] { false, false, true }), + new( new [] { false, false, false }), + new( new [] { true, true, false }) + ]) }; } @@ -109,38 +110,38 @@ public static IEnumerable FlipLeftRightData() { yield return new object[] { - new BitMatrix(new List - { - new BitArray( new [] { true, true, false }), - new BitArray( new [] { false, false, false }), - new BitArray( new [] { true, false, true }), - new BitArray( new [] { true , false, false }) - }), - - new BitMatrix(new List - { - new BitArray( new [] { false, true, true}), - new BitArray( new [] { false, false, false }), - new BitArray( new [] { true, false, true }), - new BitArray( new [] { false, false, true }) - }) + new BitMatrix( + [ + new( new [] { true, true, false }), + new( new [] { false, false, false }), + new( new [] { true, false, true }), + new( new [] { true , false, false }) + ]), + + new BitMatrix( + [ + new( new [] { false, true, true}), + new( new [] { false, false, false }), + new( new [] { true, false, true }), + new( new [] { false, false, true }) + ]) }; yield return new object[] { - new BitMatrix(new List - { - new BitArray( new [] { true, true, false, false }), - new BitArray( new [] { false, false, true, false }), - new BitArray( new [] { false, true, true, true }) - }), - - new BitMatrix(new List - { - new BitArray( new [] { false, false, true, true }), - new BitArray( new [] { false, true, false, false }), - new BitArray( new [] { true, true, true, false }) - }) + new BitMatrix( + [ + new( new [] { true, true, false, false }), + new( new [] { false, false, true, false }), + new( new [] { false, true, true, true }) + ]), + + new BitMatrix( + [ + new( new [] { false, false, true, true }), + new( new [] { false, true, false, false }), + new( new [] { true, true, true, false }) + ]) }; } @@ -148,38 +149,38 @@ public static IEnumerable RotateClockwiseData() { yield return new object[] { - new BitMatrix(new List - { - new BitArray( new [] { true, false, true }), - new BitArray( new [] { false, false, false }), - new BitArray( new [] { false, false, true }) - }), - - new BitMatrix(new List - { - new BitArray( new [] { false, false, true}), - new BitArray( new [] { false, false, false }), - new BitArray( new [] { true, false, true }) - }) + new BitMatrix( + [ + new( new [] { true, false, true }), + new( new [] { false, false, false }), + new( new [] { false, false, true }) + ]), + + new BitMatrix( + [ + new( new [] { false, false, true}), + new( new [] { false, false, false }), + new( new [] { true, false, true }) + ]) }; yield return new object[] { - new BitMatrix(new List - { - new BitArray( new [] { true, false, true, false }), - new BitArray( new [] { false, true, false, true}), - new BitArray( new [] { false, false, true, true }), - new BitArray( new [] { true, true, false, true }) - }), - - new BitMatrix(new List - { - new BitArray( new [] { true, false, false, true }), - new BitArray( new [] { true, false, true, false }), - new BitArray( new [] { false, true, false, true }), - new BitArray( new [] { true, true, true, false }) - }) + new BitMatrix( + [ + new( new [] { true, false, true, false }), + new( new [] { false, true, false, true}), + new( new [] { false, false, true, true }), + new( new [] { true, true, false, true }) + ]), + + new BitMatrix( + [ + new( new [] { true, false, false, true }), + new( new [] { true, false, true, false }), + new( new [] { false, true, false, true }), + new( new [] { true, true, true, false }) + ]) }; } @@ -187,38 +188,38 @@ public static IEnumerable RotateAnticlockwiseData() { yield return new object[] { - new BitMatrix(new List - { - new BitArray( new [] { false, false, true}), - new BitArray( new [] { false, false, false }), - new BitArray( new [] { true, false, true }) - }), - - new BitMatrix(new List - { - new BitArray( new [] { true, false, true }), - new BitArray( new [] { false, false, false }), - new BitArray( new [] { false, false, true }) - }) + new BitMatrix( + [ + new( new [] { false, false, true}), + new( new [] { false, false, false }), + new( new [] { true, false, true }) + ]), + + new BitMatrix( + [ + new( new [] { true, false, true }), + new( new [] { false, false, false }), + new( new [] { false, false, true }) + ]) }; yield return new object[] { - new BitMatrix(new List - { - new BitArray( new [] { true, false, false, true }), - new BitArray( new [] { true, false, true, false }), - new BitArray( new [] { false, true, false, true }), - new BitArray( new [] { true, true, true, false }) - }), - - new BitMatrix(new List - { - new BitArray( new [] { true, false, true, false }), - new BitArray( new [] { false, true, false, true}), - new BitArray( new [] { false, false, true, true }), - new BitArray( new [] { true, true, false, true }) - }) + new BitMatrix( + [ + new( new [] { true, false, false, true }), + new( new [] { true, false, true, false }), + new( new [] { false, true, false, true }), + new( new [] { true, true, true, false }) + ]), + + new BitMatrix( + [ + new( new [] { true, false, true, false }), + new( new [] { false, true, false, true}), + new( new [] { false, false, true, true }), + new( new [] { true, true, false, true }) + ]) }; } @@ -226,38 +227,40 @@ public static IEnumerable Rotate180Data() { yield return new object[] { - new BitMatrix(new List - { - new BitArray( new [] { false, false, true}), - new BitArray( new [] { false, false, false }), - new BitArray( new [] { true, false, true }) - }), - - new BitMatrix(new List - { - new BitArray( new [] { true, false, true }), - new BitArray( new [] { false, false, false }), - new BitArray( new [] { true, false, false }) - }) + new BitMatrix( + [ + new( new [] { false, false, true}), + new( new [] { false, false, false }), + new( new [] { true, false, true }) + ]), + + new BitMatrix( + [ + new( new [] { true, false, true }), + new( new [] { false, false, false }), + new( new [] { true, false, false }) + ]) }; yield return new object[] { - new BitMatrix(new List - { - new BitArray( new [] { true, false, false, false }), - new BitArray( new [] { true, false, true, false }), - new BitArray( new [] { false, true, false, true }), - new BitArray( new [] { true, true, true, false }) - }), - - new BitMatrix(new List - { - new BitArray( new [] { false, true, true, true}), - new BitArray( new [] { true, false, true, false}), - new BitArray( new [] { false, true, false, true }), - new BitArray( new [] { false, false, false, true }) - }) + new BitMatrix( + [ + new( new [] { true, false, false, false }), + new( new [] { true, false, true, false }), + new( new [] { false, true, false, true }), + new( new [] { true, true, true, false }) + ]), + + new BitMatrix( + [ + new( new [] { false, true, true, true}), + new( new [] { true, false, true, false}), + new( new [] { false, true, false, true }), + new( new [] { false, false, false, true }) + ]) }; } } + +#pragma warning restore CA1861 // Avoid constant arrays as arguments diff --git a/tests/SheepTools.Test/Model/IntPointTest.cs b/tests/SheepTools.Test/Model/IntPointTest.cs index f8c45ef..bb38fb3 100644 --- a/tests/SheepTools.Test/Model/IntPointTest.cs +++ b/tests/SheepTools.Test/Model/IntPointTest.cs @@ -19,7 +19,7 @@ public void Equal() Assert.True(a == b); Assert.True(a != c); - HashSet set = new() { a }; + HashSet set = [a]; Assert.False(set.Add(b)); Assert.True(set.Add(c)); } diff --git a/tests/SheepTools.Test/Model/IntPointWithValueTest.cs b/tests/SheepTools.Test/Model/IntPointWithValueTest.cs index 045b7e7..d063954 100644 --- a/tests/SheepTools.Test/Model/IntPointWithValueTest.cs +++ b/tests/SheepTools.Test/Model/IntPointWithValueTest.cs @@ -18,7 +18,7 @@ public void Equal() Assert.True(a == b); Assert.True(a != c); - HashSet> set = new() { a }; + HashSet> set = [a]; Assert.False(set.Add(b)); Assert.True(set.Add(c)); } diff --git a/tests/SheepTools.Test/Model/NodeTest.cs b/tests/SheepTools.Test/Model/NodeTest.cs index 9e4ed7a..aad2647 100644 --- a/tests/SheepTools.Test/Model/NodeTest.cs +++ b/tests/SheepTools.Test/Model/NodeTest.cs @@ -46,7 +46,7 @@ public void NodeEqual() Assert.NotEqual(c, d); Assert.True(c != d); - HashSet set = new() { a }; + HashSet set = [a]; Assert.False(set.Add(b)); Assert.True(set.Add(c)); Assert.True(set.Add(d)); @@ -70,7 +70,7 @@ public void CustomNodeEqual() Assert.NotEqual(c, d); Assert.True(c != d); - HashSet set = new() { a }; + HashSet set = [a]; Assert.False(set.Add(b)); Assert.True(set.Add(c)); Assert.True(set.Add(d)); @@ -79,10 +79,10 @@ public void CustomNodeEqual() [Fact] public void DescendantsCount() { - var d = new CustomNode(DateTime.Now); - var c = new CustomNode(DateTime.Now, d); - var b = new CustomNode(DateTime.Now, c); - var a = new CustomNode(DateTime.Now, b); + var d = new CustomNode(DateTime.Now.Subtract(TimeSpan.FromHours(5))); + var c = new CustomNode(DateTime.Now.Subtract(TimeSpan.FromHours(4)), d); + var b = new CustomNode(DateTime.Now.Subtract(TimeSpan.FromHours(3)), c); + var a = new CustomNode(DateTime.Now.Subtract(TimeSpan.FromHours(2)), b); Assert.Equal(0, d.DescendantsCount()); Assert.Equal(0, d.GrandChildrenCount()); @@ -152,7 +152,7 @@ public void GetCommonAncestor() /// https://adventofcode.com/2019/day/6 /// /// - private static ICollection GenerateTestGraphWithChildren() + private static List GenerateTestGraphWithChildren() { var nodeList = new List(); @@ -163,16 +163,16 @@ private static ICollection GenerateTestGraphWithChildren() var k = new Node("K", l); var j = new Node("J", k); var g = new Node("G", h); - var e = new Node("E", new[] { f, j }); - var d = new Node("D", new[] { e, i }); + var e = new Node("E", [f, j]); + var d = new Node("D", [e, i]); var c = new Node("C", d); - var b = new Node("B", new[] { c, g }); + var b = new Node("B", [c, g]); var com = new Node("COM", b); b.Children.Add(g); e.Children.Add(j); d.Children.Add(i); - nodeList.AddRange(new[] { com, b, c, d, e, f, g, h, i, j, k, l }); + nodeList.AddRange([com, b, c, d, e, f, g, h, i, j, k, l]); return nodeList; } @@ -181,7 +181,7 @@ private static ICollection GenerateTestGraphWithChildren() /// https://adventofcode.com/2019/day/6 /// /// - private static ICollection GenerateTestGraphWithParent() + private static List GenerateTestGraphWithParent() { var nodeList = new List(); @@ -198,7 +198,7 @@ private static ICollection GenerateTestGraphWithParent() var k = new Node(j, "K"); var l = new Node(k, "L"); - nodeList.AddRange(new[] { com, b, c, d, e, f, g, h, i, j, k, l }); + nodeList.AddRange([com, b, c, d, e, f, g, h, i, j, k, l]); return nodeList; } diff --git a/tests/SheepTools.Test/Model/Point3DTest.cs b/tests/SheepTools.Test/Model/Point3DTest.cs index 5ef7e2e..b1e69af 100644 --- a/tests/SheepTools.Test/Model/Point3DTest.cs +++ b/tests/SheepTools.Test/Model/Point3DTest.cs @@ -19,7 +19,7 @@ public void Equal() Assert.True(a == b); Assert.True(a != c); - HashSet set = new() { a }; + HashSet set = [a]; Assert.False(set.Add(b)); Assert.True(set.Add(c)); } diff --git a/tests/SheepTools.Test/Model/PointTest.cs b/tests/SheepTools.Test/Model/PointTest.cs index a7734f0..f6c4e56 100644 --- a/tests/SheepTools.Test/Model/PointTest.cs +++ b/tests/SheepTools.Test/Model/PointTest.cs @@ -20,7 +20,7 @@ public void Equal() Assert.True(a == b); Assert.True(a != c); - HashSet set = new() { a }; + HashSet set = [a]; Assert.False(set.Add(b)); Assert.True(set.Add(c)); } @@ -190,7 +190,7 @@ public void CalculateClosestManhattanPoint() var d = new Point(1, 2); var e = new Point(-2, 1); - var result = a.CalculateClosestManhattanPoint(new[] { b, c, d, e }); + var result = a.CalculateClosestManhattanPoint([b, c, d, e]); Assert.Equal(b, result); } @@ -205,7 +205,7 @@ public void CalculateClosestManhattanPointNotTied() var e = new Point(-2, 1); var f = new Point(1, 1); - var result = a.CalculateClosestManhattanPointNotTied(new[] { b, c, d, e, f }); + var result = a.CalculateClosestManhattanPointNotTied([b, c, d, e, f]); Assert.Null(result); } diff --git a/tests/SheepTools.XUnit.Test/AsssertTest.cs b/tests/SheepTools.XUnit.Test/AsssertTest.cs index bb33f94..0a263b2 100644 --- a/tests/SheepTools.XUnit.Test/AsssertTest.cs +++ b/tests/SheepTools.XUnit.Test/AsssertTest.cs @@ -27,7 +27,7 @@ public async Task DoesNotThrowAsync() { var list = new List { 2 }; - await Asssert.DoesNotThrowAsync(() => Task.FromResult(list.Count)).ConfigureAwait(false); - await Assert.ThrowsAsync(() => Asssert.DoesNotThrowAsync(() => Task.FromResult(list[3]))).ConfigureAwait(false); + await Asssert.DoesNotThrowAsync(() => Task.FromResult(list.Count)); + await Assert.ThrowsAsync(() => Asssert.DoesNotThrowAsync(() => Task.FromResult(list[3]))); } }