Skip to content

Commit

Permalink
add TestResult.LoadedAssemblies
Browse files Browse the repository at this point in the history
  • Loading branch information
NeVeSpl committed Nov 30, 2023
1 parent ca4af1e commit f0c1cac
Show file tree
Hide file tree
Showing 19 changed files with 190 additions and 76 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ var types = result.FailingTypes;
```

> **Tip**
Loading types is time time-consuming, since `Type` class is immutable, it can be shared between tests.
Loading types is time-consuming, since `Type` class is immutable, its instance can be shared between tests.

## Rules for dependency analysis

Expand Down
31 changes: 30 additions & 1 deletion documentation/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
* [AreSealed](#PredicateAreSealed)
* [AreStateless](#PredicateAreStateless)
* [AreStatic](#PredicateAreStatic)
* [AreStaticless](#PredicateAreStaticless)
* [AreStructures](#PredicateAreStructures)
* [AreUsedByAny](#PredicateAreUsedByAny)
* [DoNotHaveCustomAttribute](#PredicateDoNotHaveCustomAttribute)
Expand Down Expand Up @@ -131,6 +132,7 @@
* [BeSealed](#ConditionBeSealed)
* [BeStateless](#ConditionBeStateless)
* [BeStatic](#ConditionBeStatic)
* [BeStaticless](#ConditionBeStaticless)
* [BeStructures](#ConditionBeStructures)
* [BeUsedByAny](#ConditionBeUsedByAny)
* [HaveCustomAttribute](#ConditionHaveCustomAttribute)
Expand Down Expand Up @@ -209,6 +211,7 @@

* [FailingTypes](#TestResultFailingTypes)
* [IsSuccessful](#TestResultIsSuccessful)
* [LoadedAssemblies](#TestResultLoadedAssemblies)
* [LoadedTypes](#TestResultLoadedTypes)
* [SelectedTypesForTesting](#TestResultSelectedTypesForTesting)

Expand All @@ -220,14 +223,18 @@
* [ReflectionType](#ITypeReflectionType)
* [SourceFilePath](#ITypeSourceFilePath)

## IAssembly

* [FullName](#IAssemblyFullName)

## Options

* [Comparer](#OptionsComparer)

## Types
### Types.FromFile
```csharp
Types Types.FromFile(string fileName)
Types Types.FromFile(string fileName, IEnumerable<string> searchDirectories = null)
```
Creates a list of all the types in a particular module file.
### Types.FromPath
Expand Down Expand Up @@ -477,6 +484,11 @@ Selects types that are stateless, they do not have instance state
PredicateList Predicate.AreStatic()
```
Selects types that are static.
### Predicate.AreStaticless
```csharp
PredicateList Predicate.AreStaticless()
```
Selects types that are staticless, they do not have static state
### Predicate.AreStructures
```csharp
PredicateList Predicate.AreStructures()
Expand Down Expand Up @@ -836,6 +848,11 @@ Selects types that are stateless, they do not have instance state
ConditionList Condition.BeStatic()
```
Selects types that are static.
### Condition.BeStaticless
```csharp
ConditionList Condition.BeStaticless()
```
Selects types that are staticless, they do not have static state
### Condition.BeStructures
```csharp
ConditionList Condition.BeStructures()
Expand Down Expand Up @@ -1200,6 +1217,11 @@ Gets a list of the types that failed the test.
IsSuccessful
```
Gets a flag indicating the success or failure of the test.
### TestResult.LoadedAssemblies
```csharp
LoadedAssemblies
```
Gets a list of all the assemblies that were loded by <see cref="T:NetArchTest.Rules.Types"/>.
### TestResult.LoadedTypes
```csharp
LoadedTypes
Expand Down Expand Up @@ -1238,6 +1260,13 @@ SourceFilePath
```
Path to the source file where type is defined.

## IAssembly
### IAssembly.FullName
```csharp
FullName
```
FullName of the assembly

