Skip to content

Commit

Permalink
add example rules for testing the target framework of an Architecture
Browse files Browse the repository at this point in the history
Signed-off-by: Fritz Brandhuber <[email protected]>
  • Loading branch information
brandhuf committed May 23, 2022
1 parent b938367 commit 3b0e7ac
Show file tree
Hide file tree
Showing 3 changed files with 164 additions and 0 deletions.
41 changes: 41 additions & 0 deletions ArchUnitNET/Domain/Extensions/AssemblyExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Copyright 2019 Florian Gather <[email protected]>
// Copyright 2019 Fritz Brandhuber <[email protected]>
// Copyright 2020 Pavel Fischer <[email protected]>
//
// SPDX-License-Identifier: Apache-2.0
//

using System.Linq;
using System.Runtime.Versioning;

namespace ArchUnitNET.Domain.Extensions
{
public static class AssemblyExtensions
{
public static bool IsDotNetStandard(this Assembly assembly)
{
return assembly.Name.StartsWith("netstandard") || assembly.GetTargetFramework().StartsWith(".NETStandard");
}

public static bool IsDotNetCore(this Assembly assembly)
{
return assembly.GetTargetFramework().StartsWith(".NETCore");
}

public static bool IsDotNetFramework(this Assembly assembly)
{
return assembly.Name.StartsWith("mscorlib") || assembly.GetTargetFramework().StartsWith(".NETFramework");
}

public static string GetTargetFramework(this Assembly assembly)
{
var targetFrameworkAttribute = assembly.AttributeInstances.FirstOrDefault(att =>
att.Type.FullName == typeof(TargetFrameworkAttribute).FullName);

var frameworkNameAttribute = targetFrameworkAttribute?.AttributeArguments.FirstOrDefault(argument =>
!(argument is AttributeNamedArgument namedArgument) || namedArgument.Name != "FrameworkDisplayName");

return frameworkNameAttribute?.Value as string ?? "";
}
}
}
58 changes: 58 additions & 0 deletions ArchUnitNET/Fluent/CustomArchRule.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// Copyright 2019 Florian Gather <[email protected]>
// Copyright 2019 Fritz Brandhuber <[email protected]>
// Copyright 2020 Pavel Fischer <[email protected]>
//
// SPDX-License-Identifier: Apache-2.0
//

using System;
using System.Collections.Generic;
using System.Linq;
using ArchUnitNET.Domain;

namespace ArchUnitNET.Fluent
{
public class CustomArchRule : IArchRule
{
public string Description { get; }

private readonly Func<Architecture, IArchRule, IEnumerable<EvaluationResult>> _evaluationFunc;

public CustomArchRule(Func<Architecture, IArchRule, IEnumerable<EvaluationResult>> evaluationFunc,
string description)
{
_evaluationFunc = evaluationFunc;
Description = description;
}

public bool HasNoViolations(Architecture architecture)
{
return Evaluate(architecture).All(result => result.Passed);
}

public IEnumerable<EvaluationResult> Evaluate(Architecture architecture)
{
return _evaluationFunc.Invoke(architecture, this);
}

public CombinedArchRuleDefinition And()
{
return new CombinedArchRuleDefinition(this, LogicalConjunctionDefinition.And);
}

public CombinedArchRuleDefinition Or()
{
return new CombinedArchRuleDefinition(this, LogicalConjunctionDefinition.Or);
}

public IArchRule And(IArchRule archRule)
{
return new CombinedArchRule(this, LogicalConjunctionDefinition.And, archRule);
}

public IArchRule Or(IArchRule archRule)
{
return new CombinedArchRule(this, LogicalConjunctionDefinition.Or, archRule);
}
}
}
65 changes: 65 additions & 0 deletions ArchUnitNET/Library/Rules/TargetFrameworkRules.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// Copyright 2019 Florian Gather <[email protected]>
// Copyright 2019 Fritz Brandhuber <[email protected]>
// Copyright 2020 Pavel Fischer <[email protected]>
//
// SPDX-License-Identifier: Apache-2.0
//

using System.Collections.Generic;
using ArchUnitNET.Domain;
using ArchUnitNET.Domain.Extensions;
using ArchUnitNET.Fluent;

namespace ArchUnitNET.Library.Rules
{
public static class TargetFrameworkRules
{
public static IArchRule ShouldBeDotNetStandard()
{
IEnumerable<EvaluationResult> Evaluate(Architecture architecture, IArchRule archRule)
{
foreach (var assembly in architecture.Assemblies)
{
var passed = assembly.IsDotNetStandard();
var description = "has target framework " + assembly.GetTargetFramework();
yield return new EvaluationResult(assembly, new StringIdentifier(assembly.FullName), passed,
description, archRule, architecture);
}
}

return new CustomArchRule(Evaluate, "Should be .Net Standard");
}

public static IArchRule ShouldBeDotNetCore()
{
IEnumerable<EvaluationResult> Evaluate(Architecture architecture, IArchRule archRule)
{
foreach (var assembly in architecture.Assemblies)
{
var passed = assembly.IsDotNetCore();
var description = "has target framework " + assembly.GetTargetFramework();
yield return new EvaluationResult(assembly, new StringIdentifier(assembly.FullName), passed,
description, archRule, architecture);
}
}

return new CustomArchRule(Evaluate, "Should be .Net Core");
}

public static IArchRule ShouldBeDotNetFramework()
{
IEnumerable<EvaluationResult> Evaluate(Architecture architecture, IArchRule archRule)
{
foreach (var assembly in architecture.Assemblies)
{
var passed = assembly.IsDotNetFramework();
var description = "has target framework " + assembly.GetTargetFramework();
yield return new EvaluationResult(assembly, new StringIdentifier(assembly.FullName), passed,
description, archRule, architecture);
}
}

return new CustomArchRule(Evaluate, "Should be .Net Framework");
}
}
}

0 comments on commit 3b0e7ac

Please sign in to comment.