Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/Neo.ConsoleService/ConsoleServiceBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -122,9 +122,9 @@ public abstract class ConsoleServiceBase
return arguments;
}

private bool OnCommand(string commandLine)
internal bool OnCommand(string commandLine)
{
if (string.IsNullOrEmpty(commandLine)) return true;
if (string.IsNullOrWhiteSpace(commandLine)) return true;

var possibleHelp = "";
var tokens = commandLine.Tokenize();
Expand Down
61 changes: 61 additions & 0 deletions tests/Neo.ConsoleService.Tests/UT_CommandServiceBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Threading.Tasks;

namespace Neo.ConsoleService.Tests
{
Expand All @@ -22,6 +23,7 @@ public class UT_CommandServiceBase
private class TestConsoleService : ConsoleServiceBase
{
public override string ServiceName => "TestService";
public bool _asyncTestCalled = false;

// Test method with various parameter types
[ConsoleCommand("test", Category = "Test Commands")]
Expand All @@ -31,6 +33,25 @@ public void TestMethod(string strParam, uint intParam, bool boolParam, string op
[ConsoleCommand("testenum", Category = "Test Commands")]
public void TestEnumMethod(TestEnum enumParam) { }

[ConsoleCommand("testversion", Category = "Test Commands")]
public Version TestMethodVersion() { return new Version("1.0.0"); }

[ConsoleCommand("testambiguous", Category = "Test Commands")]
public void TestAmbiguousFirst() { }

[ConsoleCommand("testambiguous", Category = "Test Commands")]
public void TestAmbiguousSecond() { }

[ConsoleCommand("testcrash", Category = "Test Commands")]
public void TestCrashMethod(uint number) { }

[ConsoleCommand("testasync", Category = "Test Commands")]
public async Task TestAsyncCommand()
{
await Task.Delay(100);
_asyncTestCalled = true;
}

public enum TestEnum { Value1, Value2, Value3 }
}

Expand Down Expand Up @@ -116,5 +137,45 @@ public void TestParseSequentialArguments()
var args5 = new List<CommandToken>();
Assert.ThrowsExactly<ArgumentException>(() => service.ParseSequentialArguments(method, args5.Trim()));
}

[TestMethod]
public void TestOnCommand()
{
var service = new TestConsoleService();
service.RegisterCommand(service, "TestConsoleService");

// Test case 1: Missing command
var resultEmptyCommand = service.OnCommand("");
Assert.IsTrue(resultEmptyCommand);

// Test case 2: White space command
var resultWhiteSpaceCommand = service.OnCommand(" ");
Assert.IsTrue(resultWhiteSpaceCommand);

// Test case 3: Not exist command
var resultNotExistCommand = service.OnCommand("notexist");
Assert.IsFalse(resultNotExistCommand);

// Test case 4: Exists command test
var resultTestCommand = service.OnCommand("testversion");
Assert.IsTrue(resultTestCommand);

// Test case 5: Exists command with quote
var resultTestCommandWithQuote = service.OnCommand("testversion --noargs");
Assert.IsTrue(resultTestCommandWithQuote);

// Test case 6: Ambiguous command tst
var ex = Assert.ThrowsExactly<ArgumentException>(() => service.OnCommand("testambiguous"));
Assert.Contains("Ambiguous calls for", ex.Message);

// Test case 7: Help test
var resultTestHelp = service.OnCommand("testcrash notANumber");
Assert.IsTrue(resultTestHelp);

// Test case 8: Test Task
var resultTestTaskAsync = service.OnCommand("testasync");
Assert.IsTrue(resultTestTaskAsync);
Assert.IsTrue(service._asyncTestCalled);
}
}
}
Loading