## Options
### Options.Comparer
```csharp
Expand Down
3 changes: 2 additions & 1 deletion documentation/api.nt
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ capture output
condition_type = data.Classes | Symbols.WhereNamespaceStartsWith "NetArchTest.Rules" | Symbols.WhereNameMatches "^Condition"
testresult_type = data.Classes | Symbols.WhereNamespaceStartsWith "NetArchTest.Rules" | Symbols.WhereNameMatches "^TestResult"
itype_type = data.Interfaces | Symbols.WhereNamespaceStartsWith "NetArchTest.Rules" | Symbols.WhereNameMatches "^IType"
iassembly_type = data.Interfaces | Symbols.WhereNamespaceStartsWith "NetArchTest.Rules" | Symbols.WhereNameMatches "^IAssembly"
options_type = data.Classes | Symbols.WhereNamespaceStartsWith "NetArchTest.Rules" | Symbols.WhereNameMatches "^Options"

all_types = types_type | Array.Concat predicate_type | Array.Concat condition_type | Array.Concat testresult_type | Array.Concat itype_type | Array.Concat options_type
all_types = types_type | Array.Concat predicate_type | Array.Concat condition_type | Array.Concat testresult_type | Array.Concat itype_type | Array.Concat iassembly_type | Array.Concat options_type
}}


Expand Down
2 changes: 1 addition & 1 deletion documentation/readme.nt
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ var types = result.FailingTypes;
```

> **Tip**
Loading types is time time-consuming, since `Type` class is immutable, it can be shared between tests.
Loading types is time-consuming, since `Type` class is immutable, its instance can be shared between tests.

## Rules for dependency analysis

Expand Down
8 changes: 8 additions & 0 deletions sources/NetArchTest/Assemblies/AssemblySpec.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using System.Collections.Generic;
using System.Linq;
using Mono.Cecil;
using NetArchTest.Assemblies.PublicUse;
using NetArchTest.Rules;

