示例#1
0
        public ValidatorTemplateResult Run()
        {
            var validatorTemplateResult = new ValidatorTemplateResult();

            validatorTemplateResult.Name                   = _validatorTemplateInfo.Name;
            validatorTemplateResult.ShouldReport           = !NoReportingAttribute.HasAttribute(_validatorFixtureInfo) || !NoReportingAttribute.HasAttribute(_validatorTemplateInfo);
            validatorTemplateResult.SummaryAtInstanceLevel = new ResultSummary();

            if (SkipAttribute.HasAttribute(_validatorTemplateInfo))
            {
                validatorTemplateResult.Status = GroupStatus.Skipped;
                return(validatorTemplateResult);
            }

            validatorTemplateResult.InstanceResults = new List <ValidatorInstanceResult>();

            var validatorFixture = _validatorFixtureInfo.GetConstructor(Type.EmptyTypes).Invoke(new object[0]);

            ProjectorAttribute            projector = _validatorTemplateInfo.GetCustomAttribute <ProjectorAttribute>() ?? _s_defaultProjector;
            IEnumerable <FilterAttribute> filters   = _validatorTemplateInfo.GetCustomAttributes <FilterAttribute>();
            HashSet <object> exceptionsSet          = _validatorTemplateInfo.GetCustomAttribute <ExceptionsAttribute>()?.GetExceptionsSet();

            CancellationTokenSource cts = new CancellationTokenSource();

            var environ = _environ
                          .Extend(cts)
                          .Extend((Action <Environ>)(env =>
            {
                try
                {
                    if (!filters.All(fa => fa.IsAllowed(env)))
                    {
                        return;
                    }

                    if (exceptionsSet != null && projector.IsException(exceptionsSet, env))
                    {
                        return;
                    }

                    var checkRecorder = new RunnerCheckRecorder(validatorTemplateResult.ShouldRecord);

                    try
                    {
                        _validatorTemplateInfo.Invoke(validatorFixture, env.Extend <CheckRecorder>(checkRecorder).ResolveParameters(_validatorTemplateInfo.GetParameters()));

                        if (checkRecorder.IsSuccess)
                        {
                            validatorTemplateResult.SummaryAtInstanceLevel.AddSuccess();
                        }
                        else if (checkRecorder.IsFailure)
                        {
                            validatorTemplateResult.SummaryAtInstanceLevel.AddFailure();
                            // Prevent allocation if we aren't going to explicitly record this.
                            if (!validatorTemplateResult.ShouldRecord)
                            {
                                return;
                            }
                            validatorTemplateResult.AddInstanceResult(new ValidatorInstanceResult()
                            {
                                Name         = projector.GetName(env),
                                Status       = GroupStatus.Failure,
                                CheckResults = checkRecorder.Results,
                                Error        = null
                            });
                        }
                    }
                    catch (TargetInvocationException e) when(e.InnerException is FailedToValidateCriticalCheckException)
                    {
                        validatorTemplateResult.SummaryAtInstanceLevel.AddFailure();
                        validatorTemplateResult.AddInstanceResult(new ValidatorInstanceResult()
                        {
                            Name         = projector.GetName(env),
                            ShouldReport = validatorTemplateResult.ShouldReport,
                            Status       = GroupStatus.Failure,
                            CheckResults = checkRecorder.Results,
                            Error        = null
                        });
                    }
                    catch (TargetInvocationException e)
                    {
                        validatorTemplateResult.SummaryAtInstanceLevel.AddError();
                        validatorTemplateResult.AddInstanceResult(new ValidatorInstanceResult()
                        {
                            Name         = projector.GetName(env),
                            ShouldReport = validatorTemplateResult.ShouldReport,
                            Status       = GroupStatus.Error,
                            CheckResults = null,
                            Error        = e.InnerException
                        });
                    }
                }
                catch (Exception e)
                {
                    validatorTemplateResult.SetError(e);
                    validatorTemplateResult.InstanceResults = null;
                    cts.Cancel();
                }
            }));

            environ = environ.Extend(environ); // This is the base environment. It should be accessible downstream.

            try
            {
                projector.Project(environ);
            }
            catch (TargetInvocationException e) when(e.InnerException is OperationCanceledException)
            {
                // There was an error with executing an instance's code. The possibly parallel
                // execution of instance validators was stopped. Nothing to do here, as the error
                // is already saved globally.
            }

            if (validatorTemplateResult.InstanceResults?.Count == 0 && validatorTemplateResult.Error == null)
            {
                validatorTemplateResult.Status = GroupStatus.Success;
            }
            else if (validatorTemplateResult.Error != null)
            {
                validatorTemplateResult.Status = GroupStatus.Error;
            }
            else
            {
                validatorTemplateResult.Status = GroupStatus.Failure;
            }

            return(validatorTemplateResult);
        }
示例#2
0
 public bool IsException(HashSet <object> exceptionsSet, Environ environ)
 {
     return((bool)_isExceptionMethodInfo.Invoke(_projectorObject, environ.Extend(exceptionsSet).ResolveParameters(_isExceptionMethodInfo.GetParameters())));
 }