From a6a49ecc3a7d9879fee076542895d8704e3adb65 Mon Sep 17 00:00:00 2001 From: NeVeSpl Date: Sun, 26 Nov 2023 06:08:37 +0100 Subject: [PATCH] add TestResult.LoadedTypes and TestResult.SelectedTypesForTesting --- README.md | 6 +- documentation/readme.nt | 6 +- sources/NetArchTest/IType.cs | 1 - .../RuleEngine/FunctionSequence.cs | 6 +- sources/NetArchTest/RuleEngine/RuleContext.cs | 20 +++--- .../NetArchTest/Slices/SliceConditionList.cs | 6 +- sources/NetArchTest/TestResult.cs | 63 +++++++++---------- 7 files changed, 54 insertions(+), 54 deletions(-) diff --git a/README.md b/README.md index 3408120..32cc849 100644 --- a/README.md +++ b/README.md @@ -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(); ``` diff --git a/documentation/readme.nt b/documentation/readme.nt index cb543f3..a2731c7 100644 --- a/documentation/readme.nt +++ b/documentation/readme.nt @@ -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(); ``` diff --git a/sources/NetArchTest/IType.cs b/sources/NetArchTest/IType.cs index e1380b8..9770329 100644 --- a/sources/NetArchTest/IType.cs +++ b/sources/NetArchTest/IType.cs @@ -30,7 +30,6 @@ public interface IType /// string Explanation { get; } - /// /// Path to the source file where type is defined. /// diff --git a/sources/NetArchTest/RuleEngine/FunctionSequence.cs b/sources/NetArchTest/RuleEngine/FunctionSequence.cs index dbf8e9d..9d950e4 100644 --- a/sources/NetArchTest/RuleEngine/FunctionSequence.cs +++ b/sources/NetArchTest/RuleEngine/FunctionSequence.cs @@ -29,16 +29,16 @@ public void CreateGroup() } - public IEnumerable ExecuteToGetFailingTypes(IEnumerable inputTypes, bool selected, Options options, IEnumerable allTypes) + public IReadOnlyList ExecuteToGetFailingTypes(IReadOnlyList inputTypes, bool selected, Options options, IEnumerable allTypes) { return Execute(inputTypes, selected, true, options, allTypes); } - public IEnumerable Execute(IEnumerable inputTypes, Options options, IEnumerable allTypes) + public IReadOnlyList Execute(IReadOnlyList inputTypes, Options options, IEnumerable allTypes) { return Execute(inputTypes, true, false, options, allTypes); } - private IEnumerable Execute(IEnumerable inputTypes, bool selected, bool isFailPathRun, Options options, IEnumerable allTypes) + private IReadOnlyList Execute(IReadOnlyList inputTypes, bool selected, bool isFailPathRun, Options options, IEnumerable allTypes) { if (isEmpty) return inputTypes; diff --git a/sources/NetArchTest/RuleEngine/RuleContext.cs b/sources/NetArchTest/RuleEngine/RuleContext.cs index 9b28b3c..ab46ac0 100644 --- a/sources/NetArchTest/RuleEngine/RuleContext.cs +++ b/sources/NetArchTest/RuleEngine/RuleContext.cs @@ -9,22 +9,22 @@ namespace NetArchTest.RuleEngine { internal class RuleContext { - private readonly IReadOnlyList types; + private readonly IReadOnlyList lodedTypes; public PredicateContext PredicateContext { get; } = new PredicateContext(); public ConditionContext ConditionContext { get; } = new ConditionContext(); public RuleContext(IEnumerable inpuTypes) { - types = inpuTypes.ToArray(); + lodedTypes = inpuTypes.ToArray(); } public IEnumerable Execute(Options options) { - IEnumerable 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; } @@ -32,8 +32,8 @@ 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) { @@ -48,12 +48,12 @@ public TestResult GetResult(Options options) if (success) { - return TestResult.Success(); + return new TestResult(lodedTypes, filteredTypes, Array.Empty(), 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); } diff --git a/sources/NetArchTest/Slices/SliceConditionList.cs b/sources/NetArchTest/Slices/SliceConditionList.cs index c5fe377..fb18d8a 100644 --- a/sources/NetArchTest/Slices/SliceConditionList.cs +++ b/sources/NetArchTest/Slices/SliceConditionList.cs @@ -1,5 +1,6 @@ using System; using System.Linq; +using NetArchTest.Assemblies; using NetArchTest.Rules; using NetArchTest.Slices.Model; @@ -42,12 +43,13 @@ public TestResult GetResult() if (isSuccessful) { - return TestResult.Success(); + // todo replace Array.Empty() with real data + return new TestResult(Array.Empty(), Array.Empty(), Array.Empty(), true); } else { var failingTypes = filteredTypes.Where(x => x.IsPassing == !successIsWhen); - return TestResult.Failure(failingTypes.Select(x => x.TypeSpec)); + return new TestResult(Array.Empty(), Array.Empty(), failingTypes.Select(x => x.TypeSpec), false); } } } diff --git a/sources/NetArchTest/TestResult.cs b/sources/NetArchTest/TestResult.cs index e2dae27..2a13e6f 100644 --- a/sources/NetArchTest/TestResult.cs +++ b/sources/NetArchTest/TestResult.cs @@ -9,11 +9,27 @@ namespace NetArchTest.Rules /// /// Defines a result from a test carried out on a . /// - [DebuggerDisplay("FailingTypes = {FailingTypes.Count}")] + [DebuggerDisplay("LoadedTypes = {LoadedTypes.Count}, SelectedTypesForTesting = {SelectedTypesForTesting.Count}, FailingTypes = {FailingTypes.Count}")] public sealed class TestResult - { - private TestResult() + { + //private readonly IEnumerable loadedTypes; + //private readonly IEnumerable selectedTypes; + //private readonly IEnumerable failingTypes; + + [DebuggerBrowsable(DebuggerBrowsableState.Never)] + private readonly Lazy> lazyLoadedTypes; + [DebuggerBrowsable(DebuggerBrowsableState.Never)] + private readonly Lazy> lazySelectedTypes; + [DebuggerBrowsable(DebuggerBrowsableState.Never)] + private readonly Lazy> lazyFailingTypes; + + + internal TestResult(IEnumerable loadedTypes, IEnumerable selectedTypes, IEnumerable failingTypes, bool isSuccessful) { + lazyLoadedTypes = new Lazy>(() => loadedTypes.Select(x => x.CreateWrapper()).ToArray()); + lazySelectedTypes = new Lazy>(() => selectedTypes.Select(x => x.CreateWrapper()).ToArray()); + lazyFailingTypes = new Lazy>(() => failingTypes.Select(x => x.CreateWrapper()).ToArray()); + IsSuccessful = isSuccessful; } @@ -23,38 +39,21 @@ private TestResult() public bool IsSuccessful { get; private set; } /// - /// Gets a list of the types that failed the test. - /// - /// - /// 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. - /// - [DebuggerDisplay("[{FailingTypes.Count}]")] - public IReadOnlyList FailingTypes { get; private set; } = Array.Empty(); - + /// Gets a list of all the types that were loded by . + /// + [DebuggerDisplay("[{LoadedTypes.Count}]")] + public IReadOnlyList LoadedTypes => lazyLoadedTypes.Value; /// - /// Creates a new instance of indicating a successful test. - /// - /// Instance of - 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. + /// + [DebuggerDisplay("[{SelectedTypesForTesting.Count}]")] + public IReadOnlyList SelectedTypesForTesting => lazySelectedTypes.Value; /// - /// Creates a new instance of indicating a failed test. - /// - /// Instance of - internal static TestResult Failure(IEnumerable failingTypes) - { - return new TestResult - { - IsSuccessful = false, - FailingTypes = failingTypes.Select(x => x.CreateWrapper()).ToArray() - }; - } + /// Gets a list of the types that failed the test. + /// + [DebuggerDisplay("[{FailingTypes.Count}]")] + public IReadOnlyList FailingTypes => lazyFailingTypes.Value; } } \ No newline at end of file