- This adds in operators to SDate. And Tests. And a NUnit Adapter - sorry about the latter..

This commit is contained in:
Nicholas Johnson 2017-06-16 03:47:36 -07:00 committed by Jesse Plamondon-Willard
parent 3c3953a7fd
commit 230ab1738a
4 changed files with 195 additions and 0 deletions

View File

@ -83,6 +83,47 @@ namespace StardewModdingAPI.Tests
return this.ParseDate(dateStr).AddDays(addDays).ToString();
}
[Test(Description = "Assert that the equality operators work as expected")]
public void EqualityOperators()
{
SDate s1 = new SDate(1, "spring", 2);
SDate s2 = new SDate(1, "spring", 2);
SDate s3 = new SDate(1, "spring", 3);
SDate s4 = new SDate(12, "spring", 2);
SDate s5 = new SDate(1, "summer", 2);
Assert.AreEqual(true, s1 == s2);
Assert.AreNotEqual(true, s1 == s3);
Assert.AreNotEqual(true, s1 == s4);
Assert.AreNotEqual(true, s1 == s5);
}
[Test(Description = "Assert that the comparison operators work as expected")]
public void ComparisonOperators()
{
SDate s1 = new SDate(1, "spring", 2);
SDate s2 = new SDate(1, "spring", 2);
SDate s3 = new SDate(1, "spring", 3);
SDate s4 = new SDate(12, "spring", 2);
SDate s5 = new SDate(1, "summer", 2);
SDate s6 = new SDate(1, "winter", 1);
SDate s7 = new SDate(13, "fall", 1);
Assert.AreEqual(true, s1 <= s2);
Assert.AreEqual(true, s1 >= s2);
Assert.AreEqual(true, s1 < s4);
Assert.AreEqual(true, s1 <= s4);
Assert.AreEqual(true, s4 > s1);
Assert.AreEqual(true, s4 >= s1);
Assert.AreEqual(true, s5 > s7);
Assert.AreEqual(true, s5 >= s7);
Assert.AreEqual(true, s6 < s5);
Assert.AreEqual(true, s6 <= s5);
Assert.AreEqual(true, s1 < s5);
Assert.AreEqual(true, s1 <= s5);
Assert.AreEqual(true, s5 > s1);
Assert.AreEqual(true, s5 >= s1);
}
/*********
** Private methods

View File

@ -63,6 +63,9 @@
<Name>StardewModdingAPI</Name>
</ProjectReference>
</ItemGroup>
<ItemGroup>
<Service Include="{82A7F48D-3B50-4B1E-B82E-3ADA8210C358}" />
</ItemGroup>
<Import Project="$(SolutionDir)\crossplatform.targets" />
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>

View File

@ -4,4 +4,5 @@
<package id="Moq" version="4.7.10" targetFramework="net45" />
<package id="Newtonsoft.Json" version="8.0.3" targetFramework="net45" />
<package id="NUnit" version="3.6.1" targetFramework="net452" />
<package id="NUnit3TestAdapter" version="3.7.0" targetFramework="net45" />
</packages>

View File

@ -113,6 +113,156 @@ namespace StardewModdingAPI.Utilities
return new SDate(Game1.dayOfMonth, Game1.currentSeason, Game1.year);
}
/*********
** Operator methods
*********/
/// <summary>
/// Equality operator. Tests the date being equal to each other
/// </summary>
/// <param name="s1">The first date being compared</param>
/// <param name="s2">The second date being compared</param>
/// <returns>The equality of the dates</returns>
public static bool operator ==(SDate s1, SDate s2)
{
if (s1.Day == s2.Day && s1.Year == s2.Year && s1.Season == s2.Season)
return true;
else
return false;
}
/// <summary>
/// Inequality operator. Tests the date being not equal to each other
/// </summary>
/// <param name="s1">The first date being compared</param>
/// <param name="s2">The second date being compared</param>
/// <returns>The inequality of the dates</returns>
public static bool operator !=(SDate s1, SDate s2)
{
if (s1.Day == s2.Day && s1.Year == s2.Year && s1.Season == s2.Season)
return false;
else
return true;
}
/// <summary>
/// Less than operator. Tests the date being less than to each other
/// </summary>
/// <param name="s1">The first date being compared</param>
/// <param name="s2">The second date being compared</param>
/// <returns>If the dates are less than</returns>
public static bool operator >(SDate s1, SDate s2)
{
if (s1.Year > s2.Year)
return true;
else if (s1.Year == s2.Year)
{
if (s1.Season == "winter" && s2.Season != "winter")
return true;
else if (s1.Season == s2.Season && s1.Day > s2.Day)
return true;
if (s1.Season == "fall" && (s2.Season == "summer" || s2.Season == "spring"))
return true;
if (s1.Season == "summer" && s2.Season == "spring")
return true;
}
return false;
}
/// <summary>
/// Less or equal than operator. Tests the date being less than or equal to each other
/// </summary>
/// <param name="s1">The first date being compared</param>
/// <param name="s2">The second date being compared</param>
/// <returns>If the dates are less than or equal than</returns>
public static bool operator >=(SDate s1, SDate s2)
{
if (s1.Year > s2.Year)
return true;
else if (s1.Year == s2.Year)
{
if (s1.Season == "winter" && s2.Season != "winter")
return true;
else if (s1.Season == s2.Season && s1.Day >= s2.Day)
return true;
if (s1.Season == "fall" && (s2.Season == "summer" || s2.Season == "spring"))
return true;
if (s1.Season == "summer" && s2.Season == "spring")
return true;
}
return false;
}
/// <summary>
/// Greater or equal than operator. Tests the date being greater than or equal to each other
/// </summary>
/// <param name="s1">The first date being compared</param>
/// <param name="s2">The second date being compared</param>
/// <returns>If the dates are greater than or equal than</returns>
public static bool operator <=(SDate s1, SDate s2)
{
if (s1.Year < s2.Year)
return true;
else if (s1.Year == s2.Year)
{
if (s1.Season == s2.Season && s1.Day <= s2.Day)
return true;
else if (s1.Season == "spring" && s2.Season != "spring")
return true;
if (s1.Season == "summer" && (s2.Season == "fall" || s2.Season == "winter"))
return true;
if (s1.Season == "fall" && s2.Season == "winter")
return true;
}
return false;
}
/// <summary>
/// Greater than operator. Tests the date being greater than to each other
/// </summary>
/// <param name="s1">The first date being compared</param>
/// <param name="s2">The second date being compared</param>
/// <returns>If the dates are greater than</returns>
public static bool operator <(SDate s1, SDate s2)
{
if (s1.Year < s2.Year)
return true;
else if (s1.Year == s2.Year)
{
if (s1.Season == s2.Season && s1.Day < s2.Day)
return true;
else if (s1.Season == "spring" && s2.Season != "spring")
return true;
if (s1.Season == "summer" && (s2.Season == "fall" || s2.Season == "winter"))
return true;
if (s1.Season == "fall" && s2.Season == "winter")
return true;
}
return false;
}
/// <summary>
/// Overrides the equals function.
/// </summary>
/// <param name="obj">Object being compared.</param>
/// <returns>The equalaity of the object.</returns>
public override bool Equals(object obj)
{
return base.Equals(obj);
}
/// <summary>
/// This returns the hashcode of the object
/// </summary>
/// <returns>The hashcode of the object.</returns>
public override int GetHashCode()
{
return base.GetHashCode();
}
/*********
** Private methods