Esempio n. 1
0
            public void ToModelReport_Should_Translate()
            {
                var draft = new ErrorsCollection();

                draft.AddError(new Error("foo"));
                draft.AddError(new Error("bar"));

                var innerDraft = new ErrorsCollection();

                innerDraft.AddError("inner", new Error("test123"));
                innerDraft.AddError("inner", new Error("test321"));

                draft.AddError("test", innerDraft);

                var report = ResultsTestHelpers.MockValidationResult(draft, translations: new[]
                {
                    new Translation("test", new Dictionary <string, string> {
                        { "foo", "FOO" }, { "test123", "TEST123" }
                    }),
                    new Translation("test1", new Dictionary <string, string> {
                        { "bar", "BAR" }, { "test321", "TEST321" }
                    })
                }).ToModelReport("test") as ModelReport;

                ExpectMembersInReport(report, new[] { string.Empty, "test" });

                // ReSharper disable once PossibleNullReferenceException
                ExpectMessagesInList(report[string.Empty] as ModelReportErrorsList, new[] { "FOO", "bar" });

                ExpectMembersInReport(report["test"] as ModelReport, new[] { "inner" });

                // ReSharper disable once PossibleNullReferenceException
                ExpectMessagesInList((report["test"] as ModelReport)["inner"] as ModelReportErrorsList, new[] { "TEST123", "test321" });
            }
Esempio n. 2
0
            public void ToListReport_Should_ThrowException_When_InvalidTranslationName()
            {
                var errorsCollection = new ErrorsCollection();

                var nestedErrorsCollection = new ErrorsCollection();

                nestedErrorsCollection.AddError(new Error("test123"));
                nestedErrorsCollection.AddError(new Error("test321"));
                nestedErrorsCollection.AddError("inner", new Error("foo"));
                nestedErrorsCollection.AddError("inner", new Error("bar"));

                errorsCollection.AddError("test", nestedErrorsCollection);

                var report = ResultsTestHelpers.MockValidationResult(errorsCollection, translations: new[]
                {
                    new Translation("test", new Dictionary <string, string> {
                        { "foo", "FOO" }, { "test123", "TEST123" }
                    }),
                    new Translation("test1", new Dictionary <string, string> {
                        { "bar", "BAR" }, { "test321", "TEST321" }
                    })
                });

                Assert.Throws <TranslationNotFoundException>(() => { report.ToListReport("test_non_existing"); });
            }
Esempio n. 3
0
        public void ToModelReport_Should_Generate_RootMessages_And_MemberMessages_WithoutDuplicates()
        {
            var draft = new ErrorsCollection();

            draft.AddError(new Error("foo"));
            draft.AddError(new Error("bar"));
            draft.AddError(new Error("bar"));
            draft.AddError(new Error("foo"));

            var innerDraft = new ErrorsCollection();

            innerDraft.AddError("inner", new Error("test123"));
            innerDraft.AddError("inner", new Error("test321"));
            innerDraft.AddError("inner", new Error("test321"));
            innerDraft.AddError("inner", new Error("test123"));

            draft.AddError("test", innerDraft);

            var report = ResultsTestHelpers.MockValidationResult(draft).ToModelReport() as ModelReport;

            ExpectMembersInReport(report, new[] { string.Empty, "test" });

            // ReSharper disable once PossibleNullReferenceException
            ExpectMessagesInList(report[string.Empty] as ModelReportErrorsList, new[] { "foo", "bar" });

            ExpectMembersInReport(report["test"] as ModelReport, new[] { "inner" });

            // ReSharper disable once PossibleNullReferenceException
            ExpectMessagesInList((report["test"] as ModelReport)["inner"] as ModelReportErrorsList, new[] { "test123", "test321" });
        }
