-
Notifications
You must be signed in to change notification settings - Fork 62
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #161 from brandhuf/add_example_rules_for_target_fr…
…ameworks Add example rules for testing the target framework of an Architecture
- Loading branch information
Showing
3 changed files
with
164 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ?? ""; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} | ||
} | ||
} |