Skip to content

Commit

Permalink
Upgraded project to C# 12
Browse files Browse the repository at this point in the history
  • Loading branch information
Pogromca-SCP committed Nov 16, 2023
1 parent 48147e1 commit c197cf3
Show file tree
Hide file tree
Showing 43 changed files with 459 additions and 557 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ jobs:
- name: Setup .NET
uses: actions/setup-dotnet@v3
with:
dotnet-version: 6.0.x
dotnet-version: 8.0.x

- name: Download depot downloader
shell: pwsh
Expand Down
53 changes: 25 additions & 28 deletions SLCommandScript.Core.UnitTests/Commands/CommandsUtilsTests.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
using NUnit.Framework;
using PluginAPI.Enums;
using CommandSystem;
using CommandSystem.Commands.RemoteAdmin.Broadcasts;
using CommandSystem;
using CommandSystem.Commands.RemoteAdmin;
using CommandSystem.Commands.RemoteAdmin.Broadcasts;
using CommandSystem.Commands.Shared;
using System.Collections.Generic;
using System.Linq;
using RemoteAdmin;
using SLCommandScript.Core.Commands;
using FluentAssertions;
using Moq;
using NUnit.Framework;
using PluginAPI.Enums;
using RemoteAdmin;
using SLCommandScript.Core.Commands;
using System.Collections.Generic;
using System.Linq;

namespace SLCommandScript.Core.UnitTests.Commands;

Expand All @@ -21,26 +21,25 @@ public class CommandsUtilsTests
private const CommandType InvalidCommandType = CommandType.Console;

#region Test Case Sources
private static readonly CommandType[] _allHandlerTypes = { CommandType.RemoteAdmin, CommandType.Console,
private static readonly CommandType[] _allHandlerTypes = [CommandType.RemoteAdmin, CommandType.Console,
CommandType.GameConsole, CommandType.RemoteAdmin | CommandType.Console, CommandType.RemoteAdmin | CommandType.GameConsole,
CommandType.GameConsole | CommandType.Console, CommandType.RemoteAdmin | CommandType.GameConsole | CommandType.Console };
CommandType.GameConsole | CommandType.Console, CommandType.RemoteAdmin | CommandType.GameConsole | CommandType.Console];

private static readonly string[] _invalidCommandNames = { null, "", " ", " \t ", " \t \t\t" };
private static readonly string[] _invalidCommandNames = [null, "", " ", " \t ", " \t \t\t"];

private static readonly string[] _validCommandNames = { "hello", "item list", "?.cassie" };
private static readonly string[] _validCommandNames = ["hello", "item list", "?.cassie"];

private static readonly string[][] _invalidAliases = { new[] { " " }, new[] { null, "test" }, new[] { "hello", " \t", " ", null } };
private static readonly string[][] _invalidAliases = [[" "], [null, "test"], ["hello", " \t", " ", null]];

private static readonly string[][] _validAliases = { null, new[] { "string", "example" }, new string[0] };
private static readonly string[][] _validAliases = [null, ["string", "example"], []];

private static readonly CommandType[] _validHandlerTypes = { CommandType.RemoteAdmin, CommandType.GameConsole,
CommandType.RemoteAdmin | CommandType.GameConsole };
private static readonly CommandType[] _validHandlerTypes = [CommandType.RemoteAdmin, CommandType.GameConsole, CommandType.RemoteAdmin | CommandType.GameConsole];

private static readonly string[] _existingCommandNames = { "help", "HelP", "bc", "cassie", "BC" };
private static readonly string[] _existingCommandNames = ["help", "HelP", "bc", "cassie", "BC"];

private static readonly string[] _commandsToRegister = { "wtf", "dotheflip", "weeee" };
private static readonly string[] _commandsToRegister = ["wtf", "dotheflip", "weeee"];

private static readonly ICommand[] _exampleCommands = { new BroadcastCommand(), new CassieCommand(), new HelpCommand(ClientCommandHandler.Create()) };
private static readonly ICommand[] _exampleCommands = [new BroadcastCommand(), new CassieCommand(), new HelpCommand(ClientCommandHandler.Create())];

