Skip to content

Commit

Permalink
Renamed project 'Tests' to 'Anarchy.Tests'
Browse files Browse the repository at this point in the history
  • Loading branch information
andreas-henning committed Jul 30, 2022
1 parent 5776b78 commit 61af228
Show file tree
Hide file tree
Showing 18 changed files with 132 additions and 388 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

# Excluded from versioning because it contains discord access credentials use by Anarchy's unit tests.
# Use appsettings.json as a template to create your own version of this file.
Tests/appsettings.Development.json
Anarchy.Tests/appsettings.Development.json

# User-specific files
*.rsuser
Expand Down
2 changes: 1 addition & 1 deletion Tests/Tests.csproj → Anarchy.Tests/Anarchy.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

<RootNamespace>Discord</RootNamespace>

<BaseOutputPath>..\.bin\Tests</BaseOutputPath>
<BaseOutputPath>..\.bin\$(MSBuildProjectName)</BaseOutputPath>
</PropertyGroup>

<ItemGroup>
Expand Down
6 changes: 6 additions & 0 deletions Anarchy.Tests/Directory.Build.props
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<Project>
<PropertyGroup>
<BaseIntermediateOutputPath>..\.obj\Anarchy.Tests</BaseIntermediateOutputPath>
<MSBuildProjectExtensionsPath>..\.obj\Anarchy.Tests</MSBuildProjectExtensionsPath>
</PropertyGroup>
</Project>
19 changes: 15 additions & 4 deletions Tests/Globals.cs → Anarchy.Tests/Globals.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace Discord
[TestClass]
public static class Globals
{
internal static AppSettings Settings { get; private set; } = GetAppSettings();
internal static Settings.App Settings { get; private set; } = GetAppSettings();

public static class FileNames
{
Expand All @@ -25,7 +25,18 @@ public static void AssemblyInit(TestContext context)
{
var autoResetEvent = new AutoResetEvent(false);

var client = new DiscordSocketClient();
// All tests will use the proxy specified in appsettings.json, which can be added like so:
// ,
// "Proxy": {
// "Host": "127.0.0.1",
// "Port": 8888
// }

var client = new DiscordSocketClient(new DiscordSocketConfig()
{
Proxy = Settings.Proxy?.CreateProxy()
});

client.OnLoggedIn += OnLoggedIn;
client.Login(Settings.Token);

Expand All @@ -40,13 +51,13 @@ void OnLoggedIn(DiscordSocketClient client, LoginEventArgs args)
Client = client;
}

private static AppSettings GetAppSettings()
private static Settings.App GetAppSettings()
{
var path = File.Exists(FileNames.SettingDevelopment)
? FileNames.SettingDevelopment
: FileNames.Setting;

return JsonSerializer.Deserialize<AppSettings>(File.ReadAllText(path))!;
return JsonSerializer.Deserialize<Settings.App>(File.ReadAllText(path))!;
}
}
}
54 changes: 54 additions & 0 deletions Anarchy.Tests/REST/Messsage/AttachmentTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
using System.Text;
using Microsoft.Maui.Graphics;

namespace Discord
{
[TestClass]
public class AttachmentTests
{
[TestMethod]
public void SendFile()
{
var client = Globals.Client;
var channelId = Globals.Settings.ChannelId;

var msg = client.SendFile(channelId, Globals.FileNames.PoetryTxt, $"{nameof(SendFile)} attachment test.");

Assert.AreEqual(1, msg.Attachments.Count);
}

[TestMethod]
public void SendMessageProperties()
{
var image1 = DiscordImageSource.FromStream(File.OpenRead(Globals.FileNames.Image1), ImageFormat.Png);
var image2 = DiscordImageSource.FromStream(File.OpenRead(Globals.FileNames.Image2), ImageFormat.Jpeg);
var poetryText = File.ReadAllText(Globals.FileNames.PoetryTxt);

var props = new MessageProperties
{
Content = $"{nameof(MessageProperties)} attachment test.",
Attachments = new List<PartialDiscordAttachment>()
{
// Attach an arbitrary file.
new PartialDiscordAttachment(Globals.FileNames.Image1),
// Attach an existing DiscordImage.
new PartialDiscordAttachment(image2, Globals.FileNames.Image2),
// Attach an arbitrary file with custom specifications for everything.
new PartialDiscordAttachment(
new DiscordAttachmentFile(Encoding.UTF8.GetBytes(poetryText), MediaTypeNames.Text.Plain),
Globals.FileNames.PoetryTxt,
"Text File")
}
};

var client = Globals.Client;
var channelId = Globals.Settings.ChannelId;

var msg = client.SendMessage(channelId, props);
Assert.AreEqual(props.Attachments.Count, msg.Attachments.Count);

var postedImage1 = DiscordImageSource.FromUrl(msg.Attachments[0].Url).Result;
CollectionAssert.AreEqual(postedImage1.PlatformImage.Bytes, image1.PlatformImage.Bytes);
}
}
}
18 changes: 18 additions & 0 deletions Anarchy.Tests/REST/Messsage/MessageTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
namespace Discord
{
[TestClass]
public class MessageTests
{
[TestMethod]
public void SendMessage()
{
const string content = "The simplest possible message.";

var msg = Globals.Client.SendMessage(
Globals.Settings.ChannelId,
content);

Assert.AreEqual(content, msg.Content);
}
}
}
File renamed without changes
File renamed without changes
File renamed without changes.
9 changes: 9 additions & 0 deletions Anarchy.Tests/Settings/App.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace Discord.Settings
{
internal class App
{
public string Token { get; set; } = string.Empty;
public ulong ChannelId { get; set; }
public ProxySettings? Proxy { get; set; }
}
}
15 changes: 15 additions & 0 deletions Anarchy.Tests/Settings/Proxy.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System.Net;

namespace Discord.Settings
{
internal class ProxySettings
{
public string Host { get; set; } = String.Empty;
public int Port { get; set; }

public IWebProxy CreateProxy()
{
return new WebProxy(Host, Port);
}
}
}
File renamed without changes.
4 changes: 4 additions & 0 deletions Anarchy.Tests/appsettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"Token": "<string: A discord user token>",
"ChannelId": "<ulong: The discord channel ID the unit tests will post to. The user identified by 'Token' must have access to this channel."
}
Loading

0 comments on commit 61af228

Please sign in to comment.