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 3.2 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. using Microsoft.VisualStudio.TestTools.UnitTesting;
  2. using System;
  3. using System.Linq;
  4. using System.Threading;
  5. using System.Threading.Tasks;
  6. namespace Discord.Net.Tests
  7. {
  8. [TestClass]
  9. public class Tests
  10. {
  11. private const int EventTimeout = 5000; //Max time in milliseconds to wait for an event response from our test actions
  12. private DiscordClient _bot1, _bot2;
  13. private Server _testServer;
  14. private Channel _testServerChannel;
  15. private Random _random;
  16. [TestInitialize]
  17. public void Initialize()
  18. {
  19. _random = new Random();
  20. _bot1 = new DiscordClient();
  21. _bot2 = new DiscordClient();
  22. _bot1.Connect(Settings.Test1_Username, Settings.Test1_Password).Wait();
  23. _bot2.Connect(Settings.Test2_Username, Settings.Test2_Password).Wait();
  24. //Cleanup existing servers
  25. Task.WaitAll(_bot1.Servers.Select(x => _bot1.LeaveServer(x)).ToArray());
  26. Task.WaitAll(_bot2.Servers.Select(x => _bot2.LeaveServer(x)).ToArray());
  27. //Create new server and invite other bot to it
  28. _testServer = _bot1.CreateServer("Discord.Net Testbed", Regions.US_East).Result;
  29. _testServerChannel = _testServer.DefaultChannel;
  30. Invite invite = _bot1.CreateInvite(_testServer, 60, 1, false, false).Result;
  31. _bot2.AcceptInvite(invite).Wait();
  32. }
  33. [TestMethod]
  34. public void TestSendMessage()
  35. {
  36. string text = $"test_{_random.Next()}";
  37. AssertEvent<DiscordClient.MessageEventArgs>(
  38. "MessageCreated event never received",
  39. () => _bot1.SendMessage(_testServerChannel, text),
  40. x => _bot2.MessageCreated += x,
  41. x => _bot2.MessageCreated -= x,
  42. (s, e) => e.Message.Text == text);
  43. }
  44. [TestMethod]
  45. public void TestCreateTextRoom()
  46. => TestCreateRoom(ChannelTypes.Text);
  47. [TestMethod]
  48. public void TestCreateVoiceRoom()
  49. => TestCreateRoom(ChannelTypes.Voice);
  50. private void TestCreateRoom(string type)
  51. {
  52. Channel channel = null;
  53. string name = $"test_{_random.Next()}";
  54. AssertEvent<DiscordClient.ChannelEventArgs>(
  55. "ChannelCreated event never received",
  56. () => channel = _bot1.CreateChannel(_testServer, name, type).Result,
  57. x => _bot2.ChannelCreated += x,
  58. x => _bot2.ChannelCreated -= x,
  59. (s, e) => e.Channel.Name == name);
  60. AssertEvent<DiscordClient.ChannelEventArgs>(
  61. "ChannelDestroyed event never received",
  62. () => _bot1.DestroyChannel(channel),
  63. x => _bot2.ChannelDestroyed += x,
  64. x => _bot2.ChannelDestroyed -= x,
  65. (s, e) => e.Channel.Name == name);
  66. }
  67. [TestCleanup]
  68. public void Cleanup()
  69. {
  70. if (_bot1.IsConnected)
  71. Task.WaitAll(_bot1.Servers.Select(x => _bot1.LeaveServer(x)).ToArray());
  72. if (_bot2.IsConnected)
  73. Task.WaitAll(_bot2.Servers.Select(x => _bot2.LeaveServer(x)).ToArray());
  74. _bot1.Disconnect().Wait();
  75. _bot2.Disconnect().Wait();
  76. }
  77. private void AssertEvent<TArgs>(string msg, Action action, Action<EventHandler<TArgs>> addEvent, Action<EventHandler<TArgs>> removeEvent, Func<object, TArgs, bool> test = null)
  78. {
  79. ManualResetEvent trigger = new ManualResetEvent(false);
  80. bool result = false;
  81. EventHandler<TArgs> handler = (s, e) =>
  82. {
  83. if (test != null)
  84. result |= test(s, e);
  85. else
  86. result = true;
  87. };
  88. addEvent(handler);
  89. action();
  90. trigger.WaitOne(EventTimeout);
  91. removeEvent(handler);
  92. Assert.AreEqual(true, result, msg);
  93. }
  94. }
  95. }

Introduction

No Description

No topics