Exemple #1
0
        /// <summary>
        /// Validate date properties are in correct range, roughly
        /// </summary>
        /// <param name="year"></param>
        /// <param name="month"></param>
        /// <param name="day"></param>
        /// <exception cref="ArgumentException"></exception>
        private static void ValidateLocalDate(int year, int month, int day)
        {
            typeof(LocalDateStruct).GetProperties().ToDictionary(x => x.Name, x => x.GetCustomAttribute <RangeAttribute>())
            .ForEach(x =>
            {
                // ReSharper disable once SwitchStatementMissingSomeCases
                switch (x.Key)
                {
                case "Year":
                    ValidateRangeAction(x.Value, year);
                    break;

                case "Month":
                    ValidateRangeAction(x.Value, month);
                    break;

                case "Day":
                    ValidateRangeAction(x.Value, day);
                    break;
                }
            });

            // More specific range validation
            if (day > YearUtility.NumberOfDaysInMonth(year, month))
            {
                throw new LocalDateRangeException();
            }
        }
Exemple #2
0
        /// <summary>
        /// Creates a fixture from a random predefined list
        /// </summary>
        /// <returns></returns>
        private Fixture GetFixture()
        {
            var fixture = new Fixture();

            var proerties = typeof(LocalDate).GetProperties()
                            .ToDictionary(x => x.Name, y => y.GetCustomAttribute <RangeAttribute>())
                            .ToDictionary(x => x.Key, y => (minimum: y.Value.Minimum, maximum: y.Value.Maximum));

            var list = new List <LocalDate>();

            for (var i = 0; i < 100; i++)
            {
                var day   = _random.Next((int)proerties["Day"].minimum, (int)proerties["Day"].maximum);
                var month = _random.Next((int)proerties["Month"].minimum, (int)proerties["Month"].maximum);
                var year  = _random.Next((int)proerties["Year"].minimum, (int)proerties["Year"].maximum);

                // Fix the day value
                while (day > YearUtility.NumberOfDaysInMonth(year, month))
                {
                    day--;
                }

                list.Add(new LocalDate(year, month, day));
            }

            fixture.Customizations.Add(new ElementsBuilder <LocalDate>(list));

            return(fixture);
        }