Skip to content

Commit

Permalink
Fix DateTime extension 'CalculateAge'. Add new tests.
Browse files Browse the repository at this point in the history
Fix DateTime extension 'CalculateAge'.
Add new tests for 'AsNotNull' methods.
  • Loading branch information
I-RzR-I committed Jan 9, 2025
1 parent 99a4806 commit 4067f4f
Showing 1 changed file with 30 additions and 4 deletions.
34 changes: 30 additions & 4 deletions src/tests/DataTypeTests/DateTimeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
using System;
using DomainCommonExtensions.DataTypeExtensions;
using Microsoft.VisualStudio.TestTools.UnitTesting;
// ReSharper disable ArrangeObjectCreationWhenTypeEvident

#endregion

Expand Down Expand Up @@ -164,12 +165,13 @@ public void CalculateAgeTest()
var date = new DateTime(1994, 8, 8, 13, 0, 0);
var res = date.CalculateAge();

var age = DateTime.Now.Date.Day >= date.Day
? DateTime.Now.Year - 1994
: (DateTime.Now.Year - 1994) + 1;
var age = DateTime.Now.Year - date.Year;
if (DateTime.Now.Month < date.Month ||
DateTime.Now.Month == date.Month && DateTime.Now.Day < date.Day)
age -= 1;

Assert.IsNotNull(res);
Assert.IsTrue(res.Equals(age));
Assert.AreEqual(age, res);
}

[TestMethod]
Expand Down Expand Up @@ -288,5 +290,29 @@ public void ToTimeStampTest()

Assert.AreEqual(946684800, result);
}

[TestMethod]
public void AsNotNull_CurrentDate_Test()
{
DateTime? dt = null;

var newDt = dt.AsNotNull();

Assert.IsNull(dt);
Assert.IsNotNull(newDt);
Assert.AreEqual(newDt.Date, DateTime.Now.Date);
}

[TestMethod]
public void AsNotNull_CustomDate_Test()
{
DateTime? dt = null;
var customDt = new DateTime(2025, 12, 21);
var newDt = dt.AsNotNull(customDt);

Assert.IsNull(dt);
Assert.IsNotNull(newDt);
Assert.AreEqual(newDt, customDt);
}
}
}

0 comments on commit 4067f4f

Please sign in to comment.