From 954e88bae5ca12c94e4ce95b8a185c3cc2007d5f Mon Sep 17 00:00:00 2001 From: "lucas.hipolito" Date: Tue, 14 Oct 2025 11:45:21 +0200 Subject: [PATCH] Unit test coverage for If activity --- .../Branching/IfTests.cs | 240 ++++++++++++++++++ 1 file changed, 240 insertions(+) create mode 100644 test/unit/Elsa.Activities.UnitTests/Branching/IfTests.cs diff --git a/test/unit/Elsa.Activities.UnitTests/Branching/IfTests.cs b/test/unit/Elsa.Activities.UnitTests/Branching/IfTests.cs new file mode 100644 index 000000000..81b9f3436 --- /dev/null +++ b/test/unit/Elsa.Activities.UnitTests/Branching/IfTests.cs @@ -0,0 +1,240 @@ +using Elsa.Activities.UnitTests.Helpers; +using Elsa.Extensions; + +namespace Elsa.Activities.UnitTests.Branching; + +public class IfTests +{ + [Fact] + public async Task Should_Evaluate_Condition_And_Set_Result_When_Activity_Runs() + { + // Arrange + var ifActivity = new If(() => true); + + // Act + var context = await ActivityTestHelper.ExecuteActivityAsync(ifActivity); + + // Assert + var resultValue = (bool)context.GetExecutionOutput(_ => ifActivity.Result)!; + Assert.True(resultValue); + } + + [Fact] + public async Task Should_Set_Result_To_True_When_Condition_Is_True() + { + // Arrange + var variable = new Variable("testVar", "initial", "testVar"); + var thenActivity = new SetVariable(variable, new Input("then_executed")); + + var ifActivity = new If(() => true) + { + Then = thenActivity + }; + + // Act + var context = await ActivityTestHelper.ExecuteActivityAsync(ifActivity); + + // Assert + var resultValue = (bool)context.GetExecutionOutput(_ => ifActivity.Result)!; + Assert.True(resultValue); + } + + [Fact] + public async Task Should_Set_Result_To_False_When_Condition_Is_False() + { + // Arrange + var variable = new Variable("testVar", "initial", "testVar"); + var elseActivity = new SetVariable(variable, new Input("else_executed")); + + var ifActivity = new If(() => false) + { + Else = elseActivity + }; + + // Act + var context = await ActivityTestHelper.ExecuteActivityAsync(ifActivity); + + // Assert + var resultValue = (bool)context.GetExecutionOutput(_ => ifActivity.Result)!; + Assert.False(resultValue); + } + + [Fact] + public async Task Should_Set_Result_To_True_When_Condition_Is_True_With_Both_Branches() + { + // Arrange + var thenVariable = new Variable("thenVar", "initial", "thenVar"); + var elseVariable = new Variable("elseVar", "initial", "elseVar"); + + var thenActivity = new SetVariable(thenVariable, new Input("then_executed")); + var elseActivity = new SetVariable(elseVariable, new Input("else_executed")); + + var ifActivity = new If(() => true) + { + Then = thenActivity, + Else = elseActivity + }; + + // Act + var context = await ActivityTestHelper.ExecuteActivityAsync(ifActivity); + + // Assert + var resultValue = (bool)context.GetExecutionOutput(_ => ifActivity.Result)!; + Assert.True(resultValue); + } + + [Fact] + public async Task Should_Set_Result_To_False_When_Condition_Is_False_With_Both_Branches() + { + // Arrange + var thenVariable = new Variable("thenVar", "initial", "thenVar"); + var elseVariable = new Variable("elseVar", "initial", "elseVar"); + + var thenActivity = new SetVariable(thenVariable, new Input("then_executed")); + var elseActivity = new SetVariable(elseVariable, new Input("else_executed")); + + var ifActivity = new If(() => false) + { + Then = thenActivity, + Else = elseActivity + }; + + // Act + var context = await ActivityTestHelper.ExecuteActivityAsync(ifActivity); + + // Assert + var resultValue = (bool)context.GetExecutionOutput(_ => ifActivity.Result)!; + Assert.False(resultValue); + } + + [Fact] + public async Task Should_Set_Result_With_Missing_Else_Branch_When_Condition_Is_True() + { + // Arrange + var variable = new Variable("testVar", "initial", "testVar"); + var thenActivity = new SetVariable(variable, new Input("then_executed")); + + var ifActivity = new If(() => true) + { + Then = thenActivity + // Else is intentionally null + }; + + // Act + var context = await ActivityTestHelper.ExecuteActivityAsync(ifActivity); + + // Assert + var resultValue = (bool)context.GetExecutionOutput(_ => ifActivity.Result)!; + Assert.True(resultValue); + } + + [Fact] + public async Task Should_Set_Result_With_Missing_Else_Branch_When_Condition_Is_False() + { + // Arrange + var variable = new Variable("testVar", "initial", "testVar"); + var thenActivity = new SetVariable(variable, new Input("then_executed")); + + var ifActivity = new If(() => false) + { + Then = thenActivity + // Else is intentionally null + }; + + // Act + var context = await ActivityTestHelper.ExecuteActivityAsync(ifActivity); + + // Assert + var resultValue = (bool)context.GetExecutionOutput(_ => ifActivity.Result)!; + Assert.False(resultValue); + } + + [Fact] + public async Task Should_Set_Result_With_Missing_Then_Branch_When_Condition_Is_True() + { + // Arrange + var variable = new Variable("testVar", "initial", "testVar"); + var elseActivity = new SetVariable(variable, new Input("else_executed")); + + var ifActivity = new If(() => true) + { + // Then is intentionally null + Else = elseActivity + }; + + // Act + var context = await ActivityTestHelper.ExecuteActivityAsync(ifActivity); + + // Assert + var resultValue = (bool)context.GetExecutionOutput(_ => ifActivity.Result)!; + Assert.True(resultValue); + } + + [Fact] + public async Task Should_Set_Result_With_Missing_Then_Branch_When_Condition_Is_False() + { + // Arrange + var variable = new Variable("testVar", "initial", "testVar"); + var elseActivity = new SetVariable(variable, new Input("else_executed")); + + var ifActivity = new If(() => false) + { + // Then is intentionally null + Else = elseActivity + }; + + // Act + var context = await ActivityTestHelper.ExecuteActivityAsync(ifActivity); + + // Assert + var resultValue = (bool)context.GetExecutionOutput(_ => ifActivity.Result)!; + Assert.False(resultValue); + } + + [Fact] + public async Task Should_Not_Throw_When_Both_Branches_Are_Missing_And_Condition_Is_True() + { + // Arrange + var ifActivity = new If(() => true) + { + // Both Then and Else are intentionally null + }; + + // Act & Assert + var exception = await Record.ExceptionAsync(async () => await ActivityTestHelper.ExecuteActivityAsync(ifActivity)); + + Assert.Null(exception); + } + + [Fact] + public async Task Should_Not_Throw_When_Both_Branches_Are_Missing_And_Condition_Is_False() + { + // Arrange + var ifActivity = new If(() => false) + { + // Both Then and Else are intentionally null + }; + + // Act & Assert + var exception = await Record.ExceptionAsync(async () => await ActivityTestHelper.ExecuteActivityAsync(ifActivity)); + + Assert.Null(exception); + } + + [Fact] + public async Task Should_Set_Result_When_Both_Branches_Are_Missing() + { + // Arrange + var ifActivity = new If(() => true) + { + // Both Then and Else are intentionally null + }; + + // Act + var context = await ActivityTestHelper.ExecuteActivityAsync(ifActivity); + + // Assert + var resultValue = (bool)context.GetExecutionOutput(_ => ifActivity.Result)!; + Assert.True(resultValue); + } +}