From ff1e87c6c9445744940392f148a1d402e4bd11b6 Mon Sep 17 00:00:00 2001 From: Craig Oates Date: Thu, 2 Nov 2017 21:40:05 +0000 Subject: [PATCH] Added testing and console demo. - Unit tests checkes the coercion logic in the CW_Core_Console. - CW_Core_Console now has command-methods for users to test out and review. Apart from proof reading and the inevitable refactoring, this is the bulk of the work done (codewise). --- CW_Core_Console/CW_Core_Console.csproj | 7 ++ CW_Core_Console/ConsoleCommands.cs | 118 +++++++++++++++++- CW_Core_Tests/CW_Core_Tests.csproj | 8 +- CW_Core_Tests/ConsoleTests.cs | 117 +++++++++++++++++ CW_Core_Tests/UnitTest1.cs | 14 --- Console.Waterworks.Core.sln | 6 +- .../Assistants/CommandsAssistant.cs | 2 - .../Console.Waterworks.Core.csproj | 2 + 8 files changed, 249 insertions(+), 25 deletions(-) create mode 100644 CW_Core_Tests/ConsoleTests.cs delete mode 100644 CW_Core_Tests/UnitTest1.cs diff --git a/CW_Core_Console/CW_Core_Console.csproj b/CW_Core_Console/CW_Core_Console.csproj index aea02a2..f1e5937 100644 --- a/CW_Core_Console/CW_Core_Console.csproj +++ b/CW_Core_Console/CW_Core_Console.csproj @@ -3,6 +3,13 @@ Exe netcoreapp2.0 + CW_Core_Console.Program + Craig Oates + + + This console program is part of the Console.Waterworks.Core project. Its main use is to help test and demonstrate the Console.Waterworks.Core class library. + Copyright © 2017 Craig Oates + en-GB diff --git a/CW_Core_Console/ConsoleCommands.cs b/CW_Core_Console/ConsoleCommands.cs index 1205338..89232ae 100644 --- a/CW_Core_Console/ConsoleCommands.cs +++ b/CW_Core_Console/ConsoleCommands.cs @@ -1,11 +1,123 @@ using System; -using System.Collections.Generic; -using System.Text; +using Console.Waterworks.Core.Attributes; +using Console.Waterworks.Core; namespace CW_Core_Console { public class ConsoleCommands { - public static string Test() => "Working"; + #region Demo-Methods + + // These command-methods are to show how a Console.Waterworks program typically looks. + + [ListCommand()] + [Description("Displays the Help section at run-time")] + [Parameters("None")] + [Usage("CW_Console> Help")] + public static string Help() + { + CW_Liaison liaison = new CW_Liaison(); + return liaison.RequestHelpDocumentation("CW_Core_Console"); + } + + [ListCommand()] + [Description("Outputs a message indicating this program is running okay.")] + [Parameters("None")] + [Usage("CW_Console> Test")] + public static string Test() => "Test complete."; + + [ListCommand()] + [Description("Repeats back to the user the string they entered.")] + [Parameters(" input")] + [Usage("CW_Console> Say \"Hello, World.\"")] + public static string Say(string input) => $"{input}"; + + [ListCommand()] + [Description("Displays the date and time at the moment the command is entered")] + [Parameters("None")] + [Usage("CW_Console> GetDate")] + public static string GetDate() => $"{DateTime.Now}"; + + [ListCommand()] + [Description("Takes the two ints and adds them together.")] + [Parameters(" int, int2")] + [Usage("CW_Console> Add 31 10")] + public static string Add(int int1, int int2) => $"{int1} + {int2} = {int1 + int2}"; + + [ListCommand()] + [Description("Terminates the program.")] + [Parameters("None")] + [Usage("CW_Console> Quit")] + public static void Quit() => Environment.Exit(-1); +#endregion + + #region Alias-Methods + + /* + * A NOTE ABOUT ALIAS-METHODS - DELETE AFTER READING + * ========================================================= + * These methods are shorthand versions of the ones above. + * For the most part, this is a hack. + * But, it is a useful one. + * When new users start using your console program, they need help getting started. + * This is why I recommend using descriptive names for your command-methods. + * But, when your users become more familiar with the program, they will want terser commands. + * They will no longer need their hand holding. This is where these alias-commands come in. + */ + + [ListCommand(false)] // change to true or delete "false" for it to show at run-time. + [Description("Alias for \"Test\". See \"Test\" for details.")] + [Parameters("None")] + [Usage("CW_Console> test")] + public static string test() => Test(); + + [ListCommand(false)] + [Description("Alias for \"Say\". See \"Say\" for details.")] + [Parameters("None")] + [Usage("CW_Console> say \"Hello, World.\"")] + public static string say(string input) => Say(input); + + [ListCommand(false)] + [Description("Alias for \"GetDate\". See \"GetDate\" for details.")] + [Parameters("None")] + [Usage("CW_Console> date")] + public static string date() => GetDate(); + + [ListCommand(false)] + [Description("Alias for \"Add\". See \"Add\" for details.")] + [Parameters("None")] + [Usage("CW_Console> add 24 67")] + public static string add(int int1, int int2) => Add(int1, int2); + + [ListCommand(false)] + [Description("Alias for Quit. See Quit for details.")] + [Parameters("None")] + [Usage("CW_Console> quit")] + public static void quit() => Quit(); + #endregion + + #region Test-Methods + + // The command-methods are for testing purposes only. + // You can use them at run-time but they will not appear in the help section. + + // The template + // public static string paramTest(int param1) => $"Param: {param1}"; + public static string StringTest(String param1) => $"{param1}"; + public static string Int16Test(Int16 param1) => $"{param1}"; + public static string Int32Test(Int32 param1) => $"{param1}"; + public static string Int64Test(Int64 param1) => $"{param1}"; + public static string BooleanTest(Boolean param1) => $"{param1}"; + public static string ByteTest(Byte param1) => $"{param1}"; + public static string CharTest(Char param1) => $"{param1}"; + public static string DateTimeTest(DateTime param1) => $"{param1}"; + public static string DecimalTest(Decimal param1) => $"{param1}"; + public static string SingleTest(Single param1) => $"{param1}"; + public static string UInt16Test(UInt16 param1) => $"{param1}"; + public static string UInt32Test(UInt32 param1) => $"{param1}"; + public static string UInt64Test(UInt64 param1) => $"{param1}"; + + + #endregion } } diff --git a/CW_Core_Tests/CW_Core_Tests.csproj b/CW_Core_Tests/CW_Core_Tests.csproj index aa57a86..738593f 100644 --- a/CW_Core_Tests/CW_Core_Tests.csproj +++ b/CW_Core_Tests/CW_Core_Tests.csproj @@ -4,12 +4,14 @@ netcoreapp2.0 false + + - - - + + + diff --git a/CW_Core_Tests/ConsoleTests.cs b/CW_Core_Tests/ConsoleTests.cs new file mode 100644 index 0000000..802a270 --- /dev/null +++ b/CW_Core_Tests/ConsoleTests.cs @@ -0,0 +1,117 @@ +using CW_Core_Console; +using System; +using Xunit; + +namespace CW_Core_Tests +{ + public class ConsoleTests + { + [Fact] + public void StringTest() + { + string testInput = "Test Input"; + var realInput = ConsoleCommands.StringTest(testInput); + Assert.Equal(testInput, realInput); + } + + [Fact] + public void Int16Test() + { + // Int16 = -32,768 to + 32,767 + Int16 testInput = 1; + var realInput = ConsoleCommands.Int16Test(testInput); + Assert.Equal(testInput.ToString(), realInput); + } + + [Fact] + public void Int32Test() + { + // Int32 = -2,147,483,648 to +2,147,483,647 + Int32 testInput = 10000; + var realInput = ConsoleCommands.Int32Test(testInput); + Assert.Equal(testInput.ToString(), realInput); + } + + [Fact] + public void Int64Test() + { + // Int64 = -9,223,372,036,854,775,808 to +9,223,372,036,854,775,807 + Int64 testInput = 1000000000; + var realInput = ConsoleCommands.Int64Test(testInput); + Assert.Equal(testInput.ToString(), realInput); + } + + [Fact] + public void BooleanTest() + { + Boolean testInput = true; + var realInput = ConsoleCommands.BooleanTest(testInput); + Assert.Equal(testInput.ToString(), realInput); + } + + [Fact] + public void ByteTest() + { + Byte testInput = 1; + var realInput = ConsoleCommands.ByteTest(testInput); + Assert.Equal(testInput.ToString(), realInput); + } + + [Fact] + public void CharTest() + { + Char testInput = 'a'; + var realInput = ConsoleCommands.CharTest(testInput); + Assert.Equal(testInput.ToString(), realInput); + } + + [Fact] + public void DateTimeTest() + { + DateTime testInput = DateTime.Today; // Be careful with this. + var realInput = ConsoleCommands.DateTimeTest(testInput); + Assert.Equal(testInput.ToString(), realInput); + } + + [Fact] + public void DecimalTest() + { + Decimal testInput = 1.001m; + var realInput = ConsoleCommands.DecimalTest(testInput); + Assert.Equal(testInput.ToString(), realInput); + } + + [Fact] + public void SingleTest() + { + Single testInput = 1.001f; + var realInput = ConsoleCommands.SingleTest(testInput); + Assert.Equal(testInput.ToString(), realInput); + } + + [Fact] + public void UInt16Test() + { + // UInt16 = 0 to 65535 + UInt16 testInput = 1; + var realInput = ConsoleCommands.UInt16Test(testInput); + Assert.Equal(testInput.ToString(), realInput); + } + + [Fact] + public void UInt32Test() + { + // UInt32 = 0 to 4,294,967,295 + UInt32 testInput = 100000; + var realInput = ConsoleCommands.UInt32Test(testInput); + } + + [Fact] + public void UInt64Test() + { + // UInt64 = 0 to 18,446,744,073,709,551,615 + UInt64 testInput = 10000000000; + var realInput = ConsoleCommands.UInt64Test(testInput); + } + } +} diff --git a/CW_Core_Tests/UnitTest1.cs b/CW_Core_Tests/UnitTest1.cs deleted file mode 100644 index 4024bcb..0000000 --- a/CW_Core_Tests/UnitTest1.cs +++ /dev/null @@ -1,14 +0,0 @@ -using System; -using Xunit; - -namespace CW_Core_Tests -{ - public class UnitTest1 - { - [Fact] - public void Test1() - { - - } - } -} diff --git a/Console.Waterworks.Core.sln b/Console.Waterworks.Core.sln index dac6355..9679e60 100644 --- a/Console.Waterworks.Core.sln +++ b/Console.Waterworks.Core.sln @@ -3,11 +3,11 @@ Microsoft Visual Studio Solution File, Format Version 12.00 # Visual Studio 15 VisualStudioVersion = 15.0.27004.2006 MinimumVisualStudioVersion = 10.0.40219.1 -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Console.Waterworks.Core", "Console.Waterworks.Core\Console.Waterworks.Core.csproj", "{D3F12E66-79AF-4CC3-BDAE-D574AA6CEA3E}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Console.Waterworks.Core", "Console.Waterworks.Core\Console.Waterworks.Core.csproj", "{D3F12E66-79AF-4CC3-BDAE-D574AA6CEA3E}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CW_Core_Console", "CW_Core_Console\CW_Core_Console.csproj", "{FE969A76-153C-4691-8561-98F6426BB786}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CW_Core_Console", "CW_Core_Console\CW_Core_Console.csproj", "{FE969A76-153C-4691-8561-98F6426BB786}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CW_Core_Tests", "CW_Core_Tests\CW_Core_Tests.csproj", "{B36C3922-1786-4E8D-9016-2EAD5E806D19}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CW_Core_Tests", "CW_Core_Tests\CW_Core_Tests.csproj", "{B36C3922-1786-4E8D-9016-2EAD5E806D19}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution diff --git a/Console.Waterworks.Core/Assistants/CommandsAssistant.cs b/Console.Waterworks.Core/Assistants/CommandsAssistant.cs index 1bfa177..07e73e5 100644 --- a/Console.Waterworks.Core/Assistants/CommandsAssistant.cs +++ b/Console.Waterworks.Core/Assistants/CommandsAssistant.cs @@ -85,8 +85,6 @@ namespace Console.Waterworks.Core.Assistants { try { - //var result = typeInfo.GetMethod(command.Name).Invoke(command.Name, inputArguments).ToString(); - // return result.ToString(); return typeInfo.GetMethod(command.Name).Invoke(command.Name, inputArguments).ToString(); } catch (TargetInvocationException ex) diff --git a/Console.Waterworks.Core/Console.Waterworks.Core.csproj b/Console.Waterworks.Core/Console.Waterworks.Core.csproj index 5766db6..86e6bd7 100644 --- a/Console.Waterworks.Core/Console.Waterworks.Core.csproj +++ b/Console.Waterworks.Core/Console.Waterworks.Core.csproj @@ -2,6 +2,8 @@ netcoreapp2.0 + English (United Kingdom) + Copyright © 2017 Craig Oates