Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 23 additions & 10 deletions Source/aweXpect.Core/Core/ExpectationBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ public abstract class ExpectationBuilder
{
private const string DefaultCurrentSubject = "it";

private CancellationToken? _cancellationToken;

private ResultContexts? _contexts;

/// <summary>
Expand All @@ -31,7 +29,6 @@ public abstract class ExpectationBuilder
private string _it = DefaultCurrentSubject;

private Node _node = new ExpectationNode();
private TimeSpan? _timeout;

private ITimeSystem? _timeSystem;

Expand Down Expand Up @@ -59,6 +56,20 @@ private protected ExpectationBuilder()
ExpectationGrammars = ExpectationGrammars.None;
}

/// <summary>
/// The explicit cancellation token to be used for the expectation.
/// </summary>
Comment thread
vbreuss marked this conversation as resolved.
/// <remarks>
/// When not set, the expectation will still use the cancellation token from
/// <see cref="AwexpectCustomization.SettingsCustomization.TestCancellation" />
/// </remarks>
public CancellationToken? CancellationToken { get; private set; }

/// <summary>
/// The explicit timeout to be applied to the expectation.
/// </summary>
public TimeSpan? Timeout { get; private set; }

/// <summary>
/// The expected grammatical form of the expectation text.
/// </summary>
Expand Down Expand Up @@ -278,13 +289,13 @@ public MemberExpectationBuilder<TSource, TTarget> ForAsyncMember<TSource, TTarge
/// Adds a <paramref name="cancellationToken" /> to be used by the constraints.
/// </summary>
public void WithCancellation(CancellationToken cancellationToken)
=> _cancellationToken = cancellationToken;
=> CancellationToken = cancellationToken;

/// <summary>
/// Adds a <paramref name="timeout" /> to be used by the constraints.
/// </summary>
public void WithTimeout(TimeSpan timeout)
=> _timeout = timeout;
=> Timeout = timeout;

/// <summary>
/// Adds a <paramref name="reason" /> to the current expectation constraint.
Expand Down Expand Up @@ -388,7 +399,7 @@ public virtual ExpectationBuilder UpdateContexts(Action<ResultContexts> callback
}

/// <summary>
/// Adds the <paramref name="resultContext"/> to the context that is included in the failure message.
/// Adds the <paramref name="resultContext" /> to the context that is included in the failure message.
/// </summary>
public virtual ExpectationBuilder AddContext(ResultContext resultContext)
{
Expand All @@ -406,7 +417,7 @@ public virtual ExpectationBuilder AddContext(ResultContext resultContext)
/// Creates the exception message from the <paramref name="failure" />.
/// </summary>
internal Task<string> FromFailure(ConstraintResult failure)
=> FromFailure(Subject, failure, _contexts, _cancellationToken ?? CancellationToken.None);
=> FromFailure(Subject, failure, _contexts, CancellationToken ?? System.Threading.CancellationToken.None);

/// <summary>
/// Creates the exception message from the <paramref name="failure" />.
Expand Down Expand Up @@ -463,9 +474,11 @@ internal Task<ConstraintResult> IsMet()
EvaluationContext.EvaluationContext context = new();
ITimeSystem timeSystem = _timeSystem ?? RealTimeSystem.Instance;
TestCancellation? testCancellation = Customize.aweXpect.Settings().TestCancellation.Get();
_cancellationToken ??= testCancellation?.CancellationTokenFactory?.Invoke() ?? CancellationToken.None;
return IsMet(GetRootNode(), context, timeSystem, _timeout ?? testCancellation?.Timeout,
_cancellationToken.Value);
CancellationToken cancellationToken = CancellationToken ??
testCancellation?.CancellationTokenFactory?.Invoke() ??
System.Threading.CancellationToken.None;
return IsMet(GetRootNode(), context, timeSystem, Timeout ?? testCancellation?.Timeout,
cancellationToken);
}

