Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 27 additions & 1 deletion src/VsixTesting.Xunit/Internal/VsTestInvoker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -50,6 +52,18 @@ protected override object CallTestMethod(object testClassInstance)
}
}

protected override Task<decimal> 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
Expand All @@ -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")));
}
}
}
}
15 changes: 15 additions & 0 deletions src/VsixTesting/Common/MethodInfoExtensions.cs
Original file line number Diff line number Diff line change
@@ -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;
}
}
}
13 changes: 12 additions & 1 deletion test/VsixTesting.Xunit.Tests/VsFactTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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());
}
Expand Down Expand Up @@ -121,6 +124,14 @@ async void ContinuationRunsOnSameSynchronizationContext()
await Task.Yield();
Assert.IsType<DispatcherSynchronizationContext>(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
Expand Down
3 changes: 2 additions & 1 deletion test/VsixTesting.Xunit.Tests/VsTheoryTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
Expand Down