ExpressionTest_Bind.cs 5.4 KB

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