365 lines
7.6 KiB
C#
365 lines
7.6 KiB
C#
// Copyright (c) The Geekeey Authors
|
|
// SPDX-License-Identifier: EUPL-1.2
|
|
|
|
namespace Geekeey.Request.Result.Tests;
|
|
|
|
internal sealed class ResultMatchingTests
|
|
{
|
|
[Test]
|
|
public async Task I_can_match_and_it_calls_success_func_for_success()
|
|
{
|
|
var result = Prelude.Success(2);
|
|
var match = result.Match(
|
|
v => v,
|
|
_ => throw new InvalidOperationException());
|
|
|
|
await Assert.That(match).IsEqualTo(2);
|
|
}
|
|
|
|
[Test]
|
|
public async Task I_can_match_and_it_calls_failure_func_for_failure()
|
|
{
|
|
var result = Prelude.Failure<int>("error");
|
|
var match = result.Match(
|
|
_ => throw new InvalidOperationException(),
|
|
e => e);
|
|
|
|
await Assert.That(match.Message).IsEqualTo("error");
|
|
}
|
|
|
|
[Test]
|
|
public async Task I_can_switch_and_it_calls_success_action_for_success()
|
|
{
|
|
var called = false;
|
|
var value = default(int);
|
|
|
|
var result = Prelude.Success(2);
|
|
result.Switch(OnSuccess, OnFailure);
|
|
|
|
await Assert.That(called).IsTrue();
|
|
await Assert.That(value).IsEqualTo(2);
|
|
|
|
return;
|
|
|
|
void OnSuccess(int i)
|
|
{
|
|
value = i;
|
|
called = true;
|
|
}
|
|
|
|
void OnFailure(Error e)
|
|
{
|
|
throw new InvalidOperationException();
|
|
}
|
|
}
|
|
|
|
[Test]
|
|
public async Task I_can_switch_and_it_calls_failure_action_for_failure()
|
|
{
|
|
var called = false;
|
|
var value = default(Error);
|
|
|
|
var result = Prelude.Failure<int>("error");
|
|
result.Switch(OnSuccess, OnFailure);
|
|
|
|
await Assert.That(called).IsTrue();
|
|
await Assert.That(value?.Message).IsEqualTo("error");
|
|
|
|
return;
|
|
|
|
void OnSuccess(int i)
|
|
{
|
|
throw new InvalidOperationException();
|
|
}
|
|
|
|
void OnFailure(Error e)
|
|
{
|
|
value = e;
|
|
called = true;
|
|
}
|
|
}
|
|
|
|
[Test]
|
|
public async Task I_can_match_async_and_it_calls_success_func_for_success()
|
|
{
|
|
var result = Prelude.Success(2);
|
|
var match = await result.MatchAsync(
|
|
Task.FromResult,
|
|
_ => throw new InvalidOperationException());
|
|
|
|
await Assert.That(match).IsEqualTo(2);
|
|
}
|
|
|
|
[Test]
|
|
public async Task I_can_match_async_and_it_calls_failure_func_for_failure()
|
|
{
|
|
var result = Prelude.Failure<int>("error");
|
|
var match = await result.MatchAsync(
|
|
_ => throw new InvalidOperationException(),
|
|
Task.FromResult);
|
|
|
|
await Assert.That(match.Message).IsEqualTo("error");
|
|
}
|
|
|
|
[Test]
|
|
public async Task I_can_switch_async_and_it_calls_success_action_for_success()
|
|
{
|
|
var called = false;
|
|
var value = default(int);
|
|
|
|
var result = Prelude.Success(2);
|
|
await result.SwitchAsync(OnSuccess, OnFailure);
|
|
|
|
await Assert.That(called).IsTrue();
|
|
await Assert.That(value).IsEqualTo(2);
|
|
return;
|
|
|
|
Task OnSuccess(int i)
|
|
{
|
|
value = i;
|
|
called = true;
|
|
return Task.CompletedTask;
|
|
}
|
|
|
|
Task OnFailure(Error e)
|
|
{
|
|
throw new InvalidOperationException();
|
|
}
|
|
}
|
|
|
|
[Test]
|
|
public async Task I_can_switch_async_and_it_calls_failure_action_for_failure()
|
|
{
|
|
var called = false;
|
|
var value = default(Error);
|
|
|
|
var result = Prelude.Failure<int>("error");
|
|
await result.SwitchAsync(OnSuccess, OnFailure);
|
|
|
|
await Assert.That(called).IsTrue();
|
|
await Assert.That(value?.Message).IsEqualTo("error");
|
|
|
|
return;
|
|
|
|
Task OnSuccess(int i)
|
|
{
|
|
throw new InvalidOperationException();
|
|
}
|
|
|
|
Task OnFailure(Error e)
|
|
{
|
|
value = e;
|
|
called = true;
|
|
return Task.CompletedTask;
|
|
}
|
|
}
|
|
|
|
[Test]
|
|
public async Task I_can_match_and_it_calls_success_func_for_success_ValueTask()
|
|
{
|
|
var result = Prelude.Success(2);
|
|
var match = await result.MatchAsync(
|
|
ValueTask.FromResult,
|
|
_ => throw new InvalidOperationException());
|
|
|
|
await Assert.That(match).IsEqualTo(2);
|
|
}
|
|
|
|
[Test]
|
|
public async Task I_can_match_async_and_it_calls_failure_func_for_failure_ValueTask()
|
|
{
|
|
var result = Prelude.Failure<int>("error");
|
|
var match = await result.MatchAsync(
|
|
_ => throw new InvalidOperationException(),
|
|
ValueTask.FromResult);
|
|
|
|
await Assert.That(match.Message).IsEqualTo("error");
|
|
}
|
|
|
|
[Test]
|
|
public async Task I_can_switch_async_and_it_calls_success_action_for_success_ValueTask()
|
|
{
|
|
var called = false;
|
|
var value = default(int);
|
|
|
|
var result = Prelude.Success(2);
|
|
await result.SwitchAsync(OnSuccess, OnFailure);
|
|
|
|
await Assert.That(called).IsTrue();
|
|
await Assert.That(value).IsEqualTo(2);
|
|
|
|
return;
|
|
|
|
ValueTask OnSuccess(int i)
|
|
{
|
|
value = i;
|
|
called = true;
|
|
return ValueTask.CompletedTask;
|
|
}
|
|
|
|
ValueTask OnFailure(Error e)
|
|
{
|
|
throw new InvalidOperationException();
|
|
}
|
|
}
|
|
|
|
[Test]
|
|
public async Task I_can_switch_async_and_it_calls_failure_action_for_failure_ValueTask()
|
|
{
|
|
var called = false;
|
|
var value = default(Error);
|
|
|
|
var result = Prelude.Failure<int>("error");
|
|
await result.SwitchAsync(OnSuccess, OnFailure);
|
|
|
|
await Assert.That(called).IsTrue();
|
|
await Assert.That(value?.Message).IsEqualTo("error");
|
|
|
|
return;
|
|
|
|
ValueTask OnSuccess(int i)
|
|
{
|
|
throw new InvalidOperationException();
|
|
}
|
|
|
|
ValueTask OnFailure(Error e)
|
|
{
|
|
value = e;
|
|
called = true;
|
|
return ValueTask.CompletedTask;
|
|
}
|
|
}
|
|
|
|
[Test]
|
|
public async Task I_can_match_non_generic_result_and_it_calls_success_func_for_success()
|
|
{
|
|
var result = Prelude.Success();
|
|
var match = result.Match(
|
|
() => "success",
|
|
_ => throw new InvalidOperationException());
|
|
|
|
await Assert.That(match).IsEqualTo("success");
|
|
}
|
|
|
|
[Test]
|
|
public async Task I_can_match_non_generic_result_and_it_calls_failure_func_for_failure()
|
|
{
|
|
var result = Prelude.Failure("error");
|
|
var match = result.Match(
|
|
() => throw new InvalidOperationException(),
|
|
e => e);
|
|
|
|
await Assert.That(match.Message).IsEqualTo("error");
|
|
}
|
|
|
|
[Test]
|
|
public async Task I_can_switch_non_generic_result_and_it_calls_success_action_for_success()
|
|
{
|
|
var called = false;
|
|
|
|
var result = Prelude.Success();
|
|
result.Switch(OnSuccess, OnFailure);
|
|
|
|
await Assert.That(called).IsTrue();
|
|
|
|
void OnSuccess()
|
|
{
|
|
called = true;
|
|
}
|
|
|
|
void OnFailure(Error e)
|
|
{
|
|
throw new InvalidOperationException();
|
|
}
|
|
}
|
|
|
|
[Test]
|
|
public async Task I_can_switch_non_generic_result_and_it_calls_failure_action_for_failure()
|
|
{
|
|
var called = false;
|
|
var value = default(Error);
|
|
|
|
var result = Prelude.Failure("error");
|
|
result.Switch(OnSuccess, OnFailure);
|
|
|
|
await Assert.That(called).IsTrue();
|
|
await Assert.That(value?.Message).IsEqualTo("error");
|
|
|
|
void OnSuccess()
|
|
{
|
|
throw new InvalidOperationException();
|
|
}
|
|
|
|
void OnFailure(Error e)
|
|
{
|
|
value = e;
|
|
called = true;
|
|
}
|
|
}
|
|
|
|
[Test]
|
|
public async Task I_can_match_async_non_generic_result_and_it_calls_success_func_for_success()
|
|
{
|
|
var result = Prelude.Success();
|
|
var match = await result.MatchAsync(
|
|
() => Task.FromResult("success"),
|
|
_ => throw new InvalidOperationException());
|
|
|
|
await Assert.That(match).IsEqualTo("success");
|
|
}
|
|
|
|
[Test]
|
|
public async Task I_can_switch_async_non_generic_result_and_it_calls_success_action_for_success()
|
|
{
|
|
var called = false;
|
|
|
|
var result = Prelude.Success();
|
|
await result.SwitchAsync(OnSuccess, OnFailure);
|
|
|
|
await Assert.That(called).IsTrue();
|
|
|
|
Task OnSuccess()
|
|
{
|
|
called = true;
|
|
return Task.CompletedTask;
|
|
}
|
|
|
|
Task OnFailure(Error e)
|
|
{
|
|
throw new InvalidOperationException();
|
|
}
|
|
}
|
|
|
|
[Test]
|
|
public async Task I_can_match_async_non_generic_result_and_it_calls_success_func_for_success_ValueTask()
|
|
{
|
|
var result = Prelude.Success();
|
|
var match = await result.MatchAsync(
|
|
() => ValueTask.FromResult("success"),
|
|
_ => throw new InvalidOperationException());
|
|
|
|
await Assert.That(match).IsEqualTo("success");
|
|
}
|
|
|
|
[Test]
|
|
public async Task I_can_switch_async_non_generic_result_and_it_calls_success_action_for_success_ValueTask()
|
|
{
|
|
var called = false;
|
|
|
|
var result = Prelude.Success();
|
|
await result.SwitchAsync(OnSuccess, OnFailure);
|
|
|
|
await Assert.That(called).IsTrue();
|
|
|
|
ValueTask OnSuccess()
|
|
{
|
|
called = true;
|
|
return ValueTask.CompletedTask;
|
|
}
|
|
|
|
ValueTask OnFailure(Error e)
|
|
{
|
|
throw new InvalidOperationException();
|
|
}
|
|
}
|
|
}
|