diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/SMAPI.Common/SemanticVersionImpl.cs | 13 | ||||
-rw-r--r-- | src/SMAPI.Tests/Utilities/SemanticVersionTests.cs | 20 | ||||
-rw-r--r-- | src/SMAPI/SemanticVersion.cs | 6 |
3 files changed, 37 insertions, 2 deletions
diff --git a/src/SMAPI.Common/SemanticVersionImpl.cs b/src/SMAPI.Common/SemanticVersionImpl.cs index 193d23f9..53cf5a21 100644 --- a/src/SMAPI.Common/SemanticVersionImpl.cs +++ b/src/SMAPI.Common/SemanticVersionImpl.cs @@ -49,6 +49,19 @@ namespace StardewModdingAPI.Common } /// <summary>Construct an instance.</summary> + /// <param name="version">The assembly version.</param> + /// <exception cref="ArgumentNullException">The <paramref name="version"/> is null.</exception> + public SemanticVersionImpl(Version version) + { + if (version == null) + throw new ArgumentNullException(nameof(version), "The input version can't be null."); + + this.Major = version.Major; + this.Minor = version.Minor; + this.Patch = version.Build; + } + + /// <summary>Construct an instance.</summary> /// <param name="version">The semantic version string.</param> /// <exception cref="ArgumentNullException">The <paramref name="version"/> is null.</exception> /// <exception cref="FormatException">The <paramref name="version"/> is not a valid semantic version.</exception> diff --git a/src/SMAPI.Tests/Utilities/SemanticVersionTests.cs b/src/SMAPI.Tests/Utilities/SemanticVersionTests.cs index 73ecd56e..d3e0988e 100644 --- a/src/SMAPI.Tests/Utilities/SemanticVersionTests.cs +++ b/src/SMAPI.Tests/Utilities/SemanticVersionTests.cs @@ -16,7 +16,7 @@ namespace StardewModdingAPI.Tests.Utilities /**** ** Constructor ****/ - [Test(Description = "Assert that the constructor sets the expected values for all valid versions.")] + [Test(Description = "Assert that the constructor sets the expected values for all valid versions when constructed from a string.")] [TestCase("1.0", ExpectedResult = "1.0")] [TestCase("1.0.0", ExpectedResult = "1.0")] [TestCase("3000.4000.5000", ExpectedResult = "3000.4000.5000")] @@ -28,7 +28,7 @@ namespace StardewModdingAPI.Tests.Utilities return new SemanticVersion(input).ToString(); } - [Test(Description = "Assert that the constructor sets the expected values for all valid versions.")] + [Test(Description = "Assert that the constructor sets the expected values for all valid versions when constructed from the individual numbers.")] [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")] @@ -48,6 +48,22 @@ namespace StardewModdingAPI.Tests.Utilities return version.ToString(); } + [Test(Description = "Assert that the constructor sets the expected values for all valid versions when constructed from an assembly version.")] + [TestCase(1, 0, 0, ExpectedResult = "1.0")] + [TestCase(1, 2, 3, ExpectedResult = "1.2.3")] + [TestCase(3000, 4000, 5000, ExpectedResult = "3000.4000.5000")] + public string Constructor_FromAssemblyVersion(int major, int minor, int patch) + { + // act + ISemanticVersion version = new SemanticVersion(new Version(major, minor, patch)); + + // 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."); + return version.ToString(); + } + [Test(Description = "Assert that the constructor throws the expected exception for invalid versions.")] [TestCase(null)] [TestCase("")] diff --git a/src/SMAPI/SemanticVersion.cs b/src/SMAPI/SemanticVersion.cs index ce86dceb..4826c947 100644 --- a/src/SMAPI/SemanticVersion.cs +++ b/src/SMAPI/SemanticVersion.cs @@ -49,6 +49,12 @@ namespace StardewModdingAPI public SemanticVersion(string version) : this(new SemanticVersionImpl(version)) { } + /// <summary>Construct an instance.</summary> + /// <param name="version">The assembly version.</param> + /// <exception cref="ArgumentNullException">The <paramref name="version"/> is null.</exception> + public SemanticVersion(Version version) + : this(new SemanticVersionImpl(version)) { } + /// <summary>Get an integer indicating whether this version precedes (less than 0), supercedes (more than 0), or is equivalent to (0) the specified version.</summary> /// <param name="other">The version to compare with this instance.</param> /// <exception cref="ArgumentNullException">The <paramref name="other"/> value is null.</exception> |