https://github.com/kasthack-labs/kasthack.notempty
.NotEmpty<T>() test extension
https://github.com/kasthack-labs/kasthack.notempty
emptiness nullability nunit test testing xunit
Last synced: 7 months ago
JSON representation
.NotEmpty<T>() test extension
- Host: GitHub
- URL: https://github.com/kasthack-labs/kasthack.notempty
- Owner: kasthack-labs
- License: agpl-3.0
- Created: 2021-12-12T12:53:41.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2023-01-08T04:52:50.000Z (about 3 years ago)
- Last Synced: 2025-08-18T12:00:52.495Z (7 months ago)
- Topics: emptiness, nullability, nunit, test, testing, xunit
- Language: C#
- Homepage:
- Size: 79.1 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yml
- License: LICENSE
- Code of conduct: .github/CODE_OF_CONDUCT.md
Awesome Lists containing this project
README
# kasthack.NotEmpty
[](https://nuget.org/packages/kasthack.NotEmpty.Core)
[](https://github.com/kasthack-labs/kasthack.NotEmpty/releases/latest)
[](LICENSE)
[](https://github.com/kasthack-labs/kasthack.NotEmpty/actions?query=workflow%3A.NET)
[](https://patreon.com/kasthack)
[](https://patreon.com/kasthack)
## What?
kasthack.Empty is a library for recursively checking objects for emptinness(being null, default value, an empty collection or a string). It saves you from writing boilerplate in tests for deserializers / parsers / API clients.
## Why does this exist?
Manually checking properties for emptinness leaves an opportunity to miss something and makes the developer to write boilerplate.
## Usage
1. Install the appropriate package
* [Nunit](https://www.nuget.org/packages/kasthack.NotEmpty.Nunit/)
* [Xunit](https://www.nuget.org/packages/kasthack.NotEmpty.Xunit/)
* [MsTest](https://www.nuget.org/packages/kasthack.NotEmpty.MsTest/)
* [<no frameworks>](https://www.nuget.org/packages/kasthack.NotEmpty.Raw/)
2. Check your objects / their properties for emptinness. Look at the tests for more details.
````csharp
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();
//
}
[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 => 0;
}
}
````