1
0
mirror of https://github.com/oliverbooth/X10D synced 2024-11-22 19:48:46 +00:00

Add tests for DateTime/Offset

This commit is contained in:
Oliver Booth 2022-04-24 10:28:50 +01:00
parent 30e925cdb0
commit e2a496face
No known key found for this signature in database
GPG Key ID: 32A00B35503AF634
4 changed files with 156 additions and 2 deletions

View File

@ -0,0 +1,69 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using X10D.Time;
namespace X10D.Tests.Time;
[TestClass]
public class DateTimeOffsetTests
{
[TestMethod]
public void AgeShouldBeCorrect()
{
DateTimeOffset birthday = new DateTime(2000, 1, 1);
DateTimeOffset today = DateTime.Now.Date;
Assert.AreEqual(today.Year - birthday.Year, birthday.Age());
}
[TestMethod]
public void FirstShouldBeCorrect()
{
DateTimeOffset date = new DateTime(2000, 1, 1);
Assert.AreEqual(new DateTime(2000, 1, 1), date.First(DayOfWeek.Saturday));
Assert.AreEqual(new DateTime(2000, 1, 2), date.First(DayOfWeek.Sunday));
Assert.AreEqual(new DateTime(2000, 1, 3), date.First(DayOfWeek.Monday));
Assert.AreEqual(new DateTime(2000, 1, 4), date.First(DayOfWeek.Tuesday));
Assert.AreEqual(new DateTime(2000, 1, 5), date.First(DayOfWeek.Wednesday));
Assert.AreEqual(new DateTime(2000, 1, 6), date.First(DayOfWeek.Thursday));
Assert.AreEqual(new DateTime(2000, 1, 7), date.First(DayOfWeek.Friday));
}
[TestMethod]
public void FirstDayOfMonthShouldBeCorrect()
{
DateTimeOffset today = DateTime.Now.Date;
Assert.AreEqual(new DateTime(today.Year, today.Month, 1), today.FirstDayOfMonth());
}
[TestMethod]
public void LastShouldBeCorrect()
{
DateTimeOffset date = new DateTime(2000, 1, 1);
Assert.AreEqual(new DateTime(2000, 1, 29), date.Last(DayOfWeek.Saturday));
Assert.AreEqual(new DateTime(2000, 1, 30), date.Last(DayOfWeek.Sunday));
Assert.AreEqual(new DateTime(2000, 1, 31), date.Last(DayOfWeek.Monday));
Assert.AreEqual(new DateTime(2000, 1, 25), date.Last(DayOfWeek.Tuesday));
Assert.AreEqual(new DateTime(2000, 1, 26), date.Last(DayOfWeek.Wednesday));
Assert.AreEqual(new DateTime(2000, 1, 27), date.Last(DayOfWeek.Thursday));
Assert.AreEqual(new DateTime(2000, 1, 28), date.Last(DayOfWeek.Friday));
}
[TestMethod]
public void LastDayOfMonthShouldBeCorrect()
{
DateTimeOffset date = new DateTime(2000, 1, 1);
Assert.AreEqual(new DateTime(date.Year, date.Month, 31), date.LastDayOfMonth());
}
[TestMethod]
public void NextShouldBeCorrect()
{
DateTimeOffset date = new DateTime(2000, 1, 1);
Assert.AreEqual(new DateTime(2000, 1, 8), date.Next(DayOfWeek.Saturday));
}
}

View File

@ -0,0 +1,85 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using X10D.Time;
namespace X10D.Tests.Time;
[TestClass]
public class DateTimeTests
{
[TestMethod]
public void AgeShouldBeCorrect()
{
var birthday = new DateTime(2000, 1, 1);
DateTime today = DateTime.Now.Date;
Assert.AreEqual(today.Year - birthday.Year, birthday.Age());
}
[TestMethod]
public void FirstShouldBeCorrect()
{
var date = new DateTime(2000, 1, 1);
Assert.AreEqual(new DateTime(2000, 1, 1), date.First(DayOfWeek.Saturday));
Assert.AreEqual(new DateTime(2000, 1, 2), date.First(DayOfWeek.Sunday));
Assert.AreEqual(new DateTime(2000, 1, 3), date.First(DayOfWeek.Monday));
Assert.AreEqual(new DateTime(2000, 1, 4), date.First(DayOfWeek.Tuesday));
Assert.AreEqual(new DateTime(2000, 1, 5), date.First(DayOfWeek.Wednesday));
Assert.AreEqual(new DateTime(2000, 1, 6), date.First(DayOfWeek.Thursday));
Assert.AreEqual(new DateTime(2000, 1, 7), date.First(DayOfWeek.Friday));
}
[TestMethod]
public void FirstDayOfMonthShouldBeCorrect()
{
DateTime today = DateTime.Now.Date;
Assert.AreEqual(new DateTime(today.Year, today.Month, 1), today.FirstDayOfMonth());
}
[TestMethod]
public void LastShouldBeCorrect()
{
var date = new DateTime(2000, 1, 1);
Assert.AreEqual(new DateTime(2000, 1, 29), date.Last(DayOfWeek.Saturday));
Assert.AreEqual(new DateTime(2000, 1, 30), date.Last(DayOfWeek.Sunday));
Assert.AreEqual(new DateTime(2000, 1, 31), date.Last(DayOfWeek.Monday));
Assert.AreEqual(new DateTime(2000, 1, 25), date.Last(DayOfWeek.Tuesday));
Assert.AreEqual(new DateTime(2000, 1, 26), date.Last(DayOfWeek.Wednesday));
Assert.AreEqual(new DateTime(2000, 1, 27), date.Last(DayOfWeek.Thursday));
Assert.AreEqual(new DateTime(2000, 1, 28), date.Last(DayOfWeek.Friday));
}
[TestMethod]
public void LastDayOfMonthShouldBeCorrect()
{
var date = new DateTime(2000, 1, 1);
Assert.AreEqual(new DateTime(date.Year, date.Month, 31), date.LastDayOfMonth());
}
[TestMethod]
public void NextShouldBeCorrect()
{
var date = new DateTime(2000, 1, 1);
Assert.AreEqual(new DateTime(2000, 1, 8), date.Next(DayOfWeek.Saturday));
}
[TestMethod]
public void ToUnixTimeMillisecondsShouldBeCorrect()
{
var date = new DateTime(2000, 1, 1);
Assert.AreEqual(946684800000, date.ToUnixTimeMilliseconds());
}
[TestMethod]
public void ToUnixTimeSecondsShouldBeCorrect()
{
var date = new DateTime(2000, 1, 1);
Assert.AreEqual(946684800, date.ToUnixTimeSeconds());
}
}

View File

@ -1,4 +1,4 @@
namespace X10D;
namespace X10D.Time;
/// <summary>
/// Extension methods for <see cref="DateTime" />.

View File

@ -1,4 +1,4 @@
namespace X10D;
namespace X10D.Time;
/// <summary>
/// Extension methods for <see cref="DateTimeOffset" />.