3
3
using Shouldly ;
4
4
using System ;
5
5
using System . Collections . Generic ;
6
+ using System . Linq ;
6
7
using System . Text . RegularExpressions ;
7
8
8
9
namespace CodingSeb . ExpressionEvaluator . Tests
@@ -985,6 +986,12 @@ public void TypeTesting(string expression, Type type)
985
986
986
987
#endregion
987
988
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
+
988
995
#endregion
989
996
public object DirectExpressionEvaluation ( string expression )
990
997
{
@@ -1277,9 +1284,9 @@ private void Evaluator_EvaluateVariable(object sender, VariableEvaluationEventAr
1277
1284
}
1278
1285
}
1279
1286
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 )
1283
1290
{
1284
1291
ExpressionEvaluator evaluator = new ExpressionEvaluator ( new ContextObject1 ( ) ) ;
1285
1292
@@ -1303,6 +1310,27 @@ private void Evaluator_EvaluateParameterCast(object sender, ParameterCastEvaluat
1303
1310
}
1304
1311
}
1305
1312
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
+
1306
1334
#endregion
1307
1335
1308
1336
#endregion
@@ -1492,6 +1520,7 @@ public static IEnumerable<TestCaseData> TestCasesForExceptionThrowingEvaluation
1492
1520
{ "P1var" , "P1" } ,
1493
1521
{ "myObj" , new ClassForTest1 ( ) } ,
1494
1522
{ "nullVar" , null } ,
1523
+ { "myArray" , new int [ ] { 1 , 2 , 3 } } ,
1495
1524
} ) ;
1496
1525
1497
1526
evaluator . PreEvaluateVariable += ( sender , e ) =>
@@ -1506,11 +1535,18 @@ public static IEnumerable<TestCaseData> TestCasesForExceptionThrowingEvaluation
1506
1535
e . CancelEvaluation = true ;
1507
1536
} ;
1508
1537
1538
+ evaluator . PreEvaluateIndexing += ( sender , e ) =>
1539
+ {
1540
+ if ( e . This is int [ ] )
1541
+ e . CancelEvaluation = true ;
1542
+ } ;
1543
+
1509
1544
yield return new TestCaseData ( evaluator , "Pi" , typeof ( ExpressionEvaluatorSyntaxErrorException ) ) . SetCategory ( "OnTheFly canceled Var" ) ;
1510
1545
yield return new TestCaseData ( evaluator , "P1var" , typeof ( ExpressionEvaluatorSyntaxErrorException ) ) . SetCategory ( "OnTheFly canceled Var" ) ;
1511
1546
yield return new TestCaseData ( evaluator , "myObj.PropertyThatWillFailed" , typeof ( ExpressionEvaluatorSyntaxErrorException ) ) . SetCategory ( "OnTheFly canceled Var" ) ;
1512
1547
yield return new TestCaseData ( evaluator , "myObj.Add3To(5)" , typeof ( ExpressionEvaluatorSyntaxErrorException ) ) . SetCategory ( "OnTheFly canceled Func" ) ;
1513
1548
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" ) ;
1514
1550
#endregion
1515
1551
1516
1552
#region Bugs corrections
@@ -1529,7 +1565,20 @@ public static IEnumerable<TestCaseData> TestCasesForExceptionThrowingEvaluation
1529
1565
[ TestCaseSource ( nameof ( TestCasesForExceptionThrowingEvaluation ) ) ]
1530
1566
public void ExceptionThrowingEvaluation ( ExpressionEvaluator evaluator , string expression , Type exceptionType )
1531
1567
{
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 ) ;
1533
1582
}
1534
1583
1535
1584
#endregion
0 commit comments