You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

Tests.cs 5.8 kB

10 years ago
10 years ago
9 years ago
9 years ago
9 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. using Microsoft.VisualStudio.TestTools.UnitTesting;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Threading;
  6. using System.Threading.Tasks;
  7. namespace Discord.Tests
  8. {
  9. //TODO: Tests are massively incomplete and out of date, needing a full rewrite
  10. [TestClass]
  11. public class Tests
  12. {
  13. private const int EventTimeout = 5000; //Max time in milliseconds to wait for an event response from our test actions
  14. private static DiscordClient _hostClient, _targetBot, _observerBot;
  15. private static Server _testServer;
  16. private static Channel _testServerChannel;
  17. private static Random _random;
  18. [ClassInitialize]
  19. public static void Initialize(TestContext testContext)
  20. {
  21. var settings = Settings.Instance;
  22. _random = new Random();
  23. _hostClient = new DiscordClient();
  24. _targetBot = new DiscordClient();
  25. _observerBot = new DiscordClient();
  26. _hostClient.Connect(settings.User1.Email, settings.User1.Password).Wait();
  27. _targetBot.Connect(settings.User2.Email, settings.User2.Password).Wait();
  28. _observerBot.Connect(settings.User3.Email, settings.User3.Password).Wait();
  29. //Cleanup existing servers
  30. WaitMany(
  31. _hostClient.Servers.Select(x => x.IsOwner ? x.Delete() : x.Leave()),
  32. _targetBot.Servers.Select(x => x.IsOwner ? x.Delete() : x.Leave()),
  33. _observerBot.Servers.Select(x => x.IsOwner ? x.Delete() : x.Leave()));
  34. //Create new server and invite the other bots to it
  35. _testServer = _hostClient.CreateServer("Discord.Net Testing", _hostClient.Regions.First()).Result;
  36. _testServerChannel = _testServer.DefaultChannel;
  37. Invite invite = _testServer.CreateInvite(60, 1, false, false).Result;
  38. WaitAll(
  39. _targetBot.GetInvite(invite.Code).Result.Accept(),
  40. _observerBot.GetInvite(invite.Code).Result.Accept());
  41. }
  42. //Channels
  43. [TestMethod]
  44. public void TestCreateTextChannel()
  45. => TestCreateChannel(ChannelType.Text);
  46. [TestMethod]
  47. public void TestCreateVoiceChannel()
  48. => TestCreateChannel(ChannelType.Voice);
  49. private void TestCreateChannel(ChannelType type)
  50. {
  51. Channel channel = null;
  52. string name = $"#test_{_random.Next()}";
  53. AssertEvent<ChannelEventArgs>(
  54. "ChannelCreated event never received",
  55. async () => channel = await _testServer.CreateChannel(name.Substring(1), type),
  56. x => _targetBot.ChannelCreated += x,
  57. x => _targetBot.ChannelCreated -= x,
  58. (s, e) => e.Channel.Name == name);
  59. AssertEvent<ChannelEventArgs>(
  60. "ChannelDestroyed event never received",
  61. async () => await channel.Delete(),
  62. x => _targetBot.ChannelDestroyed += x,
  63. x => _targetBot.ChannelDestroyed -= x,
  64. (s, e) => e.Channel.Name == name);
  65. }
  66. [TestMethod]
  67. [ExpectedException(typeof(InvalidOperationException))]
  68. public async Task TestCreateChannel_NoName()
  69. {
  70. await _testServer.CreateChannel($"", ChannelType.Text);
  71. }
  72. [TestMethod]
  73. [ExpectedException(typeof(InvalidOperationException))]
  74. public async Task TestCreateChannel_NoType()
  75. {
  76. string name = $"#test_{_random.Next()}";
  77. await _testServer.CreateChannel($"", ChannelType.FromString(""));
  78. }
  79. [TestMethod]
  80. [ExpectedException(typeof(InvalidOperationException))]
  81. public async Task TestCreateChannel_BadType()
  82. {
  83. string name = $"#test_{_random.Next()}";
  84. await _testServer.CreateChannel($"", ChannelType.FromString("badtype"));
  85. }
  86. //Messages
  87. [TestMethod]
  88. public void TestSendMessage()
  89. {
  90. string text = $"test_{_random.Next()}";
  91. AssertEvent<MessageEventArgs>(
  92. "MessageCreated event never received",
  93. () => _testServerChannel.SendMessage(text),
  94. x => _targetBot.MessageReceived += x,
  95. x => _targetBot.MessageReceived -= x,
  96. (s, e) => e.Message.Text == text);
  97. }
  98. [ClassCleanup]
  99. public static void Cleanup()
  100. {
  101. WaitMany(
  102. _hostClient.State == ConnectionState.Connected ? _hostClient.Servers.Select(x => x.IsOwner ? x.Delete() : x.Leave()) : null,
  103. _targetBot.State == ConnectionState.Connected ? _targetBot.Servers.Select(x => x.IsOwner ? x.Delete() : x.Leave()) : null,
  104. _observerBot.State == ConnectionState.Connected ? _observerBot.Servers.Select(x => x.IsOwner ? x.Delete() : x.Leave()) : null);
  105. WaitAll(
  106. _hostClient.Disconnect(),
  107. _targetBot.Disconnect(),
  108. _observerBot.Disconnect());
  109. }
  110. private static void AssertEvent<TArgs>(string msg, Func<Task> action, Action<EventHandler<TArgs>> addEvent, Action<EventHandler<TArgs>> removeEvent, Func<object, TArgs, bool> test = null)
  111. {
  112. AssertEvent(msg, action, addEvent, removeEvent, test, true);
  113. }
  114. private static void AssertNoEvent<TArgs>(string msg, Func<Task> action, Action<EventHandler<TArgs>> addEvent, Action<EventHandler<TArgs>> removeEvent, Func<object, TArgs, bool> test = null)
  115. {
  116. AssertEvent(msg, action, addEvent, removeEvent, test, false);
  117. }
  118. private static void AssertEvent<TArgs>(string msg, Func<Task> action, Action<EventHandler<TArgs>> addEvent, Action<EventHandler<TArgs>> removeEvent, Func<object, TArgs, bool> test, bool assertTrue)
  119. {
  120. ManualResetEventSlim trigger = new ManualResetEventSlim(false);
  121. bool result = false;
  122. EventHandler<TArgs> handler = (s, e) =>
  123. {
  124. if (test != null)
  125. {
  126. result |= test(s, e);
  127. trigger.Set();
  128. }
  129. else
  130. result = true;
  131. };
  132. addEvent(handler);
  133. var task = action();
  134. trigger.Wait(EventTimeout);
  135. task.Wait();
  136. removeEvent(handler);
  137. Assert.AreEqual(assertTrue, result, msg);
  138. }
  139. private static void WaitAll(params Task[] tasks)
  140. {
  141. Task.WaitAll(tasks);
  142. }
  143. private static void WaitAll(IEnumerable<Task> tasks)
  144. {
  145. Task.WaitAll(tasks.ToArray());
  146. }
  147. private static void WaitMany(params IEnumerable<Task>[] tasks)
  148. {
  149. Task.WaitAll(tasks.Where(x => x != null).SelectMany(x => x).ToArray());
  150. }
  151. }
  152. }