internal static string MakeState(ITestOperationState state) { var builder = new StateStringBuilder(); state.BuildDescription(builder); return(builder.ToString()); }
protected override async Task <T2> ExecuteInner(RunContext runContext, CancellationToken cancellationToken) { var firstResult = await this.first.Execute(runContext, cancellationToken); this.nextInstruction = this.selector(firstResult).CreateState(); return(await this.nextInstruction.Execute(runContext, cancellationToken)); }
private void AddResponder( string description, ITestOperationState primaryState, ITestOperationState wait, [CanBeNull] ITestOperationState instruction) { var status = primaryState.Status; var statusLine = status.MakeStatusLine(description); if (status is TestOperationStatus.Completed || status is TestOperationStatus.NotExecuted) { this.Add(statusLine); } else { this.AddIndented(statusLine, b => b .AddOptional("WAIT FOR", wait) .AddOptional("THEN RESPOND WITH", instruction)); // Add primary state failure details, if we didn't have failure details yet var failureIncluded = wait.Status is TestOperationStatus.Failed || instruction?.Status is TestOperationStatus.Failed; if (!failureIncluded) { this.AddFailureDetails(status); } } }
internal void AddExpectWithin( ITestOperationState expectOperation, TimeSpan timeout, ITestOperationState operation) { var timeoutString = $"{timeout.TotalSeconds:0.00 s}"; if (operation is IBoxedOperationState boxedState) { operation = boxedState.WrappedState; } if (operation is IDiscreteWaitConditionState discreteSate) { this.AddStatus( expectOperation.Status, $"{discreteSate.Description} EXPECTED WITHIN {timeoutString}", discreteSate.ExtraContext); } else if (operation is IBasicResponderState responderState) { this.AddResponder( $"{responderState.Description} CONDITION EXPECTED WITHIN {timeoutString}", expectOperation, responderState.WaitState, responderState.InstructionState); } else { this.AddParentWithChildren( $"EXPECT WITHIN {timeoutString}", expectOperation, new[] { operation }); } }
public SelectOperationState(ITestOperationState <T1> first, Func <T1, T2> selector, SourceContext sourceContext) : base(sourceContext) { this.first = first; this.selector = selector; this.sourceContext = sourceContext; }
internal void AddUntilResponder <T>( string respondToDescription, ITestOperationState <IMultipleTaskSource <ITestOperationState <T> > > responder, string untilDescription, ITestOperationState condition) => this .AddOptional(untilDescription, condition) .AddOptional(respondToDescription, responder);
private static IEnumerable <string> FailureLines( ITestOperationState rootOperation, string what) => new[] { $"Test operation execution {what}!", $"Failure context:\n{StateStringBuilder.MakeState(rootOperation)}", };
private string MakeErrorMessage( ITestOperationState rootOperation, Exception exception) => string.Join( UnityEmptyLine, FailureLines(rootOperation, "failed") .Concat(this.MakeGlobalContext()) .Append($"Error: {exception}"));
public State( ITestInstruction <T1> first, Func <T1, ITestInstruction <T2> > selector, SourceContext sourceContext) : base(sourceContext) { this.first = first.CreateState(); this.selector = selector; }
public State( ITestInstruction <T1> first, Func <T1, ITestInstruction <T2> > selector, SourceContext sourceContext) : base(sourceContext) { this.first = first.CreateState(); this.continuation = new Continuation <T1, T2>(firstResult => selector(firstResult).CreateState()); }
internal void AddSelect <T1, T2>( ITestOperationState primaryState, TestOperationStatus selectStatus) { primaryState.BuildDescription(this); this.AddStatus( selectStatus, $"SELECT {typeof(T1).Name} -> {typeof(T2).Name}"); }
public State( ITestWaitCondition <TFirst> first, ITestWaitCondition <TSecond> second, SourceContext sourceContext) : base(sourceContext) { this.first = first.CreateState(); this.second = second.CreateState(); }
public State( ITestInstruction <T1> first, ITestInstruction <T2> second, SourceContext sourceContext) : base(sourceContext) { this.first = first.CreateState(); this.second = second.CreateState(); }
protected override async Task <TSecond> ExecuteInner( RunContext runContext, CancellationToken cancellationToken) { var firstResult = await this.first.Execute(runContext, cancellationToken); this.second = this.continuation(firstResult).CreateState(); return(await this.second.Execute(runContext, cancellationToken)); }
public State( ITestWaitCondition <TFirst> first, Func <TFirst, ITestWaitCondition <TSecond> > continuation, SourceContext sourceContext) : base(sourceContext) { this.first = first.CreateState(); this.continuation = continuation; }
protected override async Task <T2> ExecuteInner(RunContext runContext, CancellationToken cancellationToken) { var result = await this.first.Execute(runContext, cancellationToken); this.selectState = new SynchronousTestInstruction <T2>( null, // This should never be used () => this.selector(result), this.sourceContext).CreateState(); return(await this.selectState.Execute(runContext, cancellationToken)); }
public State( ITestWaitCondition <TFirst> first, Func <TFirst, ITestWaitCondition <TSecond> > continuation, SourceContext sourceContext) : base(sourceContext) { this.first = first.CreateState(); this.continuation = new Continuation <TFirst, TSecond>( firstResult => continuation(firstResult).CreateState()); }
public State( ITestResponder <T1> responder, Func <T1, T2> selector, SourceContext sourceContext) : base(sourceContext) { this.responder = responder.CreateState(); this.selector = selector; this.sourceContext = sourceContext; }
public State( string description, ITestWaitCondition <TArg> waitCondition, Func <TArg, ITestInstruction <T> > makeInstruction, SourceContext sourceContext) : base(sourceContext) { this.Description = description; this.waitCondition = waitCondition.CreateState(); this.makeInstruction = makeInstruction; }
public UntilResponderState( string untilDescription, ITestOperationState <IMultipleTaskSource <ITestOperationState <object> > > respondTo, ITestOperationState <T> until, SourceContext sourceContext) : base(sourceContext) { this.untilDescription = untilDescription; this.respondTo = respondTo; this.until = until; }
protected override async Task <ITestOperationState <T> > ExecuteInner( RunContext runContext, CancellationToken cancellationToken) { var waitResult = await this.waitCondition.Execute(runContext, cancellationToken); var instruction = this.makeInstruction(waitResult).CreateState(); this.InstructionState = instruction; return(instruction); }
/// <summary> /// Starts executing the explicitly created operation state. /// This can be used for e.g. logging the state periodically. /// </summary> /// <remarks> /// Be careful not to call this twice on the same state object: the consequences are undefined. /// </remarks> /// <returns> /// An task which will complete with the value of the test operation, once it has completed, /// or an error if the operation fails. /// </returns> /// <param name="state">Test operation state to start executing.</param> /// <typeparam name="T">Result type of the test operation.</typeparam> /// <inheritdoc cref="Docs.Inherit.CallerMemberWithExecutor{T}"/> public static Task <T> ToTask <T>( this ITestOperationState <T> state, TestInstructionExecutor executor, CancellationToken cancellationToken = default, [CallerMemberName] string memberName = "", [CallerFilePath] string sourceFilePath = "", [CallerLineNumber] int sourceLineNumber = 0) => executor.RunInstruction( state, new SourceContext(nameof(ToTask), memberName, sourceFilePath, sourceLineNumber), cancellationToken);
/// <summary> /// **Unity-only!** /// /// Start executing an explicitly created operation state. /// This can be used for e.g. logging the state periodically. /// </summary> /// <remarks> /// Be careful not to call this twice on the same state object: the consequences are undefined. /// </remarks> /// <returns> /// A yield instruction which will complete with the value of the test operation, once it has completed, /// or publish an error if the operation fails. /// </returns> /// <param name="state">Test operation state to start executing.</param> /// <typeparam name="T">Result type of the test operation.</typeparam> /// <inheritdoc cref="Docs.Inherit.YieldInstruction{T}"/> public static TestOperationYieldInstruction <T> ToYieldInstruction <T>( this ITestOperationState <T> state, TestInstructionExecutor executor, bool throwOnError = true, CancellationToken cancellationToken = default, [CallerMemberName] string memberName = "", [CallerFilePath] string sourceFilePath = "", [CallerLineNumber] int sourceLineNumber = 0) => new TestOperationYieldInstruction <T>(executor.RunInstruction( state, new SourceContext(nameof(ToYieldInstruction), memberName, sourceFilePath, sourceLineNumber), cancellationToken), throwOnError);
internal void AddContinuation( ITestOperationState first, [CanBeNull] ITestOperationState second) { first.BuildDescription(this); if (second != null) { second.BuildDescription(this); } else { this.AddStatus(TestOperationStatus.NotExecuted.Instance, "..."); } }
internal StateStringBuilder AddParentWithChildren( string parentDescription, ITestOperationState parentState, IEnumerable <ITestOperationState> children) => this .AddIndented( parentState.Status.MakeStatusLine(parentDescription), b => { foreach (var child in children) { child.BuildDescription(b); } }) .AddFailureDetails(parentState.Status);
internal async Task <T> RunInstruction <T>( ITestOperationState <T> rootState, SourceContext sourceContext, CancellationToken cancellationToken) { var runContext = new RunContext(sourceContext, this.scheduler); using (var linkedTokenSource = CancellationTokenSource.CreateLinkedTokenSource( cancellationToken, this.mainCancellationTokenSource.Token)) { try { NotificationCallbacks.ForEach(callback => callback(TestOperationStateTransition.Started, rootState)); if (this.externalResultSource != null) { return(await linkedTokenSource.Token.Amb( this.externalResultSource.GetExternalResult <T>, ct => rootState.Execute(runContext, ct))); } else { return(await rootState.Execute(runContext, linkedTokenSource.Token)); } } catch (Exception e) { if (this.ShouldRethrow(e)) { throw; } var message = e is TimeoutException ? this.MakeTimeoutMessage(rootState) : this.MakeErrorMessage(rootState, e); this.failureListener?.OperationFailed(e, message); throw new TestFailureException(message, e); } finally { NotificationCallbacks.ForEach(callback => callback(TestOperationStateTransition.Finished, rootState)); } } }
internal void AddContinuation( ITestOperationState first, ContinuationState continuation) { first.BuildDescription(this); switch (continuation) { case ContinuationState.Available available: available.State.BuildDescription(this); break; case ContinuationState.NotAvailable notAvailable: this.AddStatus(notAvailable.CreationStatus, "..."); break; } }
public async Task <TSecond> Execute( TFirst source, RunContext runContext, CancellationToken cancellationToken) { this.creationStatus = new TestOperationStatus.Waiting(this.creationStatus, runContext.MakeWaitContext()); try { this.executionState = this.makeContinuation(source); } catch (Exception e) { this.creationStatus = new TestOperationStatus.Failed(this.creationStatus, e, runContext.SourceContext); throw; } this.creationStatus = new TestOperationStatus.Completed(this.creationStatus); return(await this.executionState.Execute(runContext, cancellationToken)); }
private string MakeTimeoutMessage(ITestOperationState rootOperation) => string.Join( UnityEmptyLine, FailureLines(rootOperation, "timed out") .Concat(this.MakeGlobalContext()));
public State(ITestWaitCondition <T> condition, TimeSpan timeout, SourceContext sourceContext) : base(sourceContext) { this.condition = condition.CreateState(); this.timeout = timeout; }