1
- using NUnit . Framework ;
1
+ using Newtonsoft . Json ;
2
+ using NUnit . Framework ;
3
+ using Shouldly ;
2
4
using System ;
3
5
using System . Collections . Generic ;
4
6
using System . Text . RegularExpressions ;
5
- using Shouldly ;
6
- using Newtonsoft . Json ;
7
7
8
8
namespace CodingSeb . ExpressionEvaluator . Tests
9
9
{
@@ -91,6 +91,23 @@ public void TypeTesting(string expression, Type type)
91
91
92
92
#region Test cases for DirectExpressionEvaluation
93
93
94
+ #region Other bases numbers
95
+
96
+ [ TestCase ( "0xab" , ExpectedResult = 0xab , Category = "HexNumber" ) ]
97
+ [ TestCase ( "0xAB" , ExpectedResult = 0xab , Category = "HexNumber" ) ]
98
+ [ TestCase ( "0x1" , ExpectedResult = 0x1 , Category = "HexNumber" ) ]
99
+ [ TestCase ( "0xf" , ExpectedResult = 0xf , Category = "HexNumber" ) ]
100
+ [ TestCase ( "-0xf" , ExpectedResult = - 0xf , Category = "HexNumber" ) ]
101
+ [ TestCase ( "0xff_2a" , ExpectedResult = 0xff_2a , Category = "HexNumber" ) ]
102
+
103
+ [ TestCase ( "0b01100111" , ExpectedResult = 0b01100111 , Category = "BinaryNumber" ) ]
104
+ [ TestCase ( "0b0100" , ExpectedResult = 0b0100 , Category = "BinaryNumber" ) ]
105
+ [ TestCase ( "0b1010" , ExpectedResult = 0b1010 , Category = "BinaryNumber" ) ]
106
+ [ TestCase ( "0b10_10" , ExpectedResult = 0b10_10 , Category = "BinaryNumber" ) ]
107
+ [ TestCase ( "-0b10_10" , ExpectedResult = - 0b10_10 , Category = "BinaryNumber" ) ]
108
+
109
+ #endregion
110
+
94
111
#region Null Expression
95
112
[ TestCase ( "null" , ExpectedResult = null , Category = "Null Expression" ) ]
96
113
#endregion
@@ -391,6 +408,16 @@ public void TypeTesting(string expression, Type type)
391
408
[ TestCase ( "3 --(2 *+(5 - 3 - +(-Abs(-5) - 6)))" , TestOf = typeof ( double ) , ExpectedResult = 29 , Category = "ParenthesisPriority" ) ]
392
409
#endregion
393
410
411
+ #region BitwiseComplement
412
+ [ TestCase ( "~-10" , TestOf = typeof ( int ) , ExpectedResult = 9 , Category = "BitwiseComplement" ) ]
413
+ [ TestCase ( "~-2" , TestOf = typeof ( int ) , ExpectedResult = 1 , Category = "BitwiseComplement" ) ]
414
+ [ TestCase ( "~-1" , TestOf = typeof ( int ) , ExpectedResult = 0 , Category = "BitwiseComplement" ) ]
415
+ [ TestCase ( "~0" , TestOf = typeof ( int ) , ExpectedResult = - 1 , Category = "BitwiseComplement" ) ]
416
+ [ TestCase ( "~1" , TestOf = typeof ( int ) , ExpectedResult = - 2 , Category = "BitwiseComplement" ) ]
417
+ [ TestCase ( "~2" , TestOf = typeof ( int ) , ExpectedResult = - 3 , Category = "BitwiseComplement" ) ]
418
+ [ TestCase ( "~10" , TestOf = typeof ( int ) , ExpectedResult = - 11 , Category = "BitwiseComplement" ) ]
419
+ #endregion
420
+
394
421
#region SimpleModulo
395
422
[ TestCase ( "-4 % 2" , TestOf = typeof ( int ) , ExpectedResult = 0 , Category = "SimpleModulo" ) ]
396
423
[ TestCase ( "-3 % 2" , TestOf = typeof ( int ) , ExpectedResult = - 1 , Category = "SimpleModulo" ) ]
@@ -490,6 +517,24 @@ public void TypeTesting(string expression, Type type)
490
517
[ TestCase ( "typeof(string) == 12.GetType()" , ExpectedResult = false , Category = "typeof keyword" ) ]
491
518
#endregion
492
519
520
+ #region sizeof keyword
521
+
522
+ [ TestCase ( "sizeof(sbyte)" , ExpectedResult = sizeof ( sbyte ) , Category = "sizeof keyword" ) ]
523
+ [ TestCase ( "sizeof(byte)" , ExpectedResult = sizeof ( byte ) , Category = "sizeof keyword" ) ]
524
+ [ TestCase ( "sizeof(short)" , ExpectedResult = sizeof ( short ) , Category = "sizeof keyword" ) ]
525
+ [ TestCase ( "sizeof(ushort)" , ExpectedResult = sizeof ( ushort ) , Category = "sizeof keyword" ) ]
526
+ [ TestCase ( "sizeof(int)" , ExpectedResult = sizeof ( int ) , Category = "sizeof keyword" ) ]
527
+ [ TestCase ( "sizeof(uint)" , ExpectedResult = sizeof ( uint ) , Category = "sizeof keyword" ) ]
528
+ [ TestCase ( "sizeof(long)" , ExpectedResult = sizeof ( long ) , Category = "sizeof keyword" ) ]
529
+ [ TestCase ( "sizeof(ulong)" , ExpectedResult = sizeof ( ulong ) , Category = "sizeof keyword" ) ]
530
+ [ TestCase ( "sizeof(char)" , ExpectedResult = sizeof ( char ) , Category = "sizeof keyword" ) ]
531
+ [ TestCase ( "sizeof(float)" , ExpectedResult = sizeof ( float ) , Category = "sizeof keyword" ) ]
532
+ [ TestCase ( "sizeof(double)" , ExpectedResult = sizeof ( double ) , Category = "sizeof keyword" ) ]
533
+ [ TestCase ( "sizeof(decimal)" , ExpectedResult = sizeof ( decimal ) , Category = "sizeof keyword" ) ]
534
+ [ TestCase ( "sizeof(bool)" , ExpectedResult = sizeof ( bool ) , Category = "sizeof keyword" ) ]
535
+
536
+ #endregion
537
+
493
538
#region Create instance with new Keyword
494
539
[ TestCase ( "new ClassForTest1().GetType()" , ExpectedResult = typeof ( ClassForTest1 ) , Category = "Create instance with new Keyword" ) ]
495
540
[ TestCase ( "new ClassForTest2(15).GetType()" , ExpectedResult = typeof ( ClassForTest2 ) , Category = "Create instance with new Keyword" ) ]
@@ -970,9 +1015,7 @@ public static IEnumerable<TestCaseData> TestCasesForWithCustomVariablesExpressio
970
1015
971
1016
yield return new TestCaseData ( "+x" , variablesForSimpleVariablesInjection , true ) . SetCategory ( "SimpleVariablesInjection,Unary +" ) . Returns ( 5 ) ;
972
1017
yield return new TestCaseData ( "-5 + +x" , variablesForSimpleVariablesInjection , true ) . SetCategory ( "SimpleVariablesInjection,Unary +" ) . Returns ( 0 ) ;
973
- yield return new TestCaseData ( "-5++x" , variablesForSimpleVariablesInjection , true ) . SetCategory ( "SimpleVariablesInjection,Unary +" ) . Returns ( 0 ) ;
974
1018
yield return new TestCaseData ( "5 + +x" , variablesForSimpleVariablesInjection , true ) . SetCategory ( "SimpleVariablesInjection,Unary +" ) . Returns ( 10 ) ;
975
- yield return new TestCaseData ( "5++x" , variablesForSimpleVariablesInjection , true ) . SetCategory ( "SimpleVariablesInjection,Unary +" ) . Returns ( 10 ) ;
976
1019
yield return new TestCaseData ( "5 - +x" , variablesForSimpleVariablesInjection , true ) . SetCategory ( "SimpleVariablesInjection,Unary +" ) . Returns ( 0 ) ;
977
1020
yield return new TestCaseData ( "5-+x" , variablesForSimpleVariablesInjection , true ) . SetCategory ( "SimpleVariablesInjection,Unary +" ) . Returns ( 0 ) ;
978
1021
yield return new TestCaseData ( "-5 - +x" , variablesForSimpleVariablesInjection , true ) . SetCategory ( "SimpleVariablesInjection,Unary +" ) . Returns ( - 10 ) ;
@@ -985,23 +1028,15 @@ public static IEnumerable<TestCaseData> TestCasesForWithCustomVariablesExpressio
985
1028
yield return new TestCaseData ( "5 + -x" , variablesForSimpleVariablesInjection , true ) . SetCategory ( "SimpleVariablesInjection,Unary -" ) . Returns ( 0 ) ;
986
1029
yield return new TestCaseData ( "5+-x" , variablesForSimpleVariablesInjection , true ) . SetCategory ( "SimpleVariablesInjection,Unary -" ) . Returns ( 0 ) ;
987
1030
yield return new TestCaseData ( "-5 - -x" , variablesForSimpleVariablesInjection , true ) . SetCategory ( "SimpleVariablesInjection,Unary -" ) . Returns ( 0 ) ;
988
- yield return new TestCaseData ( "-5--x" , variablesForSimpleVariablesInjection , true ) . SetCategory ( "SimpleVariablesInjection,Unary -" ) . Returns ( 0 ) ;
989
1031
yield return new TestCaseData ( "5 - -x" , variablesForSimpleVariablesInjection , true ) . SetCategory ( "SimpleVariablesInjection,Unary -" ) . Returns ( 10 ) ;
990
- yield return new TestCaseData ( "5--x" , variablesForSimpleVariablesInjection , true ) . SetCategory ( "SimpleVariablesInjection,Unary -" ) . Returns ( 10 ) ;
991
1032
yield return new TestCaseData ( "-x - -y" , variablesForSimpleVariablesInjection , true ) . SetCategory ( "SimpleVariablesInjection,Unary -" ) . Returns ( 15 ) ;
992
- yield return new TestCaseData ( "-x--y" , variablesForSimpleVariablesInjection , true ) . SetCategory ( "SimpleVariablesInjection,Unary -" ) . Returns ( 15 ) ;
993
1033
yield return new TestCaseData ( "+x - -y" , variablesForSimpleVariablesInjection , true ) . SetCategory ( "SimpleVariablesInjection,Unary both +-" ) . Returns ( 25 ) ;
994
- yield return new TestCaseData ( "+x--y" , variablesForSimpleVariablesInjection , true ) . SetCategory ( "SimpleVariablesInjection,Unary both +-" ) . Returns ( 25 ) ;
995
1034
yield return new TestCaseData ( "+x + -y" , variablesForSimpleVariablesInjection , true ) . SetCategory ( "SimpleVariablesInjection,Unary both +-" ) . Returns ( - 15 ) ;
996
1035
yield return new TestCaseData ( "+x+-y" , variablesForSimpleVariablesInjection , true ) . SetCategory ( "SimpleVariablesInjection,Unary both +-" ) . Returns ( - 15 ) ;
997
1036
yield return new TestCaseData ( "-x - +y" , variablesForSimpleVariablesInjection , true ) . SetCategory ( "SimpleVariablesInjection,Unary both +-" ) . Returns ( - 25 ) ;
998
1037
yield return new TestCaseData ( "-x-+y" , variablesForSimpleVariablesInjection , true ) . SetCategory ( "SimpleVariablesInjection,Unary both +-" ) . Returns ( - 25 ) ;
999
1038
yield return new TestCaseData ( "-x + +y" , variablesForSimpleVariablesInjection , true ) . SetCategory ( "SimpleVariablesInjection,Unary both +-" ) . Returns ( 15 ) ;
1000
- yield return new TestCaseData ( "-x++y" , variablesForSimpleVariablesInjection , true ) . SetCategory ( "SimpleVariablesInjection,Unary both +-" ) . Returns ( 15 ) ;
1001
- yield return new TestCaseData ( "-x++y" , variablesForSimpleVariablesInjection , true ) . SetCategory ( "SimpleVariablesInjection,Unary both +-" ) . Returns ( 15 ) ;
1002
1039
yield return new TestCaseData ( "(-x + +y)" , variablesForSimpleVariablesInjection , true ) . SetCategory ( "SimpleVariablesInjection,Unary both +-,Parenthis" ) . Returns ( 15 ) ;
1003
- yield return new TestCaseData ( "(-x++y)" , variablesForSimpleVariablesInjection , true ) . SetCategory ( "SimpleVariablesInjection,Unary both +-,Parenthis" ) . Returns ( 15 ) ;
1004
- yield return new TestCaseData ( "-(-x++y)" , variablesForSimpleVariablesInjection , true ) . SetCategory ( "SimpleVariablesInjection,Unary both +-,Parenthis" ) . Returns ( - 15 ) ;
1005
1040
1006
1041
yield return new TestCaseData ( "ISTHISREAL" , variablesForSimpleVariablesInjection , false ) . SetCategory ( "SimpleVariablesInjection,IgnoreCase" ) . Returns ( true ) . SetCategory ( "Options, OptionCaseSensitiveEvaluationActive" ) ;
1007
1042
yield return new TestCaseData ( "isthisreal" , variablesForSimpleVariablesInjection , false ) . SetCategory ( "SimpleVariablesInjection,IgnoreCase" ) . Returns ( true ) . SetCategory ( "Options, OptionCaseSensitiveEvaluationActive" ) ;
@@ -1367,5 +1402,88 @@ public void ExceptionThrowingEvaluation(ExpressionEvaluator evaluator, string ex
1367
1402
}
1368
1403
1369
1404
#endregion
1405
+
1406
+ #region EvaluateWithSpecificEvaluator
1407
+
1408
+ #region TestCasesEvaluateWithSpecificEvaluator
1409
+
1410
+ public static IEnumerable < TestCaseData > TestCasesEvaluateWithSpecificEvaluator
1411
+ {
1412
+ get
1413
+ {
1414
+ #region Different culture for numbers
1415
+
1416
+ yield return new TestCaseData ( new ExpressionEvaluator
1417
+ {
1418
+ OptionNumberParsingDecimalSeparator = "," ,
1419
+ }
1420
+ , "0,5" )
1421
+ . Returns ( 0.5 )
1422
+ . SetCategory ( "Options" )
1423
+ . SetCategory ( "Numbers Culture" ) ;
1424
+
1425
+ yield return new TestCaseData ( new ExpressionEvaluator
1426
+ {
1427
+ OptionNumberParsingDecimalSeparator = "'" ,
1428
+ }
1429
+ , "0'5" )
1430
+ . Returns ( 0.5 )
1431
+ . SetCategory ( "Options" )
1432
+ . SetCategory ( "Numbers Culture" ) ;
1433
+
1434
+ yield return new TestCaseData ( new ExpressionEvaluator
1435
+ {
1436
+ OptionNumberParsingDecimalSeparator = "." ,
1437
+ }
1438
+ , "0.5" )
1439
+ . Returns ( 0.5 )
1440
+ . SetCategory ( "Options" )
1441
+ . SetCategory ( "Numbers Culture" ) ;
1442
+
1443
+ yield return new TestCaseData ( new ExpressionEvaluator
1444
+ {
1445
+ OptionNumberParsingDecimalSeparator = "," ,
1446
+ OptionFunctionArgumentsSeparator = ";"
1447
+ }
1448
+ , "Max(0,5; 0,7)" )
1449
+ . Returns ( 0.7 )
1450
+ . SetCategory ( "Options" )
1451
+ . SetCategory ( "Numbers Culture" ) ;
1452
+
1453
+ yield return new TestCaseData ( new ExpressionEvaluator
1454
+ {
1455
+ OptionNumberParsingDecimalSeparator = "," ,
1456
+ OptionNumberParsingThousandSeparator = "'" ,
1457
+ OptionFunctionArgumentsSeparator = ";"
1458
+ }
1459
+ , "Max(1'200,5; 1'111'000,7)" )
1460
+ . Returns ( 1111000.7 )
1461
+ . SetCategory ( "Options" )
1462
+ . SetCategory ( "Numbers Culture" ) ;
1463
+
1464
+ yield return new TestCaseData ( new ExpressionEvaluator
1465
+ {
1466
+ OptionNumberParsingDecimalSeparator = "," ,
1467
+ OptionNumberParsingThousandSeparator = "'" ,
1468
+ OptionInitializersSeparator = ";"
1469
+ }
1470
+ , "new double[]{1'200,5; 1'111'000,7}.Max()" )
1471
+ . Returns ( 1111000.7 )
1472
+ . SetCategory ( "Options" )
1473
+ . SetCategory ( "Numbers Culture" ) ;
1474
+
1475
+ #endregion
1476
+ }
1477
+ }
1478
+
1479
+ #endregion
1480
+
1481
+ [ TestCaseSource ( nameof ( TestCasesEvaluateWithSpecificEvaluator ) ) ]
1482
+ public object EvaluateWithSpecificEvaluator ( ExpressionEvaluator evaluator , string expression )
1483
+ {
1484
+ return evaluator . Evaluate ( expression ) ;
1485
+ }
1486
+
1487
+ #endregion
1370
1488
}
1371
1489
}
0 commit comments