Skip to content

Commit

Permalink
Merge pull request #145 from brandhuf/fix-name-matching
Browse files Browse the repository at this point in the history
Change matching of name and fullname patterns to exact match by default
  • Loading branch information
fgather authored Mar 11, 2022
2 parents bb21423 + f531140 commit 300a999
Show file tree
Hide file tree
Showing 20 changed files with 297 additions and 420 deletions.
5 changes: 3 additions & 2 deletions ArchUnitNET/Domain/Extensions/NamingExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
// SPDX-License-Identifier: Apache-2.0
//

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
Expand Down Expand Up @@ -37,7 +38,7 @@ public static bool NameMatches(this IHasName cls, string pattern, bool useRegula
return pattern != null && Regex.IsMatch(cls.Name, pattern);
}

return cls.NameContains(pattern);
return string.Equals(cls.Name, pattern, StringComparison.OrdinalIgnoreCase);
}

public static bool FullNameMatches(this IHasName cls, string pattern, bool useRegularExpressions = false)
Expand All @@ -47,7 +48,7 @@ public static bool FullNameMatches(this IHasName cls, string pattern, bool useRe
return pattern != null && Regex.IsMatch(cls.FullName, pattern);
}

return cls.FullNameContains(pattern);
return string.Equals(cls.FullName, pattern, StringComparison.OrdinalIgnoreCase);
}

public static bool FullNameContains(this IHasName cls, string pattern)
Expand Down
3 changes: 1 addition & 2 deletions ArchUnitNET/Domain/Extensions/TypeExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using ArchUnitNET.Domain.Dependencies;

