Skip to content

Commit

Permalink
math (#30)
Browse files Browse the repository at this point in the history
  • Loading branch information
StefH committed Apr 20, 2021
1 parent efa6245 commit ecd8d30
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 3 deletions.
40 changes: 38 additions & 2 deletions src/Handlebars.Net.Helpers/Helpers/MathHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,22 +37,52 @@ public double Divide(object value1, object value2)
return ExecuteUtils.Execute(value1, value2, (x1, x2) => 1.0 * x1 / 1.0 * x2);
}

[HandlebarsWriter(WriterType.Value)]
public bool Equal(object value1, object value2)
{
return ExecuteUtils.Execute(Context, value1, value2, (x1, x2) => x1 == x2, (x1, x2) => x1 == x2, (x1, x2) => x1 == x2);
}

[HandlebarsWriter(WriterType.Value)]
public double Floor(object value1, object value2)
{
return ExecuteUtils.Execute(value1, value2, (x1, x2) => Math.Floor(1.0 * x1 / 1.0 * x2));
}

[HandlebarsWriter(WriterType.Value)]
public bool GreaterThan(object value1, object value2)
{
return ExecuteUtils.Execute(Context, value1, value2, (x1, x2) => x1 > x2, (x1, x2) => x1 > x2, (x1, x2) => x1 > x2);
}

[HandlebarsWriter(WriterType.Value)]
public bool GreaterThanEqual(object value1, object value2)
{
return ExecuteUtils.Execute(Context, value1, value2, (x1, x2) => x1 >= x2, (x1, x2) => x1 >= x2, (x1, x2) => x1 >= x2);
}

[HandlebarsWriter(WriterType.Value)]
public bool LessThan(object value1, object value2)
{
return ExecuteUtils.Execute(Context, value1, value2, (x1, x2) => x1 < x2, (x1, x2) => x1 < x2, (x1, x2) => x1 < x2);
}

[HandlebarsWriter(WriterType.Value)]
public bool LessThanEqual(object value1, object value2)
{
return ExecuteUtils.Execute(Context, value1, value2, (x1, x2) => x1 <= x2, (x1, x2) => x1 <= x2, (x1, x2) => x1 <= x2);
}

[HandlebarsWriter(WriterType.Value)]
public object Max(object value1, object value2)
{
return ExecuteUtils.Execute(Context, value1, value2, Math.Max, Math.Max, Math.Max);
return ExecuteUtils.Execute(Context, value1, value2, (x1, x2) => Math.Max(x1, x2), (x1, x2) => Math.Max(x1, x2), (x1, x2) => Math.Max(x1, x2));
}

[HandlebarsWriter(WriterType.Value)]
public object Min(object value1, object value2)
{
return ExecuteUtils.Execute(Context, value1, value2, Math.Min, Math.Min, Math.Min);
return ExecuteUtils.Execute(Context, value1, value2, (x1, x2) => Math.Min(x1, x2), (x1, x2) => Math.Min(x1, x2), (x1, x2) => Math.Min(x1, x2));
}

[HandlebarsWriter(WriterType.Value)]
Expand All @@ -73,6 +103,12 @@ public object Multiply(object value1, object value2)
return ExecuteUtils.Execute(Context, value1, value2, (x1, x2) => x1 * x2, (x1, x2) => x1 * x2, (x1, x2) => x1 * x2);
}

[HandlebarsWriter(WriterType.Value)]
public bool NotEqual(object value1, object value2)
{
return ExecuteUtils.Execute(Context, value1, value2, (x1, x2) => x1 != x2, (x1, x2) => x1 != x2, (x1, x2) => x1 != x2);
}

[HandlebarsWriter(WriterType.Value)]
public object Plus(object value1, object value2)
{
Expand Down
4 changes: 3 additions & 1 deletion src/Handlebars.Net.Helpers/Utils/ExecuteUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public static object Execute(IHandlebars context, object value, Func<int, int> i
}
}

public static object Execute(IHandlebars context, object value1, object value2, Func<int, int, int> intFunc, Func<long, long, long> longFunc, Func<double, double, double> doubleFunc)
public static TResult Execute<TResult>(IHandlebars context, object value1, object value2, Func<int, int, TResult> intFunc, Func<long, long, TResult> longFunc, Func<double, double, TResult> doubleFunc)
{
var supported = new[] { typeof(int), typeof(long), typeof(double) };

Expand Down Expand Up @@ -85,10 +85,12 @@ public static object Execute(IHandlebars context, object value1, object value2,
{
object1 = StringValueParser.Parse(context, value1 is string string1 ? string1 : value1.ToString());
}

if (!supported.Contains(value2.GetType()))
{
object2 = StringValueParser.Parse(context, value2 is string string2 ? string2 : value2.ToString());
}

return Execute(context, object1, object2, intFunc, longFunc, doubleFunc);
}
}
Expand Down
17 changes: 17 additions & 0 deletions test/Handlebars.Net.Helpers.Tests/Helpers/MathHelpersTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,23 @@ public void Add_Complex_And_Complex()
result4.Should().Be(84.1);
}

[Theory]
[InlineData(-1, 0, false)]
[InlineData(-2147483649L, 0, false)]
[InlineData(2147483649L, 0, true)]
[InlineData(1.2, 0.2, true)]
[InlineData(-1.2, 0.2, false)]
[InlineData("-1", 0, false)]
[InlineData("1.2", 0, true)]
public void GreaterThan(object value1, object value2, bool expected)
{
// Act
var result = _sut.GreaterThan(value1, value2);

// Assert
result.Should().Be(expected);
}

[Theory]
[InlineData(-1, 1, 1)]
[InlineData(-1.2, 1, 1)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,21 @@ public void Add(string template, string expected)
// Assert
result.Should().StartWith(expected);
}

[Theory]
[InlineData("{{[Math.LessThan] 2 1}}", "False")]
[InlineData("{{[Math.LessThan] 1 2}}", "True")]
[InlineData("{{[Math.LessThan] 2.2 3.1}}", "True")]
public void LessThan(string template, string expected)
{
// Arrange
var action = _handlebarsContext.Compile(template);

// Act
var result = action("");

// Assert
result.Should().Be(expected);
}
}
}

0 comments on commit ecd8d30

Please sign in to comment.