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. public class ExpressionTest_Bind
  31. {
  32. [Test]
  33. [ExpectedException (typeof (ArgumentNullException))]
  34. public void Arg1Null ()
  35. {
  36. Expression.Bind (null, Expression.Constant (1));
  37. }
  38. [Test]
  39. [ExpectedException (typeof (ArgumentNullException))]
  40. public void Arg2Null ()
  41. {
  42. Expression.Bind (MemberClass.GetRwFieldInfo (), null);
  43. }
  44. [Test]
  45. [ExpectedException (typeof (ArgumentException))]
  46. public void Method1 ()
  47. {
  48. // This tests the MethodInfo version of Bind(): should raise an exception
  49. // because the method is not an accessor.
  50. Expression.Bind (MemberClass.GetMethodInfo (), Expression.Constant (1));
  51. }
  52. [Test]
  53. [ExpectedException (typeof (ArgumentException))]
  54. public void Method2 ()
  55. {
  56. // This tests the MemberInfo version of Bind(): should raise an exception
  57. // because the argument is not a field or property accessor.
  58. Expression.Bind ((MemberInfo)MemberClass.GetMethodInfo (), Expression.Constant (1));
  59. }
  60. [Test]
  61. [ExpectedException (typeof (ArgumentException))]
  62. public void Event ()
  63. {
  64. Expression.Bind (MemberClass.GetEventInfo (), Expression.Constant (1));
  65. }
  66. [Test]
  67. [ExpectedException (typeof (ArgumentException))]
  68. public void PropertyRo ()
  69. {
  70. Expression.Bind (MemberClass.GetRoPropertyInfo (), Expression.Constant (1));
  71. }
  72. [Test]
  73. public void FieldRo ()
  74. {
  75. MemberAssignment expr = Expression.Bind (MemberClass.GetRoFieldInfo (), Expression.Constant (1));
  76. Assert.AreEqual (MemberBindingType.Assignment, expr.BindingType, "Bind#01");
  77. Assert.AreEqual ("TestField1 = 1", expr.ToString(), "Bind#02");
  78. }
  79. [Test]
  80. public void FieldRw ()
  81. {
  82. MemberAssignment expr = Expression.Bind (MemberClass.GetRwFieldInfo (), Expression.Constant (1));
  83. Assert.AreEqual (MemberBindingType.Assignment, expr.BindingType, "Bind#03");
  84. Assert.AreEqual ("TestField2 = 1", expr.ToString(), "Bind#04");
  85. }
  86. [Test]
  87. public void FieldStatic ()
  88. {
  89. MemberAssignment expr = Expression.Bind (MemberClass.GetStaticFieldInfo (), Expression.Constant (1));
  90. Assert.AreEqual (MemberBindingType.Assignment, expr.BindingType, "Bind#05");
  91. Assert.AreEqual ("StaticField = 1", expr.ToString(), "Bind#06");
  92. }
  93. [Test]
  94. public void PropertyRw ()
  95. {
  96. MemberAssignment expr = Expression.Bind (MemberClass.GetRwPropertyInfo (), Expression.Constant (1));
  97. Assert.AreEqual (MemberBindingType.Assignment, expr.BindingType, "Bind#07");
  98. Assert.AreEqual ("TestProperty2 = 1", expr.ToString(), "Bind#08");
  99. }
  100. [Test]
  101. public void PropertyStatic ()
  102. {
  103. MemberAssignment expr = Expression.Bind (MemberClass.GetStaticPropertyInfo (), Expression.Constant (1));
  104. Assert.AreEqual (MemberBindingType.Assignment, expr.BindingType, "Bind#09");
  105. Assert.AreEqual ("StaticProperty = 1", expr.ToString(), "Bind#10");
  106. }
  107. [Test]
  108. public void PropertyAccessor ()
  109. {
  110. MethodInfo mi = typeof(MemberClass).GetMethod("get_TestProperty2");
  111. MemberAssignment expr = Expression.Bind (mi, Expression.Constant (1));
  112. Assert.AreEqual (MemberBindingType.Assignment, expr.BindingType, "Bind#11");
  113. Assert.AreEqual ("TestProperty2 = 1", expr.ToString(), "Bind#12");
  114. Assert.AreEqual (MemberClass.GetRwPropertyInfo(), expr.Member, "Bind#13");
  115. }
  116. [Test]
  117. public void PropertyAccessorStatic ()
  118. {
  119. MethodInfo mi = typeof(MemberClass).GetMethod("get_StaticProperty");
  120. MemberAssignment expr = Expression.Bind (mi, Expression.Constant (1));
  121. Assert.AreEqual (MemberBindingType.Assignment, expr.BindingType, "Bind#14");
  122. Assert.AreEqual ("StaticProperty = 1", expr.ToString(), "Bind#15");
  123. Assert.AreEqual (MemberClass.GetStaticPropertyInfo(), expr.Member, "Bind#16");
  124. }
  125. struct Slot {
  126. public int Integer { get; set; }
  127. public short Short { get; set; }
  128. }
  129. [Test]
  130. [Category ("NotWorkingInterpreter")]
  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. }