33using Shouldly ;
44using System ;
55using System . Collections . Generic ;
6+ using System . Linq ;
67using System . Text . RegularExpressions ;
78
89namespace CodingSeb . ExpressionEvaluator . Tests
@@ -985,6 +986,12 @@ public void TypeTesting(string expression, Type type)
985986
986987 #endregion
987988
989+ #region Bugs correction
990+
991+ [ TestCase ( "new DateTime(1985,9,11).ToString(\" dd.MM.yyyy\" )" , ExpectedResult = "11.09.1985" , Category = "Complex expression,Static method,Instance method,Lambda function,Cast" ) ]
992+
993+ #endregion
994+
988995 #endregion
989996 public object DirectExpressionEvaluation ( string expression )
990997 {
@@ -1277,9 +1284,9 @@ private void Evaluator_EvaluateVariable(object sender, VariableEvaluationEventAr
12771284 }
12781285 }
12791286
1280- [ TestCase ( "ClassForTest1.Add(1, 5)" , ExpectedResult = 6 , Category = "On the fly method" ) ]
1281- [ TestCase ( "ClassForTest1.Add(1, 5.0)" , ExpectedResult = 6 , Category = "On the fly method" ) ]
1282- public object OnTheFlyEvaluation2 ( string expression )
1287+ [ TestCase ( "ClassForTest1.Add(1, 5)" , ExpectedResult = 6 ) ]
1288+ [ TestCase ( "ClassForTest1.Add(1, 5.0)" , ExpectedResult = 6 ) ]
1289+ public object OnTheFlyCastEvaluation ( string expression )
12831290 {
12841291 ExpressionEvaluator evaluator = new ExpressionEvaluator ( new ContextObject1 ( ) ) ;
12851292
@@ -1303,6 +1310,27 @@ private void Evaluator_EvaluateParameterCast(object sender, ParameterCastEvaluat
13031310 }
13041311 }
13051312
1313+ [ TestCase ( "2[\" Test\" ]" , ExpectedResult = "Test,Test" ) ]
1314+ [ TestCase ( "3[\" Hello\" ]" , ExpectedResult = "Hello,Hello,Hello" ) ]
1315+ public object OnTheFlyIndexingEvaluation ( string expression )
1316+ {
1317+ ExpressionEvaluator evaluator = new ExpressionEvaluator ( new ContextObject1 ( ) ) ;
1318+
1319+ evaluator . PreEvaluateIndexing += Evaluator_PreEvaluateIndexing ;
1320+
1321+ evaluator . Namespaces . Add ( "CodingSeb.ExpressionEvaluator.Tests" ) ;
1322+
1323+ return evaluator . Evaluate ( expression ) ;
1324+ }
1325+
1326+ private void Evaluator_PreEvaluateIndexing ( object sender , IndexingPreEvaluationEventArg e )
1327+ {
1328+ if ( e . This is int intValue && e . EvaluateArg ( ) is string text )
1329+ {
1330+ e . Value = string . Join ( "," , Enumerable . Repeat ( text , intValue ) ) ;
1331+ }
1332+ }
1333+
13061334 #endregion
13071335
13081336 #endregion
@@ -1492,6 +1520,7 @@ public static IEnumerable<TestCaseData> TestCasesForExceptionThrowingEvaluation
14921520 { "P1var" , "P1" } ,
14931521 { "myObj" , new ClassForTest1 ( ) } ,
14941522 { "nullVar" , null } ,
1523+ { "myArray" , new int [ ] { 1 , 2 , 3 } } ,
14951524 } ) ;
14961525
14971526 evaluator . PreEvaluateVariable += ( sender , e ) =>
@@ -1506,11 +1535,18 @@ public static IEnumerable<TestCaseData> TestCasesForExceptionThrowingEvaluation
15061535 e . CancelEvaluation = true ;
15071536 } ;
15081537
1538+ evaluator . PreEvaluateIndexing += ( sender , e ) =>
1539+ {
1540+ if ( e . This is int [ ] )
1541+ e . CancelEvaluation = true ;
1542+ } ;
1543+
15091544 yield return new TestCaseData ( evaluator , "Pi" , typeof ( ExpressionEvaluatorSyntaxErrorException ) ) . SetCategory ( "OnTheFly canceled Var" ) ;
15101545 yield return new TestCaseData ( evaluator , "P1var" , typeof ( ExpressionEvaluatorSyntaxErrorException ) ) . SetCategory ( "OnTheFly canceled Var" ) ;
15111546 yield return new TestCaseData ( evaluator , "myObj.PropertyThatWillFailed" , typeof ( ExpressionEvaluatorSyntaxErrorException ) ) . SetCategory ( "OnTheFly canceled Var" ) ;
15121547 yield return new TestCaseData ( evaluator , "myObj.Add3To(5)" , typeof ( ExpressionEvaluatorSyntaxErrorException ) ) . SetCategory ( "OnTheFly canceled Func" ) ;
15131548 yield return new TestCaseData ( evaluator , "Abs(-5)" , typeof ( ExpressionEvaluatorSyntaxErrorException ) ) . SetCategory ( "OnTheFly canceled Func" ) ;
1549+ yield return new TestCaseData ( evaluator , "myArray[1]" , typeof ( ExpressionEvaluatorSyntaxErrorException ) ) . SetCategory ( "OnTheFlyCanceledIndexing" ) ;
15141550 #endregion
15151551
15161552 #region Bugs corrections
@@ -1529,7 +1565,20 @@ public static IEnumerable<TestCaseData> TestCasesForExceptionThrowingEvaluation
15291565 [ TestCaseSource ( nameof ( TestCasesForExceptionThrowingEvaluation ) ) ]
15301566 public void ExceptionThrowingEvaluation ( ExpressionEvaluator evaluator , string expression , Type exceptionType )
15311567 {
1532- Assert . Catch ( exceptionType , ( ) => evaluator . Evaluate ( expression ) ) ;
1568+ Exception e = null ;
1569+ object result = null ;
1570+
1571+ try
1572+ {
1573+ result = evaluator . Evaluate ( expression ) ;
1574+ }
1575+ catch ( Exception exception )
1576+ {
1577+ e = exception ;
1578+ }
1579+
1580+ result . ShouldBeNull ( ) ;
1581+ e . ShouldNotBeNull ( ) . ShouldBeOfType ( exceptionType ) ;
15331582 }
15341583
15351584 #endregion
0 commit comments