-
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 BitzArt.CoreExtensions * Set TargetFrameworks * Add ImplicitUsings * Update TaskExtensions
- Loading branch information
Showing
9 changed files
with
301 additions
and
1 deletion.
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 |
---|---|---|
@@ -0,0 +1,37 @@ | ||
name: Release CoreExtensions package | ||
|
||
on: | ||
repository_dispatch: | ||
push: | ||
tags: | ||
- "CoreExtensions-v[0-9]+.[0-9]+.[0-9]+*" | ||
|
||
env: | ||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | ||
NUGET_APIKEY: ${{ secrets.NUGET_APIKEY}} | ||
|
||
jobs: | ||
|
||
Release: | ||
name: 'Release CoreExtensions package' | ||
runs-on: ubuntu-latest | ||
steps: | ||
|
||
- name: Checkout | ||
uses: actions/checkout@v2 | ||
|
||
- name: Verify commit | ||
run: | | ||
git fetch --no-tags --prune --depth=1 origin +refs/heads/*:refs/remotes/origin/* | ||
git branch --remote --contains | grep origin/main | ||
- name: Set version | ||
run: echo "VERSION=${GITHUB_REF/refs\/tags\/CoreExtensions-v/}" >> $GITHUB_ENV | ||
|
||
- name: Build | ||
run: | | ||
dotnet build src/Misc/BitzArt.CoreExtensions/BitzArt.CoreExtensions.csproj --configuration Release /p:Version=${VERSION} | ||
dotnet pack src/Misc/BitzArt.CoreExtensions/BitzArt.CoreExtensions.csproj --configuration Release /p:Version=${VERSION} --no-build --output . | ||
- name: Push | ||
run: dotnet nuget push BitzArt.CoreExtensions.${VERSION}.nupkg --source https://api.nuget.org/v3/index.json --api-key ${NUGET_APIKEY} |
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,30 @@ | ||
name: Tests | ||
|
||
on: | ||
push: | ||
branches: | ||
- main | ||
tags-ignore: | ||
- '*' | ||
paths: | ||
- "src/**" | ||
- "tests/**" | ||
- "Miscellaneous.sln" | ||
- ".github/workflows/Tests.yml" | ||
|
||
jobs: | ||
tests: | ||
|
||
runs-on: ubuntu-latest | ||
|
||
steps: | ||
- uses: actions/checkout@v2 | ||
|
||
- name: Install dependencies | ||
run: dotnet restore | ||
|
||
- name: Build | ||
run: dotnet build --configuration Release --no-restore | ||
|
||
- name: Test | ||
run: dotnet test --no-restore --verbosity normal |
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
19 changes: 19 additions & 0 deletions
19
src/Misc/BitzArt.CoreExtensions/BitzArt.CoreExtensions.csproj
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,19 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFrameworks>net6.0;net7.0;net8.0</TargetFrameworks> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<RootNamespace>BitzArt</RootNamespace> | ||
<Nullable>enable</Nullable> | ||
<GenerateDocumentationFile>true</GenerateDocumentationFile> | ||
|
||
<PackageId>BitzArt.CoreExtensions</PackageId> | ||
<Authors>BitzArt</Authors> | ||
<Description>Base Class Library Extensions</Description> | ||
<PackageLicenseExpression>MIT</PackageLicenseExpression> | ||
<RepositoryType>git</RepositoryType> | ||
<RepositoryUrl>https://github.com/BitzArt/Miscellaneous</RepositoryUrl> | ||
<PackageProjectUrl>https://github.com/BitzArt/Miscellaneous</PackageProjectUrl> | ||
</PropertyGroup> | ||
|
||
</Project> |
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,37 @@ | ||
namespace BitzArt; | ||
|
||
/// <summary> | ||
/// Condition for comparing values. | ||
/// </summary> | ||
public enum ComparisonType : byte | ||
{ | ||
/// <summary> | ||
/// Represents an equality condition. | ||
/// </summary> | ||
Equal = 0, | ||
|
||
/// <summary> | ||
/// Represents an inequality condition. | ||
/// </summary> | ||
NotEqual = 1, | ||
|
||
/// <summary> | ||
/// Represents a 'greater than' condition. | ||
/// </summary> | ||
GreaterThan = 2, | ||
|
||
/// <summary> | ||
/// Represents a 'greater than or equal' condition. | ||
/// </summary> | ||
GreaterThanOrEqual = 3, | ||
|
||
/// <summary> | ||
/// Represents a 'less than' condition. | ||
/// </summary> | ||
LessThan = 4, | ||
|
||
/// <summary> | ||
/// Represents a 'less than or equal' condition. | ||
/// </summary> | ||
LessThanOrEqual = 5 | ||
} |
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,39 @@ | ||
namespace BitzArt; | ||
|
||
/// <summary> | ||
/// Order direction (ascending/descending). | ||
/// </summary> | ||
public enum OrderDirection : byte | ||
{ | ||
/// <summary> | ||
/// Ascending order direction. | ||
/// </summary> | ||
Ascending = 0, | ||
|
||
/// <summary> | ||
/// Descending order direction. | ||
/// </summary> | ||
Descending = 1 | ||
} | ||
|
||
/// <summary> | ||
/// Extensions to <see cref="OrderDirection"/>. | ||
/// </summary> | ||
public static class OrderDirectionExtensions | ||
{ | ||
/// <summary> | ||
/// Reverse the order direction. | ||
/// </summary> | ||
/// <param name="orderDirection"></param> | ||
/// <returns></returns> | ||
/// <exception cref="ArgumentOutOfRangeException"></exception> | ||
public static OrderDirection Reverse(this OrderDirection orderDirection) | ||
{ | ||
return orderDirection switch | ||
{ | ||
OrderDirection.Ascending => OrderDirection.Descending, | ||
OrderDirection.Descending => OrderDirection.Ascending, | ||
_ => throw new ArgumentOutOfRangeException(nameof(orderDirection), orderDirection, null) | ||
}; | ||
} | ||
} |
51 changes: 51 additions & 0 deletions
51
src/Misc/BitzArt.CoreExtensions/Extensions/TaskExtensions.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,51 @@ | ||
namespace BitzArt; | ||
|
||
/// <summary> | ||
/// Extension methods for <see cref="Task"/>. | ||
/// </summary> | ||
public static class TaskExtensions | ||
{ | ||
/// <summary> | ||
/// Awaits the <paramref name="task"/> until it is completed or cancelled, while ignoring cancellation exceptions. | ||
/// </summary> | ||
/// <param name="task">The task to await.</param> | ||
/// <param name="ignoreCancellation">Whether to ignore cancellation exceptions.</param> | ||
/// <param name="byTaskStatus">Determines whether to check for task cancellation by task status (if <see langword="true"/>) or by <see cref="OperationCanceledException"/> (if <see langword="false"/>).</param> | ||
public static Task IgnoreCancellation(this Task task, bool ignoreCancellation = true, bool byTaskStatus = true) | ||
{ | ||
if (!ignoreCancellation) return task; | ||
|
||
return byTaskStatus | ||
? IgnoreCancellationByTaskCancelledAsync(task) | ||
: IgnoreCancellationByExceptionAsync(task); | ||
} | ||
|
||
private static async Task IgnoreCancellationByTaskCancelledAsync(Task task) | ||
{ | ||
try | ||
{ | ||
await task; | ||
} | ||
catch | ||
{ | ||
if (task.IsCanceled) return; | ||
throw; | ||
} | ||
} | ||
|
||
private static async Task IgnoreCancellationByExceptionAsync(Task task) | ||
{ | ||
try | ||
{ | ||
await task; | ||
} | ||
catch (OperationCanceledException) | ||
{ | ||
return; | ||
} | ||
catch | ||
{ | ||
throw; | ||
} | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
tests/Misc/BitzArt.CoreExtensions.Tests/BitzArt.CoreExtensions.Tests.csproj
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,27 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net8.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
|
||
<IsPackable>false</IsPackable> | ||
<IsTestProject>true</IsTestProject> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="coverlet.collector" Version="6.0.0" /> | ||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" /> | ||
<PackageReference Include="xunit" Version="2.5.3" /> | ||
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.3" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\..\..\src\Misc\BitzArt.CoreExtensions\BitzArt.CoreExtensions.csproj" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<Using Include="Xunit" /> | ||
</ItemGroup> | ||
|
||
</Project> |
44 changes: 44 additions & 0 deletions
44
tests/Misc/BitzArt.CoreExtensions.Tests/TaskExtensionsTests.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,44 @@ | ||
namespace BitzArt.CoreExtensions.Tests; | ||
|
||
public class TaskExtensionsTests | ||
{ | ||
[Fact] | ||
public async Task IgnoreCancellation_WhenTrue_ShouldHandleExceptionByIgnoring() | ||
{ | ||
var cts = new CancellationTokenSource(); | ||
cts.CancelAfter(10); | ||
|
||
await Task.Delay(100, cts.Token).IgnoreCancellation(true); | ||
} | ||
|
||
[Fact] | ||
public async Task IgnoreCancellation_WhenFalse_ShouldDoNothing() | ||
{ | ||
var cts = new CancellationTokenSource(); | ||
cts.CancelAfter(10); | ||
|
||
await Assert.ThrowsAnyAsync<OperationCanceledException>(() | ||
=> Task.Delay(100, cts.Token).IgnoreCancellation(false)); | ||
} | ||
|
||
[Fact] | ||
public async Task IgnoreCancellation_WhenTrueAndInnerOperationCancelled_ShouldHandleExceptionByIgnoring() | ||
{ | ||
await TestMethodAsync().IgnoreCancellation(true); | ||
} | ||
|
||
[Fact] | ||
public async Task IgnoreCancellation_WhenFalseAndInnerOperationCancelled_ShouldDoNothing() | ||
{ | ||
await Assert.ThrowsAnyAsync<OperationCanceledException>(() | ||
=> TestMethodAsync().IgnoreCancellation(false)); | ||
} | ||
|
||
private async Task TestMethodAsync() | ||
{ | ||
var cts = new CancellationTokenSource(); | ||
cts.CancelAfter(10); | ||
|
||
await Task.Delay(100, cts.Token); | ||
} | ||
} |