Skip to content

Commit

Permalink
(#255) fixed ElseIf when Predicate.RequiresDocument is false
Browse files Browse the repository at this point in the history
  • Loading branch information
nils-a authored and daveaglick committed Dec 20, 2022
1 parent ca7a49c commit f6b50b3
Show file tree
Hide file tree
Showing 2 changed files with 121 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/core/Statiq.Core/Modules/Control/ExecuteIf.cs
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ protected override async Task<IEnumerable<IDocument>> ExecuteContextAsync(IExecu
}
}
}
else
else if (!hasExecuted)
{
if (await condition.Predicate.GetValueAsync(null, context))
{
Expand Down
120 changes: 120 additions & 0 deletions tests/core/Statiq.Core.Tests/Modules/Control/ExecuteIfFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand Down

0 comments on commit f6b50b3

Please sign in to comment.