From 173cfe839f02f2de06052f0d5e6c346797ce3cc3 Mon Sep 17 00:00:00 2001 From: Michael Date: Wed, 12 Dec 2018 05:31:43 +0100 Subject: [PATCH] Add unit tests for empty string, null and single curly brace. --- MustacheSharp.Tests/FormatCompilerTester.cs | 45 +++++++++++++++++++ .../HtmlFormatCompilerTester.cs | 23 ++++++++++ 2 files changed, 68 insertions(+) diff --git a/MustacheSharp.Tests/FormatCompilerTester.cs b/MustacheSharp.Tests/FormatCompilerTester.cs index 64faf57..bf0c58c 100644 --- a/MustacheSharp.Tests/FormatCompilerTester.cs +++ b/MustacheSharp.Tests/FormatCompilerTester.cs @@ -1527,6 +1527,51 @@ Odd Assert.AreEqual(expected, actual, "The string was not passed to the formatter."); } + [TestMethod] + public void TestCompile_EmptyStringProperty() + { + FormatCompiler compiler = new FormatCompiler(); + const string format = @"{{Greeting}} {{Name}}"; + var data = new + { + Greeting = "Hello", + Name = "" + }; + Generator generator = compiler.Compile(format); + string actual = generator.Render(data); + string expected = "Hello "; + Assert.AreEqual(expected, actual, "An empty string property should be rendered as an empty string."); + } + + [TestMethod] + public void TestCompile_NullValueProperty() { + FormatCompiler compiler = new FormatCompiler(); + const string format = @"{{Greeting}} {{Name}}"; + var data = new + { + Greeting = "Hello", + Name = (String)null + }; + Generator generator = compiler.Compile(format); + string actual = generator.Render(data); + string expected = "Hello "; + Assert.AreEqual(expected, actual, "An null valued property should be rendered as an empty string."); + } + + [TestMethod] + public void TestCompile_AllowSingleCurlyBracesInData() { + FormatCompiler compiler = new FormatCompiler(); + const string format = @"See this code: {{Code}}!"; + var data = new + { + Code = "function() { retrurn 'this is evil'; }" + }; + Generator generator = compiler.Compile(format); + string actual = generator.Render(data); + string expected = "See this code: function() { retrurn 'this is evil'; }!"; + Assert.AreEqual(expected, actual, "Should not touch single curly braces in data values."); + } + #endregion #region Numbers diff --git a/MustacheSharp.Tests/HtmlFormatCompilerTester.cs b/MustacheSharp.Tests/HtmlFormatCompilerTester.cs index 60e5974..bf26fe7 100644 --- a/MustacheSharp.Tests/HtmlFormatCompilerTester.cs +++ b/MustacheSharp.Tests/HtmlFormatCompilerTester.cs @@ -28,5 +28,28 @@ namespace Mustache.Test }); Assert.AreEqual("Hello, John \"The Man\" Standford!!!", html); } + + [TestMethod] + public void ShouldNotTouchValueContainingSingleCurlyBraces() { + HtmlFormatCompiler compiler = new HtmlFormatCompiler(); + var generator = compiler.Compile("Bold statement!"); + string html = generator.Render(new + { + Style = "b { color: red; }" + }); + Assert.AreEqual("Bold statement!", html); + } + + [TestMethod] + public void ShouldNotTouchValueContainingSingleCurlyBracesInsideTripleCurlyBraces() { + HtmlFormatCompiler compiler = new HtmlFormatCompiler(); + var generator = compiler.Compile("Bold statement!"); + string html = generator.Render(new + { + Style = "b { color: red; }" + }); + Assert.AreEqual("Bold statement!", html); + } + } }