~ruther/NosSmooth

ref: 762fc34c97e8b1f6e9290426b8bc2a78122e28ba NosSmooth/Tests/NosSmooth.Core.Tests/Packets/PacketHandlerTests.Events.cs -rw-r--r-- 2.7 KiB
762fc34c — Rutherther Merge pull request #82 from Rutherther/feat/serialize-stackalloc 2 years ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
//
//  PacketHandlerTests.Events.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 System;
using System.Reflection;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using NosSmooth.Core.Commands;
using NosSmooth.Core.Packets;
using NosSmooth.Core.Tests.Fakes;
using NosSmooth.Core.Tests.Fakes.Commands;
using NosSmooth.Core.Tests.Fakes.Packets;
using NosSmooth.Core.Tests.Fakes.Packets.Events;
using NosSmooth.PacketSerializer.Abstractions.Attributes;
using NosSmooth.PacketSerializer.Extensions;
using NosSmooth.PacketSerializer.Packets;
using Remora.Results;
using Xunit;

namespace NosSmooth.Core.Tests.Packets;

/// <summary>
/// Tests <see cref="PacketHandler"/>.
/// </summary>
public class PacketHandlerTestsEvents
{
    /// <summary>
    /// Tests that the handle packet will call the pre event.
    /// </summary>
    /// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
    [Fact]
    public async Task HandlePacket_CallsPreEvent()
    {
        var called = false;
        var client = new FakeEmptyNostaleClient();
        var provider = new ServiceCollection()
            .AddPacketSerialization()
            .AddGeneratedSerializers(Assembly.GetExecutingAssembly())
            .AddSingleton(typeof(ILogger<>), typeof(FakeLogger<>))
            .AddSingleton<IPacketHandler, ManagedPacketHandler>()
            .AddScoped<IPreExecutionEvent>
            (
                _ => new PacketEvent
                (
                    (_, _, p, _) =>
                    {
                        return Result.FromSuccess();
                    },
                    (_, _, _, _, _) => throw new NotImplementedException()
                )
            )
            .AddScoped<IPacketResponder<FakePacket>>
            (
                _ => new FakePacketResponder<FakePacket>
                (
                    args =>
                    {
                        called = true;
                        return Result.FromSuccess();
                    }
                )
            )
            .BuildServiceProvider();

        provider.GetRequiredService<IPacketTypesRepository>().AddPacketType(typeof(FakePacket));
        var result = await provider.GetRequiredService<IPacketHandler>().HandlePacketAsync
            (client, PacketSource.Client, "fake a");
        Assert.True(result.IsSuccess);
        Assert.True(called);
    }

    // TODO: calls pre event
    // TODO: returns pre event error

    // TODO: calls post event
    // TODO: passes correct result to post event
}
Do not follow this link