100 lines
No EOL
3.2 KiB
C#
100 lines
No EOL
3.2 KiB
C#
// 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<int>(() => throw new CustomTestException());
|
|
|
|
using var scope = Assert.Multiple();
|
|
await Assert.That(result.IsSuccess).IsFalse();
|
|
var instance = await Assert.That(result.Error).IsTypeOf<ExceptionError>();
|
|
await Assert.That(instance?.Exception).IsTypeOf<CustomTestException>();
|
|
}
|
|
|
|
[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<int> () => throw new CustomTestException());
|
|
|
|
using var scope = Assert.Multiple();
|
|
await Assert.That(result.IsSuccess).IsFalse();
|
|
var instance = await Assert.That(result.Error).IsTypeOf<ExceptionError>();
|
|
await Assert.That(instance?.Exception).IsTypeOf<CustomTestException>();
|
|
}
|
|
|
|
[Test]
|
|
public async Task I_can_try_with_async_await_throwing_exception_and_get_a_failure_result()
|
|
{
|
|
var result = await Prelude.TryAsync(async Task<int> () =>
|
|
{
|
|
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<ExceptionError>();
|
|
await Assert.That(instance?.Exception).IsTypeOf<CustomTestException>();
|
|
}
|
|
|
|
[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<int> () => throw new CustomTestException());
|
|
|
|
using var scope = Assert.Multiple();
|
|
await Assert.That(result.IsSuccess).IsFalse();
|
|
var instance = await Assert.That(result.Error).IsTypeOf<ExceptionError>();
|
|
await Assert.That(instance?.Exception).IsTypeOf<CustomTestException>();
|
|
}
|
|
|
|
[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<int> () =>
|
|
{
|
|
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<ExceptionError>();
|
|
await Assert.That(instance?.Exception).IsTypeOf<CustomTestException>();
|
|
}
|
|
} |