From c82f141252201709606a1ca7dcacd405b3992b6a Mon Sep 17 00:00:00 2001 From: pawel-madurski Date: Thu, 8 Oct 2015 10:04:08 +0200 Subject: [PATCH 1/3] Fix issue in EqualityHelpers while comparing null target --- .../Blocks.NustacheHelpers.Tests.csproj | 7 ++-- .../EqualsHelperTests.cs | 35 +++++++++++++++++++ Blocks.NustacheHelpers.Tests/packages.config | 1 + Blocks.NustacheHelpers.sln | 6 ---- .../Blocks.NustacheHelpers.csproj | 8 ++--- Blocks.NustacheHelpers/EqualityHelpers.cs | 11 ++++++ 6 files changed, 54 insertions(+), 14 deletions(-) diff --git a/Blocks.NustacheHelpers.Tests/Blocks.NustacheHelpers.Tests.csproj b/Blocks.NustacheHelpers.Tests/Blocks.NustacheHelpers.Tests.csproj index e346fd9..f269f93 100644 --- a/Blocks.NustacheHelpers.Tests/Blocks.NustacheHelpers.Tests.csproj +++ b/Blocks.NustacheHelpers.Tests/Blocks.NustacheHelpers.Tests.csproj @@ -39,6 +39,9 @@ False ..\packages\NUnit.2.6.2\lib\nunit.framework.dll + + ..\packages\Nustache.1.15.3.5\lib\net20\Nustache.Core.dll + @@ -59,10 +62,6 @@ {9655265d-8dda-409f-8f81-9cd91fbee387} Blocks.NustacheHelpers - - {3a725cd9-6024-441f-84b5-4226d78352c5} - Nustache.Core - diff --git a/Blocks.NustacheHelpers.Tests/EqualsHelperTests.cs b/Blocks.NustacheHelpers.Tests/EqualsHelperTests.cs index f1a9b47..d1f0175 100644 --- a/Blocks.NustacheHelpers.Tests/EqualsHelperTests.cs +++ b/Blocks.NustacheHelpers.Tests/EqualsHelperTests.cs @@ -131,6 +131,18 @@ public void IfEqHelper_renders_inverse_with_null_comparand() result.Should().Be("not equal"); } + [Test, Category("if_eq")] + public void IfEqHelper_renders_inverse_with_null_target() + { + object one = null; + object two = new { thing = "thing" }; + + var result = Render.StringToString("{{#if_eq one compare=two}}equal{{else}}not equal{{/if_eq}}", + new { one, two }); + + result.Should().Be("not equal"); + } + [Test, Category("unless_eq")] public void UnlessEqHelper_renders_expected_result_with_equal_objects() { @@ -236,6 +248,17 @@ public void UnlessEqHelper_renders_expected_result_with_null_comparand() result.Should().Be("not equal"); } + [Test, Category("unless_eq")] + public void UnlessEqHelper_renders_expected_result_with_null_target() + { + object one = null; + object two = new { thing = "thing" }; + + var result = Render.StringToString("{{#unless_eq one compare=two}}not equal{{/unless_eq}}", new { one, two }); + + result.Should().Be("not equal"); + } + [Test, Category("unless_eq")] public void UnlessEqHelper_renders_inverse_with_null_comparand() { @@ -247,5 +270,17 @@ public void UnlessEqHelper_renders_inverse_with_null_comparand() result.Should().Be("not equal"); } + + [Test, Category("unless_eq")] + public void UnlessEqHelper_renders_inverse_with_null_target() + { + object one = null; + object two = new { thing = "thing" }; + + var result = Render.StringToString("{{#unless_eq one compare=two}}not equal{{else}}equal{{/unless_eq}}", + new { one, two }); + + result.Should().Be("not equal"); + } } } \ No newline at end of file diff --git a/Blocks.NustacheHelpers.Tests/packages.config b/Blocks.NustacheHelpers.Tests/packages.config index e6606e6..b096209 100644 --- a/Blocks.NustacheHelpers.Tests/packages.config +++ b/Blocks.NustacheHelpers.Tests/packages.config @@ -2,4 +2,5 @@ + \ No newline at end of file diff --git a/Blocks.NustacheHelpers.sln b/Blocks.NustacheHelpers.sln index 706324b..2ba22fa 100644 --- a/Blocks.NustacheHelpers.sln +++ b/Blocks.NustacheHelpers.sln @@ -10,8 +10,6 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = ".nuget", ".nuget", "{7A4A00 EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Blocks.NustacheHelpers", "Blocks.NustacheHelpers\Blocks.NustacheHelpers.csproj", "{9655265D-8DDA-409F-8F81-9CD91FBEE387}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Nustache.Core", "Nustache\Nustache.Core\Nustache.Core.csproj", "{3A725CD9-6024-441F-84B5-4226D78352C5}" -EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Blocks.NustacheHelpers.Tests", "Blocks.NustacheHelpers.Tests\Blocks.NustacheHelpers.Tests.csproj", "{653541F0-0960-45EE-B4BA-625D2037A1C8}" EndProject Global @@ -24,10 +22,6 @@ Global {9655265D-8DDA-409F-8F81-9CD91FBEE387}.Debug|Any CPU.Build.0 = Debug|Any CPU {9655265D-8DDA-409F-8F81-9CD91FBEE387}.Release|Any CPU.ActiveCfg = Release|Any CPU {9655265D-8DDA-409F-8F81-9CD91FBEE387}.Release|Any CPU.Build.0 = Release|Any CPU - {3A725CD9-6024-441F-84B5-4226D78352C5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {3A725CD9-6024-441F-84B5-4226D78352C5}.Debug|Any CPU.Build.0 = Debug|Any CPU - {3A725CD9-6024-441F-84B5-4226D78352C5}.Release|Any CPU.ActiveCfg = Release|Any CPU - {3A725CD9-6024-441F-84B5-4226D78352C5}.Release|Any CPU.Build.0 = Release|Any CPU {653541F0-0960-45EE-B4BA-625D2037A1C8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {653541F0-0960-45EE-B4BA-625D2037A1C8}.Debug|Any CPU.Build.0 = Debug|Any CPU {653541F0-0960-45EE-B4BA-625D2037A1C8}.Release|Any CPU.ActiveCfg = Release|Any CPU diff --git a/Blocks.NustacheHelpers/Blocks.NustacheHelpers.csproj b/Blocks.NustacheHelpers/Blocks.NustacheHelpers.csproj index 007a6d9..a575559 100644 --- a/Blocks.NustacheHelpers/Blocks.NustacheHelpers.csproj +++ b/Blocks.NustacheHelpers/Blocks.NustacheHelpers.csproj @@ -32,6 +32,9 @@ 4 + + ..\packages\Nustache.1.15.3.5\lib\net20\Nustache.Core.dll + @@ -44,10 +47,7 @@ - - {3a725cd9-6024-441f-84b5-4226d78352c5} - Nustache.Core - + diff --git a/Blocks.NustacheHelpers/EqualityHelpers.cs b/Blocks.NustacheHelpers/EqualityHelpers.cs index 86a4d8f..ab86e14 100644 --- a/Blocks.NustacheHelpers/EqualityHelpers.cs +++ b/Blocks.NustacheHelpers/EqualityHelpers.cs @@ -45,6 +45,11 @@ public static void Register() internal static void IfEqHelper(RenderContext ctx, IList args, IDictionary options, RenderBlock fn, RenderBlock inverse) { + if (args[0] == null) + { + inverse(args[0]); + return; + } if (args[0].Equals(options["compare"])) fn(args[0]); else @@ -57,6 +62,12 @@ internal static void IfEqHelper(RenderContext ctx, IList args, IDictiona internal static void UnlessEqHelper(RenderContext ctx, IList args, IDictionary options, RenderBlock fn, RenderBlock inverse) { + if (args[0] == null) + { + fn(args[0]); + return; + } + if (args[0].Equals(options["compare"])) inverse(args[0]); else From 85ac324a5cddb379f2bdbce261814bb8f90f52b9 Mon Sep 17 00:00:00 2001 From: pawel-madurski Date: Thu, 8 Oct 2015 10:32:30 +0200 Subject: [PATCH 2/3] change object compare method to use static property from object --- .../EqualsHelperTests.cs | 23 +++++++++++++++++++ Blocks.NustacheHelpers/EqualityHelpers.cs | 15 ++---------- 2 files changed, 25 insertions(+), 13 deletions(-) diff --git a/Blocks.NustacheHelpers.Tests/EqualsHelperTests.cs b/Blocks.NustacheHelpers.Tests/EqualsHelperTests.cs index d1f0175..bc19920 100644 --- a/Blocks.NustacheHelpers.Tests/EqualsHelperTests.cs +++ b/Blocks.NustacheHelpers.Tests/EqualsHelperTests.cs @@ -143,6 +143,18 @@ public void IfEqHelper_renders_inverse_with_null_target() result.Should().Be("not equal"); } + [Test, Category("if_eq")] + public void IfEqHelper_renders_inverse_with_null_target_and_null_comperand() + { + object one = null; + object two = null; + + var result = Render.StringToString("{{#if_eq one compare=two}}equal{{else}}not equal{{/if_eq}}", + new { one, two }); + + result.Should().Be("equal"); + } + [Test, Category("unless_eq")] public void UnlessEqHelper_renders_expected_result_with_equal_objects() { @@ -259,6 +271,17 @@ public void UnlessEqHelper_renders_expected_result_with_null_target() result.Should().Be("not equal"); } + [Test, Category("unless_eq")] + public void UnlessEqHelper_renders_expected_result_with_null_target_and_null_comperand() + { + object one = null; + object two = null; + + var result = Render.StringToString("{{#unless_eq one compare=two}}not equal{{/unless_eq}}", new { one, two }); + + result.Should().BeEmpty(); + } + [Test, Category("unless_eq")] public void UnlessEqHelper_renders_inverse_with_null_comparand() { diff --git a/Blocks.NustacheHelpers/EqualityHelpers.cs b/Blocks.NustacheHelpers/EqualityHelpers.cs index ab86e14..6c855d1 100644 --- a/Blocks.NustacheHelpers/EqualityHelpers.cs +++ b/Blocks.NustacheHelpers/EqualityHelpers.cs @@ -45,12 +45,7 @@ public static void Register() internal static void IfEqHelper(RenderContext ctx, IList args, IDictionary options, RenderBlock fn, RenderBlock inverse) { - if (args[0] == null) - { - inverse(args[0]); - return; - } - if (args[0].Equals(options["compare"])) + if (Object.Equals(args[0], options["compare"])) fn(args[0]); else inverse(args[0]); @@ -62,13 +57,7 @@ internal static void IfEqHelper(RenderContext ctx, IList args, IDictiona internal static void UnlessEqHelper(RenderContext ctx, IList args, IDictionary options, RenderBlock fn, RenderBlock inverse) { - if (args[0] == null) - { - fn(args[0]); - return; - } - - if (args[0].Equals(options["compare"])) + if (Object.Equals(args[0],options["compare"])) inverse(args[0]); else fn(args[0]); From 915bf50e55176f354b18082ec34733e40149597f Mon Sep 17 00:00:00 2001 From: pawel-madurski Date: Fri, 6 Nov 2015 15:07:48 +0100 Subject: [PATCH 3/3] Add if/unless zero helper --- .../EqualsHelperTests.cs | 88 +++++++++++++++++++ Blocks.NustacheHelpers/EqualityHelpers.cs | 53 +++++++++++ Blocks.NustacheHelpers/packages.config | 4 + 3 files changed, 145 insertions(+) create mode 100644 Blocks.NustacheHelpers/packages.config diff --git a/Blocks.NustacheHelpers.Tests/EqualsHelperTests.cs b/Blocks.NustacheHelpers.Tests/EqualsHelperTests.cs index bc19920..ba5d467 100644 --- a/Blocks.NustacheHelpers.Tests/EqualsHelperTests.cs +++ b/Blocks.NustacheHelpers.Tests/EqualsHelperTests.cs @@ -305,5 +305,93 @@ public void UnlessEqHelper_renders_inverse_with_null_target() result.Should().Be("not equal"); } + + [Test, Category("if_eq_zero")] + public void IfEqZeroHelper_renders_expected_with_zero_number() + { + object one = 0; + + var result = Render.StringToString("{{#if_eq_zero one}}equal{{else}}not equal{{/if_eq_zero}}", + new { one }); + + result.Should().Be("equal"); + } + + [Test, Category("if_eq_zero")] + public void IfEqZeroHelper_renders_expected_with_zero_string() + { + object one = "0"; + + var result = Render.StringToString("{{#if_eq_zero one}}equal{{else}}not equal{{/if_eq_zero}}", + new { one }); + + result.Should().Be("equal"); + } + + [Test, Category("if_eq_zero")] + public void IfEqZeroHelper_renders_inverse_with_null() + { + object one = null; + + var result = Render.StringToString("{{#if_eq_zero one}}equal{{else}}not equal{{/if_eq_zero}}", + new { one }); + + result.Should().Be("not equal"); + } + + [Test, Category("if_eq_zero")] + public void IfEqZeroHelper_renders_inverse_with_not_zero() + { + object one = 123123; + + var result = Render.StringToString("{{#if_eq_zero one}}equal{{else}}not equal{{/if_eq_zero}}", + new { one }); + + result.Should().Be("not equal"); + } + + [Test, Category("unless_eq_zero")] + public void UnlessEqZeroHelper_renders_expected_with_zero_number() + { + object one = 0; + + var result = Render.StringToString("{{#unless_eq_zero one}}equal{{else}}not equal{{/unless_eq_zero}}", + new { one }); + + result.Should().Be("not equal"); + } + + [Test, Category("unless_eq_zero")] + public void UnlessEqZeroHelper_renders_expected_with_zero_string() + { + object one = "0"; + + var result = Render.StringToString("{{#unless_eq_zero one}}equal{{else}}not equal{{/unless_eq_zero}}", + new { one }); + + result.Should().Be("not equal"); + } + + [Test, Category("unless_eq_zero")] + public void UnlessEqZeroHelper_renders_inverse_with_null() + { + object one = null; + + var result = Render.StringToString("{{#unless_eq_zero one}}equal{{else}}not equal{{/unless_eq_zero}}", + new { one }); + + result.Should().Be("equal"); + } + + [Test, Category("unless_eq_zero")] + public void UnlessEqZeroHelper_renders_inverse_with_not_zero() + { + object one = 123123; + + var result = Render.StringToString("{{#unless_eq_zero one}}equal{{else}}not equal{{/unless_eq_zero}}", + new { one }); + + result.Should().Be("equal"); + } } } \ No newline at end of file diff --git a/Blocks.NustacheHelpers/EqualityHelpers.cs b/Blocks.NustacheHelpers/EqualityHelpers.cs index 6c855d1..613a104 100644 --- a/Blocks.NustacheHelpers/EqualityHelpers.cs +++ b/Blocks.NustacheHelpers/EqualityHelpers.cs @@ -11,9 +11,15 @@ public static void Register() if (!Helpers.Contains("if_eq")) Helpers.Register("if_eq", IfEqHelper); + if (!Helpers.Contains("if_eq_zero")) + Helpers.Register("if_eq_zero", IfEqZeroHelper); + if (!Helpers.Contains("unless_eq")) Helpers.Register("unless_eq", UnlessEqHelper); + if (!Helpers.Contains("unless_eq_zero")) + Helpers.Register("unless_eq_zero", UnlessEqZeroHelper); + if (!Helpers.Contains("if_gt")) Helpers.Register("if_gt", IfGtHelper); @@ -51,6 +57,53 @@ internal static void IfEqHelper(RenderContext ctx, IList args, IDictiona inverse(args[0]); } + /// + /// if_eq_zero this + /// + internal static void IfEqZeroHelper(RenderContext ctx, IList args, IDictionary options, + RenderBlock fn, RenderBlock inverse) + { + bool isZero = false; + double decArg = 0; + if (args[0] != null && Double.TryParse(args[0].ToString(), out decArg)) + { + isZero = decArg.Equals(0D); + } + + if (isZero) + { + fn(args[0]); + } + else + { + inverse(args[0]); + } + } + + /// + /// unless_eq_zero this + /// + internal static void UnlessEqZeroHelper(RenderContext ctx, IList args, IDictionary options, + RenderBlock fn, RenderBlock inverse) + { + bool isZero = false; + double decArg = 0; + if (args[0] != null && Double.TryParse(args[0].ToString(), out decArg)) + { + isZero = decArg.Equals(0D); + } + + if (isZero) + { + inverse(args[0]); + } + else + { + fn(args[0]); + + } + } + /// /// unless_eq this compare=that /// diff --git a/Blocks.NustacheHelpers/packages.config b/Blocks.NustacheHelpers/packages.config new file mode 100644 index 0000000..aa915a6 --- /dev/null +++ b/Blocks.NustacheHelpers/packages.config @@ -0,0 +1,4 @@ + + + + \ No newline at end of file