// Copyright (c) The Geekeey Authors // SPDX-License-Identifier: EUPL-1.2 namespace Geekeey.Request.Result.Tests; internal sealed class PreludeTests { [Test] public async Task I_can_try_with_success_value_and_get_a_success_result() { var result = Prelude.Try(() => 2); using var scope = Assert.Multiple(); await Assert.That(result.IsSuccess).IsTrue(); await Assert.That(result.Value).IsEqualTo(2); } [Test] public async Task I_can_try_with_throwing_exception_and_get_a_failure_result() { var result = Prelude.Try(() => throw new CustomTestException()); using var scope = Assert.Multiple(); await Assert.That(result.IsSuccess).IsFalse(); var instance = await Assert.That(result.Error).IsTypeOf(); await Assert.That(instance?.Exception).IsTypeOf(); } [Test] public async Task I_can_try_with_async_success_value_and_get_a_success_result() { var result = await Prelude.TryAsync(() => Task.FromResult(2)); using var scope = Assert.Multiple(); await Assert.That(result.IsSuccess).IsTrue(); await Assert.That(result.Value).IsEqualTo(2); } [Test] public async Task I_can_try_with_async_throwing_exception_and_get_a_failure_result() { var result = await Prelude.TryAsync(Task () => throw new CustomTestException()); using var scope = Assert.Multiple(); await Assert.That(result.IsSuccess).IsFalse(); var instance = await Assert.That(result.Error).IsTypeOf(); await Assert.That(instance?.Exception).IsTypeOf(); } [Test] public async Task I_can_try_with_async_await_throwing_exception_and_get_a_failure_result() { var result = await Prelude.TryAsync(async Task () => { await Task.CompletedTask; throw new CustomTestException(); }); using var scope = Assert.Multiple(); await Assert.That(result.IsSuccess).IsFalse(); var instance = await Assert.That(result.Error).IsTypeOf(); await Assert.That(instance?.Exception).IsTypeOf(); } [Test] public async Task I_can_try_with_async_success_value_and_get_a_success_result_of_type_ValueTask() { var result = await Prelude.TryAsync(() => ValueTask.FromResult(2)); using var scope = Assert.Multiple(); await Assert.That(result.IsSuccess).IsTrue(); await Assert.That(result.Value).IsEqualTo(2); } [Test] public async Task I_can_try_with_async_throwing_exception_and_get_a_failure_result_of_type_ValueTask() { var result = await Prelude.TryAsync(ValueTask () => throw new CustomTestException()); using var scope = Assert.Multiple(); await Assert.That(result.IsSuccess).IsFalse(); var instance = await Assert.That(result.Error).IsTypeOf(); await Assert.That(instance?.Exception).IsTypeOf(); } [Test] public async Task I_can_try_with_async_await_throwing_exception_and_get_a_failure_result_of_type_ValueTask() { var result = await Prelude.TryAsync(async ValueTask () => { await Task.CompletedTask; throw new CustomTestException(); }); using var scope = Assert.Multiple(); await Assert.That(result.IsSuccess).IsFalse(); var instance = await Assert.That(result.Error).IsTypeOf(); await Assert.That(instance?.Exception).IsTypeOf(); } }