diff --git a/Source/aweXpect.Core/Delegates/ThatDelegate.Throws.cs b/Source/aweXpect.Core/Delegates/ThatDelegate.Throws.cs index 192eddc20..289f61a2b 100644 --- a/Source/aweXpect.Core/Delegates/ThatDelegate.Throws.cs +++ b/Source/aweXpect.Core/Delegates/ThatDelegate.Throws.cs @@ -124,7 +124,7 @@ public override void AppendResult(StringBuilder stringBuilder, string? indentati public override ConstraintResult Negate() { - throwOptions.CheckThrow(!throwOptions.DoCheckThrow); + throwOptions.DoCheckThrow = !throwOptions.DoCheckThrow; return this; } } diff --git a/Source/aweXpect.Core/Delegates/ThatDelegate.ThrowsExactly.cs b/Source/aweXpect.Core/Delegates/ThatDelegate.ThrowsExactly.cs index b8a6686de..49231a19f 100644 --- a/Source/aweXpect.Core/Delegates/ThatDelegate.ThrowsExactly.cs +++ b/Source/aweXpect.Core/Delegates/ThatDelegate.ThrowsExactly.cs @@ -121,7 +121,7 @@ public override void AppendResult(StringBuilder stringBuilder, string? indentati public override ConstraintResult Negate() { - throwOptions.CheckThrow(!throwOptions.DoCheckThrow); + throwOptions.DoCheckThrow = !throwOptions.DoCheckThrow; return this; } } diff --git a/Source/aweXpect.Core/Delegates/ThatDelegate.cs b/Source/aweXpect.Core/Delegates/ThatDelegate.cs index a71df19c6..1ac5f9354 100644 --- a/Source/aweXpect.Core/Delegates/ThatDelegate.cs +++ b/Source/aweXpect.Core/Delegates/ThatDelegate.cs @@ -35,11 +35,15 @@ internal static string FormatForMessage(Exception? exception) return message; } - private sealed class DelegateIsNotNullWithinTimeoutConstraint(string it, ExpectationGrammars grammars, ThrowsOption options) + private sealed class DelegateIsNotNullWithinTimeoutConstraint( + string it, + ExpectationGrammars grammars, + ThrowsOption options) : ConstraintResult(grammars), IValueConstraint { private DelegateValue? _actual; + public ConstraintResult IsMetBy(DelegateValue value) { _actual = value; @@ -55,7 +59,7 @@ public ConstraintResult IsMetBy(DelegateValue value) Outcome = Outcome.Failure; return this; } - + Outcome = Outcome.Success; return this; } @@ -88,12 +92,22 @@ public override ConstraintResult Negate() => this; } - internal class ThrowsOption + /// + /// Options on expectations if a delegate throws. + /// + public class ThrowsOption { - public bool DoCheckThrow { get; private set; } = true; - - public TimeSpanEqualityOptions? ExecutionTimeOptions { get; set; } + /// + /// Flag indicating if the delegate is expected to throw an exception. + /// + /// + /// If set to , the delegate must not throw any exception. + /// + public bool DoCheckThrow { get; set; } = true; - public void CheckThrow(bool doCheckThrow) => DoCheckThrow = doCheckThrow; + /// + /// Options on the execution time to allow specifying a timeout. + /// + public TimeSpanEqualityOptions? ExecutionTimeOptions { get; set; } } } diff --git a/Source/aweXpect.Core/Delegates/ThatDelegateThrows.OnlyIf.cs b/Source/aweXpect.Core/Delegates/ThatDelegateThrows.OnlyIf.cs index 1ddaf90ef..e3862460e 100644 --- a/Source/aweXpect.Core/Delegates/ThatDelegateThrows.OnlyIf.cs +++ b/Source/aweXpect.Core/Delegates/ThatDelegateThrows.OnlyIf.cs @@ -8,7 +8,7 @@ public partial class ThatDelegateThrows /// public ThatDelegateThrows OnlyIf(bool predicate) { - _throwOptions.CheckThrow(predicate); - return new ThatDelegateThrows(ExpectationBuilder, _throwOptions); + ThrowOptions.DoCheckThrow = predicate; + return new ThatDelegateThrows(ExpectationBuilder, ThrowOptions); } } diff --git a/Source/aweXpect.Core/Delegates/ThatDelegateThrows.Within.cs b/Source/aweXpect.Core/Delegates/ThatDelegateThrows.Within.cs index 8202dea9c..a50530f17 100644 --- a/Source/aweXpect.Core/Delegates/ThatDelegateThrows.Within.cs +++ b/Source/aweXpect.Core/Delegates/ThatDelegateThrows.Within.cs @@ -17,7 +17,7 @@ public ThatDelegateThrows Within(TimeSpan duration) TimeSpanEqualityOptions options = new(); options.Within(duration); - _throwOptions.ExecutionTimeOptions = options; + ThrowOptions.ExecutionTimeOptions = options; return this; } } diff --git a/Source/aweXpect.Core/Delegates/ThatDelegateThrows.cs b/Source/aweXpect.Core/Delegates/ThatDelegateThrows.cs index c13e4f6d0..df7b2c730 100644 --- a/Source/aweXpect.Core/Delegates/ThatDelegateThrows.cs +++ b/Source/aweXpect.Core/Delegates/ThatDelegateThrows.cs @@ -7,21 +7,19 @@ namespace aweXpect.Delegates; /// /// An when an exception was thrown. /// -public partial class ThatDelegateThrows - : ExpectationResult>, IThatDelegateThrows +public partial class ThatDelegateThrows( + ExpectationBuilder expectationBuilder, + ThatDelegate.ThrowsOption throwOptions) + : ExpectationResult>(expectationBuilder), IThatDelegateThrows where TException : Exception? { - private readonly ThatDelegate.ThrowsOption _throwOptions; - - internal ThatDelegateThrows(ExpectationBuilder expectationBuilder, ThatDelegate.ThrowsOption throwOptions) - : base(expectationBuilder) - { - ExpectationBuilder = expectationBuilder; - _throwOptions = throwOptions; - } + /// + /// The throw options. + /// + public ThatDelegate.ThrowsOption ThrowOptions { get; } = throwOptions; /// /// The expectation builder. /// - public ExpectationBuilder ExpectationBuilder { get; } + public ExpectationBuilder ExpectationBuilder { get; } = expectationBuilder; } diff --git a/Tests/aweXpect.Core.Api.Tests/Expected/aweXpect.Core_net8.0.txt b/Tests/aweXpect.Core.Api.Tests/Expected/aweXpect.Core_net8.0.txt index a067b0bea..cfb0e40e9 100644 --- a/Tests/aweXpect.Core.Api.Tests/Expected/aweXpect.Core_net8.0.txt +++ b/Tests/aweXpect.Core.Api.Tests/Expected/aweXpect.Core_net8.0.txt @@ -460,6 +460,12 @@ namespace aweXpect.Delegates public aweXpect.Delegates.ThatDelegateThrows ThrowsExactly() where TException : System.Exception { } public aweXpect.Delegates.ThatDelegateThrows ThrowsException() { } + public class ThrowsOption + { + public ThrowsOption() { } + public bool DoCheckThrow { get; set; } + public aweXpect.Options.TimeSpanEqualityOptions? ExecutionTimeOptions { get; set; } + } public sealed class WithValue : aweXpect.Delegates.ThatDelegate, aweXpect.Core.IExpectThat>, aweXpect.Core.IThat> { public WithValue(aweXpect.Core.ExpectationBuilder expectationBuilder) { } @@ -488,7 +494,9 @@ namespace aweXpect.Delegates public class ThatDelegateThrows : aweXpect.Results.ExpectationResult>, aweXpect.Core.IExpectThat, aweXpect.Core.IThatDelegateThrows, aweXpect.Core.IThat where TException : System.Exception? { + public ThatDelegateThrows(aweXpect.Core.ExpectationBuilder expectationBuilder, aweXpect.Delegates.ThatDelegate.ThrowsOption throwOptions) { } public aweXpect.Core.ExpectationBuilder ExpectationBuilder { get; } + public aweXpect.Delegates.ThatDelegate.ThrowsOption ThrowOptions { get; } public aweXpect.Core.IThat Which { get; } public aweXpect.Delegates.ThatDelegateThrows OnlyIf(bool predicate) { } public aweXpect.Results.AndOrResult> Whose(System.Func memberSelector, System.Action> expectations, [System.Runtime.CompilerServices.CallerArgumentExpression("memberSelector")] string doNotPopulateThisValue = "") { } diff --git a/Tests/aweXpect.Core.Api.Tests/Expected/aweXpect.Core_netstandard2.0.txt b/Tests/aweXpect.Core.Api.Tests/Expected/aweXpect.Core_netstandard2.0.txt index 99f7b6ddf..af0fb21ba 100644 --- a/Tests/aweXpect.Core.Api.Tests/Expected/aweXpect.Core_netstandard2.0.txt +++ b/Tests/aweXpect.Core.Api.Tests/Expected/aweXpect.Core_netstandard2.0.txt @@ -460,6 +460,12 @@ namespace aweXpect.Delegates public aweXpect.Delegates.ThatDelegateThrows ThrowsExactly() where TException : System.Exception { } public aweXpect.Delegates.ThatDelegateThrows ThrowsException() { } + public class ThrowsOption + { + public ThrowsOption() { } + public bool DoCheckThrow { get; set; } + public aweXpect.Options.TimeSpanEqualityOptions? ExecutionTimeOptions { get; set; } + } public sealed class WithValue : aweXpect.Delegates.ThatDelegate, aweXpect.Core.IExpectThat>, aweXpect.Core.IThat> { public WithValue(aweXpect.Core.ExpectationBuilder expectationBuilder) { } @@ -488,7 +494,9 @@ namespace aweXpect.Delegates public class ThatDelegateThrows : aweXpect.Results.ExpectationResult>, aweXpect.Core.IExpectThat, aweXpect.Core.IThatDelegateThrows, aweXpect.Core.IThat where TException : System.Exception? { + public ThatDelegateThrows(aweXpect.Core.ExpectationBuilder expectationBuilder, aweXpect.Delegates.ThatDelegate.ThrowsOption throwOptions) { } public aweXpect.Core.ExpectationBuilder ExpectationBuilder { get; } + public aweXpect.Delegates.ThatDelegate.ThrowsOption ThrowOptions { get; } public aweXpect.Core.IThat Which { get; } public aweXpect.Delegates.ThatDelegateThrows OnlyIf(bool predicate) { } public aweXpect.Results.AndOrResult> Whose(System.Func memberSelector, System.Action> expectations, [System.Runtime.CompilerServices.CallerArgumentExpression("memberSelector")] string doNotPopulateThisValue = "") { }