Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 6297db9

Browse files
authoredJan 9, 2024
Adds global type filtering (#90)
1 parent 170ac38 commit 6297db9

File tree

5 files changed

+70
-8
lines changed

5 files changed

+70
-8
lines changed
 

‎src/Core/Conventional.Tests/AsyncConformistScenarios.cs

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,24 +44,55 @@ public override async Task<ConventionResult> IsSatisfiedBy(Type type)
4444

4545
public class AsyncConformistScenarios
4646
{
47+
[SetUp]
48+
public void Setup()
49+
{
50+
ConventionConfiguration.GlobalTypeFilter = type => type != typeof(YouCantSeeMe);
51+
}
52+
4753
[Test]
4854
public void HappyPath_DoesNotThrowExceptions()
4955
{
50-
Action action = async () => await typeof(String).MustConformTo(new AlwaysSuccessfulAsyncConvention());
56+
Action action = async () => await typeof(String).MustConformTo(new AlwaysSuccessfulAsyncConvention());
5157

5258
action.ShouldNotThrow();
5359
}
5460

5561
[Test]
5662
public async Task FluentSyntax_OutputsExpectedFailuresInCorrectOrder()
5763
{
58-
var results = await new[] {typeof(String)}
64+
var results = await new[] { typeof(String) }
5965
.MustConformTo(new NeverSuccessfulAsyncConvention())
6066
.AndMustConformTo(new AlsoNeverSuccessfulAsyncConvention());
6167

6268
results.Failures.Should().HaveCount(2);
6369
results.Failures[0].Should().StartWith("I failed!");
6470
results.Failures[1].Should().StartWith("I also failed!");
6571
}
72+
73+
private class YouCantSeeMe
74+
{
75+
public string Name { get; set; }
76+
}
77+
78+
private class YouCanSeeMe
79+
{
80+
public string Name { get; set; }
81+
}
82+
83+
[Test]
84+
public async Task WhenDefaultTypeFilterIsSet_ItIsAppliedToAllConventions()
85+
{
86+
var result = await new[] { typeof(YouCantSeeMe), typeof(YouCanSeeMe) }
87+
.MustConformTo(new NeverSuccessfulAsyncConvention());
88+
89+
result.Failures.Should().HaveCount(1);
90+
}
91+
92+
[TearDown]
93+
public void TearDown()
94+
{
95+
ConventionConfiguration.ResetGlobalTypeFilter();
96+
}
6697
}
6798
}

‎src/Core/Conventional.Tests/ConventionConfigurationScenarios.cs

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using NUnit.Framework;
1+
using FluentAssertions;
2+
using NUnit.Framework;
23

34
namespace Conventional.Tests
45
{
@@ -8,8 +9,9 @@ public class ConventionConfigurationScenarios
89
public void Setup()
910
{
1011
ConventionConfiguration.DefaultFailureAssertionCallback = Assert.Pass;
12+
ConventionConfiguration.GlobalTypeFilter = type => type != typeof(YouCantSeeMe);
1113
}
12-
14+
1315
private class AbjectConformanceFailure
1416
{
1517
public AbjectConformanceFailure(string name, string description)
@@ -39,11 +41,30 @@ public void WhenDefaultFailureAssertionIsSet_AndWeHaveACompositeConvention_Autom
3941
Convention.PropertiesMustHavePublicSetters.And(
4042
Convention.MustHaveADefaultConstructor)));
4143
}
44+
45+
private class YouCantSeeMe
46+
{
47+
public string Name { get; set; }
48+
}
49+
50+
private class YouCanSeeMe
51+
{
52+
public string Name { get; set; }
53+
}
54+
55+
[Test]
56+
public void WhenDefaultTypeFilterIsSet_ItIsAppliedToAllConventions()
57+
{
58+
new[] { typeof(YouCantSeeMe), typeof(YouCanSeeMe) }
59+
.MustConformTo(Convention.PropertiesMustHavePrivateSetters)
60+
.Failures.Should().HaveCount(1);
61+
}
4262

4363
[TearDown]
4464
public void TearDown()
4565
{
4666
ConventionConfiguration.DefaultFailureAssertionCallback = null;
67+
ConventionConfiguration.ResetGlobalTypeFilter();
4768
}
4869
}
4970
}

‎src/Core/Conventional/AsyncConformist.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,11 @@ public static async Task<ConventionResult> MustConformTo(this Type type, IAsyncC
1515

1616
public static async Task<WrappedConventionResult> MustConformTo(this IEnumerable<Type> types, IAsyncConventionSpecification conventionSpecification)
1717
{
18+
var filteredTypes = types.Where(ConventionConfiguration.GlobalTypeFilter).ToArray();
19+
1820
return Conformist.EnforceConformance(new WrappedConventionResult(
19-
types,
20-
await Task.WhenAll(types.Select(conventionSpecification.IsSatisfiedBy))));
21+
filteredTypes,
22+
await Task.WhenAll(filteredTypes.Select(conventionSpecification.IsSatisfiedBy))));
2123
}
2224

2325
public static async Task<WrappedConventionResult> AndMustConformTo(this Task<WrappedConventionResult> results, IAsyncConventionSpecification conventionSpecification)

‎src/Core/Conventional/Conformist.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,11 @@ public static ConventionResult MustConformTo(this Type type, IConventionSpecific
1515

1616
public static WrappedConventionResult MustConformTo(this IEnumerable<Type> types, IConventionSpecification conventionSpecification)
1717
{
18+
var filteredTypes = types.Where(ConventionConfiguration.GlobalTypeFilter).ToArray();
19+
1820
return EnforceConformance(new WrappedConventionResult(
19-
types,
20-
types.Select(conventionSpecification.IsSatisfiedBy)));
21+
filteredTypes,
22+
filteredTypes.Select(conventionSpecification.IsSatisfiedBy)));
2123
}
2224

2325
public static ConventionResult MustConformTo(this Assembly assembly, IAssemblyConventionSpecification assemblyConventionSpecification)

‎src/Core/Conventional/ConventionConfiguration.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,11 @@ public static class ConventionConfiguration
77
public static Action<string> DefaultFailureAssertionCallback { get; set; }
88
public static Action<string> DefaultWarningAssertionCallback { get; set; }
99
public static Func<DateTime> DefaultCurrentDateResolver { get; set; } = () => DateTime.UtcNow;
10+
private static readonly Func<Type, bool> DefaultGlobalTypeFilter = t => true;
11+
public static Func<Type, bool> GlobalTypeFilter { get; set; } = DefaultGlobalTypeFilter;
12+
public static void ResetGlobalTypeFilter()
13+
{
14+
GlobalTypeFilter = DefaultGlobalTypeFilter;
15+
}
1016
}
1117
}

0 commit comments

Comments
 (0)
Failed to load comments.