Skip to content
This repository was archived by the owner on Jun 10, 2024. It is now read-only.
This repository was archived by the owner on Jun 10, 2024. It is now read-only.

extension method implementation does not work with parameterized input #23

Description

@ibt1haj

i have implemented a extension function in ncalc-async like so

public static async Task<object> EvaluateCustomMathFunction(Expression exp)
{
    exp.EvaluateFunctionAsync += NCalcExtensionFunctions;
    var result = await exp.EvaluateAsync();
    return result;
}

private static async Task NCalcExtensionFunctions(string name, FunctionArgs args)
{
    if (name == Constants.Pos)
    {
        var tempResult = await args.Parameters[0].EvaluateAsync();

        if (tempResult is IEnumerable<object> list)
        {
            args.Result = list.Select(x => Convert.ToDouble(x) > 0 ? x : 0);
        }
        else
        {
            var doubleResult = Convert.ToDouble(tempResult);
            args.Result = doubleResult > 0 ? doubleResult : 0;
        }
    }
}

the line

var tempResult = await args.Parameters[0].EvaluateAsync();

always returns zero in case of parameterized input

following is a test case with parameterized input

 [Test]
 public async Task EvaluateCustomMathFunction_WithParameters()
 {
     // Arrange
     var exp = new Expression("Pos(A)", EvaluateOptions.IterateParameters);
     exp.Parameters["A"] = new int[] { 5, 10, -5, 10 };

     // Act
     var result = (IEnumerable) await CustomExpressionEvaluation.EvaluateCustomMathFunction(exp);

     // Assert
     Assert.AreEqual(new int[] { 5, 10, 0, 10 },result);
 }

if the input is a static value then the extension function works correctly

[Test]
public async Task EvaluateCustomMathFunction_PositiveNumber()
{
    // Arrange
    var exp = new Expression("Pos(-5)"); 

    // Act
    var result = await CustomExpressionEvaluation.EvaluateCustomMathFunction(exp);

    // Assert
    Assert.That(result, Is.EqualTo(0)); // The method should return 0 for negative input
}

what am i missing in my implementation of the extension method?
i have also opened a discussion for it on #22

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions