Exemplo n.º 1
0
        public ValidatorFixtureResult Run()
        {
            var validatorFixtureResult = new ValidatorFixtureResult();

            validatorFixtureResult.Name                   = _validatorFixtureInfo.GetCustomAttribute <ValidatorFixtureAttribute>().Name;
            validatorFixtureResult.ShouldReport           = !NoReportingAttribute.HasAttribute(_validatorFixtureInfo);
            validatorFixtureResult.SummaryAtTemplateLevel = new ResultSummary();
            validatorFixtureResult.SummaryAtInstanceLevel = new ResultSummary();

            if (SkipAttribute.HasAttribute(_validatorFixtureInfo))
            {
                validatorFixtureResult.Status = GroupStatus.Skipped;
                return(validatorFixtureResult);
            }

            validatorFixtureResult.TemplateResults = new List <ValidatorTemplateResult>();

            try
            {
                foreach (var validatorTemplateInfo in _validatorFixtureInfo.GetMethods().Where(ValidatorTemplateAttribute.HasAttribute))
                {
                    var templateRunner          = new TemplateRunner(_environ, _validatorFixtureInfo, validatorTemplateInfo);
                    var validatorTemplateResult = templateRunner.Run();
                    validatorFixtureResult.AddTemplateResult(validatorTemplateResult);
                }
            }
            catch (Exception e)
            {
                validatorFixtureResult.Error           = e;
                validatorFixtureResult.TemplateResults = null;
            }

            if (validatorFixtureResult.Error != null)
            {
                validatorFixtureResult.Status = GroupStatus.Error;
            }
            else if (validatorFixtureResult.SummaryAtTemplateLevel.Failure > 0)
            {
                validatorFixtureResult.Status = GroupStatus.Failure;
            }
            else
            {
                validatorFixtureResult.Status = GroupStatus.Success;
            }

            return(validatorFixtureResult);
        }
Exemplo n.º 2
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);
        }
Exemplo n.º 3
0
 public void HasAttributeForFunction()
 {
     Assert.That(SkipAttribute.HasAttribute(typeof(SkipAttributeTest).GetMethod("FunctionWithAttribute")), Is.True);
     Assert.That(SkipAttribute.HasAttribute(typeof(SkipAttributeTest).GetMethod("FunctionWithoutAttribute")), Is.False);
 }
Exemplo n.º 4
0
 public void HasAttributeForClass()
 {
     Assert.That(SkipAttribute.HasAttribute(typeof(ClassWithAttribute).GetTypeInfo()), Is.True);
     Assert.That(SkipAttribute.HasAttribute(typeof(ClassWithoutAttribute).GetTypeInfo()), Is.False);
 }