Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: SimonCropp/Polyfill
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: 6.2.2
Choose a base ref
...
head repository: SimonCropp/Polyfill
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: 6.2.3
Choose a head ref
  • 2 commits
  • 3 files changed
  • 1 contributor

Commits on Aug 9, 2024

  1. fix for not empty array

    SimonCropp committed Aug 9, 2024
    Copy the full SHA
    6532ca7 View commit details
  2. Merge pull request #206 from SimonCropp/fix-for-not-empty-array

    fix for Guard with not empty array
    SimonCropp authored Aug 9, 2024
    Copy the full SHA
    360b8e6 View commit details
Showing with 47 additions and 25 deletions.
  1. +1 −1 src/Directory.Build.props
  2. +9 −1 src/Polyfill/Guard_NotEmpty.cs
  3. +37 −23 src/Tests/GuardTests.cs
2 changes: 1 addition & 1 deletion src/Directory.Build.props
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<Project>
<PropertyGroup>
<Version>6.2.2</Version>
<Version>6.2.3</Version>
<AssemblyVersion>1.0.0</AssemblyVersion>
<PackageTags>Polyfill</PackageTags>
<DisableImplicitNamespaceImports>true</DisableImplicitNamespaceImports>
10 changes: 9 additions & 1 deletion src/Polyfill/Guard_NotEmpty.cs
Original file line number Diff line number Diff line change
@@ -131,10 +131,18 @@ public static void NotEmpty<T>(
}
}

if (value is T[] array)
{
if (array.Length == 0)
{
throw new ArgumentException("Argument cannot be empty.", argumentName);
}
}

var enumerator = value.GetEnumerator();
try
{
if (enumerator.MoveNext())
if (!enumerator.MoveNext())
{
throw new ArgumentException("Argument cannot be empty.", argumentName);
}
60 changes: 37 additions & 23 deletions src/Tests/GuardTests.cs
Original file line number Diff line number Diff line change
@@ -1,30 +1,33 @@
[TestFixture]
public class GuardTests
{
string nullString = null!;
List<string> nullList = null!;
IList<string> nullIList = null!;
IReadOnlyList<string> nullIReadOnlyList = null!;
ICollection<string> nullICollection = null!;
IReadOnlyCollection<string> nullIReadOnlyCollection = null!;
IEnumerable<string> nullEnumerable = null!;
string[] nullArray = null!;
Dictionary<int, string> nullDictionary = null!;
IDictionary<int, string> nullIDictionary = null!;
IReadOnlyDictionary<int, string> nullIReadOnlyDictionary = null!;
object nullObject = null!;
static string nullString = null!;
static List<string> nullList = null!;
static IList<string> nullIList = null!;
static IReadOnlyList<string> nullIReadOnlyList = null!;
static ICollection<string> nullICollection = null!;
static IReadOnlyCollection<string> nullIReadOnlyCollection = null!;
static IEnumerable<string> nullEnumerable = null!;
static string[] nullArray = null!;
static Dictionary<int, string> nullDictionary = null!;
static IDictionary<int, string> nullIDictionary = null!;
static IReadOnlyDictionary<int, string> nullIReadOnlyDictionary = null!;
static object nullObject = null!;

string emptyString = string.Empty;
List<string> emptyList = [];
IList<string> emptyIList = [];
IReadOnlyList<string> emptyIReadOnlyList = [];
ICollection<string> emptyICollection = [];
IReadOnlyCollection<string> emptyIReadOnlyCollection = [];
IEnumerable<string> emptyEnumerable = [];
Dictionary<int, string> emptyDictionary = new();
IDictionary<int, string> emptyIDictionary = new Dictionary<int, string>();
IReadOnlyDictionary<int, string> emptyIReadOnlyDictionary = new Dictionary<int, string>();
string[] emptyArray = [];
static string emptyString = string.Empty;
static List<string> emptyList = [];
static IList<string> emptyIList = [];
static IReadOnlyList<string> emptyIReadOnlyList = [];
static ICollection<string> emptyICollection = [];
static IReadOnlyCollection<string> emptyIReadOnlyCollection = [];
static IEnumerable<string> emptyEnumerable = [];
static Dictionary<int, string> emptyDictionary = new();
static IDictionary<int, string> emptyIDictionary = new Dictionary<int, string>();
static IReadOnlyDictionary<int, string> emptyIReadOnlyDictionary = new Dictionary<int, string>();
static string[] emptyArray = [];
static string[] nonEmptyArray = ["value"];
static List<string> nonEmptyList = ["value"];
static IEnumerable<string> nonEmptyEnumerable = nonEmptyList.Select(x => x);

[Test]
public void NotNull()
@@ -53,6 +56,10 @@ public void NotNull()
() => Guard.NotNull(nullIDictionary));
Assert.Throws<ArgumentNullException>(
() => Guard.NotNull(nullIReadOnlyDictionary));
Guard.NotNull(nonEmptyArray);
Guard.NotNull(nonEmptyEnumerable);
Guard.NotNull("value");
Guard.NotNull(nonEmptyEnumerable);
}

[Test]
@@ -112,6 +119,10 @@ public void NotNullOrEmpty()
() => Guard.NotNullOrEmpty(nullIDictionary));
Assert.Throws<ArgumentNullException>(
() => Guard.NotNullOrEmpty(nullIReadOnlyDictionary));
Guard.NotNullOrEmpty(nonEmptyArray);
Guard.NotNullOrEmpty(nonEmptyEnumerable);
Guard.NotNullOrEmpty("value");
Guard.NotNullOrEmpty(nonEmptyEnumerable);
}

[Test]
@@ -166,6 +177,9 @@ public void NotEmpty()
Guard.NotEmpty(nullIReadOnlyList);
Guard.NotEmpty(nullIReadOnlyCollection);
Guard.NotEmpty(nullArray);
Guard.NotEmpty(nonEmptyArray);
Guard.NotEmpty("value");
Guard.NotEmpty(nonEmptyList);
Guard.NotEmpty(nullEnumerable);
Guard.NotEmpty(nullDictionary);
Guard.NotEmpty(nullIDictionary);