From 76a3f60a941a169320b632e07e6872c1e677ab7c Mon Sep 17 00:00:00 2001 From: Travis Parks Date: Fri, 1 Aug 2014 11:35:37 -0400 Subject: [PATCH] Treat DBNull as False in Conditions --- mustache-sharp.test/FormatCompilerTester.cs | 106 +++++++++++++++++++- mustache-sharp/ConditionTagDefinition.cs | 2 +- mustache-sharp/Properties/AssemblyInfo.cs | 4 +- 3 files changed, 108 insertions(+), 4 deletions(-) diff --git a/mustache-sharp.test/FormatCompilerTester.cs b/mustache-sharp.test/FormatCompilerTester.cs index b57d1fc..0833b4a 100644 --- a/mustache-sharp.test/FormatCompilerTester.cs +++ b/mustache-sharp.test/FormatCompilerTester.cs @@ -792,6 +792,110 @@ Middle"; Assert.AreEqual("BeforeAfter", result, "The wrong text was generated."); } + /// + /// If the condition evaluates to false, the content of an if statement should not be printed. + /// + [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."); + } + + /// + /// If the condition evaluates to false, the content of an if statement should not be printed. + /// + [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."); + } + + /// + /// If the condition evaluates to false, the content of an if statement should not be printed. + /// + [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()); + Assert.AreEqual("BeforeAfter", result, "The wrong text was generated."); + } + + /// + /// If the condition evaluates to false, the content of an if statement should not be printed. + /// + [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."); + } + + /// + /// If the condition evaluates to false, the content of an if statement should not be printed. + /// + [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."); + } + + /// + /// If the condition evaluates to false, the content of an if statement should not be printed. + /// + [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."); + } + + /// + /// If the condition evaluates to false, the content of an if statement should not be printed. + /// + [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."); + } + + /// + /// If the condition evaluates to false, the content of an if statement should not be printed. + /// + [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."); + } + /// /// If the condition evaluates to false, the content of an if statement should not be printed. /// @@ -920,7 +1024,7 @@ Content{{/if}}"; /// If the a header follows a footer, it shouldn't generate a new line. /// [TestMethod] - public void TestCompile_IfNewLineContentNewLineEndIfIfNewLineContenNewLineEndIf_PrintsContent() + public void TestCompile_IfNewLineContentNewLineEndIfIfNewLineContentNewLineEndIf_PrintsContent() { FormatCompiler parser = new FormatCompiler(); const string format = @"{{#if this}} diff --git a/mustache-sharp/ConditionTagDefinition.cs b/mustache-sharp/ConditionTagDefinition.cs index 3363626..82e0ef2 100644 --- a/mustache-sharp/ConditionTagDefinition.cs +++ b/mustache-sharp/ConditionTagDefinition.cs @@ -65,7 +65,7 @@ namespace Mustache private bool isConditionSatisfied(object condition) { - if (condition == null) + if (condition == null || condition == DBNull.Value) { return false; } diff --git a/mustache-sharp/Properties/AssemblyInfo.cs b/mustache-sharp/Properties/AssemblyInfo.cs index 94b8d5c..85e7f9b 100644 --- a/mustache-sharp/Properties/AssemblyInfo.cs +++ b/mustache-sharp/Properties/AssemblyInfo.cs @@ -14,6 +14,6 @@ using System.Runtime.InteropServices; [assembly: CLSCompliant(true)] [assembly: ComVisible(false)] [assembly: Guid("e5a4263d-d450-4d85-a4d5-44c0a2822668")] -[assembly: AssemblyVersion("0.2.8.0")] -[assembly: AssemblyFileVersion("0.2.8.0")] +[assembly: AssemblyVersion("0.2.8.1")] +[assembly: AssemblyFileVersion("0.2.8.1")] [assembly: InternalsVisibleTo("mustache-sharp.test")] \ No newline at end of file