Added some tests, fixed related bugs
This commit is contained in:
@@ -1,47 +0,0 @@
|
||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Discord.Net.Tests
|
||||
{
|
||||
[TestClass]
|
||||
public class ChannelTests
|
||||
{
|
||||
private DiscordClient _bot1, _bot2;
|
||||
|
||||
[TestInitialize]
|
||||
public void Initialize()
|
||||
{
|
||||
_bot1 = new DiscordClient();
|
||||
_bot2 = new DiscordClient();
|
||||
|
||||
_bot1.Connect(Settings.Test1_Username, Settings.Test1_Password).Wait();
|
||||
_bot2.Connect(Settings.Test2_Username, Settings.Test2_Password).Wait();
|
||||
|
||||
//Cleanup existing servers
|
||||
Task.WaitAll(_bot1.Servers.Select(x => _bot1.LeaveServer(x)).ToArray());
|
||||
Task.WaitAll(_bot2.Servers.Select(x => _bot2.LeaveServer(x)).ToArray());
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public async Task DoNothing()
|
||||
{
|
||||
Server server = await _bot1.CreateServer("Discord.Net Testbed", Region.US_East);
|
||||
Invite invite = await _bot1.CreateInvite(server, 60, 1, false, false);
|
||||
await _bot2.AcceptInvite(invite);
|
||||
await _bot2.LeaveServer(server);
|
||||
}
|
||||
|
||||
[TestCleanup]
|
||||
public void Cleanup()
|
||||
{
|
||||
if (_bot1.IsConnected)
|
||||
Task.WaitAll(_bot1.Servers.Select(x => _bot1.LeaveServer(x)).ToArray());
|
||||
if (_bot2.IsConnected)
|
||||
Task.WaitAll(_bot2.Servers.Select(x => _bot2.LeaveServer(x)).ToArray());
|
||||
|
||||
_bot1.Disconnect().Wait();
|
||||
_bot2.Disconnect().Wait();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -50,7 +50,7 @@
|
||||
</Otherwise>
|
||||
</Choose>
|
||||
<ItemGroup>
|
||||
<Compile Include="ChannelTests.cs" />
|
||||
<Compile Include="Tests.cs" />
|
||||
<Compile Include="Credentials.cs" />
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
</ItemGroup>
|
||||
|
||||
103
Discord.Net.Tests/Tests.cs
Normal file
103
Discord.Net.Tests/Tests.cs
Normal file
@@ -0,0 +1,103 @@
|
||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Discord.Net.Tests
|
||||
{
|
||||
[TestClass]
|
||||
public class Tests
|
||||
{
|
||||
private DiscordClient _bot1, _bot2;
|
||||
private Server _testServer;
|
||||
private Channel _testServerChannel;
|
||||
private Random _random;
|
||||
|
||||
[TestInitialize]
|
||||
public void Initialize()
|
||||
{
|
||||
_random = new Random();
|
||||
|
||||
_bot1 = new DiscordClient();
|
||||
_bot2 = new DiscordClient();
|
||||
|
||||
_bot1.Connect(Settings.Test1_Username, Settings.Test1_Password).Wait();
|
||||
_bot2.Connect(Settings.Test2_Username, Settings.Test2_Password).Wait();
|
||||
|
||||
//Cleanup existing servers
|
||||
Task.WaitAll(_bot1.Servers.Select(x => _bot1.LeaveServer(x)).ToArray());
|
||||
Task.WaitAll(_bot2.Servers.Select(x => _bot2.LeaveServer(x)).ToArray());
|
||||
|
||||
_testServer = _bot1.CreateServer("Discord.Net Testbed", Region.US_East).Result;
|
||||
_testServerChannel = _testServer.DefaultChannel;
|
||||
Invite invite = _bot1.CreateInvite(_testServer, 60, 1, false, false).Result;
|
||||
_bot2.AcceptInvite(invite).Wait();
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void TestSendMessage()
|
||||
{
|
||||
string text = $"test_{_random.Next()}";
|
||||
AssertEvent<DiscordClient.MessageEventArgs>(
|
||||
"MessageCreated event never received",
|
||||
() => _bot1.SendMessage(_testServerChannel, text),
|
||||
x => _bot2.MessageCreated += x,
|
||||
x => _bot2.MessageCreated -= x,
|
||||
(s, e) => e.Message.Text == text);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void TestCreateRoom()
|
||||
{
|
||||
Channel channel;
|
||||
string name = $"test_{_random.Next()}";
|
||||
AssertEvent<DiscordClient.ChannelEventArgs>(
|
||||
"ChannelCreated event never received",
|
||||
() => channel = _bot1.CreateChannel(_testServerChannel, name),
|
||||
x => _bot2.ChannelCreated += x,
|
||||
x => _bot2.ChannelCreated -= x,
|
||||
(s, e) => e.Channel.Name == name);
|
||||
|
||||
AssertEvent<DiscordClient.ChannelEventArgs>(
|
||||
"ChannelDestroyed event never received",
|
||||
() => _bot1.DestroyChannel(channel),
|
||||
x => _bot2.ChannelDestroyed += x,
|
||||
x => _bot2.ChannelDestroyed -= x,
|
||||
(s, e) => e.Channel.Name == name);
|
||||
}
|
||||
|
||||
[TestCleanup]
|
||||
public void Cleanup()
|
||||
{
|
||||
if (_bot1.IsConnected)
|
||||
Task.WaitAll(_bot1.Servers.Select(x => _bot1.LeaveServer(x)).ToArray());
|
||||
if (_bot2.IsConnected)
|
||||
Task.WaitAll(_bot2.Servers.Select(x => _bot2.LeaveServer(x)).ToArray());
|
||||
|
||||
_bot1.Disconnect().Wait();
|
||||
_bot2.Disconnect().Wait();
|
||||
}
|
||||
|
||||
private void AssertEvent<TArgs>(string msg, Action action, Action<EventHandler<TArgs>> addEvent, Action<EventHandler<TArgs>> removeEvent, Func<object, TArgs, bool> test = null)
|
||||
{
|
||||
ManualResetEvent trigger = new ManualResetEvent(false);
|
||||
bool result = false;
|
||||
|
||||
EventHandler<TArgs> handler = (s, e) =>
|
||||
{
|
||||
if (test != null)
|
||||
result = test(s, e);
|
||||
else
|
||||
result = true;
|
||||
};
|
||||
|
||||
addEvent(handler);
|
||||
action();
|
||||
trigger.WaitOne(5000);
|
||||
removeEvent(handler);
|
||||
|
||||
Assert.AreEqual(true, result, msg);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -4,6 +4,7 @@ using Discord.Helpers;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
using System.Reflection;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
@@ -51,7 +52,12 @@ namespace Discord
|
||||
(server, model) =>
|
||||
{
|
||||
server.Name = model.Name;
|
||||
if (model is ExtendedServerInfo)
|
||||
if (!server.Channels.Any()) //Assume a default channel exists with the same id as the server. Not sure if this is safe?
|
||||
{
|
||||
var defaultChannel = new ChannelReference() { Id = server.DefaultChannelId, GuildId = server.Id };
|
||||
_channels.Update(defaultChannel.Id, defaultChannel.GuildId, defaultChannel);
|
||||
}
|
||||
if (model is ExtendedServerInfo)
|
||||
{
|
||||
var extendedModel = model as ExtendedServerInfo;
|
||||
server.AFKChannelId = extendedModel.AFKChannelId;
|
||||
@@ -422,7 +428,12 @@ namespace Discord
|
||||
public async Task<Server> LeaveServer(string id)
|
||||
{
|
||||
CheckReady();
|
||||
await DiscordAPI.LeaveServer(id, _httpOptions);
|
||||
try
|
||||
{
|
||||
await DiscordAPI.LeaveServer(id, _httpOptions);
|
||||
}
|
||||
//Happens if the room was destroyed as we try to leave it
|
||||
catch (WebException ex) when ((ex.Response as HttpWebResponse)?.StatusCode == HttpStatusCode.NotFound) {}
|
||||
return _servers.Remove(id);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user