From 56dfefc0597f6a6683c109908e3b8bea46761f15 Mon Sep 17 00:00:00 2001 From: Rutherther Date: Wed, 4 Jan 2023 22:55:58 +0100 Subject: [PATCH] feat(tests): add tests structure --- .../Commands/Control/ControlCommandsTests.cs | 14 ++++ .../Control/TakeControlCommandHandlerTests.cs | 14 ++++ .../Fakes/Packets/Events/PacketEvent.cs | 69 ++++++++++++++++++ .../Packets/PacketHandlerTests.Events.cs | 73 +++++++++++++++++++ .../Packets/PacketHandlerTests.Handling.cs | 21 ++++++ .../Packets/PacketHandlerTests.cs | 15 ++++ 6 files changed, 206 insertions(+) create mode 100644 Tests/NosSmooth.Core.Tests/Commands/Control/ControlCommandsTests.cs create mode 100644 Tests/NosSmooth.Core.Tests/Commands/Control/TakeControlCommandHandlerTests.cs create mode 100644 Tests/NosSmooth.Core.Tests/Fakes/Packets/Events/PacketEvent.cs create mode 100644 Tests/NosSmooth.Core.Tests/Packets/PacketHandlerTests.Events.cs create mode 100644 Tests/NosSmooth.Core.Tests/Packets/PacketHandlerTests.Handling.cs create mode 100644 Tests/NosSmooth.Core.Tests/Packets/PacketHandlerTests.cs diff --git a/Tests/NosSmooth.Core.Tests/Commands/Control/ControlCommandsTests.cs b/Tests/NosSmooth.Core.Tests/Commands/Control/ControlCommandsTests.cs new file mode 100644 index 0000000000000000000000000000000000000000..a68581abeffbc7d83290f0b126b4f3005cbb9f30 --- /dev/null +++ b/Tests/NosSmooth.Core.Tests/Commands/Control/ControlCommandsTests.cs @@ -0,0 +1,14 @@ +// +// ControlCommandsTests.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. + +namespace NosSmooth.Core.Tests.Commands.Control; + +/// +/// Tests control commands logic. +/// +public class ControlCommandsTests +{ +} \ No newline at end of file diff --git a/Tests/NosSmooth.Core.Tests/Commands/Control/TakeControlCommandHandlerTests.cs b/Tests/NosSmooth.Core.Tests/Commands/Control/TakeControlCommandHandlerTests.cs new file mode 100644 index 0000000000000000000000000000000000000000..ed0702a554cfb6b83058d377155decd6ba7a83f8 --- /dev/null +++ b/Tests/NosSmooth.Core.Tests/Commands/Control/TakeControlCommandHandlerTests.cs @@ -0,0 +1,14 @@ +// +// TakeControlCommandHandlerTests.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. + +namespace NosSmooth.Core.Tests.Commands.Control; + +/// +/// Tests handling of take control command. +/// +public class TakeControlCommandHandlerTests +{ +} \ No newline at end of file diff --git a/Tests/NosSmooth.Core.Tests/Fakes/Packets/Events/PacketEvent.cs b/Tests/NosSmooth.Core.Tests/Fakes/Packets/Events/PacketEvent.cs new file mode 100644 index 0000000000000000000000000000000000000000..c6684b24c464147d844ff0632842c9de3bb8a654 --- /dev/null +++ b/Tests/NosSmooth.Core.Tests/Fakes/Packets/Events/PacketEvent.cs @@ -0,0 +1,69 @@ +// +// PacketEvent.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.Collections.Generic; +using System.Threading; +using System.Threading.Tasks; +using NosSmooth.Core.Client; +using NosSmooth.Core.Packets; +using NosSmooth.Packets; +using NosSmooth.PacketSerializer.Abstractions.Attributes; +using Remora.Results; + +namespace NosSmooth.Core.Tests.Fakes.Packets.Events; + +/// +/// A fake packet event. +/// +public class PacketEvent : IPreExecutionEvent, IPostExecutionEvent +{ + private readonly Func _preHandler; + private readonly Func, Result> _postHandler; + + /// + /// Initializes a new instance of the class. + /// + /// The pre handler. + /// The post handler. + public PacketEvent + ( + Func preHandler, + Func, Result> postHandler + ) + { + _preHandler = preHandler; + _postHandler = postHandler; + + } + + /// + public Task ExecuteBeforeExecutionAsync + (INostaleClient client, PacketEventArgs packetArgs, CancellationToken ct = default) + where TPacket : IPacket + => Task.FromResult(_preHandler(client, packetArgs.Source, packetArgs.Packet, packetArgs.PacketString)); + + /// + public Task ExecuteAfterExecutionAsync + ( + INostaleClient client, + PacketEventArgs packetArgs, + IReadOnlyList executionResults, + CancellationToken ct = default + ) + where TPacket : IPacket + => Task.FromResult + ( + _postHandler + ( + client, + packetArgs.Source, + packetArgs.Packet, + packetArgs.PacketString, + executionResults + ) + ); +} \ No newline at end of file diff --git a/Tests/NosSmooth.Core.Tests/Packets/PacketHandlerTests.Events.cs b/Tests/NosSmooth.Core.Tests/Packets/PacketHandlerTests.Events.cs new file mode 100644 index 0000000000000000000000000000000000000000..f37f93ac4d0d09aca341f905638e16dc3975d153 --- /dev/null +++ b/Tests/NosSmooth.Core.Tests/Packets/PacketHandlerTests.Events.cs @@ -0,0 +1,73 @@ +// +// 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.Threading.Tasks; +using Microsoft.Extensions.DependencyInjection; +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 Remora.Results; +using Xunit; + +namespace NosSmooth.Core.Tests.Packets; + +/// +/// Tests . +/// +public class PacketHandlerTestsEvents +{ + /// + /// Tests that the handle packet will call the pre event. + /// + /// A representing the asynchronous operation. + [Fact] + public async Task HandlePacket_CallsPreEvent() + { + var called = false; + var client = new FakeEmptyNostaleClient(); + var provider = new ServiceCollection() + .AddSingleton() + .AddScoped + ( + _ => new PacketEvent + ( + (_, _, p, _) => + { + return Result.FromSuccess(); + }, + (_, _, _, _, _) => throw new NotImplementedException() + ) + ) + .AddScoped> + ( + _ => new FakePacketResponder + ( + args => + { + called = true; + return Result.FromSuccess(); + } + ) + ) + .BuildServiceProvider(); + + var result = await provider.GetRequiredService().HandlePacketAsync + (client, PacketSource.Client, new FakePacket("a"), "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 +} \ No newline at end of file diff --git a/Tests/NosSmooth.Core.Tests/Packets/PacketHandlerTests.Handling.cs b/Tests/NosSmooth.Core.Tests/Packets/PacketHandlerTests.Handling.cs new file mode 100644 index 0000000000000000000000000000000000000000..46e8bfaf1ba6abf8c1eb9177d64a2ca721acc184 --- /dev/null +++ b/Tests/NosSmooth.Core.Tests/Packets/PacketHandlerTests.Handling.cs @@ -0,0 +1,21 @@ +// +// PacketHandlerTests.Handling.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 NosSmooth.Core.Packets; + +namespace NosSmooth.Core.Tests.Packets; + +/// +/// Tests . +/// +public class PacketHandlerTestsHandling +{ + // TODO: check if arguments are correct + // TODO: calls responder + // TODO: returns responder error + // TODO: returns responder and post event error + // TODO: does not call responder if pre event failed +} \ No newline at end of file diff --git a/Tests/NosSmooth.Core.Tests/Packets/PacketHandlerTests.cs b/Tests/NosSmooth.Core.Tests/Packets/PacketHandlerTests.cs new file mode 100644 index 0000000000000000000000000000000000000000..eede400fd46944c6d882fdb6748e939452a5e0fb --- /dev/null +++ b/Tests/NosSmooth.Core.Tests/Packets/PacketHandlerTests.cs @@ -0,0 +1,15 @@ +// +// PacketHandlerTests.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. + +namespace NosSmooth.Core.Tests.Packets; + +/// +/// Tests packet handling. +/// +public partial class PacketHandlerTests +{ + // TODO: exceptions +} \ No newline at end of file