-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: SmartFormat.Net template engine (#15)
- Loading branch information
Showing
7 changed files
with
116 additions
and
6 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
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
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,21 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using SmartFormat; | ||
|
||
namespace Didot.Core.TemplateEngines; | ||
public class SmartFormatWrapper : ITemplateEngine | ||
{ | ||
public string Render(string template, dynamic model) | ||
=> Smart.Format(template, model); | ||
|
||
public string Render(Stream stream, dynamic model) | ||
{ | ||
using var reader = new StreamReader(stream); | ||
var template = reader.ReadToEnd(); | ||
return Render(template, model); | ||
} | ||
} |
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
81 changes: 81 additions & 0 deletions
81
testing/Didot.Core.Testing/TemplateEngines/SmartFormatWrapperTests.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,81 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Dynamic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using Didot.Core.TemplateEngines; | ||
using NUnit.Framework; | ||
|
||
namespace Didot.Core.Testing.TemplateEngines; | ||
public class SmartFormatWrapperTests | ||
{ | ||
[Test] | ||
public void Render_SingleProperty_Successful() | ||
{ | ||
var engine = new SmartFormatWrapper(); | ||
var model = new Dictionary<string, object>() | ||
{ { "Name", "World"} }; | ||
var result = engine.Render("Hello {model.Name}", new { model }); | ||
Assert.That(result, Is.EqualTo("Hello World")); | ||
} | ||
|
||
[Test] | ||
public void Render_MultiProperty_Successful() | ||
{ | ||
var engine = new SmartFormatWrapper(); | ||
var model = new Dictionary<string, object>() | ||
{ { "Name", "Albert"}, {"Age", 30 } }; | ||
var result = engine.Render("Hello {model.Name}. You're {model.Age} years old.", new { model }); | ||
Assert.That(result, Is.EqualTo("Hello Albert. You're 30 years old.")); | ||
} | ||
|
||
[Test] | ||
public void Render_NestedProperties_Successful() | ||
{ | ||
var engine = new SmartFormatWrapper(); | ||
var name = new Dictionary<string, object>() | ||
{ { "First", "Albert"}, {"Last", "Einstein" } }; | ||
var model = new Dictionary<string, object>() | ||
{ { "Name", name}, {"Age", 30 } }; | ||
var result = engine.Render("Hello {model.Name.First} {model.Name.Last}. Your age is {model.Age} years old.", new { model }); | ||
Assert.That(result, Is.EqualTo("Hello Albert Einstein. Your age is 30 years old.")); | ||
} | ||
|
||
[Test] | ||
public void Render_ArrayItems_Successful() | ||
{ | ||
var engine = new SmartFormatWrapper(); | ||
var albert = new Dictionary<string, object>() | ||
{ { "Name", "Albert"}, {"Age", 30 } }; | ||
var nikola = new Dictionary<string, object>() | ||
{ { "Name", "Nikola"}, {"Age", 50 } }; | ||
var model = new[] { albert, nikola }; | ||
var result = engine.Render("Hello {model[0].Name}. Your colleague is {model[1].Age} years old.", new { model }); | ||
Assert.That(result, Is.EqualTo("Hello Albert. Your colleague is 50 years old.")); | ||
} | ||
|
||
[Test] | ||
public void Render_ArrayLoop_Successful() | ||
{ | ||
var engine = new SmartFormatWrapper(); | ||
var albert = new Dictionary<string, object>() | ||
{ { "Name", "Albert"}, {"Age", 30 } }; | ||
var nikola = new Dictionary<string, object>() | ||
{ { "Name", "Nikola"}, {"Age", 50 } }; | ||
var model = new[] { albert, nikola }; | ||
var result = engine.Render("Hello {model:{Name}|, }!", new { model }); | ||
Assert.That(result, Is.EqualTo("Hello Albert, Nikola!")); | ||
} | ||
|
||
[Test] | ||
public void Render_Stream_Successful() | ||
{ | ||
var engine = new SmartFormatWrapper(); | ||
var model = new Dictionary<string, object>() | ||
{ { "Name", "World"} }; | ||
using var stream = new MemoryStream(Encoding.UTF8.GetBytes("Hello {model.Name}")); | ||
var result = engine.Render(stream, new { model }); | ||
Assert.That(result, Is.EqualTo("Hello World")); | ||
} | ||
} |