-
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.
- Loading branch information
Showing
1 changed file
with
53 additions
and
0 deletions.
There are no files selected for viewing
53 changes: 53 additions & 0 deletions
53
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,53 @@ | ||
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."); | ||
} | ||
} | ||
|
||
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); | ||
} | ||
} |