Skip to content

Commit

Permalink
introduce Options
Browse files Browse the repository at this point in the history
  • Loading branch information
NeVeSpl committed Nov 21, 2023
1 parent fa3b718 commit a4d6d5b
Show file tree
Hide file tree
Showing 23 changed files with 123 additions and 198 deletions.
9 changes: 6 additions & 3 deletions src/NetArchTest.Rules/Condition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ internal Condition(RuleContext rule)
}
internal Condition(RuleContext rule, bool should) : this(rule)
{
rule.ConditionContext.Should= should;
rule.ConditionContext.Should = should;
}


Expand All @@ -34,11 +34,14 @@ private void AddFunctionCall(Func<IEnumerable<TypeSpec>, IEnumerable<TypeSpec>>
{
context.Sequence.AddFunctionCall(func);
}
private void AddFunctionCall(Func<FunctionSequenceExecutionContext, IEnumerable<TypeSpec>, IEnumerable<TypeSpec>> func)
{
context.Sequence.AddFunctionCall(func);
}







/// <summary>
/// Selects types are decorated with a specific custom attribut.
Expand Down
12 changes: 6 additions & 6 deletions src/NetArchTest.Rules/ConditionList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,24 +44,24 @@ public Condition Or()
/// Returns an indication of whether all the selected types satisfy the conditions.
/// </summary>
/// <returns>An indication of whether the conditions are true, along with a list of types failing the check if they are not.</returns>
public TestResult GetResult()
public TestResult GetResult(Options options = null)
{
return rule.GetResult();
return rule.GetResult(options);
}

/// <summary>
/// Returns the list of types that satisfy the conditions.
/// </summary>
/// <returns>A list of types.</returns>
public IEnumerable<IType> GetTypes()
public IEnumerable<IType> GetTypes(Options options = null)
{
return rule.GetTypes();
return rule.GetTypes(options);
}


internal IEnumerable<Type> GetReflectionTypes()
internal IEnumerable<Type> GetReflectionTypes(Options options = null)
{
return rule.GetReflectionTypes();
return rule.GetReflectionTypes(options);
}
}
}
12 changes: 6 additions & 6 deletions src/NetArchTest.Rules/Condition_Dependencies.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public sealed partial class Condition
/// <returns>An updated set of conditions that can be applied to a list of types.</returns>
public ConditionList HaveDependencyOnAny(params string[] dependencies)
{
context.Sequence.AddFunctionCall((context, inputTypes) => FunctionDelegates.HaveDependencyOnAny(context, inputTypes, dependencies, true));
AddFunctionCall((context, inputTypes) => FunctionDelegates.HaveDependencyOnAny(context, inputTypes, dependencies, true));
return CreateConditionList();
}

Expand All @@ -22,7 +22,7 @@ public ConditionList HaveDependencyOnAny(params string[] dependencies)
/// <returns>An updated set of conditions that can be applied to a list of types.</returns>
public ConditionList HaveDependencyOnAll(params string[] dependencies)
{
context.Sequence.AddFunctionCall((context, inputTypes) => FunctionDelegates.HaveDependencyOnAll(context, inputTypes, dependencies, true));
AddFunctionCall((context, inputTypes) => FunctionDelegates.HaveDependencyOnAll(context, inputTypes, dependencies, true));
return CreateConditionList();
}

Expand All @@ -33,7 +33,7 @@ public ConditionList HaveDependencyOnAll(params string[] dependencies)
/// <returns>An updated set of conditions that can be applied to a list of types.</returns>
public ConditionList OnlyHaveDependencyOn(params string[] dependencies)
{
context.Sequence.AddFunctionCall((context, inputTypes) => FunctionDelegates.OnlyHaveDependenciesOnAnyOrNone(context, inputTypes, dependencies, true));
AddFunctionCall((context, inputTypes) => FunctionDelegates.OnlyHaveDependenciesOnAnyOrNone(context, inputTypes, dependencies, true));
return CreateConditionList();
}

