diff --git a/README.md b/README.md index c3791e9..e195837 100644 --- a/README.md +++ b/README.md @@ -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 tests for more details. ````csharp using kasthack.NotEmpty.Xunit; // replace the namespace to match your test framework diff --git a/src/kasthack.NotEmpty.Core/NotEmptyExtensionsBase.cs b/src/kasthack.NotEmpty.Core/NotEmptyExtensionsBase.cs index 73539d6..cb90078 100644 --- a/src/kasthack.NotEmpty.Core/NotEmptyExtensionsBase.cs +++ b/src/kasthack.NotEmpty.Core/NotEmptyExtensionsBase.cs @@ -9,7 +9,7 @@ public abstract class NotEmptyExtensionsBase { public void NotEmpty(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!); @@ -23,6 +23,7 @@ public void NotEmpty(T? value) private void NotEmptyInternal(T? value, string? path = null) { string message = GetEmptyMessage(path); + this.Assert(value is not null, message); //fast lane this.Assert(!EqualityComparer.Default.Equals(default!, value!), message); switch (value) { @@ -48,10 +49,7 @@ private void NotEmptyInternal(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) { @@ -65,9 +63,9 @@ private static class CachedEmptyDelegate .GetMethod(nameof(NotEmptyExtensionsBase.NotEmptyInternal), BindingFlags.NonPublic | BindingFlags.Instance)! .GetGenericMethodDefinition(); - private static readonly Dictionary> Delegates = new(); + private static readonly Dictionary> Delegates = new(); - public static Action GetDelegate(NotEmptyExtensionsBase @this, Type type) + public static Action GetDelegate(NotEmptyExtensionsBase @this, Type type) { if (!Delegates.TryGetValue(type, out var result)) { @@ -77,7 +75,7 @@ private static class CachedEmptyDelegate { var valueParam = Expression.Parameter(typeof(object)); var pathParam = Expression.Parameter(typeof(string)); - result = (Action)Expression + result = (Action)Expression .Lambda( Expression.Call( Expression.Constant(@this), diff --git a/src/kasthack.NotEmpty.Tests/NotEmptyTestBase.cs b/src/kasthack.NotEmpty.Tests/NotEmptyTestBase.cs index 70a127b..52895cc 100644 --- a/src/kasthack.NotEmpty.Tests/NotEmptyTestBase.cs +++ b/src/kasthack.NotEmpty.Tests/NotEmptyTestBase.cs @@ -30,7 +30,10 @@ public abstract class NotEmptyTestBase public void PrimitiveWorks() => this.action(1); [Fact] - public void ZeroThrows() => Assert.ThrowsAny(() => this.action(0)); + public void EmptyStringThrows() => Assert.ThrowsAny(() => this.action(string.Empty)); + + [Fact] + public void DefaultThrows() => Assert.ThrowsAny(() => this.action(0)); [Fact] public void EmptyArrayThrows() => Assert.ThrowsAny(() => this.action(new object[] { })); @@ -39,15 +42,15 @@ public abstract class NotEmptyTestBase public void EmptyListThrows() => Assert.ThrowsAny(() => this.action(new List())); [Fact] - public void ListWorks() => this.action(new List { new object() }); + public void ArrayWithDefaultThrows() => Assert.ThrowsAny(() => this.action(new object[] { 1, 0 })); [Fact] - public void ArrayWorks() => this.action(new object[] { new object() }); + public void ArrayWithNullThrows() => Assert.ThrowsAny(() => this.action(new object?[] { null, new object() })); [Fact] - public void ArrayWithDefaultThrows() => Assert.ThrowsAny(() => this.action(new object[] { 1, 0 })); + public void ListWorks() => this.action(new List { new object() }); [Fact] - public void ArrayWithNullThrows() => Assert.ThrowsAny(() => this.action(new object?[] { null, new object() })); + public void ArrayWorks() => this.action(new object[] { new object() }); } } \ No newline at end of file diff --git a/src/kasthack.NotEmpty.Tests/kasthack.NotEmpty.Tests.csproj b/src/kasthack.NotEmpty.Tests/kasthack.NotEmpty.Tests.csproj index 8b5212d..21e0541 100644 --- a/src/kasthack.NotEmpty.Tests/kasthack.NotEmpty.Tests.csproj +++ b/src/kasthack.NotEmpty.Tests/kasthack.NotEmpty.Tests.csproj @@ -1,9 +1,8 @@ - net6.0 + net461 enable - false