1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
|
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
//
// Authors:
// Federico Di Gregorio <fog@initd.org>
using System;
using System.Reflection;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using NUnit.Framework;
namespace MonoTests.System.Linq.Expressions
{
[TestFixture]
[Category("SRE")]
public class ExpressionTest_Bind
{
[Test]
[ExpectedException (typeof (ArgumentNullException))]
public void Arg1Null ()
{
Expression.Bind (null, Expression.Constant (1));
}
[Test]
[ExpectedException (typeof (ArgumentNullException))]
public void Arg2Null ()
{
Expression.Bind (MemberClass.GetRwFieldInfo (), null);
}
[Test]
[ExpectedException (typeof (ArgumentException))]
public void Method1 ()
{
// This tests the MethodInfo version of Bind(): should raise an exception
// because the method is not an accessor.
Expression.Bind (MemberClass.GetMethodInfo (), Expression.Constant (1));
}
[Test]
[ExpectedException (typeof (ArgumentException))]
public void Method2 ()
{
// This tests the MemberInfo version of Bind(): should raise an exception
// because the argument is not a field or property accessor.
Expression.Bind ((MemberInfo)MemberClass.GetMethodInfo (), Expression.Constant (1));
}
[Test]
[ExpectedException (typeof (ArgumentException))]
public void Event ()
{
Expression.Bind (MemberClass.GetEventInfo (), Expression.Constant (1));
}
[Test]
[ExpectedException (typeof (ArgumentException))]
public void PropertyRo ()
{
Expression.Bind (MemberClass.GetRoPropertyInfo (), Expression.Constant (1));
}
[Test]
public void FieldRo ()
{
MemberAssignment expr = Expression.Bind (MemberClass.GetRoFieldInfo (), Expression.Constant (1));
Assert.AreEqual (MemberBindingType.Assignment, expr.BindingType, "Bind#01");
Assert.AreEqual ("TestField1 = 1", expr.ToString(), "Bind#02");
}
[Test]
public void FieldRw ()
{
MemberAssignment expr = Expression.Bind (MemberClass.GetRwFieldInfo (), Expression.Constant (1));
Assert.AreEqual (MemberBindingType.Assignment, expr.BindingType, "Bind#03");
Assert.AreEqual ("TestField2 = 1", expr.ToString(), "Bind#04");
}
[Test]
public void FieldStatic ()
{
MemberAssignment expr = Expression.Bind (MemberClass.GetStaticFieldInfo (), Expression.Constant (1));
Assert.AreEqual (MemberBindingType.Assignment, expr.BindingType, "Bind#05");
Assert.AreEqual ("StaticField = 1", expr.ToString(), "Bind#06");
}
[Test]
public void PropertyRw ()
{
MemberAssignment expr = Expression.Bind (MemberClass.GetRwPropertyInfo (), Expression.Constant (1));
Assert.AreEqual (MemberBindingType.Assignment, expr.BindingType, "Bind#07");
Assert.AreEqual ("TestProperty2 = 1", expr.ToString(), "Bind#08");
}
[Test]
public void PropertyStatic ()
{
MemberAssignment expr = Expression.Bind (MemberClass.GetStaticPropertyInfo (), Expression.Constant (1));
Assert.AreEqual (MemberBindingType.Assignment, expr.BindingType, "Bind#09");
Assert.AreEqual ("StaticProperty = 1", expr.ToString(), "Bind#10");
}
[Test]
public void PropertyAccessor ()
{
MethodInfo mi = typeof(MemberClass).GetMethod("get_TestProperty2");
MemberAssignment expr = Expression.Bind (mi, Expression.Constant (1));
Assert.AreEqual (MemberBindingType.Assignment, expr.BindingType, "Bind#11");
Assert.AreEqual ("TestProperty2 = 1", expr.ToString(), "Bind#12");
Assert.AreEqual (MemberClass.GetRwPropertyInfo(), expr.Member, "Bind#13");
}
[Test]
public void PropertyAccessorStatic ()
{
MethodInfo mi = typeof(MemberClass).GetMethod("get_StaticProperty");
MemberAssignment expr = Expression.Bind (mi, Expression.Constant (1));
Assert.AreEqual (MemberBindingType.Assignment, expr.BindingType, "Bind#14");
Assert.AreEqual ("StaticProperty = 1", expr.ToString(), "Bind#15");
Assert.AreEqual (MemberClass.GetStaticPropertyInfo(), expr.Member, "Bind#16");
}
struct Slot {
public int Integer { get; set; }
public short Short { get; set; }
}
[Test]
public void BindValueTypes ()
{
var i = Expression.Parameter (typeof (int), "i");
var s = Expression.Parameter (typeof (short), "s");
var gslot = Expression.Lambda<Func<int, short, Slot>> (
Expression.MemberInit (
Expression.New (typeof (Slot)),
Expression.Bind (typeof (Slot).GetProperty ("Integer"), i),
Expression.Bind (typeof (Slot).GetProperty ("Short"), s)), i, s).Compile ();
Assert.AreEqual (new Slot { Integer = 42, Short = -1 }, gslot (42, -1));
}
}
}
|