Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test Updates #35

Merged
merged 1 commit into from
Jul 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ public async Task TestAddTupleToMemory()
workflow.AddTupleToMemory(inputTuple);

// Assert

workflow.Exception.Should().BeNull();
workflow.Memory.Count.Should().Be(4); // Unit is always added, along with input.
workflow.Memory.Values.Should().Contain(false);
Expand Down Expand Up @@ -53,6 +52,73 @@ public async Task TestInvalidAddTupleToMemoryNull()
Assert.Throws<WorkflowException>(() => workflow.AddTupleToMemory((ITuple)null!));
}

[Theory]
public async Task TestAddTupleToMemoryWithDifferentTypes()
{
// Arrange
var input = 1;
var inputTuple = (42, "world", 3.14);

var workflow = new TestWorkflow().Activate(input);

// Act
workflow.AddTupleToMemory(inputTuple);

// Assert
workflow.Exception.Should().BeNull();
workflow.Memory.Count.Should().Be(4); // Unit is always added, along with input.
workflow.Memory.Values.Should().Contain(42);
workflow.Memory.Values.Should().Contain("world");
workflow.Memory.Values.Should().Contain(3.14);
}

[Theory]
public async Task TestAddEmptyTupleToMemory()
{
// Arrange
var input = 1;
var inputTuple = new ValueTuple();

var workflow = new TestWorkflow().Activate(input);

// Act
Assert.Throws<WorkflowException>(() => workflow.AddTupleToMemory(inputTuple));
}

[Theory]
public async Task TestAddTupleToMemoryWithMoreThanSevenElements()
{
// Arrange
var input = 1;
var inputTuple = (1, 2, 3, 4, 5, 6, 7, 8);

var workflow = new TestWorkflow().Activate(input);

// Act
Assert.Throws<WorkflowException>(() => workflow.AddTupleToMemory(inputTuple));
}

[Theory]
public async Task TestAddMultipleTuplesToMemory()
{
// Arrange
var input = 1;
var inputTuple1 = (1, "first");
var inputTuple2 = (2, "second");

var workflow = new TestWorkflow().Activate(input);
workflow.AddTupleToMemory(inputTuple1);

// Act
workflow.AddTupleToMemory(inputTuple2);

// Assert
workflow.Exception.Should().BeNull();
workflow.Memory.Count.Should().Be(3); // Unit is always added, along with input.
workflow.Memory.Values.Should().Contain(2);
workflow.Memory.Values.Should().Contain("second");
}

