Skip to content

Commit

Permalink
Merge branch 'release/1.4.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
olivegamestudio committed Aug 28, 2024
2 parents 0d4fbf1 + 0575aa6 commit 67f4fa7
Show file tree
Hide file tree
Showing 4 changed files with 134 additions and 22 deletions.
44 changes: 44 additions & 0 deletions .github/workflows/dotnet.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# This workflow will build a .NET project
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-net

name: .NET

on:
push:
branches: [ "main", "develop" ]
pull_request:
branches: [ "main", "develop" ]

jobs:
build:

runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
filter: tree:0

- name: Get tags
run: git fetch --tags origin

- name: Setup .NET
uses: actions/setup-dotnet@v4
with:
dotnet-version: 8.0.x

- name: Restore dependencies
run: dotnet restore

- name: Build
run: dotnet build --configuration Release --no-restore

- name: Test
run: dotnet test --configuration Release -p:CollectCoverage=true

- name: Publish Nuget
run: |
dotnet nuget push '**/*.nupkg' -k ${NUGET} -s https://api.nuget.org/v3/index.json --skip-duplicate --no-symbols
env:
NUGET: ${{ secrets.NUGET }}
37 changes: 16 additions & 21 deletions Utility.Result.Tests/ResultTests.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using Musts;

namespace Utility;

public class ResultTests
Expand All @@ -6,58 +8,51 @@ public class ResultTests
public void OperatorAnd_Result_Returns_Success()
{
Result result = Result.Ok() && Result.Ok();
Assert.IsTrue(result.Success);
result.MustBeSuccess();

result = Result.Fail("Failed") && Result.Ok();
Assert.IsTrue(result.IsFailure);
result.MustBeFailure();

result = Result.Ok() && Result.Fail("Failed");
Assert.IsTrue(result.IsFailure);
result.MustBeFailure();
}

[Test]
public void Ok_Result_Returns_Success()
{
Result result = Result.Ok();

Assert.IsTrue(result.Success);
Assert.IsFalse(result.IsFailure);
Assert.IsTrue(string.IsNullOrEmpty(result.Error));
Assert.That(0, Is.EqualTo(result.ErrorCode));
result.MustBeSuccess();
result.Error.MustBeNullOrEmpty();
result.ErrorCode.MustBeZero();
}

[Test]
public void Ok_Result_WithValue_Returns_Success()
{
Result<int> result = Result.Ok(99);

Assert.IsTrue(result.Value == 99);
Assert.IsTrue(result.Success);
Assert.IsFalse(result.IsFailure);
Assert.IsTrue(string.IsNullOrEmpty(result.Error));
Assert.That(0, Is.EqualTo(result.ErrorCode));
result.Value.MustBeEqual(99);
result.MustBeSuccess();
result.Error.MustBeNullOrEmpty();
result.ErrorCode.MustBeZero();
}

[Test]
public void Fail_Result_Returns_Failure()
{
Result result = Result.Fail("The operation failed because of ...");

Assert.IsTrue(result.IsFailure);
Assert.IsFalse(result.Success);
Assert.IsFalse(string.IsNullOrEmpty(result.Error));
Assert.That(0, Is.EqualTo(result.ErrorCode));
result.MustBeFailure();
result.ErrorCode.MustBeZero();
}

