Skip to content

Commit

Permalink
Add more unit tests for DateTimeOrHuman
Browse files Browse the repository at this point in the history
  • Loading branch information
filipetoscano committed Feb 10, 2025
1 parent 85b1241 commit 329f27d
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/Resend/DateTimeOrHuman.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public override string ToString()
public int CompareTo( DateTimeOrHuman other )
{
if ( this.IsMoment != other.IsMoment )
throw new NotSupportedException( "Cannot compare DateTimeOrHuman values of different kind" );
throw new NotSupportedException( "DH001: Cannot compare DateTimeOrHuman values of different kind" );

if ( this.IsMoment == true )
return this.Moment!.Value.CompareTo( other.Moment!.Value );
Expand All @@ -65,7 +65,7 @@ public static implicit operator DateTime( DateTimeOrHuman value )
if ( value.IsMoment == true )
return value.Moment!.Value;

throw new InvalidOperationException( "Value is not a DateTime" );
throw new InvalidOperationException( "DH002: Value is not a DateTime" );
}


Expand Down
97 changes: 97 additions & 0 deletions tests/Resend.Tests/DateTimeOrHumanTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,103 @@ public class WrapperClass
}


/// <summary />
[Fact]
public void DateTimeToString()
{
var dt = DateTime.UtcNow;
DateTimeOrHuman src = dt;

Assert.Equal( dt.ToString(), src.ToString() );
}


/// <summary />
[Fact]
public void DateTimeCast()
{
var dt = DateTime.UtcNow;
DateTimeOrHuman src = dt;
DateTime tgt = src;

Assert.Equal( dt, tgt );
}


/// <summary />
[Fact]
public void DateTimeCastFromHuman()
{
DateTimeOrHuman src = "in 5 mins";

Action act = () =>
{
DateTime tgt = src;
};

var ex = Assert.Throws<InvalidOperationException>( act );
Assert.NotNull( ex.Message );
Assert.StartsWith( "DH002:", ex.Message );
}


/// <summary />
[Fact]
public void DateTimeComparable()
{
var dt1 = DateTime.UtcNow;
var dt2 = DateTime.UtcNow.AddDays( 1 );

DateTimeOrHuman src1 = dt1;
DateTimeOrHuman src2 = dt2;

Assert.Equal( dt1.CompareTo( dt2 ), src1.CompareTo( src2 ) );
}


/// <summary />
[Fact]
public void HumanToString()
{
var str = "in 5 mins";
DateTimeOrHuman v = str;

Assert.Equal( str, v.ToString() );
}


/// <summary />
[Fact]
public void HumanComparable()
{
var str1 = "in 5 mins";
var str2 = "tomorrow 5:00 am";

DateTimeOrHuman v1 = str1;
DateTimeOrHuman v2 = str2;

Assert.Equal( str1.CompareTo( str2 ), v1.CompareTo( v2 ) );
}


/// <summary />
[Fact]
public void MixedComparable()
{
var str = "in 5 mins";
var dt = DateTime.UtcNow;

DateTimeOrHuman v1 = str;
DateTimeOrHuman v2 = dt;

Action act = () => v1.CompareTo( v2 );

var ex = Assert.Throws<NotSupportedException>( act );
Assert.NotNull( ex.Message );
Assert.StartsWith( "DH001:", ex.Message );
}


/// <summary />
[Fact]
public void PropertyNotNull()
Expand Down

0 comments on commit 329f27d

Please sign in to comment.