forked from ncalc/ncalc
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'upstream/master'
- Loading branch information
Showing
8 changed files
with
175 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
using NCalc.Domain; | ||
|
||
namespace NCalc.Visitors; | ||
|
||
/// <summary> | ||
/// Visitor dedicated to extract <see cref="Function"/> names from a <see cref="LogicalExpression"/>. | ||
/// </summary> | ||
public sealed class FunctionExtractionVisitor : ILogicalExpressionVisitor<List<string>> | ||
{ | ||
public List<string> Visit(Identifier identifier) => []; | ||
|
||
public List<string> Visit(LogicalExpressionList list) | ||
{ | ||
var functions = new List<string>(); | ||
foreach (var value in list) | ||
{ | ||
if (value is Function function) | ||
{ | ||
if (!functions.Contains(function.Identifier.Name)) | ||
{ | ||
functions.Add(function.Identifier.Name); | ||
} | ||
} | ||
} | ||
return functions; | ||
} | ||
|
||
public List<string> Visit(UnaryExpression expression) => expression.Expression.Accept(this); | ||
|
||
public List<string> Visit(BinaryExpression expression) | ||
{ | ||
var leftParameters = expression.LeftExpression.Accept(this); | ||
var rightParameters = expression.RightExpression.Accept(this); | ||
|
||
leftParameters.AddRange(rightParameters); | ||
return leftParameters.Distinct().ToList(); | ||
} | ||
|
||
public List<string> Visit(TernaryExpression expression) | ||
{ | ||
var leftParameters = expression.LeftExpression.Accept(this); | ||
var middleParameters = expression.MiddleExpression.Accept(this); | ||
var rightParameters = expression.RightExpression.Accept(this); | ||
|
||
leftParameters.AddRange(middleParameters); | ||
leftParameters.AddRange(rightParameters); | ||
return leftParameters.Distinct().ToList(); | ||
} | ||
|
||
public List<string> Visit(Function function) | ||
{ | ||
var functions = new List<string> { function.Identifier.Name }; | ||
|
||
foreach (var expression in function.Expressions) | ||
{ | ||
var exprParameters = expression.Accept(this); | ||
functions.AddRange(exprParameters); | ||
} | ||
return functions.Distinct().ToList(); | ||
} | ||
|
||
public List<string> Visit(ValueExpression expression) => []; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
namespace NCalc.Tests; | ||
|
||
[Trait("Category","Extraction")] | ||
public class ExtractionTests | ||
{ | ||
[Fact] | ||
public void ShouldGetParametersIssue103() | ||
{ | ||
var expression = new Expression("PageState == 'LIST' && a == 1 && customFunction() == true || in(1 + 1, 1, 2, 3)", ExpressionOptions.CaseInsensitiveStringComparer) | ||
{ | ||
Parameters = | ||
{ | ||
["a"] = 1 | ||
} | ||
}; | ||
expression.DynamicParameters["PageState"] = _ => "List"; | ||
expression.Functions["customfunction"] = _ => true; | ||
|
||
var parameters = expression.GetParameterNames(); | ||
Assert.Contains("a", parameters); | ||
Assert.Contains("PageState", parameters); | ||
Assert.Equal(2, parameters.Count); | ||
} | ||
|
||
[Fact] | ||
public void ShouldGetParametersOneTimeIssue141() | ||
{ | ||
var expression = | ||
new Expression("if(x=0,x,y)", | ||
ExpressionOptions.CaseInsensitiveStringComparer); | ||
var parameters = expression.GetParameterNames(); | ||
|
||
Assert.Equal(2,parameters.Count); | ||
} | ||
|
||
[Fact] | ||
public void ShouldGetParametersWithUnary() | ||
{ | ||
var expression = new Expression("-0.68"); | ||
var p = expression.GetParameterNames(); | ||
Assert.Empty(p); | ||
} | ||
|
||
[Theory] | ||
[InlineData("(a, b, c)", 3)] | ||
[InlineData("725 - 1 == result * secret_operation(secretValue)", 2)] | ||
[InlineData("getLightsaberColor(selectedJedi) == selectedColor", 2)] | ||
public void ShouldGetParameters(string formula, int expectedCount) | ||
{ | ||
var expression = new Expression(formula); | ||
var p = expression.GetParameterNames(); | ||
Assert.Equal(expectedCount, p.Count); | ||
} | ||
|
||
[InlineData("(a, drop_database(), c) == toUpper(getName())", 3)] | ||
[InlineData("Abs(523/2) == Abs(523/2)", 1)] | ||
[InlineData("getLightsaberColor('Yoda') == selectedColor", 1)] | ||
[Theory] | ||
public void ShouldGetFunctions(string formula, int expectedCount) | ||
{ | ||
var expression = new Expression(formula); | ||
var functions = expression.GetFunctionNames(); | ||
Assert.Equal(expectedCount, functions.Count); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.