.NotEmpty<T>() test extension
$ dotnet add package kasthack.NotEmpty.CoreManually checking properties for emptinness leaves an opportunity to miss something and makes the developer to write boilerplate.
using kasthack.NotEmpty.Xunit; // replace the namespace to match your test framework
public class MyAmazingTest
{
[Fact]
public void MyThingWorks()
{
var targetObject = MyClass.GetResult();
targetObject.NotEmpty();
//<actual asserts>
}
[Fact]
public void TestOptions()
{
// won't throw
new {
PropertyThanLegitimatelyCanBeAnEmptyStringButNotNull = "",
}.NotEmpty(new AssertOptions {
AllowEmptyStrings = true,
});
//won't throw
new {
PropertyThanLegitimatelyCanBeAnEmptyCollectionButNotNull = new int[]{},
}.NotEmpty(new AssertOptions {
AllowEmptyCollections = true,
});
// won't throw
new {
FileContentThatObviouslyContainsSomeNullBytes = new byte[]{ 0 }
}.NotEmpty(new AssertOptions {
AllowZerosInNumberArrays = true
});
// won't throw BUT will stop at 200 iterations
// default MaxDepth is 100
new {
DeeplyNestedObject = new InfiniteNestedStruct()
}.NotEmpty(new AssertOptions {
MaxDepth = 200
});
// won't throw
new ComputedProperty().NotEmpty(new AssertOptions {
IgnoreComputedProperties = true
})
}
public struct InfiniteNestedStruct
{
public int Value { get; set; } = 1;
public InfiniteNestedStruct Child => new InfiniteNestedStruct { Value = this.Value + 1 };
}
public class ComputedProperty
{
public int Value => 1;
}
}