forked from sebastienros/fluid
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FromStatementTests.cs
125 lines (99 loc) · 3.86 KB
/
FromStatementTests.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
121
122
123
124
125
using System.Collections.Generic;
using System.IO;
using System.Text.Encodings.Web;
using System.Threading.Tasks;
using Fluid.Ast;
using Fluid.Tests.Mocks;
using Fluid.Values;
using Xunit;
namespace Fluid.Tests;
public class FromStatementTests
{
#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 FromStatement_ShouldThrowFileNotFoundException_IfTheFileProviderIsNotPresent()
{
var expression = new LiteralExpression(new StringValue("_Macros.liquid"));
var sw = new StringWriter();
try
{
var fromStatement = new FromStatement(_parser, expression, new List<string> { "foo" });
await fromStatement.WriteToAsync(sw, HtmlEncoder.Default, new TemplateContext());
Assert.True(false);
}
catch (FileNotFoundException)
{
return;
}
}
[Fact]
public async Task FromStatement_ShouldOnlyImportListedMacrosToLocalScope()
{
var expression = new LiteralExpression(new StringValue("_Macros.liquid"));
var sw = new StringWriter();
var fileProvider = new MockFileProvider();
fileProvider.Add("_Macros.liquid", @"
{% macro hello_world() %}
Hello world!
{% endmacro %}
{% macro hello(first, last='Smith') %}
Hello {{first | capitalize}} {{last}}!
{% endmacro %}
");
var options = new TemplateOptions { FileProvider = fileProvider };
var context = new TemplateContext(options);
var fromStatement = new FromStatement(_parser, expression, new List<string>{"hello_world"});
await fromStatement.WriteToAsync(sw, HtmlEncoder.Default, context);
Assert.IsType<FunctionValue>(context.GetValue("hello_world"));
Assert.IsType<NilValue>(context.GetValue("hello"));
}
[Fact]
public async Task FromStatement_ShouldNotRenderAnyOutput()
{
var expression = new LiteralExpression(new StringValue("_Macros.liquid"));
var sw = new StringWriter();
var fileProvider = new MockFileProvider();
fileProvider.Add("_Macros.liquid", @"
{% macro hello_world() %}
Hello world!
{% endmacro %}
{% macro hello(first, last='Smith') %}
Hello {{first | capitalize}} {{last}}!
{% endmacro %}
{{ hello_world() }}
");
var options = new TemplateOptions { FileProvider = fileProvider };
var context = new TemplateContext(options);
var fromStatement = new FromStatement(_parser, expression, new List<string> { "hello_world" });
await fromStatement.WriteToAsync(sw, HtmlEncoder.Default, context);
var result = sw.ToString();
Assert.Equal("", result);
}
[Fact]
public async Task FromStatement_ShouldInvokeImportedMarcos()
{
var expression = new LiteralExpression(new StringValue("_Macros.liquid"));
var sw = new StringWriter();
var fileProvider = new MockFileProvider();
fileProvider.Add("_Macros.liquid", @"
{%- macro hello_world() -%}
Hello world!
{%- endmacro -%}
{%- macro hello(first, last='Doe') -%}
Hello {{first | capitalize}} {{last}}!
{%- endmacro -%}
");
var source = @"
{%- from '_Macros' import hello_world, hello -%}
{{ hello_world() }} {{ hello('John') }}";
_parser.TryParse(source, out var template, out var error);
var options = new TemplateOptions { FileProvider = fileProvider };
var context = new TemplateContext(options);
var result = await template.RenderAsync(context);
Assert.Equal("Hello world! Hello John Doe!", result);
}
}