summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authoruzvkl <dennis.kobert@student.kit.edu>2019-05-20 03:00:44 +0200
committeruzvkl <dennis.kobert@student.kit.edu>2019-05-20 03:00:44 +0200
commitd51f315a8da246c3852017b4a1c127a29a43811f (patch)
treef28fdc31578f4bbd9e3f1fb9a8b2ef9591634670
parented26623e17e8dfcc036f88cca6de10d5a35697ec (diff)
Begin Testing
-rw-r--r--DSALib/Auxiliary/Calculator/StringSolver.cs4
-rw-r--r--DSALib/Auxiliary/Dice.cs10
-rw-r--r--DiscoBot.sln8
-rw-r--r--DiscordBot/CommandHandler.cs100
-rw-r--r--NUnitTestProject1/Auxiliary/Calculator/StringSolverTests.cs105
-rw-r--r--NUnitTestProject1/Auxiliary/DiceTests.cs71
-rw-r--r--NUnitTestProject1/NUnitTest.csproj20
7 files changed, 261 insertions, 57 deletions
diff --git a/DSALib/Auxiliary/Calculator/StringSolver.cs b/DSALib/Auxiliary/Calculator/StringSolver.cs
index bf903da..45d6a54 100644
--- a/DSALib/Auxiliary/Calculator/StringSolver.cs
+++ b/DSALib/Auxiliary/Calculator/StringSolver.cs
@@ -67,7 +67,7 @@ namespace DSALib.Auxiliary.Calculator
}
}
- return string.Empty;
+ throw new ArgumentException("Invalid brace sequence");
}
private static Ops GetOps(char c)
@@ -120,7 +120,7 @@ namespace DSALib.Auxiliary.Calculator
switch (c)
{
case ')':
- throw new ArgumentException("Unmögliche Anordnung von Klammern");
+ throw new ArgumentException("Invalid brace sequence");
case '(':
arguments.Add(new StringSolver(GetInner(ref workInput)));
index = -1;
diff --git a/DSALib/Auxiliary/Dice.cs b/DSALib/Auxiliary/Dice.cs
index b07d47f..0bfabeb 100644
--- a/DSALib/Auxiliary/Dice.cs
+++ b/DSALib/Auxiliary/Dice.cs
@@ -15,18 +15,20 @@ namespace DSALib.Auxiliary
public static int Roll(string input)
{
var strings = input.ToLower().Split(new[] {'w', 'd'}, 2, StringSplitOptions.RemoveEmptyEntries).ToList();
- var count = Convert.ToInt32(strings[0]);
- var d = Convert.ToInt32(strings[0]);
+
if (strings.Count != 2)
- throw new ArgumentException($"{input}: erfüllt nicht die Formatvogaben( Anzahl d Augenzahl)");
+ throw new ArgumentException($"{input}: does not satisfy the format requirements( dice count (d|w) die size)");
+
+ var count = Convert.ToInt32(strings[0]);
+ var d = Convert.ToInt32(strings[0]);
return Roll(count, d);
}
public static int Roll(int count, int d)
{
- if (d <= 0) return 0;
+ if (d <= 0 || count <= 0) return 0;
var sum = 0;
for (var i = 0; i < Math.Abs(count); i++)
diff --git a/DiscoBot.sln b/DiscoBot.sln
index ffc5896..72ea5dd 100644
--- a/DiscoBot.sln
+++ b/DiscoBot.sln
@@ -9,7 +9,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FireBase", "FireBase\FireBa
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DSALib", "DSALib\DSALib.csproj", "{C5D9AFDF-70E2-4A47-96FF-1EC47C1DE38D}"
EndProject
-Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DiscordBot", "DiscordBot\DiscordBot.csproj", "{F1418B62-F043-4761-9BDD-AE078B6A99FB}"
+Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DiscordBot", "DiscordBot\DiscordBot.csproj", "{F1418B62-F043-4761-9BDD-AE078B6A99FB}"
+EndProject
+Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NUnitTest", "NUnitTestProject1\NUnitTest.csproj", "{CF821E64-B50E-420F-98A2-07315B362ED0}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@@ -33,6 +35,10 @@ Global
{F1418B62-F043-4761-9BDD-AE078B6A99FB}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F1418B62-F043-4761-9BDD-AE078B6A99FB}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F1418B62-F043-4761-9BDD-AE078B6A99FB}.Release|Any CPU.Build.0 = Release|Any CPU
+ {CF821E64-B50E-420F-98A2-07315B362ED0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+ {CF821E64-B50E-420F-98A2-07315B362ED0}.Debug|Any CPU.Build.0 = Debug|Any CPU
+ {CF821E64-B50E-420F-98A2-07315B362ED0}.Release|Any CPU.ActiveCfg = Release|Any CPU
+ {CF821E64-B50E-420F-98A2-07315B362ED0}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
diff --git a/DiscordBot/CommandHandler.cs b/DiscordBot/CommandHandler.cs
index 0f6aa7e..b41756f 100644
--- a/DiscordBot/CommandHandler.cs
+++ b/DiscordBot/CommandHandler.cs
@@ -1,32 +1,30 @@
-
+#region
+
using System;
using System.Collections.Generic;
-using System.IO;
using System.Linq;
-using System.Net;
using System.Net.Http;
using System.Reflection;
using System.Threading.Tasks;
-using Discord;
using Discord.Commands;
using Discord.WebSocket;
+#endregion
+
namespace DiscordBot
{
public class CommandHandler
{
+ private static readonly HttpClient _HttpClient = new HttpClient();
private readonly DiscordSocketClient _client;
private readonly CommandService _commands;
- private static readonly HttpClient _HttpClient = new HttpClient();
- public CommandHandler(DiscordSocketClient client, CommandService commands)
- {
+ public CommandHandler(DiscordSocketClient client, CommandService commands) {
_commands = commands;
_client = client;
}
- public async Task InstallCommandsAsync()
- {
+ public Task InstallCommandsAsync() {
// Hook the MessageReceived event into our command handler
_client.MessageReceived += HandleCommandAsync;
@@ -38,24 +36,49 @@ namespace DiscordBot
//
// If you do not use Dependency Injection, pass null.
// See Dependency Injection guide for more information.
- await _commands.AddModulesAsync(assembly: Assembly.GetEntryAssembly(),
- services: null);
+ return _commands.AddModulesAsync(Assembly.GetEntryAssembly(),
+ null);
+ }
+
+
+ private static async Task<string> SendCommand(string name, string command, string url) {
+ command = command.Remove(0, 1);
+ var args = command.Split(new[] {' '}, StringSplitOptions.RemoveEmptyEntries);
+
+ string cmdContent = string.Empty;
+ if (args.Length > 1) {
+ cmdContent = "\"" + args.Skip(1).Aggregate((s, n) => s + "\", \"" + n) + "\"";
+ }
+
+ var values = new Dictionary<string, string> {
+ {"Name", name},
+ {"CmdIdentifier", args.First()},
+ {"CmdTexts", "[" + cmdContent + "]"}
+ };
+
+ var content = new FormUrlEncodedContent(values);
+
+ var response = await _HttpClient.PostAsync(url, content);
+
+ return await response.Content.ReadAsStringAsync();
}
- private async Task HandleCommandAsync(SocketMessage messageParam)
- {
+ private async Task HandleCommandAsync(SocketMessage messageParam) {
// Don't process the command if it was a system message
var message = messageParam as SocketUserMessage;
- if (message == null) return;
+ if (message == null) {
+ return;
+ }
// Create a number to track where the prefix ends and the command begins
- int argPos = 0;
+ var argPos = 0;
// Determine if the message is a command based on the prefix and make sure no bots trigger commands
if (!(message.HasCharPrefix('!', ref argPos) ||
- message.HasMentionPrefix(_client.CurrentUser, ref argPos)) ||
- message.Author.IsBot)
+ message.HasMentionPrefix(_client.CurrentUser, ref argPos)) ||
+ message.Author.IsBot) {
return;
+ }
// Create a WebSocket-based command context based on the message
var context = new SocketCommandContext(_client, message);
@@ -66,47 +89,24 @@ namespace DiscordBot
// Keep in mind that result does not indicate a return value
// rather an object stating if the command executed successfully.
var result = await _commands.ExecuteAsync(
- context: context,
- argPos: argPos,
- services: null);
+ context,
+ argPos,
+ null);
// Optionally, we may inform the user if the command fails
// to be executed; however, this may not always be desired,
// as it may clog up the request queue should a user spam a
// command.
- if (result.Error == CommandError.UnknownCommand)
- {
- var response = await SendCommand(message.Author.Username, message.Content,"https://kobert.dev/api/dsa/commands");
+ if (result.Error == CommandError.UnknownCommand) {
+ string response = await SendCommand(message.Author.Username, message.Content,
+ "https://kobert.dev/api/dsa/commands");
//var response = "invalid";
await context.Channel.SendMessageAsync(response);
}
- else if (!result.IsSuccess) await context.Channel.SendMessageAsync(result.ErrorReason);
- }
-
-
-
- private static async Task<string> SendCommand(string name, string command, string url)
- {
- command = command.Remove(0, 1);
- var args = command.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
-
- var cmdContent = string.Empty;
- if (args.Length > 1) cmdContent = "\"" + args.Skip(1).Aggregate((s, n) => s + "\", \"" + n) + "\"";
-
- var values = new Dictionary<string, string>
- {
- { "Name", name },
- { "CmdIdentifier", args.First()},
- { "CmdTexts", "[" + cmdContent + "]"}
- };
-
- var content = new FormUrlEncodedContent(values);
-
- var response = await _HttpClient.PostAsync(url, content);
-
- return await response.Content.ReadAsStringAsync();
+ else if (!result.IsSuccess) {
+ await context.Channel.SendMessageAsync(result.ErrorReason);
+ }
}
-
}
-}
+} \ No newline at end of file
diff --git a/NUnitTestProject1/Auxiliary/Calculator/StringSolverTests.cs b/NUnitTestProject1/Auxiliary/Calculator/StringSolverTests.cs
new file mode 100644
index 0000000..998a78b
--- /dev/null
+++ b/NUnitTestProject1/Auxiliary/Calculator/StringSolverTests.cs
@@ -0,0 +1,105 @@
+using DSALib.Auxiliary.Calculator;
+using Moq;
+using NUnit.Framework;
+
+namespace NUnitTest.Auxiliary.Calculator
+{
+ [TestFixture]
+ public class StringSolverTests
+ {
+ private MockRepository mockRepository;
+
+
+
+ [SetUp]
+ public void SetUp()
+ {
+ this.mockRepository = new MockRepository(MockBehavior.Strict);
+
+
+ }
+
+ [TearDown]
+ public void TearDown()
+ {
+ this.mockRepository.VerifyAll();
+ }
+
+ private StringSolver CreateStringSolver(string input)
+ {
+ return new StringSolver(input);
+ }
+
+ [Test]
+ public void Solve_StateUnderTest_ExpectedBehavior()
+ {
+ // Arrange
+ var unitUnderTest = this.CreateStringSolver("1+1");
+
+ // Act
+ var result = unitUnderTest.Solve();
+
+ // Assert
+ Assert.AreEqual(2,result);
+ }
+
+ [Test]
+ public void Solve_mult()
+ {
+ // Arrange
+ var unitUnderTest = this.CreateStringSolver("1+1-4*6+2");
+
+ // Act
+ var result = unitUnderTest.Solve();
+
+ // Assert
+ Assert.AreEqual(-20, result);
+ }
+
+ [Test]
+ public void Solve_braces()
+ {
+ // Arrange
+ var unitUnderTest = this.CreateStringSolver("1+(1-4)*6+2");
+
+ // Act
+ var result = unitUnderTest.Solve();
+
+ // Assert
+ Assert.AreEqual(-15, result);
+ }
+
+ [Test]
+ public void Solve_wrong_braces()
+ {
+ // Arrange
+ var unitUnderTest = this.CreateStringSolver("1+)(1-4)*6+2");
+
+ // Act
+ Assert.Throws<System.ArgumentException>(() =>unitUnderTest.Solve(), "Invalid brace sequence");
+ }
+
+ [Test, MaxTime(200)]
+ public void Solve_braces_timeout()
+ {
+ // Arrange
+ var unitUnderTest = this.CreateStringSolver("1+(1-(4)*6+2");
+
+ // Act
+ Assert.Throws<System.ArgumentException>(() => unitUnderTest.Solve(), "Invalid brace sequence");
+ }
+
+ [Test]
+ public void ToString_StateUnderTest_ExpectedBehavior()
+ {
+ // Arrange
+ var unitUnderTest = this.CreateStringSolver("3+-4");
+
+ // Act
+ var result = unitUnderTest.ToString();
+
+ // Assert
+ Assert.AreEqual("(0+3+-4)", result);
+ }
+ }
+}
diff --git a/NUnitTestProject1/Auxiliary/DiceTests.cs b/NUnitTestProject1/Auxiliary/DiceTests.cs
new file mode 100644
index 0000000..6b37492
--- /dev/null
+++ b/NUnitTestProject1/Auxiliary/DiceTests.cs
@@ -0,0 +1,71 @@
+using DSALib.Auxiliary;
+using Moq;
+using NUnit.Framework;
+using System;
+
+namespace NUnitTest.Auxiliary
+{
+ [TestFixture]
+ public class DiceTests
+ {
+ private MockRepository mockRepository;
+
+
+
+ [SetUp]
+ public void SetUp()
+ {
+ this.mockRepository = new MockRepository(MockBehavior.Strict);
+
+
+ }
+
+ [TearDown]
+ public void TearDown()
+ {
+ this.mockRepository.VerifyAll();
+ }
+
+ private void CreateDice()
+ {}
+
+ [Test]
+ public void Roll_StateUnderTest_ExpectedBehavior()
+ {
+ // Arrange
+ int d = 20;
+
+ // Act
+ var result = Dice.Roll(d);
+
+ // Assert
+ Assert.True(result > 0 && result < d+1);
+ }
+
+ [Test]
+ public void Roll_StateUnderTest_ExpectedBehavior1()
+ {
+ // Arrange
+ string input = "w";
+
+ // Act
+ Assert.Throws<ArgumentException>( () => Dice.Roll(input));
+ }
+
+ [Test]
+ public void Roll_zero_dice()
+ {
+ // Arrange
+ int count = 0;
+ int d = 2;
+
+ // Act
+ var result = Dice.Roll(
+ count,
+ d);
+
+ // Assert
+ Assert.AreEqual(0, result);
+ }
+ }
+}
diff --git a/NUnitTestProject1/NUnitTest.csproj b/NUnitTestProject1/NUnitTest.csproj
new file mode 100644
index 0000000..7b3b1ea
--- /dev/null
+++ b/NUnitTestProject1/NUnitTest.csproj
@@ -0,0 +1,20 @@
+<Project Sdk="Microsoft.NET.Sdk">
+
+ <PropertyGroup>
+ <TargetFramework>netcoreapp2.2</TargetFramework>
+
+ <IsPackable>false</IsPackable>
+ </PropertyGroup>
+
+ <ItemGroup>
+ <PackageReference Include="Moq" Version="4.5.28" />
+ <PackageReference Include="nunit" Version="3.11.0" />
+ <PackageReference Include="NUnit3TestAdapter" Version="3.11.0" />
+ <PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.9.0" />
+ </ItemGroup>
+
+ <ItemGroup>
+ <ProjectReference Include="..\DSALib\DSALib.csproj" />
+ </ItemGroup>
+
+</Project>