diff --git a/collections-arrays/DeclareAnEmptyStringArrayInCSharp/EmptyStringArrayFactory/ArrayFactory.cs b/collections-arrays/DeclareAnEmptyStringArrayInCSharp/EmptyStringArrayFactory/ArrayFactory.cs index 85f94960a6..844729241b 100644 --- a/collections-arrays/DeclareAnEmptyStringArrayInCSharp/EmptyStringArrayFactory/ArrayFactory.cs +++ b/collections-arrays/DeclareAnEmptyStringArrayInCSharp/EmptyStringArrayFactory/ArrayFactory.cs @@ -1,42 +1,43 @@ -namespace EmptyStringArrayFactory +namespace EmptyStringArrayFactory; + +public class ArrayFactory { - public class ArrayFactory + public static string[] CreateWithNewAndZeroLength() + { + return new string[0]; + } + + public static string[] CreateWithEmptyInitializer() + { + return new string[] { }; + } + + public static string[] CreateWithImplicitInitializer() + { + string[] myArray = { }; + + return myArray; + } + + public static string[] CreateWithEnumerableEmpty() { - public static string[] CreateEmptyArrayExample1() - { - return new string[0]; - } - - public static string[] CreateEmptyArrayExample2() - { - return new string[] { }; - } - - public static string[] CreateEmptyArrayExample3() - { - string[] myArray = { }; - - return myArray; - } - - public static string[] CreateEmptyArrayExample4() - { - return Enumerable.Empty().ToArray(); - } - - public static string[] CreateEmptyArrayExample5() - { - return Enumerable.Repeat("", 0).ToArray(); - } - - public static string[] CreateEmptyArrayExample6() - { - return Enumerable.Repeat(string.Empty, 0).ToArray(); - } - - public static string[] CreateEmptyArrayExample7() - { - return Array.Empty(); - } + return Enumerable.Empty().ToArray(); } -} \ No newline at end of file + + public static string[] CreateWithEnumerableRepeatEmptyString() + { + return Enumerable.Repeat("", 0).ToArray(); + } + + public static string[] CreateWithEnumerableRepeatStringEmpty() + { + return Enumerable.Repeat(string.Empty, 0).ToArray(); + } + + public static string[] CreateWithArrayEmpty() + { + return Array.Empty(); + } + + public static string[] CreateEmptyArrayWithCollectionExpression() => []; +} diff --git a/collections-arrays/DeclareAnEmptyStringArrayInCSharp/EmptyStringArrayFactory/EmptyStringArrayFactory.csproj b/collections-arrays/DeclareAnEmptyStringArrayInCSharp/EmptyStringArrayFactory/EmptyStringArrayFactory.csproj index cfadb03dd5..9ed914b5ba 100644 --- a/collections-arrays/DeclareAnEmptyStringArrayInCSharp/EmptyStringArrayFactory/EmptyStringArrayFactory.csproj +++ b/collections-arrays/DeclareAnEmptyStringArrayInCSharp/EmptyStringArrayFactory/EmptyStringArrayFactory.csproj @@ -1,7 +1,7 @@ - net7.0 + net10.0 enable enable diff --git a/collections-arrays/DeclareAnEmptyStringArrayInCSharp/Tests/ArrayUnitTest.cs b/collections-arrays/DeclareAnEmptyStringArrayInCSharp/Tests/ArrayUnitTest.cs index 999d69c15b..eb0aea4bc5 100644 --- a/collections-arrays/DeclareAnEmptyStringArrayInCSharp/Tests/ArrayUnitTest.cs +++ b/collections-arrays/DeclareAnEmptyStringArrayInCSharp/Tests/ArrayUnitTest.cs @@ -3,69 +3,92 @@ namespace Tests { public class ArrayUnitTest { - private readonly int _expectedSize = 0; + [Fact] + public void WhenCreateWithNewAndZeroLengthIsCalled_ThenReturnAnEmptyStringArray() + { + var actual = ArrayFactory.CreateWithNewAndZeroLength(); + + Assert.NotNull(actual); + Assert.IsType(actual); + Assert.Empty(actual); + } [Fact] - public void WhenCreateEmptyArrayExample1IsCalled_ThenReturnAnEmptyStringArray() + public void WhenCreateWithEmptyInitializerIsCalled_ThenReturnAnEmptyStringArray() { - var actual = ArrayFactory.CreateEmptyArrayExample1(); + var actual = ArrayFactory.CreateWithEmptyInitializer(); Assert.NotNull(actual); Assert.IsType(actual); + Assert.Empty(actual); } [Fact] - public void WhenCreateEmptyArrayExample2IsCalled_ThenReturnAnEmptyStringArray() + public void WhenCreateWithImplicitInitializerIsCalled_ThenReturnAnEmptyStringArray() { - var actual = ArrayFactory.CreateEmptyArrayExample2(); + var actual = ArrayFactory.CreateWithImplicitInitializer(); Assert.NotNull(actual); Assert.IsType(actual); + Assert.Empty(actual); } [Fact] - public void WhenCreateEmptyArrayExample3IsCalled_ThenReturnAnEmptyStringArray() + public void WhenCreateWithEnumerableEmptyIsCalled_ThenReturnAnEmptyStringArray() { - var actual = ArrayFactory.CreateEmptyArrayExample3(); + var actual = ArrayFactory.CreateWithEnumerableEmpty(); Assert.NotNull(actual); Assert.IsType(actual); + Assert.Empty(actual); } [Fact] - public void WhenCreateEmptyArrayExample4IsCalled_ThenReturnAnEmptyStringArray() + public void WhenCreateWithEnumerableRepeatEmptyStringIsCalled_ThenReturnAnEmptyStringArray() { - var actual = ArrayFactory.CreateEmptyArrayExample4(); + var actual = ArrayFactory.CreateWithEnumerableRepeatEmptyString(); Assert.NotNull(actual); Assert.IsType(actual); + Assert.Empty(actual); } [Fact] - public void WhenCreateEmptyArrayExample5IsCalled_ThenReturnAnEmptyStringArray() + public void WhenCreateWithEnumerableRepeatStringEmptyIsCalled_ThenReturnAnEmptyStringArray() { - var actual = ArrayFactory.CreateEmptyArrayExample5(); + var actual = ArrayFactory.CreateWithEnumerableRepeatStringEmpty(); Assert.NotNull(actual); Assert.IsType(actual); + Assert.Empty(actual); } [Fact] - public void WhenCreateEmptyArrayExample6IsCalled_ThenReturnAnEmptyStringArray() + public void WhenCreateWithArrayEmptyIsCalled_ThenReturnAnEmptyStringArray() { - var actual = ArrayFactory.CreateEmptyArrayExample6(); + var actual = ArrayFactory.CreateWithArrayEmpty(); Assert.NotNull(actual); Assert.IsType(actual); + Assert.Empty(actual); } [Fact] - public void WhenCreateEmptyArrayExample7IsCalled_ThenReturnAnEmptyStringArray() + public void WhenCreateEmptyArrayWithCollectionExpressionIsCalled_ThenReturnAnEmptyStringArray() { - var actual = ArrayFactory.CreateEmptyArrayExample7(); + var actual = ArrayFactory.CreateEmptyArrayWithCollectionExpression(); Assert.NotNull(actual); Assert.IsType(actual); + Assert.Empty(actual); } + + [Fact] + public void WhenCreateWithArrayEmptyIsCalledTwice_ThenBothCallsReturnTheSameInstance() => + Assert.Same(ArrayFactory.CreateWithArrayEmpty(), ArrayFactory.CreateWithArrayEmpty()); + + [Fact] + public void WhenCreateWithCollectionExpressionIsCalled_ThenItReturnsTheArrayEmptyInstance() => + Assert.Same(Array.Empty(), ArrayFactory.CreateEmptyArrayWithCollectionExpression()); } -} \ No newline at end of file +} diff --git a/collections-arrays/DeclareAnEmptyStringArrayInCSharp/Tests/Tests.csproj b/collections-arrays/DeclareAnEmptyStringArrayInCSharp/Tests/Tests.csproj index e28daa028e..022478f311 100644 --- a/collections-arrays/DeclareAnEmptyStringArrayInCSharp/Tests/Tests.csproj +++ b/collections-arrays/DeclareAnEmptyStringArrayInCSharp/Tests/Tests.csproj @@ -1,7 +1,7 @@ - net7.0 + net10.0 enable enable @@ -10,13 +10,13 @@ - - - + + + runtime; build; native; contentfiles; analyzers; buildtransitive all - + runtime; build; native; contentfiles; analyzers; buildtransitive all