Skip to content

Commit

Permalink
Aliases + user input parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
EnoughTea committed Jan 4, 2020
1 parent dc9b5d3 commit 71920b1
Show file tree
Hide file tree
Showing 11 changed files with 215 additions and 31 deletions.
6 changes: 3 additions & 3 deletions Compro.Tests/Compro.Tests.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
Expand All @@ -8,8 +8,8 @@

<ItemGroup>
<PackageReference Include="nunit" Version="3.12.0" />
<PackageReference Include="NUnit3TestAdapter" Version="3.15.1" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.2.0" />
<PackageReference Include="NUnit3TestAdapter" Version="3.16.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.4.0" />
</ItemGroup>

<ItemGroup>
Expand Down
43 changes: 35 additions & 8 deletions Compro.Tests/JsonCommandPieceConverterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,56 +14,83 @@ public class JsonCommandPieceConverterTests
public void OneTimeSetUp()
{
_defaultConverter = new JsonConverter();
var arr = ArrayPool<object>.Shared.Rent(10);
ArrayPool<object>.Shared.Return(arr, true);
}

[Test]
public void FloatsShouldBeConverted()
{
var floatString = _defaultConverter.ConvertToString(0.5f).Try().IfFail("");
string floatString = _defaultConverter.ConvertToString(0.5f).Try().IfFail(e => e.Message);

Assert.AreEqual("0.5", floatString);
}

[Test]
public void DoublesShouldBeConverted()
{
var doubleString = _defaultConverter.ConvertToString(0.5).IfFail("");
string doubleString = _defaultConverter.ConvertToString(0.5).IfFail(e => e.Message);

Assert.AreEqual("0.5", doubleString);
}

[Test]
public void NansShouldBeConverted()
{
var nan = _defaultConverter.ConvertToString(float.NaN).IfFail("");
string nan = _defaultConverter.ConvertToString(float.NaN).IfFail(e => e.Message);

Assert.AreEqual("NaN", nan);
}

[Test]
public void NegativeInfinityShouldBeConverted()
{
var negInf = _defaultConverter.ConvertToString(float.NegativeInfinity).IfFail("");
string negInf = _defaultConverter.ConvertToString(float.NegativeInfinity).IfFail(e => e.Message);

Assert.AreEqual("-Infinity", negInf);
}

[Test]
public void PositiveInfinityShouldBeConverted()
{
var posInf = _defaultConverter.ConvertToString(float.PositiveInfinity).IfFail("");
string posInf = _defaultConverter.ConvertToString(float.PositiveInfinity).IfFail(e => e.Message);

Assert.AreEqual("Infinity", posInf);
}

[Test]
public void PositiveInfinityStringShouldBeConverted()
{
var posInf = (float) _defaultConverter.ConvertFromString("Infinity", typeof(float)).IfFail(0f);
float posInf = (float) _defaultConverter.ConvertFromString("Infinity", typeof(float)).IfFail(0f);

Assert.AreEqual(float.PositiveInfinity, posInf);
}

[Test]
public void SimpleStringShouldBeConverted()
{
string target = "\"Simple string\"";
string simple = (string) _defaultConverter.ConvertFromString(target, typeof(string))
.IfFail(e => e.Message);

Assert.AreEqual("Simple string", simple);
}

[Test]
public void StringWithNestedQuotesShouldBeConverted()
{
string target = "\"Not \\\"so \\\"very\\\" simple\\\" string\"";
string nestedQuotes = (string) _defaultConverter.ConvertFromString(target,
typeof(string)).IfFail(e => e.Message);

Assert.AreEqual("Not \"so \"very\" simple\" string", nestedQuotes);
}

