1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-14 12:33:01 +08:00

Add automated test cases

This commit is contained in:
smoogipoo 2019-11-05 19:31:58 +09:00
parent 6c58faf30c
commit f2118b0eba
2 changed files with 113 additions and 67 deletions

View File

@ -2,8 +2,6 @@
// See the LICENCE file in the repository root for full licence text. // See the LICENCE file in the repository root for full licence text.
using System; using System;
using System.Collections.Generic;
using System.Linq;
using NUnit.Framework; using NUnit.Framework;
using osu.Framework.Graphics; using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers; using osu.Framework.Graphics.Containers;
@ -27,7 +25,7 @@ namespace osu.Game.Rulesets.Osu.Tests
{ {
Children = new Drawable[] Children = new Drawable[]
{ {
hitObjectContainer = new Container<DrawableOsuHitObject> { RelativeSizeAxes = Axes.Both }, hitObjectContainer = new TestHitObjectContainer { RelativeSizeAxes = Axes.Both },
followPointRenderer = new FollowPointRenderer { RelativeSizeAxes = Axes.Both } followPointRenderer = new FollowPointRenderer { RelativeSizeAxes = Axes.Both }
}; };
}); });
@ -35,22 +33,88 @@ namespace osu.Game.Rulesets.Osu.Tests
[Test] [Test]
public void TestAddSingleHitCircle() public void TestAddSingleHitCircle()
{ {
addObjects(() => new OsuHitObject[] { new HitCircle { Position = new Vector2(100, 100) } }); addObjectsStep(() => new OsuHitObject[] { new HitCircle { Position = new Vector2(100, 100) } });
assertGroups();
} }
[Test] [Test]
public void TestRemoveSingleHitCircle() public void TestRemoveSingleHitCircle()
{ {
DrawableOsuHitObject obj = null; addObjectsStep(() => new OsuHitObject[] { new HitCircle { Position = new Vector2(100, 100) } });
addObjects(() => new OsuHitObject[] { new HitCircle { Position = new Vector2(100, 100) } }, o => obj = o); removeObjectStep(() => getObject(0));
removeObject(() => obj);
assertGroups();
} }
[Test] [Test]
public void TestAddMultipleHitCircles() public void TestAddMultipleHitCircles()
{ {
addObjects(() => new OsuHitObject[] addMultipleObjectsStep();
assertGroups();
}
[Test]
public void TestRemoveEndHitCircle()
{
addMultipleObjectsStep();
removeObjectStep(() => getObject(4));
assertGroups();
}
[Test]
public void TestRemoveStartHitCircle()
{
addMultipleObjectsStep();
removeObjectStep(() => getObject(0));
assertGroups();
}
[Test]
public void TestRemoveMiddleHitCircle()
{
addMultipleObjectsStep();
removeObjectStep(() => getObject(2));
assertGroups();
}
[Test]
public void TestMoveHitCircle()
{
addMultipleObjectsStep();
AddStep("move hitobject", () => getObject(2).HitObject.Position = new Vector2(300, 100));
assertGroups();
}
[TestCase(0, 0)] // Start -> Start
[TestCase(0, 2)] // Start -> Middle
[TestCase(0, 5)] // Start -> End
[TestCase(2, 0)] // Middle -> Start
[TestCase(1, 3)] // Middle -> Middle (forwards)
[TestCase(3, 1)] // Middle -> Middle (backwards)
[TestCase(4, 0)] // End -> Start
[TestCase(4, 2)] // End -> Middle
[TestCase(4, 4)] // End -> End
public void TestReorderHitObjects(int startIndex, int endIndex)
{
addMultipleObjectsStep();
reorderObjectStep(startIndex, endIndex);
assertGroups();
}
private void addMultipleObjectsStep() => addObjectsStep(() => new OsuHitObject[]
{ {
new HitCircle { Position = new Vector2(100, 100) }, new HitCircle { Position = new Vector2(100, 100) },
new HitCircle { Position = new Vector2(200, 200) }, new HitCircle { Position = new Vector2(200, 200) },
@ -58,85 +122,8 @@ namespace osu.Game.Rulesets.Osu.Tests
new HitCircle { Position = new Vector2(400, 400) }, new HitCircle { Position = new Vector2(400, 400) },
new HitCircle { Position = new Vector2(500, 500) }, new HitCircle { Position = new Vector2(500, 500) },
}); });
}
[Test] private void addObjectsStep(Func<OsuHitObject[]> ctorFunc)
public void TestRemoveEndHitCircle()
{
var objects = new List<DrawableOsuHitObject>();
AddStep("reset", () => objects.Clear());
addObjects(() => new OsuHitObject[]
{
new HitCircle { Position = new Vector2(100, 100) },
new HitCircle { Position = new Vector2(200, 200) },
new HitCircle { Position = new Vector2(300, 300) },
new HitCircle { Position = new Vector2(400, 400) },
new HitCircle { Position = new Vector2(500, 500) },
}, o => objects.Add(o));
removeObject(() => objects.Last());
}
[Test]
public void TestRemoveStartHitCircle()
{
var objects = new List<DrawableOsuHitObject>();
AddStep("reset", () => objects.Clear());
addObjects(() => new OsuHitObject[]
{
new HitCircle { Position = new Vector2(100, 100) },
new HitCircle { Position = new Vector2(200, 200) },
new HitCircle { Position = new Vector2(300, 300) },
new HitCircle { Position = new Vector2(400, 400) },
new HitCircle { Position = new Vector2(500, 500) },
}, o => objects.Add(o));
removeObject(() => objects.First());
}
[Test]
public void TestRemoveMiddleHitCircle()
{
var objects = new List<DrawableOsuHitObject>();
AddStep("reset", () => objects.Clear());
addObjects(() => new OsuHitObject[]
{
new HitCircle { Position = new Vector2(100, 100) },
new HitCircle { Position = new Vector2(200, 200) },
new HitCircle { Position = new Vector2(300, 300) },
new HitCircle { Position = new Vector2(400, 400) },
new HitCircle { Position = new Vector2(500, 500) },
}, o => objects.Add(o));
removeObject(() => objects[2]);
}
[Test]
public void TestMoveHitCircle()
{
var objects = new List<DrawableOsuHitObject>();
AddStep("reset", () => objects.Clear());
addObjects(() => new OsuHitObject[]
{
new HitCircle { Position = new Vector2(100, 100) },
new HitCircle { Position = new Vector2(200, 200) },
new HitCircle { Position = new Vector2(300, 300) },
new HitCircle { Position = new Vector2(400, 400) },
new HitCircle { Position = new Vector2(500, 500) },
}, o => objects.Add(o));
AddStep("move hitobject", () => objects[2].HitObject.Position = new Vector2(300, 100));
}
private void addObjects(Func<OsuHitObject[]> ctorFunc, Action<DrawableOsuHitObject> storeFunc = null)
{ {
AddStep("add hitobjects", () => AddStep("add hitobjects", () =>
{ {
@ -166,13 +153,11 @@ namespace osu.Game.Rulesets.Osu.Tests
hitObjectContainer.Add(drawableObject); hitObjectContainer.Add(drawableObject);
followPointRenderer.AddFollowPoints(drawableObject); followPointRenderer.AddFollowPoints(drawableObject);
storeFunc?.Invoke(drawableObject);
} }
}); });
} }
private void removeObject(Func<DrawableOsuHitObject> getFunc) private void removeObjectStep(Func<DrawableOsuHitObject> getFunc)
{ {
AddStep("remove hitobject", () => AddStep("remove hitobject", () =>
{ {
@ -182,5 +167,64 @@ namespace osu.Game.Rulesets.Osu.Tests
followPointRenderer.RemoveFollowPoints(drawableObject); followPointRenderer.RemoveFollowPoints(drawableObject);
}); });
} }
private void reorderObjectStep(int startIndex, int endIndex)
{
AddStep($"move object {startIndex} to {endIndex}", () =>
{
DrawableOsuHitObject toReorder = getObject(startIndex);
double targetTime;
if (endIndex < hitObjectContainer.Count)
targetTime = getObject(endIndex).HitObject.StartTime - 1;
else
targetTime = getObject(hitObjectContainer.Count - 1).HitObject.StartTime + 1;
hitObjectContainer.Remove(toReorder);
toReorder.HitObject.StartTime = targetTime;
hitObjectContainer.Add(toReorder);
});
}
private void assertGroups()
{
AddAssert("has correct group count", () => followPointRenderer.Groups.Count == hitObjectContainer.Count);
AddAssert("group endpoints are correct", () =>
{
for (int i = 0; i < hitObjectContainer.Count; i++)
{
DrawableOsuHitObject expectedStart = getObject(i);
DrawableOsuHitObject expectedEnd = i < hitObjectContainer.Count - 1 ? getObject(i + 1) : null;
if (getGroup(i).Start != expectedStart)
throw new AssertionException($"Object {i} expected to be the start of group {i}.");
if (getGroup(i).End != expectedEnd)
throw new AssertionException($"Object {(expectedEnd == null ? "null" : i.ToString())} expected to be the end of group {i}.");
}
return true;
});
}
private DrawableOsuHitObject getObject(int index) => hitObjectContainer[index];
private FollowPointGroup getGroup(int index) => followPointRenderer.Groups[index];
private class TestHitObjectContainer : Container<DrawableOsuHitObject>
{
protected override int Compare(Drawable x, Drawable y)
{
var osuX = (DrawableOsuHitObject)x;
var osuY = (DrawableOsuHitObject)y;
int compare = osuX.HitObject.StartTime.CompareTo(osuY.HitObject.StartTime);
if (compare == 0)
return base.Compare(x, y);
return compare;
}
}
} }
} }

View File

@ -11,6 +11,8 @@ namespace osu.Game.Rulesets.Osu.Objects.Drawables.Connections
{ {
public class FollowPointRenderer : CompositeDrawable public class FollowPointRenderer : CompositeDrawable
{ {
internal IReadOnlyList<FollowPointGroup> Groups => groups;
private readonly List<FollowPointGroup> groups = new List<FollowPointGroup>(); private readonly List<FollowPointGroup> groups = new List<FollowPointGroup>();
/// <summary> /// <summary>