internal abstract Task<ConstraintResult> IsMet(Node rootNode,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,9 @@ namespace aweXpect.Core
public abstract class ExpectationBuilder
{
protected ExpectationBuilder(string subjectExpression, aweXpect.Core.ExpectationGrammars grammars = 0) { }
public System.Threading.CancellationToken? CancellationToken { get; }
public aweXpect.Core.ExpectationGrammars ExpectationGrammars { get; }
public System.TimeSpan? Timeout { get; }
public aweXpect.Core.ExpectationBuilder AddConstraint<TValue>(System.Func<string, aweXpect.Core.ExpectationGrammars, aweXpect.Core.Constraints.IAsyncConstraint<TValue>> constraintBuilder) { }
public aweXpect.Core.ExpectationBuilder AddConstraint<TValue>(System.Func<string, aweXpect.Core.ExpectationGrammars, aweXpect.Core.Constraints.IAsyncContextConstraint<TValue>> constraintBuilder) { }
public aweXpect.Core.ExpectationBuilder AddConstraint<TValue>(System.Func<string, aweXpect.Core.ExpectationGrammars, aweXpect.Core.Constraints.IContextConstraint<TValue>> constraintBuilder) { }
Expand Down Expand Up @@ -1179,6 +1181,7 @@ namespace aweXpect.Results
public class ExpectationResult : aweXpect.Results.Expectation, aweXpect.Core.IOptionsProvider<aweXpect.Core.ExpectationBuilder>
{
public ExpectationResult(aweXpect.Core.ExpectationBuilder expectationBuilder) { }
public aweXpect.Results.ExpectationResult Because(System.Threading.Tasks.Task<string> reason) { }
public aweXpect.Results.ExpectationResult Because(string reason) { }
public System.Runtime.CompilerServices.TaskAwaiter GetAwaiter() { }
public override string? ToString() { }
Expand All @@ -1194,6 +1197,7 @@ namespace aweXpect.Results
where TSelf : aweXpect.Results.ExpectationResult<TType, TSelf>
{
public ExpectationResult(aweXpect.Core.ExpectationBuilder expectationBuilder) { }
public TSelf Because(System.Threading.Tasks.Task<string> reason) { }
public TSelf Because(string reason) { }
[System.Diagnostics.StackTraceHidden]
public System.Runtime.CompilerServices.TaskAwaiter<TType> GetAwaiter() { }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,9 @@ namespace aweXpect.Core
public abstract class ExpectationBuilder
{
protected ExpectationBuilder(string subjectExpression, aweXpect.Core.ExpectationGrammars grammars = 0) { }
public System.Threading.CancellationToken? CancellationToken { get; }
public aweXpect.Core.ExpectationGrammars ExpectationGrammars { get; }
public System.TimeSpan? Timeout { get; }
public aweXpect.Core.ExpectationBuilder AddConstraint<TValue>(System.Func<string, aweXpect.Core.ExpectationGrammars, aweXpect.Core.Constraints.IAsyncConstraint<TValue>> constraintBuilder) { }
public aweXpect.Core.ExpectationBuilder AddConstraint<TValue>(System.Func<string, aweXpect.Core.ExpectationGrammars, aweXpect.Core.Constraints.IAsyncContextConstraint<TValue>> constraintBuilder) { }
public aweXpect.Core.ExpectationBuilder AddConstraint<TValue>(System.Func<string, aweXpect.Core.ExpectationGrammars, aweXpect.Core.Constraints.IContextConstraint<TValue>> constraintBuilder) { }
Expand Down Expand Up @@ -1179,6 +1181,7 @@ namespace aweXpect.Results
public class ExpectationResult : aweXpect.Results.Expectation, aweXpect.Core.IOptionsProvider<aweXpect.Core.ExpectationBuilder>
{
public ExpectationResult(aweXpect.Core.ExpectationBuilder expectationBuilder) { }
public aweXpect.Results.ExpectationResult Because(System.Threading.Tasks.Task<string> reason) { }
public aweXpect.Results.ExpectationResult Because(string reason) { }
Comment thread
vbreuss marked this conversation as resolved.
public System.Runtime.CompilerServices.TaskAwaiter GetAwaiter() { }
public override string? ToString() { }
Expand All @@ -1194,6 +1197,7 @@ namespace aweXpect.Results
where TSelf : aweXpect.Results.ExpectationResult<TType, TSelf>
{
public ExpectationResult(aweXpect.Core.ExpectationBuilder expectationBuilder) { }
public TSelf Because(System.Threading.Tasks.Task<string> reason) { }
public TSelf Because(string reason) { }
[System.Diagnostics.StackTraceHidden]
public System.Runtime.CompilerServices.TaskAwaiter<TType> GetAwaiter() { }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,9 @@ namespace aweXpect.Core
public abstract class ExpectationBuilder
{
protected ExpectationBuilder(string subjectExpression, aweXpect.Core.ExpectationGrammars grammars = 0) { }
public System.Threading.CancellationToken? CancellationToken { get; }
public aweXpect.Core.ExpectationGrammars ExpectationGrammars { get; }
public System.TimeSpan? Timeout { get; }
public aweXpect.Core.ExpectationBuilder AddConstraint<TValue>(System.Func<string, aweXpect.Core.ExpectationGrammars, aweXpect.Core.Constraints.IAsyncConstraint<TValue>> constraintBuilder) { }
public aweXpect.Core.ExpectationBuilder AddConstraint<TValue>(System.Func<string, aweXpect.Core.ExpectationGrammars, aweXpect.Core.Constraints.IAsyncContextConstraint<TValue>> constraintBuilder) { }
public aweXpect.Core.ExpectationBuilder AddConstraint<TValue>(System.Func<string, aweXpect.Core.ExpectationGrammars, aweXpect.Core.Constraints.IContextConstraint<TValue>> constraintBuilder) { }
Expand Down Expand Up @@ -1142,6 +1144,7 @@ namespace aweXpect.Results
public class ExpectationResult : aweXpect.Results.Expectation, aweXpect.Core.IOptionsProvider<aweXpect.Core.ExpectationBuilder>
{
public ExpectationResult(aweXpect.Core.ExpectationBuilder expectationBuilder) { }
public aweXpect.Results.ExpectationResult Because(System.Threading.Tasks.Task<string> reason) { }
public aweXpect.Results.ExpectationResult Because(string reason) { }
public System.Runtime.CompilerServices.TaskAwaiter GetAwaiter() { }
public override string? ToString() { }
Expand All @@ -1156,6 +1159,7 @@ namespace aweXpect.Results
where TSelf : aweXpect.Results.ExpectationResult<TType, TSelf>
{
public ExpectationResult(aweXpect.Core.ExpectationBuilder expectationBuilder) { }
public TSelf Because(System.Threading.Tasks.Task<string> reason) { }
public TSelf Because(string reason) { }
public System.Runtime.CompilerServices.TaskAwaiter<TType> GetAwaiter() { }
public override string? ToString() { }
Expand Down