~ruther/NosSmooth

d40bd57dbe8830b6320f41d354d44f815ab10230 — František Boháček 3 years ago 9fe8a75
feat: add move packet with tests
A Core/NosSmooth.Packets/Packets/Server/Map/MovePacket.cs => Core/NosSmooth.Packets/Packets/Server/Map/MovePacket.cs +34 -0
@@ 0,0 1,34 @@
//
//  MovePacket.cs
//
//  Copyright (c) František Boháček. All rights reserved.
//  Licensed under the MIT license. See LICENSE file in the project root for full license information.

using NosCore.Shared.Enumerations;
using NosSmooth.Packets.Attributes;

namespace NosSmooth.Packets.Packets.Server.Map;

/// <summary>
/// The entity has moved to the given position.
/// </summary>
/// <param name="EntityType"></param>
/// <param name="EntityId"></param>
/// <param name="MapX"></param>
/// <param name="MapY"></param>
/// <param name="Speed"></param>
[PacketHeader("mv", PacketSource.Server)]
[GenerateSerializer]
public record MovePacket
(
    [PacketIndex(0)]
    VisualType EntityType,
    [PacketIndex(1)]
    long EntityId,
    [PacketIndex(2)]
    short MapX,
    [PacketIndex(3)]
    short MapY,
    [PacketIndex(4)]
    byte Speed
) : IPacket;
\ No newline at end of file

A Tests/NosSmooth.Packets.Tests/Converters/Packets/MovePacketConverterTests.cs => Tests/NosSmooth.Packets.Tests/Converters/Packets/MovePacketConverterTests.cs +65 -0
@@ 0,0 1,65 @@
//
//  MovePacketConverterTests.cs
//
//  Copyright (c) František Boháček. All rights reserved.
//  Licensed under the MIT license. See LICENSE file in the project root for full license information.

using Microsoft.Extensions.DependencyInjection;
using NosCore.Shared.Enumerations;
using NosSmooth.Packets.Attributes;
using NosSmooth.Packets.Converters;
using NosSmooth.Packets.Converters.Packets;
using NosSmooth.Packets.Extensions;
using NosSmooth.Packets.Packets.Server.Map;
using NosSmooth.Packets.Packets.Server.Map.Generated;
using Xunit;

namespace NosSmooth.Packets.Tests.Converters.Packets;

/// <summary>
/// Tests <see cref="MovePacketConverter"/>.
/// </summary>
public class MovePacketConverterTests
{
    private readonly IPacketSerializer _packetSerializer;

    /// <summary>
    /// Initializes a new instance of the <see cref="MovePacketConverterTests"/> class.
    /// </summary>
    public MovePacketConverterTests()
    {
        var provider = new ServiceCollection()
            .AddSingleton<TypeConverterRepository>()
            .AddPacketSerialization()
            .BuildServiceProvider();

        _packetSerializer = provider.GetRequiredService<IPacketSerializer>();
    }

    /// <summary>
    /// Tests that the converter serializes mv packet correctly.
    /// </summary>
    [Fact]
    public void Converter_Serialization_SerializesCorrectly()
    {
        MovePacket packet = new MovePacket(VisualType.Monster, 122, 15, 20, 10);
        var result = _packetSerializer.Serialize(packet);
        Assert.True(result.IsSuccess);

        Assert.Equal("mv 3 122 15 20 10", result.Entity);
    }

    /// <summary>
    /// Tests that the converter serializes mv packet correctly.
    /// </summary>
    [Fact]
    public void Converter_Deserialization_DeserializesCorrectly()
    {
        var packetString = "mv 3 122 15 20 10";
        var result = _packetSerializer.Deserialize(packetString, PacketSource.Server);
        Assert.True(result.IsSuccess);

        MovePacket actualPacket = new MovePacket(VisualType.Monster, 122, 15, 20, 10);
        Assert.Equal(result.Entity, actualPacket);
    }
}
\ No newline at end of file

Do not follow this link