From c61590f26f6249a890ca198d039be72f9ca6fc82 Mon Sep 17 00:00:00 2001 From: Kyle Ratti Date: Fri, 3 May 2024 17:07:19 -0400 Subject: [PATCH] test: 100% test coverage of Result --- Base.Tests/Structures/ResultTests.cs | 65 ++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 Base.Tests/Structures/ResultTests.cs diff --git a/Base.Tests/Structures/ResultTests.cs b/Base.Tests/Structures/ResultTests.cs new file mode 100644 index 0000000..8d0013a --- /dev/null +++ b/Base.Tests/Structures/ResultTests.cs @@ -0,0 +1,65 @@ +using System; +using FruityFoundation.Base.Structures; +using NUnit.Framework; + +namespace Base.Tests.Structures; + +public class ResultTests +{ + [Test] + public void Result_CreateSuccess_ReturnsSuccess() + { + // Arrange + var value = Result.CreateSuccess(25); + + // Act + var mergeResult = value.Merge( + onSuccess: _ => "onSuccess", + onFailure: _ => "onFailure"); + + // Assert + Assert.That(value, Is.InstanceOf>()); + Assert.That(value.IsSuccess, Is.True); + Assert.That(value.IsFailure, Is.False); + Assert.That(value.SuccessVal, Is.EqualTo(25)); + var failureValException = Assert.Throws(() => _ = value.FailureVal); + Assert.That(failureValException.Message, Is.EqualTo("Maybe value is empty")); + + Assert.That(mergeResult, Is.EqualTo("onSuccess")); + + Assert.That(value.TrySuccess(out var successVal), Is.True); + Assert.That(successVal, Is.EqualTo(25)); + + Assert.That(value.TryFailure(out var failureVal), Is.False); + Assert.That(failureVal, Is.Default); + } + + [Test] + public void Result_CreateFailure_ReturnsFailure() + { + // Arrange + var value = Result.CreateFailure("bananas aren't numbers"); + + // Act + var mergeResult = value.Merge( + onSuccess: _ => "onSuccess", + onFailure: _ => "onFailure"); + + // Assert + Assert.That(value, Is.InstanceOf>()); + Assert.That(value.IsSuccess, Is.False); + Assert.That(value.IsFailure, Is.True); + Assert.That(value.FailureVal, Is.EqualTo("bananas aren't numbers")); + + var successValException = Assert.Throws(() => _ = value.SuccessVal); + Assert.That(successValException.Message, Is.EqualTo("Maybe value is empty")); + + Assert.That(mergeResult, Is.EqualTo("onFailure")); + + Assert.That(value.TrySuccess(out var successVal), Is.False); + Assert.That(successVal, Is.Default); + + Assert.That(value.TryFailure(out var failureVal), Is.True); + Assert.That(failureVal, Is.EqualTo("bananas aren't numbers")); + } +}