From 8f0d77749d1a31fe352fabbb7f4de20fe57ba548 Mon Sep 17 00:00:00 2001 From: Christopher F Date: Fri, 13 May 2016 20:58:06 -0400 Subject: [PATCH] Begin Rewriting Unit Tests Comment out Tests.cs (will delete later) Build backend for Testing/Mock Rest Replies --- Discord.Net.sln | 8 +++- .../Discord.Net.Tests.csproj | 12 ++++-- .../Discord.Net.Tests/Rest/EndpointHandler.cs | 32 ++++++++++++++ test/Discord.Net.Tests/Rest/TestRestClient.cs | 42 +++++++++++++++++++ test/Discord.Net.Tests/Tests.cs | 20 +++++---- 5 files changed, 103 insertions(+), 11 deletions(-) create mode 100644 test/Discord.Net.Tests/Rest/EndpointHandler.cs create mode 100644 test/Discord.Net.Tests/Rest/TestRestClient.cs diff --git a/Discord.Net.sln b/Discord.Net.sln index 520566a3d..1903df403 100644 --- a/Discord.Net.sln +++ b/Discord.Net.sln @@ -1,10 +1,12 @@  Microsoft Visual Studio Solution File, Format Version 12.00 # Visual Studio 15 -VisualStudioVersion = 15.0.25123.0 +VisualStudioVersion = 15.0.25302.0 MinimumVisualStudioVersion = 10.0.40219.1 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Discord.Net", "src\Discord.Net\Discord.Net.csproj", "{18F6FE23-73F6-4CA6-BBD9-F0139DC3EE90}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Discord.Net.Tests", "test\Discord.Net.Tests\Discord.Net.Tests.csproj", "{855D6B1D-847B-42DA-BE6A-23683EA89511}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -15,6 +17,10 @@ Global {18F6FE23-73F6-4CA6-BBD9-F0139DC3EE90}.Debug|Any CPU.Build.0 = Debug|Any CPU {18F6FE23-73F6-4CA6-BBD9-F0139DC3EE90}.Release|Any CPU.ActiveCfg = Release|Any CPU {18F6FE23-73F6-4CA6-BBD9-F0139DC3EE90}.Release|Any CPU.Build.0 = Release|Any CPU + {855D6B1D-847B-42DA-BE6A-23683EA89511}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {855D6B1D-847B-42DA-BE6A-23683EA89511}.Debug|Any CPU.Build.0 = Debug|Any CPU + {855D6B1D-847B-42DA-BE6A-23683EA89511}.Release|Any CPU.ActiveCfg = Release|Any CPU + {855D6B1D-847B-42DA-BE6A-23683EA89511}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/test/Discord.Net.Tests/Discord.Net.Tests.csproj b/test/Discord.Net.Tests/Discord.Net.Tests.csproj index 2a50610cc..3526e12ca 100644 --- a/test/Discord.Net.Tests/Discord.Net.Tests.csproj +++ b/test/Discord.Net.Tests/Discord.Net.Tests.csproj @@ -55,6 +55,11 @@ + + + + + @@ -62,11 +67,12 @@ - - {c6a50d24-cbd3-4e76-852c-4dca60bbd608} - Discord.Net.Net45 + + {18F6FE23-73F6-4CA6-BBD9-F0139DC3EE90} + Discord.Net + diff --git a/test/Discord.Net.Tests/Rest/EndpointHandler.cs b/test/Discord.Net.Tests/Rest/EndpointHandler.cs new file mode 100644 index 000000000..318139d13 --- /dev/null +++ b/test/Discord.Net.Tests/Rest/EndpointHandler.cs @@ -0,0 +1,32 @@ +using System; +using System.Collections.Generic; + +namespace Discord.Tests.Rest +{ + public class EndpointHandler + { + public static EndpointHandler Instance; + + public delegate string RestMessageHandler(string method, string json); + + public Dictionary Handlers; + + public EndpointHandler() + { + Instance = this; + + // Setup Endpoints + Handlers = new Dictionary(); + + // /users Endpoints + Handlers.Add("users/@me", Responses.Users.UserHandlers.Me_Handler); + } + + public string HandleMessage(string method, string endpoint, string json) + { + if (Handlers.ContainsKey(endpoint)) + return Handlers[endpoint].Invoke(method, json); + throw new NotImplementedException($"{method} -> {endpoint} -> {json}"); + } + } +} diff --git a/test/Discord.Net.Tests/Rest/TestRestClient.cs b/test/Discord.Net.Tests/Rest/TestRestClient.cs new file mode 100644 index 000000000..a1b4f1b10 --- /dev/null +++ b/test/Discord.Net.Tests/Rest/TestRestClient.cs @@ -0,0 +1,42 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Discord.Rest; +using Discord.Net.Rest; +using System.Threading; +using System.IO; + +namespace Discord.Tests.Rest +{ + class TestRestClient : IRestClient + { + public static Dictionary Headers = new Dictionary(); + + public TestRestClient(string baseUrl, CancellationToken cancelToken) + { + + } + + Task IRestClient.Send(string method, string endpoint, IReadOnlyDictionary multipartParams) + { + throw new NotImplementedException("method only used for SendFile, not concerned with that yet."); + } + + Task IRestClient.Send(string method, string endpoint, string json) + { + return Task.FromResult(new MemoryStream(Encoding.UTF8.GetBytes(EndpointHandler.Instance.HandleMessage(method, endpoint, json)))); + } + + void IRestClient.SetHeader(string key, string value) + { + if (Headers.ContainsKey(key)) + { + Headers.Remove(key); + } + Headers.Add(key, value); + Console.WriteLine($"[Header Set]: {key} -> {value}"); + } + } +} diff --git a/test/Discord.Net.Tests/Tests.cs b/test/Discord.Net.Tests/Tests.cs index d8d09cd3d..1b51a4c94 100644 --- a/test/Discord.Net.Tests/Tests.cs +++ b/test/Discord.Net.Tests/Tests.cs @@ -1,4 +1,5 @@ -using Microsoft.VisualStudio.TestTools.UnitTesting; +using Discord.Rest; +using Microsoft.VisualStudio.TestTools.UnitTesting; using System; using System.Collections.Generic; using System.Linq; @@ -7,8 +8,11 @@ using System.Threading.Tasks; namespace Discord.Tests { - //TODO: Tests are massively incomplete and out of date, needing a full rewrite + // these tests are really bad + // we're never going to look at them again + // but in case i do decide to look at them again they are still here + /* [TestClass] public class Tests { @@ -311,8 +315,9 @@ namespace Discord.Tests async () => await _targetBot.CurrentUser.Modify(TargetPassword, name), x => _obGuildBot.UserUpdated += x, x => _obGuildBot.UserUpdated -= x, - (s, e) => e.After.Username == name);*/ - } + (s, e) => e.After.Username == name); + }*/ + /* [TestMethod] public void TestSetStatus() { @@ -327,8 +332,9 @@ namespace Discord.Tests { throw new NotImplementedException(); /*_client.SetStatus(status); - await Task.Delay(50);*/ - } + await Task.Delay(50); + }*/ + /* [TestMethod] public void TestSetGame() { @@ -490,5 +496,5 @@ namespace Discord.Tests } #endregion - } + }*/ }