Add zero-length objects check and tests

This commit is contained in:
Naxess 2021-07-13 10:50:11 +02:00
parent 351a46b379
commit 6da2a3d51f
2 changed files with 141 additions and 0 deletions

View File

@ -0,0 +1,94 @@
// 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.
using System.Collections.Generic;
using System.Linq;
using Moq;
using NUnit.Framework;
using osu.Game.Beatmaps;
using osu.Game.Rulesets.Edit;
using osu.Game.Rulesets.Edit.Checks;
using osu.Game.Rulesets.Objects;
using osu.Game.Rulesets.Objects.Types;
using osu.Game.Rulesets.Osu.Objects;
using osu.Game.Tests.Beatmaps;
using osuTK;
namespace osu.Game.Tests.Editing.Checks
{
[TestFixture]
public class CheckZeroLengthObjectsTest
{
private CheckZeroLengthObjects check;
[SetUp]
public void Setup()
{
check = new CheckZeroLengthObjects();
}
[Test]
public void TestCircle()
{
assertOk(new List<HitObject>
{
new HitCircle { StartTime = 1000, Position = new Vector2(0, 0) }
});
}
[Test]
public void TestRegularSlider()
{
assertOk(new List<HitObject>
{
getSliderMock(1000).Object
});
}
[Test]
public void TestZeroLengthSlider()
{
assertZeroLength(new List<HitObject>
{
getSliderMock(0).Object
});
}
[Test]
public void TestNegativeLengthSlider()
{
assertZeroLength(new List<HitObject>
{
getSliderMock(-1000).Object
});
}
private Mock<Slider> getSliderMock(double duration)
{
var mockSlider = new Mock<Slider>();
mockSlider.As<IHasDuration>().Setup(d => d.Duration).Returns(duration);
return mockSlider;
}
private void assertOk(List<HitObject> hitObjects)
{
Assert.That(check.Run(getContext(hitObjects)), Is.Empty);
}
private void assertZeroLength(List<HitObject> hitObjects)
{
var issues = check.Run(getContext(hitObjects)).ToList();
Assert.That(issues, Has.Count.EqualTo(1));
Assert.That(issues.First().Template is CheckZeroLengthObjects.IssueTemplateZeroLength);
}
private BeatmapVerifierContext getContext(List<HitObject> hitObjects)
{
var beatmap = new Beatmap<HitObject> { HitObjects = hitObjects };
return new BeatmapVerifierContext(beatmap, new TestWorkingBeatmap(beatmap));
}
}
}

View File

@ -0,0 +1,47 @@
// 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.
using System.Collections.Generic;
using osu.Game.Rulesets.Edit.Checks.Components;
using osu.Game.Rulesets.Objects;
using osu.Game.Rulesets.Objects.Types;
namespace osu.Game.Rulesets.Edit.Checks
{
public class CheckZeroLengthObjects : ICheck
{
/// <summary>
/// The duration can be this low before being treated as having no length, in case of precision errors. Unit is milliseconds.
/// </summary>
private const double leniency = 0.5d;
public CheckMetadata Metadata { get; } = new CheckMetadata(CheckCategory.Compose, "Zero-length hitobjects");
public IEnumerable<IssueTemplate> PossibleTemplates => new IssueTemplate[]
{
new IssueTemplateZeroLength(this)
};
public IEnumerable<Issue> Run(BeatmapVerifierContext context)
{
foreach (var hitObject in context.Beatmap.HitObjects)
{
if (!(hitObject is IHasDuration hasDuration))
continue;
if (hasDuration.Duration < leniency)
yield return new IssueTemplateZeroLength(this).Create(hitObject, hasDuration.Duration);
}
}
public class IssueTemplateZeroLength : IssueTemplate
{
public IssueTemplateZeroLength(ICheck check)
: base(check, IssueType.Problem, "{0} has a duration of {1:0}.")
{
}
public Issue Create(HitObject hitobject, double duration) => new Issue(hitobject, this, hitobject.GetType(), duration);
}
}
}