[ExpectedException (typeof (InvalidOperationException))]
public void NonNullLeftParameter ()
{
- // This throws because they are both doubles, which are never
+ // This throws because they are both doubles, which are never
Expression.Coalesce (Expression.Constant (1.0), Expression.Constant (2.0));
}
ParameterExpression pa = Expression.Parameter (typeof (int?), "a");
ParameterExpression pb = Expression.Parameter (typeof (int?), "b");
BinaryExpression c = Expression.Coalesce (pa, pb);
-
+ Assert.AreEqual ("(a ?? b)", c.ToString ());
+
Expression<Func<int?, int?, int?>> e = Expression.Lambda<Func<int?,int?,int?>>(
c, new ParameterExpression [] { pa, pb });
+ Assert.AreEqual ("(a, b) => (a ?? b)", e.ToString ());
Func<int?,int?,int?> comp = e.Compile ();
public struct D {
}
-
+
[Test]
[ExpectedException (typeof (InvalidOperationException))]
public void NoOperatorClass ()
public void Numeric ()
{
BinaryExpression expr = Expression.Equal (Expression.Constant (1), Expression.Constant (2));
- Assert.AreEqual (ExpressionType.Equal, expr.NodeType, "Equal#01");
- Assert.AreEqual (typeof (bool), expr.Type, "Equal#02");
- Assert.IsNull (expr.Method, "Equal#03");
+ Assert.AreEqual (ExpressionType.Equal, expr.NodeType);
+ Assert.AreEqual (typeof (bool), expr.Type);
+ Assert.IsNull (expr.Method);
+ Assert.AreEqual ("(1 = 2)", expr.ToString ());
}
[Test]
BinaryExpression expr = Expression.Equal (Expression.Constant (a, typeof(int?)),
Expression.Constant (b, typeof(int?)),
false, null);
- Assert.AreEqual (ExpressionType.Equal, expr.NodeType, "Add#05");
- Assert.AreEqual (typeof (bool), expr.Type, "Equal#06");
- Assert.AreEqual (true, expr.IsLifted, "Equal#07");
- Assert.AreEqual (false, expr.IsLiftedToNull, "Equal#07");
- Assert.IsNull (expr.Method, "Equal#08");
+ Assert.AreEqual (ExpressionType.Equal, expr.NodeType);
+ Assert.AreEqual (typeof (bool), expr.Type);
+ Assert.AreEqual (true, expr.IsLifted);
+ Assert.AreEqual (false, expr.IsLiftedToNull);
+ Assert.IsNull (expr.Method);
+ Assert.AreEqual ("(1 = 2)", expr.ToString ());
}
[Test]
BinaryExpression expr = Expression.Equal (Expression.Constant (a, typeof(int?)),
Expression.Constant (b, typeof(int?)),
true, null);
- Assert.AreEqual (ExpressionType.Equal, expr.NodeType, "Equal#01");
- Assert.AreEqual (typeof (bool?), expr.Type, "Equal#02");
- Assert.AreEqual (true, expr.IsLifted, "Equal#03");
- Assert.AreEqual (true, expr.IsLiftedToNull, "Equal#04");
- Assert.IsNull (expr.Method, "Equal#04");
+ Assert.AreEqual (ExpressionType.Equal, expr.NodeType);
+ Assert.AreEqual (typeof (bool?), expr.Type);
+ Assert.AreEqual (true, expr.IsLifted);
+ Assert.AreEqual (true, expr.IsLiftedToNull);
+ Assert.IsNull (expr.Method);
+ Assert.AreEqual ("(1 = 2)", expr.ToString ());
}
[Test]
BinaryExpression expr = Expression.Equal (Expression.Constant (a, typeof (int?)),
Expression.Constant (b, typeof (int)));
}
-
+
[Test]
public void UserDefinedClass ()
{
MethodInfo mi = typeof (OpClass).GetMethod ("op_Equality");
BinaryExpression expr = Expression.Equal (Expression.Constant (new OpClass ()), Expression.Constant (new OpClass ()));
- Assert.AreEqual (ExpressionType.Equal, expr.NodeType, "Add#09");
- Assert.AreEqual (typeof (bool), expr.Type, "Add#10");
- Assert.AreEqual (mi, expr.Method, "Add#11");
- Assert.AreEqual ("op_Equality", expr.Method.Name, "Add#12");
+ Assert.AreEqual (ExpressionType.Equal, expr.NodeType);
+ Assert.AreEqual (typeof (bool), expr.Type);
+ Assert.AreEqual (mi, expr.Method);
+ Assert.AreEqual ("op_Equality", expr.Method.Name);
+
+ Assert.AreEqual ("(value(MonoTests.System.Linq.Expressions.OpClass) = value(MonoTests.System.Linq.Expressions.OpClass))", expr.ToString ());
}
//
[Test]
public void UserDefinedEqual ()
{
-
+
}
-
+
}
}
public void InvalidArgType ()
{
// invalid argument type
-
+
ParameterExpression p = Expression.Parameter (typeof (string), "AAA");
Expression.Lambda (typeof (delegate_object_int), Expression.Constant ("foo"), new ParameterExpression [1] {p});
}
public void InvalidArgType2 ()
{
// invalid argument type
-
+
ParameterExpression p = Expression.Parameter (typeof (string), "AAA");
Expression.Lambda (typeof (delegate_object_object), Expression.Constant ("foo"), new ParameterExpression [1] {p});
}
-
+
[Test]
public void Assignability ()
{
// allowed delegate has string, delegate has base class (object)
ParameterExpression p = Expression.Parameter (typeof (object), "ParObject");
- Expression.Lambda (typeof (delegate_object_string), Expression.Constant (""), new ParameterExpression [1] {p});
+ var l = Expression.Lambda (typeof (delegate_object_string), Expression.Constant (""), new ParameterExpression [1] {p});
+
+ Assert.AreEqual ("ParObject => \"\"", l.ToString ());
}
[Test]
Expression<Func<int,int,int>> l = Expression.Lambda<Func<int,int,int>>(
Expression.Add (a, b), new ParameterExpression [] { a, b });
+
+ Assert.AreEqual ("(a, b) => (a + b)", l.ToString ());
+
Func<int,int,int> xx = l.Compile ();
int res = xx (10, 20);
Assert.AreEqual (res, 30);
}
-
+
[Test]
public void Compile ()
{
Expression<Func<int>> l = Expression.Lambda<Func<int>> (Expression.Constant (1), new ParameterExpression [0]);
+
+ Assert.AreEqual ("() => 1", l.ToString ());
+
Func<int> fi = l.Compile ();
fi ();
}
ParameterExpression p1 = Expression.Parameter(typeof(int?), "va");
ParameterExpression p2 = Expression.Parameter(typeof(int?), "vb");
Expression add = Expression.Add(p1, p2);
-
+
// This should throw, since the add.Type is "int?" and the return
// type we have here is int.