[Test]
public void SingleCharShouldBeConverted()
{
string degree = _defaultConverter.ConvertToString('\u00f8')
.IfFail(e => e.Message);

Assert.AreEqual("\u00f8", degree);
}
}
}
6 changes: 3 additions & 3 deletions Compro.Tests/TerminalCommandOnMethodTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ namespace Compro.Tests
public class TerminalCommandOnMethodTests
{
private TestCommandProvider _commandProvider;
private TerminalCommandOnMethod[] _commands;
private ConsoleCommandOnMethod[] _commands;

[OneTimeSetUp]
public void OneTimeSetUp()
{
_commandProvider = new TestCommandProvider();
_commands = TerminalCommandOnMethod.GatherFromInstance(_commandProvider, CommandIOPartConverters.Shared);
_commands = ConsoleCommandOnMethod.GatherFromInstance(_commandProvider, CommandIOPartConverters.Shared);
}

[Test]
Expand All @@ -32,7 +32,7 @@ public void CommandsWithComplexReturnShouldWorkOutOfTheBox()
{
var findCommand = _commands.First(c => c.Name == "Find");

var result = findCommand.Call("Macron");
var result = findCommand.Call("\"Macron\"");

Assert.IsTrue(result.ConvertedValue.Contains("Putin"), result.ConvertedValue);
Assert.AreNotEqual(CommandCallSuccess.Void, result);
Expand Down
15 changes: 15 additions & 0 deletions Compro.Tests/UserInputParserTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using NUnit.Framework;

namespace Compro.Tests
{
public class UserInputParserTests
{
[Test]
public void Test()
{
var p = new UserInputParser();
var parseResult = p.Parse("sqrt(0.5, -1000000, \"this is ,simple string\"," +
" \"and ,this \\\",is \\\"heavily,\\\"nested\\\"\\\" string\")");
}
}
}
2 changes: 1 addition & 1 deletion Compro/Attributes/CommandAliasAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Compro
{
[AttributeUsage(AttributeTargets.Method)]
[AttributeUsage(AttributeTargets.Method, AllowMultiple = true)]
public class CommandAliasAttribute : Attribute
{
public string Alias { get; }
Expand Down
1 change: 1 addition & 0 deletions Compro/Command/CommandCallFailure.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ public class CommandCallFailure : ICommandCallResult
{
public CommandCallFailure(Exception e)
{
Exception = e ?? throw new ArgumentNullException(nameof(e));
ReturnedValue = CommandCallSuccess.Void.ReturnedValue;
ConvertedValue = CommandCallSuccess.Void.ConvertedValue;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@

namespace Compro
{
public class TerminalCommandOnMethod : ITerminalCommand
public class ConsoleCommandOnMethod : IConsoleCommand
{
public object ExecutedMethodInstance { get; }

public MethodInfo ExecutedMethodInfo { get; }

public TerminalCommandOnMethod(object executedMethodInstance,
public ConsoleCommandOnMethod(object executedMethodInstance,
MethodInfo executedMethodInfo,
CommandIOPartConverters converters)
{
Expand All @@ -24,11 +24,17 @@ public TerminalCommandOnMethod(object executedMethodInstance,
ExecutedMethodInfo = executedMethodInfo ?? throw new ArgumentNullException(nameof(executedMethodInfo));
Name = executedMethodInfo.Name;
Description = executedMethodInfo.GetCustomAttribute<CommandExecutableAttribute>()?.Description ?? "";
Aliases = executedMethodInfo.GetCustomAttributes<CommandAliasAttribute>()
.Select(a => a.Alias)
.ToList()
.AsReadOnly();
Result = ExtractResult(executedMethodInfo, converters);
Parameters = ExtractParameters(executedMethodInfo, converters);
}

public string Name { get; }

public IReadOnlyList<string> Aliases { get; }

public IReadOnlyList<CommandParameterInfo> Parameters { get; }

Expand All @@ -48,14 +54,21 @@ from returned in Execute(convertedArgs)
e => new CommandCallFailure(e));
}

public static TerminalCommandOnMethod[] GatherFromInstance(object instance, CommandIOPartConverters converters)
/// <inheritdoc />
public bool CanBeCalledBy(string name)
{
return string.Equals(Name, name, StringComparison.OrdinalIgnoreCase) ||
Aliases.Exists(alias => string.Equals(alias, name, StringComparison.OrdinalIgnoreCase));
}

public static ConsoleCommandOnMethod[] GatherFromInstance(object instance, CommandIOPartConverters converters)
{
if (instance == null) throw new ArgumentNullException(nameof(instance));
if (converters == null) throw new ArgumentNullException(nameof(converters));

var commandMethodInfos = instance.GetType().GetMethods().Where(methodInfo =>
methodInfo.GetCustomAttributes(typeof(CommandExecutableAttribute), false).Length > 0);
return commandMethodInfos.Select(cmi => new TerminalCommandOnMethod(instance, cmi, converters))
return commandMethodInfos.Select(cmi => new ConsoleCommandOnMethod(instance, cmi, converters))
.ToArray();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@
namespace Compro
{
/// <summary> Represents a terminal command which can be executed with given arguments. </summary>
public interface ITerminalCommand
public interface IConsoleCommand
{
string Name { get; }

IReadOnlyList<string> Aliases { get; }

string Description { get; }

Expand All @@ -15,5 +17,7 @@ public interface ITerminalCommand
CommandReturnInfo Result { get; }

ICommandCallResult Call(params string[] args);

bool CanBeCalledBy(string name);
}
}
2 changes: 1 addition & 1 deletion Compro/Compro.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="LanguageExt.Core" Version="3.3.47" />
<PackageReference Include="LanguageExt.Core" Version="3.3.48" />
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
</ItemGroup>

Expand Down
12 changes: 2 additions & 10 deletions Compro/Converters/JsonConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,20 +28,12 @@ public Try<object> ConvertFromString(string repr, Type targetType)
if (repr == null) throw new ArgumentNullException(nameof(repr));
if (targetType == null) throw new ArgumentNullException(nameof(targetType));

return targetType switch {
_ when targetType == typeof(string) => DeserializeNoOp(repr),
_ when targetType == typeof(char) => DeserializeNoOp(repr.FirstOrDefault()),
_ => Deserialize(repr, targetType)
};
return Deserialize(repr, targetType);
}

public Try<string> ConvertToString<T>(T value)
{
return value switch {
string stringValue => SerializeNoOp(stringValue),
char charValue => SerializeNoOp(charValue.ToString()),
_ => Serialize(value)
};
return Serialize(value);
}

// Stuff below used to avoid closure allocations.
Expand Down
Loading

0 comments on commit 71920b1

Please sign in to comment.