ExpressionTest_Invoke.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. [Category("SRE")]
  36. public class ExpressionTest_Invoke {
  37. static Expression CreateInvokable<T> ()
  38. {
  39. return Expression.Parameter (typeof (T), "invokable");
  40. }
  41. [Test]
  42. [ExpectedException (typeof (ArgumentNullException))]
  43. public void NullExpression ()
  44. {
  45. Expression.Invoke (null, new Expression [0]);
  46. }
  47. [Test]
  48. [ExpectedException (typeof (ArgumentNullException))]
  49. public void NullArgument ()
  50. {
  51. Expression.Invoke (CreateInvokable<Action<int>> (), new [] { null as Expression });
  52. }
  53. [Test]
  54. [ExpectedException (typeof (ArgumentException))]
  55. public void NonInvokableExpressionType ()
  56. {
  57. Expression.Invoke (CreateInvokable<int> (), null);
  58. }
  59. [Test]
  60. [ExpectedException (typeof (InvalidOperationException))]
  61. public void ArgumentCountDoesntMatchParametersLength ()
  62. {
  63. Expression.Invoke (CreateInvokable<Action<int>> (), 1.ToConstant (), 2.ToConstant ());
  64. }
  65. [Test]
  66. [ExpectedException (typeof (ArgumentException))]
  67. public void ArgumentNotAssignableToParameterType ()
  68. {
  69. Expression.Invoke (CreateInvokable<Action<int>> (), "".ToConstant ());
  70. }
  71. [Test]
  72. public void EmptyArguments ()
  73. {
  74. var invoke = Expression.Invoke (CreateInvokable<Action> (), null);
  75. Assert.AreEqual (typeof (void), invoke.Type);
  76. Assert.IsNotNull (invoke.Arguments);
  77. Assert.AreEqual (0, invoke.Arguments.Count);
  78. Assert.AreEqual ("Invoke(invokable)", invoke.ToString ());
  79. }
  80. [Test]
  81. [Category ("NotDotNet")] // https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=352402
  82. public void InvokeFunc ()
  83. {
  84. var invoke = Expression.Invoke (CreateInvokable<Func<string, string, int>> (), "foo".ToConstant (), "bar".ToConstant ());
  85. Assert.AreEqual (typeof (int), invoke.Type);
  86. Assert.AreEqual (2, invoke.Arguments.Count);
  87. Assert.AreEqual ("Invoke(invokable, \"foo\", \"bar\")", invoke.ToString ());
  88. }
  89. [Test]
  90. [Category ("NotDotNet")] // https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=352402
  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. Assert.AreEqual ("Invoke(i => i, 1)", invoke.ToString ());
  99. }
  100. delegate string StringAction (string s);
  101. static string Identity (string s)
  102. {
  103. return s;
  104. }
  105. [Test]
  106. public void TestCompileInvokePrivateDelegate ()
  107. {
  108. var action = Expression.Parameter (typeof (StringAction), "action");
  109. var str = Expression.Parameter (typeof (string), "str");
  110. var invoker = Expression.Lambda<Func<StringAction, string, string>> (
  111. Expression.Invoke (action, str), action, str).Compile ();
  112. Assert.AreEqual ("foo", invoker (Identity, "foo"));
  113. }
  114. [Test]
  115. public void InvokeWithExpressionLambdaAsArguments ()
  116. {
  117. var p = Expression.Parameter (typeof (string), "s");
  118. Func<string, Expression<Func<string, string>>, string> caller = (s, f) => f.Compile ().Invoke (s);
  119. var invoke = Expression.Lambda<Func<string>> (
  120. Expression.Invoke (
  121. Expression.Constant (caller),
  122. Expression.Constant ("KABOOM!"),
  123. Expression.Lambda<Func<string, string>> (
  124. Expression.Call (p, typeof (string).GetMethod ("ToLowerInvariant")), p)));
  125. Assert.AreEqual (ExpressionType.Quote,
  126. (invoke.Body as InvocationExpression).Arguments [1].NodeType);
  127. Assert.AreEqual ("kaboom!", invoke.Compile ().DynamicInvoke ());
  128. }
  129. }
  130. }