diff --git a/src/VsixTesting.Xunit/Internal/VsTestInvoker.cs b/src/VsixTesting.Xunit/Internal/VsTestInvoker.cs index 3aece46..f43264d 100644 --- a/src/VsixTesting.Xunit/Internal/VsTestInvoker.cs +++ b/src/VsixTesting.Xunit/Internal/VsTestInvoker.cs @@ -7,12 +7,14 @@ namespace VsixTesting.XunitX.Internal using System.Diagnostics; using System.IO; using System.Reflection; + using System.Runtime.CompilerServices; using System.Threading; - using System.Windows; + using System.Threading.Tasks; using VsixTesting.Utilities; using VsixTesting.XunitX.Internal.Utilities; using Xunit.Abstractions; using Xunit.Sdk; + using Common; internal class VsTestInvoker : XunitTestInvoker { @@ -50,6 +52,18 @@ protected override object CallTestMethod(object testClassInstance) } } + protected override Task InvokeTestMethodAsync(object testClassInstance) + { + if (Settings.UIThread && !TestMethod.IsAsync()) + { + ValidateTestMethodParameters(); + Timer.Aggregate(() => CallTestMethod(testClassInstance)); + return Task.FromResult(Timer.Total); + } + + return base.InvokeTestMethodAsync(testClassInstance); + } + private void TakeScreenShot(string name) { try @@ -66,5 +80,17 @@ private void TakeScreenShot(string name) Aggregator.Add(new Exception("Failed saving screenshot", e)); } } + + // https://github.com/xunit/xunit/src/xunit.execution/Sdk/Frameworks/Runners/TestInvoker.cs#L251 + private void ValidateTestMethodParameters() + { + int parameterCount = TestMethod.GetParameters().Length; + int valueCount = (TestMethodArguments != null) ? TestMethodArguments.Length : 0; + if (parameterCount != valueCount) + { + Aggregator.Add(new InvalidOperationException(string.Format( + "The test method expected {0} parameter value{1}, but {2} parameter value{3} {4} provided.", parameterCount, (parameterCount == 1) ? string.Empty : "s", valueCount, (valueCount == 1) ? string.Empty : "s", (valueCount == 1) ? "was" : "were"))); + } + } } } diff --git a/src/VsixTesting/Common/MethodInfoExtensions.cs b/src/VsixTesting/Common/MethodInfoExtensions.cs new file mode 100644 index 0000000..7889c18 --- /dev/null +++ b/src/VsixTesting/Common/MethodInfoExtensions.cs @@ -0,0 +1,15 @@ +// Copyright (c) 2018 Jose Torres. All rights reserved. Licensed under the Apache License, Version 2.0. See LICENSE.md file in the project root for full license information. + +namespace Common +{ + using System.Reflection; + using System.Runtime.CompilerServices; + + internal static class MethodInfoExtensions + { + public static bool IsAsync(this MethodInfo methodInfo) + { + return methodInfo.GetCustomAttribute(typeof(AsyncStateMachineAttribute)) != null; + } + } +} diff --git a/test/VsixTesting.Xunit.Tests/VsFactTests.cs b/test/VsixTesting.Xunit.Tests/VsFactTests.cs index 113714a..6910dc0 100644 --- a/test/VsixTesting.Xunit.Tests/VsFactTests.cs +++ b/test/VsixTesting.Xunit.Tests/VsFactTests.cs @@ -6,6 +6,8 @@ namespace VsixTesting.XunitX.Tests using System.Diagnostics; using System.IO; using System.Threading; + using System.Threading.Tasks; + using System.Windows; using System.Windows.Threading; using Microsoft.VisualStudio.ComponentModelHost; using Microsoft.VisualStudio.Shell; @@ -90,8 +92,9 @@ void MethodHasSameManagedThreadAsConstructor() => Assert.Equal(constructorThreadId, Thread.CurrentThread.ManagedThreadId); [VsFact] - void MethodHasSameSyncContextTypeAsConstructor() + async void MethodHasSameSyncContextTypeAsConstructor() { + await Task.Delay(0); var asyncTestSyncContext = (AsyncTestSyncContext)SynchronizationContext.Current; Assert.IsType(synchronizationContext.GetType(), asyncTestSyncContext.GetInnerSyncContext()); } @@ -121,6 +124,14 @@ async void ContinuationRunsOnSameSynchronizationContext() await Task.Yield(); Assert.IsType(SynchronizationContext.Current); } + + [VsFact] + void DteWindow() + { + var dte = (EnvDTE.DTE)ServiceProvider.GlobalProvider.GetService(typeof(SDTE)); + var window = dte.ItemOperations.NewFile(Name: Guid.NewGuid() + ".txt"); + window.Close(EnvDTE.vsSaveChanges.vsSaveChangesNo); + } } public class ServiceProviderTests diff --git a/test/VsixTesting.Xunit.Tests/VsTheoryTests.cs b/test/VsixTesting.Xunit.Tests/VsTheoryTests.cs index cda9b9e..a7af25f 100644 --- a/test/VsixTesting.Xunit.Tests/VsTheoryTests.cs +++ b/test/VsixTesting.Xunit.Tests/VsTheoryTests.cs @@ -94,8 +94,9 @@ void MethodHasSameManagedThreadAsConstructor(int zero) [VsTheory] [InlineData(0)] - void MethodHasSameSyncContextTypeAsConstructor(int zero) + async void MethodHasSameSyncContextTypeAsConstructor(int zero) { + await Task.Delay(0); var asyncTestSyncContext = (AsyncTestSyncContext)SynchronizationContext.Current; Assert.IsType(synchronizationContext.GetType(), asyncTestSyncContext.GetInnerSyncContext()); }