-
Notifications
You must be signed in to change notification settings - Fork 438
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #284 from jafin/feat/fluid-templateoptions
feat(fluid) Adds templateOptions as a optional parameter
- Loading branch information
Showing
6 changed files
with
105 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
test/FluentEmail.Liquid.Tests/ComplexModel/ComplexModelRenderTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using FluentAssertions; | ||
using FluentEmail.Core; | ||
using Fluid; | ||
using Microsoft.Extensions.FileProviders; | ||
using Microsoft.Extensions.Options; | ||
using NUnit.Framework; | ||
|
||
namespace FluentEmail.Liquid.Tests.ComplexModel | ||
{ | ||
public class ComplexModelRenderTests | ||
{ | ||
public ComplexModelRenderTests() | ||
{ | ||
SetupRenderer(); | ||
} | ||
|
||
[Test] | ||
public void Can_Render_Complex_Model_Properties() | ||
{ | ||
var model = new ParentModel | ||
{ | ||
ParentName = new NameDetails { Firstname = "Luke", Surname = "Dinosaur" }, | ||
ChildrenNames = new List<NameDetails> | ||
{ | ||
new NameDetails { Firstname = "ChildFirstA", Surname = "ChildLastA" }, | ||
new NameDetails { Firstname = "ChildFirstB", Surname = "ChildLastB" } | ||
} | ||
}; | ||
|
||
var expected = @" | ||
Parent: Luke | ||
Children: | ||
* ChildFirstA ChildLastA | ||
* ChildFirstB ChildLastB | ||
"; | ||
|
||
var email = Email | ||
.From(TestData.FromEmail) | ||
.To(TestData.ToEmail) | ||
.Subject(TestData.Subject) | ||
.UsingTemplate(Template(), model); | ||
email.Data.Body.Should().Be(expected); | ||
} | ||
|
||
private string Template() | ||
{ | ||
return @" | ||
Parent: {{ ParentName.Firstname }} | ||
Children: | ||
{% for Child in ChildrenNames %} | ||
* {{ Child.Firstname }} {{ Child.Surname }}{% endfor %} | ||
"; | ||
} | ||
|
||
private static void SetupRenderer( | ||
IFileProvider fileProvider = null, | ||
Action<TemplateContext, object> configureTemplateContext = null) | ||
{ | ||
var options = new LiquidRendererOptions | ||
{ | ||
FileProvider = fileProvider, | ||
ConfigureTemplateContext = configureTemplateContext, | ||
TemplateOptions = new TemplateOptions { MemberAccessStrategy = new UnsafeMemberAccessStrategy() } | ||
}; | ||
Email.DefaultRenderer = new LiquidRenderer(Options.Create(options)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
using System.Collections.Generic; | ||
|
||
namespace FluentEmail.Liquid.Tests | ||
{ | ||
public class ParentModel | ||
{ | ||
public string Id { get; set; } | ||
public NameDetails ParentName { get; set; } | ||
public List<NameDetails> ChildrenNames { get; set; } = new List<NameDetails>(); | ||
} | ||
|
||
public class NameDetails | ||
{ | ||
public string Firstname { get; set; } | ||
public string Surname { get; set; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
namespace FluentEmail.Liquid.Tests | ||
{ | ||
public static class TestData | ||
{ | ||
public const string ToEmail = "bob@test.com"; | ||
public const string FromEmail = "johno@test.com"; | ||
public const string Subject = "sup dawg"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters