forked from TNG/ArchUnitNET
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExampleArchUnitTest.cs
More file actions
94 lines (76 loc) · 3.64 KB
/
ExampleArchUnitTest.cs
File metadata and controls
94 lines (76 loc) · 3.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
// Copyright 2019 Florian Gather <florian.gather@tngtech.com>
// Copyright 2019 Paula Ruiz <paularuiz22@gmail.com>
// Copyright 2019 Fritz Brandhuber <fritz.brandhuber@tngtech.com>
//
// SPDX-License-Identifier: Apache-2.0
// ReSharper disable InconsistentNaming
// ReSharper disable SuggestVarOrType_SimpleTypes
using ArchUnitNET.Domain;
using ArchUnitNET.Fluent;
using ArchUnitNET.Loader;
using Xunit;
using static ArchUnitNET.Fluent.ArchRuleDefinition;
namespace ExampleTest
{
public class ExampleArchUnitTest
{
// TIP: load your architecture once at the start to maximize performance of your tests
private static readonly Architecture Architecture =
new ArchLoader().LoadAssemblies(typeof(ExampleClass).Assembly, typeof(ForbiddenClass).Assembly).Build();
// replace <ExampleClass> and <ForbiddenClass> with classes from the assemblies you want to test
//declare variables you'll use throughout your tests up here
//use As() to give them a custom description
private readonly IObjectProvider<IType> ExampleLayer =
Types().That().ResideInAssembly("ExampleAssembly").As("Example Layer");
private readonly IObjectProvider<Class> ExampleClasses =
Classes().That().ImplementInterface("IExampleInterface").As("Example Classes");
private readonly IObjectProvider<IType> ForbiddenLayer =
Types().That().ResideInNamespace("ForbiddenNamespace").As("Forbidden Layer");
private readonly IObjectProvider<Interface> ForbiddenInterfaces =
Interfaces().That().HaveFullNameContaining("forbidden").As("Forbidden Interfaces");
//write some tests
[Fact]
public void TypesShouldBeInCorrectLayer()
{
//you can use the fluent API to write your own rules
IArchRule exampleClassesShouldBeInExampleLayer =
Classes().That().Are(ExampleClasses).Should().Be(ExampleLayer);
IArchRule forbiddenInterfacesShouldBeInForbiddenLayer =
Interfaces().That().Are(ForbiddenInterfaces).Should().Be(ForbiddenLayer);
//check if your architecture fulfils your rules
exampleClassesShouldBeInExampleLayer.Check(Architecture);
forbiddenInterfacesShouldBeInForbiddenLayer.Check(Architecture);
//you can also combine your rules
IArchRule combinedArchRule =
exampleClassesShouldBeInExampleLayer.And(forbiddenInterfacesShouldBeInForbiddenLayer);
combinedArchRule.Check(Architecture);
}
[Fact]
public void ExampleLayerShouldNotAccessForbiddenLayer()
{
//you can give your rules a custom reason, which is displayed when it fails (together with the types that failed the rule)
IArchRule exampleLayerShouldNotAccessForbiddenLayer = Types().That().Are(ExampleLayer).Should()
.NotDependOnAny(ForbiddenLayer).Because("it's forbidden");
exampleLayerShouldNotAccessForbiddenLayer.Check(Architecture);
}
[Fact]
public void ForbiddenClassesShouldHaveCorrectName()
{
Classes().That().AreAssignableTo(ForbiddenInterfaces).Should().HaveNameContaining("forbidden")
.Check(Architecture);
}
[Fact]
public void ExampleClassesShouldNotCallForbiddenMethods()
{
Classes().That().Are(ExampleClasses).Should().NotCallAny(
MethodMembers().That().AreDeclaredIn(ForbiddenLayer).Or().HaveNameContaining("forbidden"))
.Check(Architecture);
}
}
}
internal class ExampleClass
{
}
internal class ForbiddenClass
{
}