Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions ArchUnitNET/Domain/Extensions/AttributeExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.Linq;

namespace ArchUnitNET.Domain.Extensions
Expand Down Expand Up @@ -26,5 +27,28 @@ public static bool OnlyHasAttributesMatching(this IHasAttributes a, string patte
return a.Attributes.IsNullOrEmpty()
|| a.Attributes.All(attribute => attribute.FullNameMatches(pattern));
}

internal static IEnumerable<object> GetAllAttributeArgumentValues(
this AttributeInstance instance
) =>
instance
.AttributeArguments.Select(arg => arg.Value)
.Select(value =>
value is ITypeInstance<IType> typeInstance ? typeInstance.Type : value
);

internal static IEnumerable<(string, object)> GetAllNamedAttributeArgumentTuples(
this AttributeInstance instance
) =>
instance
.AttributeArguments.OfType<AttributeNamedArgument>()
.Select(arg =>
(
arg.Name,
arg.Value is ITypeInstance<IType> typeInstance
? typeInstance.Type
: arg.Value
)
);
}
}
20 changes: 20 additions & 0 deletions ArchUnitNET/Domain/Extensions/EnumerableExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,25 @@ public static string FormatDescription<T>(
return $"{multipleDescription} {string.Join(" and ", list.Select(elementDescription))}";
}
}

internal static IEnumerable<object> ResolveAttributeArguments(
this IEnumerable<object> objects,
Architecture architecture
)
{
return objects.Select(obj =>
obj is Type type ? architecture.GetITypeOfType(type) : obj
);
}

internal static IEnumerable<(string, object)> ResolveNamedAttributeArgumentTuples(
this IEnumerable<(string, object)> namedArguments,
Architecture architecture
)
{
return namedArguments.Select(arg =>
(arg.Item1, arg.Item2 is Type type ? architecture.GetITypeOfType(type) : arg.Item2)
);
}
}
}
48 changes: 48 additions & 0 deletions ArchUnitNET/Domain/Extensions/ICanBeAnalyzedExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
using System;
Copy link
Collaborator

@mak638 mak638 Nov 29, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nitpick: using System; is unnecessary

using System.Collections.Generic;
using System.Linq;

namespace ArchUnitNET.Domain.Extensions
{
internal static class ICanBeAnalyzedExtensions
{
internal static IEnumerable<object> GetAllAttributeArgumentValues(
this ICanBeAnalyzed obj,
Attribute attribute = null
)
{
var attributeInstances =
attribute == null
? obj.AttributeInstances
: obj.AttributeInstances.Where(instance => instance.Type.Equals(attribute));
return attributeInstances
.SelectMany(instance => instance.AttributeArguments.Select(arg => arg.Value))
.Select(value =>
value is ITypeInstance<IType> typeInstance ? typeInstance.Type : value
);
}

internal static IEnumerable<(string, object)> GetAllNamedAttributeArgumentTuples(
this ICanBeAnalyzed obj,
Attribute attribute = null
)
{
var attributeInstances =
attribute == null
? obj.AttributeInstances
: obj.AttributeInstances.Where(instance => instance.Type.Equals(attribute));
return attributeInstances.SelectMany(instance =>
instance
.AttributeArguments.OfType<AttributeNamedArgument>()
.Select(arg =>
(
arg.Name,
arg.Value is ITypeInstance<IType> typeInstance
? typeInstance.Type
: arg.Value
)
)
);
}
}
}
34 changes: 15 additions & 19 deletions ArchUnitNET/Fluent/Syntax/Elements/GivenObjectsThat.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Linq;
using ArchUnitNET.Domain;
using ArchUnitNET.Domain.Extensions;
using ArchUnitNET.Fluent.Predicates;
using static ArchUnitNET.Fluent.Syntax.ConjunctionFactory;
using Attribute = ArchUnitNET.Domain.Attribute;
Expand Down Expand Up @@ -59,18 +60,16 @@ protected GivenObjectsThat(IArchRuleCreator<TRuleType> ruleCreator)
public TGivenRuleTypeConjunction HaveAnyAttributesWithArguments(IEnumerable<object> argumentValues) => AddPredicate(ObjectPredicatesDefinition<TRuleType>.HaveAnyAttributesWithArguments(argumentValues));
public TGivenRuleTypeConjunction HaveAnyAttributesWithArguments(object firstArgumentValue, params object[] moreArgumentValues) => AddPredicate(ObjectPredicatesDefinition<TRuleType>.HaveAnyAttributesWithArguments(new[] { firstArgumentValue }.Concat(moreArgumentValues)));

