コード例 #1
0
        public void Should_IsValid_Return_True_If_NoErrors(string name, Specification <ValidationTestData.TestClass> specification, ValidationTestData.TestClass model, bool expectedIsValid, ValidationTestData.ReferenceLoopExceptionCase exceptionCase)
        {
            _ = name;

            var validator = new Validator <ValidationTestData.TestClass>(specification);

            validator.ShouldHaveIsValueTrueIfNoErrors(model, expectedIsValid, exceptionCase);
        }
コード例 #2
0
        public void Should_Validate_With_ReferenceLoopProtection(string name, bool referenceLoopProtectionEnabled, Specification <ValidationTestData.TestClass> specification, ValidationTestData.TestClass model, IReadOnlyDictionary <string, IReadOnlyList <ValidationTestData.ErrorTestCase> > errorCases, ValidationTestData.ReferenceLoopExceptionCase exceptionCase)
        {
            _ = name;

            var settings = new ValidatorSettings();

            if (referenceLoopProtectionEnabled)
            {
                settings.WithReferenceLoopProtection();
            }
            else
            {
                settings.WithReferenceLoopProtectionDisabled();
            }

            var validator = new Validator <ValidationTestData.TestClass>(specification, settings);

            validator.ShouldValidateAndHaveResult(model, false, errorCases, exceptionCase);
        }
コード例 #3
0
        public void Should_Validate_AndFailFast(string name, Specification <ValidationTestData.TestClass> specification, ValidationTestData.TestClass model, IReadOnlyDictionary <string, IReadOnlyList <ValidationTestData.ErrorTestCase> > errorCases, ValidationTestData.ReferenceLoopExceptionCase exceptionCase)
        {
            _ = name;

            var validator = new Validator <ValidationTestData.TestClass>(specification);

            validator.ShouldValidateAndHaveResult(model, true, errorCases, exceptionCase);
        }
コード例 #4
0
            public void Should_Validate(string name, Specification <ValidationTestData.TestClass> specification, ValidationTestData.TestClass model, IReadOnlyDictionary <string, IReadOnlyList <ValidationTestData.ErrorTestCase> > errorCases, ValidationTestData.ReferenceLoopExceptionCase exceptionCase)
            {
                _ = name;

                var holder = new TestClassSpecificationHolder()
                {
                    Specification = specification
                };

                var validator = Validator.Factory.Create(holder);

                validator.ShouldValidateAndHaveResult(model, false, errorCases, exceptionCase);
            }
コード例 #5
0
            public void Should_IsValid_Return_True_If_NoErrors(string name, Specification <ValidationTestData.TestClass> specification, ValidationTestData.TestClass model, bool expectedIsValid, ValidationTestData.ReferenceLoopExceptionCase exceptionCase)
            {
                _ = name;

                var holder = new TestClassSpecificationHolder()
                {
                    Specification = specification
                };

                var validator = Validator.Factory.Create(holder);

                validator.ShouldHaveIsValueTrueIfNoErrors(model, expectedIsValid, exceptionCase);
            }
コード例 #6
0
        public static void ShouldHaveIsValidTrueIfNoErrors <T>(this IValidator <T> validator, T model, bool expectedIsValid, ValidationTestData.ReferenceLoopExceptionCase exceptionCase)
        {
            bool isValid = false;

            Action action = () =>
            {
                isValid = validator.IsValid(model);
            };

            if (exceptionCase is null)
            {
                action.Should().NotThrow();

                isValid.Should().Be(expectedIsValid);
            }
            else
            {
                var exceptionThrown = action.Should().ThrowExactly <ReferenceLoopException>().Which;

                exceptionThrown.Type.Should().Be(exceptionCase.Type);
                exceptionThrown.Path.Should().BeNull();
                exceptionThrown.NestedPath.Should().BeNull();
            }
        }
コード例 #7
0
        public static void ShouldValidateAndHaveResult <T>(this IValidator <T> validator, T model, bool failFast, IReadOnlyDictionary <string, IReadOnlyList <ValidationTestData.ErrorTestCase> > rawErrorsExpectations, ValidationTestData.ReferenceLoopExceptionCase exceptionCase)
        {
            IValidationResult result = null;

            Action action = () =>
            {
                result = validator.Validate(model, failFast);
            };

            if (exceptionCase is null)
            {
                action.Should().NotThrow();

                var errorOutput = ((ValidationResult)result).GetErrorOutput();

                errorOutput.ShouldMatchExpectations(rawErrorsExpectations);
            }
            else
            {
                var exceptionThrown = action.Should().ThrowExactly <ReferenceLoopException>().Which;

                exceptionThrown.Type.Should().Be(exceptionCase.Type);
                exceptionThrown.Path.Should().Be(exceptionCase.Path);
                exceptionThrown.NestedPath.Should().Be(exceptionCase.NestedPath);
            }
        }