Skip to content

Commit

Permalink
#12 Begin adding tests for replacing async methods
Browse files Browse the repository at this point in the history
  • Loading branch information
Miista committed May 2, 2024
1 parent 3663b77 commit afcce01
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 18 deletions.
4 changes: 2 additions & 2 deletions src/Pose/Pose.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)' == 'Debug' ">
<DebugSymbols>false</DebugSymbols>
<DebugType>none</DebugType>
<DefineConstants />
<DebugType>full</DebugType>
<DefineConstants/>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Mono.Reflection.Core" Version="1.1.1" />
Expand Down
3 changes: 2 additions & 1 deletion src/Pose/PoseContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ public static async Task Isolate(Func<Task> entryPoint, params Shim[] shims)
#endif

// ReSharper disable once PossibleNullReferenceException
await (methodInfo.CreateDelegate(delegateType).DynamicInvoke() as Task);
var task = methodInfo.CreateDelegate(delegateType).DynamicInvoke() as Task;
await task;
}
}
}
27 changes: 12 additions & 15 deletions src/Sandbox/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,27 +24,24 @@ public static async Task DoWorkAsync()
await Task.Delay(1000);
}

private static async Task Run(MyClass myClass)
{
await myClass.DoSomethingAsync();
}

public static async Task Main(string[] args)
{
var staticAsyncShim = Shim.Replace(() => DoWorkAsync()).With(
delegate
{
Console.Write("Don't do work!");
return Task.CompletedTask;
});
var taskShim = Shim.Replace(() => Is.A<MyClass>().DoSomethingAsync())
.With(delegate(MyClass @this)
var myClass = new MyClass();
var myShim = Shim.Replace(() => myClass.DoSomethingAsync())
.With(
delegate (MyClass @this)
{
Console.WriteLine("Shimming async Task");
Console.WriteLine("LOL");
return Task.CompletedTask;
}
);
await PoseContext.Isolate(
async () =>
{
await DoWorkAsync();
}, staticAsyncShim
);

PoseContext.Isolate(() => Run(myClass), myShim);

// #if NET48
// Console.WriteLine("4.8");
Expand Down
56 changes: 56 additions & 0 deletions test/Pose.Tests/ShimTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -793,6 +793,62 @@ public void Can_shim_constructor_of_sealed_reference_type()

public class AsyncMethods
{
public class General
{
private class MyClass
{
public async Task DoSomethingAsync() => await Task.CompletedTask;
}

[Fact]
public void Can_replace_async_instance_method_for_specific_instance()
{
// Arrange
var myClass = new MyClass();
var shim = Shim.Replace(() => myClass.DoSomethingAsync());

// Act
Action act = () =>
{
shim
.With(
delegate (MyClass @this)
{
Console.WriteLine("LOL");
return Task.CompletedTask;
}
);
};

// Assert
act.Should().NotThrow(because: "the async method can be replaced");
}

[Fact]
public void Can_replace_async_instance_method_for_specific_instance_with_async_delegate()
{
// Arrange
var myClass = new MyClass();
var shim = Shim.Replace(() => myClass.DoSomethingAsync());

// Act
Action act = () =>
{
shim
.With(
delegate (MyClass @this)
{
Console.WriteLine("LOL");
return Task.CompletedTask;
}
);
};

// Assert
act.Should().NotThrow(because: "the async method can be replaced");
}
}

public class StaticTypes
{
private class Instance
Expand Down

0 comments on commit afcce01

Please sign in to comment.