From 476ed37d2009c5fcf26fde40d3068fae23e85166 Mon Sep 17 00:00:00 2001 From: dboerlage Date: Thu, 27 Apr 2017 12:08:24 +0200 Subject: [PATCH] Adding Unit Tests --- .gitignore | 4 ++- .vscode/launch.json | 52 ++++++++++++++++++++++++++++++++++++ Geekbot.net.sln | 6 +++++ Geekbot.net.userprefs | 28 +++++++++++++++++++ Geekbot.net/Lib/LevelCalc.cs | 2 +- Geekbot.net/Modules/Cat.cs | 34 ++++++++++++++++++++--- Geekbot.net/Program.cs | 2 +- Tests/Tests.csproj | 25 +++++++++++++++++ Tests/UnitTest1.cs | 52 ++++++++++++++++++++++++++++++++++++ 9 files changed, 198 insertions(+), 7 deletions(-) create mode 100644 .vscode/launch.json create mode 100644 Geekbot.net.userprefs create mode 100755 Tests/Tests.csproj create mode 100755 Tests/UnitTest1.cs diff --git a/.gitignore b/.gitignore index 57f9728..55ab035 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,8 @@ Geekbot.net/bin Geekbot.net/obj +Tests/bin +Tests/obj Backup/ .vs/ UpgradeLog.htm -.idea \ No newline at end of file +.idea diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..c975ed1 --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,52 @@ +{ + "version": "0.2.0", + "configurations": [ + { + "name": ".NET Core Launch (console)", + "type": "coreclr", + "request": "launch", + "preLaunchTask": "build", + "program": "${workspaceRoot}/bin/Debug//", + "args": [], + "cwd": "${workspaceRoot}", + "stopAtEntry": false, + "console": "internalConsole" + }, + { + "name": ".NET Core Launch (web)", + "type": "coreclr", + "request": "launch", + "preLaunchTask": "build", + "program": "${workspaceRoot}/bin/Debug//", + "args": [], + "cwd": "${workspaceRoot}", + "stopAtEntry": false, + "launchBrowser": { + "enabled": true, + "args": "${auto-detect-url}", + "windows": { + "command": "cmd.exe", + "args": "/C start ${auto-detect-url}" + }, + "osx": { + "command": "open" + }, + "linux": { + "command": "xdg-open" + } + }, + "env": { + "ASPNETCORE_ENVIRONMENT": "Development" + }, + "sourceFileMap": { + "/Views": "${workspaceRoot}/Views" + } + }, + { + "name": ".NET Core Attach", + "type": "coreclr", + "request": "attach", + "processId": "${command:pickProcess}" + } + ] +} \ No newline at end of file diff --git a/Geekbot.net.sln b/Geekbot.net.sln index 990eb42..fe98069 100644 --- a/Geekbot.net.sln +++ b/Geekbot.net.sln @@ -5,6 +5,8 @@ VisualStudioVersion = 12.0.0.0 MinimumVisualStudioVersion = 10.0.0.1 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Geekbot.net", "Geekbot.net/Geekbot.net.csproj", "{FDCB3D92-E7B5-47BB-A9B5-CFAEFA57CDB4}" EndProject +Project("{64BFF91A-5FCE-45C2-B281-5EA31729FFBF}") = "Tests", "Tests\Tests.csproj", "{CE7B71E3-F73A-40D1-901C-C20BEFB15851}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -15,6 +17,10 @@ Global {FDCB3D92-E7B5-47BB-A9B5-CFAEFA57CDB4}.Debug|Any CPU.Build.0 = Debug|Any CPU {FDCB3D92-E7B5-47BB-A9B5-CFAEFA57CDB4}.Release|Any CPU.ActiveCfg = Release|Any CPU {FDCB3D92-E7B5-47BB-A9B5-CFAEFA57CDB4}.Release|Any CPU.Build.0 = Release|Any CPU + {CE7B71E3-F73A-40D1-901C-C20BEFB15851}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {CE7B71E3-F73A-40D1-901C-C20BEFB15851}.Debug|Any CPU.Build.0 = Debug|Any CPU + {CE7B71E3-F73A-40D1-901C-C20BEFB15851}.Release|Any CPU.ActiveCfg = Release|Any CPU + {CE7B71E3-F73A-40D1-901C-C20BEFB15851}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/Geekbot.net.userprefs b/Geekbot.net.userprefs new file mode 100644 index 0000000..dca434f --- /dev/null +++ b/Geekbot.net.userprefs @@ -0,0 +1,28 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/Geekbot.net/Lib/LevelCalc.cs b/Geekbot.net/Lib/LevelCalc.cs index 1298319..631a5e2 100644 --- a/Geekbot.net/Lib/LevelCalc.cs +++ b/Geekbot.net/Lib/LevelCalc.cs @@ -4,7 +4,7 @@ using System.Text; namespace Geekbot.net.Lib { - class LevelCalc + public class LevelCalc { private static int GetExperienceAtLevel(int level) { diff --git a/Geekbot.net/Modules/Cat.cs b/Geekbot.net/Modules/Cat.cs index 5ca9017..410b53a 100644 --- a/Geekbot.net/Modules/Cat.cs +++ b/Geekbot.net/Modules/Cat.cs @@ -5,21 +5,47 @@ using RestSharp; namespace Geekbot.net.Modules { - public class Cat : ModuleBase + public class Cat : ModuleBase, AsyncReplier { private readonly ICatClient catClient; + private readonly AsyncReplier _asyncReplier; + private readonly Func _requestFunc; + + public class CatResponse + { + public string file { get; set; } + } + public Cat(ICatClient catClient) { this.catClient = catClient; + _asyncReplier = this; + _requestFunc = (() => new RestRequest("meow.php", Method.GET)); } +// +// public Cat(ICatClient catClient, Func requestFunc, AsyncReplier asyncReplier) +// { +// this.catClient = catClient; +// _asyncReplier = asyncReplier ?? this; +// _requestFunc = requestFunc ?? (() => new RestRequest("meow.php", Method.GET)); +// } [Command("cat", RunMode = RunMode.Async), Summary("Return a random image of a cat.")] public async Task Say() { - var request = new RestRequest("meow.php", Method.GET); + var request = _requestFunc(); + var response = catClient.Client.Execute(request); + await _asyncReplier.ReplyAsyncInt(response.Data.file); + } - dynamic response = catClient.Client.Execute(request); - await ReplyAsync(response.Data["file"]); + public async Task ReplyAsyncInt(dynamic data) + { + await ReplyAsync(data); } } + + public interface AsyncReplier + { + Task ReplyAsyncInt(dynamic data); + } } \ No newline at end of file diff --git a/Geekbot.net/Program.cs b/Geekbot.net/Program.cs index 16030ac..ba50465 100755 --- a/Geekbot.net/Program.cs +++ b/Geekbot.net/Program.cs @@ -130,7 +130,7 @@ namespace Geekbot.net } if (!(message.HasCharPrefix('!', ref argPos) || message.HasMentionPrefix(client.CurrentUser, ref argPos))) return; var context = new CommandContext(client, message); - Task.Run(() => commands.ExecuteAsync(context, argPos, map)); + Task.Run(async () => await commands.ExecuteAsync(context, argPos, map)); } public async Task HandleMessageReceived(SocketMessage messsageParam) diff --git a/Tests/Tests.csproj b/Tests/Tests.csproj new file mode 100755 index 0000000..bc2c1ce --- /dev/null +++ b/Tests/Tests.csproj @@ -0,0 +1,25 @@ + + + netcoreapp1.1 + + + + 4.19.2 + + + + 4.7.8 + + + 105.2.4-rc4-24214-01 + + + + + + + {FDCB3D92-E7B5-47BB-A9B5-CFAEFA57CDB4} + Geekbot.net + + + \ No newline at end of file diff --git a/Tests/UnitTest1.cs b/Tests/UnitTest1.cs new file mode 100755 index 0000000..3467b45 --- /dev/null +++ b/Tests/UnitTest1.cs @@ -0,0 +1,52 @@ +using System; +using System.Collections.Generic; +using System.Threading.Tasks; +using FluentAssertions; +using Geekbot.net.Lib; +using Geekbot.net.Modules; +using Moq; +using RestSharp; +using Xunit; + +namespace Tests +{ + public class UnitTest1 + { + [Fact] + public async Task TestCat() + { + // setup + var catClient = new Mock(MockBehavior.Strict); + var client = new Mock(MockBehavior.Strict); + catClient.Setup(cc => cc.Client).Returns(client.Object); + var response = new Mock>(MockBehavior.Strict); + var resultData = new Cat.CatResponse {file = "unit-test"}; + response.SetupGet(r => r.Data).Returns(resultData); + Console.WriteLine(resultData.file); + var request = new Mock(MockBehavior.Strict); + Func requestFunc = () => request.Object; + client.Setup(c => c.Execute(request.Object)).Returns(response.Object); + Mock asyncReplier = new Mock(MockBehavior.Strict); + asyncReplier.Setup(ar => ar.ReplyAsyncInt(resultData.file)).Returns(Task.FromResult(true)).Verifiable(); + + // execute + //var cat = new Cat(catClient.Object, requestFunc, asyncReplier.Object); + //await cat.Say(); + + // validate + //asyncReplier.Verify(); + } + + [Theory] + [InlineData(1, 0)] + [InlineData(33, 4561)] + [InlineData(79, 449702)] + [InlineData(79, 449702 + 1)] + public void TestLevel(int expectedIndex, int experience) + { + var index = LevelCalc.GetLevelAtExperience(experience); + index.Should().Be(expectedIndex); + } + + } +} \ No newline at end of file