Skip to content

Commit

Permalink
check if method is async and await results, #199
Browse files Browse the repository at this point in the history
Signed-off-by: sriv <srikanth.ddit@gmail.com>
  • Loading branch information
sriv committed Feb 6, 2024
1 parent 92efd92 commit ca9d340
Show file tree
Hide file tree
Showing 9 changed files with 49 additions and 6 deletions.
7 changes: 6 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,9 @@ deploy
artifacts
.ionide
.DS_Store
.fake
.fake
.idea
Gauge.Dotnet.sln.DotSettings.user
_testdata/Sample/.gauge/
_testdata/Sample/logs/
_testdata/Sample/reports/
2 changes: 1 addition & 1 deletion _testdata/Sample/IntegrationTestSample.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework>net7.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
Expand Down
8 changes: 8 additions & 0 deletions _testdata/Sample/StepImplementation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,14 @@ public void SaySomething(string what, string who)
GaugeMessages.WriteMessage("{0}, {1}!", what, who);
}

[Step("Say <what> to <who> async")]
public async Task SaySomethingAsync(string what, string who)
{
Console.WriteLine("{0}, {1}!", what, who);
GaugeMessages.WriteMessage("{0}, {1}!", what, who);
await Task.Delay(100);
}

[Step("I throw an unserializable exception")]
public void ThrowUnserializableException()
{
Expand Down
5 changes: 3 additions & 2 deletions _testdata/Sample/gauge_bin/IntegrationTestSample.deps.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
{
"runtimeTarget": {
"name": ".NETCoreApp,Version=v3.0",
"name": ".NETCoreApp,Version=v7.0/linux-x64",
"signature": ""
},
"compilationOptions": {},
"targets": {
".NETCoreApp,Version=v3.0": {
".NETCoreApp,Version=v7.0": {},
".NETCoreApp,Version=v7.0/linux-x64": {
"IntegrationTestSample/1.0.0": {
"dependencies": {
"Gauge.CSharp.Lib": "0.7.6"
Expand Down
Binary file modified _testdata/Sample/gauge_bin/IntegrationTestSample.dll
Binary file not shown.
Binary file modified _testdata/Sample/gauge_bin/IntegrationTestSample.pdb
Binary file not shown.
23 changes: 22 additions & 1 deletion integration-test/ExecutionOrchestratorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@ public void ShouldExecuteMethodAndReturnResult()
ClassicAssert.False(executionResult.Failed);
}


[Test]
public void ShouldGetPendingMessages()
{
Expand All @@ -103,6 +102,28 @@ public void ShouldGetPendingMessages()
ClassicAssert.Contains("hello, world!", executionResult.Message);
}

[Test]
public void ShouldExecuteAsyncStepImplementation()
{
var reflectionWrapper = new ReflectionWrapper();
var activatorWrapper = new ActivatorWrapper();
var path = new AssemblyLocater(new DirectoryWrapper()).GetTestAssembly();
var assemblyLoader = new AssemblyLoader(path, new GaugeLoadContext(path), reflectionWrapper, activatorWrapper, new StepRegistry());
var classInstanceManager = assemblyLoader.GetClassInstanceManager();
var executionInfoMapper = new ExecutionInfoMapper(assemblyLoader, activatorWrapper);
var executionOrchestrator = new ExecutionOrchestrator(reflectionWrapper, assemblyLoader,
classInstanceManager,
new HookExecutor(assemblyLoader, reflectionWrapper, classInstanceManager, executionInfoMapper),
new StepExecutor(assemblyLoader, reflectionWrapper, classInstanceManager));

var gaugeMethod = assemblyLoader.GetStepRegistry().MethodFor("Say {} to {} async");

var executionResult = executionOrchestrator.ExecuteStep(gaugeMethod, "hello", "async world");

Assert.False(executionResult.Failed, executionResult.ErrorMessage);
Assert.Contains("hello, async world!", executionResult.Message);
}

[Test]
public void ShouldGetStacktraceForAggregateException()
{
Expand Down
2 changes: 1 addition & 1 deletion integration-test/ImplementCodeProcessorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public void ShouldProcessMessageForExistingClass()
var result = processor.Process(message);
ClassicAssert.AreEqual(1, result.TextDiffs.Count);
StringAssert.Contains("Step Method", result.TextDiffs[0].Content);
ClassicAssert.AreEqual(107, result.TextDiffs[0].Span.Start);
ClassicAssert.AreEqual(115, result.TextDiffs[0].Span.Start);
}

[Test]
Expand Down
8 changes: 8 additions & 0 deletions src/MethodExecutor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@


using System;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using Gauge.Dotnet.Wrappers;

namespace Gauge.Dotnet
Expand Down Expand Up @@ -38,6 +40,12 @@ protected void Execute(MethodInfo method, params object[] parameters)
throw new TypeLoadException(error);
}

if (method.GetCustomAttributes(typeof(AsyncStateMachineAttribute), false).Any())
{
((dynamic)_reflectionWrapper.Invoke(method, instance, parameters)).GetAwaiter().GetResult();
return;
}

_reflectionWrapper.Invoke(method, instance, parameters);
}
}
Expand Down

0 comments on commit ca9d340

Please sign in to comment.