Exemplo n.º 1
0
        internal static string MakeState(ITestOperationState state)
        {
            var builder = new StateStringBuilder();

            state.BuildDescription(builder);
            return(builder.ToString());
        }
Exemplo n.º 2
0
            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));
            }
Exemplo n.º 3
0
        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);
                }
            }
        }
Exemplo n.º 4
0
        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;
 }
Exemplo n.º 6
0
 internal void AddUntilResponder <T>(
     string respondToDescription,
     ITestOperationState <IMultipleTaskSource <ITestOperationState <T> > > responder,
     string untilDescription,
     ITestOperationState condition)
 => this
 .AddOptional(untilDescription, condition)
 .AddOptional(respondToDescription, responder);
Exemplo n.º 7
0
 private static IEnumerable <string> FailureLines(
     ITestOperationState rootOperation,
     string what)
 => new[]
 {
     $"Test operation execution {what}!",
     $"Failure context:\n{StateStringBuilder.MakeState(rootOperation)}",
 };
Exemplo n.º 8
0
 private string MakeErrorMessage(
     ITestOperationState rootOperation,
     Exception exception)
 => string.Join(
     UnityEmptyLine,
     FailureLines(rootOperation, "failed")
     .Concat(this.MakeGlobalContext())
     .Append($"Error: {exception}"));
Exemplo n.º 9
0
 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());
 }
Exemplo n.º 11
0
 internal void AddSelect <T1, T2>(
     ITestOperationState primaryState,
     TestOperationStatus selectStatus)
 {
     primaryState.BuildDescription(this);
     this.AddStatus(
         selectStatus,
         $"SELECT {typeof(T1).Name} -> {typeof(T2).Name}");
 }
Exemplo n.º 12
0
 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));
        }
Exemplo n.º 17
0
 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());
 }
Exemplo n.º 18
0
 public State(
     ITestResponder <T1> responder,
     Func <T1, T2> selector,
     SourceContext sourceContext)
     : base(sourceContext)
 {
     this.responder     = responder.CreateState();
     this.selector      = selector;
     this.sourceContext = sourceContext;
 }
Exemplo n.º 19
0
 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;
 }
Exemplo n.º 20
0
 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;
 }
Exemplo n.º 21
0
            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);
            }
Exemplo n.º 22
0
 /// <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);
Exemplo n.º 24
0
 internal void AddContinuation(
     ITestOperationState first,
     [CanBeNull] ITestOperationState second)
 {
     first.BuildDescription(this);
     if (second != null)
     {
         second.BuildDescription(this);
     }
     else
     {
         this.AddStatus(TestOperationStatus.NotExecuted.Instance, "...");
     }
 }
Exemplo n.º 25
0
 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);
Exemplo n.º 26
0
        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));
                }
            }
        }
Exemplo n.º 27
0
        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;
            }
        }
Exemplo n.º 28
0
        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));
        }
Exemplo n.º 29
0
 private string MakeTimeoutMessage(ITestOperationState rootOperation)
 => string.Join(
     UnityEmptyLine,
     FailureLines(rootOperation, "timed out")
     .Concat(this.MakeGlobalContext()));
Exemplo n.º 30
0
 public State(ITestWaitCondition <T> condition, TimeSpan timeout, SourceContext sourceContext)
     : base(sourceContext)
 {
     this.condition = condition.CreateState();
     this.timeout   = timeout;
 }