-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve message when Scriban cannot execute predicate for context menu (
#201) * improve predicate exception message * Add tests * update
- Loading branch information
Showing
5 changed files
with
90 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
src/RepoM.ActionMenu.Interface/YamlModel/Templating/PredicateEvaluationException.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
namespace RepoM.ActionMenu.Interface.YamlModel.Templating; | ||
|
||
using System; | ||
|
||
public sealed class PredicateEvaluationException : Exception | ||
{ | ||
public PredicateEvaluationException(string predicate, Exception innerException) | ||
: base(CreateMessage(predicate, innerException), innerException) | ||
{ | ||
PredicateText = predicate; | ||
} | ||
|
||
public string PredicateText { get; } | ||
|
||
private static string CreateMessage(string predicateText, Exception exception) | ||
{ | ||
return $"Could not evaluate predicate '{predicateText}' because {exception.Message}"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
tests/RepoM.ActionMenu.Core.Tests/Yaml/Model/ScribanPredicateTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
namespace RepoM.ActionMenu.Core.Tests.Yaml.Model; | ||
|
||
using System; | ||
using System.Threading.Tasks; | ||
using FluentAssertions; | ||
using RepoM.ActionMenu.Core.Misc; | ||
using RepoM.ActionMenu.Core.Yaml.Model.Templating; | ||
using RepoM.ActionMenu.Interface.ActionMenuFactory; | ||
using RepoM.ActionMenu.Interface.YamlModel.Templating; | ||
using Scriban; | ||
using Xunit; | ||
|
||
public class ScribanPredicateTests | ||
{ | ||
[Fact] | ||
public async Task EvaluateAsync_ShouldThrowPredicateEvaluationException_WhenPredicateIsWrong() | ||
{ | ||
// arrange | ||
var sut = new ScribanPredicate | ||
{ | ||
Value = "file.exists x", | ||
}; | ||
var realTemplateParser = new FixedTemplateParser(); | ||
((ICreateTemplate)sut).CreateTemplate(realTemplateParser); | ||
ITemplateEvaluator templateEvaluator = new FakeTemplateContext(realTemplateParser); | ||
|
||
// act | ||
Func<Task<bool>> act = async () => await sut.EvaluateAsync(templateEvaluator); | ||
|
||
// assert | ||
(await act.Should().ThrowAsync<PredicateEvaluationException>()) | ||
.WithMessage("Could not evaluate predicate 'file.exists x' because <input>(1,6) : error : Cannot get the member file.exists for a null object.") | ||
.And.PredicateText.Should().Be("file.exists x"); | ||
|
||
} | ||
} | ||
|
||
file class FakeTemplateContext : TemplateContext, ITemplateEvaluator | ||
{ | ||
private readonly ITemplateParser _templateParser; | ||
|
||
public FakeTemplateContext(ITemplateParser templateParser) | ||
{ | ||
_templateParser = templateParser; | ||
} | ||
public Task<string> RenderStringAsync(string text) | ||
{ | ||
throw new NotImplementedException(); | ||
} | ||
|
||
public async Task<object> EvaluateAsync(string text) | ||
{ | ||
Template template = _templateParser.ParseScriptOnly(text); | ||
return await template.EvaluateAsync(this).ConfigureAwait(false); | ||
} | ||
} |