Skip to content

Commit

Permalink
#12: Add tests for async stubbing and getting the MoveNext method
Browse files Browse the repository at this point in the history
  • Loading branch information
Miista committed May 2, 2024
1 parent 9ed298f commit 95b9f35
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 0 deletions.
35 changes: 35 additions & 0 deletions test/Pose.Tests/Extensions/TypeExtensionsTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using System;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;
using FluentAssertions;
using Pose.Extensions;
using Xunit;

namespace Pose.Tests
{
public class TypeExtensionsTests
{
private static async Task<int> GetIntAsync() => await Task.FromResult(1);

[Fact]
public void Can_get_explicitly_implemented_MoveNext_method_on_state_machine()
{
// Arrange
var stateMachineType = typeof(TypeExtensionsTests).GetMethod(nameof(GetIntAsync), BindingFlags.Static | BindingFlags.NonPublic)?.GetCustomAttribute<AsyncStateMachineAttribute>()?.StateMachineType;

// Act
Func<MethodInfo> func = () => stateMachineType.GetExplicitlyImplementedMethod<IAsyncStateMachine>(nameof(IAsyncStateMachine.MoveNext));

// Assert
func.Should().NotThrow(because: "it is possible to get the MoveNext method on the state machine");

var moveNextMethod = func();
moveNextMethod.Should().NotBeNull(because: "the method exists");
moveNextMethod.ReturnType.Should().Be(typeof(void));

var parameters = moveNextMethod.GetParameters();
parameters.Should().BeEmpty(because: "the method does not take any parameters");
}
}
}
24 changes: 24 additions & 0 deletions test/Pose.Tests/IL/StubsTests.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;
using FluentAssertions;
using Pose.Extensions;
using Pose.IL;
using Xunit;

Expand Down Expand Up @@ -87,6 +91,26 @@ public void Can_generate_stub_for_virtual_call()
valueParameter.ParameterType.Should().Be(typeof(string), because: "the second parameter is the value to be added");
}

private static async Task<int> GetIntAsync() => await Task.FromResult(1);

[Fact]
public void Can_generate_stub_for_async_virtual_call()
{
// Arrange
var stateMachineType = typeof(StubsTests)?.GetMethod(nameof(GetIntAsync), BindingFlags.Static | BindingFlags.NonPublic)?.GetCustomAttribute<AsyncStateMachineAttribute>()?.StateMachineType;
var moveNextMethod = stateMachineType.GetExplicitlyImplementedMethod<IAsyncStateMachine>(nameof(IAsyncStateMachine.MoveNext));

// Act
var dynamicMethod = Stubs.GenerateStubForVirtualCall(moveNextMethod);

// Assert
var dynamicParameters = dynamicMethod.GetParameters();
dynamicParameters.Should().HaveCount(1, because: "the dynamic method takes only the instance parameter");

var instanceParameter = dynamicParameters[0];
instanceParameter.ParameterType.Should().Be(stateMachineType, because: "the first parameter is the instance");
}

[Fact]
public void Can_generate_stub_for_reference_type_constructor()
{
Expand Down

0 comments on commit 95b9f35

Please sign in to comment.