namespace ArchUnitNET.Domain.Extensions
Expand All @@ -30,7 +29,7 @@ public static IEnumerable<IType> GetAssignableTypes(this IType type)
switch (type)
{
case Interface intf:
return intf.ImplementedInterfaces.Concat(new[] { intf });
return intf.ImplementedInterfaces.Concat(new[] {intf});
case Class cls:
return cls.InheritedClasses.Concat(new[] {cls}).Concat(cls.ImplementedInterfaces);
case Struct str:
Expand Down
40 changes: 8 additions & 32 deletions ArchUnitNET/Fluent/Syntax/Elements/GivenObjectsThat.cs
Original file line number Diff line number Diff line change
Expand Up @@ -382,27 +382,15 @@ public TGivenRuleTypeConjunction HaveAttributeWithNamedArguments(Type attribute,
return Create<TGivenRuleTypeConjunction, TRuleType>(_ruleCreator);
}

public TGivenRuleTypeConjunction HaveName(string name)
public TGivenRuleTypeConjunction HaveName(string pattern, bool useRegularExpressions = false)
{
_ruleCreator.AddPredicate(ObjectPredicatesDefinition<TRuleType>.HaveName(name));
_ruleCreator.AddPredicate(ObjectPredicatesDefinition<TRuleType>.HaveName(pattern, useRegularExpressions));
return Create<TGivenRuleTypeConjunction, TRuleType>(_ruleCreator);
}

public TGivenRuleTypeConjunction HaveNameMatching(string pattern, bool useRegularExpressions = false)
public TGivenRuleTypeConjunction HaveFullName(string pattern, bool useRegularExpressions = false)
{
_ruleCreator.AddPredicate(ObjectPredicatesDefinition<TRuleType>.HaveNameMatching(pattern, useRegularExpressions));
return Create<TGivenRuleTypeConjunction, TRuleType>(_ruleCreator);
}

public TGivenRuleTypeConjunction HaveFullName(string fullname)
{
_ruleCreator.AddPredicate(ObjectPredicatesDefinition<TRuleType>.HaveFullName(fullname));
return Create<TGivenRuleTypeConjunction, TRuleType>(_ruleCreator);
}

public TGivenRuleTypeConjunction HaveFullNameMatching(string pattern, bool useRegularExpressions = false)
{
_ruleCreator.AddPredicate(ObjectPredicatesDefinition<TRuleType>.HaveFullNameMatching(pattern, useRegularExpressions));
_ruleCreator.AddPredicate(ObjectPredicatesDefinition<TRuleType>.HaveFullName(pattern, useRegularExpressions));
return Create<TGivenRuleTypeConjunction, TRuleType>(_ruleCreator);
}

Expand Down Expand Up @@ -732,27 +720,15 @@ public TGivenRuleTypeConjunction DoNotHaveAttributeWithNamedArguments(Type attri
return Create<TGivenRuleTypeConjunction, TRuleType>(_ruleCreator);
}

public TGivenRuleTypeConjunction DoNotHaveName(string name)
{
_ruleCreator.AddPredicate(ObjectPredicatesDefinition<TRuleType>.DoNotHaveName(name));
return Create<TGivenRuleTypeConjunction, TRuleType>(_ruleCreator);
}

public TGivenRuleTypeConjunction DoNotHaveNameMatching(string pattern, bool useRegularExpressions = false)
{
_ruleCreator.AddPredicate(ObjectPredicatesDefinition<TRuleType>.DoNotHaveNameMatching(pattern, useRegularExpressions));
return Create<TGivenRuleTypeConjunction, TRuleType>(_ruleCreator);
}

public TGivenRuleTypeConjunction DoNotHaveFullName(string fullname)
public TGivenRuleTypeConjunction DoNotHaveName(string pattern, bool useRegularExpressions = false)
{
_ruleCreator.AddPredicate(ObjectPredicatesDefinition<TRuleType>.DoNotHaveFullName(fullname));
_ruleCreator.AddPredicate(ObjectPredicatesDefinition<TRuleType>.DoNotHaveName(pattern, useRegularExpressions));
return Create<TGivenRuleTypeConjunction, TRuleType>(_ruleCreator);
}

public TGivenRuleTypeConjunction DoNotHaveFullNameMatching(string pattern, bool useRegularExpressions = false)
public TGivenRuleTypeConjunction DoNotHaveFullName(string pattern, bool useRegularExpressions = false)
{
_ruleCreator.AddPredicate(ObjectPredicatesDefinition<TRuleType>.DoNotHaveFullNameMatching(pattern, useRegularExpressions));
_ruleCreator.AddPredicate(ObjectPredicatesDefinition<TRuleType>.DoNotHaveFullName(pattern, useRegularExpressions));
return Create<TGivenRuleTypeConjunction, TRuleType>(_ruleCreator);
}

Expand Down
12 changes: 4 additions & 8 deletions ArchUnitNET/Fluent/Syntax/Elements/IObjectConditions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,8 @@ public interface IObjectConditions<out TReturnType, out TRuleType> where TRuleTy
TReturnType HaveAttributeWithNamedArguments(Attribute attribute, (string,object) firstAttributeArgument, params (string,object)[] moreAttributeArguments);
TReturnType HaveAttributeWithNamedArguments(Type attribute, IEnumerable<(string,object)> attributeArguments);
TReturnType HaveAttributeWithNamedArguments(Type attribute, (string,object) firstAttributeArgument, params (string,object)[] moreAttributeArguments);
TReturnType HaveName(string name);
TReturnType HaveNameMatching(string pattern, bool useRegularExpressions = false);
TReturnType HaveFullName(string fullname);
TReturnType HaveFullNameMatching(string pattern, bool useRegularExpressions = false);
TReturnType HaveName(string pattern, bool useRegularExpressions = false);
TReturnType HaveFullName(string pattern, bool useRegularExpressions = false);
TReturnType HaveNameStartingWith(string pattern);
TReturnType HaveNameEndingWith(string pattern);
TReturnType HaveNameContaining(string pattern);
Expand Down Expand Up @@ -128,10 +126,8 @@ public interface IObjectConditions<out TReturnType, out TRuleType> where TRuleTy
TReturnType NotHaveAttributeWithNamedArguments(Attribute attribute, (string,object) firstAttributeArgument, params (string,object)[] moreAttributeArguments);
TReturnType NotHaveAttributeWithNamedArguments(Type attribute, IEnumerable<(string,object)> attributeArguments);
TReturnType NotHaveAttributeWithNamedArguments(Type attribute, (string,object) firstAttributeArgument, params (string,object)[] moreAttributeArguments);
TReturnType NotHaveName(string name);
TReturnType NotHaveNameMatching(string pattern, bool useRegularExpressions = false);
TReturnType NotHaveFullName(string fullname);
TReturnType NotHaveFullNameMatching(string pattern, bool useRegularExpressions = false);
TReturnType NotHaveName(string pattern, bool useRegularExpressions = false);
TReturnType NotHaveFullName(string pattern, bool useRegularExpressions = false);
TReturnType NotHaveNameStartingWith(string pattern);
TReturnType NotHaveNameEndingWith(string pattern);
TReturnType NotHaveNameContaining(string pattern);
Expand Down
12 changes: 4 additions & 8 deletions ArchUnitNET/Fluent/Syntax/Elements/IObjectPredicates.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,8 @@ public interface IObjectPredicates<out TReturnType, TRuleType> where TRuleType :
TReturnType HaveAttributeWithNamedArguments(Attribute attribute, (string,object) firstAttributeArgument, params (string,object)[] moreAttributeArguments);
TReturnType HaveAttributeWithNamedArguments(Type attribute, IEnumerable<(string,object)> attributeArguments);
TReturnType HaveAttributeWithNamedArguments(Type attribute, (string,object) firstAttributeArgument, params (string,object)[] moreAttributeArguments);
TReturnType HaveName(string name);
TReturnType HaveNameMatching(string pattern, bool useRegularExpressions = false);
TReturnType HaveFullName(string fullname);
TReturnType HaveFullNameMatching(string pattern, bool useRegularExpressions = false);
TReturnType HaveName(string pattern, bool useRegularExpressions = false);
TReturnType HaveFullName(string pattern, bool useRegularExpressions = false);
TReturnType HaveNameStartingWith(string pattern);
TReturnType HaveNameEndingWith(string pattern);
TReturnType HaveNameContaining(string pattern);
Expand Down Expand Up @@ -129,10 +127,8 @@ public interface IObjectPredicates<out TReturnType, TRuleType> where TRuleType :
TReturnType DoNotHaveAttributeWithNamedArguments(Attribute attribute, (string,object) firstAttributeArgument, params (string,object)[] moreAttributeArguments);
TReturnType DoNotHaveAttributeWithNamedArguments(Type attribute, IEnumerable<(string,object)> attributeArguments);
TReturnType DoNotHaveAttributeWithNamedArguments(Type attribute, (string,object) firstAttributeArgument, params (string,object)[] moreAttributeArguments);
TReturnType DoNotHaveName(string name);
TReturnType DoNotHaveNameMatching(string pattern, bool useRegularExpressions = false);
TReturnType DoNotHaveFullName(string fullname);
TReturnType DoNotHaveFullNameMatching(string pattern, bool useRegularExpressions = false);
TReturnType DoNotHaveName(string pattern, bool useRegularExpressions = false);
TReturnType DoNotHaveFullName(string pattern, bool useRegularExpressions = false);
TReturnType DoNotHaveNameStartingWith(string pattern);
TReturnType DoNotHaveNameEndingWith(string pattern);
TReturnType DoNotHaveNameContaining(string pattern);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public static ICondition<TRuleType> BeDeclaredIn(string pattern, bool useRegular
{
return new SimpleCondition<TRuleType>(member => member.IsDeclaredIn(pattern, useRegularExpressions),
member => "is declared in " + member.DeclaringType.FullName,
"be declared in types with full name " + (useRegularExpressions ? "matching" : "containing") + " \"" +
"be declared in types with full name " + (useRegularExpressions ? "matching " : "") + "\"" +
pattern + "\"");
}

Expand All @@ -42,8 +42,8 @@ bool Condition(TRuleType ruleType)
{
var firstPattern = patternList.First();
description = patternList.Where(obj => !obj.Equals(firstPattern)).Distinct().Aggregate(
"be declared in types with full name " + (useRegularExpressions ? "matching" : "containing") +
" \"" + firstPattern + "\"",
"be declared in types with full name " + (useRegularExpressions ? "matching " : "") +
"\"" + firstPattern + "\"",
(current, pattern) => current + " or \"" + pattern + "\"");
}

Expand Down Expand Up @@ -185,8 +185,8 @@ public static ICondition<TRuleType> NotBeDeclaredIn(string pattern, bool useRegu
{
return new SimpleCondition<TRuleType>(member => !member.IsDeclaredIn(pattern, useRegularExpressions),
member => "is declared in " + member.DeclaringType.FullName,
"not be declared in types with full name " + (useRegularExpressions ? "matching" : "containing") +
" \"" + pattern + "\"");
"not be declared in types with full name " + (useRegularExpressions ? "matching " : "") +
"\"" + pattern + "\"");
}

public static ICondition<TRuleType> NotBeDeclaredIn(IEnumerable<string> patterns,
Expand All @@ -208,8 +208,8 @@ bool Condition(TRuleType ruleType)
{
var firstPattern = patternList.First();
description = patternList.Where(obj => !obj.Equals(firstPattern)).Distinct().Aggregate(
"not be declared in types with full name " + (useRegularExpressions ? "matching" : "containing") +
" \"" + firstPattern + "\"",
"not be declared in types with full name " + (useRegularExpressions ? "matching " : "") +
"\"" + firstPattern + "\"",
(current, pattern) => current + " or \"" + pattern + "\"");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public static class MemberPredicatesDefinition<T> where T : IMember
public static IPredicate<T> AreDeclaredIn(string pattern, bool useRegularExpressions = false)
{
return new SimplePredicate<T>(member => member.IsDeclaredIn(pattern, useRegularExpressions),
"are declared in types with full name " + (useRegularExpressions ? "matching" : "containing") + " \"" +
"are declared in types with full name " + (useRegularExpressions ? "matching " : "") + "\"" +
pattern + "\"");
}

Expand All @@ -40,8 +40,8 @@ bool Condition(T ruleType)
{
var firstPattern = patternList.First();
description = patternList.Where(obj => !obj.Equals(firstPattern)).Distinct().Aggregate(
"are declared in types with full name " + (useRegularExpressions ? "matching" : "containing") +
" \"" + firstPattern + "\"", (current, pattern) => current + " or \"" + pattern + "\"");
"are declared in types with full name " + (useRegularExpressions ? "matching " : "") +
"\"" + firstPattern + "\"", (current, pattern) => current + " or \"" + pattern + "\"");
}

return new SimplePredicate<T>(Condition, description);
Expand Down Expand Up @@ -148,8 +148,8 @@ public static IPredicate<T> AreStatic()
public static IPredicate<T> AreNotDeclaredIn(string pattern, bool useRegularExpressions = false)
{
return new SimplePredicate<T>(member => !member.IsDeclaredIn(pattern, useRegularExpressions),
"are not declared in types with full name " + (useRegularExpressions ? "matching" : "containing") +
" \"" + pattern + "\"");
"are not declared in types with full name " + (useRegularExpressions ? "matching " : "") +
"\"" + pattern + "\"");
}

public static IPredicate<T> AreNotDeclaredIn(IEnumerable<string> patterns, bool useRegularExpressions = false)
Expand All @@ -170,8 +170,8 @@ bool Condition(T ruleType)
{
var firstPattern = patternList.First();
description = patternList.Where(obj => !obj.Equals(firstPattern)).Distinct().Aggregate(
"are not declared in types with full name " + (useRegularExpressions ? "matching" : "containing") +
" \"" + firstPattern + "\"", (current, pattern) => current + " or \"" + pattern + "\"");
"are not declared in types with full name " + (useRegularExpressions ? "matching " : "") +
"\"" + firstPattern + "\"", (current, pattern) => current + " or \"" + pattern + "\"");
}

return new SimplePredicate<T>(Condition, description);
Expand Down
Loading

0 comments on commit 300a999

Please sign in to comment.