public void ThrowFilterIncorrectException(Exception innerException)
 {
     throw new InputValidationException(innerException,
                                        (
                                            PropertyName: nameof(Filter),
                                            ErrorMessage: $"The specified filter string '{Filter}' is invalid."
                                        )
                                        );
 }
 public void ThrowOrderByIncorrectException(Exception innerException)
 {
     throw new InputValidationException(innerException,
                                        (
                                            PropertyName: nameof(OrderBy),
                                            ErrorMessage: $"The specified orderBy string '{OrderBy}' is invalid."
                                        )
                                        );
 }
        /// <summary>
        /// Gets input from ReadLine and loops until the input matches against the given regex.
        /// </summary>
        /// <param name="fieldName">The name of the field, used in the prompt.</param>
        /// <param name="regex">The regex to validate the input with.</param>
        /// <returns>The validated input.</returns>
        public static string GetAndValidateInput(string fieldName, Regex regex)
        {
            char   firstLetter = fieldName.ToUpper().ToCharArray()[0];
            string name        = firstLetter.ToString() + fieldName.Substring(1);
            string prompt      = string.Format("{0}: ", name);

            string input = "";

            while (true)
            {
                try
                {
                    input = ReadLine(prompt);

                    if (regex.IsMatch(input))
                    {
                        break;
                    }
                    else
                    {
                        InputValidationException ive = new InputValidationException(string.Format("Invalid {0}.", name));
                        throw ive;
                    }
                }
                catch (InputValidationException ive)
                {
                    Console.Write(string.Format("{0} Press any key to reenter...", ive.Message));
                    Console.WriteLine();
                    Console.ReadKey();
                    continue;
                }
            }

            return(input);
        }
        public void Instantiation_Blank_SetsProperties()
        {
            var innerException = new Exception();

            var sut = new InputValidationException(innerException);

            sut.InnerException.Should().Be(innerException);
            sut.Message.Should().Contain("validation");
            sut.Errors.Should().NotBeNull();
        }
Exemple #5
0
        public void ReportInputValidationException(ModuleLiteral env, InputValidationException inputValidationException, LineInfo lineInfo)
        {
            var location = lineInfo.AsUniversalLocation(env, Context);

            Logger.ReportInputValidationError(
                LoggingContext,
                location.AsLoggingLocation(),
                inputValidationException.ErrorContext.ToErrorString(Context),
                !string.IsNullOrEmpty(inputValidationException.Message) ? ": " + inputValidationException.Message : string.Empty,
                Context.GetStackTraceAsErrorMessage(location));
        }
        public void Instantiation_WithValidationFailures_SetsProperties()
        {
            var failures = new List <ValidationFailure>()
            {
                new ValidationFailure("Prop", "Prop not good, mister."),
                new ValidationFailure("Prop", "Prop really not good, mister.")
            };

            var sut = new InputValidationException(failures);

            sut.InnerException.Should().BeNull();
            sut.Message.Should().Contain("validation");
            sut.Errors.Should().HaveCount(1).And.ContainKey("Prop");
            sut.Errors.First().Should().BeEquivalentTo(
                new {
                Key   = "Prop",
                Value = new[] {
                    failures[0].ErrorMessage,
                    failures[1].ErrorMessage
                }
            },
                because: "Validations failured are expected to be grouped by property name."
                );
        }
        public void RouteFinder_Should_Call_RouteValidator()
        {
            // Arrange
            var startPoint = this.pointA;
            var endPoint   = this.pointA;

            var routes = new List <Path>
            {
                new Path {
                    Id = 1, PointOne = this.pointA, PointTwo = this.pointB, Distance = 1
                },
                new Path {
                    Id = 2, PointOne = this.pointB, PointTwo = this.pointC, Distance = 4
                },
                new Path {
                    Id = 3, PointOne = this.pointC, PointTwo = this.pointD, Distance = 3
                },
                new Path {
                    Id = 4, PointOne = this.pointA, PointTwo = this.pointD, Distance = 20
                }
            };

            var expected = new List <Route>
            {
                new Route()
                {
                    Paths = new List <Path>()
                }
            };

            var mockRouteExplorer = new Mock <IRouteExplorer>();

            mockRouteExplorer
            .Setup(x => x.GetAllPossibleRoutes(It.IsAny <List <Path> >(), It.IsAny <Point>(), It.IsAny <Point>()))
            .Returns(expected);

            var mockShortestRouteFinder = new Mock <IShortestRouteFinder>();

            var mockShortestRouteFinderResult = new Route();

            mockShortestRouteFinder
            .Setup(x => x.GetShortestRoute(expected))
            .Returns(mockShortestRouteFinderResult);

            var mockRouteValidator = new Mock <IRouteValidator>();

            var mockRouteValidatorResult = new InputValidationException("The start and end point cannot be the same");

            mockRouteValidator
            .Setup(x => x.ValidateInput(startPoint, endPoint))
            .Throws(mockRouteValidatorResult);

            var routeFinder = new RouteFinder(mockRouteExplorer.Object, mockShortestRouteFinder.Object, new DatabaseReader(), mockRouteValidator.Object);

            // Act
            Action result = () => routeFinder.CalculateShortestRoute(startPoint, endPoint);

            // Assert
            result.Should().Throw <InputValidationException>()
            .WithMessage("The start and end point cannot be the same");
        }