private class TestWorkflow : Workflow<int, string>
{
protected override async Task<Either<Exception, string>> RunInternal(int input) =>
Expand Down
124 changes: 124 additions & 0 deletions ChainSharp.Tests.Unit/UnitTests/Extensions/FunctionalTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
using ChainSharp.Extensions;
using FluentAssertions;
using LanguageExt;

namespace ChainSharp.Tests.Unit.UnitTests.Extensions;

public class FunctionalTests : TestSetup
{
[Theory]
public async Task TestUnwrapTaskEitherRight()
{
// Arrange
var either = Task.FromResult<Either<Exception, int>>(Prelude.Right<Exception, int>(42));

// Act
var result = await either.Unwrap();

// Assert
result.Should().Be(42);
}

[Theory]
public async Task TestUnwrapTaskEitherLeft()
{
// Arrange
var exception = new InvalidOperationException("Test exception");
var either = Task.FromResult<Either<Exception, int>>(
Prelude.Left<Exception, int>(exception)
);

// Act
Func<Task> act = async () => await either.Unwrap();

// Assert
await act.Should().ThrowAsync<InvalidOperationException>().WithMessage("Test exception");
}

[Theory]
public void TestUnwrapEitherRight()
{
// Arrange
var either = Prelude.Right<Exception, int>(42);

// Act
var result = either.Unwrap();

// Assert
result.Should().Be(42);
}

[Theory]
public void TestUnwrapEitherLeft()
{
// Arrange
var exception = new InvalidOperationException("Test exception");
var either = Prelude.Left<Exception, int>(exception);

// Act
Action act = () => either.Unwrap();

// Assert
act.Should().Throw<InvalidOperationException>().WithMessage("Test exception");
}

[Theory]
public async Task TestUnwrapTaskEitherRightDifferentTypes()
{
// Arrange
var either = Task.FromResult<Either<Exception, string>>(
Prelude.Right<Exception, string>("success")
);

// Act
var result = await either.Unwrap();

// Assert
result.Should().Be("success");
}

[Theory]
public void TestUnwrapEitherRightDifferentTypes()
{
// Arrange
var either = Prelude.Right<Exception, double>(3.14);

// Act
var result = either.Unwrap();

// Assert
result.Should().Be(3.14);
}

[Theory]
public async Task TestUnwrapTaskEitherLeftDifferentTypes()
{
// Arrange
var exception = new ArgumentNullException("param");
var either = Task.FromResult<Either<ArgumentException, bool>>(
Prelude.Left<ArgumentException, bool>(exception)
);

// Act
Func<Task> act = async () => await either.Unwrap();

// Assert
await act.Should()
.ThrowAsync<ArgumentNullException>()
.WithMessage("Value cannot be null. (Parameter 'param')");
}

[Theory]
public void TestUnwrapEitherLeftDifferentTypes()
{
// Arrange
var exception = new ArgumentException("Test exception");
var either = Prelude.Left<Exception, char>(exception);

// Act
Action act = () => either.Unwrap();

// Assert
act.Should().Throw<ArgumentException>().WithMessage("Test exception");
}
}
169 changes: 169 additions & 0 deletions ChainSharp.Tests.Unit/UnitTests/Extensions/IsTupleTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
using System.Runtime.CompilerServices;
using ChainSharp.Extensions;
using FluentAssertions;

namespace ChainSharp.Tests.Unit.UnitTests.Extensions;

public class IsTupleTests : TestSetup
{
[Theory]
public async Task TestBuiltInIsTuple()
{
// Arrange
var builtInTuple = ("hello", false).GetType();

// Act
var result = builtInTuple.IsTuple();

// Assert
result.Should().BeTrue();
}

[Theory]
public async Task TestValueTupleIsTuple()
{
// Arrange
var valueTuple = new ValueTuple<int>().GetType();

// Act
var result = valueTuple.IsTuple();

// Assert
result.Should().BeTrue();
}

[Theory]
public async Task TestITupleIsTuple()
{
// Arrange
var iTuple = ((ITuple)("hello", false)).GetType();

// Act
var result = iTuple.IsTuple();

// Assert
result.Should().BeTrue();
}

[Theory]
public async Task TestInvalidIsTuple()
{
// Arrange
var notATuple = new object().GetType();

// Act
var result = notATuple.IsTuple();

// Assert
result.Should().BeFalse();
}

[Theory]
public async Task TestNestedTupleIsTuple()
{
// Arrange
var nestedTuple = ((1, ("hello", false))).GetType();

// Act
var result = nestedTuple.IsTuple();

// Assert
result.Should().BeTrue();
}

[Theory]
public async Task TestTupleWithMoreThanSevenElementsIsTuple()
{
// Arrange
var longTuple = (1, 2, 3, 4, 5, 6, 7, 8).GetType();

// Act
var result = longTuple.IsTuple();

// Assert
result.Should().BeTrue();
}

[Theory]
public async Task TestTupleWithGenericTypeIsTuple()
{
// Arrange
var genericTuple = (new List<int>(), new Dictionary<string, int>()).GetType();

// Act
var result = genericTuple.IsTuple();

// Assert
result.Should().BeTrue();
}

[Theory]
public async Task TestTupleWithNullElementIsTuple()
{
// Arrange
var tupleWithNull = ((string)null, 42).GetType();

// Act
var result = tupleWithNull.IsTuple();

// Assert
result.Should().BeTrue();
}

[Theory]
public async Task TestClassWithTupleFieldIsNotTuple()
{
// Arrange
var classWithTupleField = typeof(ClassWithTupleField);

// Act
var result = classWithTupleField.IsTuple();

// Assert
result.Should().BeFalse();
}

private class ClassWithTupleField
{
public (int, string) TupleField;
}

[Theory]
public async Task TestTupleInArrayIsTuple()
{
// Arrange
var tupleArray = new (int, string)[] { (1, "a"), (2, "b") }.GetType();

// Act
var result = tupleArray.IsTuple();

// Assert
result.Should().BeFalse();
}

[Theory]
public async Task TestTupleTypeIsTuple()
{
// Arrange
var tupleType = typeof((int, string));

// Act
var result = tupleType.IsTuple();

// Assert
result.Should().BeTrue();
}

[Theory]
public async Task TestTupleInsideGenericTypeIsNotTuple()
{
// Arrange
var listOfTuples = new List<(int, string)>().GetType();

// Act
var result = listOfTuples.IsTuple();

// Assert
result.Should().BeFalse();
}
}
3 changes: 3 additions & 0 deletions ChainSharp.Tests.Unit/UnitTests/Workflow/WorkflowTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
namespace ChainSharp.Tests.Unit.UnitTests.Workflow;

public class WorkflowTests { }
Loading