Skip to content

Commit

Permalink
Merge pull request #94 from jdiamond/dev
Browse files Browse the repository at this point in the history
Release 1.15.1
  • Loading branch information
Romanx committed May 16, 2015
2 parents 97a21c8 + 37ccdf9 commit 43b749c
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 1 deletion.
9 changes: 9 additions & 0 deletions Nustache.Compilation.Tests/Nustache.Compilation.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,14 @@
<Reference Include="Newtonsoft.Json">
<HintPath>..\packages\Newtonsoft.Json.5.0.6\lib\net40\Newtonsoft.Json.dll</HintPath>
</Reference>
<Reference Include="nunit.core">
<HintPath>..\packages\NUnitTestAdapter.2.0.0\lib\nunit.core.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="nunit.core.interfaces">
<HintPath>..\packages\NUnitTestAdapter.2.0.0\lib\nunit.core.interfaces.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="nunit.framework">
<HintPath>..\packages\NUnit.2.6.2\lib\nunit.framework.dll</HintPath>
</Reference>
Expand All @@ -51,6 +59,7 @@
</ItemGroup>
<ItemGroup>
<Compile Include="Compiled_Templates_Support.cs" />
<Compile Include="UseEncoderDelegate.cs" />
<Compile Include="Describe_Compiled_Template_Data_Types.cs" />
<Compile Include="Describe_Compound_Expression_Enumerator.cs" />
<Compile Include="Mustache_Spec\examples\delimiters.cs" />
Expand Down
52 changes: 52 additions & 0 deletions Nustache.Compilation.Tests/UseEncoderDelegate.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;
using System.IO;
using Nustache.Compilation;
using Nustache.Core;

namespace Nustache.Compilation.Tests
{
[TestFixture]
public class Encoder_Delegate_Usage
{
class TemplateData
{
public string Value { get; set; }
}

[Test]
public void ReplacingHtmlEncodeWorksForCompiledTemplates()
{
// replace the default encoder with one that wraps the input in "--"
Encoders.HtmlEncoder encoder = (input) => "--" + input + "--";
Encoders.HtmlEncode = encoder;

var template = Template("{{Value}}");
var compiled = template.Compile<TemplateData>(null);


var inputText = "Some cool text";
var data = new TemplateData()
{
Value = inputText
};

var expectedOutput = encoder(inputText);

Assert.AreEqual(expectedOutput, compiled(data));

// reset the used HTML encoder to default
Encoders.HtmlEncode = Encoders.DefaultHtmlEncode;
}

private Template Template(string text)
{
var template = new Template();
template.Load(new StringReader(text));
return template;
}
}
}
4 changes: 3 additions & 1 deletion Nustache.Compilation/CompilePartVisitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,9 @@ public void Visit(VariableReference variable)

if (variable.Escaped)
{
parts.Add(CompoundExpression.IndentOnLineEnd(Expression.Call(null, typeof(Encoders).GetMethod("DefaultHtmlEncode"), getter), context));
var escaperProperty = typeof(Encoders).GetProperty("HtmlEncode", System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.Public);
var escaperMethod = (Delegate)escaperProperty.GetValue(null, null);
parts.Add(CompoundExpression.IndentOnLineEnd(Expression.Call(null, escaperMethod.Method, getter), context));
}
else
{
Expand Down
46 changes: 46 additions & 0 deletions Nustache.Core.Tests/Example_DataTable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,51 @@ public void It_can_Render_Datatables_Case_Insensitive()

Assert.AreEqual("123", result);
}

[Test]
public void It_Should_Render_Inverted_When_Having_No_Rows()
{
var dt = new System.Data.DataTable();
dt.Columns.Add("Foo");

var result = Render.StringToString(
@"<table>
<thead>
<tr>
<th>Foo</th>
</tr>
</thead>
<tbody>
{{#Data}}
<tr>
<td>{{Foo}}</td>
</tr>
{{/Data}}
{{^Data}}
<tr>
<td>
No data exists.
</td>
</tr>
{{/Data}}
</tbody>
</table>", new { Data = dt });

Assert.AreEqual(
@"<table>
<thead>
<tr>
<th>Foo</th>
</tr>
</thead>
<tbody>
<tr>
<td>
No data exists.
</td>
</tr>
</tbody>
</table>", result);
}
}
}
5 changes: 5 additions & 0 deletions Nustache.Core/RenderContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,11 @@ public bool IsTruthy(object value)
return ((IEnumerable)value).GetEnumerator().MoveNext();
}

if (value is DataTable)
{
return ((DataTable)value).Rows.Count > 0;
}

return true;
}

Expand Down

0 comments on commit 43b749c

Please sign in to comment.