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
13 changes: 13 additions & 0 deletions Liquid.NET.Tests/Constants/DictionaryValueTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,5 +58,18 @@ public void It_Should_Dereference_A_Nested_Dictionary()
Assert.That(result, Is.EqualTo("Result : Dict 3->Dict3"));
}

[Test]
public void It_Should_Fail_When_Dereferencing_A_Missing_Property()
{
// Arrange
var dict = new Dictionary<String, IExpressionConstant>
{
};
DictionaryValue dictValue = new DictionaryValue(dict);

// Assert
Assert.That(dictValue.ValueAt("string1").HasValue, Is.False);

}
}
}
97 changes: 97 additions & 0 deletions Liquid.NET.Tests/Constants/MissingValueTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Liquid.NET.Constants;
using NUnit.Framework;

namespace Liquid.NET.Tests.Constants
{
[TestFixture]
public class MissingValueTests
{
[Test]
[TestCase("x", "x")]
[TestCase("d.x", "x")]
[TestCase("d[x]", "x")]
[TestCase("a.b.c", "a")]
public void It_Should_Display_An_Error_When_Dereferencing_Missing_Value(String varname, String missingVar)
{
// Arrange

ITemplateContext ctx = new TemplateContext()
.ErrorWhenValueMissing();
//ctx.DefineLocalVariable("e", new ArrayValue(new List<IExpressionConstant>()));
ctx.DefineLocalVariable("d", new DictionaryValue(new Dictionary<String,IExpressionConstant>()));

// Act
var result = RenderingHelper.RenderTemplate("Result : {{ "+varname+" }}", ctx);

// Assert
Assert.That(result, Is.EqualTo("Result : ERROR: " + missingVar + " is undefined"));

}

[Test]
[TestCase("e[1]")]
[TestCase("e.first")]
[TestCase("e.last")]
public void It_Should_Display_An_Error_When_Dereferencing_Empty_Array(String varname)
{
// Arrange
ITemplateContext ctx = new TemplateContext()
.ErrorWhenValueMissing();
ctx.DefineLocalVariable("e", new ArrayValue(new List<IExpressionConstant>()));

// Act
var result = RenderingHelper.RenderTemplate("Result : {{ " + varname + " }}", ctx);

// Assert
Assert.That(result, Is.EqualTo("Result : ERROR: cannot dereference empty array"));

}

[Test]
public void It_Should_Display_Error_When_Dereferencing_Array_With_Non_Int()
{
// Arrange
ITemplateContext ctx = new TemplateContext()
.ErrorWhenValueMissing();
ctx.DefineLocalVariable("e", new ArrayValue(new List<IExpressionConstant>()));

// Act
var result = RenderingHelper.RenderTemplate("Result : {{ e.x }}", ctx);

// Assert
Assert.That(result, Is.StringContaining("invalid index: 'x'"));

}

[Test]
[TestCase("x")]
[TestCase("e[1]")]
[TestCase("e.x")]
[TestCase("d.x")]
[TestCase("d[x]")]
[TestCase("a.b.c")]
public void It_Should_Not_Display_An_Error_When_Dereferencing_Missing_Value(String varname)
{
// Arrange
Console.WriteLine(varname);
TemplateContext ctx = new TemplateContext();
ctx.DefineLocalVariable("e", new ArrayValue(new List<IExpressionConstant>()));
ctx.DefineLocalVariable("d", new DictionaryValue(new Dictionary<String, IExpressionConstant>()));

// Act
var result = RenderingHelper.RenderTemplate("Result : {{ " + varname + " }}", ctx);

// Assert
Assert.That(result, Is.EqualTo("Result : "));

}



}
}
58 changes: 58 additions & 0 deletions Liquid.NET.Tests/Constants/ReflectorTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
using System;
using Liquid.NET.Constants;
using NUnit.Framework;

namespace Liquid.NET.Tests.Constants
{
[TestFixture]
public class ReflectorTests
{
[Test]
public void It_Should_Create_A_Dictionary_From_An_Object()
{
// Arrange
var testObj = new TestObj();
var reflector = new Reflector();

// Act

var exprObject = (DictionaryValue) reflector.GenerateExpressionConstant(testObj);

// Assert
Assert.That(exprObject, Is.Not.Null);
Assert.That(exprObject.ValueAt("MyProperty").Value, Is.EqualTo(testObj.MyProperty));
Assert.That(exprObject.ValueAt("MyPropertyWithPrivateSetter").Value, Is.EqualTo(testObj.MyPropertyWithPrivateSetter));
Assert.That(exprObject.ValueAt("MyPublicField").Value, Is.EqualTo(testObj.MyPublicField));
Assert.That(exprObject.ValueAt("MyPrivateProperty").HasValue, Is.False);
Assert.That(exprObject.ValueAt("MyPrivateField").HasValue, Is.False);
}

public class TestObj
{
public TestObj()
{
MyProperty = "A Property";
MyPropertyWithPrivateSetter = "Property With Private Setter";
MyPrivateProperty = "A Private Property";
}

public String MyPublicField = "a public field";

#pragma warning disable 169
#pragma warning disable 414
// ReSharper disable once InconsistentNaming
private String MyPrivateField = "a private field";
#pragma warning restore 414
#pragma warning restore 169

public String MyProperty { get; set; }

public String MyPropertyWithPrivateSetter { get; private set; }

private String MyPrivateProperty { get; set; }

}


}
}
15 changes: 15 additions & 0 deletions Liquid.NET.Tests/Expressions/VariableReferenceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,23 @@ public void It_Should_Derefence_A_Variable()

