Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Commit a9423ae

Browse files
committed
Remove Console.WriteLine spew from System.Linq.Expressions.Tests
The System.Linq.Expressions tests have started doing Console.WriteLines during testing, resulting in unnecessary output to the console and CI logs. In some places I've converted them to be Debug.WriteLines. In other cases they highlighted places where Assert.Throws should be used.
1 parent cd2f1cc commit a9423ae

File tree

1 file changed

+46
-65
lines changed

1 file changed

+46
-65
lines changed

src/System.Linq.Expressions/tests/SequenceTests/SequenceTests.cs

Lines changed: 46 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System;
55
using System.Collections;
66
using System.Collections.Generic;
7+
using System.Diagnostics;
78
using System.Linq;
89
using System.Linq.Expressions;
910
using System.Reflection;
@@ -1113,7 +1114,7 @@ public static void TestUserDefinedCoercion<X, Y>()
11131114

11141115
public static void AssertIsCoercion(UnaryExpression u, string opName, Type expected)
11151116
{
1116-
Console.WriteLine("Convert: {0} -> {1}", u.Operand.Type, u.Type);
1117+
Debug.WriteLine("Convert: {0} -> {1}", u.Operand.Type, u.Type);
11171118
Assert.NotNull(u.Method);
11181119
Assert.Equal(opName, u.Method.Name);
11191120
Assert.Equal(expected, u.Type);
@@ -2125,37 +2126,31 @@ public static void UnaryPlus()
21252126
{
21262127
ConstantExpression ce = Expression.Constant((UInt16)10);
21272128

2128-
try
2129-
{
2130-
UnaryExpression result = Expression.UnaryPlus(ce);
2129+
UnaryExpression result = Expression.UnaryPlus(ce);
21312130

2131+
Assert.Throws<InvalidOperationException>(() =>
2132+
{
21322133
//unary Plus Operator
21332134
byte val = 10;
21342135
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();
21592154
}
21602155

21612156
private struct S
@@ -2766,16 +2761,12 @@ private static S TestConvertChecked<T, S>(T value)
27662761
[Fact]
27672762
public static void ConvertNullToInt()
27682763
{
2769-
try
2764+
Assert.Throws<NullReferenceException>(() =>
27702765
{
27712766
Expression<Func<ValueType, int>> e = v => (int)v;
27722767
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+
});
27792770
}
27802771

27812772
[Fact]
@@ -2817,7 +2808,8 @@ public static void MixedTypeNullableOps()
28172808
{
28182809
Expression<Func<decimal, int?, decimal?>> e = (d, i) => d + i;
28192810
var f = e.Compile();
2820-
Console.WriteLine(f(1.0m, 4));
2811+
var result = f(1.0m, 4);
2812+
Debug.WriteLine(result);
28212813
}
28222814

28232815
[Fact]
@@ -2843,7 +2835,8 @@ public static void AddNullConstants()
28432835
Expression.Constant(1, typeof(int?))
28442836
));
28452837

2846-
Console.WriteLine(f.Compile()());
2838+
var result = f.Compile()();
2839+
Debug.WriteLine(result);
28472840
}
28482841

28492842
[Fact]
@@ -2888,7 +2881,7 @@ public static void LiftedAddDateTimeTimeSpan()
28882881
{
28892882
Expression<Func<DateTime?, TimeSpan, DateTime?>> f = (x, y) => x + y;
28902883
Assert.Equal(ExpressionType.Add, f.Body.NodeType);
2891-
Console.WriteLine(f);
2884+
Debug.WriteLine(f);
28922885
Func<DateTime?, TimeSpan, DateTime?> d = f.Compile();
28932886
DateTime? dt = DateTime.Now;
28942887
TimeSpan ts = new TimeSpan(3, 2, 1);
@@ -2902,7 +2895,7 @@ public static void LiftedAddDateTimeTimeSpan2()
29022895
{
29032896
Expression<Func<DateTime?, TimeSpan?, DateTime?>> f = (x, y) => x + y;
29042897
Assert.Equal(ExpressionType.Add, f.Body.NodeType);
2905-
Console.WriteLine(f);
2898+
Debug.WriteLine(f);
29062899
Func<DateTime?, TimeSpan?, DateTime?> d = f.Compile();
29072900
DateTime? dt = DateTime.Now;
29082901
TimeSpan? ts = new TimeSpan(3, 2, 1);
@@ -2918,7 +2911,7 @@ public static void LiftedSubDateTime()
29182911
{
29192912
Expression<Func<DateTime?, DateTime?, TimeSpan?>> f = (x, y) => x - y;
29202913
Assert.Equal(ExpressionType.Subtract, f.Body.NodeType);
2921-
Console.WriteLine(f);
2914+
Debug.WriteLine(f);
29222915
Func<DateTime?, DateTime?, TimeSpan?> d = f.Compile();
29232916
DateTime? dt1 = DateTime.Now;
29242917
DateTime? dt2 = new DateTime(2006, 5, 1);
@@ -2934,7 +2927,7 @@ public static void LiftedEqualDateTime()
29342927
{
29352928
Expression<Func<DateTime?, DateTime?, bool>> f = (x, y) => x == y;
29362929
Assert.Equal(ExpressionType.Equal, f.Body.NodeType);
2937-
Console.WriteLine(f);
2930+
Debug.WriteLine(f);
29382931
Func<DateTime?, DateTime?, bool> d = f.Compile();
29392932
DateTime? dt1 = DateTime.Now;
29402933
DateTime? dt2 = new DateTime(2006, 5, 1);
@@ -2950,7 +2943,7 @@ public static void LiftedNotEqualDateTime()
29502943
{
29512944
Expression<Func<DateTime?, DateTime?, bool>> f = (x, y) => x != y;
29522945
Assert.Equal(ExpressionType.NotEqual, f.Body.NodeType);
2953-
Console.WriteLine(f);
2946+
Debug.WriteLine(f);
29542947
Func<DateTime?, DateTime?, bool> d = f.Compile();
29552948
DateTime? dt1 = DateTime.Now;
29562949
DateTime? dt2 = new DateTime(2006, 5, 1);
@@ -2966,7 +2959,7 @@ public static void LiftedLessThanDateTime()
29662959
{
29672960
Expression<Func<DateTime?, DateTime?, bool>> f = (x, y) => x < y;
29682961
Assert.Equal(ExpressionType.LessThan, f.Body.NodeType);
2969-
Console.WriteLine(f);
2962+
Debug.WriteLine(f);
29702963
Func<DateTime?, DateTime?, bool> d = f.Compile();
29712964
DateTime? dt1 = DateTime.Now;
29722965
DateTime? dt2 = new DateTime(2006, 5, 1);
@@ -2982,7 +2975,7 @@ public static void LessThanDateTime()
29822975
{
29832976
Expression<Func<DateTime, DateTime, bool>> f = (x, y) => x < y;
29842977
Assert.Equal(ExpressionType.LessThan, f.Body.NodeType);
2985-
Console.WriteLine(f);
2978+
Debug.WriteLine(f);
29862979
Func<DateTime, DateTime, bool> d = f.Compile();
29872980
DateTime dt1 = DateTime.Now;
29882981
DateTime dt2 = new DateTime(2006, 5, 1);
@@ -3611,7 +3604,7 @@ private static R TestUnary<T, R>(ExpressionType op, T v)
36113604
[Fact]
36123605
public static void ShiftULong()
36133606
{
3614-
try
3607+
Assert.Throws<InvalidOperationException>(() =>
36153608
{
36163609
Expression<Func<ulong>> e =
36173610
Expression.Lambda<Func<ulong>>(
@@ -3620,49 +3613,37 @@ public static void ShiftULong()
36203613
Expression.Constant((ulong)1, typeof(ulong))),
36213614
Enumerable.Empty<ParameterExpression>());
36223615
Func<ulong> f = e.Compile();
3623-
Console.WriteLine(f());
3624-
Assert.False(true);
3625-
}
3626-
catch (InvalidOperationException)
3627-
{
3628-
}
3616+
f();
3617+
});
36293618
}
36303619

36313620
[Fact]
36323621
public static void MultiplyMinInt()
36333622
{
3634-
try
3623+
Assert.Throws<OverflowException>(() =>
36353624
{
36363625
Func<long> f = Expression.Lambda<Func<long>>(
36373626
Expression.MultiplyChecked(
36383627
Expression.Constant((long)-1, typeof(long)),
36393628
Expression.Constant(long.MinValue, typeof(long))),
36403629
Enumerable.Empty<ParameterExpression>()
36413630
).Compile();
3642-
Console.WriteLine(f());
3643-
Assert.False(true);
3644-
}
3645-
catch (OverflowException)
3646-
{
3647-
}
3631+
f();
3632+
});
36483633
}
36493634

36503635
[Fact]
36513636
public static void MultiplyMinInt2()
36523637
{
3653-
try
3638+
Assert.Throws<OverflowException>(() =>
36543639
{
36553640
Func<long> f = Expression.Lambda<Func<long>>(
36563641
Expression.MultiplyChecked(
36573642
Expression.Constant(long.MinValue, typeof(long)),
36583643
Expression.Constant((long)-1, typeof(long))),
36593644
Enumerable.Empty<ParameterExpression>()).Compile();
3660-
Console.WriteLine(f());
3661-
Assert.False(true);
3662-
}
3663-
catch (OverflowException)
3664-
{
3665-
}
3645+
f();
3646+
});
36663647
}
36673648

36683649
[Fact]

0 commit comments

Comments
 (0)