using System; using NUnit.Framework; [TestFixture] public class When_constructing_a_time_period { [Test, ExpectedException(typeof(ArgumentOutOfRangeException))] public void Should_throw_exception_if_end_falls_before_start() { var start = DateTime.Today.AddDays(2); var end = DateTime.Today; new TimePeriod(start, end); } [Test, ExpectedException(typeof(ArgumentException))] public void Should_throw_exception_if_end_is_datetime_max() { new TimePeriod(DateTime.Today, DateTime.MaxValue); } [Test] public void Duration_should_be_timespan_between_start_and_end() { Assert.AreEqual(DateTime.Today.AddDays(2) - DateTime.Today, new TimePeriod(DateTime.Today, DateTime.Today.AddDays(2)).Duration); } } [TestFixture] public class When_creating_a_time_period_from_two_strings { [Test, ExpectedException(typeof(ArgumentException))] public void Should_throw_exception_if_start_value_is_null_or_empty( [Values(null, "")]string startValue) { TimePeriod.Parse(startValue, null); } [Test] public void The_end_value_can_be_null_or_empty([Values(null, "")]string endValue) { var p = TimePeriod.Parse("2009/06/13 15:00", endValue); var expected = new TimePeriod(new DateTime(2009, 6, 13, 15, 0, 0), null); Assert.AreEqual(expected, p); } [Test] public void Should_create_time_period( [Values(null, "")]string startValue) { var p = TimePeriod.Parse("2009/06/13 15:00", "2009/06/14 15:00"); var expected = new TimePeriod(new DateTime(2009, 6, 13, 15, 0, 0), new DateTime(2009, 6, 14, 15, 0, 0)); Assert.AreEqual(expected, p); } [Test, ExpectedException] public void Should_throw_exception_if_start_date_is_invalid_format() { TimePeriod.Parse("asdasdsa", null); } [Test, ExpectedException] public void Should_throw_exception_if_end_date_is_invalid_format() { TimePeriod.Parse("2009/06/13 15:00", "sdfsdf"); } } [TestFixture] public class When_time_period_has_null_end_point { [Test] public void Should_be_infinite() { Assert.IsTrue(new TimePeriod(DateTime.Now, null).IsInfinite); } [Test] public void Duration_should_be_timespan_maxvalue() { Assert.AreEqual(TimeSpan.MaxValue, new TimePeriod(DateTime.Now, null).Duration); } } [TestFixture] public class When_checking_if_a_time_period_includes_a_time { protected TimePeriod period = new TimePeriod(DateTime.Today, DateTime.Today.AddMonths(1)); [Test] public void Start_should_be_inclusive() { Assert.IsTrue(period.Includes(DateTime.Today)); } [Test] public void End_should_be_inclusive() { Assert.IsTrue(period.Includes(DateTime.Today.AddMonths(1))); } [Test] public void Should_include_time_within_period() { Assert.IsTrue(period.Includes(DateTime.Today.AddDays(2))); } [Test] public void Should_exclude_time_before_period_starts() { Assert.IsFalse(period.Includes(DateTime.Today.AddDays(-1))); } [Test] public void Should_exclude_time_after_period_ends() { Assert.IsFalse(period.Includes(DateTime.Today.AddMonths(2))); } [TestFixture] public class and_the_period_is_infinite { [Test] public void Should_include_any_time_after_start() { var period = new TimePeriod(DateTime.Now, null); Assert.IsTrue(period.Includes(DateTime.MaxValue)); } } } [TestFixture] public class When_comparing_two_time_periods { [Test] public void Should_be_equal_if_start_and_ends_are_the_same() { var a = new TimePeriod(DateTime.Today, null); var b = new TimePeriod(DateTime.Today, null); Assert.AreEqual(a, b); } [Test] public void Should_not_be_equal_if_start_date_is_different() { var a = new TimePeriod(DateTime.Today, null); var b = new TimePeriod(DateTime.Today.AddDays(1), null); Assert.AreNotEqual(a, b); } [Test] public void Should_not_be_equal_if_end_date_is_different() { var a = new TimePeriod(DateTime.Today, null); var b = new TimePeriod(DateTime.Today, DateTime.Today.AddDays(1)); Assert.AreNotEqual(a, b); } [Test] public void Should_not_equal_null() { var a = new TimePeriod(DateTime.Today, null); Assert.IsFalse(a.Equals(null)); } } [TestFixture] public class When_checking_one_time_period_starts_after_another { [Test] public void Should_not_be_after_if_time_period_being_checked_is_null() { var a = new TimePeriod(DateTime.Today, null); Assert.IsFalse(a.StartsAfter(null)); } [Test] public void Should_not_be_after_if_start_is_the_same() { var a = new TimePeriod(DateTime.Today, null); var b = new TimePeriod(DateTime.Today, DateTime.Today.AddDays(7)); Assert.IsFalse(a.StartsAfter(b)); } [Test] public void Should_be_true_if_time_period_starts_after_time_period_being_checked() { var a = new TimePeriod(DateTime.Today.AddDays(1), null); var b = new TimePeriod(DateTime.Today, null); Assert.IsTrue(a.StartsAfter(b)); } } [TestFixture] public class When_checking_one_time_period_starts_before_another { [Test] public void Should_not_be_before_if_time_period_being_checked_is_null() { var a = new TimePeriod(DateTime.Today, null); Assert.IsFalse(a.StartsBefore(null)); } [Test] public void Should_not_be_before_if_start_is_the_same() { var a = new TimePeriod(DateTime.Today, null); var b = new TimePeriod(DateTime.Today, DateTime.Today.AddDays(7)); Assert.IsFalse(a.StartsBefore(b)); } [Test] public void Should_be_true_if_time_period_starts_before_time_period_being_checked() { var a = new TimePeriod(DateTime.Today, null); var b = new TimePeriod(DateTime.Today.AddDays(1), DateTime.Today.AddDays(7)); Assert.IsTrue(a.StartsBefore(b)); } } [TestFixture] public class When_checking_one_time_period_ends_after_another { [Test] public void Should_be_false_if_time_period_being_checked_is_null() { var a = new TimePeriod(DateTime.Today, null); Assert.IsFalse(a.EndsAfter(null)); } [Test] public void Should_be_false_after_if_ends_are_the_same() { var a = new TimePeriod(DateTime.Today, DateTime.Today.AddDays(7)); var b = new TimePeriod(DateTime.Today.AddDays(-7), DateTime.Today.AddDays(7)); Assert.IsFalse(a.EndsAfter(b)); } [Test] public void Should_be_false_if_both_ends_are_null() { var a = new TimePeriod(DateTime.Today, null); var b = new TimePeriod(DateTime.Today, null); Assert.IsFalse(a.EndsAfter(b)); } [Test] public void Should_be_true_time_period_being_checked_ends_later() { var a = new TimePeriod(DateTime.Today, DateTime.Today.AddDays(14)); var b = new TimePeriod(DateTime.Today, DateTime.Today.AddDays(7)); Assert.IsTrue(a.EndsAfter(b)); } [Test] public void Should_be_true_if_time_period_is_infinite_and_other_is_not() { var a = new TimePeriod(DateTime.Today, null); var b = new TimePeriod(DateTime.Today.AddDays(1), DateTime.Today.AddDays(7)); Assert.IsTrue(a.EndsAfter(b)); } } [TestFixture] public class When_checking_one_time_period_ends_before_another { [Test] public void Should_not_be_true_if_time_period_being_checked_is_null() { var a = new TimePeriod(DateTime.Today, null); Assert.IsFalse(a.EndsBefore(null)); } [Test] public void Should_not_be_true_if_ends_are_the_same() { var a = new TimePeriod(DateTime.Today, DateTime.Today.AddDays(7)); var b = new TimePeriod(DateTime.Today.AddDays(-7), DateTime.Today.AddDays(7)); Assert.IsFalse(a.EndsBefore(b)); } [Test] public void Should_not_be_true_if_both_ends_are_null() { var a = new TimePeriod(DateTime.Today, null); var b = new TimePeriod(DateTime.Today, null); Assert.IsFalse(a.EndsBefore(b)); } [Test] public void Should_be_true_if_time_period_ends_before_time_period_being_checked() { var a = new TimePeriod(DateTime.Today, DateTime.Today.AddDays(3)); var b = new TimePeriod(DateTime.Today, DateTime.Today.AddDays(7)); Assert.IsTrue(a.EndsBefore(b)); } [Test] public void Should_be_true_if_time_period_isnt_infinite_and_the_other_is() { var a = new TimePeriod(DateTime.Today, DateTime.Today.AddDays(7)); var b = new TimePeriod(DateTime.Today, null); Assert.IsTrue(a.EndsBefore(b)); } } [TestFixture] public class When_checking_if_a_time_period_preceeds_another { [Test] public void Time_period_preceeds_another_if_its_end_equals_the_start_of_the_other() { var a = new TimePeriod(DateTime.Today, DateTime.Today.AddDays(7)); var b = new TimePeriod(DateTime.Today.AddDays(7), null); Assert.IsTrue(a.ImmediatelyPrecedes(b)); } [Test] public void Time_period_cannot_precede_another_that_is_infinite() { var a = new TimePeriod(DateTime.Today, null); var b = new TimePeriod(DateTime.Today.AddDays(7), DateTime.Today.AddDays(14)); Assert.IsFalse(a.ImmediatelyPrecedes(b)); } } [TestFixture] public class When_checking_if_a_time_period_follows_another { [Test] public void Time_period_follows_another_if_its_start_equals_the_end_of_the_other() { var a = new TimePeriod(DateTime.Today.AddDays(7), DateTime.Today.AddDays(14)); var b = new TimePeriod(DateTime.Today, DateTime.Today.AddDays(7)); Assert.IsTrue(a.ImmediatelyFollows(b)); } [Test] public void Time_period_cannot_follow_another_that_is_infinite() { var a = new TimePeriod(DateTime.Today.AddDays(7), DateTime.Today.AddDays(14)); var b = new TimePeriod(DateTime.Today, null); Assert.IsFalse(a.ImmediatelyFollows(b)); } } [TestFixture] public class When_checking_if_two_time_periods_overlap { [Test] public void there_is_an_overlap_if_a_time_period_begins_within_the_boundries_of_another() { var a = new TimePeriod(DateTime.Today, DateTime.Today.AddDays(14)); var b = new TimePeriod(DateTime.Today.AddDays(3), null); Assert.IsTrue(a.Overlaps(b)); } [Test] public void there_is_an_overlap_if_a_time_period_contains_another_that_exists_within_its_boundries() { var a = new TimePeriod(DateTime.Today.AddDays(3), DateTime.Today.AddDays(8)); var b = new TimePeriod(DateTime.Today, null); Assert.IsTrue(a.Overlaps(b)); } [Test] public void there_is_no_overlap_if_the_date_ranges_do_not_cross() { var a = new TimePeriod(DateTime.Today, DateTime.Today.AddDays(7)); var b = new TimePeriod(DateTime.Today.AddDays(100), DateTime.Today.AddDays(150)); Assert.IsFalse(a.Overlaps(b)); } } [TestFixture] public class When_calculating_the_percentage_elapsed { TimePeriod period; [SetUp] public void SetUp() { period = new TimePeriod(DateTime.Now, DateTime.Now.AddMonths(6)); } [Test] public void Should_be_zero_at_the_start() { Assert.AreEqual(0f, period.GetPercentageElapsedAsAt(period.Start)); } [Test] public void Should_be_one_hundred_percent_at_the_end() { Assert.AreEqual(1.0f, period.GetPercentageElapsedAsAt(period.End.Value)); } [Test] public void Should_be_zero_if_the_time_period_is_infinite() { Assert.AreEqual(0.0f, new TimePeriod(DateTime.Now.AddYears(-1), null).GetPercentageElapsed()); } } [TestFixture] public class When_calculating_time_slice_remaining { TimePeriod period; [SetUp] public void SetUp() { period = new TimePeriod(DateTime.Today, DateTime.Today.AddMonths(6)); } [Test] public void Should_be_six_months_at_the_start() { Assert.AreEqual(period, period.GetRemainingSliceAsAt(period.Start)); } [Test] public void Should_be_zero_days_and_time_at_the_end() { Assert.AreEqual(new TimePeriod(DateTime.Today.AddMonths(6), DateTime.Today.AddMonths(6)), period.GetRemainingSliceAsAt(period.End.Value)); } [Test] public void Should_return_null_if_the_date_is_past_the_end_of_the_time_period() { Assert.AreEqual(null, period.GetRemainingSliceAsAt(DateTime.Today.AddYears(1))); } }