public TGivenRuleTypeConjunction HaveAttributeWithArguments(Attribute attribute, IEnumerable<object> argumentValues) => AddPredicate(ObjectPredicatesDefinition<TRuleType>.HaveAttributeWithArguments(attribute, argumentValues));
public TGivenRuleTypeConjunction HaveAttributeWithArguments(Attribute attribute, object firstArgumentValue, params object[] moreArgumentValues) => AddPredicate(ObjectPredicatesDefinition<TRuleType>.HaveAttributeWithArguments(attribute, new[] { firstArgumentValue }.Concat(moreArgumentValues)));
public TGivenRuleTypeConjunction HaveAttributeWithArguments(Type attribute, IEnumerable<object> argumentValues) => AddPredicate(ObjectPredicatesDefinition<TRuleType>.HaveAttributeWithArguments(attribute, argumentValues));
public TGivenRuleTypeConjunction HaveAttributeWithArguments(Type attribute, object firstArgumentValue, params object[] moreArgumentValues) => AddPredicate(ObjectPredicatesDefinition<TRuleType>.HaveAttributeWithArguments(attribute, new[] { firstArgumentValue }.Concat(moreArgumentValues)));
public TGivenRuleTypeConjunction HaveAttributeWithArguments(Attribute attribute, IEnumerable<object> argumentValues) => AddPredicate(ObjectPredicatesDefinition<TRuleType>.HaveAttributeWithArguments(attribute.FullName, _ => attribute, argumentValues));
public TGivenRuleTypeConjunction HaveAttributeWithArguments(Type attribute, IEnumerable<object> argumentValues) => AddPredicate(ObjectPredicatesDefinition<TRuleType>.HaveAttributeWithArguments(attribute.FullName, architecture => architecture.GetAttributeOfType(attribute), argumentValues));

public TGivenRuleTypeConjunction HaveAnyAttributesWithNamedArguments(IEnumerable<(string, object)> attributeArguments) => AddPredicate(ObjectPredicatesDefinition<TRuleType>.HaveAnyAttributesWithNamedArguments(attributeArguments));
public TGivenRuleTypeConjunction HaveAnyAttributesWithNamedArguments((string, object) firstAttributeArgument, params (string, object)[] moreAttributeArguments) => HaveAnyAttributesWithNamedArguments(new[] { firstAttributeArgument }.Concat(moreAttributeArguments));
public TGivenRuleTypeConjunction HaveAnyAttributesWithNamedArguments(params (string, object)[] attributeArguments) => HaveAnyAttributesWithNamedArguments((IEnumerable<(string, object)>)attributeArguments);

public TGivenRuleTypeConjunction HaveAttributeWithNamedArguments(Attribute attribute, IEnumerable<(string, object)> attributeArguments) => AddPredicate(ObjectPredicatesDefinition<TRuleType>.HaveAttributeWithNamedArguments(attribute, attributeArguments));
public TGivenRuleTypeConjunction HaveAttributeWithNamedArguments(Attribute attribute, (string, object) firstAttributeArgument, params (string, object)[] moreAttributeArguments) => HaveAttributeWithNamedArguments(attribute, new[] { firstAttributeArgument }.Concat(moreAttributeArguments));
public TGivenRuleTypeConjunction HaveAttributeWithNamedArguments(Type attribute, IEnumerable<(string, object)> attributeArguments) => AddPredicate(ObjectPredicatesDefinition<TRuleType>.HaveAttributeWithNamedArguments(attribute, attributeArguments));
public TGivenRuleTypeConjunction HaveAttributeWithNamedArguments(Type attribute, (string, object) firstAttributeArgument, params (string, object)[] moreAttributeArguments) => HaveAttributeWithNamedArguments(attribute, new[] { firstAttributeArgument }.Concat(moreAttributeArguments));
public TGivenRuleTypeConjunction HaveAttributeWithNamedArguments(Attribute attribute, IEnumerable<(string, object)> attributeArguments) => AddPredicate(ObjectPredicatesDefinition<TRuleType>.HaveAttributeWithNamedArguments(attribute.FullName, _ => attribute, attributeArguments));
public TGivenRuleTypeConjunction HaveAttributeWithNamedArguments(Attribute attribute, params (string, object)[] attributeArguments) => HaveAttributeWithNamedArguments(attribute, (IEnumerable<(string, object)>)attributeArguments);
public TGivenRuleTypeConjunction HaveAttributeWithNamedArguments(Type attribute, IEnumerable<(string, object)> attributeArguments) => AddPredicate(ObjectPredicatesDefinition<TRuleType>.HaveAttributeWithNamedArguments(attribute.FullName, architecture => architecture.GetAttributeOfType(attribute), attributeArguments));
public TGivenRuleTypeConjunction HaveAttributeWithNamedArguments(Type attribute, params (string, object)[] attributeArguments) => HaveAttributeWithNamedArguments(attribute, (IEnumerable<(string, object)>)attributeArguments);

