forked from sebastienros/fluid
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FunctionTests.cs
120 lines (102 loc) · 4.29 KB
/
FunctionTests.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
using Fluid.Values;
using System;
using System.Threading.Tasks;
using Xunit;
namespace Fluid.Tests
{
public class FunctionTests
{
#if COMPILED
private static FluidParser _parser = new FluidParser(new FluidParserOptions { AllowFunctions = true }).Compile();
#else
private static FluidParser _parser = new FluidParser(new FluidParserOptions { AllowFunctions = true });
#endif
[Fact]
public async Task FunctionCallsShouldDefaultToNil()
{
var source = @"
{{- a() -}}
";
_parser.TryParse(source, out var template, out var error);
var context = new TemplateContext();
var result = await template.RenderAsync(context);
Assert.Equal("", result);
}
[Fact]
public async Task FunctionCallsAreInvoked()
{
var source = @"{{ a() | append: b()}}";
_parser.TryParse(source, out var template, out var error);
Assert.True(template != null, error);
var context = new TemplateContext();
context.SetValue("a", new FunctionValue((args, c) => new StringValue("hello")));
context.SetValue("b", new FunctionValue((args, c) => new ValueTask<FluidValue>(new StringValue("world"))));
// Use a loop to exercise the arguments cache
for (var i = 0; i < 10; i++)
{
var result = await template.RenderAsync(context);
Assert.Equal("helloworld", result);
}
}
[Fact]
public async Task FunctionCallsUseArguments()
{
var source = @"{{ a('x', 2) }}";
_parser.TryParse(source, out var template, out var error);
Assert.True(template != null, error);
var context = new TemplateContext();
context.SetValue("a", new FunctionValue((args, c) => new StringValue(new String(args.At(0).ToStringValue()[0], (int) args.At(1).ToNumberValue()))));
// Use a loop to exercise the arguments cache
for (var i = 0; i < 10; i++)
{
var result = await template.RenderAsync(context);
Assert.Equal("xx", result);
}
}
[Fact]
public async Task FunctionCallsUseNamedArguments()
{
var source = @"{{ a(c = 'x', r = 2) }}";
_parser.TryParse(source, out var template, out var error);
Assert.True(template != null, error);
var context = new TemplateContext();
context.SetValue("a", new FunctionValue((args, c) => new StringValue(new String(args["c"].ToStringValue()[0], (int)args["r"].ToNumberValue()))));
// Use a loop to exercise the arguments cache
for (var i = 0; i < 10; i++)
{
var result = await template.RenderAsync(context);
Assert.Equal("xx", result);
}
}
[Fact]
public async Task FunctionCallsRecursively()
{
var source = @"{{ a(b(), r = 2) }}";
_parser.TryParse(source, out var template, out var error);
Assert.True(template != null, error);
var context = new TemplateContext();
context.SetValue("a", new FunctionValue((args, c) => new StringValue(new String(args.At(0).ToStringValue()[0], (int)args["r"].ToNumberValue()))));
context.SetValue("b", new FunctionValue((args, c) => new StringValue("hello")));
// Use a loop to exercise the arguments cache
for (var i = 0; i < 10; i++)
{
var result = await template.RenderAsync(context);
Assert.Equal("hh", result);
}
}
[Fact]
public async Task ShouldDefineMacro()
{
var source = @"
{%- macro hello(first, last='Smith') -%}
Hello {{first | capitalize }} {{last}}
{%- endmacro -%}
{{- hello('mike') }} {{ hello(last= 'ros', first='sebastien') -}}
";
_parser.TryParse(source, out var template, out var error);
var context = new TemplateContext();
var result = await template.RenderAsync(context);
Assert.Equal("Hello Mike Smith Hello Sebastien ros", result);
}
}
}