Esempio n. 4
0
            public void ToListReport_Should_Translate()
            {
                var errorsCollection = new ErrorsCollection();

                var nestedErrorsCollection = new ErrorsCollection();

                nestedErrorsCollection.AddError(new Error("test123"));
                nestedErrorsCollection.AddError(new Error("test321"));
                nestedErrorsCollection.AddError("inner", new Error("foo"));
                nestedErrorsCollection.AddError("inner", new Error("bar"));

                errorsCollection.AddError("test", nestedErrorsCollection);

                var report = ResultsTestHelpers.MockValidationResult(errorsCollection, translations: new[]
                {
                    new Translation("test", new Dictionary <string, string> {
                        { "foo", "FOO" }, { "test123", "TEST123" }
                    }),
                    new Translation("test1", new Dictionary <string, string> {
                        { "bar", "BAR" }, { "test321", "TEST321" }
                    })
                }).ToListReport("test");

                ExpectMembersInReport(report, "", new[] { "test" });

                ExpectMessagesInReport(report, "test", new[] { "TEST123", "test321" });

                ExpectMembersInReport(report, "test", new[] { "inner" });

                ExpectMessagesInReport(report, "test.inner", new[] { "FOO", "bar" });
            }
Esempio n. 5
0
        public void AddError_Should_AddInnerCollection_When_Multiple_To_SameMember_WithDuplicates()
        {
            var errorsCollection = new ErrorsCollection();

            var innerCollection = new ErrorsCollection();

            innerCollection.AddError("inner", new Error("test321"));
            innerCollection.AddError("inner", new Error("foo"));
            innerCollection.AddError("inner", new Error("bar"));

            var innerCollection2 = new ErrorsCollection();

            innerCollection2.AddError("inner", new Error("test123"));
            innerCollection2.AddError("inner", new Error("test321"));
            innerCollection2.AddError("inner", new Error("foo"));

            errorsCollection.AddError("test", innerCollection);
            errorsCollection.AddError("test", innerCollection2);

            ErrorsCollectionTestsHelpers.ExpectMembers(errorsCollection, new[] { "test" });
            ErrorsCollectionTestsHelpers.ExpectMembers(errorsCollection.Members["test"], new[] { "inner" });
            ErrorsCollectionTestsHelpers.ExpectErrors(errorsCollection.Members["test"].Members["inner"], new[] { "test321", "foo", "bar", "test123", "test321", "foo" });

            Assert.False(errorsCollection.IsEmpty);
        }
Esempio n. 6
0
        public void Include_Should_Include_When_ContainsSingleLevel()
        {
            var errorsCollection = new ErrorsCollection();

            errorsCollection.AddError("foo", new Error("test1"));
            errorsCollection.AddError("foo", new Error("test2"));

            errorsCollection.AddError("bar", new Error("test3"));
            errorsCollection.AddError("bar", new Error("test4"));

            var another = new ErrorsCollection();

            another.AddError("foo", new Error("111"));
            another.AddError("foo", new Error("222"));
            another.AddError("bar", new Error("333"));
            another.AddError("bar", new Error("444"));
            another.AddError("X", new Error("555"));
            another.AddError("Y", new Error("666"));

            errorsCollection.Include(another);

            ErrorsCollectionTestsHelpers.ExpectMembers(errorsCollection, new[] { "foo", "bar", "X", "Y" });

            ErrorsCollectionTestsHelpers.ExpectErrors(errorsCollection.Members["foo"], new[] { "test1", "test2", "111", "222" });
            ErrorsCollectionTestsHelpers.ExpectErrors(errorsCollection.Members["bar"], new[] { "test3", "test4", "333", "444" });
            ErrorsCollectionTestsHelpers.ExpectErrors(errorsCollection.Members["X"], new[] { "555" });
            ErrorsCollectionTestsHelpers.ExpectErrors(errorsCollection.Members["Y"], new[] { "666" });
        }
