diff --git a/src/Neo.ConsoleService/ConsoleServiceBase.cs b/src/Neo.ConsoleService/ConsoleServiceBase.cs index bb98ba963f..153418b5d0 100644 --- a/src/Neo.ConsoleService/ConsoleServiceBase.cs +++ b/src/Neo.ConsoleService/ConsoleServiceBase.cs @@ -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(); diff --git a/tests/Neo.ConsoleService.Tests/UT_CommandServiceBase.cs b/tests/Neo.ConsoleService.Tests/UT_CommandServiceBase.cs index 0b94d76dfe..157b07bdf0 100644 --- a/tests/Neo.ConsoleService.Tests/UT_CommandServiceBase.cs +++ b/tests/Neo.ConsoleService.Tests/UT_CommandServiceBase.cs @@ -13,6 +13,7 @@ using System; using System.Collections.Generic; using System.Reflection; +using System.Threading.Tasks; namespace Neo.ConsoleService.Tests { @@ -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")] @@ -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 } } @@ -116,5 +137,45 @@ public void TestParseSequentialArguments() var args5 = new List(); Assert.ThrowsExactly(() => 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(() => 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); + } } }