diff --git a/src/core/Statiq.Core/Modules/Control/ExecuteIf.cs b/src/core/Statiq.Core/Modules/Control/ExecuteIf.cs index c2239437..51ee2222 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 a2a3d199..f3f90199 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() {