mirror of
https://github.com/space-wizards/RobustToolbox.git
synced 2026-09-01 17:47:24 +02:00
* Add parsing for arrays of values via ValueArray<T>. * oops forgot command description * add valarr to engine toolshed perms, fix stupidness, remove useless code in parser * make optional not suck * support for skipping optional parameters * Switch to List<T> because I'm dumb and that just works™ * remove optional stuff to separate into its own PR * Remove ValArr command `valarr int [ 1, 2 ]` can just be replaced with ` val List<int> [ 1, 2 ]` or ` val int[] [ 1, 2 ]` * Fix namespace * Add support for array parsing * Make ListTypeParser output nullable * Pass ListLengthAttribute in the CommandArgument struct The current method of trying to fetch the attribute for argument doesn't actually work. i.e., the argIndex calculation isn't always correct. This is still kinda janky and I hate it, but it should work better. * Fix empty list parsing, add tests * rename flag, and clarify description * More tests --------- Co-authored-by: ElectroJr <leonsfriedrich@gmail.com>
256 lines
6.6 KiB
C#
256 lines
6.6 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using Robust.Shared.Analyzers;
|
|
using Robust.Shared.Console;
|
|
using Robust.Shared.Prototypes;
|
|
using Robust.Shared.Toolshed;
|
|
using Robust.Shared.Toolshed.Syntax;
|
|
using Robust.Shared.Toolshed.TypeParsers;
|
|
|
|
namespace Robust.UnitTesting.Shared.Toolshed;
|
|
|
|
// This file just contains a collection of various test commands for use in other tests.
|
|
|
|
[ToolshedCommand]
|
|
internal sealed class TestVoidCommand : ToolshedCommand
|
|
{
|
|
[CommandImplementation] public void Impl() {}
|
|
}
|
|
|
|
[ToolshedCommand]
|
|
internal sealed class TestIntCommand : ToolshedCommand
|
|
{
|
|
[CommandImplementation] public int Impl() => 1;
|
|
}
|
|
|
|
|
|
[ToolshedCommand]
|
|
internal sealed class TestTypeArgCommand : ToolshedCommand
|
|
{
|
|
public override Type[] TypeParameterParsers => [typeof(TypeTypeParser)];
|
|
[CommandImplementation] public string Impl<T>() => typeof(T).Name;
|
|
}
|
|
|
|
[ToolshedCommand]
|
|
internal sealed class TestMultiTypeArgCommand : ToolshedCommand
|
|
{
|
|
public override Type[] TypeParameterParsers => [typeof(TypeTypeParser), typeof(TypeTypeParser)];
|
|
[CommandImplementation] public string Impl<T1, T2>(int i)
|
|
=> $"{typeof(T1).Name}, {typeof(T2).Name}, {i}";
|
|
}
|
|
|
|
[ToolshedCommand]
|
|
internal sealed class TestIntStrArgCommand : ToolshedCommand
|
|
{
|
|
[CommandImplementation] public int Impl(int i, string str) => i;
|
|
}
|
|
|
|
[ToolshedCommand]
|
|
internal sealed class TestPipedIntCommand : ToolshedCommand
|
|
{
|
|
[CommandImplementation] public int Impl([PipedArgument] int i) => i;
|
|
}
|
|
|
|
[ToolshedCommand]
|
|
internal sealed class TestCustomVarRefParserCommand : ToolshedCommand
|
|
{
|
|
[CommandImplementation]
|
|
public int Impl([CommandArgument(typeof(Parser))] int i) => i;
|
|
|
|
[Virtual]
|
|
public class Parser : CustomTypeParser<int>
|
|
{
|
|
public override bool TryParse(ParserContext ctx, out int result)
|
|
{
|
|
result = default;
|
|
if (!Toolshed.TryParse(ctx, out int _))
|
|
return false;
|
|
|
|
// Disregard the parsed value and always return 1
|
|
result = 1;
|
|
return true;
|
|
}
|
|
|
|
public override CompletionResult? TryAutocomplete(ParserContext ctx, CommandArgument? arg)
|
|
{
|
|
return new CompletionResult([new("A")], "B");
|
|
}
|
|
}
|
|
}
|
|
|
|
[ToolshedCommand]
|
|
internal sealed class TestOptionalArgsCommand : ToolshedCommand
|
|
{
|
|
[CommandImplementation]
|
|
public int[] Impl(int x, int y = 0, int z = 1)
|
|
=> [x, y, z];
|
|
}
|
|
|
|
[ToolshedCommand]
|
|
internal sealed class TestParamsCollectionCommand : ToolshedCommand
|
|
{
|
|
[CommandImplementation]
|
|
public int[] Impl(int x, int y = 0, params int[] others)
|
|
=> [x, y, ..others];
|
|
}
|
|
|
|
[ToolshedCommand]
|
|
internal sealed class TestParamsOnlyCommand : ToolshedCommand
|
|
{
|
|
[CommandImplementation]
|
|
public int[] Impl(params int[] others)
|
|
=> others;
|
|
}
|
|
|
|
[ToolshedCommand]
|
|
internal sealed class TestCustomParserCommand : ToolshedCommand
|
|
{
|
|
[CommandImplementation]
|
|
public int Impl([CommandArgument(typeof(Parser))] int i) => i;
|
|
|
|
internal sealed class Parser : TestCustomVarRefParserCommand.Parser
|
|
{
|
|
// Disable ValueRef support.
|
|
// I.e., this parser will not not try to parse variables or blocks
|
|
public override bool EnableValueRef => false;
|
|
}
|
|
}
|
|
|
|
[ToolshedCommand]
|
|
internal sealed class TestEnumerableInferCommand : ToolshedCommand
|
|
{
|
|
[CommandImplementation, TakesPipedTypeAsGeneric]
|
|
public Type Impl<T>([PipedArgument] IEnumerable<T> x, T y) => typeof(T);
|
|
}
|
|
|
|
[ToolshedCommand]
|
|
internal sealed class TestListInferCommand : ToolshedCommand
|
|
{
|
|
[CommandImplementation, TakesPipedTypeAsGeneric]
|
|
public Type Impl<T>([PipedArgument] List<T> x, T y) => typeof(T);
|
|
}
|
|
|
|
[ToolshedCommand]
|
|
internal sealed class TestArrayInferCommand : ToolshedCommand
|
|
{
|
|
[CommandImplementation, TakesPipedTypeAsGeneric]
|
|
public Type Impl<T>([PipedArgument] T[] x, T y) => typeof(T);
|
|
}
|
|
|
|
[ToolshedCommand]
|
|
internal sealed class TestNestedEnumerableInferCommand : ToolshedCommand
|
|
{
|
|
[CommandImplementation, TakesPipedTypeAsGeneric]
|
|
public Type Impl<T>([PipedArgument] IEnumerable<ProtoId<T>> x)
|
|
where T : class, IPrototype
|
|
{
|
|
return typeof(T);
|
|
}
|
|
}
|
|
|
|
[ToolshedCommand]
|
|
internal sealed class TestNestedListInferCommand : ToolshedCommand
|
|
{
|
|
[CommandImplementation, TakesPipedTypeAsGeneric]
|
|
public Type Impl<T>([PipedArgument] List<ProtoId<T>> x)
|
|
where T : class, IPrototype
|
|
{
|
|
return typeof(T);
|
|
}
|
|
}
|
|
|
|
[ToolshedCommand]
|
|
internal sealed class TestNestedArrayInferCommand : ToolshedCommand
|
|
{
|
|
[CommandImplementation, TakesPipedTypeAsGeneric]
|
|
public Type Impl<T>([PipedArgument] ProtoId<T>[] x)
|
|
where T : class, IPrototype
|
|
{
|
|
return typeof(T);
|
|
}
|
|
}
|
|
|
|
[ToolshedCommand]
|
|
internal sealed class TestArrayCommand : ToolshedCommand
|
|
{
|
|
[CommandImplementation]
|
|
public int[] Impl() => Array.Empty<int>();
|
|
}
|
|
|
|
[ToolshedCommand]
|
|
internal sealed class TestListCommand : ToolshedCommand
|
|
{
|
|
[CommandImplementation]
|
|
public List<int> Impl() => new();
|
|
}
|
|
|
|
[ToolshedCommand]
|
|
internal sealed class TestEnumerableCommand : ToolshedCommand
|
|
{
|
|
private static int[] _arr = {1, 3, 3};
|
|
|
|
[CommandImplementation]
|
|
public IEnumerable<int> Impl() => _arr.Select(x => 2 * x);
|
|
}
|
|
|
|
[ToolshedCommand]
|
|
internal sealed class TestNestedArrayCommand : ToolshedCommand
|
|
{
|
|
[CommandImplementation]
|
|
public ProtoId<EntityCategoryPrototype>[] Impl() => [];
|
|
}
|
|
|
|
[ToolshedCommand]
|
|
internal sealed class TestNestedListCommand : ToolshedCommand
|
|
{
|
|
[CommandImplementation]
|
|
public List<ProtoId<EntityCategoryPrototype>> Impl() => new();
|
|
}
|
|
|
|
[ToolshedCommand]
|
|
internal sealed class TestNestedEnumerableCommand : ToolshedCommand
|
|
{
|
|
private static ProtoId<EntityCategoryPrototype>[] _arr = [];
|
|
|
|
[CommandImplementation]
|
|
public IEnumerable<ProtoId<EntityCategoryPrototype>> Impl() => _arr.OrderByDescending(x => x.Id);
|
|
}
|
|
|
|
[ToolshedCommand]
|
|
internal sealed class TestImplicitImplCommand : ToolshedCommand
|
|
{
|
|
public int Impl() => 1;
|
|
}
|
|
|
|
[ToolshedCommand]
|
|
internal sealed class TestExplicitImplCommand : ToolshedCommand
|
|
{
|
|
public int Impl() => 1;
|
|
|
|
[CommandImplementation]
|
|
public int Impl2() => 2;
|
|
}
|
|
|
|
[ToolshedCommand]
|
|
internal sealed class TestArrayParseCommand : ToolshedCommand
|
|
{
|
|
[CommandImplementation]
|
|
public int[] Impl(int[] val) => val;
|
|
}
|
|
|
|
[ToolshedCommand]
|
|
internal sealed class TestListParseCommand : ToolshedCommand
|
|
{
|
|
[CommandImplementation]
|
|
public List<int> Impl(List<int> val) => val;
|
|
}
|
|
|
|
[ToolshedCommand]
|
|
internal sealed class TestListLengthCommand : ToolshedCommand
|
|
{
|
|
[CommandImplementation]
|
|
public List<int> Impl([ListLength(MinLength = 1, MaxLength = 2)] List<int> val) => val;
|
|
}
|
|
|