using System;
using System.Diagnostics.CodeAnalysis;
using NUnit.Framework;
namespace StardewModdingAPI.Tests.Utilities
{
/// Unit tests for .
[TestFixture]
internal class SemanticVersionTests
{
/*********
** Unit tests
*********/
/****
** Constructor
****/
[Test(Description = "Assert that the constructor sets the expected values for all valid versions.")]
[TestCase("1.0", ExpectedResult = "1.0")]
[TestCase("1.0.0", ExpectedResult = "1.0")]
[TestCase("3000.4000.5000", ExpectedResult = "3000.4000.5000")]
[TestCase("1.2-some-tag.4", ExpectedResult = "1.2-some-tag.4")]
[TestCase("1.2.3-some-tag.4", ExpectedResult = "1.2.3-some-tag.4")]
[TestCase("1.2.3-some-tag.4 ", ExpectedResult = "1.2.3-some-tag.4")]
public string Constructor_FromString(string input)
{
return new SemanticVersion(input).ToString();
}
[Test(Description = "Assert that the constructor sets the expected values for all valid versions.")]
[TestCase(1, 0, 0, null, ExpectedResult = "1.0")]
[TestCase(3000, 4000, 5000, null, ExpectedResult = "3000.4000.5000")]
[TestCase(1, 2, 3, "", ExpectedResult = "1.2.3")]
[TestCase(1, 2, 3, " ", ExpectedResult = "1.2.3")]
[TestCase(1, 2, 3, "some-tag.4", ExpectedResult = "1.2.3-some-tag.4")]
[TestCase(1, 2, 3, "some-tag.4 ", ExpectedResult = "1.2.3-some-tag.4")]
public string Constructor_FromParts(int major, int minor, int patch, string tag)
{
// act
ISemanticVersion version = new SemanticVersion(major, minor, patch, tag);
// assert
Assert.AreEqual(major, version.MajorVersion, "The major version doesn't match the given value.");
Assert.AreEqual(minor, version.MinorVersion, "The minor version doesn't match the given value.");
Assert.AreEqual(patch, version.PatchVersion, "The patch version doesn't match the given value.");
Assert.AreEqual(string.IsNullOrWhiteSpace(tag) ? null : tag.Trim(), version.Build, "The tag doesn't match the given value.");
return version.ToString();
}
[Test(Description = "Assert that the constructor throws the expected exception for invalid versions.")]
[TestCase(null)]
[TestCase("")]
[TestCase(" ")]
[TestCase("1")]
[TestCase("01.0")]
[TestCase("1.05")]
[TestCase("1.5.06")] // leading zeros specifically prohibited by spec
[TestCase("1.2.3.4")]
[TestCase("1.apple")]
[TestCase("1.2.apple")]
[TestCase("1.2.3.apple")]
[TestCase("1..2..3")]
[TestCase("1.2.3-")]
[TestCase("1.2.3-some-tag...")]
[TestCase("1.2.3-some-tag...4")]
[TestCase("apple")]
[TestCase("-apple")]
[TestCase("-5")]
public void Constructor_FromString_WithInvalidValues(string input)
{
if (input == null)
this.AssertAndLogException(() => new SemanticVersion(input));
else
this.AssertAndLogException(() => new SemanticVersion(input));
}
/****
** CompareTo
****/
[Test(Description = "Assert that version.CompareTo returns the expected value.")]
// equal
[TestCase("0.5.7", "0.5.7", ExpectedResult = 0)]
[TestCase("1.0", "1.0", ExpectedResult = 0)]
[TestCase("1.0-beta", "1.0-beta", ExpectedResult = 0)]
[TestCase("1.0-beta.10", "1.0-beta.10", ExpectedResult = 0)]
[TestCase("1.0-beta", "1.0-beta ", ExpectedResult = 0)]
// less than
[TestCase("0.5.7", "0.5.8", ExpectedResult = -1)]
[TestCase("1.0", "1.1", ExpectedResult = -1)]
[TestCase("1.0-beta", "1.0", ExpectedResult = -1)]
[TestCase("1.0-beta", "1.0-beta.2", ExpectedResult = -1)]
[TestCase("1.0-beta.1", "1.0-beta.2", ExpectedResult = -1)]
[TestCase("1.0-beta.2", "1.0-beta.10", ExpectedResult = -1)]
[TestCase("1.0-beta-2", "1.0-beta-10", ExpectedResult = -1)]
// more than
[TestCase("0.5.8", "0.5.7", ExpectedResult = 1)]
[TestCase("1.1", "1.0", ExpectedResult = 1)]
[TestCase("1.0", "1.0-beta", ExpectedResult = 1)]
[TestCase("1.0-beta.2", "1.0-beta", ExpectedResult = 1)]
[TestCase("1.0-beta.2", "1.0-beta.1", ExpectedResult = 1)]
[TestCase("1.0-beta.10", "1.0-beta.2", ExpectedResult = 1)]
[TestCase("1.0-beta-10", "1.0-beta-2", ExpectedResult = 1)]
public int CompareTo(string versionStrA, string versionStrB)
{
ISemanticVersion versionA = new SemanticVersion(versionStrA);
ISemanticVersion versionB = new SemanticVersion(versionStrB);
return versionA.CompareTo(versionB);
}
/****
** IsOlderThan
****/
[Test(Description = "Assert that version.IsOlderThan returns the expected value.")]
// keep test cases in sync with CompareTo for simplicity.
// equal
[TestCase("0.5.7", "0.5.7", ExpectedResult = false)]
[TestCase("1.0", "1.0", ExpectedResult = false)]
[TestCase("1.0-beta", "1.0-beta", ExpectedResult = false)]
[TestCase("1.0-beta.10", "1.0-beta.10", ExpectedResult = false)]
[TestCase("1.0-beta", "1.0-beta ", ExpectedResult = false)]
// less than
[TestCase("0.5.7", "0.5.8", ExpectedResult = true)]
[TestCase("1.0", "1.1", ExpectedResult = true)]
[TestCase("1.0-beta", "1.0", ExpectedResult = true)]
[TestCase("1.0-beta", "1.0-beta.2", ExpectedResult = true)]
[TestCase("1.0-beta.1", "1.0-beta.2", ExpectedResult = true)]
[TestCase("1.0-beta.2", "1.0-beta.10", ExpectedResult = true)]
[TestCase("1.0-beta-2", "1.0-beta-10", ExpectedResult = true)]
// more than
[TestCase("0.5.8", "0.5.7", ExpectedResult = false)]
[TestCase("1.1", "1.0", ExpectedResult = false)]
[TestCase("1.0", "1.0-beta", ExpectedResult = false)]
[TestCase("1.0-beta.2", "1.0-beta", ExpectedResult = false)]
[TestCase("1.0-beta.2", "1.0-beta.1", ExpectedResult = false)]
[TestCase("1.0-beta.10", "1.0-beta.2", ExpectedResult = false)]
[TestCase("1.0-beta-10", "1.0-beta-2", ExpectedResult = false)]
public bool IsOlderThan(string versionStrA, string versionStrB)
{
ISemanticVersion versionA = new SemanticVersion(versionStrA);
ISemanticVersion versionB = new SemanticVersion(versionStrB);
return versionA.IsOlderThan(versionB);
}
/****
** IsNewerThan
****/
[Test(Description = "Assert that version.IsNewerThan returns the expected value.")]
// keep test cases in sync with CompareTo for simplicity.
// equal
[TestCase("0.5.7", "0.5.7", ExpectedResult = false)]
[TestCase("1.0", "1.0", ExpectedResult = false)]
[TestCase("1.0-beta", "1.0-beta", ExpectedResult = false)]
[TestCase("1.0-beta.10", "1.0-beta.10", ExpectedResult = false)]
[TestCase("1.0-beta", "1.0-beta ", ExpectedResult = false)]
// less than
[TestCase("0.5.7", "0.5.8", ExpectedResult = false)]
[TestCase("1.0", "1.1", ExpectedResult = false)]
[TestCase("1.0-beta", "1.0", ExpectedResult = false)]
[TestCase("1.0-beta", "1.0-beta.2", ExpectedResult = false)]
[TestCase("1.0-beta.1", "1.0-beta.2", ExpectedResult = false)]
[TestCase("1.0-beta.2", "1.0-beta.10", ExpectedResult = false)]
[TestCase("1.0-beta-2", "1.0-beta-10", ExpectedResult = false)]
// more than
[TestCase("0.5.8", "0.5.7", ExpectedResult = true)]
[TestCase("1.1", "1.0", ExpectedResult = true)]
[TestCase("1.0", "1.0-beta", ExpectedResult = true)]
[TestCase("1.0-beta.2", "1.0-beta", ExpectedResult = true)]
[TestCase("1.0-beta.2", "1.0-beta.1", ExpectedResult = true)]
[TestCase("1.0-beta.10", "1.0-beta.2", ExpectedResult = true)]
[TestCase("1.0-beta-10", "1.0-beta-2", ExpectedResult = true)]
public bool IsNewerThan(string versionStrA, string versionStrB)
{
ISemanticVersion versionA = new SemanticVersion(versionStrA);
ISemanticVersion versionB = new SemanticVersion(versionStrB);
return versionA.IsNewerThan(versionB);
}
/****
** IsBetween
****/
[Test(Description = "Assert that version.IsNewerThan returns the expected value.")]
// is between
[TestCase("0.5.7-beta.3", "0.5.7-beta.3", "0.5.7-beta.3", ExpectedResult = true)]
[TestCase("1.0", "1.0", "1.1", ExpectedResult = true)]
[TestCase("1.0", "1.0-beta", "1.1", ExpectedResult = true)]
[TestCase("1.0", "0.5", "1.1", ExpectedResult = true)]
[TestCase("1.0-beta.2", "1.0-beta.1", "1.0-beta.3", ExpectedResult = true)]
[TestCase("1.0-beta-2", "1.0-beta-1", "1.0-beta-3", ExpectedResult = true)]
// is not between
[TestCase("1.0-beta", "1.0", "1.1", ExpectedResult = false)]
[TestCase("1.0", "1.1", "1.0", ExpectedResult = false)]
[TestCase("1.0-beta.2", "1.1", "1.0", ExpectedResult = false)]
[TestCase("1.0-beta.2", "1.0-beta.10", "1.0-beta.3", ExpectedResult = false)]
[TestCase("1.0-beta-2", "1.0-beta-10", "1.0-beta-3", ExpectedResult = false)]
public bool IsBetween(string versionStr, string lowerStr, string upperStr)
{
ISemanticVersion lower = new SemanticVersion(lowerStr);
ISemanticVersion upper = new SemanticVersion(upperStr);
ISemanticVersion version = new SemanticVersion(versionStr);
return version.IsBetween(lower, upper);
}
/*********
** Private methods
*********/
/// Assert that the expected exception type is thrown, and log the action output and thrown exception.
/// The expected exception type.
/// The action which may throw the exception.
/// The message to log if the expected exception isn't thrown.
[SuppressMessage("ReSharper", "UnusedParameter.Local", Justification = "The message argument is deliberately only used in precondition checks since this is an assertion method.")]
private void AssertAndLogException(Func