Esempio n. 7
0
        public void Include_Should_Include_When_ContainsMultipleNestedLevels()
        {
            var nested11 = new ErrorsCollection();

            nested11.AddError("arg11", new Error("val111"));
            nested11.AddError("arg11", new Error("val112"));

            var nested12 = new ErrorsCollection();

            nested12.AddError("arg12", new Error("val121"));
            nested12.AddError("arg12", new Error("val122"));

            var nested1 = new ErrorsCollection();

            nested1.AddError("arg1", new Error("val11"));
            nested1.AddError("arg1", new Error("val12"));

            nested1.AddError("arg1", nested11);
            nested1.AddError("arg1", nested12);

            var errorsCollection = new ErrorsCollection();

            errorsCollection.AddError("foo", nested1);

            var anotherNested12 = new ErrorsCollection();

            anotherNested12.AddError("arg12", new Error("val122"));
            anotherNested12.AddError("arg12", new Error("val123"));

            var anotherNested13 = new ErrorsCollection();

            anotherNested13.AddError("arg13", new Error("val131"));
            anotherNested13.AddError("arg13", new Error("val132"));

            var anotherNested1 = new ErrorsCollection();

            anotherNested1.AddError("arg1", new Error("val12"));
            anotherNested1.AddError("arg1", new Error("val13"));
            anotherNested1.AddError("arg1", anotherNested12);
            anotherNested1.AddError("arg1", anotherNested13);

            var another = new ErrorsCollection();

            another.AddError("foo", anotherNested1);

            errorsCollection.Include(another);

            ErrorsCollectionTestsHelpers.ExpectMembers(errorsCollection, new[] { "foo" });

            ErrorsCollectionTestsHelpers.ExpectMembers(errorsCollection.Members["foo"], new[] { "arg1" });

            ErrorsCollectionTestsHelpers.ExpectErrors(errorsCollection.Members["foo"].Members["arg1"], new[] { "val11", "val12", "val12", "val13" });
            ErrorsCollectionTestsHelpers.ExpectMembers(errorsCollection.Members["foo"].Members["arg1"], new[] { "arg11", "arg12", "arg13" });

            ErrorsCollectionTestsHelpers.ExpectErrors(errorsCollection.Members["foo"].Members["arg1"].Members["arg11"], new[] { "val111", "val112" });
            ErrorsCollectionTestsHelpers.ExpectErrors(errorsCollection.Members["foo"].Members["arg1"].Members["arg12"], new[] { "val121", "val122", "val122", "val123" });
            ErrorsCollectionTestsHelpers.ExpectErrors(errorsCollection.Members["foo"].Members["arg1"].Members["arg13"], new[] { "val131", "val132" });
        }
Esempio n. 8
0
            public void GetSingleError_Should_ReturnFalse_When_ManyErrors()
            {
                var errorsCollection = new ErrorsCollection();

                errorsCollection.AddError(new Error("test123"));
                errorsCollection.AddError(new Error("test1234"));
                errorsCollection.AddError(new Error("test12345"));

                Assert.Throws <NoSingleErrorCollectionException>(() => { errorsCollection.GetSingleError(); });
            }
Esempio n. 9
0
            public void ContainsSingleError_Should_ReturnFalse_When_ManyErrors()
            {
                var errorsCollection = new ErrorsCollection();

                errorsCollection.AddError(new Error("test123"));
                errorsCollection.AddError(new Error("test1234"));
                errorsCollection.AddError(new Error("test12345"));

                Assert.False(errorsCollection.ContainsSingleError());
            }
Esempio n. 10
0
        public void AddError_Should_Add_WithDuplicates()
        {
            var errorsCollection = new ErrorsCollection();

            errorsCollection.AddError(new Error("test123"));
            errorsCollection.AddError(new Error("test123"));
            errorsCollection.AddError(new Error("foo"));
            errorsCollection.AddError(new Error("foo"));

            ErrorsCollectionTestsHelpers.ExpectErrors(errorsCollection, new[] { "test123", "test123", "foo", "foo" });

            Assert.False(errorsCollection.IsEmpty);
        }
Esempio n. 11
0
        public void AddError_Should_Add_When_MultipleErrors()
        {
            var errorsCollection = new ErrorsCollection();

            errorsCollection.AddError(new Error("test123"));
            errorsCollection.AddError(new Error("test321"));
            errorsCollection.AddError(new Error("foo"));
            errorsCollection.AddError(new Error("bar"));

            ErrorsCollectionTestsHelpers.ExpectErrors(errorsCollection, new[] { "test123", "test321", "foo", "bar" });

            Assert.False(errorsCollection.IsEmpty);
        }
Esempio n. 12
0
        public void ToModelReport_Should_Generate_RootMessages()
        {
            var draft = new ErrorsCollection();

            draft.AddError(new Error("test123"));
            draft.AddError(new Error("test321"));
            draft.AddError(new Error("foo"));
            draft.AddError(new Error("bar"));

            var report = ResultsTestHelpers.MockValidationResult(draft).ToModelReport() as ModelReportErrorsList;

            ExpectMessagesInList(report, new[] { "test123", "test321", "foo", "bar" });
        }