public TGivenRuleTypeConjunction HaveName(string name) => AddPredicate(ObjectPredicatesDefinition<TRuleType>.HaveName(name));
public TGivenRuleTypeConjunction HaveNameMatching(string pattern) => AddPredicate(ObjectPredicatesDefinition<TRuleType>.HaveNameMatching(pattern));
Expand Down Expand Up @@ -122,20 +121,17 @@ protected GivenObjectsThat(IArchRuleCreator<TRuleType> ruleCreator)
public TGivenRuleTypeConjunction DoNotHaveAnyAttributes(IEnumerable<Type> attributes) => DoNotHaveAnyAttributes(new SystemTypeObjectProvider<Attribute>(attributes));

public TGivenRuleTypeConjunction DoNotHaveAnyAttributesWithArguments(IEnumerable<object> argumentValues) => AddPredicate(ObjectPredicatesDefinition<TRuleType>.DoNotHaveAnyAttributesWithArguments(argumentValues));
public TGivenRuleTypeConjunction DoNotHaveAnyAttributesWithArguments(object firstArgumentValue, params object[] moreArgumentValues) => AddPredicate(ObjectPredicatesDefinition<TRuleType>.DoNotHaveAnyAttributesWithArguments(new[] { firstArgumentValue }.Concat(moreArgumentValues)));

public TGivenRuleTypeConjunction DoNotHaveAttributeWithArguments(Attribute attribute, IEnumerable<object> argumentValues) => AddPredicate(ObjectPredicatesDefinition<TRuleType>.DoNotHaveAttributeWithArguments(attribute, argumentValues));
public TGivenRuleTypeConjunction DoNotHaveAttributeWithArguments(Attribute attribute, object firstArgumentValue, params object[] moreArgumentValues) => AddPredicate(ObjectPredicatesDefinition<TRuleType>.DoNotHaveAttributeWithArguments(attribute, new[] { firstArgumentValue }.Concat(moreArgumentValues)));
public TGivenRuleTypeConjunction DoNotHaveAttributeWithArguments(Type attribute, IEnumerable<object> argumentValues) => AddPredicate(ObjectPredicatesDefinition<TRuleType>.DoNotHaveAttributeWithArguments(attribute, argumentValues));
public TGivenRuleTypeConjunction DoNotHaveAttributeWithArguments(Type attribute, object firstArgumentValue, params object[] moreArgumentValues) => AddPredicate(ObjectPredicatesDefinition<TRuleType>.DoNotHaveAttributeWithArguments(attribute, new[] { firstArgumentValue }.Concat(moreArgumentValues)));
public TGivenRuleTypeConjunction DoNotHaveAttributeWithArguments(Attribute attribute, IEnumerable<object> argumentValues) => AddPredicate(ObjectPredicatesDefinition<TRuleType>.DoNotHaveAttributeWithArguments(attribute.FullName, _ => attribute, argumentValues));
public TGivenRuleTypeConjunction DoNotHaveAttributeWithArguments(Type attribute, IEnumerable<object> argumentValues) => AddPredicate(ObjectPredicatesDefinition<TRuleType>.DoNotHaveAttributeWithArguments(attribute.FullName, architecture => architecture.GetAttributeOfType(attribute), argumentValues));

