4
4
using System ;
5
5
using System . Collections ;
6
6
using System . Collections . Generic ;
7
+ using System . Diagnostics ;
7
8
using System . Linq ;
8
9
using System . Linq . Expressions ;
9
10
using System . Reflection ;
@@ -1113,7 +1114,7 @@ public static void TestUserDefinedCoercion<X, Y>()
1113
1114
1114
1115
public static void AssertIsCoercion ( UnaryExpression u , string opName , Type expected )
1115
1116
{
1116
- Console . WriteLine ( "Convert: {0} -> {1}" , u . Operand . Type , u . Type ) ;
1117
+ Debug . WriteLine ( "Convert: {0} -> {1}" , u . Operand . Type , u . Type ) ;
1117
1118
Assert . NotNull ( u . Method ) ;
1118
1119
Assert . Equal ( opName , u . Method . Name ) ;
1119
1120
Assert . Equal ( expected , u . Type ) ;
@@ -2125,37 +2126,31 @@ public static void UnaryPlus()
2125
2126
{
2126
2127
ConstantExpression ce = Expression . Constant ( ( UInt16 ) 10 ) ;
2127
2128
2128
- try
2129
- {
2130
- UnaryExpression result = Expression . UnaryPlus ( ce ) ;
2129
+ UnaryExpression result = Expression . UnaryPlus ( ce ) ;
2131
2130
2131
+ Assert . Throws < InvalidOperationException > ( ( ) =>
2132
+ {
2132
2133
//unary Plus Operator
2133
2134
byte val = 10 ;
2134
2135
Expression < Func < byte > > e =
2135
- Expression . Lambda < Func < byte > > (
2136
- Expression . UnaryPlus ( Expression . Constant ( val , typeof ( byte ) ) ) ,
2137
- Enumerable . Empty < ParameterExpression > ( ) ) ;
2138
- Func < byte > f = e . Compile ( ) ;
2139
- Assert . Equal ( f ( ) , ( val ) ) ;
2140
-
2141
- //Userdefined objects
2142
- Complex comp = new Complex ( 10 , 20 ) ;
2143
- Expression < Func < Complex > > e1 =
2144
- Expression . Lambda < Func < Complex > > (
2145
- Expression . UnaryPlus ( Expression . Constant ( comp , typeof ( Complex ) ) ) ,
2146
- Enumerable . Empty < ParameterExpression > ( ) ) ;
2147
- Func < Complex > f1 = e1 . Compile ( ) ;
2148
- Complex comp1 = f1 ( ) ;
2149
- Assert . True ( ( comp1 . x == comp . x + 1 && comp1 . y == comp . y + 1 ) ) ;
2150
-
2151
- Expression < Func < Complex , Complex > > testExpr = ( x ) => + x ;
2152
- Assert . Equal ( testExpr . ToString ( ) , "x => +x" ) ;
2153
- var v = testExpr . Compile ( ) ;
2154
- }
2155
- catch ( Exception e )
2156
- {
2157
- Console . WriteLine ( e . Message ) ;
2158
- }
2136
+ Expression . Lambda < Func < byte > > (
2137
+ Expression . UnaryPlus ( Expression . Constant ( val , typeof ( byte ) ) ) ,
2138
+ Enumerable . Empty < ParameterExpression > ( ) ) ;
2139
+ } ) ;
2140
+
2141
+ //Userdefined objects
2142
+ Complex comp = new Complex ( 10 , 20 ) ;
2143
+ Expression < Func < Complex > > e1 =
2144
+ Expression . Lambda < Func < Complex > > (
2145
+ Expression . UnaryPlus ( Expression . Constant ( comp , typeof ( Complex ) ) ) ,
2146
+ Enumerable . Empty < ParameterExpression > ( ) ) ;
2147
+ Func < Complex > f1 = e1 . Compile ( ) ;
2148
+ Complex comp1 = f1 ( ) ;
2149
+ Assert . True ( ( comp1 . x == comp . x + 1 && comp1 . y == comp . y + 1 ) ) ;
2150
+
2151
+ Expression < Func < Complex , Complex > > testExpr = ( x ) => + x ;
2152
+ Assert . Equal ( testExpr . ToString ( ) , "x => +x" ) ;
2153
+ var v = testExpr . Compile ( ) ;
2159
2154
}
2160
2155
2161
2156
private struct S
@@ -2766,16 +2761,12 @@ private static S TestConvertChecked<T, S>(T value)
2766
2761
[ Fact ]
2767
2762
public static void ConvertNullToInt ( )
2768
2763
{
2769
- try
2764
+ Assert . Throws < NullReferenceException > ( ( ) =>
2770
2765
{
2771
2766
Expression < Func < ValueType , int > > e = v => ( int ) v ;
2772
2767
Func < ValueType , int > f = e . Compile ( ) ;
2773
- Console . WriteLine ( f ( null ) ) ;
2774
- Assert . False ( true ) ;
2775
- }
2776
- catch ( NullReferenceException )
2777
- {
2778
- }
2768
+ f ( null ) ;
2769
+ } ) ;
2779
2770
}
2780
2771
2781
2772
[ Fact ]
@@ -2817,7 +2808,8 @@ public static void MixedTypeNullableOps()
2817
2808
{
2818
2809
Expression < Func < decimal , int ? , decimal ? > > e = ( d , i ) => d + i ;
2819
2810
var f = e . Compile ( ) ;
2820
- Console . WriteLine ( f ( 1.0m , 4 ) ) ;
2811
+ var result = f ( 1.0m , 4 ) ;
2812
+ Debug . WriteLine ( result ) ;
2821
2813
}
2822
2814
2823
2815
[ Fact ]
@@ -2843,7 +2835,8 @@ public static void AddNullConstants()
2843
2835
Expression . Constant ( 1 , typeof ( int ? ) )
2844
2836
) ) ;
2845
2837
2846
- Console . WriteLine ( f . Compile ( ) ( ) ) ;
2838
+ var result = f . Compile ( ) ( ) ;
2839
+ Debug . WriteLine ( result ) ;
2847
2840
}
2848
2841
2849
2842
[ Fact ]
@@ -2888,7 +2881,7 @@ public static void LiftedAddDateTimeTimeSpan()
2888
2881
{
2889
2882
Expression < Func < DateTime ? , TimeSpan , DateTime ? > > f = ( x , y ) => x + y ;
2890
2883
Assert . Equal ( ExpressionType . Add , f . Body . NodeType ) ;
2891
- Console . WriteLine ( f ) ;
2884
+ Debug . WriteLine ( f ) ;
2892
2885
Func < DateTime ? , TimeSpan , DateTime ? > d = f . Compile ( ) ;
2893
2886
DateTime ? dt = DateTime . Now ;
2894
2887
TimeSpan ts = new TimeSpan ( 3 , 2 , 1 ) ;
@@ -2902,7 +2895,7 @@ public static void LiftedAddDateTimeTimeSpan2()
2902
2895
{
2903
2896
Expression < Func < DateTime ? , TimeSpan ? , DateTime ? > > f = ( x , y ) => x + y ;
2904
2897
Assert . Equal ( ExpressionType . Add , f . Body . NodeType ) ;
2905
- Console . WriteLine ( f ) ;
2898
+ Debug . WriteLine ( f ) ;
2906
2899
Func < DateTime ? , TimeSpan ? , DateTime ? > d = f . Compile ( ) ;
2907
2900
DateTime ? dt = DateTime . Now ;
2908
2901
TimeSpan ? ts = new TimeSpan ( 3 , 2 , 1 ) ;
@@ -2918,7 +2911,7 @@ public static void LiftedSubDateTime()
2918
2911
{
2919
2912
Expression < Func < DateTime ? , DateTime ? , TimeSpan ? > > f = ( x , y ) => x - y ;
2920
2913
Assert . Equal ( ExpressionType . Subtract , f . Body . NodeType ) ;
2921
- Console . WriteLine ( f ) ;
2914
+ Debug . WriteLine ( f ) ;
2922
2915
Func < DateTime ? , DateTime ? , TimeSpan ? > d = f . Compile ( ) ;
2923
2916
DateTime ? dt1 = DateTime . Now ;
2924
2917
DateTime ? dt2 = new DateTime ( 2006 , 5 , 1 ) ;
@@ -2934,7 +2927,7 @@ public static void LiftedEqualDateTime()
2934
2927
{
2935
2928
Expression < Func < DateTime ? , DateTime ? , bool > > f = ( x , y ) => x == y ;
2936
2929
Assert . Equal ( ExpressionType . Equal , f . Body . NodeType ) ;
2937
- Console . WriteLine ( f ) ;
2930
+ Debug . WriteLine ( f ) ;
2938
2931
Func < DateTime ? , DateTime ? , bool > d = f . Compile ( ) ;
2939
2932
DateTime ? dt1 = DateTime . Now ;
2940
2933
DateTime ? dt2 = new DateTime ( 2006 , 5 , 1 ) ;
@@ -2950,7 +2943,7 @@ public static void LiftedNotEqualDateTime()
2950
2943
{
2951
2944
Expression < Func < DateTime ? , DateTime ? , bool > > f = ( x , y ) => x != y ;
2952
2945
Assert . Equal ( ExpressionType . NotEqual , f . Body . NodeType ) ;
2953
- Console . WriteLine ( f ) ;
2946
+ Debug . WriteLine ( f ) ;
2954
2947
Func < DateTime ? , DateTime ? , bool > d = f . Compile ( ) ;
2955
2948
DateTime ? dt1 = DateTime . Now ;
2956
2949
DateTime ? dt2 = new DateTime ( 2006 , 5 , 1 ) ;
@@ -2966,7 +2959,7 @@ public static void LiftedLessThanDateTime()
2966
2959
{
2967
2960
Expression < Func < DateTime ? , DateTime ? , bool > > f = ( x , y ) => x < y ;
2968
2961
Assert . Equal ( ExpressionType . LessThan , f . Body . NodeType ) ;
2969
- Console . WriteLine ( f ) ;
2962
+ Debug . WriteLine ( f ) ;
2970
2963
Func < DateTime ? , DateTime ? , bool > d = f . Compile ( ) ;
2971
2964
DateTime ? dt1 = DateTime . Now ;
2972
2965
DateTime ? dt2 = new DateTime ( 2006 , 5 , 1 ) ;
@@ -2982,7 +2975,7 @@ public static void LessThanDateTime()
2982
2975
{
2983
2976
Expression < Func < DateTime , DateTime , bool > > f = ( x , y ) => x < y ;
2984
2977
Assert . Equal ( ExpressionType . LessThan , f . Body . NodeType ) ;
2985
- Console . WriteLine ( f ) ;
2978
+ Debug . WriteLine ( f ) ;
2986
2979
Func < DateTime , DateTime , bool > d = f . Compile ( ) ;
2987
2980
DateTime dt1 = DateTime . Now ;
2988
2981
DateTime dt2 = new DateTime ( 2006 , 5 , 1 ) ;
@@ -3611,7 +3604,7 @@ private static R TestUnary<T, R>(ExpressionType op, T v)
3611
3604
[ Fact ]
3612
3605
public static void ShiftULong ( )
3613
3606
{
3614
- try
3607
+ Assert . Throws < InvalidOperationException > ( ( ) =>
3615
3608
{
3616
3609
Expression < Func < ulong > > e =
3617
3610
Expression . Lambda < Func < ulong > > (
@@ -3620,49 +3613,37 @@ public static void ShiftULong()
3620
3613
Expression . Constant ( ( ulong ) 1 , typeof ( ulong ) ) ) ,
3621
3614
Enumerable . Empty < ParameterExpression > ( ) ) ;
3622
3615
Func < ulong > f = e . Compile ( ) ;
3623
- Console . WriteLine ( f ( ) ) ;
3624
- Assert . False ( true ) ;
3625
- }
3626
- catch ( InvalidOperationException )
3627
- {
3628
- }
3616
+ f ( ) ;
3617
+ } ) ;
3629
3618
}
3630
3619
3631
3620
[ Fact ]
3632
3621
public static void MultiplyMinInt ( )
3633
3622
{
3634
- try
3623
+ Assert . Throws < OverflowException > ( ( ) =>
3635
3624
{
3636
3625
Func < long > f = Expression . Lambda < Func < long > > (
3637
3626
Expression . MultiplyChecked (
3638
3627
Expression . Constant ( ( long ) - 1 , typeof ( long ) ) ,
3639
3628
Expression . Constant ( long . MinValue , typeof ( long ) ) ) ,
3640
3629
Enumerable . Empty < ParameterExpression > ( )
3641
3630
) . Compile ( ) ;
3642
- Console . WriteLine ( f ( ) ) ;
3643
- Assert . False ( true ) ;
3644
- }
3645
- catch ( OverflowException )
3646
- {
3647
- }
3631
+ f ( ) ;
3632
+ } ) ;
3648
3633
}
3649
3634
3650
3635
[ Fact ]
3651
3636
public static void MultiplyMinInt2 ( )
3652
3637
{
3653
- try
3638
+ Assert . Throws < OverflowException > ( ( ) =>
3654
3639
{
3655
3640
Func < long > f = Expression . Lambda < Func < long > > (
3656
3641
Expression . MultiplyChecked (
3657
3642
Expression . Constant ( long . MinValue , typeof ( long ) ) ,
3658
3643
Expression . Constant ( ( long ) - 1 , typeof ( long ) ) ) ,
3659
3644
Enumerable . Empty < ParameterExpression > ( ) ) . Compile ( ) ;
3660
- Console . WriteLine ( f ( ) ) ;
3661
- Assert . False ( true ) ;
3662
- }
3663
- catch ( OverflowException )
3664
- {
3665
- }
3645
+ f ( ) ;
3646
+ } ) ;
3666
3647
}
3667
3648
3668
3649
[ Fact ]
0 commit comments