Expand All @@ -44,7 +44,7 @@ public ConditionList OnlyHaveDependencyOn(params string[] dependencies)
/// <returns>An updated set of conditions that can be applied to a list of types.</returns>
public ConditionList NotHaveDependencyOnAny(params string[] dependencies)
{
context.Sequence.AddFunctionCall((context, inputTypes) => FunctionDelegates.HaveDependencyOnAny(context, inputTypes, dependencies, false));
AddFunctionCall((context, inputTypes) => FunctionDelegates.HaveDependencyOnAny(context, inputTypes, dependencies, false));
return CreateConditionList();
}

Expand All @@ -55,7 +55,7 @@ public ConditionList NotHaveDependencyOnAny(params string[] dependencies)
/// <returns>An updated set of conditions that can be applied to a list of types.</returns>
public ConditionList NotHaveDependencyOnAll(params string[] dependencies)
{
context.Sequence.AddFunctionCall((context, inputTypes) => FunctionDelegates.HaveDependencyOnAll(context, inputTypes, dependencies, false));
AddFunctionCall((context, inputTypes) => FunctionDelegates.HaveDependencyOnAll(context, inputTypes, dependencies, false));
return CreateConditionList();
}

Expand All @@ -66,7 +66,7 @@ public ConditionList NotHaveDependencyOnAll(params string[] dependencies)
/// <returns>An updated set of conditions that can be applied to a list of types.</returns>
public ConditionList HaveDependencyOtherThan(params string[] dependencies)
{
context.Sequence.AddFunctionCall((context, inputTypes) => FunctionDelegates.OnlyHaveDependenciesOnAnyOrNone(context, inputTypes, dependencies, false));
AddFunctionCall((context, inputTypes) => FunctionDelegates.OnlyHaveDependenciesOnAnyOrNone(context, inputTypes, dependencies, false));
return CreateConditionList();
}
}
Expand Down
58 changes: 9 additions & 49 deletions src/NetArchTest.Rules/Condition_Names.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public sealed partial class Condition
/// <returns>An updated set of conditions that can be applied to a list of types.</returns>
public ConditionList HaveName(string name)
{
AddFunctionCall(x => FunctionDelegates.HaveName(x, name, true));
AddFunctionCall((context, inputTypes) => FunctionDelegates.HaveName(context, inputTypes, name, true));
return CreateConditionList();
}

Expand All @@ -23,7 +23,7 @@ public ConditionList HaveName(string name)
/// <returns>An updated set of conditions that can be applied to a list of types.</returns>
public ConditionList NotHaveName(string name)
{
AddFunctionCall(x => FunctionDelegates.HaveName(x, name, false));
AddFunctionCall((context, inputTypes) => FunctionDelegates.HaveName(context, inputTypes, name, false));
return CreateConditionList();
}

Expand Down Expand Up @@ -56,7 +56,7 @@ public ConditionList NotHaveNameMatching(string pattern)
/// <returns>An updated set of conditions that can be applied to a list of types.</returns>
public ConditionList HaveNameStartingWith(string start)
{
AddFunctionCall(x => FunctionDelegates.HaveNameStartingWith(x, start, true));
AddFunctionCall((context, inputTypes) => FunctionDelegates.HaveNameStartingWith(context, inputTypes, start, true));
return CreateConditionList();
}

Expand All @@ -67,33 +67,9 @@ public ConditionList HaveNameStartingWith(string start)
/// <returns>An updated set of conditions that can be applied to a list of types.</returns>
public ConditionList NotHaveNameStartingWith(string start)
{
AddFunctionCall(x => FunctionDelegates.HaveNameStartingWith(x, start, false));
AddFunctionCall((context, inputTypes) => FunctionDelegates.HaveNameStartingWith(context, inputTypes, start, false));
return CreateConditionList();
}

/// <summary>
/// Selects types whose names start with the specified text.
/// </summary>
/// <param name="start">The text to match against.</param>
/// <param name="comparer">The string comparer.</param>
/// <returns>An updated set of conditions that can be applied to a list of types.</returns>
public ConditionList HaveNameStartingWith(string start, StringComparison comparer)
{
AddFunctionCall(x => FunctionDelegates.HaveNameStartingWith(x, start, true, comparer));
return CreateConditionList();
}

/// <summary>
/// Selects types whose names do not start with the specified text.
/// </summary>
/// <param name="start">The text to match against.</param>
/// <param name="comparer">The string comparer.</param>
/// <returns>An updated set of conditions that can be applied to a list of types.</returns>
public ConditionList NotHaveNameStartingWith(string start, StringComparison comparer)
{
AddFunctionCall(x => FunctionDelegates.HaveNameStartingWith(x, start, false, comparer));
return CreateConditionList();
}
}

