Skip to content

Commit 8adc328

Browse files
committed
The Big Bang
1 parent cd669f9 commit 8adc328

20 files changed

Lines changed: 325 additions & 322 deletions

src/NetArchTest.Rules/Assemblies/IType.cs

Lines changed: 0 additions & 21 deletions
This file was deleted.
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
5+
namespace NetArchTest.Rules.Assemblies
6+
{
7+
internal interface ITypes
8+
{
9+
IEnumerable<TypeSpec> Failing { get; }
10+
IEnumerable<TypeSpec> Passing { get; }
11+
}
12+
13+
14+
internal sealed class TypesImpl : ITypes
15+
{
16+
public IEnumerable<TypeSpec> Failing { get; }
17+
public IEnumerable<TypeSpec> Passing { get; }
18+
19+
20+
21+
}
22+
}

src/NetArchTest.Rules/Assemblies/TypeSource.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ internal sealed class TypeSource
1515

1616

1717

18-
public static IEnumerable<IType> FromAssemblies(IEnumerable<Assembly> assemblies, IEnumerable<string> searchDirectories = null)
18+
public static IEnumerable<TypeSpec> FromAssemblies(IEnumerable<Assembly> assemblies, IEnumerable<string> searchDirectories = null)
1919
{
2020
foreach (var assembly in assemblies)
2121
{
@@ -30,7 +30,7 @@ public static IEnumerable<IType> FromAssemblies(IEnumerable<Assembly> assemblies
3030
}
3131
}
3232
}
33-
public static IEnumerable<IType> FromFiles(IEnumerable<string> fileNames, IEnumerable<string> searchDirectories = null)
33+
public static IEnumerable<TypeSpec> FromFiles(IEnumerable<string> fileNames, IEnumerable<string> searchDirectories = null)
3434
{
3535
foreach (var fileName in fileNames)
3636
{
@@ -41,7 +41,7 @@ public static IEnumerable<IType> FromFiles(IEnumerable<string> fileNames, IEnume
4141
}
4242
}
4343

44-
private static IEnumerable<IType> ReadTypes(string assemblyLocation, IEnumerable<string> searchDirectories = null)
44+
private static IEnumerable<TypeSpec> ReadTypes(string assemblyLocation, IEnumerable<string> searchDirectories = null)
4545
{
4646
ReaderParameters readerParameters = null;
4747

@@ -87,7 +87,7 @@ private static AssemblyDefinition ReadAssemblyDefinition(string path, ReaderPara
8787
return null;
8888
}
8989
}
90-
private static IEnumerable<IType> GetAllTypes(IEnumerable<TypeDefinition> types)
90+
private static IEnumerable<TypeSpec> GetAllTypes(IEnumerable<TypeDefinition> types)
9191
{
9292
foreach (var type in types)
9393
{
@@ -96,7 +96,7 @@ private static IEnumerable<IType> GetAllTypes(IEnumerable<TypeDefinition> types)
9696
continue;
9797
}
9898

99-
yield return new TypeImpl(type);
99+
yield return new TypeSpec(type);
100100

101101
if (type.NestedTypes?.Any() == true)
102102
{
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
using Mono.Cecil;
2+
3+
namespace NetArchTest.Rules.Assemblies
4+
{
5+
internal sealed class TypeSpec
6+
{
7+
public TypeDefinition Definition { get; }
8+
public string FullName => Definition.FullName;
9+
internal bool IsSelected { get; set; }
10+
11+
12+
public TypeSpec(TypeDefinition definition)
13+
{
14+
Definition = definition;
15+
}
16+
17+
18+
public static implicit operator TypeDefinition(TypeSpec type)
19+
{
20+
return type.Definition;
21+
}
22+
}
23+
}
Lines changed: 17 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,30 @@
1-
namespace NetArchTest.Rules
2-
{
3-
using System;
4-
using System.Collections.Generic;
5-
using System.Linq;
6-
using Mono.Cecil;
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using NetArchTest.Rules.Assemblies;
75

6+
namespace NetArchTest.Rules
7+
{
88
/// <summary>
99
/// A set of conditions that can be applied to a list of types.
1010
/// </summary>
11-
public sealed class Conditions
12-
{
13-
/// <summary> A list of types that conditions can be applied to. </summary>
14-
private readonly IEnumerable<TypeDefinition> _types;
15-
16-
/// <summary> The sequence of conditions that is applied to the type of list. </summary>
11+
public sealed class Condition
12+
{
13+
private readonly IEnumerable<TypeSpec> _types;
1714
private readonly FunctionSequence _sequence;
1815

19-
/// <summary> Determines the polarity of the selection, i.e. "should" or "should not". </summary>
20-
private readonly bool _should;
2116

22-
/// <summary>
23-
/// Initializes a new instance of the <see cref="Conditions"/> class.
24-
/// </summary>
25-
internal Conditions(IEnumerable<TypeDefinition> types, bool should)
17+
/// <summary> Determines the polarity of the selection, i.e. "should" or "should not". </summary>
18+
private readonly bool _should;
19+
20+
21+
internal Condition(IEnumerable<TypeSpec> types, bool should, FunctionSequence calls = null)
2622
{
27-
_types = types.ToList();
23+
_types = types;
2824
_should = should;
29-
_sequence = new FunctionSequence();
25+
_sequence = calls ?? new FunctionSequence();
3026
}
3127

32-
/// <summary>
33-
/// Initializes a new instance of the <see cref="Conditions"/> class.
34-
/// </summary>
35-
internal Conditions(IEnumerable<TypeDefinition> types, bool should, FunctionSequence calls)
36-
{
37-
_types = types.ToList();
38-
_should = should;
39-
_sequence = calls;
40-
}
4128

4229
/// <summary>
4330
/// Selects types that have a specific name.
@@ -712,4 +699,4 @@ public ConditionList MeetCustomRule(ICustomRule rule)
712699
return new ConditionList(_types, _should, _sequence);
713700
}
714701
}
715-
}
702+
}
Lines changed: 25 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,50 @@
1-
namespace NetArchTest.Rules
2-
{
3-
using System;
4-
using System.Collections.Generic;
5-
using System.Linq;
6-
using NetArchTest.Rules.Extensions;
7-
using Mono.Cecil;
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using NetArchTest.Rules.Assemblies;
5+
using NetArchTest.Rules.Extensions;
86

7+
namespace NetArchTest.Rules
8+
{
99
/// <summary>
1010
/// A set of conditions and types that have have conjunctions (i.e. "and", "or") and executors (i.e. Types(), GetResult()) applied to them.
1111
/// </summary>
1212
public sealed class ConditionList
13-
{
14-
/// <summary> A list of types that conditions can be applied to. </summary>
15-
private readonly IEnumerable<TypeDefinition> _types;
16-
17-
/// <summary> The sequence of conditions that is applied to the type of list. </summary>
13+
{
14+
private readonly IEnumerable<TypeSpec> _types;
1815
private readonly FunctionSequence _sequence;
1916

2017
/// <summary> Determines the polarity of the selection, i.e. "should" or "should not". </summary>
2118
private readonly bool _should;
2219

23-
/// <summary>
24-
/// Initializes a new instance of the <see cref="ConditionList"/> class.
25-
/// </summary>
26-
internal ConditionList(IEnumerable<TypeDefinition> classes, bool should, FunctionSequence sequence)
20+
21+
internal ConditionList(IEnumerable<TypeSpec> classes, bool should, FunctionSequence sequence)
2722
{
28-
_types = classes.ToList();
23+
_types = classes;
2924
_should = should;
3025
_sequence = sequence;
3126
}
3227

28+
3329
/// <summary>
3430
/// Returns an indication of whether all the selected types satisfy the conditions.
3531
/// </summary>
3632
/// <returns>An indication of whether the conditions are true, along with a list of types failing the check if they are not.</returns>
3733
public TestResult GetResult()
3834
{
3935
bool success;
36+
37+
var passingTypes = _sequence.Execute(_types);
38+
4039
if (_should)
4140
{
4241
// All the classes should meet the condition
43-
success = (_sequence.Execute(_types).Count() == _types.Count());
42+
success = (passingTypes.Count() == _types.Count());
4443
}
4544
else
4645
{
4746
// No classes should meet the condition
48-
success = (!_sequence.Execute(_types).Any());
47+
success = (passingTypes.Count() == 0);
4948
}
5049

5150
if (success)
@@ -54,47 +53,38 @@ public TestResult GetResult()
5453
}
5554

5655
// If we've failed, get a collection of failing types so these can be reported in a failing test.
57-
var failedTypes = _sequence.Execute(_types, selected: !_should).ToList();
56+
var failedTypes = _sequence.ExecuteExtended(_types, selected: !_should);
5857
return TestResult.Failure(failedTypes);
5958
}
6059

61-
/// <summary>
62-
/// Returns the number of types that satisfy the conditions.
63-
/// </summary>
64-
/// <returns>A list of types.</returns>
65-
public int Count()
66-
{
67-
return _sequence.Execute(_types).Count();
68-
}
69-
7060
/// <summary>
7161
/// Returns the list of types that satisfy the conditions.
7262
/// </summary>
7363
/// <returns>A list of types.</returns>
7464
public IEnumerable<Type> GetTypes()
7565
{
76-
return _sequence.Execute(_types).Select(t => t.ToType());
66+
return _sequence.Execute(_types).Select(t => t.Definition.ToType());
7767
}
7868

7969
/// <summary>
8070
/// Specifies that any subsequent condition should be treated as an "and" condition.
8171
/// </summary>
8272
/// <returns>An set of conditions that can be applied to a list of classes.</returns>
8373
/// <remarks>And() has higher priority than Or() and it is computed first.</remarks>
84-
public Conditions And()
74+
public Condition And()
8575
{
86-
return new Conditions(_types, _should, _sequence);
76+
return new Condition(_types, _should, _sequence);
8777
}
8878

8979
/// <summary>
9080
/// Specifies that any subsequent conditions should be treated as part of an "or" condition.
9181
/// </summary>
9282
/// <returns>An set of conditions that can be applied to a list of classes.</returns>
93-
public Conditions Or()
83+
public Condition Or()
9484
{
9585
// Create a new group of functions - this has the effect of creating an "or" condition
9686
_sequence.CreateGroup();
97-
return new Conditions(_types, _should, _sequence);
87+
return new Condition(_types, _should, _sequence);
9888
}
9989
}
100-
}
90+
}

src/NetArchTest.Rules/Dependencies/DependencySearch.cs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.Collections.Generic;
55
using System.Linq;
66
using Mono.Cecil;
7+
using NetArchTest.Rules.Assemblies;
78
using NetArchTest.Rules.Dependencies.DataStructures;
89

910

@@ -18,7 +19,7 @@ internal class DependencySearch
1819
/// <param name="input">The set of type definitions to search.</param>
1920
/// <param name="dependencies">The set of dependencies to look for.</param>
2021
/// <returns>A list of found types.</returns>
21-
public IReadOnlyList<TypeDefinition> FindTypesThatHaveDependencyOnAny(IEnumerable<TypeDefinition> input, IEnumerable<string> dependencies)
22+
public IReadOnlyList<TypeSpec> FindTypesThatHaveDependencyOnAny(IEnumerable<TypeSpec> input, IEnumerable<string> dependencies)
2223
{
2324
return FindTypes(input, TypeDefinitionCheckingResult.SearchType.HaveDependencyOnAny, dependencies, true);
2425
}
@@ -29,7 +30,7 @@ public IReadOnlyList<TypeDefinition> FindTypesThatHaveDependencyOnAny(IEnumerabl
2930
/// <param name="input">The set of type definitions to search.</param>
3031
/// <param name="dependencies">The set of dependencies to look for.</param>
3132
/// <returns>A list of found types.</returns>
32-
public IReadOnlyList<TypeDefinition> FindTypesThatHaveDependencyOnAll(IEnumerable<TypeDefinition> input, IEnumerable<string> dependencies)
33+
public IReadOnlyList<TypeSpec> FindTypesThatHaveDependencyOnAll(IEnumerable<TypeSpec> input, IEnumerable<string> dependencies)
3334
{
3435
return FindTypes(input, TypeDefinitionCheckingResult.SearchType.HaveDependencyOnAll, dependencies, true);
3536
}
@@ -40,7 +41,7 @@ public IReadOnlyList<TypeDefinition> FindTypesThatHaveDependencyOnAll(IEnumerabl
4041
/// <param name="input">The set of type definitions to search.</param>
4142
/// <param name="dependencies">The set of dependencies to look for.</param>
4243
/// <returns>A list of found types.</returns>
43-
public IReadOnlyList<TypeDefinition> FindTypesThatOnlyHaveDependenciesOnAnyOrNone(IEnumerable<TypeDefinition> input, IEnumerable<string> dependencies)
44+
public IReadOnlyList<TypeSpec> FindTypesThatOnlyHaveDependenciesOnAnyOrNone(IEnumerable<TypeSpec> input, IEnumerable<string> dependencies)
4445
{
4546
return FindTypes(input, TypeDefinitionCheckingResult.SearchType.OnlyHaveDependenciesOnAnyOrNone, dependencies, false);
4647
}
@@ -51,7 +52,7 @@ public IReadOnlyList<TypeDefinition> FindTypesThatOnlyHaveDependenciesOnAnyOrNon
5152
/// <param name="input">The set of type definitions to search.</param>
5253
/// <param name="dependencies">The set of dependencies to look for.</param>
5354
/// <returns>A list of found types.</returns>
54-
public IReadOnlyList<TypeDefinition> FindTypesThatOnlyHaveDependenciesOnAny(IEnumerable<TypeDefinition> input, IEnumerable<string> dependencies)
55+
public IReadOnlyList<TypeSpec> FindTypesThatOnlyHaveDependenciesOnAny(IEnumerable<TypeSpec> input, IEnumerable<string> dependencies)
5556
{
5657
return FindTypes(input, TypeDefinitionCheckingResult.SearchType.OnlyHaveDependenciesOnAny, dependencies, false);
5758
}
@@ -62,14 +63,14 @@ public IReadOnlyList<TypeDefinition> FindTypesThatOnlyHaveDependenciesOnAny(IEnu
6263
/// <param name="input">The set of type definitions to search.</param>
6364
/// <param name="dependencies">The set of dependencies to look for.</param>
6465
/// <returns>A list of found types.</returns>
65-
public IReadOnlyList<TypeDefinition> FindTypesThatOnlyOnlyHaveDependenciesOnAll(IEnumerable<TypeDefinition> input, IEnumerable<string> dependencies)
66+
public IReadOnlyList<TypeSpec> FindTypesThatOnlyOnlyHaveDependenciesOnAll(IEnumerable<TypeSpec> input, IEnumerable<string> dependencies)
6667
{
6768
return FindTypes(input, TypeDefinitionCheckingResult.SearchType.OnlyHaveDependenciesOnAll, dependencies, false);
6869
}
6970

70-
private List<TypeDefinition> FindTypes(IEnumerable<TypeDefinition> input, TypeDefinitionCheckingResult.SearchType searchType, IEnumerable<string> dependencies, bool serachForDependencyInFieldConstant)
71+
private List<TypeSpec> FindTypes(IEnumerable<TypeSpec> input, TypeDefinitionCheckingResult.SearchType searchType, IEnumerable<string> dependencies, bool serachForDependencyInFieldConstant)
7172
{
72-
var output = new List<TypeDefinition>();
73+
var output = new List<TypeSpec>();
7374
var searchTree = new CachedNamespaceTree(dependencies);
7475

7576
foreach (var type in input)

0 commit comments

Comments
 (0)