ExpressionTest_Property.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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.Linq;
  24. using System.Linq.Expressions;
  25. using NUnit.Framework;
  26. namespace MonoTests.System.Linq.Expressions
  27. {
  28. [TestFixture]
  29. [Category("SRE")]
  30. public class ExpressionTest_Property
  31. {
  32. [Test]
  33. [ExpectedException (typeof (ArgumentNullException))]
  34. public void Arg1Null ()
  35. {
  36. Expression.Property (null, "NoProperty");
  37. }
  38. [Test]
  39. [ExpectedException (typeof (ArgumentNullException))]
  40. public void Arg2Null1 ()
  41. {
  42. Expression.Property (Expression.Constant (new MemberClass()), (string)null);
  43. }
  44. [Test]
  45. [ExpectedException (typeof (ArgumentNullException))]
  46. public void Arg2Null2 ()
  47. {
  48. Expression.Property (Expression.Constant (new MemberClass()), (PropertyInfo)null);
  49. }
  50. [Test]
  51. [ExpectedException (typeof (ArgumentNullException))]
  52. public void Arg2Null3 ()
  53. {
  54. Expression.Property (Expression.Constant (new MemberClass()), (MethodInfo)null);
  55. }
  56. [Test]
  57. [ExpectedException (typeof (ArgumentException))]
  58. public void NoProperty ()
  59. {
  60. Expression.Property (Expression.Constant (new MemberClass()), "NoProperty");
  61. }
  62. [Test]
  63. public void InstanceProperty1 ()
  64. {
  65. MemberExpression expr = Expression.Property (Expression.Constant (new MemberClass()), "TestProperty1");
  66. Assert.AreEqual (ExpressionType.MemberAccess, expr.NodeType, "Property#01");
  67. Assert.AreEqual (typeof (int), expr.Type, "Property#02");
  68. Assert.AreEqual ("value(MonoTests.System.Linq.Expressions.MemberClass).TestProperty1", expr.ToString(), "Property#03");
  69. }
  70. [Test]
  71. public void InstanceProperty2 ()
  72. {
  73. MemberExpression expr = Expression.Property (Expression.Constant (new MemberClass()), MemberClass.GetRoPropertyInfo());
  74. Assert.AreEqual (ExpressionType.MemberAccess, expr.NodeType, "Property#04");
  75. Assert.AreEqual (typeof (int), expr.Type, "Property#05");
  76. Assert.AreEqual ("value(MonoTests.System.Linq.Expressions.MemberClass).TestProperty1", expr.ToString(), "Property#06");
  77. }
  78. [Test]
  79. public void InstanceProperty3 ()
  80. {
  81. MethodInfo mi = typeof(MemberClass).GetMethod("get_TestProperty1");
  82. MemberExpression expr = Expression.Property (Expression.Constant (new MemberClass()), mi);
  83. Assert.AreEqual (ExpressionType.MemberAccess, expr.NodeType, "Property#07");
  84. Assert.AreEqual (typeof (int), expr.Type, "Property#08");
  85. Assert.AreEqual ("value(MonoTests.System.Linq.Expressions.MemberClass).TestProperty1", expr.ToString(), "Property#09");
  86. Assert.AreEqual (MemberClass.GetRoPropertyInfo(), expr.Member, "Property#10");
  87. }
  88. [Test]
  89. [ExpectedException (typeof (ArgumentException))]
  90. public void StaticProperty1 ()
  91. {
  92. // This will fail because access to a static field should be created using a PropertyInfo and
  93. // not an instance plus the field name.
  94. Expression.Property (Expression.Constant (new MemberClass()), "StaticProperty");
  95. }
  96. [Test]
  97. public void StaticProperty2 ()
  98. {
  99. MemberExpression expr = Expression.Property (null, MemberClass.GetStaticPropertyInfo());
  100. Assert.AreEqual (ExpressionType.MemberAccess, expr.NodeType, "Property#11");
  101. Assert.AreEqual (typeof (int), expr.Type, "Property#12");
  102. Assert.AreEqual ("MemberClass.StaticProperty", expr.ToString(), "Property#13");
  103. }
  104. [Test]
  105. public void StaticProperty3 ()
  106. {
  107. MethodInfo mi = typeof(MemberClass).GetMethod("get_StaticProperty");
  108. MemberExpression expr = Expression.Property (null, mi);
  109. Assert.AreEqual (ExpressionType.MemberAccess, expr.NodeType, "Property#14");
  110. Assert.AreEqual (typeof (int), expr.Type, "Property#15");
  111. Assert.AreEqual ("MemberClass.StaticProperty", expr.ToString(), "Property#16");
  112. Assert.AreEqual (MemberClass.GetStaticPropertyInfo(), expr.Member, "Property#17");
  113. }
  114. public class Foo {
  115. public string Prop { get; set; }
  116. public static string StatProp
  117. {
  118. get { return "StaticFoo"; }
  119. }
  120. }
  121. [Test]
  122. public void TestCompileGetInstanceProperty ()
  123. {
  124. var p = Expression.Parameter (typeof (Foo), "foo");
  125. var fooer = Expression.Lambda<Func<Foo, string>> (
  126. Expression.Property (p, typeof (Foo).GetProperty ("Prop")), p).Compile ();
  127. Assert.AreEqual ("foo", fooer (new Foo { Prop = "foo" }));
  128. }
  129. [Test]
  130. public void TestCompileGetStaticProperty ()
  131. {
  132. var sf = Expression.Lambda<Func<string>> (
  133. Expression.Property (null, typeof (Foo).GetProperty (
  134. "StatProp", BindingFlags.Public | BindingFlags.Static))).Compile ();
  135. Assert.AreEqual ("StaticFoo", sf ());
  136. }
  137. public struct Bar {
  138. private string slot;
  139. public string Prop {
  140. get { return slot; }
  141. set { slot = value; }
  142. }
  143. public Bar (string slot)
  144. {
  145. this.slot = slot;
  146. }
  147. }
  148. [Test]
  149. public void TestCompileGetInstancePropertyOnStruct ()
  150. {
  151. var p = Expression.Parameter (typeof (Bar), "bar");
  152. var barer = Expression.Lambda<Func<Bar, string>> (
  153. Expression.Property (p, typeof (Bar).GetProperty ("Prop")), p).Compile ();
  154. Assert.AreEqual ("bar", barer (new Bar ("bar")));
  155. }
  156. public static int StaticProperty {
  157. get { return 42; }
  158. }
  159. [Test]
  160. [Category ("NotDotNet")] // http://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=339351
  161. [ExpectedException (typeof (ArgumentException))]
  162. public void StaticPropertyWithInstanceArgument ()
  163. {
  164. Expression.Property (
  165. Expression.Parameter (GetType (), "t"),
  166. GetType ().GetProperty ("StaticProperty"));
  167. }
  168. }
  169. }