ExpressionTest_Field.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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_Field
  31. {
  32. [Test]
  33. [ExpectedException (typeof (ArgumentNullException))]
  34. public void Arg1Null ()
  35. {
  36. Expression.Field (null, "NoField");
  37. }
  38. [Test]
  39. [ExpectedException (typeof (ArgumentNullException))]
  40. public void Arg2Null1 ()
  41. {
  42. Expression.Field (Expression.Constant (new MemberClass()), (string)null);
  43. }
  44. [Test]
  45. [ExpectedException (typeof (ArgumentNullException))]
  46. public void Arg2Null2 ()
  47. {
  48. Expression.Field (Expression.Constant (new MemberClass()), (FieldInfo)null);
  49. }
  50. [Test]
  51. [ExpectedException (typeof (ArgumentException))]
  52. public void NoField ()
  53. {
  54. Expression.Field (Expression.Constant (new MemberClass()), "NoField");
  55. }
  56. [Test]
  57. public void InstanceField ()
  58. {
  59. MemberExpression expr = Expression.Field (Expression.Constant (new MemberClass()), "TestField1");
  60. Assert.AreEqual (ExpressionType.MemberAccess, expr.NodeType, "Field#01");
  61. Assert.AreEqual (typeof (int), expr.Type, "Field#02");
  62. Assert.AreEqual ("value(MonoTests.System.Linq.Expressions.MemberClass).TestField1", expr.ToString(), "Field#03");
  63. }
  64. [Test]
  65. [ExpectedException (typeof (ArgumentException))]
  66. public void StaticField1 ()
  67. {
  68. // This will fail because access to a static field should be created using a FieldInfo and
  69. // not an instance plus the field name.
  70. Expression.Field (Expression.Constant (new MemberClass()), "StaticField");
  71. }
  72. [Test]
  73. public void StaticField2 ()
  74. {
  75. MemberExpression expr = Expression.Field (null, MemberClass.GetStaticFieldInfo());
  76. Assert.AreEqual (ExpressionType.MemberAccess, expr.NodeType, "Field#07");
  77. Assert.AreEqual (typeof (int), expr.Type, "Field#08");
  78. Assert.AreEqual ("MemberClass.StaticField", expr.ToString(), "Field#09");
  79. }
  80. [Test]
  81. [Category ("NotDotNet")] // http://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=339351
  82. [ExpectedException (typeof (ArgumentException))]
  83. public void StaticFieldWithInstanceArgument ()
  84. {
  85. Expression.Field (
  86. Expression.Parameter (GetType (), "t"),
  87. GetType ().GetField ("foo"));
  88. }
  89. public static string foo = "foo";
  90. [Test]
  91. public void CompileStaticField ()
  92. {
  93. var foo = Expression.Lambda<Func<string>> (
  94. Expression.Field (null, GetType ().GetField (
  95. "foo", BindingFlags.Static | BindingFlags.Public))).Compile ();
  96. Assert.AreEqual ("foo", foo ());
  97. }
  98. public class Bar {
  99. public string baz;
  100. public Bar ()
  101. {
  102. baz = "baz";
  103. }
  104. }
  105. [Test]
  106. public void CompileInstanceField ()
  107. {
  108. var p = Expression.Parameter (typeof (Bar), "bar");
  109. var baz = Expression.Lambda<Func<Bar, string>> (
  110. Expression.Field (p, typeof (Bar).GetField (
  111. "baz", BindingFlags.Public | BindingFlags.Instance)), p).Compile ();
  112. Assert.AreEqual ("baz", baz (new Bar ()));
  113. }
  114. public struct Gazonk {
  115. public string Tzap;
  116. public Gazonk (string tzap)
  117. {
  118. Tzap = tzap;
  119. }
  120. }
  121. [Test]
  122. public void CompileStructInstanceField ()
  123. {
  124. var p = Expression.Parameter (typeof (Gazonk), "gazonk");
  125. var gazonker = Expression.Lambda<Func<Gazonk, string>> (
  126. Expression.Field (p, typeof (Gazonk).GetField ("Tzap")), p).Compile ();
  127. Assert.AreEqual ("bang", gazonker (new Gazonk ("bang")));
  128. }
  129. }
  130. }