feat: initial project commit
All checks were successful
default / default (8.0) (push) Successful in 1m7s

This commit is contained in:
Louis Seubert 2024-04-14 17:42:13 +02:00
commit 30ef7bd477
Signed by: louis9902
GPG key ID: 4B9DB28F826553BD
40 changed files with 3752 additions and 0 deletions

View file

@ -0,0 +1,119 @@
namespace Geekeey.Extensions.Result.Tests;
[TestFixture]
internal sealed class PreludeTests
{
[Test]
public void Try_ReturnsSuccess_WithoutThrowing()
{
var result = Try(() => 2);
Assert.Multiple(() =>
{
Assert.That(result.IsSuccess, Is.True);
Assert.That(result.Value, Is.EqualTo(2));
});
}
[Test]
public void Try_ReturnsFailure_WithThrowing()
{
var result = Try<int>(() => throw new CustomTestException());
Assert.Multiple(() =>
{
Assert.That(result.IsSuccess, Is.False);
Assert.That(result.Error, Is.InstanceOf<ExceptionError>());
var instance = result.Error as ExceptionError;
Assert.That(instance?.Exception, Is.InstanceOf<CustomTestException>());
});
}
[Test]
public async Task TryAsync_ReturnsSuccess_WithoutThrowing_Task()
{
var result = await TryAsync(() => Task.FromResult(2));
Assert.Multiple(() =>
{
Assert.That(result.IsSuccess, Is.True);
Assert.That(result.Value, Is.EqualTo(2));
});
}
[Test]
public async Task TryAsync_ReturnsFailure_WithThrowing_Task()
{
var result = await TryAsync(Task<int> () => throw new CustomTestException());
Assert.Multiple(() =>
{
Assert.That(result.IsSuccess, Is.False);
Assert.That(result.Error, Is.InstanceOf<ExceptionError>());
var instance = result.Error as ExceptionError;
Assert.That(instance?.Exception, Is.InstanceOf<CustomTestException>());
});
}
[Test]
public async Task TryAsync_ReturnsFailure_WithAwaitThrowing_Task()
{
var result = await TryAsync(async Task<int> () =>
{
await Task.CompletedTask;
throw new CustomTestException();
});
Assert.Multiple(() =>
{
Assert.That(result.IsSuccess, Is.False);
Assert.That(result.Error, Is.InstanceOf<ExceptionError>());
var instance = result.Error as ExceptionError;
Assert.That(instance?.Exception, Is.InstanceOf<CustomTestException>());
});
}
[Test]
public async Task TryAsync_ReturnsSuccess_WithoutThrowing_ValueTask()
{
var result = await TryAsync(() => ValueTask.FromResult(2));
Assert.Multiple(() =>
{
Assert.That(result.IsSuccess, Is.True);
Assert.That(result.Value, Is.EqualTo(2));
});
}
[Test]
public async Task TryAsync_ReturnsFailure_WithThrowing_ValueTask()
{
var result = await TryAsync(ValueTask<int> () => throw new CustomTestException());
Assert.Multiple(() =>
{
Assert.That(result.IsSuccess, Is.False);
Assert.That(result.Error, Is.InstanceOf<ExceptionError>());
var instance = result.Error as ExceptionError;
Assert.That(instance?.Exception, Is.InstanceOf<CustomTestException>());
});
}
[Test]
public async Task TryAsync_ReturnsFailure_WithAwaitThrowing_ValueTask()
{
var result = await TryAsync(async ValueTask<int> () =>
{
await Task.CompletedTask;
throw new CustomTestException();
});
Assert.Multiple(() =>
{
Assert.That(result.IsSuccess, Is.False);
Assert.That(result.Error, Is.InstanceOf<ExceptionError>());
var instance = result.Error as ExceptionError;
Assert.That(instance?.Exception, Is.InstanceOf<CustomTestException>());
});
}
}