From 7bf0c660888ab3082f95b83226a6a55c08b62959 Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Mon, 10 Jul 2017 13:25:19 -0400 Subject: [PATCH] reduce duplicate code in unit tests --- .../Core/ModResolverTests.cs | 48 ++++++++++++------- 1 file changed, 32 insertions(+), 16 deletions(-) diff --git a/src/StardewModdingAPI.Tests/Core/ModResolverTests.cs b/src/StardewModdingAPI.Tests/Core/ModResolverTests.cs index b451465e..e6ec632a 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 mock = new Mock(MockBehavior.Strict); + Mock 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 mock = new Mock(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())).Returns(() => mock.Object); + Mock 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 mock = new Mock(MockBehavior.Strict); - mock.Setup(p => p.Status).Returns(ModMetadataStatus.Found); - mock.Setup(p => p.Compatibility).Returns(() => null); + Mock 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())).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 mock = new Mock(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())).Returns(() => mock.Object); + Mock 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")); @@ -469,8 +461,9 @@ namespace StardewModdingAPI.Tests.Core /// Get a randomised basic manifest. /// The mod's name and unique ID. /// The mod version. + /// Adjust the generated manifest. /// The dependencies this mod requires. - private IManifest GetManifest(string uniqueID, string version, params IManifestDependency[] dependencies) + private IManifest GetManifest(string uniqueID, string version, Action adjust, params IManifestDependency[] dependencies) { return this.GetManifest(manifest => { @@ -478,9 +471,19 @@ namespace StardewModdingAPI.Tests.Core manifest.UniqueID = uniqueID; manifest.Version = new SemanticVersion(version); manifest.Dependencies = dependencies; + adjust?.Invoke(manifest); }); } + /// Get a randomised basic manifest. + /// The mod's name and unique ID. + /// The mod version. + /// The dependencies this mod requires. + private IManifest GetManifest(string uniqueID, string version, params IManifestDependency[] dependencies) + { + return this.GetManifest(uniqueID, version, null, dependencies); + } + /// Get a randomised basic manifest. /// The mod's name and unique ID. private Mock GetMetadata(string uniqueID) @@ -504,6 +507,7 @@ namespace StardewModdingAPI.Tests.Core private Mock GetMetadata(IManifest manifest, bool allowStatusChange = false) { Mock mod = new Mock(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 +520,17 @@ namespace StardewModdingAPI.Tests.Core } return mod; } + + /// Set up a mock mod metadata for . + /// The mock mod metadata. + /// The compatibility record to set. + private void SetupMetadataForValidation(Mock 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); + } } }