From da6844d85f8bb372501499db83fef271c1dd42c4 Mon Sep 17 00:00:00 2001 From: Arman Ghazanchyan Date: Sun, 26 Sep 2021 20:51:03 -0700 Subject: [PATCH] Refactoring --- Tests/AgeTests.cs | 23 +++++++++-------------- 1 file changed, 9 insertions(+), 14 deletions(-) diff --git a/Tests/AgeTests.cs b/Tests/AgeTests.cs index e2e1736..c893a90 100644 --- a/Tests/AgeTests.cs +++ b/Tests/AgeTests.cs @@ -18,21 +18,19 @@ public AgeTests(ITestOutputHelper output) [Theory] [InlineData("01/05/2021", "01/03/2021")] [InlineData("02/05/2021 7:28:12", "02/05/2021 6:30:15")] - public void InvalidDates(string fromDate, string toDate) + public void InvalidDates(DateTime fromDate, DateTime toDate) { const string paramName = "fromDate"; - var dob = DateTime.Parse(fromDate); - var endDate = DateTime.Parse(toDate); _output.WriteLine("Should throw an exception."); // Test initialization method - Assert.Throws(paramName, () => new Age(dob, endDate)); + Assert.Throws(paramName, () => new Age(fromDate, toDate)); // Test type's static method - Assert.Throws(paramName, () => Age.Calculate(dob, endDate)); + Assert.Throws(paramName, () => Age.Calculate(fromDate, toDate)); // Test DateTime extension method - Assert.Throws(paramName, () => dob.CalculateAge(endDate)); + Assert.Throws(paramName, () => fromDate.CalculateAge(toDate)); } [Theory] @@ -141,8 +139,8 @@ public void InvalidDates(string fromDate, string toDate) [InlineData("02/29/2016", "02/28/2021 00:00:01", 5, 0, 0, 0, 0, 1, true)] [InlineData("02/29/2016 00:00:02", "02/28/2021 00:00:01", 4, 11, 27, 23, 59, 59, true)] public void CalculateAge( - string fromDate, - string toDate, + DateTime fromDate, + DateTime toDate, int expectedYears, int expectedMonths, int expectedDays, @@ -151,12 +149,9 @@ public void CalculateAge( int? expectedSeconds = null, bool isFeb28AYearCycleForLeapling = false) { - var dob = DateTime.Parse(fromDate); - var endDate = DateTime.Parse(toDate); - var age = dob.CalculateAge(endDate, isFeb28AYearCycleForLeapling); - - _output.WriteLine($"{dob:MM/dd/yyyy HH:mm:ss}:{GetLOrNYear(dob)} - {endDate:MM/dd/yyyy HH:mm:ss}:{GetLOrNYear(endDate)}"); - if (DateTime.IsLeapYear(dob.Year)) + var age = fromDate.CalculateAge(toDate, isFeb28AYearCycleForLeapling); + _output.WriteLine($"{fromDate:MM/dd/yyyy HH:mm:ss}:{GetLOrNYear(fromDate)} - {toDate:MM/dd/yyyy HH:mm:ss}:{GetLOrNYear(toDate)}"); + if (DateTime.IsLeapYear(fromDate.Year)) { _output.WriteLine($"{nameof(isFeb28AYearCycleForLeapling)}: {isFeb28AYearCycleForLeapling}"); }