nevespl / netarchtest.enhancededition Goto Github PK
View Code? Open in Web Editor NEWA fluent API for .Net that can enforce architectural rules in unit tests.
License: MIT License
A fluent API for .Net that can enforce architectural rules in unit tests.
License: MIT License
This is something I came up in 5 minutes.
Still not perfect because I can still "create the class" with datetime and not use them.. but ok!
I could achieve the same result using rosyln analyzers
[Fact]
public void Test1()
{
var r = Types
.InCurrentDomain()
.That()
.ResideInNamespaceContaining("Template.")
.And()
.MeetCustomRule(
r1 => r1.Fields
.Any(f => f.FieldType.Name == nameof(DateTime))
)
.Should()
.NotHaveDependencyOnAll()
// .NotBeUsedByAny()
.GetResult();
r.IsSuccessful
.Should()
.BeTrue();
}
Hi
I like this project very much, but currently it's a bit hard to write custom rules, because some of the extension methods, like TypeReferenceExtensions, TypeDefinitionExtensions, ... are internal.
For example, I like to write a CustomRule that checks if there are at least 3 TraitAttributes applied to all classes inherited from IntegrationTestBase.
Currently, I can only check against name or fullname of the attribute, because I cannot easily create a TypeDefinition or TypeReference from typeof(TraitAttribute)
.
I saw, there would be a ToTypeDefinition()
method. Having this, would make it really handy.
And maybe 2nd question. How can I add a Explanation
to custom Rules? Currently, there is only a bool
as return value.
Hi, the below example seems to produce correct results, i.e. fails on the InterfaceImplementationB
class:
public interface IInterfaceA { }
public interface IInterfaceB { }
internal class InterfaceImplementationA : IInterfaceA { }
public class InterfaceImplementationB : IInterfaceB { }
[Fact]
public void InterfaceImplementationsShouldBeInternal()
{
var policy = Policy.Define("Interface Implementations", "Interface implementations should be internal")
.For(NetArchTest.Rules.Types.FromPath(AppDomain.CurrentDomain.BaseDirectory))
.Add(types =>
types.That()
.ImplementInterface(typeof(IInterfaceB))
.Should()
.NotBePublic())
.Add(types =>
types.That()
.ImplementInterface(typeof(IInterfaceA))
.Should()
.NotBePublic());
var results = policy.Evaluate();
Assert.False(results.HasViolations);
}
But, when I change the order of policy rules to the below, the test starts passing:
[Fact]
public void InterfaceImplementationsShouldBeInternal()
{
var policy = Policy.Define("Interface Implementations", "Interface implementations should be internal")
.For(NetArchTest.Rules.Types.FromPath(AppDomain.CurrentDomain.BaseDirectory))
.Add(types =>
types.That()
.ImplementInterface(typeof(IInterfaceA))
.Should()
.NotBePublic())
.Add(types =>
types.That()
.ImplementInterface(typeof(IInterfaceB))
.Should()
.NotBePublic());
var results = policy.Evaluate();
Assert.False(results.HasViolations);
}
Am I missing something in the implementation of policies, or is there a potential issue here?
I have a problem to check types that have methods with generic types.
The explementation for the failing type is "Has dependency on: T1&"
How I can allow this dep or ignore it?
Test
PolicyResults results = Policy.Define("Deps", "Desc")
.For(Types.InAssembly(typeof(KeyValuePairExtensions).Assembly))
.Add(types => types.That()
.ResideInNamespace("NetArchExample.Utils")
.ShouldNot()
.HaveDependencyOtherThan("NetArchExample.Utils", "System"))
.Evaluate();
foreach (var result in results.Results)
{
Assert.IsTrue(result.IsSuccessful);
}
Code
namespace NetArchExample.Utils
{
using System.Collections.Generic;
/// <summary>
/// Class KeyValuePairExtensions.
/// </summary>
public static class KeyValuePairExtensions
{
/// <summary>
/// Deconstruct a key value pair.
/// </summary>
/// <param name="tuple">The tuple.</param>
/// <param name="key">The deconstructed key.</param>
/// <param name="value">The deconstructed value.</param>
/// <typeparam name="T1">The type of the key.</typeparam>
/// <typeparam name="T2">The type of the value.</typeparam>
public static void Deconstruct<T1, T2>(this KeyValuePair<T1, T2> tuple, out T1 key, out T2 value)
{
key = tuple.Key;
value = tuple.Value;
}
}
}
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.