Esempio n. 13
0
        public void ToListReport_Should_Generate_RootMessages()
        {
            var errorsCollection = new ErrorsCollection();

            errorsCollection.AddError(new Error("test123"));
            errorsCollection.AddError(new Error("test321"));
            errorsCollection.AddError(new Error("foo"));
            errorsCollection.AddError(new Error("bar"));

            var report = ResultsTestHelpers.MockValidationResult(errorsCollection).ToListReport();

            ExpectMessagesInReport(report, "", new[] { "test123", "test321", "foo", "bar" });
        }
Esempio n. 14
0
        public void AddError_Should_AddToMember_WithDuplicates()
        {
            var errorsCollection = new ErrorsCollection();

            errorsCollection.AddError("test", new Error("test123"));
            errorsCollection.AddError("test", new Error("test123"));
            errorsCollection.AddError("test", new Error("test321"));
            errorsCollection.AddError("test", new Error("test321"));

            ErrorsCollectionTestsHelpers.ExpectMembers(errorsCollection, new[] { "test" });
            ErrorsCollectionTestsHelpers.ExpectErrors(errorsCollection.Members["test"], new[] { "test123", "test123", "test321", "test321" });

            Assert.False(errorsCollection.IsEmpty);
        }
Esempio n. 15
0
        public void AddError_Should_AddToMember_When_MultipleMembers_And_MultipleErrors()
        {
            var errorsCollection = new ErrorsCollection();

            errorsCollection.AddError("test", new Error("test123"));
            errorsCollection.AddError("test", new Error("test321"));
            errorsCollection.AddError("test2", new Error("foo"));
            errorsCollection.AddError("test2", new Error("bar"));

            ErrorsCollectionTestsHelpers.ExpectMembers(errorsCollection, new[] { "test", "test2" });
            ErrorsCollectionTestsHelpers.ExpectErrors(errorsCollection.Members["test"], new[] { "test123", "test321" });
            ErrorsCollectionTestsHelpers.ExpectErrors(errorsCollection.Members["test2"], new[] { "foo", "bar" });

            Assert.False(errorsCollection.IsEmpty);
        }
Esempio n. 16
0
        public void ToModelReport_Should_Generate_MemberMessages_WithoutDuplicates()
        {
            var draft = new ErrorsCollection();

            draft.AddError("test", new Error("bar"));
            draft.AddError("test", new Error("foo"));
            draft.AddError("test", new Error("foo"));
            draft.AddError("test", new Error("bar"));

            var report = (ModelReport)ResultsTestHelpers.MockValidationResult(draft).ToModelReport();

            ExpectMembersInReport(report, new[] { "test" });

            ExpectMessagesInList((ModelReportErrorsList)report["test"], new[] { "bar", "foo" });
        }
Esempio n. 17
0
        public void ToListReport_Should_LimitDepth(int maxDepth, bool expectException)
        {
            var level3 = new ErrorsCollection();

            level3.AddError(new Error("error"));

            var level2 = new ErrorsCollection();

            level2.AddError("member2", level3);

            var level1 = new ErrorsCollection();

            level1.AddError("member1", level2);

            var errorsCollection = new ErrorsCollection();

            errorsCollection.AddError("member", level1);

            var result = ResultsTestHelpers.MockValidationResult(errorsCollection, new ExecutionContextStub {
                MaxDepth = maxDepth, CollectionForceKey = "*", RequiredError = new Error("Required")
            });

            if (expectException)
            {
                Assert.Throws <MaxDepthExceededException>(() => { result.ToListReport(); });
            }
            else
            {
                result.ToListReport();
            }
        }
Esempio n. 18
0
            public void Should_InsertErrors()
            {
                Expression <Func <MemberClass, InnerClass> > selector = m => m.Inner;

                var propertyInfo = (PropertyInfo)((MemberExpression)selector.Body).Member;

                var memberScope = new MemberScope <MemberClass, InnerClass>(propertyInfo, m => m
                                                                            .Valid(c => false).WithMessage("error1")
                                                                            );

                var target = new ErrorsCollection();

                var scopeErrors = new ErrorsCollection();

                scopeErrors.AddError(new Error("error1"));

                memberScope.InsertErrors(target, scopeErrors);

                Assert.Empty(target.Errors);

                Assert.Equal(1, target.Members.Count);

                Assert.Equal("error1", target.Members["Inner"].Errors.Single().Message);
                Assert.Empty(target.Members["Inner"].Members);
            }
