Skip to content

Commit 3955b0b

Browse files
authored
Merge pull request #54 from codingseb/dev
Dev
2 parents c2f7d2c + 7b82fed commit 3955b0b

File tree

4 files changed

+33
-33
lines changed

4 files changed

+33
-33
lines changed

CodingSeb.ExpressionEvaluator.Tests/ExpressionEvaluatorTests.cs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -617,6 +617,8 @@ public void TypeTesting(string expression, Type type)
617617
[TestCase("Abs(-4) > 10 / 2 ? Abs(-3) : (Abs(-4) + 4) / 2", ExpectedResult = 4, Category = "Conditional Operator t ? x : y")]
618618
[TestCase("Abs(-4) < 10 / 2 ? (true ? 6 : 3+2) : (false ? Abs(-18) : 100 / 2)", ExpectedResult = 6, Category = "Conditional Operator t ? x : y")]
619619
[TestCase("Abs(-4) > 10 / 2 ? (true ? 6 : 3+2) : (false ? Abs(-18) : 100 / 2)", ExpectedResult = 50, Category = "Conditional Operator t ? x : y")]
620+
[TestCase("Abs(-4) > 10 / 2?(true ? 6 : 3+2):(false?Abs(-18):100 / 2)", ExpectedResult = 50, Category = "Conditional Operator t ? x : y")]
621+
[TestCase("1==1?true:false", ExpectedResult = true, Category = "Conditional Operator t ? x : y")]
620622
#endregion
621623

622624
#region Math Constants
@@ -1773,12 +1775,13 @@ void Evaluator_PreEvaluateVariable(object sender, VariablePreEvaluationEventArg
17731775
.Returns("Hello Bob")
17741776
.SetCategory("Context object");
17751777

1776-
ExpressionEvaluator evaluatorWithPersonContext = new ExpressionEvaluator();
1777-
1778-
evaluatorWithPersonContext.Context = new Person1()
1778+
ExpressionEvaluator evaluatorWithPersonContext = new ExpressionEvaluator
17791779
{
1780-
name = "John",
1781-
LastName = "Smith"
1780+
Context = new Person1()
1781+
{
1782+
name = "John",
1783+
LastName = "Smith"
1784+
}
17821785
};
17831786

17841787
yield return new TestCaseData(evaluatorWithPersonContext

CodingSeb.ExpressionEvaluator/CodingSeb.ExpressionEvaluator.csproj

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
<Product>CodingSeb.ExpressionEvaluator</Product>
66
<Description>A Simple Math and Pseudo C# Expression Evaluator in One C# File. Can also execute small C# like scripts</Description>
77
<Copyright>Copyright © Coding Seb 2017</Copyright>
8-
<Version>1.4.10.0</Version>
9-
<AssemblyVersion>1.4.10.0</AssemblyVersion>
10-
<FileVersion>1.4.10.0</FileVersion>
8+
<Version>1.4.11.0</Version>
9+
<AssemblyVersion>1.4.11.0</AssemblyVersion>
10+
<FileVersion>1.4.11.0</FileVersion>
1111
<OutputPath>bin\$(Configuration)\</OutputPath>
1212
<Authors>Coding Seb</Authors>
1313
<PackageId>CodingSeb.ExpressionEvaluator</PackageId>
@@ -18,7 +18,7 @@
1818
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
1919
<PackageIconUrl>https://github.com/codingseb/ExpressionEvaluator/blob/master/Icon.png?raw=true</PackageIconUrl>
2020
<PackageRequireLicenseAcceptance>false</PackageRequireLicenseAcceptance>
21-
<PackageReleaseNotes>* Correction of some bugs linked to nullconditional operator</PackageReleaseNotes>
21+
<PackageReleaseNotes>* Correction of Ternary conditional operator bug</PackageReleaseNotes>
2222
<PackageLicenseFile>LICENSE.md</PackageLicenseFile>
2323
<RepositoryUrl>https://github.com/codingseb/ExpressionEvaluator</RepositoryUrl>
2424
</PropertyGroup>

CodingSeb.ExpressionEvaluator/ExpressionEvaluator.cs

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/******************************************************************************************************
22
Title : ExpressionEvaluator (https://github.com/codingseb/ExpressionEvaluator)
3-
Version : 1.4.10.0
3+
Version : 1.4.11.0
44
(if last digit (the forth) is not a zero, the version is an intermediate version and can be unstable)
55
66
Author : Coding Seb
@@ -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

@@ -2456,7 +2456,7 @@ protected virtual bool EvaluateTernaryConditionalOperator(string expression, Sta
24562456
{
24572457
stack.Clear();
24582458

2459-
stack.Push(condition ? Evaluate(restOfExpression.Substring(1, j - 1)) : Evaluate(restOfExpression.Substring(j + 1)));
2459+
stack.Push(condition ? Evaluate(restOfExpression.Substring(0, j)) : Evaluate(restOfExpression.Substring(j + 1)));
24602460

24612461
i = expression.Length;
24622462

@@ -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)