2021-02-01 10:37:19 +00:00
|
|
|
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
|
|
|
|
// See the LICENCE file in the repository root for full licence text.
|
|
|
|
|
2021-02-02 05:13:50 +00:00
|
|
|
using System;
|
|
|
|
using System.Linq;
|
2021-02-01 10:37:19 +00:00
|
|
|
using Moq;
|
|
|
|
using NUnit.Framework;
|
|
|
|
using osu.Game.Rulesets.Mods;
|
2021-02-02 05:13:50 +00:00
|
|
|
using osu.Game.Rulesets.Osu.Mods;
|
2021-02-01 10:37:19 +00:00
|
|
|
using osu.Game.Utils;
|
|
|
|
|
|
|
|
namespace osu.Game.Tests.Mods
|
|
|
|
{
|
|
|
|
[TestFixture]
|
|
|
|
public class ModUtilsTest
|
|
|
|
{
|
|
|
|
[Test]
|
|
|
|
public void TestModIsCompatibleByItself()
|
|
|
|
{
|
2021-02-02 09:33:12 +00:00
|
|
|
var mod = new Mock<CustomMod1>();
|
2021-02-01 10:37:19 +00:00
|
|
|
Assert.That(ModUtils.CheckCompatibleSet(new[] { mod.Object }));
|
|
|
|
}
|
|
|
|
|
2021-06-18 04:17:32 +00:00
|
|
|
[Test]
|
|
|
|
public void TestModIsCompatibleByItselfWithIncompatibleInterface()
|
|
|
|
{
|
|
|
|
var mod = new Mock<CustomMod1>();
|
|
|
|
mod.Setup(m => m.IncompatibleMods).Returns(new[] { typeof(IModCompatibilitySpecification) });
|
|
|
|
Assert.That(ModUtils.CheckCompatibleSet(new[] { mod.Object }));
|
|
|
|
}
|
|
|
|
|
2021-02-01 10:37:19 +00:00
|
|
|
[Test]
|
|
|
|
public void TestIncompatibleThroughTopLevel()
|
|
|
|
{
|
2021-02-02 09:33:12 +00:00
|
|
|
var mod1 = new Mock<CustomMod1>();
|
|
|
|
var mod2 = new Mock<CustomMod2>();
|
2021-02-01 10:37:19 +00:00
|
|
|
|
|
|
|
mod1.Setup(m => m.IncompatibleMods).Returns(new[] { mod2.Object.GetType() });
|
|
|
|
|
|
|
|
// Test both orderings.
|
2021-02-02 09:33:12 +00:00
|
|
|
Assert.That(ModUtils.CheckCompatibleSet(new Mod[] { mod1.Object, mod2.Object }), Is.False);
|
|
|
|
Assert.That(ModUtils.CheckCompatibleSet(new Mod[] { mod2.Object, mod1.Object }), Is.False);
|
2021-02-01 10:37:19 +00:00
|
|
|
}
|
|
|
|
|
2021-06-18 04:17:32 +00:00
|
|
|
[Test]
|
|
|
|
public void TestIncompatibleThroughInterface()
|
|
|
|
{
|
|
|
|
var mod1 = new Mock<CustomMod1>();
|
|
|
|
var mod2 = new Mock<CustomMod2>();
|
|
|
|
|
|
|
|
mod1.Setup(m => m.IncompatibleMods).Returns(new[] { typeof(IModCompatibilitySpecification) });
|
|
|
|
mod2.Setup(m => m.IncompatibleMods).Returns(new[] { typeof(IModCompatibilitySpecification) });
|
|
|
|
|
|
|
|
// Test both orderings.
|
|
|
|
Assert.That(ModUtils.CheckCompatibleSet(new Mod[] { mod1.Object, mod2.Object }), Is.False);
|
|
|
|
Assert.That(ModUtils.CheckCompatibleSet(new Mod[] { mod2.Object, mod1.Object }), Is.False);
|
|
|
|
}
|
|
|
|
|
2021-02-01 10:37:19 +00:00
|
|
|
[Test]
|
2021-02-02 09:31:08 +00:00
|
|
|
public void TestMultiModIncompatibleWithTopLevel()
|
2021-02-01 10:37:19 +00:00
|
|
|
{
|
2021-02-02 09:33:12 +00:00
|
|
|
var mod1 = new Mock<CustomMod1>();
|
2021-02-01 10:37:19 +00:00
|
|
|
|
|
|
|
// The nested mod.
|
2021-02-02 09:33:12 +00:00
|
|
|
var mod2 = new Mock<CustomMod2>();
|
2021-02-01 10:37:19 +00:00
|
|
|
mod2.Setup(m => m.IncompatibleMods).Returns(new[] { mod1.Object.GetType() });
|
|
|
|
|
|
|
|
var multiMod = new MultiMod(new MultiMod(mod2.Object));
|
|
|
|
|
|
|
|
// Test both orderings.
|
2021-02-02 09:33:12 +00:00
|
|
|
Assert.That(ModUtils.CheckCompatibleSet(new Mod[] { multiMod, mod1.Object }), Is.False);
|
|
|
|
Assert.That(ModUtils.CheckCompatibleSet(new Mod[] { mod1.Object, multiMod }), Is.False);
|
2021-02-01 10:37:19 +00:00
|
|
|
}
|
|
|
|
|
2021-02-02 09:31:08 +00:00
|
|
|
[Test]
|
|
|
|
public void TestTopLevelIncompatibleWithMultiMod()
|
|
|
|
{
|
|
|
|
// The nested mod.
|
|
|
|
var mod1 = new Mock<CustomMod1>();
|
|
|
|
var multiMod = new MultiMod(new MultiMod(mod1.Object));
|
|
|
|
|
|
|
|
var mod2 = new Mock<CustomMod2>();
|
|
|
|
mod2.Setup(m => m.IncompatibleMods).Returns(new[] { typeof(CustomMod1) });
|
|
|
|
|
|
|
|
// Test both orderings.
|
|
|
|
Assert.That(ModUtils.CheckCompatibleSet(new Mod[] { multiMod, mod2.Object }), Is.False);
|
|
|
|
Assert.That(ModUtils.CheckCompatibleSet(new Mod[] { mod2.Object, multiMod }), Is.False);
|
|
|
|
}
|
|
|
|
|
2021-02-02 09:01:33 +00:00
|
|
|
[Test]
|
|
|
|
public void TestCompatibleMods()
|
|
|
|
{
|
2021-02-02 09:33:12 +00:00
|
|
|
var mod1 = new Mock<CustomMod1>();
|
|
|
|
var mod2 = new Mock<CustomMod2>();
|
2021-02-02 09:01:33 +00:00
|
|
|
|
|
|
|
// Test both orderings.
|
2021-02-02 09:33:12 +00:00
|
|
|
Assert.That(ModUtils.CheckCompatibleSet(new Mod[] { mod1.Object, mod2.Object }), Is.True);
|
|
|
|
Assert.That(ModUtils.CheckCompatibleSet(new Mod[] { mod2.Object, mod1.Object }), Is.True);
|
2021-02-02 09:01:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
public void TestIncompatibleThroughBaseType()
|
|
|
|
{
|
2021-02-02 09:33:12 +00:00
|
|
|
var mod1 = new Mock<CustomMod1>();
|
|
|
|
var mod2 = new Mock<CustomMod2>();
|
|
|
|
mod2.Setup(m => m.IncompatibleMods).Returns(new[] { typeof(Mod) });
|
2021-02-02 09:01:33 +00:00
|
|
|
|
|
|
|
// Test both orderings.
|
2021-02-02 09:33:12 +00:00
|
|
|
Assert.That(ModUtils.CheckCompatibleSet(new Mod[] { mod1.Object, mod2.Object }), Is.False);
|
|
|
|
Assert.That(ModUtils.CheckCompatibleSet(new Mod[] { mod2.Object, mod1.Object }), Is.False);
|
2021-02-01 10:37:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
public void TestAllowedThroughMostDerivedType()
|
|
|
|
{
|
2021-02-02 09:33:12 +00:00
|
|
|
var mod = new Mock<CustomMod1>();
|
2021-02-01 10:37:19 +00:00
|
|
|
Assert.That(ModUtils.CheckAllowed(new[] { mod.Object }, new[] { mod.Object.GetType() }));
|
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
public void TestNotAllowedThroughBaseType()
|
|
|
|
{
|
2021-02-02 09:33:12 +00:00
|
|
|
var mod = new Mock<CustomMod1>();
|
2021-02-01 10:37:19 +00:00
|
|
|
Assert.That(ModUtils.CheckAllowed(new[] { mod.Object }, new[] { typeof(Mod) }), Is.False);
|
|
|
|
}
|
2021-02-02 05:13:50 +00:00
|
|
|
|
2021-02-02 09:51:13 +00:00
|
|
|
private static readonly object[] invalid_mod_test_scenarios =
|
|
|
|
{
|
|
|
|
// incompatible pair.
|
|
|
|
new object[]
|
|
|
|
{
|
|
|
|
new Mod[] { new OsuModDoubleTime(), new OsuModHalfTime() },
|
|
|
|
new[] { typeof(OsuModDoubleTime), typeof(OsuModHalfTime) }
|
|
|
|
},
|
|
|
|
// incompatible pair with derived class.
|
|
|
|
new object[]
|
|
|
|
{
|
|
|
|
new Mod[] { new OsuModNightcore(), new OsuModHalfTime() },
|
|
|
|
new[] { typeof(OsuModNightcore), typeof(OsuModHalfTime) }
|
|
|
|
},
|
|
|
|
// system mod.
|
|
|
|
new object[]
|
|
|
|
{
|
|
|
|
new Mod[] { new OsuModDoubleTime(), new OsuModTouchDevice() },
|
|
|
|
new[] { typeof(OsuModTouchDevice) }
|
|
|
|
},
|
|
|
|
// multi mod.
|
|
|
|
new object[]
|
|
|
|
{
|
|
|
|
new Mod[] { new MultiMod(new OsuModHalfTime()), new OsuModHalfTime() },
|
|
|
|
new[] { typeof(MultiMod) }
|
|
|
|
},
|
|
|
|
// valid pair.
|
|
|
|
new object[]
|
|
|
|
{
|
|
|
|
new Mod[] { new OsuModDoubleTime(), new OsuModHardRock() },
|
|
|
|
null
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
[TestCaseSource(nameof(invalid_mod_test_scenarios))]
|
|
|
|
public void TestInvalidModScenarios(Mod[] inputMods, Type[] expectedInvalid)
|
2021-02-02 05:13:50 +00:00
|
|
|
{
|
|
|
|
bool isValid = ModUtils.CheckValidForGameplay(inputMods, out var invalid);
|
|
|
|
|
|
|
|
Assert.That(isValid, Is.EqualTo(expectedInvalid == null));
|
|
|
|
|
|
|
|
if (isValid)
|
|
|
|
Assert.IsNull(invalid);
|
|
|
|
else
|
2021-05-14 21:35:06 +00:00
|
|
|
Assert.That(invalid.Select(t => t.GetType()), Is.EquivalentTo(expectedInvalid));
|
2021-02-02 05:13:50 +00:00
|
|
|
}
|
2021-02-02 09:41:35 +00:00
|
|
|
|
2021-06-18 04:17:32 +00:00
|
|
|
public abstract class CustomMod1 : Mod, IModCompatibilitySpecification
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
public abstract class CustomMod2 : Mod, IModCompatibilitySpecification
|
2021-02-02 09:31:08 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2021-06-18 04:17:32 +00:00
|
|
|
public interface IModCompatibilitySpecification
|
2021-02-02 09:31:08 +00:00
|
|
|
{
|
|
|
|
}
|
2021-02-01 10:37:19 +00:00
|
|
|
}
|
|
|
|
}
|