[Test]
public void Fail_Result_WithErrorCode_Returns_Failure()
{
Result result = Result.Fail("The operation failed because of ...", -1);

Assert.IsTrue(result.IsFailure);
Assert.IsFalse(result.Success);
Assert.IsFalse(string.IsNullOrEmpty(result.Error));
Assert.That(-1, Is.EqualTo(result.ErrorCode));
result.MustBeFailure();
result.ErrorCode.MustBeEqual(-1);
}

}
4 changes: 3 additions & 1 deletion Utility.Result.Tests/Utility.Result.Tests.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
Expand All @@ -11,6 +11,8 @@
<ItemGroup>
<PackageReference Include="coverlet.collector" Version="*" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.*" />
<PackageReference Include="Musts" Version="*" />
<PackageReference Include="Musts.Result" Version="*" />
<PackageReference Include="NUnit" Version="3.*" />
<PackageReference Include="NUnit.Analyzers" Version="3.*" />
<PackageReference Include="NUnit3TestAdapter" Version="4.*" />
Expand Down
71 changes: 71 additions & 0 deletions Utility.Result/Result.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,52 @@ namespace Utility;
/// </summary>
public record Result
{
/// <summary>
/// Gets a value indicating whether the result is a success.
/// </summary>
public bool Success { get; }

/// <summary>
/// Gets a value indicating whether the result is a failure.
/// </summary>
public bool IsFailure => !Success;

/// <summary>
/// Gets the error message associated with the result.
/// </summary>
public string Error { get; private set; }

/// <summary>
/// Gets the error code associated with the result.
/// </summary>
public int ErrorCode { get; private set; }

/// <summary>
/// Defines an implicit conversion to a boolean value.
/// </summary>
/// <param name="x">The result to convert.</param>
/// <returns>True if the result is a success; otherwise, false.</returns>
public static bool operator true(Result x)
{
return x.Success;
}

/// <summary>
/// Defines an implicit conversion to a boolean value.
/// </summary>
/// <param name="x">The result to convert.</param>
/// <returns>True if the result is a failure; otherwise, false.</returns>
public static bool operator false(Result x)
{
return x.IsFailure;
}

/// <summary>
/// Combines two results using a logical AND operation.
/// </summary>
/// <param name="lhs">The left-hand side result.</param>
/// <param name="rhs">The right-hand side result.</param>
/// <returns>The combined result.</returns>
public static Result operator &(Result lhs, Result rhs)
{
if (lhs.Success && rhs.Success)
Expand All @@ -46,38 +74,81 @@ public static bool operator false(Result x)
return Result.Ok();
}

/// <summary>
/// Initializes a new instance of the <see cref="Result"/> class.
/// </summary>
/// <param name="success">A value indicating whether the result is a success.</param>
/// <param name="error">The error message associated with the result.</param>
/// <param name="errorCode">The error code associated with the result.</param>
protected Result(bool success, string error, int errorCode)
{
Success = success;
Error = error;
ErrorCode = errorCode;
}

/// <summary>
/// Creates a failure result with the specified error message and error code.
/// </summary>
/// <param name="message">The error message.</param>
/// <param name="errorCode">The error code (optional).</param>
/// <returns>A failure result.</returns>
public static Result Fail(string message, int errorCode = 0)
{
return new Result(false, message, errorCode);
}

/// <summary>
/// Creates a failure result with the specified error message and error code.
/// </summary>
/// <typeparam name="T">The type of the value associated with the result.</typeparam>
/// <param name="message">The error message.</param>
/// <param name="errorCode">The error code (optional).</param>
/// <returns>A failure result.</returns>
public static Result<T> Fail<T>(string message, int errorCode = 0)
{
return new Result<T>(default(T), false, message, errorCode);

Check warning on line 110 in Utility.Result/Result.cs

View workflow job for this annotation

GitHub Actions / build

Possible null reference argument for parameter 'value' in 'Result<T>.Result(T value, bool success, string error, int errorCode)'.

Check warning on line 110 in Utility.Result/Result.cs

View workflow job for this annotation

GitHub Actions / build

Possible null reference argument for parameter 'value' in 'Result<T>.Result(T value, bool success, string error, int errorCode)'.
}

/// <summary>
/// Creates a success result.
/// </summary>
/// <returns>A success result.</returns>
public static Result Ok()
{
return new Result(true, string.Empty, 0);
}

/// <summary>
/// Creates a success result with the specified value.
/// </summary>
/// <typeparam name="T">The type of the value associated with the result.</typeparam>
/// <param name="value">The value associated with the result.</param>
/// <returns>A success result.</returns>
public static Result<T> Ok<T>(T value)
{
return new Result<T>(value, true, string.Empty, 0);
}
}

/// <summary>
/// A result from a task which can be a success or failure, with an associated value.
/// </summary>
/// <typeparam name="T">The type of the value associated with the result.</typeparam>
public record Result<T> : Result
{
/// <summary>
/// Gets or sets the value associated with the result.
/// </summary>
public T Value { get; set; }

/// <summary>
/// Initializes a new instance of the <see cref="Result{T}"/> class.
/// </summary>
/// <param name="value">The value associated with the result.</param>
/// <param name="success">A value indicating whether the result is a success.</param>
/// <param name="error">The error message associated with the result.</param>
/// <param name="errorCode">The error code associated with the result.</param>
protected internal Result(T value, bool success, string error, int errorCode) : base(success, error, errorCode)
{
Value = value;
Expand Down

0 comments on commit 67f4fa7

Please sign in to comment.