Skip to content

Commit 9e00d85

Browse files
author
Mike Bridge
committed
LiquidTemplate.Create includes template result even when error
1 parent 1194bdc commit 9e00d85

File tree

5 files changed

+86
-20
lines changed

5 files changed

+86
-20
lines changed

Liquid.NET.Tests/Examples/ExampleTests.cs

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
using Liquid.NET.Constants;
1+
using System;
2+
using System.Linq;
3+
using Liquid.NET.Constants;
24
using NUnit.Framework;
35

46
namespace Liquid.NET.Tests.Examples
@@ -16,9 +18,9 @@ public void Test_Simple_Template()
1618

1719
ctx.DefineLocalVariable("myvariable", LiquidString.Create("Hello World"));
1820

19-
var template = LiquidTemplate.Create("<div>{{myvariable}}</div>");
21+
var templateResult = LiquidTemplate.Create("<div>{{myvariable}}</div>");
2022

21-
Assert.That(template.LiquidTemplate.Render(ctx).Result, Is.EqualTo("<div>Hello World</div>"));
23+
Assert.That(templateResult.LiquidTemplate.Render(ctx).Result, Is.EqualTo("<div>Hello World</div>"));
2224

2325
}
2426

@@ -34,9 +36,9 @@ public void Test_Simple_Collection()
3436
LiquidNumeric.Create(6)
3537
});
3638

37-
var template = LiquidTemplate.Create("<ul>{% for item in items %}<li>{{item}}</li>{% endfor %}</ul>");
39+
var templateResult = LiquidTemplate.Create("<ul>{% for item in items %}<li>{{item}}</li>{% endfor %}</ul>");
3840

39-
Assert.That(template.LiquidTemplate.Render(ctx).Result, Is.EqualTo("<ul><li>2</li><li>4</li><li>6</li></ul>"));
41+
Assert.That(templateResult.LiquidTemplate.Render(ctx).Result, Is.EqualTo("<ul><li>2</li><li>4</li><li>6</li></ul>"));
4042

4143
}
4244

@@ -56,9 +58,9 @@ public void Test_Simple_Hash()
5658
{"name", nameHash}
5759
});
5860

59-
var template = LiquidTemplate.Create("You said '{{ greeting.address }} {{ greeting.name.first }} {{ greeting.name.last }}'");
61+
var templateResult = LiquidTemplate.Create("You said '{{ greeting.address }} {{ greeting.name.first }} {{ greeting.name.last }}'");
6062

61-
Assert.That(template.LiquidTemplate.Render(ctx).Result, Is.EqualTo("You said 'Hello Tobias Lütke'"));
63+
Assert.That(templateResult.LiquidTemplate.Render(ctx).Result, Is.EqualTo("You said 'Hello Tobias Lütke'"));
6264

6365
}
6466

@@ -70,11 +72,23 @@ public void Test_Filter()
7072
.DefineLocalVariable("resultcount", LiquidNumeric.Create(42))
7173
.DefineLocalVariable("searchterm", LiquidString.Create("MiXeDcAsE"));
7274

73-
var template = LiquidTemplate.Create("{{ resultcount }} {{ resultcount | pluralize: 'item', 'items' }} were found for '{{searchterm | downcase}}'.");
75+
var templateResult = LiquidTemplate.Create("{{ resultcount }} {{ resultcount | pluralize: 'item', 'items' }} were found for '{{searchterm | downcase}}'.");
7476

75-
Assert.That(template.LiquidTemplate.Render(ctx).Result, Is.EqualTo("42 items were found for 'mixedcase'."));
77+
Assert.That(templateResult.LiquidTemplate.Render(ctx).Result, Is.EqualTo("42 items were found for 'mixedcase'."));
7678

7779
}
7880

81+
[Test]
82+
public void Test_Parsing_Error()
83+
{
84+
ITemplateContext ctx = new TemplateContext();
85+
var templateResult = LiquidTemplate.Create("This tag delimiter is not terminated: {% .");
86+
String error = String.Join(",", templateResult.ParsingErrors.Select(x => x.Message));
87+
Console.WriteLine(error);
88+
Assert.That(error, Is.StringContaining("no viable alternative at input"));
89+
90+
}
91+
92+
7993
}
8094
}

Liquid.NET.Tests/LiquidASTGeneratorTests.cs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Linq;
4+
using Antlr4.Runtime;
45
using Liquid.NET.Constants;
56
using Liquid.NET.Expressions;
67
using Liquid.NET.Symbols;
@@ -263,6 +264,39 @@ public void It_Should_Parse_A_Regular_Variable()
263264
Assert.That(result.Trim(), Is.EqualTo("6"));
264265
}
265266

