diff --git a/CodingSeb.ExpressionEvaluator.Tests/ExpressionEvaluatorTests.cs b/CodingSeb.ExpressionEvaluator.Tests/ExpressionEvaluatorTests.cs index 423fc78..690189f 100644 --- a/CodingSeb.ExpressionEvaluator.Tests/ExpressionEvaluatorTests.cs +++ b/CodingSeb.ExpressionEvaluator.Tests/ExpressionEvaluatorTests.cs @@ -617,6 +617,8 @@ public void TypeTesting(string expression, Type type) [TestCase("Abs(-4) > 10 / 2 ? Abs(-3) : (Abs(-4) + 4) / 2", ExpectedResult = 4, Category = "Conditional Operator t ? x : y")] [TestCase("Abs(-4) < 10 / 2 ? (true ? 6 : 3+2) : (false ? Abs(-18) : 100 / 2)", ExpectedResult = 6, Category = "Conditional Operator t ? x : y")] [TestCase("Abs(-4) > 10 / 2 ? (true ? 6 : 3+2) : (false ? Abs(-18) : 100 / 2)", ExpectedResult = 50, Category = "Conditional Operator t ? x : y")] + [TestCase("Abs(-4) > 10 / 2?(true ? 6 : 3+2):(false?Abs(-18):100 / 2)", ExpectedResult = 50, Category = "Conditional Operator t ? x : y")] + [TestCase("1==1?true:false", ExpectedResult = true, Category = "Conditional Operator t ? x : y")] #endregion #region Math Constants @@ -1773,12 +1775,13 @@ void Evaluator_PreEvaluateVariable(object sender, VariablePreEvaluationEventArg .Returns("Hello Bob") .SetCategory("Context object"); - ExpressionEvaluator evaluatorWithPersonContext = new ExpressionEvaluator(); - - evaluatorWithPersonContext.Context = new Person1() + ExpressionEvaluator evaluatorWithPersonContext = new ExpressionEvaluator { - name = "John", - LastName = "Smith" + Context = new Person1() + { + name = "John", + LastName = "Smith" + } }; yield return new TestCaseData(evaluatorWithPersonContext diff --git a/CodingSeb.ExpressionEvaluator/CodingSeb.ExpressionEvaluator.csproj b/CodingSeb.ExpressionEvaluator/CodingSeb.ExpressionEvaluator.csproj index 635270f..1da540e 100644 --- a/CodingSeb.ExpressionEvaluator/CodingSeb.ExpressionEvaluator.csproj +++ b/CodingSeb.ExpressionEvaluator/CodingSeb.ExpressionEvaluator.csproj @@ -5,9 +5,9 @@ CodingSeb.ExpressionEvaluator A Simple Math and Pseudo C# Expression Evaluator in One C# File. Can also execute small C# like scripts Copyright © Coding Seb 2017 - 1.4.10.0 - 1.4.10.0 - 1.4.10.0 + 1.4.11.0 + 1.4.11.0 + 1.4.11.0 bin\$(Configuration)\ Coding Seb CodingSeb.ExpressionEvaluator @@ -18,7 +18,7 @@ true https://github.com/codingseb/ExpressionEvaluator/blob/master/Icon.png?raw=true false - * Correction of some bugs linked to nullconditional operator + * Correction of Ternary conditional operator bug LICENSE.md https://github.com/codingseb/ExpressionEvaluator diff --git a/CodingSeb.ExpressionEvaluator/ExpressionEvaluator.cs b/CodingSeb.ExpressionEvaluator/ExpressionEvaluator.cs index cb0a5a2..8a14432 100644 --- a/CodingSeb.ExpressionEvaluator/ExpressionEvaluator.cs +++ b/CodingSeb.ExpressionEvaluator/ExpressionEvaluator.cs @@ -1,6 +1,6 @@ /****************************************************************************************************** Title : ExpressionEvaluator (https://github.com/codingseb/ExpressionEvaluator) - Version : 1.4.10.0 + Version : 1.4.11.0 (if last digit (the forth) is not a zero, the version is an intermediate version and can be unstable) Author : Coding Seb @@ -22,7 +22,7 @@ namespace CodingSeb.ExpressionEvaluator { /// - /// This class allow to evaluate a string math or pseudo C# expression + /// This class allow to evaluate a string math or pseudo C# expression /// public partial class ExpressionEvaluator { @@ -653,8 +653,8 @@ public string OptionNumberParsingThousandSeparator public bool OptionFluidPrefixingActive { get; set; } = true; /// - /// if true allow the use of inline namespace (Can be slow, and is less secure). - /// if false unactive inline namespace (only namespaces in Namespaces list are available). + /// if true allow the use of inline namespace (Can be slow, and is less secure). + /// if false unactive inline namespace (only namespaces in Namespaces list are available). /// By default : true /// public bool OptionInlineNamespacesEvaluationActive { get; set; } = true; @@ -744,7 +744,7 @@ public bool OptionNewFunctionEvaluationActive /// /// If true Evaluate function is callables in an expression. If false Evaluate is not callable. - /// By default : true + /// By default : true /// if set to false for security (also ensure that ExpressionEvaluator type is in TypesToBlock list) /// public bool OptionEvaluateFunctionActive { get; set; } = true; @@ -772,7 +772,7 @@ public bool OptionNewFunctionEvaluationActive /// /// If true ScriptEvaluate function is callables in an expression. If false Evaluate is not callable. - /// By default : true + /// By default : true /// if set to false for security (also ensure that ExpressionEvaluator type is in TypesToBlock list) /// public bool OptionScriptEvaluateFunctionActive { get; set; } = true; @@ -1394,7 +1394,7 @@ void forAction(int index) bool executed = false; - if (TryParseStringAndParenthisAndCurlyBrackets(ref i)){} + if (TryParseStringAndParenthisAndCurlyBrackets(ref i)) { } else if (script.Length - i > 2 && script.Substring(i, 3).Equals("';'")) { i += 2; @@ -1454,7 +1454,7 @@ public T Evaluate(string expression) private IList parsingMethods; - protected virtual IList ParsingMethods => parsingMethods ?? (parsingMethods = new List() + protected virtual IList ParsingMethods => parsingMethods ?? (parsingMethods = new List() { EvaluateCast, EvaluateNumber, @@ -1809,7 +1809,7 @@ protected virtual bool EvaluateVarOrFunc(string expression, Stack stack, try { - if(obj is NullConditionalNullValue) + if (obj is NullConditionalNullValue) { stack.Push(obj); } @@ -1851,7 +1851,7 @@ protected virtual bool EvaluateVarOrFunc(string expression, Stack stack, { if (dictionaryObject[varFuncName] is InternalDelegate internalDelegate) stack.Push(internalDelegate(oArgs.ToArray())); - else if(dictionaryObject[varFuncName] is Delegate del) + else if (dictionaryObject[varFuncName] is Delegate del) stack.Push(del.DynamicInvoke(oArgs.ToArray())); } else if (objType.GetProperty(varFuncName, InstanceBindingFlag) is PropertyInfo instancePropertyInfo @@ -2250,7 +2250,7 @@ protected virtual bool EvaluateVarOrFunc(string expression, Stack stack, { if (Variables.ContainsKey(varFuncName) && Variables[varFuncName] is StronglyTypedVariable stronglyTypedVariable) { - if(cusVarValueToPush == null && stronglyTypedVariable.Type.IsValueType && Nullable.GetUnderlyingType(stronglyTypedVariable.Type) == null) + if (cusVarValueToPush == null && stronglyTypedVariable.Type.IsValueType && Nullable.GetUnderlyingType(stronglyTypedVariable.Type) == null) { throw new ExpressionEvaluatorSyntaxErrorException($"Can not cast null to {stronglyTypedVariable.Type} because it's not a nullable valueType"); } @@ -2417,11 +2417,11 @@ protected virtual bool EvaluateOperators(string expression, Stack stack, Match match = Regex.Match(expression.Substring(i), regexPattern, optionCaseSensitiveEvaluationActive ? RegexOptions.None : RegexOptions.IgnoreCase); - if(match.Success) + if (match.Success) { string op = match.Value; stack.Push(operatorsDictionary[op]); - i+= op.Length - 1; + i += op.Length - 1; return true; } @@ -2456,7 +2456,7 @@ protected virtual bool EvaluateTernaryConditionalOperator(string expression, Sta { stack.Clear(); - stack.Push(condition ? Evaluate(restOfExpression.Substring(1, j - 1)) : Evaluate(restOfExpression.Substring(j + 1))); + stack.Push(condition ? Evaluate(restOfExpression.Substring(0, j)) : Evaluate(restOfExpression.Substring(j + 1))); i = expression.Length; @@ -2572,7 +2572,6 @@ protected virtual bool EvaluateIndexing(string expression, Stack stack, dynamic right = Evaluate(innerExp.ToString()); ExpressionOperator op = indexingBeginningMatch.Length == 2 ? ExpressionOperator.IndexingWithNullConditional : ExpressionOperator.Indexing; - if (OptionForceIntegerNumbersEvaluationsAsDoubleByDefault && right is double && Regex.IsMatch(innerExp.ToString(), @"^\d+$")) right = (int)right; @@ -2995,7 +2994,7 @@ protected virtual MethodInfo GetRealMethod(ref Type type, ref object obj, string .MakeGenericMethod(parameterType.GetGenericArguments()); modifiedArgs[a] = Delegate.CreateDelegate(parameterType, de, encapsMethod); } - else if(paramTypeName.StartsWith("Action") + else if (paramTypeName.StartsWith("Action") && modifiedArgs[a] is InternalDelegate) { InternalDelegate led = modifiedArgs[a] as InternalDelegate; @@ -3047,7 +3046,7 @@ protected virtual MethodInfo MakeConcreteMethodIfGeneric(MethodInfo methodInfo, { if (genericsTypes.Equals(string.Empty)) { - if(inferedGenericsTypes != null && inferedGenericsTypes.Length == methodInfo.GetGenericArguments().Length) + if (inferedGenericsTypes != null && inferedGenericsTypes.Length == methodInfo.GetGenericArguments().Length) { return methodInfo.MakeGenericMethod(inferedGenericsTypes); } @@ -3722,7 +3721,7 @@ public override int GetHashCode() public bool Equals(ExpressionOperator otherOperator) { - return otherOperator!= null && OperatorValue == otherOperator.OperatorValue; + return otherOperator != null && OperatorValue == otherOperator.OperatorValue; } } diff --git a/TryWindow/MainWindow.xaml.cs b/TryWindow/MainWindow.xaml.cs index a0dd5cc..8edbfe3 100644 --- a/TryWindow/MainWindow.xaml.cs +++ b/TryWindow/MainWindow.xaml.cs @@ -16,8 +16,8 @@ namespace TryWindow /// public partial class MainWindow : Window { - private string persistCodeFileName = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "code.cs"); - private string persistIterationFileName = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "iterations"); + private readonly string persistCodeFileName = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "code.cs"); + private readonly string persistIterationFileName = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "iterations"); private CancellationTokenSource cancellationTokenSource = null; @@ -88,7 +88,7 @@ private async void CalculateButton_Click(object sender, RoutedEventArgs e) { stopWatch.Stop(); } - }, cancellationTokenSource.Token); + }, cancellationTokenSource.Token).ConfigureAwait(true); if (exception == null) ResultTextBlock.Text = result; @@ -110,7 +110,6 @@ private async void CalculateButton_Click(object sender, RoutedEventArgs e) private void Evaluator_EvaluateFunction(object sender, FunctionEvaluationEventArg e) { - } private void Evaluator_EvaluateVariable(object sender, VariableEvaluationEventArg e) @@ -138,8 +137,7 @@ private void Evaluator_EvaluateVariable(object sender, VariableEvaluationEventAr private void CancelButton_Click(object sender, RoutedEventArgs e) { - if (cancellationTokenSource != null) - cancellationTokenSource.Cancel(); + cancellationTokenSource?.Cancel(); } private void ScriptTextBox_TextChanged(object sender, EventArgs e)