-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
47 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,13 @@ | ||
namespace Refresh.GameServer.Types.Commands; | ||
|
||
public record Command(string Name, string? Arguments); | ||
public ref struct Command | ||
{ | ||
public Command(ReadOnlySpan<char> name, ReadOnlySpan<char> arguments) | ||
{ | ||
this.Name = name; | ||
this.Arguments = arguments; | ||
} | ||
|
||
public ReadOnlySpan<char> Name { get; init; } | ||
public ReadOnlySpan<char> Arguments { get; init; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 23 additions & 9 deletions
32
RefreshTests.GameServer/Tests/Commands/CommandParseTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,38 +1,52 @@ | ||
using System.Buffers; | ||
using Bunkum.HttpServer; | ||
using NotEnoughLogs; | ||
using NotEnoughLogs.Loggers; | ||
using Refresh.GameServer.Services; | ||
using Refresh.GameServer.Types.Commands; | ||
|
||
namespace RefreshTests.GameServer.Tests.Commands; | ||
|
||
public class CommandParseTests : GameServerTest | ||
{ | ||
#pragma warning disable NUnit2045 | ||
private void ParseTest(CommandService service, ReadOnlySpan<char> input, ReadOnlySpan<char> expectedName, ReadOnlySpan<char> expectedArguments) | ||
{ | ||
Command command = service.ParseCommand(input); | ||
Assert.That(command.Name.SequenceEqual(expectedName), Is.True, $"Expected '{command.Name}' to equal '{expectedName}'"); | ||
Assert.That(command.Arguments.SequenceEqual(expectedArguments), Is.True, $"Expected '{command.Arguments}' to equal '{expectedArguments}'"); | ||
} | ||
#pragma warning restore NUnit2045 | ||
|
||
[Test] | ||
public void ParsingTest() | ||
{ | ||
LoggerContainer<BunkumContext> logger = new(); | ||
using LoggerContainer<BunkumContext> logger = new(); | ||
logger.RegisterLogger(new ConsoleLogger()); | ||
CommandService service = new(logger, new MatchService(logger)); | ||
|
||
Assert.That(service.ParseCommand("/parse test"), Is.EqualTo(new Command("parse", "test"))); | ||
Assert.That(service.ParseCommand("/noargs"), Is.EqualTo(new Command("noargs", null))); | ||
Assert.That(service.ParseCommand("/noargs "), Is.EqualTo(new Command("noargs", null))); | ||
ParseTest(service, "/parse test", "parse", "test"); | ||
ParseTest(service, "/noargs", "noargs", ""); | ||
ParseTest(service, "/noargs ", "noargs", ""); | ||
} | ||
|
||
[Test] | ||
public void NoSlashThrows() | ||
{ | ||
LoggerContainer<BunkumContext> logger = new(); | ||
using LoggerContainer<BunkumContext> logger = new(); | ||
logger.RegisterLogger(new ConsoleLogger()); | ||
CommandService service = new(logger, new MatchService(logger)); | ||
|
||
Assert.That(() => service.ParseCommand("parse test"), Throws.Exception); | ||
Assert.That(() => _ = service.ParseCommand("parse test"), Throws.InstanceOf<FormatException>()); | ||
} | ||
|
||
[Test] | ||
public void BlankCommandThrows() | ||
{ | ||
LoggerContainer<BunkumContext> logger = new(); | ||
using LoggerContainer<BunkumContext> logger = new(); | ||
logger.RegisterLogger(new ConsoleLogger()); | ||
CommandService service = new(logger, new MatchService(logger)); | ||
|
||
Assert.That(() => service.ParseCommand("/ test"), Throws.Exception); | ||
Assert.That(() => _ = service.ParseCommand("/ test"), Throws.InstanceOf<FormatException>()); | ||
} | ||
} |