Esempio n. 19
0
        public void Include_Should_Include_When_BothSingleLevel()
        {
            var errorsCollection = new ErrorsCollection();

            errorsCollection.AddError(new Error("test1"));
            errorsCollection.AddError(new Error("test2"));

            var another = new ErrorsCollection();

            errorsCollection.AddError(new Error("foo"));
            errorsCollection.AddError(new Error("bar"));

            errorsCollection.Include(another);

            ErrorsCollectionTestsHelpers.ExpectErrors(errorsCollection, new[] { "test1", "test2", "foo", "bar" });
        }
Esempio n. 20
0
            public void ContainsSingleError_Should_ReturnTrue_When_SingleError()
            {
                var errorsCollection = new ErrorsCollection();

                errorsCollection.AddError(new Error("test123"));

                Assert.True(errorsCollection.ContainsSingleError());
            }
Esempio n. 21
0
        public static IErrorsCollection ExecuteCollectionMember <TModel, TItem>(IMemberValidator memberValidator, TModel model, IEnumerable <TItem> memberValue, IExecutionContext executionContext, ValidationStrategy validationStrategy, int depth)
            where TModel : class
        {
            ErrorsCollection result = null;

            if (validationStrategy == ValidationStrategy.Force)
            {
                var itemErrorsCollection = ExecuteMember(
                    memberValidator,
                    model,
                    default(TItem),
                    executionContext,
                    validationStrategy,
                    depth
                    );

                result = new ErrorsCollection();

                result.AddError(executionContext.CollectionForceKey, itemErrorsCollection);
            }
            else if (memberValue != null)
            {
                var items = memberValue.ToArray();

                for (var i = 0; i < items.Length; i++)
                {
                    var item = items.ElementAt(i);

                    var itemErrorsCollection = ExecuteMember(
                        memberValidator,
                        model,
                        item,
                        executionContext,
                        validationStrategy,
                        depth
                        );

                    if (itemErrorsCollection.IsEmpty)
                    {
                        continue;
                    }

                    if (result == null)
                    {
                        result = new ErrorsCollection();
                    }

                    result.AddError(i.ToString(), itemErrorsCollection);

                    if ((validationStrategy == ValidationStrategy.FailFast) && !result.IsEmpty)
                    {
                        break;
                    }
                }
            }

            return(GetOrEmpty(result));
        }
Esempio n. 22
0
        public void AddError_Should_AddInnerCollection()
        {
            var errorsCollection = new ErrorsCollection();

            var innerCollection = new ErrorsCollection();

            innerCollection.AddError(new Error("test123"));
            innerCollection.AddError(new Error("test321"));
            innerCollection.AddError(new Error("foo"));
            innerCollection.AddError(new Error("bar"));

            errorsCollection.AddError("test", innerCollection);

            ErrorsCollectionTestsHelpers.ExpectMembers(errorsCollection, new[] { "test" });
            ErrorsCollectionTestsHelpers.ExpectErrors(errorsCollection.Members["test"], new[] { "test123", "test321", "foo", "bar" });

            Assert.False(errorsCollection.IsEmpty);
        }
Esempio n. 23
0
        public void AddError_Should_AddNothing_When_EmptyCollection()
        {
            var errorsCollection = new ErrorsCollection();

            errorsCollection.AddError("test", new ErrorsCollection());

            Assert.NotNull(errorsCollection.Members);
            Assert.Empty(errorsCollection.Members);
            Assert.True(errorsCollection.IsEmpty);
        }
Esempio n. 24
0
            public void GetSingleError_Should_ReturnTrue_When_SingleError()
            {
                var errorsCollection = new ErrorsCollection();

                var error = new Error("test123");

                errorsCollection.AddError(error);

                Assert.Same(error, errorsCollection.GetSingleError());
            }
Esempio n. 25
0
        public void AddError_Should_Add_SingleError()
        {
            var errorsCollection = new ErrorsCollection();

            errorsCollection.AddError(new Error("test123"));

            ErrorsCollectionTestsHelpers.ExpectErrors(errorsCollection, new[] { "test123" });

            Assert.False(errorsCollection.IsEmpty);
        }