// Assert
Assert.That(result.Value, Is.EqualTo("HELLO"));
}

[Test]
public void It_Should_Derefence_A_Variable_Missing_Variable_As_None()
{
// Arrange
var variableReference = new VariableReference("myvar");
var templateContext = new TemplateContext();

// Act
var result = variableReference.Eval(templateContext, new List<Option<IExpressionConstant>>());

// Assert
Assert.That(result.SuccessResult.HasValue, Is.False);


}

}
}
63 changes: 41 additions & 22 deletions Liquid.NET.Tests/Filters/Array/MapFilterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Linq;
using Liquid.NET.Constants;
using Liquid.NET.Filters.Array;
using Liquid.NET.Utils;
using NUnit.Framework;

namespace Liquid.NET.Tests.Filters.Array
Expand Down Expand Up @@ -45,25 +46,7 @@ public void It_Should_Return_None_Where_A_Field_Is_Missing()

// Act
var result = (mapFilter.Apply(new TemplateContext(), array).SuccessValue<ArrayValue>()).ToList();


// Assert
//IEnumerable<String> expected = dictionaryValues.Select(x => x.DictValue[field].;
// var expected = array.ArrValue.Select(x => x.Value.ValueAs<DictionaryValue>().DictValue)
// .Select(x => x.ContainsKey(field) ? x[field].Value.ToString() : "");


// var expected = array.ArrValue.Cast<DictionaryValue>().Select(
// x => x.DictValue.ContainsKey(field) ?
// x.DictValue[field].Value.ToString() :
// UndefinedMessage(field)).ToList();

//Logger.Log("EXPECTED: " + String.Join(",", expected));
Assert.That(result.Count(x => !x.HasValue), Is.EqualTo(1));
//var actual = result.Select(x => x.Value.ToString());

//Logger.Log("ACTUAL: " + String.Join(",", actual));
//Assert.That(actual, Is.EquivalentTo(expected));

}

Expand Down Expand Up @@ -101,10 +84,46 @@ public void It_Should_Do_The_Same_As_Lookup_When_Dictionary()
Assert.That(result, Is.EquivalentTo("Value 1 A"));
}

// private static string UndefinedMessage(string field)
// {
// return Undefined.CreateUndefinedMessage(field).ToString();
// }

[Test]
public void It_Should_Render_The_Fields()
{
// Arrange
var array = CreateArray();
ITemplateContext ctx = new TemplateContext()
.WithAllFilters().DefineLocalVariable("arr", array);
var result = RenderingHelper.RenderTemplate("Result : {{ arr | map: \"field1\" }}",ctx);

// Assert
Assert.That(result, Is.EqualTo("Result : Value 1 AValue 2 AValue 3 AValue 4 A"));
}

[Test]
public void It_Should_Render_Missing_Fields_When_ErrorsOff()
{
// Arrange
var array = CreateArray();
ITemplateContext ctx = new TemplateContext()
.WithAllFilters().DefineLocalVariable("arr", array);
var result = RenderingHelper.RenderTemplate("Result : {{ arr | map: \"awefwef\" }}", ctx);

// Assert
Assert.That(result, Is.EqualTo("Result : "));
}

[Test]
public void It_Should_Error_When_No_Field_When_ErrorsOn()
{
// Arrange
var array = CreateArray();
ITemplateContext ctx = new TemplateContext()
.ErrorWhenValueMissing()
.WithAllFilters().DefineLocalVariable("arr", array);
var result = RenderingHelper.RenderTemplate("Result : {{ arr | map: \"missing\" }}", ctx);
Console.WriteLine("Result "+result);
// Assert
Assert.That(result, Is.StringContaining("missing is undefined"));
}

