From bf41b30a9a25f1a6e9f3bbf8e993829c8ca43067 Mon Sep 17 00:00:00 2001 From: Christopher F Date: Mon, 14 Mar 2016 19:24:47 -0400 Subject: [PATCH] Added more Unit Tests for Messages, Channels; Removed Settings - Test for Server.GetChannel - Test for SendTyping [ChannelUserEventArgs] - Test for EditChannel [ChannelUpdatedEventArgs] - Test for Channel Mentions - Test for DownloadMessages with/without cache - Test for SendTTSMessage --- test/Discord.Net.Tests/Discord.Net.Tests.csproj | 1 - test/Discord.Net.Tests/Settings.cs | 32 -------- test/Discord.Net.Tests/Tests.cs | 104 +++++++++++++++++++++++- 3 files changed, 101 insertions(+), 36 deletions(-) delete mode 100644 test/Discord.Net.Tests/Settings.cs diff --git a/test/Discord.Net.Tests/Discord.Net.Tests.csproj b/test/Discord.Net.Tests/Discord.Net.Tests.csproj index 0c13b7075..0136c7ea9 100644 --- a/test/Discord.Net.Tests/Discord.Net.Tests.csproj +++ b/test/Discord.Net.Tests/Discord.Net.Tests.csproj @@ -56,7 +56,6 @@ - diff --git a/test/Discord.Net.Tests/Settings.cs b/test/Discord.Net.Tests/Settings.cs deleted file mode 100644 index 5aa37e184..000000000 --- a/test/Discord.Net.Tests/Settings.cs +++ /dev/null @@ -1,32 +0,0 @@ -using Newtonsoft.Json; -using System.IO; - -namespace Discord.Tests -{ - internal class Settings - { - private const string path = "../../config.json"; - public static readonly Settings Instance; - static Settings() - { - if (!File.Exists(path)) - throw new FileNotFoundException("config.json is missing, rename config.json.example and add credentials for three separate unused accounts for testing."); - Instance = JsonConvert.DeserializeObject(File.ReadAllText(path)); - } - - public class Account - { - [JsonProperty("email")] - public string Email { get; set; } - [JsonProperty("password")] - public string Password { get; set; } - } - - [JsonProperty("user1")] - public Account User1 { get; set; } - [JsonProperty("user2")] - public Account User2 { get; set; } - [JsonProperty("user3")] - public Account User3 { get; set; } - } -} diff --git a/test/Discord.Net.Tests/Tests.cs b/test/Discord.Net.Tests/Tests.cs index 20aac66ce..e1e7dba4d 100644 --- a/test/Discord.Net.Tests/Tests.cs +++ b/test/Discord.Net.Tests/Tests.cs @@ -28,6 +28,10 @@ namespace Discord.Tests private static string TargetEmail; private static string TargetPassword; + public static string RandomText => $"test_{_random.Next()}"; + + #region Initialization + [ClassInitialize] public static void Initialize(TestContext testContext) { @@ -61,7 +65,6 @@ namespace Discord.Tests //Create new server and invite the other bots to it _testServer = await _hostClient.CreateServer("Discord.Net Testing", _hostClient.Regions.First()); - _testServerChannel = _testServer.DefaultChannel; await Task.Delay(1000); @@ -86,7 +89,6 @@ namespace Discord.Tests await (await _observerBot.GetInvite(_testServerInvite.Code)).Accept(); } - // READY [TestMethod] [Priority(2)] public void TestReady() @@ -100,10 +102,15 @@ namespace Discord.Tests true); _targetBot.Servers.Select(x => x.IsOwner ? x.Delete() : x.Leave()); + _testServerChannel = _testServer.DefaultChannel; } + #endregion + // Servers + #region Server Tests + [TestMethod] [Priority(3)] public void TestJoinedServer() @@ -115,6 +122,10 @@ namespace Discord.Tests x => _targetBot.JoinedServer -= x); } + #endregion + + #region Channel Tests + //Channels [TestMethod] public void TestCreateTextChannel() @@ -164,6 +175,49 @@ namespace Discord.Tests string name = $"#test_{_random.Next()}"; await _testServer.CreateChannel($"", ChannelType.FromString("badtype")); } + [TestMethod] + public async Task Test_CreateGetChannel() + { + var name = $"test_{_random.Next()}"; + var channel = await _testServer.CreateChannel(name, ChannelType.Text); + var get_channel = _testServer.GetChannel(channel.Id); + Assert.AreEqual(channel.Id, get_channel.Id, "ID of Channel and GetChannel were not equal."); + } + [TestMethod] + public void TestSendTyping() + { + var channel = _testServerChannel; + AssertEvent( + "UserUpdated event never fired.", + async () => await channel.SendIsTyping(), + x => _targetBot.UserIsTyping += x, + x => _targetBot.UserIsTyping -= x); + } + [TestMethod] + public void TestEditChannel() + { + var channel = _testServerChannel; + AssertEvent( + "ChannelUpdated Never Received", + async () => await channel.Edit(RandomText, $"topic - {RandomText}", 26), + x => _targetBot.ChannelUpdated += x, + x => _targetBot.ChannelUpdated -= x); + } + [TestMethod] + public void TestChannelMention() + { + var channel = _testServerChannel; + Assert.AreEqual($"<#{channel.Id}>", channel.Mention, "Generated channel mention was not the expected channel mention."); + } + [TestMethod] + public void TestChannelUserCount() + { + Assert.AreEqual(3, _testServerChannel.Users.Count(), "Read an incorrect number of users in a channel"); + } + + #endregion + + #region Message Tests //Messages [TestMethod] @@ -195,7 +249,42 @@ namespace Discord.Tests x => _targetBot.MessageDeleted -= x, (s, e) => e.Message.Id == message.Id); } - + [TestMethod] + public async Task TestDownloadMessages_WithCache() + { + string name = $"test_{_random.Next()}"; + var channel = await _testServer.CreateChannel(name, ChannelType.Text); + for (var i = 0; i < 10; i++) await channel.SendMessage(RandomText); + while (channel.Client.MessageQueue.Count > 0) await Task.Delay(100); + var messages = await channel.DownloadMessages(10); + Assert.AreEqual(10, messages.Count(), "Expected 10 messages in downloaded array, did not see 10."); + } + [TestMethod] + public async Task TestDownloadMessages_WithoutCache() + { + string name = $"test_{_random.Next()}"; + var channel = await _testServer.CreateChannel(name, ChannelType.Text); + for (var i = 0; i < 10; i++) await channel.SendMessage(RandomText); + while (channel.Client.MessageQueue.Count > 0) await Task.Delay(100); + var messages = await channel.DownloadMessages(10, useCache: false); + Assert.AreEqual(10, messages.Count(), "Expected 10 messages in downloaded array, did not see 10."); + } + [TestMethod] + public async Task TestSendTTSMessage() + { + var channel = await _testServer.CreateChannel(RandomText, ChannelType.Text); + AssertEvent( + "MessageCreated event never fired", + async () => await channel.SendTTSMessage(RandomText), + x => _targetBot.MessageReceived += x, + x => _targetBot.MessageReceived -= x, + (s, e) => e.Message.IsTTS); + } + + #endregion + + #region Permission Tests + // Permissions [TestMethod] public async Task Test_AddGet_PermissionsRule() @@ -242,6 +331,9 @@ namespace Discord.Tests await channel.AddPermissionsRule(user, perms); } + #endregion + + [ClassCleanup] public static void Cleanup() { @@ -256,6 +348,10 @@ namespace Discord.Tests _observerBot.Disconnect()); } + #region Helpers + + // Task Helpers + private static void AssertEvent(string msg, Func action, Action> addEvent, Action> removeEvent, Func test = null) { AssertEvent(msg, action, addEvent, removeEvent, test, true); @@ -326,5 +422,7 @@ namespace Discord.Tests { Task.WaitAll(tasks.Where(x => x != null).SelectMany(x => x).ToArray()); } + + #endregion } }