Refactoring for improved testing - If activity

This commit is contained in:
lucas.hipolito 2025-10-27 10:23:04 +01:00
parent 47905536b4
commit aedcb3a349
2 changed files with 97 additions and 29 deletions

View file

@ -58,7 +58,7 @@ public class ActivityTestFixture
configure(Services);
return this;
}
/// <summary>
/// Configures the activity execution context before execution.
/// Multiple calls to this method will chain the configuration actions together.

View file

@ -22,44 +22,78 @@ public class IfTests
Assert.Equal(conditionValue, resultValue);
}
[Theory]
[InlineData(true, true, false)] // condition true, has then branch, no else branch
[InlineData(false, false, true)] // condition false, no then branch, has else branch
public async Task Should_Set_Result_Correctly_With_Branch_Configuration(bool conditionValue, bool hasThenBranch, bool hasElseBranch)
[Fact]
public async Task Should_Schedule_Then_Branch_When_Condition_Is_True_And_Then_Branch_Exists()
{
// Arrange
var ifActivity = new If(() => conditionValue);
if (hasThenBranch)
{
// Using a simple WriteLine activity to avoid variable complexity
ifActivity.Then = new WriteLine("then executed");
}
if (hasElseBranch)
{
ifActivity.Else = new WriteLine("else executed");
}
var ifActivity = new If(() => true);
var thenActivity = new WriteLine("then executed");
ifActivity.Then = thenActivity;
// Act
var context = await ExecuteAsync(ifActivity);
// Assert
var resultValue = (bool)context.GetActivityOutput(() => ifActivity.Result)!;
Assert.Equal(conditionValue, resultValue);
Assert.True(resultValue);
Assert.True(context.HasScheduledActivity(thenActivity), "Then branch should be scheduled when condition is true");
}
[Theory]
[InlineData(true)]
[InlineData(false)]
public async Task Should_Not_Throw_When_No_Branches_Are_Present(bool conditionValue)
[Fact]
public async Task Should_Schedule_Else_Branch_When_Condition_Is_False_And_Else_Branch_Exists()
{
// Arrange
var ifActivity = new If(() => conditionValue);
var ifActivity = new If(() => false);
var elseActivity = new WriteLine("else executed");
ifActivity.Else = elseActivity;
// Act & Assert
var exception = await Record.ExceptionAsync(() => ExecuteAsync(ifActivity));
Assert.Null(exception);
// Act
var context = await ExecuteAsync(ifActivity);
// Assert
var resultValue = (bool)context.GetActivityOutput(() => ifActivity.Result)!;
Assert.False(resultValue);
Assert.True(context.HasScheduledActivity(elseActivity), "Else branch should be scheduled when condition is false");
}
[Fact]
public async Task Should_Schedule_Only_Then_Branch_When_Condition_Is_True_And_Both_Branches_Exist()
{
// Arrange
var ifActivity = new If(() => true);
var thenActivity = new WriteLine("then executed");
var elseActivity = new WriteLine("else executed");
ifActivity.Then = thenActivity;
ifActivity.Else = elseActivity;
// Act
var context = await ExecuteAsync(ifActivity);
// Assert
var resultValue = (bool)context.GetActivityOutput(() => ifActivity.Result)!;
Assert.True(resultValue);
Assert.True(context.HasScheduledActivity(thenActivity), "Then branch should be scheduled when condition is true");
Assert.False(context.HasScheduledActivity(elseActivity), "Else branch should not be scheduled when condition is true");
}
[Fact]
public async Task Should_Schedule_Only_Else_Branch_When_Condition_Is_False_And_Both_Branches_Exist()
{
// Arrange
var ifActivity = new If(() => false);
var thenActivity = new WriteLine("then executed");
var elseActivity = new WriteLine("else executed");
ifActivity.Then = thenActivity;
ifActivity.Else = elseActivity;
// Act
var context = await ExecuteAsync(ifActivity);
// Assert
var resultValue = (bool)context.GetActivityOutput(() => ifActivity.Result)!;
Assert.False(resultValue);
Assert.True(context.HasScheduledActivity(elseActivity), "Else branch should be scheduled when condition is false");
Assert.False(context.HasScheduledActivity(thenActivity), "Then branch should not be scheduled when condition is false");
}
[Theory]
@ -70,7 +104,7 @@ public class IfTests
// Arrange
var ifActivity = new If(() => conditionValue)
{
Then = new WriteLine(new Input<string>("then branch"))
Then = new WriteLine("then branch")
};
// Act
@ -89,7 +123,7 @@ public class IfTests
// Arrange
var ifActivity = new If(() => conditionValue)
{
Else = new WriteLine(new Input<string>("else branch"))
Else = new WriteLine("else branch")
};
// Act
@ -99,7 +133,41 @@ public class IfTests
var resultValue = (bool)context.GetActivityOutput(() => ifActivity.Result)!;
Assert.Equal(conditionValue, resultValue);
}
[Fact]
public async Task Should_Not_Schedule_Then_Branch_When_Condition_Is_False_And_Only_Then_Branch_Exists()
{
// Arrange
var ifActivity = new If(() => false);
var thenActivity = new WriteLine("then executed");
ifActivity.Then = thenActivity;
// Act
var context = await ExecuteAsync(ifActivity);
// Assert
var resultValue = (bool)context.GetActivityOutput(() => ifActivity.Result)!;
Assert.False(resultValue);
Assert.False(context.HasScheduledActivity(thenActivity), "Then branch should not be scheduled when condition is false");
}
[Fact]
public async Task Should_Not_Schedule_Else_Branch_When_Condition_Is_True_And_Only_Else_Branch_Exists()
{
// Arrange
var ifActivity = new If(() => true);
var elseActivity = new WriteLine("else executed");
ifActivity.Else = elseActivity;
// Act
var context = await ExecuteAsync(ifActivity);
// Assert
var resultValue = (bool)context.GetActivityOutput(() => ifActivity.Result)!;
Assert.True(resultValue);
Assert.False(context.HasScheduledActivity(elseActivity), "Else branch should not be scheduled when condition is true");
}
private static Task<ActivityExecutionContext> ExecuteAsync(IActivity activity)
{
return new ActivityTestFixture(activity).ExecuteAsync();