-
Notifications
You must be signed in to change notification settings - Fork 76
Expand file tree
/
Copy pathExampleArchUnitTest.cs
More file actions
158 lines (135 loc) · 5.8 KB
/
ExampleArchUnitTest.cs
File metadata and controls
158 lines (135 loc) · 5.8 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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
// ReSharper disable InconsistentNaming
// ReSharper disable SuggestVarOrType_SimpleTypes
using ArchUnitNET.Domain;
using ArchUnitNET.Fluent;
using ArchUnitNET.Loader;
using ArchUnitNET.xUnit;
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(System.Reflection.Assembly.Load("TestAssembly"))
.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(typeof(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");
private readonly IObjectProvider<IType> TypoLayer = Types()
.That()
.ResideInNamespace("TypoNamespace")
.As("Non-existing Typo Layer");
//write some tests
[Fact(Skip = "This is just a syntax example, it does not actually work")]
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(Skip = "This is just a syntax example, it does not actually work")]
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(Skip = "This is just a syntax example, it does not actually work")]
public void TypoLayerShouldViolateByDefault()
{
// test the new default case
IArchRule typoLayerShouldFailByDefault = Types()
.That()
.Are(TypoLayer)
.Should()
.NotDependOnAny(ForbiddenLayer)
.Because("typo layer does not exist");
Assert.False(typoLayerShouldFailByDefault.HasNoViolations(Architecture));
// test the active assertion (with implicit exemption)
IArchRule typoLayerShouldDefinitelyNotExist = Types()
.That()
.Are(TypoLayer)
.Should()
.NotExist()
.Because("typo layer simply is not there");
Assert.True(typoLayerShouldDefinitelyNotExist.HasNoViolations(Architecture));
// test the explicit exemption
IArchRule typoLayerCanCauseNoViolations = Types()
.That()
.Are(TypoLayer)
.Should()
.NotDependOnAny(ForbiddenLayer)
.Because("typo layer does not exist and causes no violation")
.WithoutRequiringPositiveResults();
Assert.True(typoLayerCanCauseNoViolations.HasNoViolations(Architecture));
}
[Fact(Skip = "This is just a syntax example, it does not actually work")]
public void ForbiddenClassesShouldHaveCorrectName()
{
Classes()
.That()
.AreAssignableTo(ForbiddenInterfaces)
.Should()
.HaveNameContaining("forbidden")
.Check(Architecture);
}
[Fact(Skip = "This is just a syntax example, it does not actually work")]
public void ExampleClassesShouldNotCallForbiddenMethods()
{
Classes()
.That()
.Are(ExampleClasses)
.Should()
.NotCallAny(
MethodMembers()
.That()
.AreDeclaredIn(ForbiddenLayer)
.Or()
.HaveNameContaining("forbidden")
)
.Check(Architecture);
}
}
}
internal class ExampleClass { }
internal class ForbiddenClass { }
internal interface IExampleInterface { }