267+
[Test]
268+
public void It_Should_Generate_One_Error() // bug
269+
{
270+
// Arrange
271+
var templateResult = LiquidTemplate.Create("This tag delimiter is not terminated: {% .");
272+
273+
// Assert
274+
Assert.That(templateResult.ParsingErrors.Count, Is.EqualTo(1));
275+
}
276+
277+
[Test]
278+
public void It_Should_Generate_An_AST_Even_When_Parsing_Errors_Exist()
279+
{
280+
// Arrange
281+
ITemplateContext ctx = new TemplateContext();
282+
var templateResult = LiquidTemplate.Create("This filter is not terminated: {{ test" +
283+
"Some more text");
284+
String error = String.Join(",", templateResult.ParsingErrors.Select(x => x.Message));
285+
286+
Assert.That(error, Is.StringContaining("Missing '}}'"));
287+
Assert.That(templateResult.LiquidTemplate, Is.Not.Null);
288+
289+
// Act
290+
var result = templateResult.LiquidTemplate.Render(ctx);
291+
292+
// Assert
293+
//Console.WriteLine(result.Result);
294+
Assert.That(result.Result, Is.StringContaining("This filter is not terminated"));
295+
//Assert.That(result.Result, Is.StringContaining("Some more text")); // this seems to terminate here...
296+
297+
298+
}
299+
266300
public static IEnumerable<TreeNode<IASTNode>> FindNodesWithType(LiquidAST ast, Type type)
267301
{
268302
return FindWhere(ast.RootNode.Children, type);

Liquid.NET.Tests/Tags/CustomBlockTagTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public void It_Should_Parse_A_Custom_BlockTag()
4040
public void It_Should_Not_Parse_A_Custom_BlockTag_With_No_End()
4141
{
4242
// Act
43-
var templateContext = new TemplateContext().WithAllFilters().WithCustomTagBlockRenderer<WordReverserBlockTag>("echoargs");
43+
//var templateContext = new TemplateContext().WithAllFilters().WithCustomTagBlockRenderer<WordReverserBlockTag>("echoargs");
4444

4545
var templateResult = LiquidTemplate.Create("Result : {% echoargs \"hello\" 123 true %}echo{% endsomethingelse %}");
4646
Assert.That(templateResult.HasParsingErrors);

Liquid.NET/src/LiquidASTGenerator.cs

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22
using System.Collections.Generic;
33
using System.IO;
44
using System.Linq;
5+
using System.Resources;
56
using System.Text.RegularExpressions;
7+
using System.Xml;
68
using Antlr4.Runtime;
79
using Antlr4.Runtime.Atn;
810
using Antlr4.Runtime.Tree;
@@ -62,8 +64,8 @@ public LiquidAST Generate(String template, Action<LiquidError> onParserError)
6264
//Log("Parsing Template \r\n" + template);
6365

6466
//BufferedTokenStream tokenStream
65-
LiquidAST liquidAst = new LiquidAST();
66-
_astNodeStack.Push(liquidAst.RootNode);
67+
LiquidAST liquidAst = Reset();
68+
6769
var stringReader = new StringReader(template);
6870

6971
var liquidLexer = new LiquidLexer(new AntlrInputStream(stringReader));
@@ -86,7 +88,7 @@ public LiquidAST Generate(String template, Action<LiquidError> onParserError)
8688
parser.AddErrorListener(CreateLiquidErrorListener());
8789
new ParseTreeWalker().Walk(this, parser.init());
8890
}
89-
catch (LiquidParserException lpe)
91+
catch (LiquidParserException lpe)
9092
{
9193
// exceptions thrown when building the AST.
9294
// (don't bother with the re-parse)
@@ -97,11 +99,14 @@ public LiquidAST Generate(String template, Action<LiquidError> onParserError)
9799
}
98100
catch
99101
{
102+
100103
parser.Reset();
104+
Reset();
101105
parser.ErrorHandler = defaultErrorStrategy;
102106
parser.Interpreter.PredictionMode = PredictionMode.Ll;
103107
// the error listener is still listening from the try block...
104108
//parser.AddErrorListener(CreateLiquidErrorListener());
109+
105110
new ParseTreeWalker().Walk(this, parser.init());
106111
}
107112

@@ -119,6 +124,19 @@ public LiquidAST Generate(String template, Action<LiquidError> onParserError)
119124
return liquidAst;
120125
}
121126

127+
private LiquidAST Reset()
128+
{
129+
var liquidAst = new LiquidAST();
130+
_astNodeStack.Clear();
131+
_liquidErrors.Clear();
132+
_blockBuilderContextStack.Clear();
133+
_customTagStackAndParent.Clear();
134+
135+
_astNodeStack.Push(liquidAst.RootNode);
136+
137+
return liquidAst;
138+
}
139+
122140
private LiquidErrorListener CreateLiquidErrorListener()
123141
{
124142
var liquidErrorListener = new LiquidErrorListener();

Liquid.NET/src/LiquidTemplate.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,14 @@ public static LiquidParsingResult Create(String template)
1414
LiquidTemplate result = null;
1515
IList<LiquidError> errors = new List<LiquidError>();
1616
var liquidAst = new LiquidASTGenerator().Generate(template, errors.Add);
17-
if (errors.Any())
18-
{
19-
//throw new LiquidParserException(errors);
20-
}
21-
else
22-
{
17+
//if (errors.Any())
18+
//{
19+
// //throw new LiquidParserException(errors);
20+
//}
21+
//else
22+
//{
2323
result = new LiquidTemplate(liquidAst);
24-
}
24+
//}
2525
return LiquidParsingResult.Create(result, errors);
2626
}
2727

0 commit comments

Comments
 (0)