public ArrayValue CreateArray()
{
Expand Down
61 changes: 54 additions & 7 deletions Liquid.NET.Tests/Filters/Array/SortFilterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,7 @@ public void It_Should_Sort_An_Array_By_StringValues()
public void It_Should_Sort_Dictionaries_By_Field()
{
// Arrange
IList<IExpressionConstant> objlist = new List<IExpressionConstant>
{
DataFixtures.CreateDictionary(1, "Aa", "Value 1 B"),
DataFixtures.CreateDictionary(2, "Z", "Value 2 B"),
DataFixtures.CreateDictionary(3, "ab", "Value 3 B"),
DataFixtures.CreateDictionary(4, "b", "Value 4 B"),
};
IList<IExpressionConstant> objlist = CreateObjList();
ArrayValue arrayValue = new ArrayValue(objlist);
SortFilter sizeFilter = new SortFilter(new StringValue("field1"));

Expand All @@ -56,7 +50,60 @@ public void It_Should_Sort_Dictionaries_By_Field()
Assert.That(IdAt(result.SuccessValue<ArrayValue>(), 1, "field1").Value, Is.EqualTo("ab"));
Assert.That(IdAt(result.SuccessValue<ArrayValue>(), 2, "field1").Value, Is.EqualTo("b"));
Assert.That(IdAt(result.SuccessValue<ArrayValue>(), 3, "field1").Value, Is.EqualTo("Z"));
}

[Test]
public void It_Should_Sort_Dictionaries_By_Field_From_Template()
{
// Arrange
ITemplateContext ctx = new TemplateContext()
.WithAllFilters().DefineLocalVariable("arr", new ArrayValue(CreateObjList()));

// Act
var result = RenderingHelper.RenderTemplate("Result : {% assign x = arr | sort: \"field1\" %}{{ x | map: \"field1\" }}", ctx);

// Assert
Assert.That(result, Is.EqualTo("Result : AaabbZ"));
}

[Test]
public void It_Should_Ignore_Dictionaries_With_Missing_Fields()
{
// Arrange
ITemplateContext ctx = new TemplateContext()
.WithAllFilters().DefineLocalVariable("arr", new ArrayValue(CreateObjList()));

// Act
var result = RenderingHelper.RenderTemplate("Result : {% assign x = arr | sort: \"test\" %}{{ x | map: \"id\" }}", ctx);

// Assert
Assert.That(result, Is.EqualTo("Result : 1234"));
}

[Test]
public void It_Should_Error_With_Dictionaries_With_Missing_Fields_When_Errors_On()
{
// Arrange
ITemplateContext ctx = new TemplateContext().ErrorWhenValueMissing()
.WithAllFilters().DefineLocalVariable("arr", new ArrayValue(CreateObjList()));

// Act
var result = RenderingHelper.RenderTemplate("Result : {% assign x = arr | sort: \"test\" %}", ctx);

// Assert
Assert.That(result, Is.StringContaining("an array element is missing the field \'test\'"));
}


private IList<IExpressionConstant> CreateObjList()
{
return new List<IExpressionConstant>
{
DataFixtures.CreateDictionary(1, "Aa", "Value 1 B"),
DataFixtures.CreateDictionary(2, "Z", "Value 2 B"),
DataFixtures.CreateDictionary(3, "ab", "Value 3 B"),
DataFixtures.CreateDictionary(4, "b", "Value 4 B"),
};
}

private static IExpressionConstant IdAt(ArrayValue result, int index, String field)
Expand Down
2 changes: 1 addition & 1 deletion Liquid.NET.Tests/Filters/Strings/RemoveFilterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public void It_Should_Remove_Nil_From_A_String()
var result = RenderingHelper.RenderTemplate("Result : {{ \"test\" | remove : x }}");

// Assert
Assert.That(result, Is.EqualTo("Result : Please specify a replacement string."));
Assert.That(result, Is.EqualTo("Result : ERROR: Please specify a replacement string."));
}


Expand Down
1 change: 1 addition & 0 deletions Liquid.NET.Tests/Liquid.NET.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
<Compile Include="Constants\ExpressionConstantTests.cs" />
<Compile Include="Constants\GeneratorValueTests.cs" />
<Compile Include="Constants\IndexDereferencerTests.cs" />
<Compile Include="Constants\MissingValueTests.cs" />
<Compile Include="Constants\NumericValueTests.cs" />
<Compile Include="Constants\StringValueTests.cs" />
<Compile Include="Constants\ValueCasterTests.cs" />
Expand Down
3 changes: 3 additions & 0 deletions Liquid.NET.Tests/Ruby/FilterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ public class FilterTests {
[TestCase(@"{{ """" | append: x }}", @"", @"")]
[TestCase(@"{{ nil | default: ""NIL TEST"" }}", @"", @"NIL TEST")]
[TestCase(@"{{ """" | default: ""ES TEST"" }}", @"", @"ES TEST")]
[TestCase(@"{{ nil | map: ""test"" }}", @"", @"")]
[TestCase(@"{% assign myarray = ""1,2,3,4"" |split: "","" %}{{ myarray.x }}", @"", @"")]
[TestCase(@"{% assign myarray = ""1,2,3,4"" |split: "","" %}{{ myarray[x] }}", @"", @"")]
public void It_Should_Match_Ruby_Output(String input, String assigns, String expected) {

// Arrange
Expand Down
Loading