Browse Source

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
pull/42/head
Christopher F 9 years ago
parent
commit
bf41b30a9a
3 changed files with 101 additions and 36 deletions
  1. +0
    -1
      test/Discord.Net.Tests/Discord.Net.Tests.csproj
  2. +0
    -32
      test/Discord.Net.Tests/Settings.cs
  3. +101
    -3
      test/Discord.Net.Tests/Tests.cs

+ 0
- 1
test/Discord.Net.Tests/Discord.Net.Tests.csproj View File

@@ -56,7 +56,6 @@
</Choose>
<ItemGroup>
<Compile Include="Tests.cs" />
<Compile Include="Settings.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>


+ 0
- 32
test/Discord.Net.Tests/Settings.cs View File

@@ -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<Settings>(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; }
}
}

+ 101
- 3
test/Discord.Net.Tests/Tests.cs View File

@@ -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<ChannelUserEventArgs>(
"UserUpdated event never fired.",
async () => await channel.SendIsTyping(),
x => _targetBot.UserIsTyping += x,
x => _targetBot.UserIsTyping -= x);
}
[TestMethod]
public void TestEditChannel()
{
var channel = _testServerChannel;
AssertEvent<ChannelUpdatedEventArgs>(
"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<MessageEventArgs>(
"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<TArgs>(string msg, Func<Task> action, Action<EventHandler<TArgs>> addEvent, Action<EventHandler<TArgs>> removeEvent, Func<object, TArgs, bool> 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
}
}

Loading…
Cancel
Save