From f018d1b64afd83cabaee8a7baf1915ae84de08ea Mon Sep 17 00:00:00 2001 From: Terje Sandstrom Date: Sun, 15 Mar 2026 17:21:40 +0100 Subject: [PATCH] Code cleanup, some from compiler messages, some from R# inspections. Some just modernization. --- .../Snippets.NUnit/AttributeExamples.cs | 2 +- .../CancelAfterAttributeExamples.cs | 2 +- .../Attributes/RepeatAttributeExample.cs | 11 ++------ .../Attributes/RetryAttributeExamples.cs | 1 - .../TestFixtureAttributeExamples.cs | 11 ++++---- .../TestFixtureSourceAttributeExamples.cs | 10 ++++--- .../Snippets.NUnit/ClassicAssertExamples.cs | 28 +++++++++---------- .../ClassicVsConstraintAssertions.cs | 2 +- .../Snippets.NUnit/CustomConstraints.cs | 1 - .../Snippets.NUnit/TestCaseDataExample.cs | 4 +-- .../Snippets.NUnit/TestCaseSourceExamples.cs | 6 ++-- docs/snippets/Snippets.NUnitLite/UnitTest1.cs | 2 +- docs/snippets/Snippets.sln.DotSettings | 2 ++ 13 files changed, 39 insertions(+), 43 deletions(-) create mode 100644 docs/snippets/Snippets.sln.DotSettings diff --git a/docs/snippets/Snippets.NUnit/AttributeExamples.cs b/docs/snippets/Snippets.NUnit/AttributeExamples.cs index 458eb66cd..e69c15263 100644 --- a/docs/snippets/Snippets.NUnit/AttributeExamples.cs +++ b/docs/snippets/Snippets.NUnit/AttributeExamples.cs @@ -245,7 +245,7 @@ public void AnotherManualTest() [TestFixture] public class TimeoutAttributeExamples { -#pragma warning disable CS0618 // Type or member is obsolete[Explicit("This doesnt work on .net, only .netframework")] +#pragma warning disable CS0618 // Type or member is obsolete[Explicit("This doesn't work on .net, only .netframework")] #region TimeoutExample [Test] [Timeout(1000)] // 1 second timeout diff --git a/docs/snippets/Snippets.NUnit/Attributes/CancelAfterAttributeExamples.cs b/docs/snippets/Snippets.NUnit/Attributes/CancelAfterAttributeExamples.cs index 06b7472ce..5b3a5a536 100644 --- a/docs/snippets/Snippets.NUnit/Attributes/CancelAfterAttributeExamples.cs +++ b/docs/snippets/Snippets.NUnit/Attributes/CancelAfterAttributeExamples.cs @@ -17,7 +17,7 @@ public void RunningTestUntilCanceled(CancellationToken cancellationToken) #endregion #region TestCaseSourceWithCancellationToken - private static int[] _simpleValues = { 2, 4, 6, 8 }; + private static int[] _simpleValues = [2, 4, 6, 8]; [TestCaseSource(nameof(_simpleValues)), CancelAfter(1_000)] public void TestCaseSourceWithCancellationToken(int a, CancellationToken cancellationToken) diff --git a/docs/snippets/Snippets.NUnit/Attributes/RepeatAttributeExample.cs b/docs/snippets/Snippets.NUnit/Attributes/RepeatAttributeExample.cs index cd34612c4..8002c72c5 100644 --- a/docs/snippets/Snippets.NUnit/Attributes/RepeatAttributeExample.cs +++ b/docs/snippets/Snippets.NUnit/Attributes/RepeatAttributeExample.cs @@ -1,9 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using NUnit.Framework; +using NUnit.Framework; namespace Snippets.NUnit.Attributes; @@ -19,7 +14,7 @@ public void TestMethod1() } #endregion - private int count1 = 0; + private int count1; #region RepeatWithFaultAndOutputAttributeExample [Test, Explicit("Issue 5031 must be fixed")] [Repeat(5, StopOnFailure = false)] @@ -34,7 +29,7 @@ public void TestMethod2() #region RepeatWithFaultAttributeExample - private int count2 = 0; + private int count2; [Test,Explicit] // Marking the test as Explicit to avoid failing our doc build. You can skip this. [Repeat(5, StopOnFailure = false)] diff --git a/docs/snippets/Snippets.NUnit/Attributes/RetryAttributeExamples.cs b/docs/snippets/Snippets.NUnit/Attributes/RetryAttributeExamples.cs index 54fb2510f..8a77d7071 100644 --- a/docs/snippets/Snippets.NUnit/Attributes/RetryAttributeExamples.cs +++ b/docs/snippets/Snippets.NUnit/Attributes/RetryAttributeExamples.cs @@ -1,5 +1,4 @@ using System.Diagnostics; -using System.Threading.Tasks; using NUnit.Framework; namespace Snippets.NUnit; diff --git a/docs/snippets/Snippets.NUnit/Attributes/TestFixtureAttributeExamples.cs b/docs/snippets/Snippets.NUnit/Attributes/TestFixtureAttributeExamples.cs index 0bcb95730..11c4cee9e 100644 --- a/docs/snippets/Snippets.NUnit/Attributes/TestFixtureAttributeExamples.cs +++ b/docs/snippets/Snippets.NUnit/Attributes/TestFixtureAttributeExamples.cs @@ -44,10 +44,9 @@ public ParameterizedTestFixture(params char[] eqArguments) { _eq1 = eqArguments[0].ToString(); _eq2 = eqArguments[1].ToString(); - if (eqArguments.Length > 2) - _neq = eqArguments[2].ToString(); - else - _neq = null; + _neq = eqArguments.Length > 2 + ? eqArguments[2].ToString() + : null; } [Test] @@ -121,8 +120,8 @@ public void TestMyArgTypes(T1 t1, T2 t2) #endregion #region SpecifyTypeArgsSeparately - [TestFixture(100.0, 42, TypeArgs = new[] { typeof(double), typeof(int) })] - [TestFixture(42, 100.0, TypeArgs = new[] { typeof(int), typeof(double) })] + [TestFixture(100.0, 42, TypeArgs = [typeof(double), typeof(int)])] + [TestFixture(42, 100.0, TypeArgs = [typeof(int), typeof(double)])] public class SpecifyTypeArgsSeparately where T1 : notnull where T2 : notnull diff --git a/docs/snippets/Snippets.NUnit/Attributes/TestFixtureSourceAttributeExamples.cs b/docs/snippets/Snippets.NUnit/Attributes/TestFixtureSourceAttributeExamples.cs index eb217ba0f..38c9630a6 100644 --- a/docs/snippets/Snippets.NUnit/Attributes/TestFixtureSourceAttributeExamples.cs +++ b/docs/snippets/Snippets.NUnit/Attributes/TestFixtureSourceAttributeExamples.cs @@ -14,10 +14,11 @@ public MyTestClass(string word, int num) { /* ... */ } /* Tests */ - static object[] FixtureArgs = { + static object[] FixtureArgs = + [ new object[] { "Question", 1 }, new object[] { "Answer", 42 } - }; + ]; } #endregion } @@ -34,10 +35,11 @@ public MyTestClass(string word, int num) { /* ... */ } public class AnotherClass { - public static object[] FixtureArgs = { + public static object[] FixtureArgs = + [ new object[] { "Question", 1 }, new object[] { "Answer", 42 } - }; + ]; } #endregion } diff --git a/docs/snippets/Snippets.NUnit/ClassicAssertExamples.cs b/docs/snippets/Snippets.NUnit/ClassicAssertExamples.cs index eb639f4f9..b82f3c6fe 100644 --- a/docs/snippets/Snippets.NUnit/ClassicAssertExamples.cs +++ b/docs/snippets/Snippets.NUnit/ClassicAssertExamples.cs @@ -147,7 +147,7 @@ public void Contains_Examples() var list = new List { 1, 2, 3, 4, 5 }; ClassicAssert.Contains(3, list); - var array = new string[] { "apple", "banana", "cherry" }; + var array = new[] { "apple", "banana", "cherry" }; ClassicAssert.Contains("banana", array); } #endregion @@ -188,7 +188,7 @@ public void Empty_Examples() ClassicAssert.IsEmpty(new List()); ClassicAssert.IsNotEmpty("Hello"); - ClassicAssert.IsNotEmpty(new int[] { 1, 2, 3 }); + ClassicAssert.IsNotEmpty(new[] { 1, 2, 3 }); ClassicAssert.IsNotEmpty(new List { "item" }); } #endregion @@ -227,10 +227,10 @@ public class CollectionAssertExamples [Test] public void CollectionAssert_Basic_Examples() { - var list1 = new int[] { 1, 2, 3 }; - var list2 = new int[] { 1, 2, 3 }; - var list3 = new int[] { 3, 2, 1 }; - var list4 = new int[] { 1, 2, 4 }; + var list1 = new[] { 1, 2, 3 }; + var list2 = new[] { 1, 2, 3 }; + var list3 = new[] { 3, 2, 1 }; + var list4 = new[] { 1, 2, 4 }; // Collections are equal (same order) CollectionAssert.AreEqual(list1, list2); @@ -250,7 +250,7 @@ public void CollectionAssert_Basic_Examples() [Test] public void CollectionAssert_Contains_Examples() { - var list = new int[] { 1, 2, 3, 4, 5 }; + var list = new[] { 1, 2, 3, 4, 5 }; CollectionAssert.Contains(list, 3); CollectionAssert.DoesNotContain(list, 10); @@ -261,9 +261,9 @@ public void CollectionAssert_Contains_Examples() [Test] public void CollectionAssert_Subset_Examples() { - var superset = new int[] { 1, 2, 3, 4, 5 }; - var subset = new int[] { 2, 4 }; - var notSubset = new int[] { 1, 6 }; + var superset = new[] { 1, 2, 3, 4, 5 }; + var subset = new[] { 2, 4 }; + var notSubset = new[] { 1, 6 }; CollectionAssert.IsSubsetOf(subset, superset); CollectionAssert.IsNotSubsetOf(notSubset, superset); @@ -275,7 +275,7 @@ public void CollectionAssert_Subset_Examples() public void CollectionAssert_Empty_Examples() { var emptyList = new int[] { }; - var nonEmptyList = new int[] { 1, 2, 3 }; + var nonEmptyList = new[] { 1, 2, 3 }; CollectionAssert.IsEmpty(emptyList); CollectionAssert.IsNotEmpty(nonEmptyList); @@ -286,8 +286,8 @@ public void CollectionAssert_Empty_Examples() [Test] public void CollectionAssert_Ordered_Examples() { - var orderedList = new int[] { 1, 2, 3, 4, 5 }; - var reverseOrderedList = new int[] { 5, 4, 3, 2, 1 }; + var orderedList = new[] { 1, 2, 3, 4, 5 }; + var reverseOrderedList = new[] { 5, 4, 3, 2, 1 }; CollectionAssert.IsOrdered(orderedList); CollectionAssert.IsOrdered(reverseOrderedList, Comparer.Create((x, y) => y.CompareTo(x))); @@ -300,7 +300,7 @@ public void CollectionAssert_Ordered_Examples() [Test] public void CollectionAssert_ItemType_Examples() { - var stringList = new string[] { "a", "b", "c" }; + var stringList = new[] { "a", "b", "c" }; var mixedList = new object[] { "string", 123, null }; CollectionAssert.AllItemsAreInstancesOfType(stringList, typeof(string)); diff --git a/docs/snippets/Snippets.NUnit/ClassicVsConstraintAssertions.cs b/docs/snippets/Snippets.NUnit/ClassicVsConstraintAssertions.cs index f4e996426..8ec2af926 100644 --- a/docs/snippets/Snippets.NUnit/ClassicVsConstraintAssertions.cs +++ b/docs/snippets/Snippets.NUnit/ClassicVsConstraintAssertions.cs @@ -13,7 +13,7 @@ public class ClassicVsConstraintAssertions public void TheTest() { #region ConstraintWithoutClassicEquivalent - int[] array = { 1, 2, 3 }; + int[] array = [1, 2, 3]; Assert.That(array, Has.Exactly(1).EqualTo(3)); Assert.That(array, Has.Exactly(2).GreaterThan(1)); Assert.That(array, Has.Exactly(3).LessThan(100)); diff --git a/docs/snippets/Snippets.NUnit/CustomConstraints.cs b/docs/snippets/Snippets.NUnit/CustomConstraints.cs index 928fbade5..e6e5f6010 100644 --- a/docs/snippets/Snippets.NUnit/CustomConstraints.cs +++ b/docs/snippets/Snippets.NUnit/CustomConstraints.cs @@ -1,4 +1,3 @@ -using NUnit.Framework; using NUnit.Framework.Constraints; using static Snippets.NUnit.CustomConstraints; diff --git a/docs/snippets/Snippets.NUnit/TestCaseDataExample.cs b/docs/snippets/Snippets.NUnit/TestCaseDataExample.cs index 186fa6ef9..81f8df0a0 100644 --- a/docs/snippets/Snippets.NUnit/TestCaseDataExample.cs +++ b/docs/snippets/Snippets.NUnit/TestCaseDataExample.cs @@ -43,8 +43,8 @@ public void ExplicitTypeArgs(T input) private static IEnumerable ExplicitTypeArgsTestCases() { - yield return new TestCaseData(2) { TypeArgs = new[] { typeof(long) } }; - yield return new TestCaseData(2L) { TypeArgs = new[] { typeof(long) } }; + yield return new TestCaseData(2) { TypeArgs = [typeof(long)] }; + yield return new TestCaseData(2L) { TypeArgs = [typeof(long)] }; } } #endregion diff --git a/docs/snippets/Snippets.NUnit/TestCaseSourceExamples.cs b/docs/snippets/Snippets.NUnit/TestCaseSourceExamples.cs index c9a046441..190974afa 100644 --- a/docs/snippets/Snippets.NUnit/TestCaseSourceExamples.cs +++ b/docs/snippets/Snippets.NUnit/TestCaseSourceExamples.cs @@ -21,11 +21,11 @@ public void DivideTest(int n, int d, int q) } public static object[] DivideCases = - { + [ new object[] { 12, 3, 4 }, new object[] { 12, 2, 6 }, new object[] { 12, 4, 3 } - }; + ]; } #endregion #region ParameterizedSource @@ -130,7 +130,7 @@ public void TestOfPersonAge((Person P, bool Expected) td) Assert.That(res, Is.EqualTo(td.Expected)); } - public static IEnumerable<(Person, bool)> TestCases() + private static IEnumerable<(Person, bool)> TestCases() { yield return (new Person { Name = "John", Age = 10 }, false); yield return (new Person { Name = "Jane", Age = 30 }, true); diff --git a/docs/snippets/Snippets.NUnitLite/UnitTest1.cs b/docs/snippets/Snippets.NUnitLite/UnitTest1.cs index a8244de58..b79e05e92 100644 --- a/docs/snippets/Snippets.NUnitLite/UnitTest1.cs +++ b/docs/snippets/Snippets.NUnitLite/UnitTest1.cs @@ -19,7 +19,7 @@ public void IgnoredTest() [Test] public void FailingTest() { - Assert.That(()=>Assert.Fail(),Throws.TypeOf()); + Assert.That(Assert.Fail,Throws.TypeOf()); } diff --git a/docs/snippets/Snippets.sln.DotSettings b/docs/snippets/Snippets.sln.DotSettings new file mode 100644 index 000000000..27aa2e2ae --- /dev/null +++ b/docs/snippets/Snippets.sln.DotSettings @@ -0,0 +1,2 @@ + + True \ No newline at end of file