microsoft/teams.net
Publicmirrored from https://github.com/microsoft/teams.netAvailable
Tests/Microsoft.Teams.Common.Tests/Extensions/MethodInfoExtensionsTests.cs
87lines · modeblame
a1e39460Falguni Thakor10 months ago | 1 | using Microsoft.Teams.Common.Extensions; |
| 2 | | |
| 3 | namespace Microsoft.Teams.Common.Tests.Extensions; | |
| 4 | public class MethodInfoExtensionsTests | |
| 5 | { | |
| 6 | private class TestClass | |
| 7 | { | |
| 8 | public bool VoidCalled { get; private set; } | |
| 9 | public object? Value { get; private set; } | |
| 10 | | |
| 11 | public void VoidMethod() | |
| 12 | { | |
| 13 | VoidCalled = true; | |
| 14 | } | |
| 15 | | |
| 16 | public int SyncMethod(int x, int y) | |
| 17 | { | |
| 18 | return x + y; | |
| 19 | } | |
| 20 | | |
| 21 | public async Task AsyncVoidMethod() | |
| 22 | { | |
| 23 | await Task.Delay(1); | |
| 24 | VoidCalled = true; | |
| 25 | } | |
| 26 | | |
| 27 | public async Task<int> AsyncValueMethod(int x, int y) | |
| 28 | { | |
| 29 | await Task.Delay(1); | |
| 30 | return x * y; | |
| 31 | } | |
| 32 | | |
| 33 | public Task<object?> AsyncObjectMethod(object? value) | |
| 34 | { | |
| 35 | Value = value; | |
| 36 | return Task.FromResult(value); | |
| 37 | } | |
| 38 | } | |
| 39 | | |
| 40 | [Fact] | |
| 41 | public async Task InvokeAsync_SyncMethod_ReturnsResult() | |
| 42 | { | |
| 43 | var obj = new TestClass(); | |
| 44 | var method = typeof(TestClass).GetMethod(nameof(TestClass.SyncMethod)); | |
| 45 | var result = await method!.InvokeAsync(obj, new object[] { 2, 3 }); | |
| 46 | Assert.Equal(5, result); | |
| 47 | } | |
| 48 | | |
| 49 | [Fact] | |
| 50 | public async Task InvokeAsync_AsyncValueMethod_ReturnsResult() | |
| 51 | { | |
| 52 | var obj = new TestClass(); | |
| 53 | var method = typeof(TestClass).GetMethod(nameof(TestClass.AsyncValueMethod)); | |
| 54 | var result = await method!.InvokeAsync(obj, new object[] { 2, 4 }); | |
| 55 | Assert.Equal(8, result); | |
| 56 | } | |
| 57 | | |
| 58 | [Fact] | |
| 59 | public async Task InvokeAsync_AsyncVoidMethod_ReturnsNull() | |
| 60 | { | |
| 61 | var obj = new TestClass(); | |
| 62 | var method = typeof(TestClass).GetMethod(nameof(TestClass.AsyncVoidMethod)); | |
| 63 | var result = await method!.InvokeAsync(obj, null); | |
| 64 | Assert.True(obj.VoidCalled); | |
| 65 | } | |
| 66 | | |
| 67 | [Fact] | |
| 68 | public async Task InvokeAsync_AsyncObjectMethod_ReturnsObject() | |
| 69 | { | |
| 70 | var obj = new TestClass(); | |
| 71 | var method = typeof(TestClass).GetMethod(nameof(TestClass.AsyncObjectMethod)); | |
| 72 | var input = "test"; | |
| 73 | var result = await method!.InvokeAsync(obj, new object?[] { input }); | |
| 74 | Assert.Equal(input, result); | |
| 75 | Assert.Equal(input, obj.Value); | |
| 76 | } | |
| 77 | | |
| 78 | [Fact] | |
| 79 | public async Task InvokeAsync_VoidMethod_ReturnsNull() | |
| 80 | { | |
| 81 | var obj = new TestClass(); | |
| 82 | var method = typeof(TestClass).GetMethod(nameof(TestClass.VoidMethod)); | |
| 83 | var result = await method!.InvokeAsync(obj, null); | |
| 84 | Assert.Null(result); | |
| 85 | Assert.True(obj.VoidCalled); | |
| 86 | } | |
| 87 | } |