Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 22 additions & 3 deletions Liquid.NET.Tests/Constants/MissingValueTests.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Liquid.NET.Constants;
using NUnit.Framework;

Expand Down Expand Up @@ -55,6 +52,8 @@ public void It_Should_Display_An_Error_When_Dereferencing_Empty_Array(String var

}



[Test]
public void It_Should_Display_Error_When_Dereferencing_Array_With_Non_Int()
{
Expand All @@ -73,6 +72,26 @@ public void It_Should_Display_Error_When_Dereferencing_Array_With_Non_Int()

}

[Test]
public void It_Should_Display_Error_When_Dereferencing_Primitive_With_Index()
{
// Arrange
ITemplateContext ctx = new TemplateContext()
.ErrorWhenValueMissing();
ctx.DefineLocalVariable("e", LiquidString.Create("Hello"));

// Act
var template = LiquidTemplate.Create("Result : {{ e.x }}");
var result = template.LiquidTemplate.Render(ctx);

Assert.That(result.HasRenderingErrors, Is.True);
var errorMessage = String.Join(",", result.RenderingErrors.Select(x => x.Message));
// Assert
Assert.That(errorMessage, Is.StringContaining("invalid string index: 'x'"));

}


[Test]
[TestCase("x")]
[TestCase("e[1]")]
Expand Down
130 changes: 0 additions & 130 deletions Liquid.NET.Tests/Filters/LookupFilterTests.cs

This file was deleted.

14 changes: 9 additions & 5 deletions Liquid.NET/src/Constants/IndexDereferencer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -147,14 +147,18 @@ private LiquidExpressionResult DoLookup(ITemplateContext ctx, LiquidString str,
}
else
{
var maybeIndexResult = ValueCaster.Cast<ILiquidValue, LiquidNumeric>(indexProperty);
if (maybeIndexResult.IsError || !maybeIndexResult.SuccessResult.HasValue)
{
return LiquidExpressionResult.Error("invalid array index: " + propertyNameString);
//var maybeIndexResult = ValueCaster.Cast<ILiquidValue, LiquidNumeric>(indexProperty);
var numericIndexProperty = indexProperty as LiquidNumeric;

if (numericIndexProperty == null)
{
return ctx.Options.ErrorWhenValueMissing ?
LiquidExpressionResult.Error("invalid string index: '" + propertyNameString + "'") :
LiquidExpressionResult.Success(new None<ILiquidValue>());
}
else
{
index = maybeIndexResult.SuccessValue<LiquidNumeric>().IntValue;
index = numericIndexProperty.IntValue;
}
}

Expand Down
60 changes: 24 additions & 36 deletions Liquid.Ruby/tests.liquid
Original file line number Diff line number Diff line change
Expand Up @@ -39,48 +39,36 @@ namespace Liquid.NET.Tests.Ruby
var template = LiquidTemplate.Create(input);

// Act
String result = template.LiquidTemplate.Render(ctx);

var result = template.LiquidTemplate.Render(ctx);
Assert.That(result.HasParsingErrors, Is.False);
//Assert.That(result.HasRenderingErrors, Is.False);

// Assert
Assert.That(result.Trim(), Is.EqualTo(expected));
Assert.That(result.Result.Trim(), Is.EqualTo(expected));
}

{% if exceptions != empty %}[Test]{% for test in exceptions %}
[TestCase(@"{{test.input}}", @"{{test.assigns}}", @"{{test.expected | remove: 'EXCEPTION: '}}")]{% endfor %}
public void It_Should_Capture_An_Error(String input, String assigns, String expectedMessage)
{
// Arrange
ITemplateContext ctx = new TemplateContext()
.WithAllFilters()
.WithFileSystem(new TestFileSystem());

foreach (var tuple in DictionaryFactory.CreateStringMapFromJson(assigns))
{
ctx.DefineLocalVariable(tuple.Item1, tuple.Item2);
}

var template = LiquidTemplate.Create(input);
IList<LiquidError> errors = new List<LiquidError>();
//try
//{

String result = template.LiquidTemplate.Render(ctx, onRenderingError: errors.Add);
Assert.That(errors.Count, Is.EqualTo(1));
Assert.That(errors[0].ToString(), Is.StringContaining(expectedMessage));

// TODO: Clean this up:
//}
//catch (LiquidParserException ex)
//{
// Assert
// Assert.That(ex.LiquidErrors[0].ToString(), Is.StringContaining(expectedMessage));
//}
//catch (LiquidRendererException ex)
//{
// Assert
// Assert.That(ex.LiquidErrors[0].ToString(), Is.StringContaining(expectedMessage));
// Assert.That(errors.ToString(), Is.StringContaining(expectedMessage));
//}
{
// Arrange
ITemplateContext ctx = new TemplateContext()
.WithAllFilters()
.WithFileSystem(new TestFileSystem());

foreach (var tuple in DictionaryFactory.CreateStringMapFromJson(assigns))
{
ctx.DefineLocalVariable(tuple.Item1, tuple.Item2);
}

var template = LiquidTemplate.Create(input);
IList<LiquidError> renderingerrors = new List<LiquidError>();
IList<LiquidError> parsingerrors = new List<LiquidError>();

String result = template.LiquidTemplate.Render(ctx, onRenderingError: renderingerrors.Add, onParsingError: parsingerrors.Add);
Assert.That(parsingerrors.Count, Is.EqualTo(1));
Assert.That(parsingerrors[0].ToString(), Is.StringContaining(expectedMessage));

}
{% endif %}
}
Expand Down