/// <summary>
/// Selects types whose names do not end with the specified text.
Expand All @@ -102,7 +78,7 @@ public ConditionList NotHaveNameStartingWith(string start, StringComparison comp
/// <returns>An updated set of conditions that can be applied to a list of types.</returns>
public ConditionList HaveNameEndingWith(string end)
{
AddFunctionCall(x => FunctionDelegates.HaveNameEndingWith(x, end, true));
AddFunctionCall((context, inputTypes) => FunctionDelegates.HaveNameEndingWith(context, inputTypes, end, true));
return CreateConditionList();
}

Expand All @@ -113,7 +89,7 @@ public ConditionList HaveNameEndingWith(string end)
/// <returns>An updated set of conditions that can be applied to a list of types.</returns>
public ConditionList NotHaveNameEndingWith(string end)
{
AddFunctionCall(x => FunctionDelegates.HaveNameEndingWith(x, end, false));
AddFunctionCall((context, inputTypes) => FunctionDelegates.HaveNameEndingWith(context, inputTypes, end, false));
return CreateConditionList();
}

Expand All @@ -123,23 +99,7 @@ public ConditionList NotHaveNameEndingWith(string end)
/// <param name="end">The text to match against.</param>
/// <param name="comparer">The string comparer.</param>
/// <returns>An updated set of conditions that can be applied to a list of types.</returns>
public ConditionList HaveNameEndingWith(string end, StringComparison comparer)
{
AddFunctionCall(x => FunctionDelegates.HaveNameEndingWith(x, end, true, comparer));
return CreateConditionList();
}

/// <summary>
/// Selects types whose names do not end with the specified text.
/// </summary>
/// <param name="end">The text to match against.</param>
/// <param name="comparer">The string comparer.</param>
/// <returns>An updated set of conditions that can be applied to a list of types.</returns>
public ConditionList NotHaveNameEndingWith(string end, StringComparison comparer)
{
AddFunctionCall(x => FunctionDelegates.HaveNameEndingWith(x, end, false, comparer));
return CreateConditionList();
}



Expand Down Expand Up @@ -192,7 +152,7 @@ public ConditionList NotResideInNamespaceMatching(string pattern)
/// </summary>
/// <param name="name">The namespace part to match against.</param>
/// <returns>An updated set of predicates that can be applied to a list of types.</returns>
public ConditionList ResideInNamespaceStartingWith(string name)
internal ConditionList ResideInNamespaceStartingWith(string name)
{
AddFunctionCall(x => FunctionDelegates.ResideInNamespaceMatching(x, $"^{name}", true));
return CreateConditionList();
Expand All @@ -203,7 +163,7 @@ public ConditionList ResideInNamespaceStartingWith(string name)
/// </summary>
/// <param name="name">The namespace part to match against.</param>
/// <returns>An updated set of predicates that can be applied to a list of types.</returns>
public ConditionList NotResideInNamespaceStartingWith(string name)
internal ConditionList NotResideInNamespaceStartingWith(string name)
{
AddFunctionCall(x => FunctionDelegates.ResideInNamespaceMatching(x, $"^{name}", false));
return CreateConditionList();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
namespace System.Runtime.CompilerServices
{
internal static class IsExternalInit { }
}
19 changes: 10 additions & 9 deletions src/NetArchTest.Rules/Functions/FunctionDelegates_Names.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,24 @@
using System.Text.RegularExpressions;
using Mono.Cecil;
using NetArchTest.Assemblies;
using NetArchTest.RuleEngine;

namespace NetArchTest.Functions
{
internal static partial class FunctionDelegates
{
// Name & Namespace

internal static IEnumerable<TypeSpec> HaveName(IEnumerable<TypeSpec> input, string name, bool condition)
internal static IEnumerable<TypeSpec> HaveName(FunctionSequenceExecutionContext context, IEnumerable<TypeSpec> input, string name, bool condition)
{
var plainName = name.RemoveGenericPart();
if (condition)
{
return input.Where(c => c.Definition.GetName().Equals(plainName, StringComparison.InvariantCultureIgnoreCase));
return input.Where(c => c.Definition.GetName().Equals(plainName, context.UserOptions.Comparer));
}
else
{
return input.Where(c => !c.Definition.GetName().Equals(plainName, StringComparison.InvariantCultureIgnoreCase));
return input.Where(c => !c.Definition.GetName().Equals(plainName, context.UserOptions.Comparer));
}
}

Expand All @@ -37,27 +38,27 @@ internal static IEnumerable<TypeSpec> HaveNameMatching(IEnumerable<TypeSpec> inp
}
}

internal static IEnumerable<TypeSpec> HaveNameStartingWith(IEnumerable<TypeSpec> input, string start, bool condition, StringComparison comparer = StringComparison.InvariantCultureIgnoreCase)
internal static IEnumerable<TypeSpec> HaveNameStartingWith(FunctionSequenceExecutionContext context, IEnumerable<TypeSpec> input, string start, bool condition)
{
if (condition)
{
return input.Where(c => c.Definition.GetName().StartsWith(start, comparer));
return input.Where(c => c.Definition.GetName().StartsWith(start, context.UserOptions.Comparer));
}
else
{
return input.Where(c => !c.Definition.GetName().StartsWith(start, comparer));
return input.Where(c => !c.Definition.GetName().StartsWith(start, context.UserOptions.Comparer));
}
}

internal static IEnumerable<TypeSpec> HaveNameEndingWith(IEnumerable<TypeSpec> input, string end, bool condition, StringComparison comparer = StringComparison.InvariantCultureIgnoreCase)
internal static IEnumerable<TypeSpec> HaveNameEndingWith(FunctionSequenceExecutionContext context, IEnumerable<TypeSpec> input, string end, bool condition)
{
if (condition)
{
return input.Where(c => c.Definition.GetName().EndsWith(end, comparer));
return input.Where(c => c.Definition.GetName().EndsWith(end, context.UserOptions.Comparer));
}
else
{
return input.Where(c => !c.Definition.GetName().EndsWith(end, comparer));
return input.Where(c => !c.Definition.GetName().EndsWith(end, context.UserOptions.Comparer));
}
}

Expand Down
1 change: 1 addition & 0 deletions src/NetArchTest.Rules/NetArchTest.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
<FileVersion>1.3.7.0</FileVersion>
<PackageId>NetArchTest.eNhancedEdition </PackageId>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<LangVersion>10</LangVersion>
</PropertyGroup>

<ItemGroup>
Expand Down
14 changes: 14 additions & 0 deletions src/NetArchTest.Rules/Options.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System;

namespace NetArchTest
{
public record class Options
{
public static readonly Options Default = new Options();


public StringComparison Comparer { get; init; } = StringComparison.InvariantCultureIgnoreCase;


}
}
5 changes: 4 additions & 1 deletion src/NetArchTest.Rules/Predicate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,12 @@ private void AddFunctionCall(Func<IEnumerable<TypeSpec>, IEnumerable<TypeSpec>>
{
context.Sequence.AddFunctionCall(func);
}
private void AddFunctionCall(Func<FunctionSequenceExecutionContext, IEnumerable<TypeSpec>, IEnumerable<TypeSpec>> func)
{
context.Sequence.AddFunctionCall(func);
}




/// <summary>
/// Selects types that are decorated with a specific custom attribute.
Expand Down
12 changes: 6 additions & 6 deletions src/NetArchTest.Rules/PredicateList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,19 +73,19 @@ public Predicate Or()
/// Returns the types returned by these predicates.
/// </summary>
/// <returns>A list of types.</returns>
public IEnumerable<IType> GetTypes()
public IEnumerable<IType> GetTypes(Options options = null)
{
return rule.GetTypes();
return rule.GetTypes(options);
}


internal IEnumerable<TypeSpec> GetTypeSpecifications()
internal IEnumerable<TypeSpec> GetTypeSpecifications(Options options = null)
{
return rule.Execute();
return rule.Execute(options);
}
internal IEnumerable<Type> GetReflectionTypes()
internal IEnumerable<Type> GetReflectionTypes(Options options = null)
{
return rule.GetReflectionTypes();
return rule.GetReflectionTypes(options);
}
}
}
Loading

0 comments on commit a4d6d5b

Please sign in to comment.