1
0
mirror of https://github.com/ppy/osu.git synced 2026-06-09 14:34:45 +08:00

Add MultiplayerPlaylistItem copy constructor + tests

This commit is contained in:
Dan Balasescu
2025-03-14 18:42:36 +09:00
Unverified
parent 01828931a2
commit 2cbf71c592
3 changed files with 92 additions and 0 deletions
@@ -0,0 +1,66 @@
// 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 Bogus;
using MessagePack;
using NUnit.Framework;
using osu.Game.Online.API;
using osu.Game.Online.Rooms;
namespace osu.Game.Tests.OnlinePlay
{
[TestFixture]
public class MultiplayerPlaylistItemTest
{
[Test]
public void TestCloneMultiplayerPlaylistItem()
{
var faker = new Faker<MultiplayerPlaylistItem>()
.StrictMode(true)
.RuleFor(o => o.ID, f => f.Random.Long())
.RuleFor(o => o.OwnerID, f => f.Random.Int())
.RuleFor(o => o.BeatmapID, f => f.Random.Int())
.RuleFor(o => o.BeatmapChecksum, f => f.Random.Hash())
.RuleFor(o => o.RulesetID, f => f.Random.Int())
.RuleFor(o => o.RequiredMods, f => f.Make(5, _ => new APIMod { Acronym = f.Random.String2(3) }))
.RuleFor(o => o.AllowedMods, f => f.Make(5, _ => new APIMod { Acronym = f.Random.String2(3) }))
.RuleFor(o => o.Expired, f => f.Random.Bool())
.RuleFor(o => o.PlaylistOrder, f => f.Random.UShort())
.RuleFor(o => o.PlayedAt, f => f.Date.RecentOffset())
.RuleFor(o => o.StarRating, f => f.Random.Double())
.RuleFor(o => o.Freestyle, f => f.Random.Bool());
for (int i = 0; i < 100; i++)
{
MultiplayerPlaylistItem item = faker.Generate();
Assert.That(MessagePackSerializer.SerializeToJson(item.Clone()), Is.EqualTo(MessagePackSerializer.SerializeToJson(item)));
}
}
[Test]
public void TestConstructFromAPIModel()
{
var faker = new Faker<MultiplayerPlaylistItem>()
.StrictMode(true)
.RuleFor(o => o.ID, f => f.Random.Long())
.RuleFor(o => o.OwnerID, f => f.Random.Int())
.RuleFor(o => o.BeatmapID, f => f.Random.Int())
.RuleFor(o => o.BeatmapChecksum, f => f.Random.Hash())
.RuleFor(o => o.RulesetID, f => f.Random.Int())
.RuleFor(o => o.RequiredMods, f => f.Make(5, _ => new APIMod { Acronym = f.Random.String2(3) }))
.RuleFor(o => o.AllowedMods, f => f.Make(5, _ => new APIMod { Acronym = f.Random.String2(3) }))
.RuleFor(o => o.Expired, f => f.Random.Bool())
.RuleFor(o => o.PlaylistOrder, f => f.Random.UShort())
.RuleFor(o => o.PlayedAt, f => f.Date.RecentOffset())
.RuleFor(o => o.StarRating, f => f.Random.Double())
.RuleFor(o => o.Freestyle, f => f.Random.Bool());
for (int i = 0; i < 100; i++)
{
MultiplayerPlaylistItem initialItem = faker.Generate();
MultiplayerPlaylistItem copiedItem = new MultiplayerPlaylistItem(new PlaylistItem(initialItem));
Assert.That(MessagePackSerializer.SerializeToJson(copiedItem), Is.EqualTo(MessagePackSerializer.SerializeToJson(initialItem)));
}
}
}
}
+1
View File
@@ -1,6 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<Import Project="..\osu.TestProject.props" />
<ItemGroup Label="Package References">
<PackageReference Include="Bogus" Version="35.6.2" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.13.0" />
<PackageReference Include="DeepEqual" Version="4.2.1" />
<PackageReference Include="Nito.AsyncEx" Version="5.1.2" />
@@ -62,11 +62,17 @@ namespace osu.Game.Online.Rooms
[Key(11)]
public bool Freestyle { get; set; }
/// <summary>
/// Creates a new <see cref="MultiplayerPlaylistItem"/>.
/// </summary>
[SerializationConstructor]
public MultiplayerPlaylistItem()
{
}
/// <summary>
/// Creates a new <see cref="MultiplayerPlaylistItem"/> from an API <see cref="PlaylistItem"/>.
/// </summary>
public MultiplayerPlaylistItem(PlaylistItem item)
{
ID = item.ID;
@@ -82,5 +88,24 @@ namespace osu.Game.Online.Rooms
StarRating = item.Beatmap.StarRating;
Freestyle = item.Freestyle;
}
/// <summary>
/// Creates a copy of this <see cref="MultiplayerPlaylistItem"/>.
/// </summary>
public MultiplayerPlaylistItem Clone() => new MultiplayerPlaylistItem
{
ID = ID,
OwnerID = OwnerID,
BeatmapID = BeatmapID,
BeatmapChecksum = BeatmapChecksum,
RulesetID = RulesetID,
RequiredMods = RequiredMods.ToArray(),
AllowedMods = AllowedMods.ToArray(),
Expired = Expired,
PlaylistOrder = PlaylistOrder,
PlayedAt = PlayedAt,
StarRating = StarRating,
Freestyle = Freestyle,
};
}
}