Skip to content

Commit

Permalink
Fix pipelines, update readme, etc
Browse files Browse the repository at this point in the history
  • Loading branch information
kasthack committed Dec 18, 2021
1 parent 0000000 commit 0000000
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 17 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ Manually checking properties for emptinness leaves an opportunity to miss someth
* [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/)
* [Plain .NET](https://www.nuget.org/packages/kasthack.NotEmpty.Raw/)
* [<no frameworks>](https://www.nuget.org/packages/kasthack.NotEmpty.Raw/)

2. Check your objects / their properties for emptinness.
2. Check your objects / their properties for emptinness. Look at the <a href="src/kasthack.NotEmpty.Tests/NotEmptyTestBase.cs">tests</a> for more details.

````csharp
using kasthack.NotEmpty.Xunit; // replace the namespace to match your test framework
Expand Down
14 changes: 6 additions & 8 deletions src/kasthack.NotEmpty.Core/NotEmptyExtensionsBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public abstract class NotEmptyExtensionsBase
{
public void NotEmpty<T>(T? value)
{
// workaround for boxed structs
// workaround for boxed structs passed as objects
if (value is not null && typeof(T) == typeof(object) && value.GetType() != typeof(object))
{
this.NotEmptyBoxed(value, null!);
Expand All @@ -23,6 +23,7 @@ public void NotEmpty<T>(T? value)
private void NotEmptyInternal<T>(T? value, string? path = null)
{
string message = GetEmptyMessage(path);
this.Assert(value is not null, message); //fast lane
this.Assert(!EqualityComparer<T>.Default.Equals(default!, value!), message);
switch (value)
{
Expand All @@ -48,10 +49,7 @@ private void NotEmptyInternal<T>(T? value, string? path = null)
}
}

private static string GetEmptyMessage(string? path)
{
return $"value{path} is empty";
}
private static string GetEmptyMessage(string? path) => $"value{path} is empty";

private void NotEmptyBoxed(object? value, string? path)
{
Expand All @@ -65,9 +63,9 @@ private static class CachedEmptyDelegate
.GetMethod(nameof(NotEmptyExtensionsBase.NotEmptyInternal), BindingFlags.NonPublic | BindingFlags.Instance)!
.GetGenericMethodDefinition();

private static readonly Dictionary<Type, Action<object?, string>> Delegates = new();
private static readonly Dictionary<Type, Action<object?, string?>> Delegates = new();

public static Action<object?, string> GetDelegate(NotEmptyExtensionsBase @this, Type type)
public static Action<object?, string?> GetDelegate(NotEmptyExtensionsBase @this, Type type)
{
if (!Delegates.TryGetValue(type, out var result))
{
Expand All @@ -77,7 +75,7 @@ private static class CachedEmptyDelegate
{
var valueParam = Expression.Parameter(typeof(object));
var pathParam = Expression.Parameter(typeof(string));
result = (Action<object?, string>)Expression
result = (Action<object?, string?>)Expression
.Lambda(
Expression.Call(
Expression.Constant(@this),
Expand Down
13 changes: 8 additions & 5 deletions src/kasthack.NotEmpty.Tests/NotEmptyTestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,10 @@ public abstract class NotEmptyTestBase
public void PrimitiveWorks() => this.action(1);

[Fact]
public void ZeroThrows() => Assert.ThrowsAny<Exception>(() => this.action(0));
public void EmptyStringThrows() => Assert.ThrowsAny<Exception>(() => this.action(string.Empty));

[Fact]
public void DefaultThrows() => Assert.ThrowsAny<Exception>(() => this.action(0));

[Fact]
public void EmptyArrayThrows() => Assert.ThrowsAny<Exception>(() => this.action(new object[] { }));
Expand All @@ -39,15 +42,15 @@ public abstract class NotEmptyTestBase
public void EmptyListThrows() => Assert.ThrowsAny<Exception>(() => this.action(new List<object>()));

[Fact]
public void ListWorks() => this.action(new List<object> { new object() });
public void ArrayWithDefaultThrows() => Assert.ThrowsAny<Exception>(() => this.action(new object[] { 1, 0 }));

[Fact]
public void ArrayWorks() => this.action(new object[] { new object() });
public void ArrayWithNullThrows() => Assert.ThrowsAny<Exception>(() => this.action(new object?[] { null, new object() }));

[Fact]
public void ArrayWithDefaultThrows() => Assert.ThrowsAny<Exception>(() => this.action(new object[] { 1, 0 }));
public void ListWorks() => this.action(new List<object> { new object() });

[Fact]
public void ArrayWithNullThrows() => Assert.ThrowsAny<Exception>(() => this.action(new object?[] { null, new object() }));
public void ArrayWorks() => this.action(new object[] { new object() });
}
}
3 changes: 1 addition & 2 deletions src/kasthack.NotEmpty.Tests/kasthack.NotEmpty.Tests.csproj
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework>net461</TargetFramework>
<Nullable>enable</Nullable>

<IsPackable>false</IsPackable>
</PropertyGroup>

Expand Down

0 comments on commit 0000000

Please sign in to comment.