reduce duplicate code in unit tests
This commit is contained in:
parent
49edb712ec
commit
7bf0c66088
|
@ -126,7 +126,7 @@ namespace StardewModdingAPI.Tests.Core
|
||||||
public void ValidateManifests_Skips_Failed()
|
public void ValidateManifests_Skips_Failed()
|
||||||
{
|
{
|
||||||
// arrange
|
// arrange
|
||||||
Mock<IModMetadata> mock = new Mock<IModMetadata>(MockBehavior.Strict);
|
Mock<IModMetadata> mock = this.GetMetadata("Mod A");
|
||||||
mock.Setup(p => p.Status).Returns(ModMetadataStatus.Failed);
|
mock.Setup(p => p.Status).Returns(ModMetadataStatus.Failed);
|
||||||
|
|
||||||
// act
|
// act
|
||||||
|
@ -140,10 +140,8 @@ namespace StardewModdingAPI.Tests.Core
|
||||||
public void ValidateManifests_ModCompatibility_AssumeBroken_Fails()
|
public void ValidateManifests_ModCompatibility_AssumeBroken_Fails()
|
||||||
{
|
{
|
||||||
// arrange
|
// arrange
|
||||||
Mock<IModMetadata> mock = new Mock<IModMetadata>(MockBehavior.Strict);
|
Mock<IModMetadata> mock = this.GetMetadata("Mod A", new string[0], allowStatusChange: true);
|
||||||
mock.Setup(p => p.Status).Returns(ModMetadataStatus.Found);
|
this.SetupMetadataForValidation(mock, new ModCompatibility { Compatibility = ModCompatibilityType.AssumeBroken });
|
||||||
mock.Setup(p => p.Compatibility).Returns(new ModCompatibility { Compatibility = ModCompatibilityType.AssumeBroken });
|
|
||||||
mock.Setup(p => p.SetStatus(ModMetadataStatus.Failed, It.IsAny<string>())).Returns(() => mock.Object);
|
|
||||||
|
|
||||||
// act
|
// act
|
||||||
new ModResolver().ValidateManifests(new[] { mock.Object }, apiVersion: new SemanticVersion("1.0"));
|
new ModResolver().ValidateManifests(new[] { mock.Object }, apiVersion: new SemanticVersion("1.0"));
|
||||||
|
@ -156,11 +154,9 @@ namespace StardewModdingAPI.Tests.Core
|
||||||
public void ValidateManifests_MinimumApiVersion_Fails()
|
public void ValidateManifests_MinimumApiVersion_Fails()
|
||||||
{
|
{
|
||||||
// arrange
|
// arrange
|
||||||
Mock<IModMetadata> mock = new Mock<IModMetadata>(MockBehavior.Strict);
|
Mock<IModMetadata> mock = this.GetMetadata("Mod A", new string[0], allowStatusChange: true);
|
||||||
mock.Setup(p => p.Status).Returns(ModMetadataStatus.Found);
|
|
||||||
mock.Setup(p => p.Compatibility).Returns(() => null);
|
|
||||||
mock.Setup(p => p.Manifest).Returns(this.GetManifest(m => m.MinimumApiVersion = new SemanticVersion("1.1")));
|
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
|
// act
|
||||||
new ModResolver().ValidateManifests(new[] { mock.Object }, apiVersion: new SemanticVersion("1.0"));
|
new ModResolver().ValidateManifests(new[] { mock.Object }, apiVersion: new SemanticVersion("1.0"));
|
||||||
|
@ -173,12 +169,8 @@ namespace StardewModdingAPI.Tests.Core
|
||||||
public void ValidateManifests_MissingEntryDLL_Fails()
|
public void ValidateManifests_MissingEntryDLL_Fails()
|
||||||
{
|
{
|
||||||
// arrange
|
// arrange
|
||||||
Mock<IModMetadata> mock = new Mock<IModMetadata>(MockBehavior.Strict);
|
Mock<IModMetadata> mock = this.GetMetadata(this.GetManifest("Mod A", "1.0", manifest => manifest.EntryDll = "Missing.dll"), allowStatusChange: true);
|
||||||
mock.Setup(p => p.Status).Returns(ModMetadataStatus.Found);
|
this.SetupMetadataForValidation(mock);
|
||||||
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);
|
|
||||||
|
|
||||||
// act
|
// act
|
||||||
new ModResolver().ValidateManifests(new[] { mock.Object }, apiVersion: new SemanticVersion("1.0"));
|
new ModResolver().ValidateManifests(new[] { mock.Object }, apiVersion: new SemanticVersion("1.0"));
|
||||||
|
@ -469,8 +461,9 @@ namespace StardewModdingAPI.Tests.Core
|
||||||
/// <summary>Get a randomised basic manifest.</summary>
|
/// <summary>Get a randomised basic manifest.</summary>
|
||||||
/// <param name="uniqueID">The mod's name and unique ID.</param>
|
/// <param name="uniqueID">The mod's name and unique ID.</param>
|
||||||
/// <param name="version">The mod version.</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>
|
/// <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 =>
|
return this.GetManifest(manifest =>
|
||||||
{
|
{
|
||||||
|
@ -478,9 +471,19 @@ namespace StardewModdingAPI.Tests.Core
|
||||||
manifest.UniqueID = uniqueID;
|
manifest.UniqueID = uniqueID;
|
||||||
manifest.Version = new SemanticVersion(version);
|
manifest.Version = new SemanticVersion(version);
|
||||||
manifest.Dependencies = dependencies;
|
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>
|
/// <summary>Get a randomised basic manifest.</summary>
|
||||||
/// <param name="uniqueID">The mod's name and unique ID.</param>
|
/// <param name="uniqueID">The mod's name and unique ID.</param>
|
||||||
private Mock<IModMetadata> GetMetadata(string uniqueID)
|
private Mock<IModMetadata> GetMetadata(string uniqueID)
|
||||||
|
@ -504,6 +507,7 @@ namespace StardewModdingAPI.Tests.Core
|
||||||
private Mock<IModMetadata> GetMetadata(IManifest manifest, bool allowStatusChange = false)
|
private Mock<IModMetadata> GetMetadata(IManifest manifest, bool allowStatusChange = false)
|
||||||
{
|
{
|
||||||
Mock<IModMetadata> mod = new Mock<IModMetadata>(MockBehavior.Strict);
|
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.Status).Returns(ModMetadataStatus.Found);
|
||||||
mod.Setup(p => p.DisplayName).Returns(manifest.UniqueID);
|
mod.Setup(p => p.DisplayName).Returns(manifest.UniqueID);
|
||||||
mod.Setup(p => p.Manifest).Returns(manifest);
|
mod.Setup(p => p.Manifest).Returns(manifest);
|
||||||
|
@ -516,5 +520,17 @@ namespace StardewModdingAPI.Tests.Core
|
||||||
}
|
}
|
||||||
return mod;
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue