Treat DBNull as False in Conditions

This commit is contained in:
Travis Parks 2014-08-01 11:35:37 -04:00
parent 2bf2f0c25c
commit 76a3f60a94
3 changed files with 108 additions and 4 deletions

View File

@ -792,6 +792,110 @@ Middle";
Assert.AreEqual("BeforeAfter", result, "The wrong text was generated."); Assert.AreEqual("BeforeAfter", result, "The wrong text was generated.");
} }
/// <summary>
/// If the condition evaluates to false, the content of an if statement should not be printed.
/// </summary>
[TestMethod]
public void TestCompile_If_null_SkipsContent()
{
FormatCompiler parser = new FormatCompiler();
const string format = "Before{{#if this}}Content{{/if}}After";
Generator generator = parser.Compile(format);
string result = generator.Render(null);
Assert.AreEqual("BeforeAfter", result, "The wrong text was generated.");
}
/// <summary>
/// If the condition evaluates to false, the content of an if statement should not be printed.
/// </summary>
[TestMethod]
public void TestCompile_If_DBNull_SkipsContent()
{
FormatCompiler parser = new FormatCompiler();
const string format = "Before{{#if this}}Content{{/if}}After";
Generator generator = parser.Compile(format);
string result = generator.Render(DBNull.Value);
Assert.AreEqual("BeforeAfter", result, "The wrong text was generated.");
}
/// <summary>
/// If the condition evaluates to false, the content of an if statement should not be printed.
/// </summary>
[TestMethod]
public void TestCompile_If_EmptyIEnumerable_SkipsContent()
{
FormatCompiler parser = new FormatCompiler();
const string format = "Before{{#if this}}Content{{/if}}After";
Generator generator = parser.Compile(format);
string result = generator.Render(Enumerable.Empty<int>());
Assert.AreEqual("BeforeAfter", result, "The wrong text was generated.");
}
/// <summary>
/// If the condition evaluates to false, the content of an if statement should not be printed.
/// </summary>
[TestMethod]
public void TestCompile_If_NullChar_SkipsContent()
{
FormatCompiler parser = new FormatCompiler();
const string format = "Before{{#if this}}Content{{/if}}After";
Generator generator = parser.Compile(format);
string result = generator.Render('\0');
Assert.AreEqual("BeforeAfter", result, "The wrong text was generated.");
}
/// <summary>
/// If the condition evaluates to false, the content of an if statement should not be printed.
/// </summary>
[TestMethod]
public void TestCompile_If_ZeroInt_SkipsContent()
{
FormatCompiler parser = new FormatCompiler();
const string format = "Before{{#if this}}Content{{/if}}After";
Generator generator = parser.Compile(format);
string result = generator.Render(0);
Assert.AreEqual("BeforeAfter", result, "The wrong text was generated.");
}
/// <summary>
/// If the condition evaluates to false, the content of an if statement should not be printed.
/// </summary>
[TestMethod]
public void TestCompile_If_ZeroFloat_SkipsContent()
{
FormatCompiler parser = new FormatCompiler();
const string format = "Before{{#if this}}Content{{/if}}After";
Generator generator = parser.Compile(format);
string result = generator.Render(0f);
Assert.AreEqual("BeforeAfter", result, "The wrong text was generated.");
}
/// <summary>
/// If the condition evaluates to false, the content of an if statement should not be printed.
/// </summary>
[TestMethod]
public void TestCompile_If_ZeroDouble_SkipsContent()
{
FormatCompiler parser = new FormatCompiler();
const string format = "Before{{#if this}}Content{{/if}}After";
Generator generator = parser.Compile(format);
string result = generator.Render(0.0);
Assert.AreEqual("BeforeAfter", result, "The wrong text was generated.");
}
/// <summary>
/// If the condition evaluates to false, the content of an if statement should not be printed.
/// </summary>
[TestMethod]
public void TestCompile_If_ZeroDecimal_SkipsContent()
{
FormatCompiler parser = new FormatCompiler();
const string format = "Before{{#if this}}Content{{/if}}After";
Generator generator = parser.Compile(format);
string result = generator.Render(0m);
Assert.AreEqual("BeforeAfter", result, "The wrong text was generated.");
}
/// <summary> /// <summary>
/// If the condition evaluates to false, the content of an if statement should not be printed. /// If the condition evaluates to false, the content of an if statement should not be printed.
/// </summary> /// </summary>
@ -920,7 +1024,7 @@ Content{{/if}}";
/// If the a header follows a footer, it shouldn't generate a new line. /// If the a header follows a footer, it shouldn't generate a new line.
/// </summary> /// </summary>
[TestMethod] [TestMethod]
public void TestCompile_IfNewLineContentNewLineEndIfIfNewLineContenNewLineEndIf_PrintsContent() public void TestCompile_IfNewLineContentNewLineEndIfIfNewLineContentNewLineEndIf_PrintsContent()
{ {
FormatCompiler parser = new FormatCompiler(); FormatCompiler parser = new FormatCompiler();
const string format = @"{{#if this}} const string format = @"{{#if this}}

View File

@ -65,7 +65,7 @@ namespace Mustache
private bool isConditionSatisfied(object condition) private bool isConditionSatisfied(object condition)
{ {
if (condition == null) if (condition == null || condition == DBNull.Value)
{ {
return false; return false;
} }

View File

@ -14,6 +14,6 @@ using System.Runtime.InteropServices;
[assembly: CLSCompliant(true)] [assembly: CLSCompliant(true)]
[assembly: ComVisible(false)] [assembly: ComVisible(false)]
[assembly: Guid("e5a4263d-d450-4d85-a4d5-44c0a2822668")] [assembly: Guid("e5a4263d-d450-4d85-a4d5-44c0a2822668")]
[assembly: AssemblyVersion("0.2.8.0")] [assembly: AssemblyVersion("0.2.8.1")]
[assembly: AssemblyFileVersion("0.2.8.0")] [assembly: AssemblyFileVersion("0.2.8.1")]
[assembly: InternalsVisibleTo("mustache-sharp.test")] [assembly: InternalsVisibleTo("mustache-sharp.test")]