From 73347ec17d56928973b3ce0028e1612ed938e6d1 Mon Sep 17 00:00:00 2001 From: Nils Andresen Date: Mon, 19 Dec 2022 22:59:48 +0100 Subject: [PATCH] (#225) fixed ElseIf when Predicate.RequiresDocument is false --- .../Statiq.Core/Modules/Control/ExecuteIf.cs | 2 +- .../Modules/Control/ExecuteIfFixture.cs | 120 ++++++++++++++++++ 2 files changed, 121 insertions(+), 1 deletion(-) diff --git a/src/core/Statiq.Core/Modules/Control/ExecuteIf.cs b/src/core/Statiq.Core/Modules/Control/ExecuteIf.cs index c22394371..51ee22222 100644 --- a/src/core/Statiq.Core/Modules/Control/ExecuteIf.cs +++ b/src/core/Statiq.Core/Modules/Control/ExecuteIf.cs @@ -239,7 +239,7 @@ protected override async Task> ExecuteContextAsync(IExecu } } } - else + else if (!hasExecuted) { if (await condition.Predicate.GetValueAsync(null, context)) { diff --git a/tests/core/Statiq.Core.Tests/Modules/Control/ExecuteIfFixture.cs b/tests/core/Statiq.Core.Tests/Modules/Control/ExecuteIfFixture.cs index a2a3d1992..f3f901998 100644 --- a/tests/core/Statiq.Core.Tests/Modules/Control/ExecuteIfFixture.cs +++ b/tests/core/Statiq.Core.Tests/Modules/Control/ExecuteIfFixture.cs @@ -252,6 +252,126 @@ public async Task FalseIfWithContextResultsInCorrectCounts() c.OutputCount.ShouldBe(12); } + [Test] + public async Task TrueIfWithFollowingElseIfWithContextResultsInCorrectCounts() + { + // Given + CountModule a = new CountModule("A") + { + AdditionalOutputs = 2, + EnsureInputDocument = true + }; + CountModule b = new CountModule("B") + { + AdditionalOutputs = 2 + }; + CountModule c = new CountModule("C") + { + AdditionalOutputs = 3 + }; + + // When + await ExecuteAsync( + a, + new ExecuteIf(Config.FromContext(_ => true), b) + .ElseIf(Config.FromContext(_ => true), c)); + + // Then + a.ExecuteCount.ShouldBe(1); + b.ExecuteCount.ShouldBe(1); + c.ExecuteCount.ShouldBe(0); + } + + [Test] + public async Task FalseIfWithFollowingElseIfWithContextResultsInCorrectCounts() + { + // Given + CountModule a = new CountModule("A") + { + AdditionalOutputs = 2, + EnsureInputDocument = true + }; + CountModule b = new CountModule("B") + { + AdditionalOutputs = 2 + }; + CountModule c = new CountModule("C") + { + AdditionalOutputs = 3 + }; + + // When + await ExecuteAsync( + a, + new ExecuteIf(Config.FromContext(_ => false), b) + .ElseIf(Config.FromContext(_ => true), c)); + + // Then + a.ExecuteCount.ShouldBe(1); + b.ExecuteCount.ShouldBe(0); + c.ExecuteCount.ShouldBe(1); + } + + [Test] + public async Task TrueIfWithFollowingElseWithContextResultsInCorrectCounts() + { + // Given + CountModule a = new CountModule("A") + { + AdditionalOutputs = 2, + EnsureInputDocument = true + }; + CountModule b = new CountModule("B") + { + AdditionalOutputs = 2 + }; + CountModule c = new CountModule("C") + { + AdditionalOutputs = 3 + }; + + // When + await ExecuteAsync( + a, + new ExecuteIf(Config.FromContext(_ => true), b) + .Else(c)); + + // Then + a.ExecuteCount.ShouldBe(1); + b.ExecuteCount.ShouldBe(1); + c.ExecuteCount.ShouldBe(0); + } + + [Test] + public async Task FalseIfWithFollowingElseWithContextResultsInCorrectCounts() + { + // Given + CountModule a = new CountModule("A") + { + AdditionalOutputs = 2, + EnsureInputDocument = true + }; + CountModule b = new CountModule("B") + { + AdditionalOutputs = 2 + }; + CountModule c = new CountModule("C") + { + AdditionalOutputs = 3 + }; + + // When + await ExecuteAsync( + a, + new ExecuteIf(Config.FromContext(_ => false), b) + .Else(c)); + + // Then + a.ExecuteCount.ShouldBe(1); + b.ExecuteCount.ShouldBe(0); + c.ExecuteCount.ShouldBe(1); + } + [Test] public async Task UnmatchedDocumentsAreAddedToResults() {