Skip to content

Commit bf3cb8e

Browse files
HenrikHoyerHenrik Høyer
andauthored
Fixed casting problems in ExecuteUtils breaking EnumerableHelpers (#88)
* - Fixed casting problems in Executeutils breaking Average, Min, Max and Sum functions in EnumerationHelpers - Improved error messages - Added Tests using these methods * Added tests for ExecuteUtils * Added Sum_mixed_Properties_From_Array test Fixed typos in error messages * Use BeApproximately in floating point unit test --------- Co-authored-by: Henrik Høyer <Henrik@crunch-it.dk>
1 parent 2afe410 commit bf3cb8e

File tree

4 files changed

+140
-10
lines changed

4 files changed

+140
-10
lines changed

src/Handlebars.Net.Helpers/Helpers/EnumerableHelpers.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ namespace HandlebarsDotNet.Helpers.Helpers;
1414
internal class EnumerableHelpers : BaseHelpers, IHelpers
1515
{
1616
[HandlebarsWriter(WriterType.Value)]
17-
public object Average(IEnumerable<object> values)
17+
public double Average(IEnumerable<object> values)
1818
{
1919
return ExecuteUtils.Execute(values, x => x.Average());
2020
}
@@ -59,13 +59,13 @@ public bool IsEmpty(IEnumerable<object?>? value)
5959
}
6060

6161
[HandlebarsWriter(WriterType.Value)]
62-
public object Max(IEnumerable<object> values)
62+
public double Max(IEnumerable<object> values)
6363
{
6464
return ExecuteUtils.Execute(values, x => x.Max());
6565
}
6666

6767
[HandlebarsWriter(WriterType.Value)]
68-
public object Min(IEnumerable<object> values)
68+
public double Min(IEnumerable<object> values)
6969
{
7070
return ExecuteUtils.Execute(values, x => x.Min());
7171
}
@@ -109,7 +109,7 @@ public object Min(IEnumerable<object> values)
109109
}
110110

111111
[HandlebarsWriter(WriterType.Value)]
112-
public object Sum(IEnumerable<object> values)
112+
public double Sum(IEnumerable<object> values)
113113
{
114114
return ExecuteUtils.Execute(values, x => x.Sum());
115115
}

src/Handlebars.Net.Helpers/Utils/ExecuteUtils.cs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public static object Execute(IHandlebars context, object value, Func<int, int> i
3737
return doubleFunc(valueParsedAsDouble);
3838
}
3939

40-
throw new NotSupportedException($"The value '{valueAsString}' cannot not be converted to an int, long or double.");
40+
throw new NotSupportedException($"The value '{valueAsString}' cannot be converted to an int, long or double.");
4141

4242
default:
4343
// Just call ToString()
@@ -74,7 +74,7 @@ public static string Execute(IHandlebars context, object value, Func<int, string
7474
return doubleFunc(valueParsedAsDouble);
7575
}
7676

77-
throw new NotSupportedException($"The value '{valueAsString}' cannot not be converted to an int, long or double.");
77+
throw new NotSupportedException($"The value '{valueAsString}' cannot be converted to an int, long or double.");
7878

7979
default:
8080
// Just call ToString()
@@ -129,19 +129,20 @@ public static double Execute(object value, Func<double, double> doubleFunc)
129129
}
130130
catch
131131
{
132-
throw new NotSupportedException();
132+
throw new NotSupportedException($"The value '{value}' cannot be converted to a double.");
133133
}
134134
}
135135

136136
public static double Execute(IEnumerable<object> values, Func<IEnumerable<double>, double> doubleFunc)
137137
{
138138
try
139139
{
140-
return doubleFunc(values.Cast<double>());
140+
var doubles = values.Select(Convert.ToDouble);
141+
return doubleFunc(doubles);
141142
}
142143
catch
143144
{
144-
throw new NotSupportedException();
145+
throw new NotSupportedException($"One of the values cannot be converted to a double.");
145146
}
146147
}
147148

@@ -155,7 +156,7 @@ public static double Execute(IHandlebars context, object value1, object value2,
155156
}
156157
catch
157158
{
158-
throw new NotSupportedException($"The value '{value1}' cannot not be converted to a double.");
159+
throw new NotSupportedException($"The value '{value1}' or '{value2}' cannot be converted to a double.");
159160
}
160161
}
161162
}

