Skip to content

Commit 3fd75e7

Browse files
Sergey-Vlasovgovert
authored andcommitted
Added a test for an ExcelFunctionProcessor applied to a standard function.
1 parent 23f2aa3 commit 3fd75e7

File tree

3 files changed

+38
-0
lines changed

3 files changed

+38
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
using ExcelDna.Integration;
2+
using System.Linq.Expressions;
3+
4+
namespace ExcelDna.AddIn.RuntimeTests
5+
{
6+
internal static class ExclamationFunctionRegistration
7+
{
8+
[ExcelFunctionProcessor]
9+
public static IEnumerable<IExcelFunctionInfo> ProcessExclamationFunctions(IEnumerable<IExcelFunctionInfo> registrations, IExcelFunctionRegistrationConfiguration config)
10+
{
11+
foreach (var reg in registrations)
12+
{
13+
if (reg.FunctionAttribute.Name == "MySayHelloWithExclamation")
14+
{
15+
var concatMethod = typeof(string).GetMethod("Concat", new[] { typeof(string), typeof(string) });
16+
var newBody = Expression.Call(concatMethod!, reg.FunctionLambda.Body, Expression.Constant("!"));
17+
reg.FunctionLambda = Expression.Lambda<Func<string, string>>(newBody, reg.FunctionLambda.Parameters);
18+
}
19+
20+
yield return reg;
21+
}
22+
}
23+
}
24+
}

Source/Tests/ExcelDna.AddIn.RuntimeTests/MyFunctions.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@ public static string SayHello(string name)
1313
return $"Hello {name}";
1414
}
1515

16+
[ExcelFunction]
17+
public static string MySayHelloWithExclamation(string name)
18+
{
19+
return $"Hello with exclamation {name}";
20+
}
21+
1622
[ExcelFunction, Logging(7)]
1723
public static string SayHelloWithLoggingID(string name)
1824
{

Source/Tests/ExcelDna.RuntimeTests/Registration.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,14 @@ public void SayHello()
1414
Assert.Equal("Hello world", functionRange.Value.ToString());
1515
}
1616

17+
[ExcelFact(Workbook = "", AddIn = @"..\..\..\..\ExcelDna.AddIn.RuntimeTests\bin\Debug\net6.0-windows\ExcelDna.AddIn.RuntimeTests-AddIn")]
18+
public void ExclamationFunctionProcessor()
19+
{
20+
Range functionRange = ((Worksheet)ExcelDna.Testing.Util.Workbook.Sheets[1]).Range["B1:B1"];
21+
functionRange.Formula = "=MySayHelloWithExclamation(\"world ex\")";
22+
Assert.Equal("Hello with exclamation world ex!", functionRange.Value.ToString());
23+
}
24+
1725
[ExcelFact(Workbook = "", AddIn = @"..\..\..\..\ExcelDna.AddIn.RuntimeTests\bin\Debug\net6.0-windows\ExcelDna.AddIn.RuntimeTests-AddIn")]
1826
public void Double()
1927
{

0 commit comments

Comments
 (0)