Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Only Include Types in Error Messages that are in the given Architecture #304

Closed
wants to merge 1 commit into from
Closed
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
99 changes: 56 additions & 43 deletions ArchUnitNET/Fluent/Syntax/Elements/ObjectConditionsDefinition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -451,16 +451,18 @@ Architecture architecture
.ToList();
foreach (var failedObject in ruleTypeList.Except(passedObjects))
{
var dynamicFailDescription = "does depend on";
var first = true;
foreach (var type in failedObject.GetTypeDependencies().Except(typeList))
{
dynamicFailDescription += first
? " " + type.FullName
: " and " + type.FullName;
first = false;
}

var actualTypeDependencies = failedObject
.GetTypeDependencies(architecture)
.Except(typeList);
var dynamicFailDescription = actualTypeDependencies.IsNullOrEmpty()
? "does not depend on any type"
: actualTypeDependencies
.Where(type => !type.Equals(actualTypeDependencies.First()))
.Distinct()
.Aggregate(
"does depend on " + actualTypeDependencies.First().FullName,
(current, type) => current + " and " + type.FullName
);
yield return new ConditionResult(failedObject, false, dynamicFailDescription);
}

Expand All @@ -478,24 +480,29 @@ public static ICondition<TRuleType> DependOnAny(IEnumerable<IType> types)
{
var typeList = types.ToList();

IEnumerable<ConditionResult> Condition(IEnumerable<TRuleType> ruleTypes)
IEnumerable<ConditionResult> Condition(
IEnumerable<TRuleType> ruleTypes,
Architecture architecture
)
{
var ruleTypeList = ruleTypes.ToList();
var passedObjects = ruleTypeList
.Where(type => type.GetTypeDependencies().Intersect(typeList).Any())
.ToList();
foreach (var failedObject in ruleTypeList.Except(passedObjects))
{
var dynamicFailDescription = "does depend on";
var first = true;
foreach (var type in failedObject.GetTypeDependencies().Except(typeList))
{
dynamicFailDescription += first
? " " + type.FullName
: " and " + type.FullName;
first = false;
}

var actualTypeDependencies = failedObject
.GetTypeDependencies(architecture)
.Except(typeList);
var dynamicFailDescription = actualTypeDependencies.IsNullOrEmpty()
? "does not depend on any type"
: actualTypeDependencies
.Where(type => !type.Equals(actualTypeDependencies.First()))
.Distinct()
.Aggregate(
"does depend on " + actualTypeDependencies.First().FullName,
(current, type) => current + " and " + type.FullName
);
yield return new ConditionResult(failedObject, false, dynamicFailDescription);
}

Expand All @@ -522,7 +529,7 @@ IEnumerable<ConditionResult> Condition(IEnumerable<TRuleType> ruleTypes)
);
}

return new EnumerableCondition<TRuleType>(Condition, description);
return new ArchitectureCondition<TRuleType>(Condition, description);
}

public static ICondition<TRuleType> DependOnAny(IEnumerable<Type> types)
Expand Down Expand Up @@ -554,18 +561,18 @@ Architecture architecture
.ToList();
foreach (var failedObject in ruleTypeList.Except(passedObjects))
{
var dynamicFailDescription = "does depend on";
var first = true;
foreach (
var type in failedObject.GetTypeDependencies().Except(archUnitTypeList)
)
{
dynamicFailDescription += first
? " " + type.FullName
: " and " + type.FullName;
first = false;
}

var actualTypeDependencies = failedObject
.GetTypeDependencies(architecture)
.Except(archUnitTypeList);
var dynamicFailDescription = actualTypeDependencies.IsNullOrEmpty()
? "does not depend on any type"
: actualTypeDependencies
.Where(type => !type.Equals(actualTypeDependencies.First()))
.Distinct()
.Aggregate(
"does depend on " + actualTypeDependencies.First().FullName,
(current, type) => current + " and " + type.FullName
);
yield return new ConditionResult(failedObject, false, dynamicFailDescription);
}

