From a8cf10c7e562023a0e5e415e4be1d7c8cdf770ae Mon Sep 17 00:00:00 2001 From: Sipke Schoorstra Date: Wed, 20 Apr 2022 14:14:32 +0200 Subject: [PATCH] Update BreakWorkflowTests.cs --- .../Workflows/BreakWorkflowTests.cs | 88 ++++++++++++++++++- 1 file changed, 86 insertions(+), 2 deletions(-) diff --git a/test/Elsa.IntegrationTests/Workflows/BreakWorkflowTests.cs b/test/Elsa.IntegrationTests/Workflows/BreakWorkflowTests.cs index b3aea4f0b..401af3d2b 100644 --- a/test/Elsa.IntegrationTests/Workflows/BreakWorkflowTests.cs +++ b/test/Elsa.IntegrationTests/Workflows/BreakWorkflowTests.cs @@ -32,7 +32,7 @@ public class BreakWorkflowTests var lines = _capturingTextWriter.Lines.ToList(); Assert.Equal(new[] { "Start", "C#", "Test", "End" }, lines); } - + [Fact(DisplayName = "Break exits out of immediate ForEach only")] public async Task Test2() { @@ -42,6 +42,24 @@ public class BreakWorkflowTests Assert.Equal(new[] { "C#", "Classes", "Rust", "Classes", "Go", "Classes" }, lines); } + [Fact(DisplayName = "Break exits out of For")] + public async Task Test3() + { + var workflow = new WorkflowDefinitionBuilder().BuildWorkflow(new BreakForWorkflow()); + await _workflowRunner.RunAsync(workflow); + var lines = _capturingTextWriter.Lines.ToList(); + Assert.Equal(new[] { "Start", "0", "1", "End" }, lines); + } + + [Fact(DisplayName = "Break exits out of While")] + public async Task Test4() + { + var workflow = new WorkflowDefinitionBuilder().BuildWorkflow(new BreakWhileWorkflow()); + await _workflowRunner.RunAsync(workflow); + var lines = _capturingTextWriter.Lines.ToList(); + Assert.Equal(new[] { "Start", "1", "2", "End" }, lines); + } + private class BreakForEachWorkflow : IWorkflow { public void Build(IWorkflowDefinitionBuilder workflow) @@ -107,11 +125,77 @@ public class BreakWorkflowTests }, new WriteLine(currentInnerItem) } - } + } } } } }); } } + + private class BreakForWorkflow : IWorkflow + { + public void Build(IWorkflowDefinitionBuilder workflow) + { + var currentValue = new Variable(); + + workflow.WithRoot(new Sequence + { + Activities = + { + new WriteLine("Start"), + new For(0, 3) + { + CurrentValue = currentValue, + Body = new Sequence + { + Activities = + { + new If(context => currentValue.Get(context) == 2) + { + Then = new Break() + }, + new WriteLine(context => currentValue.Get(context).ToString()), + } + } + }, + new WriteLine("End"), + } + }); + } + } + + private class BreakWhileWorkflow : IWorkflow + { + public void Build(IWorkflowDefinitionBuilder workflow) + { + var currentValue = new Variable(0); + + workflow.WithRoot(new Sequence + { + Variables = { currentValue }, + Activities = + { + new WriteLine("Start"), + While.True(new Sequence + { + Activities = + { + new SetVariable + { + Variable = currentValue, + Value = new Input(context => currentValue.Get(context) + 1) + }, + new If(context => currentValue.Get(context) == 3) + { + Then = new Break() + }, + new WriteLine(context => currentValue.Get(context).ToString()), + } + }), + new WriteLine("End"), + } + }); + } + } } \ No newline at end of file