public TGivenRuleTypeConjunction DoNotHaveAnyAttributesWithNamedArguments(IEnumerable<(string, object)> attributeArguments) => AddPredicate(ObjectPredicatesDefinition<TRuleType>.DoNotHaveAnyAttributesWithNamedArguments(attributeArguments));
public TGivenRuleTypeConjunction DoNotHaveAnyAttributesWithNamedArguments((string, object) firstAttributeArgument, params (string, object)[] moreAttributeArguments) => DoNotHaveAnyAttributesWithNamedArguments(new[] { firstAttributeArgument }.Concat(moreAttributeArguments));
public TGivenRuleTypeConjunction DoNotHaveAnyAttributesWithNamedArguments(params (string, object)[] attributeArguments) => DoNotHaveAnyAttributesWithNamedArguments((IEnumerable<(string, object)>)attributeArguments);

public TGivenRuleTypeConjunction DoNotHaveAttributeWithNamedArguments(Attribute attribute, IEnumerable<(string, object)> attributeArguments) => AddPredicate(ObjectPredicatesDefinition<TRuleType>.DoNotHaveAttributeWithNamedArguments(attribute, attributeArguments));
public TGivenRuleTypeConjunction DoNotHaveAttributeWithNamedArguments(Attribute attribute, (string, object) firstAttributeArgument, params (string, object)[] moreAttributeArguments) => DoNotHaveAttributeWithNamedArguments(attribute, new[] { firstAttributeArgument }.Concat(moreAttributeArguments));
public TGivenRuleTypeConjunction DoNotHaveAttributeWithNamedArguments(Type attribute, IEnumerable<(string, object)> attributeArguments) => AddPredicate(ObjectPredicatesDefinition<TRuleType>.DoNotHaveAttributeWithNamedArguments(attribute, attributeArguments));
public TGivenRuleTypeConjunction DoNotHaveAttributeWithNamedArguments(Type attribute, (string, object) firstAttributeArgument, params (string, object)[] moreAttributeArguments) => DoNotHaveAttributeWithNamedArguments(attribute, new[] { firstAttributeArgument }.Concat(moreAttributeArguments));
public TGivenRuleTypeConjunction DoNotHaveAttributeWithNamedArguments(Attribute attribute, IEnumerable<(string, object)> attributeArguments) => AddPredicate(ObjectPredicatesDefinition<TRuleType>.DoNotHaveAttributeWithNamedArguments(attribute.FullName, _ => attribute, attributeArguments));
public TGivenRuleTypeConjunction DoNotHaveAttributeWithNamedArguments(Attribute attribute, params (string, object)[] attributeArguments) => DoNotHaveAttributeWithNamedArguments(attribute, (IEnumerable<(string, object)>)attributeArguments);
public TGivenRuleTypeConjunction DoNotHaveAttributeWithNamedArguments(Type attribute, IEnumerable<(string, object)> attributeArguments) => AddPredicate(ObjectPredicatesDefinition<TRuleType>.DoNotHaveAttributeWithNamedArguments(attribute.FullName, architecture => architecture.GetAttributeOfType(attribute), attributeArguments));
public TGivenRuleTypeConjunction DoNotHaveAttributeWithNamedArguments(Type attribute, params (string, object)[] attributeArguments) => DoNotHaveAttributeWithNamedArguments(attribute, (IEnumerable<(string, object)>)attributeArguments);

public TGivenRuleTypeConjunction DoNotHaveName(string name) => AddPredicate(ObjectPredicatesDefinition<TRuleType>.DoNotHaveName(name));
public TGivenRuleTypeConjunction DoNotHaveNameMatching(string pattern) => AddPredicate(ObjectPredicatesDefinition<TRuleType>.DoNotHaveNameMatching(pattern));
Expand Down
19 changes: 6 additions & 13 deletions ArchUnitNET/Fluent/Syntax/Elements/IObjectConditions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,20 +54,17 @@ public interface IObjectConditions<out TReturnType, out TRuleType>
TReturnType OnlyHaveAttributes(IEnumerable<Type> attributes);

