-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add IsNotEmpty checks for collection (#30)
(MINOR)
- Loading branch information
1 parent
6091505
commit f64c994
Showing
8 changed files
with
118 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,6 +16,7 @@ env: | |
DOTNET_VERSION: '8.0.x' | ||
|
||
permissions: | ||
checks: write | ||
contents: read | ||
|
||
jobs: | ||
|
@@ -45,19 +46,20 @@ jobs: | |
run: dotnet build -c Release -p:Version=${{ steps.version.outputs.version}} | ||
|
||
- name: Run tests | ||
run: dotnet test tests/MGR.Guard.UnitTests/MGR.Guard.UnitTests.csproj -c Release --no-build --collect:"XPlat Code Coverage" --settings coverlet.runsettings | ||
run: dotnet test tests/MGR.Guard.UnitTests/MGR.Guard.UnitTests.csproj -c Release --no-build | ||
|
||
- name: Upload dotnet test results | ||
uses: actions/upload-artifact@v4 | ||
uses: dorny/[email protected] | ||
if: always() | ||
with: | ||
name: dotnet-results | ||
path: '**/coverage.cobertura.xml' | ||
if-no-files-found: warn | ||
# Use always() to always run this step to publish test results when there are test failures | ||
if: ${{ always() }} | ||
|
||
name: .NET Tests | ||
path: '**/MGR.Guard.UnitTests.trx' | ||
reporter: dotnet-trx | ||
fail-on-error: true | ||
|
||
- name: Push to MyGet | ||
env: | ||
NUGET_URL: https://www.myget.org/F/mgrosperrin/api/v3/index.json | ||
NUGET_API_KEY: ${{ secrets.MYGET_CI_API_KEY }} | ||
run: dotnet nuget push '**/*.nupkg' --source "$($env:NUGET_URL)" --api-key "$($env:NUGET_API_KEY)" | ||
if: github.event_name == 'push' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
using System; | ||
using System.Collections; | ||
using System.IO; | ||
using System.Linq.Expressions; | ||
using JetBrains.Annotations; | ||
|
||
namespace MGR.Guard | ||
{ | ||
#if MGR_GUARD_PUBLIC | ||
public | ||
#else | ||
internal | ||
#endif | ||
static partial class GuardianExtensions | ||
{ | ||
/// <summary> | ||
/// Checks that the specified <see cref="IEnumerable " /> <paramref name="expression" /> is empty. | ||
/// </summary> | ||
/// <param name="source">The <see cref="Guardian"/> to extends.</param> | ||
/// <param name="expression">The collection as an Expression.</param> | ||
[PublicAPI] | ||
public static void IsNotEmpty([NotNull] this Guardian source, [NotNull] Expression<Func<IEnumerable>> expression) | ||
{ | ||
source.IsNotNull(expression, nameof(expression)); | ||
|
||
var (collection, parameterName) = ((IGuardian)source).ExtractValueAndParameterNameFromExpression(expression); | ||
IsNotEmpty(source, collection, parameterName); | ||
} | ||
/// <summary> | ||
/// Checks if the specified <see cref="FileSystemInfo " /> <paramref name="collection" /> exists. | ||
/// </summary> | ||
/// <param name="source">The <see cref="Guardian"/> to extends.</param> | ||
/// <param name="collection">The file.</param> | ||
/// <param name="parameterName">The name of the file parameter.</param> | ||
[PublicAPI] | ||
public static void IsNotEmpty([NotNull] this Guardian source, [NotNull] IEnumerable collection, string parameterName) | ||
{ | ||
source.IsNotNull(collection, nameof(collection)); | ||
|
||
var enumerator = collection.GetEnumerator(); | ||
var hasItems = enumerator.MoveNext(); | ||
if (!hasItems) | ||
{ | ||
throw new ArgumentException(Messages.IsNotEmptyMessage, parameterName); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
tests/MGR.Guard.UnitTests/GuardianExtensionsTests.IsNotEmpty.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
using System; | ||
using System.Collections; | ||
using System.Linq; | ||
using Xunit; | ||
|
||
namespace MGR.Guard | ||
{ | ||
public partial class GuardianExtensionsTests | ||
{ | ||
public class IsNotEmpty | ||
{ | ||
[Fact] | ||
public void Collection_Should_Not_Be_Null() | ||
{ | ||
// Arrange | ||
IEnumerable collection = null; | ||
|
||
// Act & Assert | ||
var actualException = Assert.Throws<ArgumentNullException>(() => Guardian.ChecksThat.IsNotEmpty(collection, nameof(collection))); | ||
Assert.Equal("collection", actualException.ParamName); | ||
} | ||
|
||
[Fact] | ||
public void Collection_Should_Not_Be_Empty() | ||
{ | ||
// Arrange | ||
IEnumerable collection = Enumerable.Empty<object>(); | ||
|
||
// Act & Assert | ||
var actualException = Assert.Throws<ArgumentException>(() => Guardian.ChecksThat.IsNotEmpty(collection, nameof(collection))); | ||
Assert.Equal("collection", actualException.ParamName); | ||
} | ||
|
||
[Fact] | ||
public void Non_Empty_Collection() | ||
{ | ||
// Arrange | ||
IEnumerable collection = new []{new object()}; | ||
|
||
// Act & Assert | ||
Guardian.ChecksThat.IsNotEmpty(collection, nameof(collection)); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters