ExpressionTest_Invoke.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. //
  2. // ExpressionTest_Invoke.cs
  3. //
  4. // Author:
  5. // Jb Evain ([email protected])
  6. //
  7. // (C) 2008 Novell, Inc. (http://www.novell.com)
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining
  10. // a copy of this software and associated documentation files (the
  11. // "Software"), to deal in the Software without restriction, including
  12. // without limitation the rights to use, copy, modify, merge, publish,
  13. // distribute, sublicense, and/or sell copies of the Software, and to
  14. // permit persons to whom the Software is furnished to do so, subject to
  15. // the following conditions:
  16. //
  17. // The above copyright notice and this permission notice shall be
  18. // included in all copies or substantial portions of the Software.
  19. //
  20. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  22. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  23. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  24. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  25. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  26. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. //
  28. using System;
  29. using System.Reflection;
  30. using System.Linq;
  31. using System.Linq.Expressions;
  32. using NUnit.Framework;
  33. namespace MonoTests.System.Linq.Expressions {
  34. [TestFixture]
  35. public class ExpressionTest_Invoke {
  36. static Expression CreateInvokable<T> ()
  37. {
  38. return Expression.Parameter (typeof (T), "invokable");
  39. }
  40. [Test]
  41. [ExpectedException (typeof (ArgumentNullException))]
  42. public void NullExpression ()
  43. {
  44. Expression.Invoke (null, new Expression [0]);
  45. }
  46. [Test]
  47. [ExpectedException (typeof (ArgumentNullException))]
  48. public void NullArgument ()
  49. {
  50. Expression.Invoke (CreateInvokable<Action<int>> (), new [] { null as Expression });
  51. }
  52. [Test]
  53. [ExpectedException (typeof (ArgumentException))]
  54. public void NonInvokableExpressionType ()
  55. {
  56. Expression.Invoke (CreateInvokable<int> (), null);
  57. }
  58. [Test]
  59. [ExpectedException (typeof (InvalidOperationException))]
  60. public void ArgumentCountDoesntMatchParametersLength ()
  61. {
  62. Expression.Invoke (CreateInvokable<Action<int>> (), 1.ToConstant (), 2.ToConstant ());
  63. }
  64. [Test]
  65. [ExpectedException (typeof (ArgumentException))]
  66. public void ArgumentNotAssignableToParameterType ()
  67. {
  68. Expression.Invoke (CreateInvokable<Action<int>> (), "".ToConstant ());
  69. }
  70. [Test]
  71. public void EmptyArguments ()
  72. {
  73. var invoke = Expression.Invoke (CreateInvokable<Action> (), null);
  74. Assert.AreEqual (typeof (void), invoke.Type);
  75. Assert.IsNotNull (invoke.Arguments);
  76. Assert.AreEqual (0, invoke.Arguments.Count);
  77. Assert.AreEqual ("Invoke(invokable)", invoke.ToString ());
  78. }
  79. [Test]
  80. public void InvokeFunc ()
  81. {
  82. var invoke = Expression.Invoke (CreateInvokable<Func<string, string, int>> (), "foo".ToConstant (), "bar".ToConstant ());
  83. Assert.AreEqual (typeof (int), invoke.Type);
  84. Assert.AreEqual (2, invoke.Arguments.Count);
  85. if (OnMono ())
  86. Assert.AreEqual ("Invoke(invokable, \"foo\", \"bar\")", invoke.ToString ());
  87. else
  88. Assert.AreEqual ("Invoke(invokable,\"foo\",\"bar\")", invoke.ToString ());
  89. }
  90. [Test]
  91. public void InvokeLambda ()
  92. {
  93. var p = Expression.Parameter (typeof (int), "i");
  94. var lambda = Expression.Lambda<Func<int, int>> (p, p);
  95. var invoke = Expression.Invoke (lambda, 1.ToConstant ());
  96. Assert.AreEqual (typeof (int), invoke.Type);
  97. Assert.AreEqual (1, invoke.Arguments.Count);
  98. if (OnMono ())
  99. Assert.AreEqual ("Invoke(i => i, 1)", invoke.ToString ());
  100. else
  101. Assert.AreEqual ("Invoke(i => i,1)", invoke.ToString ());
  102. }
  103. static bool OnMono ()
  104. {
  105. return typeof (Expression).Assembly.GetType ("Consts") != null;
  106. }
  107. delegate string StringAction (string s);
  108. static string Identity (string s)
  109. {
  110. return s;
  111. }
  112. [Test]
  113. public void TestCompileInvokePrivateDelegate ()
  114. {
  115. var action = Expression.Parameter (typeof (StringAction), "action");
  116. var str = Expression.Parameter (typeof (string), "str");
  117. var invoker = Expression.Lambda<Func<StringAction, string, string>> (
  118. Expression.Invoke (action, str), action, str).Compile ();
  119. Assert.AreEqual ("foo", invoker (Identity, "foo"));
  120. }
  121. }
  122. }