Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix issue in EqualityHelpers while comparing null target #1

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@
<SpecificVersion>False</SpecificVersion>
<HintPath>..\packages\NUnit.2.6.2\lib\nunit.framework.dll</HintPath>
</Reference>
<Reference Include="Nustache.Core">
<HintPath>..\packages\Nustache.1.15.3.5\lib\net20\Nustache.Core.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="Microsoft.CSharp" />
Expand All @@ -59,10 +62,6 @@
<Project>{9655265d-8dda-409f-8f81-9cd91fbee387}</Project>
<Name>Blocks.NustacheHelpers</Name>
</ProjectReference>
<ProjectReference Include="..\Nustache\Nustache.Core\Nustache.Core.csproj">
<Project>{3a725cd9-6024-441f-84b5-4226d78352c5}</Project>
<Name>Nustache.Core</Name>
</ProjectReference>
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
Expand Down
146 changes: 146 additions & 0 deletions Blocks.NustacheHelpers.Tests/EqualsHelperTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,30 @@ 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("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()
{
Expand Down Expand Up @@ -236,6 +260,28 @@ 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_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()
{
Expand All @@ -247,5 +293,105 @@ 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");
}

[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");
}
}
}
1 change: 1 addition & 0 deletions Blocks.NustacheHelpers.Tests/packages.config
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
<packages>
<package id="FluentAssertions" version="2.1.0.0" targetFramework="net40" />
<package id="NUnit" version="2.6.2" targetFramework="net40" />
<package id="Nustache" version="1.15.3.5" targetFramework="net40" />
</packages>
6 changes: 0 additions & 6 deletions Blocks.NustacheHelpers.sln
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
8 changes: 4 additions & 4 deletions Blocks.NustacheHelpers/Blocks.NustacheHelpers.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="Nustache.Core">
<HintPath>..\packages\Nustache.1.15.3.5\lib\net20\Nustache.Core.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="Microsoft.CSharp" />
Expand All @@ -44,10 +47,7 @@
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Nustache\Nustache.Core\Nustache.Core.csproj">
<Project>{3a725cd9-6024-441f-84b5-4226d78352c5}</Project>
<Name>Nustache.Core</Name>
</ProjectReference>
<None Include="packages.config" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Import Project="$(SolutionDir)\.nuget\NuGet.targets" Condition="Exists('$(SolutionDir)\.nuget\NuGet.targets')" />
Expand Down
57 changes: 55 additions & 2 deletions Blocks.NustacheHelpers/EqualityHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -45,10 +51,57 @@ public static void Register()
internal static void IfEqHelper(RenderContext ctx, IList<object> args, IDictionary<string, object> options,
RenderBlock fn, RenderBlock inverse)
{
if (args[0].Equals(options["compare"]))
if (Object.Equals(args[0], options["compare"]))
fn(args[0]);
else
inverse(args[0]);
}

/// <summary>
/// if_eq_zero this
/// </summary>
internal static void IfEqZeroHelper(RenderContext ctx, IList<object> args, IDictionary<string, object> 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]);
}
}

/// <summary>
/// unless_eq_zero this
/// </summary>
internal static void UnlessEqZeroHelper(RenderContext ctx, IList<object> args, IDictionary<string, object> 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]);

}
}

/// <summary>
Expand All @@ -57,7 +110,7 @@ internal static void IfEqHelper(RenderContext ctx, IList<object> args, IDictiona
internal static void UnlessEqHelper(RenderContext ctx, IList<object> args, IDictionary<string, object> options,
RenderBlock fn, RenderBlock inverse)
{
if (args[0].Equals(options["compare"]))
if (Object.Equals(args[0],options["compare"]))
inverse(args[0]);
else
fn(args[0]);
Expand Down
4 changes: 4 additions & 0 deletions Blocks.NustacheHelpers/packages.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Nustache" version="1.15.3.5" targetFramework="net40" />
</packages>