Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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<string>().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<string>();
}
return Enumerable.Empty<string>().ToArray();
}
}

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<string>();
}

public static string[] CreateEmptyArrayWithCollectionExpression() => [];
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<string[]>(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<string[]>(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<string[]>(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<string[]>(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<string[]>(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<string[]>(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<string[]>(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<string[]>(actual);
Assert.Empty(actual);
}

[Fact]
public void WhenCreateWithArrayEmptyIsCalledTwice_ThenBothCallsReturnTheSameInstance() =>
Assert.Same(ArrayFactory.CreateWithArrayEmpty(), ArrayFactory.CreateWithArrayEmpty());

[Fact]
public void WhenCreateWithCollectionExpressionIsCalled_ThenItReturnsTheArrayEmptyInstance() =>
Assert.Same(Array.Empty<string>(), ArrayFactory.CreateEmptyArrayWithCollectionExpression());
}
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>

Expand All @@ -10,13 +10,13 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.3.2" />
<PackageReference Include="xunit" Version="2.4.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="18.10.1" />
<PackageReference Include="xunit" Version="2.9.3" />
<PackageReference Include="xunit.runner.visualstudio" Version="4.0.1">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="3.1.2">
<PackageReference Include="coverlet.collector" Version="10.1.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
Expand Down
Loading