Skip to content

Commit

Permalink
add TestResult.LoadedTypes and TestResult.SelectedTypesForTesting
Browse files Browse the repository at this point in the history
  • Loading branch information
NeVeSpl committed Nov 26, 2023
1 parent 9e69647 commit a6a49ec
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 54 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,16 +127,16 @@ public class SampleApp_ModuleOmega_Tests

The fluent API should direct you in building up a rule, based on a combination of [predicates](documentation/api.md#predicate), [conditions](documentation/api.md#condition) and conjunctions.

The starting point for any rule is the static [`Types`](documentation/api.md#types) class, where you load a set of types from a path, assembly.
The starting point for any rule is the static [`Types`](documentation/api.md#types) class, where you load a set of types from a assembly, domain or patth.

```csharp
var types = Types.InAssembly(typeof(MyClass).Assembly);
```
Once you have selected the types you can filter them using one or more predicates. These can be chained together using `And()` or `Or()` conjunctions:
Once you have loaded the types, you can filter them using one or more predicates. These can be chained together using `And()` or `Or()` conjunctions:
```csharp
types.That().ResideInNamespace("MyProject.Data");
```
Once the set of classes have been filtered you can apply a set of conditions using the `Should()` or `ShouldNot()` methods, e.g.
Once the set of types have been filtered, you can apply a set of conditions using the `Should()` or `ShouldNot()` methods, e.g.
```csharp
types.That().ResideInNamespace("MyProject.Data").Should().BeSealed();
```
Expand Down
6 changes: 3 additions & 3 deletions documentation/readme.nt
Original file line number Diff line number Diff line change
Expand Up @@ -78,16 +78,16 @@ The library is available as a package on NuGet: [NetArchTest.eNhancedEdition](ht

The fluent API should direct you in building up a rule, based on a combination of [predicates](documentation/api.md#predicate), [conditions](documentation/api.md#condition) and conjunctions.

The starting point for any rule is the static [`Types`](documentation/api.md#types) class, where you load a set of types from a path, assembly.
The starting point for any rule is the static [`Types`](documentation/api.md#types) class, where you load a set of types from a assembly, domain or patth.

```csharp
var types = Types.InAssembly(typeof(MyClass).Assembly);
```
Once you have selected the types you can filter them using one or more predicates. These can be chained together using `And()` or `Or()` conjunctions:
Once you have loaded the types, you can filter them using one or more predicates. These can be chained together using `And()` or `Or()` conjunctions:
```csharp
types.That().ResideInNamespace("MyProject.Data");
```
Once the set of classes have been filtered you can apply a set of conditions using the `Should()` or `ShouldNot()` methods, e.g.
Once the set of types have been filtered, you can apply a set of conditions using the `Should()` or `ShouldNot()` methods, e.g.
```csharp
types.That().ResideInNamespace("MyProject.Data").Should().BeSealed();
```
Expand Down
1 change: 0 additions & 1 deletion sources/NetArchTest/IType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ public interface IType
/// </summary>
string Explanation { get; }


/// <summary>
/// Path to the source file where type is defined.
/// </summary>
Expand Down
6 changes: 3 additions & 3 deletions sources/NetArchTest/RuleEngine/FunctionSequence.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,16 @@ public void CreateGroup()
}


public IEnumerable<TypeSpec> ExecuteToGetFailingTypes(IEnumerable<TypeSpec> inputTypes, bool selected, Options options, IEnumerable<TypeSpec> allTypes)
public IReadOnlyList<TypeSpec> ExecuteToGetFailingTypes(IReadOnlyList<TypeSpec> inputTypes, bool selected, Options options, IEnumerable<TypeSpec> allTypes)
{
return Execute(inputTypes, selected, true, options, allTypes);
}
public IEnumerable<TypeSpec> Execute(IEnumerable<TypeSpec> inputTypes, Options options, IEnumerable<TypeSpec> allTypes)
public IReadOnlyList<TypeSpec> Execute(IReadOnlyList<TypeSpec> inputTypes, Options options, IEnumerable<TypeSpec> allTypes)
{
return Execute(inputTypes, true, false, options, allTypes);
}

private IEnumerable<TypeSpec> Execute(IEnumerable<TypeSpec> inputTypes, bool selected, bool isFailPathRun, Options options, IEnumerable<TypeSpec> allTypes)
private IReadOnlyList<TypeSpec> Execute(IReadOnlyList<TypeSpec> inputTypes, bool selected, bool isFailPathRun, Options options, IEnumerable<TypeSpec> allTypes)
{
if (isEmpty) return inputTypes;

Expand Down
20 changes: 10 additions & 10 deletions sources/NetArchTest/RuleEngine/RuleContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,31 +9,31 @@ namespace NetArchTest.RuleEngine
{
internal class RuleContext
{
private readonly IReadOnlyList<TypeSpec> types;
private readonly IReadOnlyList<TypeSpec> lodedTypes;
public PredicateContext PredicateContext { get; } = new PredicateContext();
public ConditionContext ConditionContext { get; } = new ConditionContext();


public RuleContext(IEnumerable<TypeSpec> inpuTypes)
{
types = inpuTypes.ToArray();
lodedTypes = inpuTypes.ToArray();
}


public IEnumerable<TypeSpec> Execute(Options options)
{
IEnumerable<TypeSpec> result = types;
result = PredicateContext.Sequence.Execute(result, options, types);
result = ConditionContext.Sequence.Execute(result, options, types);
var result = lodedTypes;
result = PredicateContext.Sequence.Execute(result, options, lodedTypes);
result = ConditionContext.Sequence.Execute(result, options, lodedTypes);
return result;
}

public TestResult GetResult(Options options)
{
bool success;

var filteredTypes = PredicateContext.Sequence.Execute(types, options, types);
var passingTypes = ConditionContext.Sequence.Execute(filteredTypes, options, types);
var filteredTypes = PredicateContext.Sequence.Execute(lodedTypes, options, lodedTypes);
var passingTypes = ConditionContext.Sequence.Execute(filteredTypes, options, lodedTypes);

if (ConditionContext.Should)
{
Expand All @@ -48,12 +48,12 @@ public TestResult GetResult(Options options)

if (success)
{
return TestResult.Success();
return new TestResult(lodedTypes, filteredTypes, 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, types);
return TestResult.Failure(failedTypes);
var failedTypes = ConditionContext.Sequence.ExecuteToGetFailingTypes(filteredTypes, selected: !ConditionContext.Should, options, lodedTypes);
return new TestResult(lodedTypes, filteredTypes, failedTypes, false);
}


Expand Down
6 changes: 4 additions & 2 deletions sources/NetArchTest/Slices/SliceConditionList.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Linq;
using NetArchTest.Assemblies;
using NetArchTest.Rules;
using NetArchTest.Slices.Model;

Expand Down Expand Up @@ -42,12 +43,13 @@ public TestResult GetResult()

if (isSuccessful)
{
return TestResult.Success();
// todo replace Array.Empty<TypeSpec>() with real data
return new TestResult(Array.Empty<TypeSpec>(), Array.Empty<TypeSpec>(), Array.Empty<TypeSpec>(), true);
}
else
{
var failingTypes = filteredTypes.Where(x => x.IsPassing == !successIsWhen);
return TestResult.Failure(failingTypes.Select(x => x.TypeSpec));
return new TestResult(Array.Empty<TypeSpec>(), Array.Empty<TypeSpec>(), failingTypes.Select(x => x.TypeSpec), false);
}
}
}
Expand Down
63 changes: 31 additions & 32 deletions sources/NetArchTest/TestResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,27 @@ namespace NetArchTest.Rules
/// <summary>
/// Defines a result from a test carried out on a <see cref="ConditionList"/>.
/// </summary>
[DebuggerDisplay("FailingTypes = {FailingTypes.Count}")]
[DebuggerDisplay("LoadedTypes = {LoadedTypes.Count}, SelectedTypesForTesting = {SelectedTypesForTesting.Count}, FailingTypes = {FailingTypes.Count}")]
public sealed class TestResult
{
private TestResult()
{
//private readonly IEnumerable<TypeSpec> loadedTypes;
//private readonly IEnumerable<TypeSpec> selectedTypes;
//private readonly IEnumerable<TypeSpec> failingTypes;

[DebuggerBrowsable(DebuggerBrowsableState.Never)]
private readonly Lazy<IReadOnlyList<IType>> lazyLoadedTypes;
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
private readonly Lazy<IReadOnlyList<IType>> lazySelectedTypes;
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
private readonly Lazy<IReadOnlyList<IType>> lazyFailingTypes;


internal TestResult(IEnumerable<TypeSpec> loadedTypes, IEnumerable<TypeSpec> selectedTypes, IEnumerable<TypeSpec> failingTypes, bool isSuccessful)
{
lazyLoadedTypes = new Lazy<IReadOnlyList<IType>>(() => loadedTypes.Select(x => x.CreateWrapper()).ToArray());
lazySelectedTypes = new Lazy<IReadOnlyList<IType>>(() => selectedTypes.Select(x => x.CreateWrapper()).ToArray());
lazyFailingTypes = new Lazy<IReadOnlyList<IType>>(() => failingTypes.Select(x => x.CreateWrapper()).ToArray());
IsSuccessful = isSuccessful;
}


Expand All @@ -23,38 +39,21 @@ private TestResult()
public bool IsSuccessful { get; private set; }

/// <summary>
/// Gets a list of the types that failed the test.
/// </summary>
/// <remarks>
/// This method loads all the types and may throw dependency loading errors if the test project does not have a direct dependency on the type being loaded.
/// </remarks>
[DebuggerDisplay("[{FailingTypes.Count}]")]
public IReadOnlyList<IType> FailingTypes { get; private set; } = Array.Empty<IType>();

/// Gets a list of all the types that were loded by <see cref="Types"/>.
/// </summary>
[DebuggerDisplay("[{LoadedTypes.Count}]")]
public IReadOnlyList<IType> LoadedTypes => lazyLoadedTypes.Value;

/// <summary>
/// Creates a new instance of <see cref="TestResult"/> indicating a successful test.
/// </summary>
/// <returns>Instance of <see cref="TestResult"/></returns>
internal static TestResult Success()
{
return new TestResult
{
IsSuccessful = true
};
}
/// Gets a list of the types that passed filtering by predicates and were used as input to conditions.
/// </summary>
[DebuggerDisplay("[{SelectedTypesForTesting.Count}]")]
public IReadOnlyList<IType> SelectedTypesForTesting => lazySelectedTypes.Value;

/// <summary>
/// Creates a new instance of <see cref="TestResult"/> indicating a failed test.
/// </summary>
/// <returns>Instance of <see cref="TestResult"/></returns>
internal static TestResult Failure(IEnumerable<TypeSpec> failingTypes)
{
return new TestResult
{
IsSuccessful = false,
FailingTypes = failingTypes.Select(x => x.CreateWrapper()).ToArray()
};
}
/// Gets a list of the types that failed the test.
/// </summary>
[DebuggerDisplay("[{FailingTypes.Count}]")]
public IReadOnlyList<IType> FailingTypes => lazyFailingTypes.Value;
}
}

0 comments on commit a6a49ec

Please sign in to comment.