private static IEnumerable<object[]> AllHandlersXInvalidCommands => JoinArrays(_allHandlerTypes, _invalidCommandNames);

Expand All @@ -59,15 +58,13 @@ private static IEnumerable<object[]> JoinArrays<TFirst, TSecond>(TFirst[] first,
#region Helper Methods
private static IEnumerable<ICommandHandler> GetExpectedCommandHandlers(CommandType handlerType) => handlerType switch
{
CommandType.RemoteAdmin => new[] { CommandProcessor.RemoteAdminCommandHandler },
CommandType.RemoteAdmin | CommandType.Console => new[] { CommandProcessor.RemoteAdminCommandHandler },
CommandType.GameConsole => new[] { QueryProcessor.DotCommandHandler },
CommandType.GameConsole | CommandType.Console => new[] { QueryProcessor.DotCommandHandler },
CommandType.RemoteAdmin | CommandType.GameConsole => new ICommandHandler[] {
CommandProcessor.RemoteAdminCommandHandler, QueryProcessor.DotCommandHandler },
CommandType.RemoteAdmin | CommandType.GameConsole | CommandType.Console => new ICommandHandler[] {
CommandProcessor.RemoteAdminCommandHandler, QueryProcessor.DotCommandHandler },
_ => new ICommandHandler[0]
CommandType.RemoteAdmin => [CommandProcessor.RemoteAdminCommandHandler],
CommandType.RemoteAdmin | CommandType.Console => [CommandProcessor.RemoteAdminCommandHandler],
CommandType.GameConsole => [QueryProcessor.DotCommandHandler],
CommandType.GameConsole | CommandType.Console => [QueryProcessor.DotCommandHandler],
CommandType.RemoteAdmin | CommandType.GameConsole => [CommandProcessor.RemoteAdminCommandHandler, QueryProcessor.DotCommandHandler],
CommandType.RemoteAdmin | CommandType.GameConsole | CommandType.Console => [CommandProcessor.RemoteAdminCommandHandler, QueryProcessor.DotCommandHandler],
_ => []
};

private static CommandType GetCommandHandlerType(ICommandHandler handler) => handler switch
Expand Down
15 changes: 6 additions & 9 deletions SLCommandScript.Core.UnitTests/Iterables/IterableListTests.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
using NUnit.Framework;
using FluentAssertions;
using System.Linq;
using System.Collections.Generic;
using FluentAssertions;
using NUnit.Framework;
using SLCommandScript.Core.Iterables;
using System.Collections.Generic;
using System.Linq;

namespace SLCommandScript.Core.UnitTests.Iterables;

[TestFixture]
public class IterableListTests
{
private static readonly string[][] _strings = { new string[] { null, null, null, null }, new[] { "example", null, "", "test" },
new[] { " \t ", "Test", "test", "TEST" } };
private static readonly string[][] _strings = [[null, null, null, null], ["example", null, "", "test"], [" \t ", "Test", "test", "TEST"]];

#region Constructor Tests
[Test]
Expand Down Expand Up @@ -95,10 +94,8 @@ public void Reset_ShouldProperlyResetIterable(string[] strings)
#endregion
}

public class TestIterable : IterableListBase<string>
public class TestIterable(IEnumerable<string> strings) : IterableListBase<string>(strings)
{
public TestIterable(IEnumerable<string> strings) : base(strings) {}

protected override void LoadVariables(IDictionary<string, string> targetVars, string str)
{
targetVars[str] = str;
Expand Down
40 changes: 18 additions & 22 deletions SLCommandScript.Core.UnitTests/Language/InterpreterTests.cs
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
using NUnit.Framework;
using SLCommandScript.Core.Language;
using CommandSystem;
using FluentAssertions;
using Moq;
using NUnit.Framework;
using SLCommandScript.Core.Interfaces;
using SLCommandScript.Core.Language;
using SLCommandScript.Core.Language.Expressions;
using CommandSystem;
using System;
using System.Threading.Tasks;
using SLCommandScript.Core.Interfaces;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace SLCommandScript.Core.UnitTests.Language;

[TestFixture]
public class InterpreterTests
{
private static bool[] _booleanValues = { false, true };
private static readonly bool[] _booleanValues = [false, true];

#region ConstructorTests
[Test]
Expand Down Expand Up @@ -151,7 +151,7 @@ public void VisitCommandExpr_ShouldFail_WhenCommandFails()
var message = "Command failed";
var commandMock = new Mock<ICommand>(MockBehavior.Strict);
commandMock.Setup(x => x.Execute(new(new[] { "example", "args" }, 1, 1), null, out message)).Returns(false);
var expr = new CommandExpr(commandMock.Object, new[] { "example", "args" }, false);
var expr = new CommandExpr(commandMock.Object, ["example", "args"], false);

// Act
var result = interpreter.VisitCommandExpr(expr);
Expand All @@ -172,7 +172,7 @@ public void VisitCommandExpr_ShouldSucceed_WhenGoldFlow()
var message = "Command succeeded";
var commandMock = new Mock<ICommand>(MockBehavior.Strict);
commandMock.Setup(x => x.Execute(new(new[] { "test" }, 1, 0), null, out message)).Returns(true);
var expr = new CommandExpr(commandMock.Object, new[] { "test" }, false);
var expr = new CommandExpr(commandMock.Object, ["test"], false);

// Act
var result = interpreter.VisitCommandExpr(expr);
Expand Down Expand Up @@ -226,7 +226,7 @@ public void VisitDelayExpr_ShouldExecuteSynchronously_WhenDurationIsTooShort(boo
var message = success ? "Command succeeded" : "Command failed";
var commandMock = new Mock<ICommand>(MockBehavior.Strict);
commandMock.Setup(x => x.Execute(new(new[] { "test" }, 1, 0), null, out message)).Returns(success);
var expr = new DelayExpr(new CommandExpr(commandMock.Object, new[] { "test" }, false), 0, null);
var expr = new DelayExpr(new CommandExpr(commandMock.Object, ["test"], false), 0, null);

// Act
var result = interpreter.VisitDelayExpr(expr);
Expand All @@ -248,7 +248,7 @@ public async Task VisitDelayExpr_ShouldExecuteAsynchronously_WhenDurationIsValid
var message = success ? "Command succeeded" : "Command failed";
var commandMock = new Mock<ICommand>(MockBehavior.Strict);
commandMock.Setup(x => x.Execute(new(new[] { "test" }, 1, 0), null, out message)).Returns(success);
var expr = new DelayExpr(new CommandExpr(commandMock.Object, new[] { "test" }, false), delay, null);
var expr = new DelayExpr(new CommandExpr(commandMock.Object, ["test"], false), delay, null);

// Act
var result = interpreter.VisitDelayExpr(expr);
Expand Down Expand Up @@ -347,7 +347,7 @@ public void VisitForeachExpr_ShouldSucceed_WhenGoldFlow()
var message = "Command succeeded";
var commandMock = new Mock<ICommand>(MockBehavior.Strict);
commandMock.Setup(x => x.Execute(new(new[] { "test", "args", "$(arg)" }, 1, 2), null, out message)).Returns(true);
var expr = new ForeachExpr(new CommandExpr(commandMock.Object, new[] { "test", "args", "$(arg)" }, false), new TestIterable());
var expr = new ForeachExpr(new CommandExpr(commandMock.Object, ["test", "args", "$(arg)"], false), new TestIterable());

// Act
var result = interpreter.VisitForeachExpr(expr);
Expand All @@ -366,7 +366,7 @@ public void VisitForeachExpr_ShouldProperlyInjectArguments()
// Arrange
var interpreter = new Interpreter(null);

var expr = new ForeachExpr(new CommandExpr(new ArgumentsInjectionTestCommand(), new[] { "$(test)", "$(i)", "$(index)", "$(I)", null, "$(wut?))$(wut?))" }, true),
var expr = new ForeachExpr(new CommandExpr(new ArgumentsInjectionTestCommand(), ["$(test)", "$(i)", "$(index)", "$(I)", null, "$(wut?))$(wut?))"], true),
new TestIterable());

// Act
Expand Down Expand Up @@ -436,8 +436,7 @@ public void VisitIfExpr_ShouldFail_WhenThenBranchFails()
var commandMock = new Mock<ICommand>(MockBehavior.Strict);
commandMock.Setup(x => x.Execute(new(new[] { "condition" }, 1, 0), null, out message)).Returns(true);

var expr = new IfExpr(new CommandExpr(null, null, false), new CommandExpr(commandMock.Object,
new[] { "condition" }, false), null);
var expr = new IfExpr(new CommandExpr(null, null, false), new CommandExpr(commandMock.Object, ["condition"], false), null);

// Act
var result = interpreter.VisitIfExpr(expr);
Expand All @@ -459,8 +458,8 @@ public void VisitIfExpr_ShouldFail_WhenElseBranchFails()
var commandMock = new Mock<ICommand>(MockBehavior.Strict);
commandMock.Setup(x => x.Execute(new(new[] { "condition" }, 1, 0), null, out message)).Returns(false);

var expr = new IfExpr(new ForeachExpr(null, null), new CommandExpr(commandMock.Object, new[] { "condition" }, false),
new CommandExpr(commandMock.Object, new[] { "else" }, false));
var expr = new IfExpr(new ForeachExpr(null, null), new CommandExpr(commandMock.Object, ["condition"], false),
new CommandExpr(commandMock.Object, ["else"], false));

// Act
var result = interpreter.VisitIfExpr(expr);
Expand All @@ -482,8 +481,7 @@ public void VisitIfExpr_ShouldSucceed_WhenThenBranchSucceeds()
var commandMock = new Mock<ICommand>(MockBehavior.Strict);
commandMock.Setup(x => x.Execute(new(new[] { "condition" }, 1, 0), null, out message)).Returns(true);

var expr = new IfExpr(new CommandExpr(commandMock.Object, new[] { "then" }, false), new CommandExpr(commandMock.Object,
new[] { "condition" }, false), null);
var expr = new IfExpr(new CommandExpr(commandMock.Object, ["then"], false), new CommandExpr(commandMock.Object, ["condition"], false), null);

// Act
var result = interpreter.VisitIfExpr(expr);
Expand All @@ -505,8 +503,7 @@ public void VisitIfdExpr_ShouldSucceed_WhenElseBranchIsNull()
var commandMock = new Mock<ICommand>(MockBehavior.Strict);
commandMock.Setup(x => x.Execute(new(new[] { "condition" }, 1, 0), null, out message)).Returns(false);

var expr = new IfExpr(new ForeachExpr(null, null), new CommandExpr(commandMock.Object,
new[] { "condition" }, false), null);
var expr = new IfExpr(new ForeachExpr(null, null), new CommandExpr(commandMock.Object, ["condition"], false), null);

// Act
var result = interpreter.VisitIfExpr(expr);
Expand All @@ -528,8 +525,7 @@ public void VisitIfdExpr_ShouldSucceed_WhenElseBranchSucceeds()
var commandMock = new Mock<ICommand>(MockBehavior.Strict);
commandMock.Setup(x => x.Execute(new(new[] { "condition" }, 1, 0), null, out message)).Returns(true);

var expr = new IfExpr(new ForeachExpr(null, null), new CommandExpr(null, null, false),
new CommandExpr(commandMock.Object, new[] { "else" }, false));
var expr = new IfExpr(new ForeachExpr(null, null), new CommandExpr(null, null, false), new CommandExpr(commandMock.Object, ["else"], false));

// Act
var result = interpreter.VisitIfExpr(expr);
Expand Down
Loading

0 comments on commit c197cf3

Please sign in to comment.