Skip to content

Commit 7b750e2

Browse files
committed
Cleaning and small refactoring
1 parent ed7231d commit 7b750e2

File tree

3 files changed

+25
-27
lines changed

3 files changed

+25
-27
lines changed

CodingSeb.ExpressionEvaluator.Tests/ExpressionEvaluatorTests.cs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1773,12 +1773,13 @@ void Evaluator_PreEvaluateVariable(object sender, VariablePreEvaluationEventArg
17731773
.Returns("Hello Bob")
17741774
.SetCategory("Context object");
17751775

1776-
ExpressionEvaluator evaluatorWithPersonContext = new ExpressionEvaluator();
1777-
1778-
evaluatorWithPersonContext.Context = new Person1()
1776+
ExpressionEvaluator evaluatorWithPersonContext = new ExpressionEvaluator
17791777
{
1780-
name = "John",
1781-
LastName = "Smith"
1778+
Context = new Person1()
1779+
{
1780+
name = "John",
1781+
LastName = "Smith"
1782+
}
17821783
};
17831784

17841785
yield return new TestCaseData(evaluatorWithPersonContext

CodingSeb.ExpressionEvaluator/ExpressionEvaluator.cs

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
namespace CodingSeb.ExpressionEvaluator
2323
{
2424
/// <summary>
25-
/// This class allow to evaluate a string math or pseudo C# expression
25+
/// This class allow to evaluate a string math or pseudo C# expression
2626
/// </summary>
2727
public partial class ExpressionEvaluator
2828
{
@@ -653,8 +653,8 @@ public string OptionNumberParsingThousandSeparator
653653
public bool OptionFluidPrefixingActive { get; set; } = true;
654654

655655
/// <summary>
656-
/// if <c>true</c> allow the use of inline namespace (Can be slow, and is less secure).
657-
/// if <c>false</c> unactive inline namespace (only namespaces in Namespaces list are available).
656+
/// if <c>true</c> allow the use of inline namespace (Can be slow, and is less secure).
657+
/// if <c>false</c> unactive inline namespace (only namespaces in Namespaces list are available).
658658
/// By default : true
659659
/// </summary>
660660
public bool OptionInlineNamespacesEvaluationActive { get; set; } = true;
@@ -744,7 +744,7 @@ public bool OptionNewFunctionEvaluationActive
744744

745745
/// <summary>
746746
/// If <c>true</c> Evaluate function is callables in an expression. If <c>false</c> Evaluate is not callable.
747-
/// By default : true
747+
/// By default : true
748748
/// if set to false for security (also ensure that ExpressionEvaluator type is in TypesToBlock list)
749749
/// </summary>
750750
public bool OptionEvaluateFunctionActive { get; set; } = true;
@@ -772,7 +772,7 @@ public bool OptionNewFunctionEvaluationActive
772772

773773
/// <summary>
774774
/// If <c>true</c> ScriptEvaluate function is callables in an expression. If <c>false</c> Evaluate is not callable.
775-
/// By default : true
775+
/// By default : true
776776
/// if set to false for security (also ensure that ExpressionEvaluator type is in TypesToBlock list)
777777
/// </summary>
778778
public bool OptionScriptEvaluateFunctionActive { get; set; } = true;
@@ -1394,7 +1394,7 @@ void forAction(int index)
13941394

13951395
bool executed = false;
13961396

1397-
if (TryParseStringAndParenthisAndCurlyBrackets(ref i)){}
1397+
if (TryParseStringAndParenthisAndCurlyBrackets(ref i)) { }
13981398
else if (script.Length - i > 2 && script.Substring(i, 3).Equals("';'"))
13991399
{
14001400
i += 2;
@@ -1454,7 +1454,7 @@ public T Evaluate<T>(string expression)
14541454

14551455
private IList<ParsingMethodDelegate> parsingMethods;
14561456

1457-
protected virtual IList<ParsingMethodDelegate> ParsingMethods => parsingMethods ?? (parsingMethods = new List<ParsingMethodDelegate>()
1457+
protected virtual IList<ParsingMethodDelegate> ParsingMethods => parsingMethods ?? (parsingMethods = new List<ParsingMethodDelegate>()
14581458
{
14591459
EvaluateCast,
14601460
EvaluateNumber,
@@ -1809,7 +1809,7 @@ protected virtual bool EvaluateVarOrFunc(string expression, Stack<object> stack,
18091809

18101810
try
18111811
{
1812-
if(obj is NullConditionalNullValue)
1812+
if (obj is NullConditionalNullValue)
18131813
{
18141814
stack.Push(obj);
18151815
}
@@ -1851,7 +1851,7 @@ protected virtual bool EvaluateVarOrFunc(string expression, Stack<object> stack,
18511851
{
18521852
if (dictionaryObject[varFuncName] is InternalDelegate internalDelegate)
18531853
stack.Push(internalDelegate(oArgs.ToArray()));
1854-
else if(dictionaryObject[varFuncName] is Delegate del)
1854+
else if (dictionaryObject[varFuncName] is Delegate del)
18551855
stack.Push(del.DynamicInvoke(oArgs.ToArray()));
18561856
}
18571857
else if (objType.GetProperty(varFuncName, InstanceBindingFlag) is PropertyInfo instancePropertyInfo
@@ -2250,7 +2250,7 @@ protected virtual bool EvaluateVarOrFunc(string expression, Stack<object> stack,
22502250
{
22512251
if (Variables.ContainsKey(varFuncName) && Variables[varFuncName] is StronglyTypedVariable stronglyTypedVariable)
22522252
{
2253-
if(cusVarValueToPush == null && stronglyTypedVariable.Type.IsValueType && Nullable.GetUnderlyingType(stronglyTypedVariable.Type) == null)
2253+
if (cusVarValueToPush == null && stronglyTypedVariable.Type.IsValueType && Nullable.GetUnderlyingType(stronglyTypedVariable.Type) == null)
22542254
{
22552255
throw new ExpressionEvaluatorSyntaxErrorException($"Can not cast null to {stronglyTypedVariable.Type} because it's not a nullable valueType");
22562256
}
@@ -2417,11 +2417,11 @@ protected virtual bool EvaluateOperators(string expression, Stack<object> stack,
24172417

24182418
Match match = Regex.Match(expression.Substring(i), regexPattern, optionCaseSensitiveEvaluationActive ? RegexOptions.None : RegexOptions.IgnoreCase);
24192419

2420-
if(match.Success)
2420+
if (match.Success)
24212421
{
24222422
string op = match.Value;
24232423
stack.Push(operatorsDictionary[op]);
2424-
i+= op.Length - 1;
2424+
i += op.Length - 1;
24252425
return true;
24262426
}
24272427

@@ -2572,7 +2572,6 @@ protected virtual bool EvaluateIndexing(string expression, Stack<object> stack,
25722572

25732573
dynamic right = Evaluate(innerExp.ToString());
25742574
ExpressionOperator op = indexingBeginningMatch.Length == 2 ? ExpressionOperator.IndexingWithNullConditional : ExpressionOperator.Indexing;
2575-
25762575

25772576
if (OptionForceIntegerNumbersEvaluationsAsDoubleByDefault && right is double && Regex.IsMatch(innerExp.ToString(), @"^\d+$"))
25782577
right = (int)right;
@@ -2995,7 +2994,7 @@ protected virtual MethodInfo GetRealMethod(ref Type type, ref object obj, string
29952994
.MakeGenericMethod(parameterType.GetGenericArguments());
29962995
modifiedArgs[a] = Delegate.CreateDelegate(parameterType, de, encapsMethod);
29972996
}
2998-
else if(paramTypeName.StartsWith("Action")
2997+
else if (paramTypeName.StartsWith("Action")
29992998
&& modifiedArgs[a] is InternalDelegate)
30002999
{
30013000
InternalDelegate led = modifiedArgs[a] as InternalDelegate;
@@ -3047,7 +3046,7 @@ protected virtual MethodInfo MakeConcreteMethodIfGeneric(MethodInfo methodInfo,
30473046
{
30483047
if (genericsTypes.Equals(string.Empty))
30493048
{
3050-
if(inferedGenericsTypes != null && inferedGenericsTypes.Length == methodInfo.GetGenericArguments().Length)
3049+
if (inferedGenericsTypes != null && inferedGenericsTypes.Length == methodInfo.GetGenericArguments().Length)
30513050
{
30523051
return methodInfo.MakeGenericMethod(inferedGenericsTypes);
30533052
}
@@ -3722,7 +3721,7 @@ public override int GetHashCode()
37223721

37233722
public bool Equals(ExpressionOperator otherOperator)
37243723
{
3725-
return otherOperator!= null && OperatorValue == otherOperator.OperatorValue;
3724+
return otherOperator != null && OperatorValue == otherOperator.OperatorValue;
37263725
}
37273726
}
37283727

TryWindow/MainWindow.xaml.cs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ namespace TryWindow
1616
/// </summary>
1717
public partial class MainWindow : Window
1818
{
19-
private string persistCodeFileName = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "code.cs");
20-
private string persistIterationFileName = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "iterations");
19+
private readonly string persistCodeFileName = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "code.cs");
20+
private readonly string persistIterationFileName = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "iterations");
2121

2222
private CancellationTokenSource cancellationTokenSource = null;
2323

@@ -88,7 +88,7 @@ private async void CalculateButton_Click(object sender, RoutedEventArgs e)
8888
{
8989
stopWatch.Stop();
9090
}
91-
}, cancellationTokenSource.Token);
91+
}, cancellationTokenSource.Token).ConfigureAwait(true);
9292

9393
if (exception == null)
9494
ResultTextBlock.Text = result;
@@ -110,7 +110,6 @@ private async void CalculateButton_Click(object sender, RoutedEventArgs e)
110110

111111
private void Evaluator_EvaluateFunction(object sender, FunctionEvaluationEventArg e)
112112
{
113-
114113
}
115114

116115
private void Evaluator_EvaluateVariable(object sender, VariableEvaluationEventArg e)
@@ -138,8 +137,7 @@ private void Evaluator_EvaluateVariable(object sender, VariableEvaluationEventAr
138137

139138
private void CancelButton_Click(object sender, RoutedEventArgs e)
140139
{
141-
if (cancellationTokenSource != null)
142-
cancellationTokenSource.Cancel();
140+
cancellationTokenSource?.Cancel();
143141
}
144142

145143
private void ScriptTextBox_TextChanged(object sender, EventArgs e)

0 commit comments

Comments
 (0)