From aedcb3a349f8b3f10e98ebb1fc9ce7f93100c5b7 Mon Sep 17 00:00:00 2001 From: "lucas.hipolito" Date: Mon, 27 Oct 2025 10:23:04 +0100 Subject: [PATCH] Refactoring for improved testing - If activity --- .../ActivityTestFixture.cs | 2 +- .../Branching/IfTests.cs | 124 ++++++++++++++---- 2 files changed, 97 insertions(+), 29 deletions(-) diff --git a/src/common/Elsa.Testing.Shared/ActivityTestFixture.cs b/src/common/Elsa.Testing.Shared/ActivityTestFixture.cs index 44a525cb9..0b077d1c9 100644 --- a/src/common/Elsa.Testing.Shared/ActivityTestFixture.cs +++ b/src/common/Elsa.Testing.Shared/ActivityTestFixture.cs @@ -58,7 +58,7 @@ public class ActivityTestFixture configure(Services); return this; } - + /// /// Configures the activity execution context before execution. /// Multiple calls to this method will chain the configuration actions together. diff --git a/test/unit/Elsa.Activities.UnitTests/Branching/IfTests.cs b/test/unit/Elsa.Activities.UnitTests/Branching/IfTests.cs index 62889f74a..422f0fc1b 100644 --- a/test/unit/Elsa.Activities.UnitTests/Branching/IfTests.cs +++ b/test/unit/Elsa.Activities.UnitTests/Branching/IfTests.cs @@ -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("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("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 ExecuteAsync(IActivity activity) { return new ActivityTestFixture(activity).ExecuteAsync();