Skip to content

Commit

Permalink
Merge pull request #36 from messerli-informatik-ag/excluded-types
Browse files Browse the repository at this point in the history
Add attribute for excluding specific types
  • Loading branch information
Jan Nils Ferner authored Apr 3, 2020
2 parents 2360dfe + 9a0d00b commit 093a89f
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ public void ThrowsWhenAssemblyIsNotFound()

[Theory]
[TypesThatNeedToBeImplementedInAssemblyData(TestAssemblyName)]
[ExcludedTypes(typeof(IInterfaceWithMethod), typeof(ImplementationFactory))]
[ExcludedTypes(typeof(AbstractClassWithProperty))]
public void AttributeSmokeTest(Type type)
{
Assert.Contains(type, TypesThatNeedToBeImplementedInAssembly);
Expand Down
19 changes: 19 additions & 0 deletions Test.Utility/ExcludedTypesAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using System;
using System.Collections.Generic;

namespace Messerli.Test.Utility
{
/// <summary>
/// Blacklists a hardcoded list of types. Used together with <see cref="TypesThatNeedToBeImplementedInAssemblyDataAttribute"/>.
/// </summary>
[AttributeUsage(AttributeTargets.Method, AllowMultiple = true)]
public class ExcludedTypesAttribute : Attribute
{
public ExcludedTypesAttribute(params Type[] types)
{
Types = types;
}

public IEnumerable<Type> Types { get; }
}
}
2 changes: 1 addition & 1 deletion Test.Utility/Test.Utility.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
</PropertyGroup>
<PropertyGroup>
<PackageId>Messerli.Test.Utility</PackageId>
<Version>0.7.2</Version>
<Version>0.7.3</Version>
<Description>Class library to simplify test-setups.</Description>
<RepositoryUrl>https://github.com/messerli-informatik-ag/test-utility</RepositoryUrl>
<RepositoryType>git</RepositoryType>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using Xunit.Sdk;
Expand All @@ -8,6 +9,7 @@ namespace Messerli.Test.Utility
/// <summary>
/// <para>
/// Provides all types that need to be implemented in an assembly as theory data.
/// You can use <see cref="ExcludedTypesAttribute" /> to exclude types.
/// </para>
/// <para>
/// The following types need to be implemented (excluding generic types):
Expand Down Expand Up @@ -47,8 +49,20 @@ public TypesThatNeedToBeImplementedInAssemblyDataAttribute(string assemblyName)
}

public override IEnumerable<object[]> GetData(MethodInfo testMethod)
=> TypesThatNeedToBeImplementedInAssemblyRetriever
{
var excludedTypes = CollectExcludedTypes(testMethod);
return TypesThatNeedToBeImplementedInAssemblyRetriever
.GetTypesThatNeedToBeImplementedInAssembly(_assemblyName)
.Where(type => !excludedTypes.Contains(type))
.Select(type => new object[] { type });
}

private static ISet<Type> CollectExcludedTypes(MethodInfo testMethod)
{
var types = testMethod
.GetCustomAttributes<ExcludedTypesAttribute>()
.SelectMany(attr => attr.Types);
return new HashSet<Type>(types);
}
}
}
3 changes: 3 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,6 @@ Rerelease of 0.6.0 with fixed nuget package.

## 0.7.2
- Add extension method `GetRegisteredTypes` on `IComponentContext` to retrieve all registered types.

## 0.7.3
- Allow blacklisting of types on `TypesThatNeedToBeImplementedInAssemblyData` attribute using a new attribute: `ExcludedTypes`.
3 changes: 3 additions & 0 deletions global.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
{
"sdk": {
"version": "3.1.201"
},
"msbuild-sdks": {
"Microsoft.Build.CentralPackageVersions": "2.0.52"
}
Expand Down

0 comments on commit 093a89f

Please sign in to comment.