public void CanCalculateWindowOneDayBeforeSpecificDate([ValueSource("SampleDates")]DateTime today)
        {
            var startBound = DateRangeBound.CreateWindowBound(1, DateInterval.Day);
            var endBound = DateRangeBound.CreateSpecificDateBound(today);
            var range = new DateRange(startBound, endBound);

            Expect(range.GetStartDate(DateTime.MinValue), Is.EqualTo(today.AddDays(-1)), "A starting window range of 1 day should always produce one day before the end");
        }
        public void EndWindowIsInvalidWithUnboundedStart(
            [ValueSource("SampleWindowAmounts")]int endAmount,
            [ValueSource("AllDateInvervals")]DateInterval endInterval)
        {
            var startBound = DateRangeBound.CreateUnboundedBound();
            var endBound = DateRangeBound.CreateWindowBound(endAmount, endInterval);
            var range = new DateRange(startBound, endBound);

            this.Expect(range.ErrorMessage, Is.EqualTo("Window to Beginning of Time"), "A range from unbounded to window is always invalid");
        }
        public void EndWindowIsValidWithStartingSpecific(
            [ValueSource("SampleDates")]DateTime starting,
            [ValueSource("SampleWindowAmounts")]int endAmount,
            [ValueSource("AllDateInvervals")]DateInterval endInterval)
        {
            var startBound = DateRangeBound.CreateSpecificDateBound(starting);
            var endBound = DateRangeBound.CreateWindowBound(endAmount, endInterval);
            var range = new DateRange(startBound, endBound);

            Expect(range.IsValid, Is.True, "A range from specific to window is always valid");
        }
        public void CanCalculateWindowOneDayBeforeRelativeDate(
            [ValueSource("SampleDates")]DateTime today,
            [ValueSource("SampleRelativeAmounts")]int relativeAmount,
            [ValueSource("AllDateInvervals")]DateInterval relativeInterval)
        {
            var startBound = DateRangeBound.CreateWindowBound(1, DateInterval.Day);
            var endBound = DateRangeBound.CreateRelativeBound(relativeAmount, relativeInterval);
            var range = new DateRange(startBound, endBound);

            Expect(range.GetStartDate(today), Is.EqualTo(range.GetEndDate(today).Value.AddDays(-1)), "A starting window range of 1 day should always produce one day before the end");
        }
        public void EndWindowIsValidWithStartingRelative(
            [ValueSource("SampleRelativeAmounts")]int startAmount,
            [ValueSource("AllDateInvervals")]DateInterval startInterval,
            [ValueSource("SampleWindowAmounts")]int endAmount,
            [ValueSource("AllDateInvervals")]DateInterval endInterval)
        {
            var startBound = DateRangeBound.CreateRelativeBound(startAmount, startInterval);
            var endBound = DateRangeBound.CreateWindowBound(endAmount, endInterval);
            var range = new DateRange(startBound, endBound);

            Expect(range.IsValid, Is.True, "A range from relative to window is always valid");
        }
        public void CanCalculateRelativeEndDateForNextMonth([ValueSource("SampleDates")]DateTime today)
        {
            var startBound = DateRangeBound.CreateUnboundedBound();
            var endBound = DateRangeBound.CreateRelativeBound(1, DateInterval.Month);
            var range = new DateRange(startBound, endBound);

            var endDate = range.GetEndDate(today);

            Expect(endDate.HasValue, Is.True, "Should have a start date");
            Expect(endDate.Value.Year, Is.EqualTo(today.AddMonths(1).Year), "A relative range of 1 month should always produce the month after 'today'");
            Expect(endDate.Value.Month, Is.EqualTo(today.AddMonths(1).Month), "A relative range of 1 month should always produce the month after 'today'");
            Expect(endDate.Value.Day, Is.EqualTo(GetLastDayOfMonth(today.AddMonths(1)).Day), "A relative range of 1 month should always produce the last day of the month after 'today'");
        }
        public void CanCalculateRelativeEndDateForLastYear([ValueSource("SampleDates")]DateTime today)
        {
            var startBound = DateRangeBound.CreateUnboundedBound();
            var endBound = DateRangeBound.CreateRelativeBound(-1, DateInterval.Year);
            var range = new DateRange(startBound, endBound);

            var endDate = range.GetEndDate(today);

            Expect(endDate.HasValue, Is.True, "Should have a start date");
            Expect(endDate.Value.Year, Is.EqualTo(today.AddYears(-1).Year), "A relative range of -1 year should always produce the year before 'today'");
            Expect(endDate.Value.Month, Is.EqualTo(12), "An ending relative range of years should always produce December");
            Expect(endDate.Value.Day, Is.EqualTo(31), "An ending relative range of years should always produce December 31");
        }
        public object FormatDateRange(DateRangeBoundJsonTransferObject start, DateRangeBoundJsonTransferObject end)
        {
            DateRangeBound startRangeBound;
            DateRangeBound endRangeBound;
            try
            {
                startRangeBound = DateRangeBound.Parse(start.value, start.specificDate, start.windowAmount, start.windowInterval);
                endRangeBound = DateRangeBound.Parse(end.value, end.specificDate, end.windowAmount, end.windowInterval);
            }
            catch (ArgumentNullException exc)
            {
                return new { isError = true, message = Localization.GetString("Missing " + exc.ParamName, ResourceFileRoot) };
            }

            var dateRange = new DateRange(startRangeBound, endRangeBound);
            if (!string.IsNullOrEmpty(dateRange.ErrorMessage))
            {
                return new { isError = true, message = Localization.GetString(dateRange.ErrorMessage, ResourceFileRoot) };
            }

            var dateRangeResourceKey = startRangeBound.IsUnbounded && endRangeBound.IsUnbounded
                                           ? "Date Range Without Bounds.Text"
                                           : startRangeBound.IsUnbounded
                                                 ? "Date Range Without Start.Format"
                                                 : endRangeBound.IsUnbounded ? "Date Range Without End.Format" : "Date Range.Format";

            var dateRangeText = string.Format(
                CultureInfo.CurrentCulture,
                Localization.GetString(dateRangeResourceKey, ResourceFileRoot),
                dateRange.GetStartDate(),
                dateRange.GetEndDate());

            #if DEBUG
            dateRangeText = dateRangeText.Replace("[L]", string.Empty);
            #endif

            return new { isError = false, message = dateRangeText };
        }
        public void CanCalculateRelativeStartDateForLastMonth([ValueSource("SampleDates")]DateTime today)
        {
            var startBound = DateRangeBound.CreateRelativeBound(-1, DateInterval.Month);
            var endBound = DateRangeBound.CreateUnboundedBound();
            var range = new DateRange(startBound, endBound);

            var startDate = range.GetStartDate(today);

            Expect(startDate.HasValue, Is.True, "Should have a start date");
            Expect(startDate.Value.Year, Is.EqualTo(today.AddMonths(-1).Year), "A relative range of -1 month should always produce the month before 'today'");
            Expect(startDate.Value.Month, Is.EqualTo(today.AddMonths(-1).Month), "A relative range of -1 month should always produce the month before 'today'");
            Expect(startDate.Value.Day, Is.EqualTo(1), "A starting relative range of -1 month should always produce the first day of the month");
        }
        public void CanCalculateWindowTenYearsBeforeSpecificDate([ValueSource("SampleDates")]DateTime today)
        {
            var startBound = DateRangeBound.CreateWindowBound(10, DateInterval.Year);
            var endBound = DateRangeBound.CreateSpecificDateBound(today);
            var range = new DateRange(startBound, endBound);

            Expect(range.GetStartDate(DateTime.MinValue), Is.EqualTo(today.AddYears(-10)), "A starting window range of 10 years should always produce ten years before the end");
        }
        public void CanCalculateRelativeStartDateForYesterday([ValueSource("SampleDates")]DateTime today, [ValueSource("DayBeforeSampleDates")]DateTime yesterday)
        {
            var startBound = DateRangeBound.CreateRelativeBound(-1, DateInterval.Day);
            var endBound = DateRangeBound.CreateUnboundedBound();
            var range = new DateRange(startBound, endBound);

            Expect(range.GetStartDate(today), Is.EqualTo(yesterday), "A relative range of -1 day should always produce the day before 'today'");
        }
        public void CanCalculateRelativeStartDateForTomorrow([ValueSource("SampleDates")]DateTime today, [ValueSource("DayAfterSampleDates")]DateTime tomorrow)
        {
            var startBound = DateRangeBound.CreateRelativeBound(1, DateInterval.Day);
            var endBound = DateRangeBound.CreateUnboundedBound();
            var range = new DateRange(startBound, endBound);

            Expect(range.GetStartDate(today), Is.EqualTo(tomorrow), "A relative range of 1 day should always produce the day after 'today'");
        }
        public void CanCalculateRelativeStartDateForToday([ValueSource("SampleDates")]DateTime today)
        {
            var startBound = DateRangeBound.CreateRelativeBound(0, DateInterval.Day);
            var endBound = DateRangeBound.CreateUnboundedBound();
            var range = new DateRange(startBound, endBound);

            Expect(range.GetStartDate(today), Is.EqualTo(today), "A relative range of zero days should always produce today");
        }
        public void CanCalculateRelativeStartDateForThisYear([ValueSource("SampleDates")]DateTime today)
        {
            var startBound = DateRangeBound.CreateRelativeBound(0, DateInterval.Year);
            var endBound = DateRangeBound.CreateUnboundedBound();
            var range = new DateRange(startBound, endBound);

            var startDate = range.GetStartDate(today);

            Expect(startDate.HasValue, Is.True, "Should have a start date");
            Expect(startDate.Value.Year, Is.EqualTo(today.Year), "A relative range of zero years should always produce the same year");
            Expect(startDate.Value.Month, Is.EqualTo(1), "A starting relative range of years should always produce January");
            Expect(startDate.Value.Day, Is.EqualTo(1), "A starting relative range of years should produce January 1");
        }
        public void CanCalculateWindowOneYearAfterSpecificDate([ValueSource("SampleDates")]DateTime today)
        {
            var startBound = DateRangeBound.CreateSpecificDateBound(today);
            var endBound = DateRangeBound.CreateWindowBound(1, DateInterval.Year);
            var range = new DateRange(startBound, endBound);

            Expect(range.GetEndDate(DateTime.MinValue), Is.EqualTo(today.AddYears(1)), "An ending window range of 1 year should always produce one year after the start");
        }
        public void CanCalculateWindowTenMonthsAfterSpecificDate([ValueSource("SampleDates")]DateTime today)
        {
            var startBound = DateRangeBound.CreateSpecificDateBound(today);
            var endBound = DateRangeBound.CreateWindowBound(10, DateInterval.Month);
            var range = new DateRange(startBound, endBound);

            Expect(range.GetEndDate(DateTime.MinValue), Is.EqualTo(today.AddMonths(10)), "An ending window range of 10 months should always produce ten months after the start");
        }
        public void StartWindowIsValidWithEndingSpecific(
            [ValueSource("SampleWindowAmounts")]int startAmount,
            [ValueSource("AllDateInvervals")]DateInterval startInterval,
            [ValueSource("SampleDates")]DateTime endingDate)
        {
            var startBound = DateRangeBound.CreateWindowBound(startAmount, startInterval);
            var endBound = DateRangeBound.CreateSpecificDateBound(endingDate);
            var range = new DateRange(startBound, endBound);

            Expect(range.IsValid, Is.True, "A range from window to specific is always valid");
        }
        public void TwoUnboundedAreValid()
        {
            var startBound = DateRangeBound.CreateUnboundedBound();
            var endBound = DateRangeBound.CreateUnboundedBound();
            var range = new DateRange(startBound, endBound);

            Expect(range.IsValid, Is.True, "A range from unbounded to unbounded is always valid");
        }
        public void TwoWindowsAreInvalid(
            [ValueSource("SampleWindowAmounts")]int startAmount,
            [ValueSource("AllDateInvervals")]DateInterval startInterval,
            [ValueSource("SampleWindowAmounts")]int endAmount,
            [ValueSource("AllDateInvervals")]DateInterval endInterval)
        {
            var startBound = DateRangeBound.CreateWindowBound(startAmount, startInterval);
            var endBound = DateRangeBound.CreateWindowBound(endAmount, endInterval);
            var range = new DateRange(startBound, endBound);

            Expect(range.ErrorMessage, Is.EqualTo("Both Window"), "A range from window to window is always invalid");
        }
        public void CanCalculateWindowOneMonthAfterRelativeDate(
            [ValueSource("SampleDates")]DateTime today,
            [ValueSource("SampleRelativeAmounts")]int relativeAmount,
            [ValueSource("AllDateInvervals")]DateInterval relativeInterval)
        {
            var startBound = DateRangeBound.CreateRelativeBound(relativeAmount, relativeInterval);
            var endBound = DateRangeBound.CreateWindowBound(1, DateInterval.Month);
            var range = new DateRange(startBound, endBound);

            Expect(range.GetEndDate(today), Is.EqualTo(range.GetStartDate(today).Value.AddMonths(1)), "An ending window range of 1 month should always produce one month after the start");
        }
        public void CanCalculateRelativeEndDateForThisMonth([ValueSource("SampleDates")]DateTime today)
        {
            var startBound = DateRangeBound.CreateUnboundedBound();
            var endBound = DateRangeBound.CreateRelativeBound(0, DateInterval.Month);
            var range = new DateRange(startBound, endBound);

            var endDate = range.GetEndDate(today);

            Expect(endDate.HasValue, Is.True, "Should have a start date");
            Expect(endDate.Value.Year, Is.EqualTo(today.Year), "A relative range of zero months should always produce the same year");
            Expect(endDate.Value.Month, Is.EqualTo(today.Month), "A relative range of zero months should always produce the same month");
            Expect(endDate.Value.Day, Is.EqualTo(GetLastDayOfMonth(today).Day), "An ending relative range of zero months should always produce the last day of 'today's' month");
        }