diff options
author | Jesse Plamondon-Willard <github@jplamondonw.com> | 2017-07-10 22:10:27 -0400 |
---|---|---|
committer | Jesse Plamondon-Willard <github@jplamondonw.com> | 2017-07-10 22:10:27 -0400 |
commit | 674ad0d90f8780130a5fcefb3869acfe2315df2a (patch) | |
tree | eb79fa170a0b74a34061ea1d6838ae5589131c85 /src/StardewModdingAPI.Tests/Core | |
parent | 1edd98aef027faa768f56cf0b3591e64e20ba096 (diff) | |
parent | 834aee92f222fa739d7daa6c71f48b7e0df47ffe (diff) | |
download | SMAPI-674ad0d90f8780130a5fcefb3869acfe2315df2a.tar.gz SMAPI-674ad0d90f8780130a5fcefb3869acfe2315df2a.tar.bz2 SMAPI-674ad0d90f8780130a5fcefb3869acfe2315df2a.zip |
Merge branch 'develop' into stable
Diffstat (limited to 'src/StardewModdingAPI.Tests/Core')
-rw-r--r-- | src/StardewModdingAPI.Tests/Core/ModResolverTests.cs | 70 |
1 files changed, 53 insertions, 17 deletions
diff --git a/src/StardewModdingAPI.Tests/Core/ModResolverTests.cs b/src/StardewModdingAPI.Tests/Core/ModResolverTests.cs index b451465e..eda3a425 100644 --- a/src/StardewModdingAPI.Tests/Core/ModResolverTests.cs +++ b/src/StardewModdingAPI.Tests/Core/ModResolverTests.cs @@ -126,7 +126,7 @@ namespace StardewModdingAPI.Tests.Core public void ValidateManifests_Skips_Failed() { // arrange - Mock<IModMetadata> mock = new Mock<IModMetadata>(MockBehavior.Strict); + Mock<IModMetadata> mock = this.GetMetadata("Mod A"); mock.Setup(p => p.Status).Returns(ModMetadataStatus.Failed); // act @@ -140,10 +140,8 @@ namespace StardewModdingAPI.Tests.Core public void ValidateManifests_ModCompatibility_AssumeBroken_Fails() { // arrange - Mock<IModMetadata> mock = new Mock<IModMetadata>(MockBehavior.Strict); - mock.Setup(p => p.Status).Returns(ModMetadataStatus.Found); - mock.Setup(p => p.Compatibility).Returns(new ModCompatibility { Compatibility = ModCompatibilityType.AssumeBroken }); - mock.Setup(p => p.SetStatus(ModMetadataStatus.Failed, It.IsAny<string>())).Returns(() => mock.Object); + Mock<IModMetadata> mock = this.GetMetadata("Mod A", new string[0], allowStatusChange: true); + this.SetupMetadataForValidation(mock, new ModCompatibility { Compatibility = ModCompatibilityType.AssumeBroken }); // act new ModResolver().ValidateManifests(new[] { mock.Object }, apiVersion: new SemanticVersion("1.0")); @@ -156,11 +154,9 @@ namespace StardewModdingAPI.Tests.Core public void ValidateManifests_MinimumApiVersion_Fails() { // arrange - Mock<IModMetadata> mock = new Mock<IModMetadata>(MockBehavior.Strict); - mock.Setup(p => p.Status).Returns(ModMetadataStatus.Found); - mock.Setup(p => p.Compatibility).Returns(() => null); + Mock<IModMetadata> mock = this.GetMetadata("Mod A", new string[0], allowStatusChange: true); mock.Setup(p => p.Manifest).Returns(this.GetManifest(m => m.MinimumApiVersion = new SemanticVersion("1.1"))); - mock.Setup(p => p.SetStatus(ModMetadataStatus.Failed, It.IsAny<string>())).Returns(() => mock.Object); + this.SetupMetadataForValidation(mock); // act new ModResolver().ValidateManifests(new[] { mock.Object }, apiVersion: new SemanticVersion("1.0")); @@ -173,12 +169,8 @@ namespace StardewModdingAPI.Tests.Core public void ValidateManifests_MissingEntryDLL_Fails() { // arrange - Mock<IModMetadata> mock = new Mock<IModMetadata>(MockBehavior.Strict); - mock.Setup(p => p.Status).Returns(ModMetadataStatus.Found); - mock.Setup(p => p.Compatibility).Returns(() => null); - mock.Setup(p => p.Manifest).Returns(this.GetManifest()); - mock.Setup(p => p.DirectoryPath).Returns(Path.GetTempPath()); - mock.Setup(p => p.SetStatus(ModMetadataStatus.Failed, It.IsAny<string>())).Returns(() => mock.Object); + Mock<IModMetadata> mock = this.GetMetadata(this.GetManifest("Mod A", "1.0", manifest => manifest.EntryDll = "Missing.dll"), allowStatusChange: true); + this.SetupMetadataForValidation(mock); // act new ModResolver().ValidateManifests(new[] { mock.Object }, apiVersion: new SemanticVersion("1.0")); @@ -187,6 +179,26 @@ namespace StardewModdingAPI.Tests.Core mock.Verify(p => p.SetStatus(ModMetadataStatus.Failed, It.IsAny<string>()), Times.Once, "The validation did not fail the metadata."); } +#if !SMAPI_1_x + [Test(Description = "Assert that validation fails when multiple mods have the same unique ID.")] + public void ValidateManifests_DuplicateUniqueID_Fails() + { + // arrange + Mock<IModMetadata> modA = this.GetMetadata("Mod A", new string[0], allowStatusChange: true); + Mock<IModMetadata> modB = this.GetMetadata(this.GetManifest("Mod A", "1.0", manifest => manifest.Name = "Mod B"), allowStatusChange: true); + Mock<IModMetadata> modC = this.GetMetadata("Mod C", new string[0], allowStatusChange: false); + foreach (Mock<IModMetadata> mod in new[] { modA, modB, modC }) + this.SetupMetadataForValidation(mod); + + // act + new ModResolver().ValidateManifests(new[] { modA.Object, modB.Object }, apiVersion: new SemanticVersion("1.0")); + + // assert + modA.Verify(p => p.SetStatus(ModMetadataStatus.Failed, It.IsAny<string>()), Times.Once, "The validation did not fail the first mod with a unique ID."); + modB.Verify(p => p.SetStatus(ModMetadataStatus.Failed, It.IsAny<string>()), Times.Once, "The validation did not fail the second mod with a unique ID."); + } +#endif + [Test(Description = "Assert that validation fails when the manifest references a DLL that does not exist.")] public void ValidateManifests_Valid_Passes() { @@ -411,7 +423,7 @@ namespace StardewModdingAPI.Tests.Core Assert.AreSame(modB.Object, mods[1], "The load order is incorrect: mod B should be second since it needs mod A."); } -#if SMAPI_2_0 +#if !SMAPI_1_x [Test(Description = "Assert that optional dependencies are sorted correctly if present.")] public void ProcessDependencies_IfOptional() { @@ -469,8 +481,9 @@ namespace StardewModdingAPI.Tests.Core /// <summary>Get a randomised basic manifest.</summary> /// <param name="uniqueID">The mod's name and unique ID.</param> /// <param name="version">The mod version.</param> + /// <param name="adjust">Adjust the generated manifest.</param> /// <param name="dependencies">The dependencies this mod requires.</param> - private IManifest GetManifest(string uniqueID, string version, params IManifestDependency[] dependencies) + private IManifest GetManifest(string uniqueID, string version, Action<Manifest> adjust, params IManifestDependency[] dependencies) { return this.GetManifest(manifest => { @@ -478,11 +491,21 @@ namespace StardewModdingAPI.Tests.Core manifest.UniqueID = uniqueID; manifest.Version = new SemanticVersion(version); manifest.Dependencies = dependencies; + adjust?.Invoke(manifest); }); } /// <summary>Get a randomised basic manifest.</summary> /// <param name="uniqueID">The mod's name and unique ID.</param> + /// <param name="version">The mod version.</param> + /// <param name="dependencies">The dependencies this mod requires.</param> + private IManifest GetManifest(string uniqueID, string version, params IManifestDependency[] dependencies) + { + return this.GetManifest(uniqueID, version, null, dependencies); + } + + /// <summary>Get a randomised basic manifest.</summary> + /// <param name="uniqueID">The mod's name and unique ID.</param> private Mock<IModMetadata> GetMetadata(string uniqueID) { return this.GetMetadata(this.GetManifest(uniqueID, "1.0")); @@ -504,6 +527,7 @@ namespace StardewModdingAPI.Tests.Core private Mock<IModMetadata> GetMetadata(IManifest manifest, bool allowStatusChange = false) { Mock<IModMetadata> mod = new Mock<IModMetadata>(MockBehavior.Strict); + mod.Setup(p => p.Compatibility).Returns(() => null); mod.Setup(p => p.Status).Returns(ModMetadataStatus.Found); mod.Setup(p => p.DisplayName).Returns(manifest.UniqueID); mod.Setup(p => p.Manifest).Returns(manifest); @@ -516,5 +540,17 @@ namespace StardewModdingAPI.Tests.Core } return mod; } + + /// <summary>Set up a mock mod metadata for <see cref="ModResolver.ValidateManifests"/>.</summary> + /// <param name="mod">The mock mod metadata.</param> + /// <param name="compatibility">The compatibility record to set.</param> + private void SetupMetadataForValidation(Mock<IModMetadata> mod, ModCompatibility compatibility = null) + { + mod.Setup(p => p.Status).Returns(ModMetadataStatus.Found); + mod.Setup(p => p.Compatibility).Returns(() => null); + mod.Setup(p => p.Manifest).Returns(this.GetManifest()); + mod.Setup(p => p.DirectoryPath).Returns(Path.GetTempPath()); + mod.Setup(p => p.Compatibility).Returns(compatibility); + } } } |