You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

90 line
2.4 KiB

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Reflection;
  5. using DotNetCore.CAP.Internal;
  6. using Xunit;
  7. namespace DotNetCore.CAP.Test
  8. {
  9. public class ObjectMethodExecutorTest
  10. {
  11. [Fact]
  12. public void CanCreateInstance()
  13. {
  14. var testClass = new MethodExecutorClass();
  15. var methodInfo = testClass.GetType().GetMethod("Foo");
  16. var executor = ObjectMethodExecutor.Create(methodInfo, typeof(MethodExecutorClass).GetTypeInfo());
  17. Assert.NotNull(executor);
  18. }
  19. [Fact]
  20. public void CanExcuteMethodWithNoParameters()
  21. {
  22. var testClass = new MethodExecutorClass();
  23. var methodInfo = testClass.GetType().GetMethod("GetThree");
  24. var executor = ObjectMethodExecutor.Create(methodInfo, typeof(MethodExecutorClass).GetTypeInfo());
  25. Assert.NotNull(executor);
  26. var objResult = executor.Execute(testClass, null);
  27. Assert.Equal(3, objResult);
  28. }
  29. [Fact]
  30. public void CanExcuteMethodWithParameters()
  31. {
  32. var testClass = new MethodExecutorClass();
  33. var methodInfo = testClass.GetType().GetMethod("Add");
  34. var executor = ObjectMethodExecutor.Create(methodInfo, typeof(MethodExecutorClass).GetTypeInfo());
  35. Assert.NotNull(executor);
  36. var objResult = executor.Execute(testClass, 1, 2);
  37. Assert.Equal(3, objResult);
  38. }
  39. [Fact]
  40. public void CanGetExcuteMethodDefaultValue()
  41. {
  42. var testClass = new MethodExecutorClass();
  43. var methodInfo = testClass.GetType().GetMethod("WithDefaultValue");
  44. var executor = ObjectMethodExecutor.Create(methodInfo, typeof(MethodExecutorClass).GetTypeInfo());
  45. var objResult = executor.GetDefaultValueForParameter(0);
  46. Assert.Equal("aaa", objResult);
  47. var objResult2 = executor.GetDefaultValueForParameter(1);
  48. Assert.Equal("bbb", objResult2);
  49. }
  50. }
  51. public class MethodExecutorClass
  52. {
  53. public void Foo()
  54. {
  55. }
  56. public int GetThree()
  57. {
  58. return 3;
  59. }
  60. public int Add(int a, int b)
  61. {
  62. return a + b;
  63. }
  64. public void WithDefaultValue(string aaa = "aaa", string bbb = "bbb")
  65. {
  66. }
  67. }
  68. }