test/Handlebars.Net.Helpers.Tests/Helpers/EnumerableHelpersTests.cs

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,5 +147,83 @@ public void Select_NestedProperty_From_DynamicArray()
147147
result.Should().HaveCount(2);
148148
result.Should().BeEquivalentTo(9, null);
149149
}
150+
151+
[Fact]
152+
public void Sum_double_Property_From_Array()
153+
{
154+
// Arrange
155+
var array = new object[] { 10.0, 20.0, 30.0 };
156+
157+
// Act
158+
var result = _sut.Sum(array);
159+
160+
// Assert
161+
result.Should().Be(60.0);
162+
}
163+
164+
[Fact]
165+
public void Sum_int_Property_From_Array()
166+
{
167+
// Arrange
168+
var array = new object[]{ 10, 20, 30 };
169+
170+
// Act
171+
var result = _sut.Sum(array);
172+
173+
// Assert
174+
result.Should().Be(60);
175+
}
176+
177+
[Fact]
178+
public void Sum_mixed_Properties_From_Array()
179+
{
180+
// Arrange
181+
var array = new object[]{ 10, 20.1f, 30.2d, 40L, 50UL, (short)60, (ushort)70, 80.3m, 90U };
182+
183+
// Act
184+
var result = _sut.Sum(array);
185+
186+
// Assert
187+
result.Should().BeApproximately(450.6, 0.0001);
188+
}
189+
190+
[Fact]
191+
public void Min_Property_From_Array()
192+
{
193+
// Arrange
194+
var array = new object[] { 10.0, 20.0, 30.0 };
195+
196+
// Act
197+
var result = _sut.Min(array);
198+
199+
// Assert
200+
result.Should().Be(10.0);
201+
}
202+
203+
[Fact]
204+
public void Max_Property_From_Array()
205+
{
206+
// Arrange
207+
var array = new object[] { 10.0, 20.0, 30.0 };
208+
209+
// Act
210+
var result = _sut.Max(array);
211+
212+
// Assert
213+
result.Should().Be(30.0);
214+
}
215+
216+
[Fact]
217+
public void Average_Property_From_Array()
218+
{
219+
// Arrange
220+
var array = new object[] { 10.0, 20.0, 30.0 };
221+
222+
// Act
223+
var result = _sut.Average(array);
224+
225+
// Assert
226+
result.Should().Be(20.0);
227+
}
150228
}
151229
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
using FluentAssertions;
2+
using HandlebarsDotNet.Helpers.Utils;
3+
using System;
4+
using System.Collections.Generic;
5+
using System.Linq;
6+
using Xunit;
7+
8+
namespace HandlebarsDotNet.Helpers.Tests.Helpers
9+
{
10+
public class ExecuteUtilsTests
11+
{
12+
[Fact]
13+
public void Execute_function_with_invalid_data_should_throw_exception()
14+
{
15+
// Arrange
16+
var value = "invalid data";
17+
18+
// Act and Assert
19+
value
20+
.Invoking(v => ExecuteUtils.Execute(v, Math.Sqrt))
21+
.Should().Throw<NotSupportedException>();
22+
}
23+
24+
[Fact]
25+
public void Execute_array_function_with_invalid_data_should_throw_exception()
26+
{
27+
// Arrange
28+
var array = new object[] { "invalid data" };
29+
var function = new Func<IEnumerable<double>, double>(x => x.FirstOrDefault());
30+
31+
// Act and Assert
32+
array
33+
.Invoking(v => ExecuteUtils.Execute(v, function))
34+
.Should().Throw<NotSupportedException>();
35+
}
36+
37+
[Fact]
38+
public void Execute_two_argument_function_with_invalid_data_should_throw_exception()
39+
{
40+
// Arrange
41+
var v1 = 10.0;
42+
var v2 = "invalid data";
43+
var function = new Func<double, double, double>((x, y) => x + y);
44+
45+
// Act and Assert
46+
v1
47+
.Invoking(v => ExecuteUtils.Execute(null!, v, v2, function))
48+
.Should().Throw<NotSupportedException>();
49+
}
50+
}
51+
}

0 commit comments

Comments
 (0)