namespace NetArchTest.Assemblies
{
Expand All @@ -21,5 +23,11 @@ public IEnumerable<TypeSpec> GetTypes()
{
return typeDefinitions.Select(x => new TypeSpec(x));
}


public IAssembly CreateWrapper()
{
return new AssemblyContainer(assemblyDefinition);
}
}
}
2 changes: 1 addition & 1 deletion sources/NetArchTest/Assemblies/DataLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ private static IEnumerable<AssemblySpec> Load(IEnumerable<string> fileNames, IEn
var assemblyDefinition = ReadAssemblyDefinition(fileName, readerParameters);
if (assemblyDefinition == null) continue;

if (exclusionTree.GetAllMatchingNames(assemblyDefinition.FullName).Any() == true) continue;
if (exclusionTree.GetAllMatchingNames(assemblyDefinition.Name.Name).Any() == true) continue;

var typeDefinitions = ReadTypes(assemblyDefinition);

Expand Down
22 changes: 22 additions & 0 deletions sources/NetArchTest/Assemblies/PublicUse/AssemblyContainer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using System.Diagnostics;
using Mono.Cecil;
using NetArchTest.Rules;

namespace NetArchTest.Assemblies.PublicUse
{
[DebuggerDisplay("{FullName}")]
internal sealed class AssemblyContainer : IAssembly
{
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
private readonly AssemblyDefinition _monoAssemblyDefinition;


public string FullName => _monoAssemblyDefinition.FullName;


public AssemblyContainer(AssemblyDefinition assemblyDefinition)
{
_monoAssemblyDefinition = assemblyDefinition;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
using Mono.Cecil;
using NetArchTest.Rules;

namespace NetArchTest.Assemblies
namespace NetArchTest.Assemblies.PublicUse
{
[DebuggerDisplay("{FullName}")]
internal sealed class TypeContainer : IType
Expand All @@ -16,33 +16,33 @@ internal sealed class TypeContainer : IType
private readonly Lazy<string> _sourceFilePath;


public Type ReflectionType => _reflactionType.Value;
public string FullName => _monoTypeDefinition.FullName;
public string Name => _monoTypeDefinition.Name;
public string Explanation { get; }
public string SourceFilePath => _sourceFilePath.Value;


internal TypeContainer(TypeDefinition monoTypeDefinition, string explanation)
{
_monoTypeDefinition = monoTypeDefinition;
_reflactionType = new Lazy<Type>(() =>
{
{
try
{
return _monoTypeDefinition.ToType();
}
catch
{
catch
{
}
return null;
});
_sourceFilePath = new Lazy<string>(() => _monoTypeDefinition.GetFilePath());
Explanation = explanation;
}


public Type ReflectionType => _reflactionType.Value;
public string FullName => _monoTypeDefinition.FullName;
public string Name => _monoTypeDefinition.Name;
public string Explanation { get; }
public string SourceFilePath => _sourceFilePath.Value;
}


public static implicit operator System.Type(TypeContainer type)
public static implicit operator Type(TypeContainer type)
{
return type.ReflectionType;
}
Expand Down
1 change: 1 addition & 0 deletions sources/NetArchTest/Assemblies/TypeSpec.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Diagnostics;
using Mono.Cecil;
using NetArchTest.Assemblies.PublicUse;

namespace NetArchTest.Assemblies
{
Expand Down
17 changes: 17 additions & 0 deletions sources/NetArchTest/IAssembly.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace NetArchTest.Rules
{
/// <summary>
/// Assembly wrapper.
/// </summary>
public interface IAssembly
{
/// <summary>
/// FullName of the assembly
/// </summary>
string FullName { get; }
}
}
2 changes: 1 addition & 1 deletion sources/NetArchTest/RuleEngine/FunctionSequence.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public IReadOnlyList<TypeSpec> ExecuteToGetFailingTypes(IReadOnlyList<TypeSpec>
{
return Execute(inputTypes, selected, true, options, allTypes);
}
public IReadOnlyList<TypeSpec> Execute(IReadOnlyList<TypeSpec> inputTypes, Options options, IEnumerable<TypeSpec> allTypes)
public IReadOnlyList<TypeSpec> Execute(IReadOnlyList<TypeSpec> inputTypes, Options options, IReadOnlyList<TypeSpec> allTypes)
{
return Execute(inputTypes, true, false, options, allTypes);
}
Expand Down
27 changes: 14 additions & 13 deletions sources/NetArchTest/RuleEngine/RuleContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,36 +9,37 @@ namespace NetArchTest.RuleEngine
{
internal class RuleContext
{
private readonly IReadOnlyList<TypeSpec> lodedTypes;
internal readonly LoadedData loadedData;
public PredicateContext PredicateContext { get; } = new PredicateContext();
public ConditionContext ConditionContext { get; } = new ConditionContext();


public RuleContext(LoadedData loadedData)
{
lodedTypes = loadedData.GetTypes().ToArray();
this.loadedData = loadedData;
}


public IEnumerable<TypeSpec> Execute(Options options)
public IReadOnlyList<TypeSpec> Execute(Options options)
{
var result = lodedTypes;
result = PredicateContext.Sequence.Execute(result, options, lodedTypes);
result = ConditionContext.Sequence.Execute(result, options, lodedTypes);
return result;
var lodedTypes = loadedData.GetTypes().ToArray();
var selectedTypes = PredicateContext.Sequence.Execute(lodedTypes, options, lodedTypes);
var passingTypes = ConditionContext.Sequence.Execute(selectedTypes, options, lodedTypes);
return passingTypes;
}

public TestResult GetResult(Options options)
{
bool success;

var filteredTypes = PredicateContext.Sequence.Execute(lodedTypes, options, lodedTypes);
var passingTypes = ConditionContext.Sequence.Execute(filteredTypes, options, lodedTypes);
var lodedTypes = loadedData.GetTypes().ToArray();
var selectedTypes = PredicateContext.Sequence.Execute(lodedTypes, options, lodedTypes);
var passingTypes = ConditionContext.Sequence.Execute(selectedTypes, options, lodedTypes);

if (ConditionContext.Should)
{
// All the classes should meet the condition
success = (passingTypes.Count() == filteredTypes.Count());
success = (passingTypes.Count() == selectedTypes.Count());
}
else
{
Expand All @@ -48,12 +49,12 @@ public TestResult GetResult(Options options)

if (success)
{
return new TestResult(lodedTypes, filteredTypes, Array.Empty<TypeSpec>(), true);
return new TestResult(loadedData.Assemblies, lodedTypes, selectedTypes, Array.Empty<TypeSpec>(), true);
}

// If we've failed, get a collection of failing types so these can be reported in a failing test.
var failedTypes = ConditionContext.Sequence.ExecuteToGetFailingTypes(filteredTypes, selected: !ConditionContext.Should, options, lodedTypes);
return new TestResult(lodedTypes, filteredTypes, failedTypes, false);
var failedTypes = ConditionContext.Sequence.ExecuteToGetFailingTypes(selectedTypes, selected: !ConditionContext.Should, options, lodedTypes);
return new TestResult(loadedData.Assemblies, lodedTypes, selectedTypes, failedTypes, false);
}


Expand Down
29 changes: 29 additions & 0 deletions sources/NetArchTest/Slices/Model/SliceContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using System.Collections.Generic;
using NetArchTest.Assemblies;

namespace NetArchTest.Slices.Model
{
internal class SliceContext
{
private readonly LoadedData loadedData;
private readonly SlicedTypes slicedTypes;

public SliceContext(LoadedData loadedData, SlicedTypes slicedTypes)
{
this.loadedData = loadedData;
this.slicedTypes = slicedTypes;
}



public IReadOnlyList<AssemblySpec> GetAssemblies()
{
return loadedData.Assemblies;
}

public SlicedTypes GetTypes()
{
return slicedTypes;
}
}
}
Loading

0 comments on commit f0c1cac

Please sign in to comment.