Esempio n. 26
0
        public void Should_IsValid_ReturnIsValid(bool isValid)
        {
            var errorCollection = new ErrorsCollection();

            if (!isValid)
            {
                errorCollection.AddError(new Error("error"));
            }

            var varlidationResult = new ValidationResult <object>(Guid.NewGuid(), new Mock <ITranslationProxy>().Object, new Mock <IExecutionOptions>().Object, new object(), errorCollection);

            Assert.Equal(isValid, varlidationResult.IsValid);
        }
Esempio n. 27
0
        public IValidationResult <T> Validate <T>(T model, Func <IValidationOptions, IValidationOptions> setOptions = null)
            where T : class
        {
            if (!Types.Contains(typeof(T)))
            {
                throw new SpecificationNotFoundException(typeof(T));
            }

            var validationOptions = setOptions != null
                ? OptionsService.GetVerifiedValidationOptions(setOptions(Options.ValidationOptions))
                : Options.ValidationOptions;

            var defaultTranslator = validationOptions.TranslationName != null
                ? TranslatorsRepository.Get(validationOptions.TranslationName)
                : TranslatorsRepository.GetOriginal();

            var translationProxy = new TranslationProxy(defaultTranslator, TranslatorsRepository);

            if (model == null)
            {
                switch (validationOptions.NullRootStrategy)
                {
                case NullRootStrategy.ArgumentNullException:
                {
                    throw new ArgumentNullException(nameof(model));
                }

                case NullRootStrategy.NoErrors:
                {
                    return(new ValidationResult <T>(Id, translationProxy, Options.ValidationOptions));
                }

                default:
                {
                    var report = new ErrorsCollection();

                    report.AddError(Options.ValidationOptions.RequiredError);

                    return(new ValidationResult <T>(Id, translationProxy, Options.ValidationOptions, null, report));
                }
                }
            }

            var specification = ValidatorsFactory.GetOrInit <T>();

            var executionContext = new ExecutionContext(validationOptions, ValidatorsFactory);

            var errorsCollection = ValidatorExecutor.Execute(specification, model, executionContext, validationOptions.ValidationStrategy, 0);

            return(new ValidationResult <T>(Id, translationProxy, Options.ValidationOptions, model, errorsCollection));
        }
Esempio n. 28
0
        public void InsertErrors(ErrorsCollection targetErrorsCollection, IErrorsCollection scopeErrorsCollection)
        {
            if (targetErrorsCollection == null)
            {
                throw new ArgumentNullException(nameof(targetErrorsCollection));
            }

            if (scopeErrorsCollection == null)
            {
                throw new ArgumentNullException(nameof(scopeErrorsCollection));
            }

            targetErrorsCollection.AddError(Name, scopeErrorsCollection);
        }
Esempio n. 29
0
        public void ToListReport_Should_Generate_NestedLevel()
        {
            var errorsCollection = new ErrorsCollection();

            var nestedErrorsCollection = new ErrorsCollection();

            nestedErrorsCollection.AddError(new Error("test123"));
            nestedErrorsCollection.AddError(new Error("test321"));
            nestedErrorsCollection.AddError("inner", new Error("foo"));
            nestedErrorsCollection.AddError("inner", new Error("bar"));

            errorsCollection.AddError("test", nestedErrorsCollection);

            var report = ResultsTestHelpers.MockValidationResult(errorsCollection).ToListReport();

            ExpectMembersInReport(report, "", new[] { "test" });

            ExpectMessagesInReport(report, "test", new[] { "test123", "test321" });

            ExpectMembersInReport(report, "test", new[] { "inner" });

            ExpectMessagesInReport(report, "test.inner", new[] { "foo", "bar" });
        }
Esempio n. 30
0
            public void Should_InsertErrors()
            {
                var target = new ErrorsCollection();

                var scopeErrors = new ErrorsCollection();

                scopeErrors.AddError(new Error("error1"));

                var modelScope = new ModelScope <MemberClass>(c => true);

                modelScope.InsertErrors(target, scopeErrors);

                Assert.Equal("error1", target.Errors.Single().Message);
                Assert.Empty(target.Members);
            }