TReturnType HaveAnyAttributesWithArguments(IEnumerable<object> argumentValues);
TReturnType HaveAnyAttributesWithArguments(object firstArgumentValue, params object[] moreArgumentValues);

TReturnType HaveAttributeWithArguments(Attribute attribute, IEnumerable<object> argumentValues);
TReturnType HaveAttributeWithArguments(Attribute attribute, object firstArgumentValue, params object[] moreArgumentValues);
TReturnType HaveAttributeWithArguments(Type attribute, IEnumerable<object> argumentValues);
TReturnType HaveAttributeWithArguments(Type attribute, object firstArgumentValue, params object[] moreArgumentValues);

TReturnType HaveAnyAttributesWithNamedArguments(IEnumerable<(string, object)> attributeArguments);
TReturnType HaveAnyAttributesWithNamedArguments((string, object) firstAttributeArgument, params (string, object)[] moreAttributeArguments);
TReturnType HaveAnyAttributesWithNamedArguments(params (string, object)[] attributeArguments);

TReturnType HaveAttributeWithNamedArguments(Attribute attribute, IEnumerable<(string, object)> attributeArguments);
TReturnType HaveAttributeWithNamedArguments(Attribute attribute, (string, object) firstAttributeArgument, params (string, object)[] moreAttributeArguments);
TReturnType HaveAttributeWithNamedArguments(Attribute attribute, params (string, object)[] attributeArguments);
TReturnType HaveAttributeWithNamedArguments(Type attribute, IEnumerable<(string, object)> attributeArguments);
TReturnType HaveAttributeWithNamedArguments(Type attribute, (string, object) firstAttributeArgument, params (string, object)[] moreAttributeArguments);
TReturnType HaveAttributeWithNamedArguments(Type attribute, params (string, object)[] attributeArguments);

TReturnType HaveName(string name);
TReturnType HaveNameMatching(string pattern);
Expand Down Expand Up @@ -121,21 +118,17 @@ public interface IObjectConditions<out TReturnType, out TRuleType>
TReturnType NotHaveAnyAttributes(IEnumerable<Type> attributes);

TReturnType NotHaveAnyAttributesWithArguments(IEnumerable<object> argumentValues);
TReturnType NotHaveAnyAttributesWithArguments(object firstArgumentValue, params object[] moreArgumentValues);

TReturnType NotHaveAttributeWithArguments(Attribute attribute, IEnumerable<object> argumentValues);
TReturnType NotHaveAttributeWithArguments(Attribute attribute, object firstArgumentValue, params object[] moreArgumentValues);
TReturnType NotHaveAttributeWithArguments(Type attribute, IEnumerable<object> argumentValues);
TReturnType NotHaveAttributeWithArguments(Type attribute, object firstArgumentValue, params object[] moreArgumentValues);

TReturnType NotHaveAnyAttributesWithNamedArguments(IEnumerable<(string, object)> attributeArguments);

TReturnType NotHaveAnyAttributesWithNamedArguments((string, object) firstAttributeArgument, params (string, object)[] moreAttributeArguments);
TReturnType NotHaveAnyAttributesWithNamedArguments(params (string, object)[] attributeArguments);

TReturnType NotHaveAttributeWithNamedArguments(Attribute attribute, IEnumerable<(string, object)> attributeArguments);
TReturnType NotHaveAttributeWithNamedArguments(Attribute attribute, (string, object) firstAttributeArgument, params (string, object)[] moreAttributeArguments);
TReturnType NotHaveAttributeWithNamedArguments(Attribute attribute, params (string, object)[] attributeArguments);
TReturnType NotHaveAttributeWithNamedArguments(Type attribute, IEnumerable<(string, object)> attributeArguments);
TReturnType NotHaveAttributeWithNamedArguments(Type attribute, (string, object) firstAttributeArgument, params (string, object)[] moreAttributeArguments);
TReturnType NotHaveAttributeWithNamedArguments(Type attribute, params (string, object)[] attributeArguments);

TReturnType NotHaveName(string name);
TReturnType NotHaveNameMatching(string pattern);
Expand Down
Loading