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

            Expect(range.GetEndDate(DateTime.MinValue), Is.EqualTo(today.AddDays(1)), "An ending window range of 1 day should always produce one day after the start");
        }
        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 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");
        }
Example #5
0
        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 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 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 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 CanCalculateRelativeEndDateForYesterday([ValueSource("SampleDates")]DateTime today, [ValueSource("DayBeforeSampleDates")]DateTime yesterday)
        {
            var startBound = DateRangeBound.CreateUnboundedBound();
            var endBound = DateRangeBound.CreateRelativeBound(-1, DateInterval.Day);
            var range = new DateRange(startBound, endBound);

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

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

            Expect(range.GetEndDate(today), Is.EqualTo(today), "A relative range of zero days should always produce today");
        }
        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");
        }