ExpressionObserverTests_Method.cs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. using Avalonia.Data;
  2. using Avalonia.Data.Core;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Reactive.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using Xunit;
  10. namespace Avalonia.Base.UnitTests.Data.Core
  11. {
  12. public class ExpressionObserverTests_Method
  13. {
  14. private class TestObject
  15. {
  16. public void MethodWithoutReturn() { }
  17. public int MethodWithReturn() => 0;
  18. public int MethodWithReturnAndParameters(int i) => i;
  19. public static void StaticMethod() { }
  20. public static void TooManyParameters(int a1, int a2, int a3, int a4, int a5, int a6, int a7, int a8, int a9) { }
  21. public static int TooManyParametersWithReturnType(int a1, int a2, int a3, int a4, int a5, int a6, int a7, int a8) => 1;
  22. }
  23. [Fact]
  24. public async Task Should_Get_Method()
  25. {
  26. var data = new TestObject();
  27. var observer = new ExpressionObserver(data, nameof(TestObject.MethodWithoutReturn));
  28. var result = await observer.Take(1);
  29. Assert.NotNull(result);
  30. GC.KeepAlive(data);
  31. }
  32. [Theory]
  33. [InlineData(nameof(TestObject.MethodWithoutReturn), typeof(Action))]
  34. [InlineData(nameof(TestObject.MethodWithReturn), typeof(Func<int>))]
  35. [InlineData(nameof(TestObject.MethodWithReturnAndParameters), typeof(Func<int, int>))]
  36. [InlineData(nameof(TestObject.StaticMethod), typeof(Action))]
  37. public async Task Should_Get_Method_WithCorrectDelegateType(string methodName, Type expectedType)
  38. {
  39. var data = new TestObject();
  40. var observer = new ExpressionObserver(data, methodName);
  41. var result = await observer.Take(1);
  42. Assert.IsType(expectedType, result);
  43. GC.KeepAlive(data);
  44. }
  45. [Fact]
  46. public async Task Can_Call_Method_Returned_From_Observer()
  47. {
  48. var data = new TestObject();
  49. var observer = new ExpressionObserver(data, nameof(TestObject.MethodWithReturnAndParameters));
  50. var result = await observer.Take(1);
  51. var callback = (Func<int, int>)result;
  52. Assert.Equal(1, callback(1));
  53. GC.KeepAlive(data);
  54. }
  55. [Theory]
  56. [InlineData(nameof(TestObject.TooManyParameters))]
  57. [InlineData(nameof(TestObject.TooManyParametersWithReturnType))]
  58. public async Task Should_Return_Error_Notification_If_Too_Many_Parameters(string methodName)
  59. {
  60. var data = new TestObject();
  61. var observer = new ExpressionObserver(data, methodName);
  62. var result = await observer.Take(1);
  63. Assert.IsType<BindingNotification>(result);
  64. Assert.Equal(BindingErrorType.Error, ((BindingNotification)result).ErrorType);
  65. GC.KeepAlive(data);
  66. }
  67. }
  68. }