1 // Licensed to the .NET Foundation under one or more agreements.
2 // The .NET Foundation licenses this file to you under the MIT license.
3 // See the LICENSE file in the project root for more information.
4 
5 using System.Collections.Generic;
6 
7 namespace System.Linq.Expressions.Tests
8 {
9     public class TypeBinaryTests
10     {
11         protected class TypeBinaryVisitCheckingVisitor : ExpressionVisitor
12         {
13             public TypeBinaryExpression LastTypeBinaryVisited { get; private set; }
VisitTypeBinary(TypeBinaryExpression node)14             protected override Expression VisitTypeBinary(TypeBinaryExpression node)
15             {
16                 LastTypeBinaryVisited = node;
17                 return base.VisitTypeBinary(node);
18             }
19         }
20 
21         protected static class Unreadable<T>
22         {
23             public static T WriteOnly
24             {
25                 set { }
26             }
27         }
28 
29         protected enum ByteBased : byte
30         {
31             A, B, C, D
32         }
33 
34         protected enum SByteBased : byte
35         {
36             A, B, C, D
37         }
38 
39         private static IEnumerable<Type> Types
40         {
41             get
42             {
43                 return new[] { typeof(void), typeof(int), typeof(object), typeof(string), typeof(long), typeof(Type), typeof(Uri), typeof(BinaryExpression), typeof(Expression), typeof(DateTime), typeof(DateTime?), typeof(IComparable), typeof(ByteBased), typeof(ByteBased?), typeof(SByteBased), typeof(StringComparison) };
44             }
45         }
46 
47         private static IEnumerable<Expression> Constants
48         {
49             get
50             {
51                 foreach (var type in Types)
52                     yield return Expression.Default(type);
53                 yield return Expression.Constant(1);
54                 yield return Expression.Constant(DateTime.MinValue);
55                 yield return Expression.Constant("hello");
56                 yield return Expression.Constant("hello", typeof(object));
57                 yield return Expression.Constant(Expression.Empty());
58                 yield return Expression.Constant(Expression.And(Expression.Constant(1), Expression.Constant(2)));
59                 yield return Expression.Constant(typeof(int));
60                 yield return Expression.New(typeof(string).GetConstructor(new[] { typeof(char[]) }), Expression.Constant(new[] { 't', 'e', 's', 't' }));
61                 yield return Expression.Convert(Expression.Constant(DateTime.MaxValue), typeof(DateTime?));
62                 yield return Expression.Constant(1, typeof(int?));
63                 yield return Expression.Constant(1, typeof(object));
64                 yield return Expression.Constant(1, typeof(IComparable));
65                 yield return Expression.Constant(DateTime.MinValue, typeof(object));
66                 yield return Expression.Constant(1, typeof(ValueType));
67                 yield return Expression.Constant(DateTime.MinValue, typeof(ValueType));
68                 yield return Expression.Constant(DateTime.MinValue, typeof(IComparable));
69                 yield return Expression.Constant(ByteBased.A);
70                 yield return Expression.Constant(ByteBased.D, typeof(ByteBased?));
71                 yield return Expression.Constant(ByteBased.B, typeof(object));
72                 yield return Expression.Constant(ByteBased.C, typeof(Enum));
73                 yield return Expression.Constant(ByteBased.C, typeof(IComparable));
74                 yield return Expression.Constant(SByteBased.A);
75                 yield return Expression.Constant(SByteBased.B, typeof(object));
76                 yield return Expression.Constant(SByteBased.C, typeof(Enum));
77                 yield return Expression.Constant(SByteBased.C, typeof(IComparable));
78                 yield return Expression.Constant(StringComparison.CurrentCulture);
79                 yield return Expression.Constant(StringComparison.CurrentCultureIgnoreCase, typeof(object));
80                 yield return Expression.Constant(StringComparison.Ordinal, typeof(Enum));
81                 yield return Expression.Constant(StringComparison.OrdinalIgnoreCase, typeof(IComparable));
82                 yield return Expression.Constant(new NullReferenceException(), typeof(Exception));
83             }
84         }
85 
86         public static IEnumerable<object[]> ExpressionAndTypeCombinations
87         {
88             get
89             {
90                 return Types.SelectMany(t => Constants, (t, c) => new object[] { c, t });
91             }
92         }
93 
94         public static IEnumerable<object[]> TypeArguments => Types.Select(t => new object[] {t});
95     }
96 }
97