forked from sebastienros/fluid
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BinaryExpressionTests.cs
257 lines (230 loc) · 9.25 KB
/
BinaryExpressionTests.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
using Fluid.Values;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Threading.Tasks;
using Xunit;
namespace Fluid.Tests
{
public class BinaryExpressionTests
{
#if COMPILED
private static FluidParser _parser = new FluidParser().Compile();
#else
private static FluidParser _parser = new FluidParser();
#endif
private async Task CheckAsync(string source, string expected, Action<TemplateContext> init = null)
{
_parser.TryParse("{% if " + source + " %}true{% else %}false{% endif %}", out var template, out var messages);
var context = new TemplateContext();
init?.Invoke(context);
var result = await template.RenderAsync(context);
Assert.Equal(expected, result);
}
[Theory]
[InlineData("1 == 1", "true")]
[InlineData("1 == 2", "false")]
public Task EqualBinaryExpressionIsEvaluated(string source, string expected)
{
return CheckAsync(source, expected);
}
[Theory]
[InlineData("1 != 1", "false")]
[InlineData("1 != 2", "true")]
[InlineData("1 <> 1", "false")]
[InlineData("1 <> 2", "true")]
public Task NotEqualBinaryExpressionIsEvaluated(string source, string expected)
{
return CheckAsync(source, expected);
}
[Theory]
[InlineData("true and true", "true")]
[InlineData("true and false", "false")]
[InlineData("false and false", "false")]
public Task AndBinaryExpressionIsEvaluated(string source, string expected)
{
return CheckAsync(source, expected);
}
[Theory]
[InlineData("true or true", "true")]
[InlineData("true or false", "true")]
[InlineData("false or false", "false")]
public Task OrBinaryExpressionIsEvaluated(string source, string expected)
{
return CheckAsync(source, expected);
}
[Theory]
[InlineData("'abc' contains 'a'", "true")]
[InlineData("'abc' contains 'd'", "false")]
[InlineData("x contains 'a'", "true")]
[InlineData("x contains 'x'", "false")]
[InlineData("y contains 'a'", "true")]
[InlineData("y contains 'x'", "false")]
public Task ContainsBinaryExpressionIsEvaluated(string source, string expected)
{
return CheckAsync(source, expected, context =>
{
context.SetValue("x", new[] { "a", "b", "c" });
context.SetValue("y", new List<string> { "a", "b", "c" });
});
}
[Theory]
[InlineData("'Strasse' contains 'ß'", "false")]
[InlineData("'ss' == 'ß'", "false")]
public Task StringComparisonsShouldBeOrdinal(string source, string expected)
{
return CheckAsync(source, expected, context =>
{
// This is the default, but forcing it for clarity of the test
// With invariant culture, Strasse and Straße are equal, and Liquid
// should not assume they are
context.CultureInfo = CultureInfo.InvariantCulture;
});
}
[Theory]
[InlineData("10 < 10", "false")]
[InlineData("10 <= 10", "true")]
public Task LowerThanBinaryExpressionIsEvaluated(string source, string expected)
{
return CheckAsync(source, expected);
}
[Theory]
[InlineData("10 > 10", "false")]
[InlineData("10 >= 10", "true")]
public Task GreaterThanBinaryExpressionIsEvaluated(string source, string expected)
{
return CheckAsync(source, expected);
}
[Theory]
[InlineData("'abc' startswith 'bc'", "false")]
[InlineData("'abc' startswith 'ab'", "true")]
[InlineData("x startswith 'b'", "false")]
[InlineData("x startswith 'a'", "true")]
[InlineData("y startswith 2", "false")]
[InlineData("y startswith 1", "true")]
[InlineData("z startswith 'a'", "false")]
public Task StartsWithBinaryExpressionIsEvaluated(string source, string expected)
{
return CheckAsync(source, expected, context =>
{
context.SetValue("x", new[] { "a", "b", "c" });
context.SetValue("y", new[] { 1, 2, 3 });
context.SetValue("z", new string[0]);
});
}
[Theory]
[InlineData("'abc' endswith 'ab'", "false")]
[InlineData("'abc' endswith 'bc'", "true")]
[InlineData("x endswith 'b'", "false")]
[InlineData("x endswith 'c'", "true")]
[InlineData("y endswith 2", "false")]
[InlineData("y endswith 3", "true")]
[InlineData("z endswith 'a'", "false")]
public Task EndsWithBinaryExpressionIsEvaluated(string source, string expected)
{
return CheckAsync(source, expected, context =>
{
context.SetValue("x", new[] { "a", "b", "c" });
context.SetValue("y", new[] { 1, 2, 3 });
context.SetValue("z", new string[0]);
});
}
[Theory]
[InlineData("'' == empty", "true")]
[InlineData("'a' == empty", "false")]
[InlineData("x == empty", "true")]
[InlineData("y == empty", "false")]
public Task EmptyValue(string source, string expected)
{
return CheckAsync(source, expected, context =>
{
context.SetValue("x", new string[0]);
context.SetValue("y", new List<string> { "foo" });
});
}
[Theory]
[InlineData("''", "true")]
[InlineData("'a'", "true")]
[InlineData("blank", "true")]
[InlineData("empty", "true")]
[InlineData("0", "true")]
[InlineData("1", "true")]
[InlineData("abc", "false")]
[InlineData("false", "false")]
public Task TruthyFalsy(string source, string expected)
{
return CheckAsync(source, expected);
}
[Theory]
[InlineData("true == true", "true", null)]
[InlineData("true != true", "false", null)]
[InlineData("0 > 0", "false", null)]
[InlineData("1 > 0", "true", null)]
[InlineData("0 < 1", "true", null)]
[InlineData("0 <= 0", "true", null)]
[InlineData("null <= 0", "false", null)]
[InlineData("0 <= null", "false", null)]
[InlineData("0 >= 0", "true", null)]
[InlineData("'test' == 'test'", "true", null)]
[InlineData("'test' != 'test'", "false", null)]
[InlineData("var == 'hello there!'", "true", "hello there!")]
[InlineData("'hello there!' == var", "true", "hello there!")]
[InlineData("'hello there!' == true", "false", "hello there!")]
[InlineData("'hello there!' == false", "false", "hello there!")]
[InlineData("null <= null", "true", null)]
[InlineData("null >= null", "true", null)]
[InlineData("null < null", "false", null)]
[InlineData("null > null", "false", null)]
[InlineData("null == null", "true", null)]
[InlineData("null != null", "false", null)]
public Task StringLiteralTrue(string source, string expected, object value)
{
// https://github.com/Shopify/liquid/blob/master/test/integration/tags/statements_test.rb
return CheckAsync(source, expected, t => t.SetValue("var", value));
}
[Theory]
[InlineData("true or false and false", "true")]
[InlineData("true and false and false or true", "false")]
[InlineData("true and true and false == false", "true")]
[InlineData("true and true and true == false", "false")]
[InlineData("false or false or true == true", "true")]
public Task OperatorsShouldBeEvaluatedFromRightToLeft(string source, string expected)
{
// https://shopify.github.io/liquid/basics/operators/
return CheckAsync(source, expected);
}
[Theory]
[InlineData("1 == 1 or 1 == 2 and 1 == 2", "true")]
[InlineData("1 == 1 and 1 == 2 and 1 == 2 or 1 == 1", "false")]
public Task OperatorsHavePriority(string source, string expected)
{
return CheckAsync(source, expected);
}
[Fact]
public void DictionaryValuesShouldBeEqual()
{
var actual = new DictionaryValue(new FluidValueDictionaryFluidIndexable(new Dictionary<string, FluidValue>
{
{ "stringProperty", StringValue.Create("testValue") }
}));
var expected = new DictionaryValue(new FluidValueDictionaryFluidIndexable(new Dictionary<string, FluidValue>
{
{ "stringProperty", StringValue.Create("testValue") }
}));
Assert.True(actual.Equals(expected));
}
[Fact]
public void ArrayValuesShouldBeEqual()
{
var actual = new ArrayValue(new FluidValue[]
{
StringValue.Create("testValue")
});
var expected = new ArrayValue(new FluidValue[]
{
StringValue.Create("testValue")
});
Assert.True(actual.Equals(expected));
}
}
}