Skip to content

Commit 1899681

Browse files
committed
Merge pull request #50 from mikebridge/poco
2 parents 82d0e2b + 6042329 commit 1899681

File tree

9 files changed

+638
-3
lines changed

9 files changed

+638
-3
lines changed

Liquid.NET.Tests/Examples/ExampleTests.cs

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Linq;
33
using Liquid.NET.Constants;
4+
using Liquid.NET.Utils;
45
using NUnit.Framework;
56

67
namespace Liquid.NET.Tests.Examples
@@ -86,7 +87,6 @@ public void Test_Parsing_Error()
8687
Assert.That(error, Is.StringContaining("line 1:52 at <EOF>: Missing '}}'"));
8788
}
8889

89-
9090
[Test]
9191
public void Test_Rendering_Error()
9292
{
@@ -98,5 +98,48 @@ public void Test_Rendering_Error()
9898
//Console.WriteLine("The RESULT was : " + renderingResult.Result);
9999
Assert.That(error, Is.StringContaining("Liquid error: divided by 0"));
100100
}
101+
102+
[Test]
103+
public void Poco_Object_Should_Be_Serialized()
104+
{
105+
ITemplateContext ctx = new TemplateContext()
106+
.DefineLocalVariable("poco", new MyPoco
107+
{
108+
MyStringField = "A string field",
109+
MyNullableIntField = 123,
110+
MyOtherField = "Some Other Field",
111+
MyIgnoredField = "This Shouldn't Show Up",
112+
MyIgnoreIfNotNullField = null,
113+
NestedPoco = new MyPoco { MyStringField = "Nested Poco"}
114+
115+
}.ToLiquid());
116+
117+
var parsingResult = LiquidTemplate.Create("Poco Result: {{ poco }}");
118+
var renderingResult = parsingResult.LiquidTemplate.Render(ctx);
119+
120+
Assert.That(renderingResult.Result, Is.StringContaining(@"Poco Result: { ""mystringfield"" : ""A string field"", ""mynullableintfield"" : 123, ""myrenamedfield"" : ""Some Other Field"", ""nestedpoco"" : { ""mystringfield"" : ""Nested Poco"", ""mynullableintfield"" : null, ""myrenamedfield"" : null } }"));
121+
}
122+
123+
public class MyPoco
124+
{
125+
public String MyStringField { get; set; }
126+
127+
public int? MyNullableIntField { get; set; }
128+
129+
[LiquidName("myrenamedfield")]
130+
public String MyOtherField { get; set; }
131+
132+
[LiquidIgnoreIfNull]
133+
public String MyIgnoreIfNotNullField { get; set; }
134+
135+
[LiquidIgnore]
136+
public String MyIgnoredField { get; set; }
137+
138+
[LiquidIgnoreIfNull]
139+
public MyPoco NestedPoco { get; set; }
140+
141+
}
142+
143+
101144
}
102145
}

Liquid.NET.Tests/Liquid.NET.Tests.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,9 @@
180180
<Compile Include="Tags\UnlessBlockTagTests.cs" />
181181
<Compile Include="TemplateContextTests.cs" />
182182
<Compile Include="Utils\EitherTests.cs" />
183+
<Compile Include="Utils\LiquidConversionExtensionsTests.cs" />
183184
<Compile Include="Utils\OptionTests.cs" />
185+
<Compile Include="Utils\LiquidValueConverterTests.cs" />
184186
<Compile Include="Utils\RegistryTests.cs" />
185187
<Compile Include="Utils\StrFTimeTests.cs" />
186188
<Compile Include="Utils\TryTests.cs" />
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
using System;
2+
using System.Linq;
3+
using Liquid.NET.Utils;
4+
using NUnit.Framework;
5+
6+
namespace Liquid.NET.Tests.Utils
7+
{
8+
[TestFixture]
9+
public class LiquidConversionExtensionsTests
10+
{
11+
[Test]
12+
public void It_Should_Extend_An_Object()
13+
{
14+
// Arrange
15+
var obj = new LiquidValueConverterTests.ClassWithAttributes { Ok = "To Liquid" };
16+
var templateContext = new TemplateContext()
17+
.ErrorWhenValueMissing()
18+
.DefineLocalVariable("test", obj.ToLiquid());
19+
20+
// Act
21+
var parserResult = LiquidTemplate.Create("Hello {{ test.ok }}");
22+
Assert.That(parserResult.HasParsingErrors, Is.False);
23+
var renderingResult = parserResult.LiquidTemplate.Render(templateContext);
24+
Assert.That(renderingResult.HasParsingErrors, Is.False);
25+
Console.WriteLine(String.Join(",", renderingResult.RenderingErrors.Select(x => x.Message)));
26+
Assert.That(renderingResult.HasRenderingErrors, Is.False);
27+
28+
// Assert
29+
Assert.That(renderingResult.Result, Is.EqualTo("Hello To Liquid"));
30+
31+
}
32+
}
33+
}

0 commit comments

Comments
 (0)