Adding Unit Tests

This commit is contained in:
dboerlage 2017-04-27 12:08:24 +02:00
parent 68b6312635
commit 476ed37d20
No known key found for this signature in database
GPG key ID: BDA07B7D3FCF147F
9 changed files with 198 additions and 7 deletions

2
.gitignore vendored
View file

@ -1,5 +1,7 @@
Geekbot.net/bin
Geekbot.net/obj
Tests/bin
Tests/obj
Backup/
.vs/
UpgradeLog.htm

52
.vscode/launch.json vendored Normal file
View file

@ -0,0 +1,52 @@
{
"version": "0.2.0",
"configurations": [
{
"name": ".NET Core Launch (console)",
"type": "coreclr",
"request": "launch",
"preLaunchTask": "build",
"program": "${workspaceRoot}/bin/Debug/<target-framework>/<project-name.dll>",
"args": [],
"cwd": "${workspaceRoot}",
"stopAtEntry": false,
"console": "internalConsole"
},
{
"name": ".NET Core Launch (web)",
"type": "coreclr",
"request": "launch",
"preLaunchTask": "build",
"program": "${workspaceRoot}/bin/Debug/<target-framework>/<project-name.dll>",
"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}"
}
]
}

View file

@ -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

28
Geekbot.net.userprefs Normal file
View file

@ -0,0 +1,28 @@
<Properties StartupConfiguration="{FDCB3D92-E7B5-47BB-A9B5-CFAEFA57CDB4}|Default">
<MonoDevelop.Ide.Workbench ActiveDocument="Tests/UnitTest1.cs">
<Files>
<File FileName="Tests/UnitTest1.cs" Line="1" Column="1" />
</Files>
<Pads>
<Pad Id="MonoDevelop.UnitTesting.TestPad">
<State name="__root__">
<Node name="Geekbot.net" expanded="True">
<Node name="Tests" expanded="True">
<Node name="Tests" expanded="True">
<Node name="UnitTest1" selected="True" />
</Node>
</Node>
</Node>
</State>
</Pad>
</Pads>
</MonoDevelop.Ide.Workbench>
<MonoDevelop.Ide.Workspace ActiveConfiguration="Debug" />
<MonoDevelop.Ide.DebuggingService.Breakpoints>
<BreakpointStore>
<Breakpoint file="/Users/dboerlage/dev/priv/Geekbot.net/Tests/UnitTest1.cs" relfile="Tests/UnitTest1.cs" line="27" column="1" />
</BreakpointStore>
</MonoDevelop.Ide.DebuggingService.Breakpoints>
<MonoDevelop.Ide.DebuggingService.PinnedWatches />
<MultiItemStartupConfigurations />
</Properties>

View file

@ -4,7 +4,7 @@ using System.Text;
namespace Geekbot.net.Lib
{
class LevelCalc
public class LevelCalc
{
private static int GetExperienceAtLevel(int level)
{

View file

@ -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<IRestRequest> _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<IRestRequest> 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<CatResponse>(request);
await _asyncReplier.ReplyAsyncInt(response.Data.file);
}
dynamic response = catClient.Client.Execute<dynamic>(request);
await ReplyAsync(response.Data["file"]);
public async Task ReplyAsyncInt(dynamic data)
{
await ReplyAsync(data);
}
}
public interface AsyncReplier
{
Task ReplyAsyncInt(dynamic data);
}
}

View file

@ -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)

25
Tests/Tests.csproj Executable file
View file

@ -0,0 +1,25 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp1.1</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="FluentAssertions">
<Version>4.19.2</Version>
</PackageReference>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.0.0" />
<PackageReference Include="Moq">
<Version>4.7.8</Version>
</PackageReference>
<PackageReference Include="RestSharp.NetCore">
<Version>105.2.4-rc4-24214-01</Version>
</PackageReference>
<PackageReference Include="xunit" Version="2.2.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.2.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Geekbot.net\Geekbot.net.csproj">
<Project>{FDCB3D92-E7B5-47BB-A9B5-CFAEFA57CDB4}</Project>
<Name>Geekbot.net</Name>
</ProjectReference>
</ItemGroup>
</Project>

52
Tests/UnitTest1.cs Executable file
View file

@ -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<ICatClient>(MockBehavior.Strict);
var client = new Mock<IRestClient>(MockBehavior.Strict);
catClient.Setup(cc => cc.Client).Returns(client.Object);
var response = new Mock<IRestResponse<Cat.CatResponse>>(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<IRestRequest>(MockBehavior.Strict);
Func<IRestRequest> requestFunc = () => request.Object;
client.Setup(c => c.Execute<Cat.CatResponse>(request.Object)).Returns(response.Object);
Mock<AsyncReplier> asyncReplier = new Mock<AsyncReplier>(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);
}
}
}