-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added handling of fixtures that implements
IAsyncLifetime
, `IAsyncD…
…isposable` (#6) Added parallelization of initialization and destruction of fixtures.
- Loading branch information
Showing
6 changed files
with
315 additions
and
105 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,15 @@ | ||
using Xunit.Abstractions; | ||
|
||
namespace Xunit.Extensions.AssemblyFixture | ||
{ | ||
/// <summary> | ||
/// Used to decorate xUnit.net test classes and collections to indicate a test which has | ||
/// per-assembly fixture data. An instance of the fixture data is initialized just before | ||
/// the first test in the assembly is run, and if it implements IDisposable, is disposed | ||
/// after the last test in the assembly is run. To gain access to the fixture data from | ||
/// inside the test, a constructor argument should be added to the test class which | ||
/// exactly matches the <typeparamref name="TFixture"/>. | ||
/// </summary> | ||
/// <typeparam name="TFixture">The type of the fixture.</typeparam> | ||
public interface IAssemblyFixture<TFixture> where TFixture : class { } | ||
} | ||
namespace Xunit.Extensions.AssemblyFixture | ||
{ | ||
/// <summary> | ||
/// Used to decorate xUnit.net test classes and collections to indicate a test which has | ||
/// per-assembly fixture data. An instance of the fixture data is initialized just before | ||
/// the first test in the assembly is run, and if it implements IDisposable, is disposed | ||
/// after the last test in the assembly is run. To gain access to the fixture data from | ||
/// inside the test, a constructor argument should be added to the test class which | ||
/// exactly matches the <typeparamref name="TFixture"/>. | ||
/// </summary> | ||
/// <typeparam name="TFixture">The type of the fixture.</typeparam> | ||
public interface IAssemblyFixture<TFixture> where TFixture : class { } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
using System.Linq; | ||
using System.Reflection; | ||
using System.Runtime.CompilerServices; | ||
|
||
namespace System | ||
{ | ||
/// <summary> | ||
/// Extension methods for <see cref="Type"/>. | ||
/// </summary> | ||
public static class TypeExtensions | ||
{ | ||
/// <summary> | ||
/// Checks if an instance of <paramref name="givenType"/> can be assigned to a type <paramref name="genericType"/>. | ||
/// </summary> | ||
/// <param name="givenType">The type under test</param> | ||
/// <param name="genericType">The targeted type</param> | ||
/// <returns><c>true</c> if <paramref name="genericType"/> is an ancestor of <paramref name="givenType"/> and <c>false</c> otherwise.</returns> | ||
public static bool IsAssignableToGenericType(this Type givenType, Type genericType) | ||
=> givenType is not null && genericType is not null | ||
&& (givenType == genericType || givenType.MapsToGenericTypeDefinition(genericType) | ||
|| givenType.HasInterfaceThatMapsToGenericTypeDefinition(genericType) | ||
|| givenType.GetTypeInfo().BaseType.IsAssignableToGenericType(genericType)); | ||
|
||
private static bool HasInterfaceThatMapsToGenericTypeDefinition(this Type givenType, Type genericType) | ||
=> givenType is not null && genericType is not null && givenType | ||
.GetTypeInfo() | ||
#if NETSTANDARD1_0 || NETSTANDARD1_1 || NETSTANDARD1_3 | ||
|
||
.ImplementedInterfaces | ||
#else | ||
.GetInterfaces() | ||
#endif | ||
.Where(it => it.GetTypeInfo().IsGenericType) | ||
.Any(it => it.GetGenericTypeDefinition() == genericType); | ||
|
||
private static bool MapsToGenericTypeDefinition(this Type givenType, Type genericType) | ||
=> givenType is not null | ||
&& genericType?.GetTypeInfo().IsGenericTypeDefinition == true | ||
&& givenType.GetTypeInfo().IsGenericType | ||
&& givenType.GetGenericTypeDefinition() == genericType; | ||
|
||
/// <summary> | ||
/// Tests if <paramref name="type"/> is an anonymous type | ||
/// </summary> | ||
/// <param name="type">The type under test</param> | ||
/// <returns><c>true</c>if <paramref name="type"/> is an anonymous type and <c>false</c> otherwise</returns> | ||
public static bool IsAnonymousType(this Type type) | ||
{ | ||
bool hasCompilerGeneratedAttribute = type?.GetTypeInfo()?.GetCustomAttributes(typeof(CompilerGeneratedAttribute), false)?.Any() ?? false; | ||
bool nameContainsAnonymousType = type?.FullName.Contains("AnonymousType") ?? false; | ||
|
||
return hasCompilerGeneratedAttribute && nameContainsAnonymousType; | ||
} | ||
} | ||
} |
Oops, something went wrong.