Expand Down Expand Up @@ -617,11 +624,11 @@ public static ICondition<TRuleType> OnlyDependOn(
bool useRegularExpressions = false
)
{
ConditionResult Condition(TRuleType ruleType)
ConditionResult Condition(TRuleType ruleType, Architecture architecture)
{
var pass = true;
var dynamicFailDescription = "does depend on";
foreach (var dependency in ruleType.GetTypeDependencies())
foreach (var dependency in ruleType.GetTypeDependencies(architecture))
{
if (!dependency.FullNameMatches(pattern, useRegularExpressions))
{
Expand All @@ -635,7 +642,7 @@ ConditionResult Condition(TRuleType ruleType)
return new ConditionResult(ruleType, pass, dynamicFailDescription);
}

return new SimpleCondition<TRuleType>(
return new ArchitectureCondition<TRuleType>(
Condition,
"only depend on types with full name "
+ (useRegularExpressions ? "matching " : "")
Expand All @@ -652,11 +659,11 @@ public static ICondition<TRuleType> OnlyDependOn(
{
var patternList = patterns.ToList();

ConditionResult Condition(TRuleType ruleType)
ConditionResult Condition(TRuleType ruleType, Architecture architecture)
{
var pass = true;
var dynamicFailDescription = "does depend on";
foreach (var dependency in ruleType.GetTypeDependencies())
foreach (var dependency in ruleType.GetTypeDependencies(architecture))
{
if (
!patternList.Any(pattern =>
Expand Down Expand Up @@ -695,7 +702,7 @@ ConditionResult Condition(TRuleType ruleType)
);
}

return new SimpleCondition<TRuleType>(Condition, description);
return new ArchitectureCondition<TRuleType>(Condition, description);
}

public static ICondition<TRuleType> OnlyDependOn(IType firstType, params IType[] moreTypes)
Expand Down Expand Up @@ -728,7 +735,9 @@ Architecture architecture
{
var dynamicFailDescription = "does depend on";
var first = true;
foreach (var type in failedObject.GetTypeDependencies().Except(typeList))
foreach (
var type in failedObject.GetTypeDependencies(architecture).Except(typeList)
)
{
dynamicFailDescription += first
? " " + type.FullName
Expand Down Expand Up @@ -766,7 +775,9 @@ Architecture architecture
{
var dynamicFailDescription = "does depend on";
var first = true;
foreach (var type in failedObject.GetTypeDependencies().Except(typeList))
foreach (
var type in failedObject.GetTypeDependencies(architecture).Except(typeList)
)
{
dynamicFailDescription += first
? " " + type.FullName
Expand Down Expand Up @@ -837,7 +848,9 @@ Architecture architecture
var dynamicFailDescription = "does depend on";
var first = true;
foreach (
var type in failedObject.GetTypeDependencies().Except(archUnitTypeList)
var type in failedObject
.GetTypeDependencies(architecture)
.Except(archUnitTypeList)
)
{
dynamicFailDescription += first
Expand Down
16 changes: 11 additions & 5 deletions ArchUnitNETTests/Fluent/Syntax/Elements/ObjectsShouldTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -635,11 +635,17 @@ public async Task DependOnAnyTest()
.Should()
.DependOnAny(helper.BaseClassWithMultipleDependenciesSystemType)
.AssertNoViolations(helper);
Types()
.That()
.Are(helper.ChildClass, helper.BaseClass)
.Should()
.DependOnAny(helper.ClassWithoutDependencies)
should = Types().That().Are(helper.ChildClass, helper.BaseClass).Should();
should.DependOnAny(helper.ClassWithoutDependencies.FullName).AssertOnlyViolations(helper);
should.DependOnAny([helper.ClassWithoutDependencies.FullName]).AssertOnlyViolations(helper);
should.DependOnAny(helper.ClassWithoutDependencies).AssertOnlyViolations(helper);
should.DependOnAny([helper.ClassWithoutDependencies]).AssertOnlyViolations(helper);
should.DependOnAny(helper.ClassWithoutDependenciesSystemType).AssertOnlyViolations(helper);
should
.DependOnAny([helper.ClassWithoutDependenciesSystemType])
.AssertOnlyViolations(helper);
should
.DependOnAny(Classes().That().Are(helper.ClassWithoutDependencies))
.AssertOnlyViolations(helper);
await helper.AssertSnapshotMatches();
}
Expand Down
Loading
Loading