microsoft/teams.net

Public

mirrored fromhttps://github.com/microsoft/teams.netAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
4e77a5cc22b7f3e3bc8a37f8d22d2bd0f75e61ac

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

Tests/Microsoft.Teams.Common.Tests/Extensions/MethodInfoExtensionsTests.cs

87lines · modepreview

using Microsoft.Teams.Common.Extensions;

namespace Microsoft.Teams.Common.Tests.Extensions;
public class MethodInfoExtensionsTests
{
    private class TestClass
    {
        public bool VoidCalled { get; private set; }
        public object? Value { get; private set; }

        public void VoidMethod()
        {
            VoidCalled = true;
        }

        public int SyncMethod(int x, int y)
        {
            return x + y;
        }

        public async Task AsyncVoidMethod()
        {
            await Task.Delay(1);
            VoidCalled = true;
        }

        public async Task<int> AsyncValueMethod(int x, int y)
        {
            await Task.Delay(1);
            return x * y;
        }

        public Task<object?> AsyncObjectMethod(object? value)
        {
            Value = value;
            return Task.FromResult(value);
        }
    }

    [Fact]
    public async Task InvokeAsync_SyncMethod_ReturnsResult()
    {
        var obj = new TestClass();
        var method = typeof(TestClass).GetMethod(nameof(TestClass.SyncMethod));
        var result = await method!.InvokeAsync(obj, new object[] { 2, 3 });
        Assert.Equal(5, result);
    }

    [Fact]
    public async Task InvokeAsync_AsyncValueMethod_ReturnsResult()
    {
        var obj = new TestClass();
        var method = typeof(TestClass).GetMethod(nameof(TestClass.AsyncValueMethod));
        var result = await method!.InvokeAsync(obj, new object[] { 2, 4 });
        Assert.Equal(8, result);
    }

    [Fact]
    public async Task InvokeAsync_AsyncVoidMethod_ReturnsNull()
    {
        var obj = new TestClass();
        var method = typeof(TestClass).GetMethod(nameof(TestClass.AsyncVoidMethod));
        var result = await method!.InvokeAsync(obj, null);
        Assert.True(obj.VoidCalled);
    }

    [Fact]
    public async Task InvokeAsync_AsyncObjectMethod_ReturnsObject()
    {
        var obj = new TestClass();
        var method = typeof(TestClass).GetMethod(nameof(TestClass.AsyncObjectMethod));
        var input = "test";
        var result = await method!.InvokeAsync(obj, new object?[] { input });
        Assert.Equal(input, result);
        Assert.Equal(input, obj.Value);
    }

    [Fact]
    public async Task InvokeAsync_VoidMethod_ReturnsNull()
    {
        var obj = new TestClass();
        var method = typeof(TestClass).GetMethod(nameof(TestClass.VoidMethod));
        var result = await method!.InvokeAsync(obj, null);
        Assert.Null(result);
        Assert.True(obj.VoidCalled);
    }
}