ExpressionTest_Lambda.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. // Miguel de Icaza ([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. public class ExpressionTest_Lambda
  30. {
  31. [Test]
  32. [ExpectedException (typeof (ArgumentException))]
  33. public void NonDelegateTypeInCtor ()
  34. {
  35. // The first parameter must be a delegate type
  36. Expression.Lambda (typeof(string), Expression.Constant (1), new ParameterExpression [0]);
  37. }
  38. delegate object delegate_object_emtpy ();
  39. delegate object delegate_object_int (int a);
  40. delegate object delegate_object_string (string s);
  41. delegate object delegate_object_object (object s);
  42. [Test]
  43. [ExpectedException (typeof (ArgumentException))]
  44. public void InvalidConversion ()
  45. {
  46. // float to object, invalid
  47. Expression.Lambda (typeof (delegate_object_emtpy), Expression.Constant (1.0), new ParameterExpression [0]);
  48. }
  49. [Test]
  50. [ExpectedException (typeof (ArgumentException))]
  51. public void InvalidConversion2 ()
  52. {
  53. // float to object, invalid
  54. Expression.Lambda (typeof (delegate_object_emtpy), Expression.Constant (1), new ParameterExpression [0]);
  55. }
  56. [Test]
  57. [ExpectedException (typeof (ArgumentException))]
  58. public void InvalidArgCount ()
  59. {
  60. // missing a parameter
  61. Expression.Lambda (typeof (delegate_object_int), Expression.Constant ("foo"), new ParameterExpression [0]);
  62. }
  63. [Test]
  64. [ExpectedException (typeof (ArgumentException))]
  65. public void InvalidArgCount2 ()
  66. {
  67. // extra parameter
  68. ParameterExpression p = Expression.Parameter (typeof (int), "AAA");
  69. Expression.Lambda (typeof (delegate_object_emtpy), Expression.Constant ("foo"), new ParameterExpression [1] {p});
  70. }
  71. [Test]
  72. [ExpectedException (typeof (ArgumentException))]
  73. public void InvalidArgType ()
  74. {
  75. // invalid argument type
  76. ParameterExpression p = Expression.Parameter (typeof (string), "AAA");
  77. Expression.Lambda (typeof (delegate_object_int), Expression.Constant ("foo"), new ParameterExpression [1] {p});
  78. }
  79. [Test]
  80. [ExpectedException (typeof (ArgumentException))]
  81. public void InvalidArgType2 ()
  82. {
  83. // invalid argument type
  84. ParameterExpression p = Expression.Parameter (typeof (string), "AAA");
  85. Expression.Lambda (typeof (delegate_object_object), Expression.Constant ("foo"), new ParameterExpression [1] {p});
  86. }
  87. [Test]
  88. public void Assignability ()
  89. {
  90. // allowed: string to object
  91. Expression.Lambda (typeof (delegate_object_emtpy), Expression.Constant ("string"), new ParameterExpression [0]);
  92. // allowed delegate has string, delegate has base class (object)
  93. ParameterExpression p = Expression.Parameter (typeof (object), "ParObject");
  94. Expression.Lambda (typeof (delegate_object_string), Expression.Constant (""), new ParameterExpression [1] {p});
  95. }
  96. [Test]
  97. public void Compile ()
  98. {
  99. Expression<Func<int>> l = Expression.Lambda<Func<int>> (Expression.Constant (1), new ParameterExpression [0]);
  100. Func<int> fi = l.Compile ();
  101. fi ();
  102. }
  103. }
  104. }