Skip to content

Commit

Permalink
Tests for the specific network formatters
Browse files Browse the repository at this point in the history
  • Loading branch information
instantiator committed Oct 22, 2023
1 parent ec89116 commit b6695bd
Show file tree
Hide file tree
Showing 5 changed files with 113 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,9 +1,24 @@
using DistributorLib.Network;
using DistributorLib.Post.Formatters;

namespace DistributorLib.Post.Formatters
{
public class PostFormatVariantFactory
{
public static IPostFormatter For(NetworkType network)
{
return network switch
{
NetworkType.Console => Console,
NetworkType.Mastodon => Mastodon,
NetworkType.Facebook => Facebook,
NetworkType.LinkedIn => LinkedIn,
NetworkType.Discord => Discord,
NetworkType.Twitter => throw new NotImplementedException(network.ToString()),
_ => throw new NotImplementedException(network.ToString())
};
}

public static IPostFormatter Console => new ConsolePostFormatter();

public static IPostFormatter Mastodon => new LengthLimitedPostFormatter(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace DistributorLib.Tests.Helpers;

public static class MessageTestHelpers
{
public static string RemoveHashTags(this string message)
=> string.Join(' ', message.Split(' ').Where(w => !w.StartsWith('#')));
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
using System.Reflection;
using DistributorLib.Input;
using DistributorLib.Network;
using DistributorLib.Post.Formatters;
using DistributorLib.Tests.Helpers;

namespace DistributorLib.Tests;

public class NetworkPostFormatterTests
{
[Fact]
public void ProjectPlacesTestFilesInTheRightPlace()
{
var runPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
Assert.False(string.IsNullOrWhiteSpace(runPath));
var testFilePath = Path.Combine(runPath, "TestData", "release-announcement-v0.1.jsonc");
Assert.False(string.IsNullOrWhiteSpace(testFilePath));
Assert.True(File.Exists(testFilePath), $"Test file not found at {testFilePath}");
}

[Theory]
[InlineData("Hello world! #hello #lovely #world", "Hello world!")]
public void RemoveHashTags_RemovesHashTags(string input, string expected)
{
var result = input.RemoveHashTags();
Assert.Equal(expected, result);
}

[Theory]
[InlineData(NetworkType.LinkedIn, 1, new[] {
@"🎉 Announcing release 0.1 of Open Social Distributor - a command line tool that can create posts and threads across a variety of social networks.
🆓 It's open source and free - and you're welcome to contribute to it if you fancy getting involved.
🧵 This post (and thread if you're looking at it on Mastodon!) is an early test for the Open Social Distributor... 🤞 https://instantiator.dev/open-social-distributor/" })]
[InlineData(NetworkType.Mastodon, 3, new[] {
"🎉 Announcing release 0.1 of Open Social Distributor - a command line tool that can create posts and threads across a variety of social networks. https://instantiator.dev/open-social-distributor/ /1",
"🆓 It's open source and free - and you're welcome to contribute to it if you fancy getting involved. /2",
"🧵 This post (and thread if you're looking at it on Mastodon!) is an early test for the Open Social Distributor... 🤞 /3" })]
[InlineData(NetworkType.Discord, 3, new[] {
"🎉 Announcing release 0.1 of Open Social Distributor - a command line tool that can create posts and threads across a variety of social networks. https://instantiator.dev/open-social-distributor/",
"🆓 It's open source and free - and you're welcome to contribute to it if you fancy getting involved.",
"🧵 This post (and thread if you're looking at it on Mastodon!) is an early test for the Open Social Distributor... 🤞" })]
public void LinkedInPostFormatter_CorrectlyFormatsAnnouncement(NetworkType network, int expectedMessages, IEnumerable<string> expectedText)
{
var runPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
var json = File.ReadAllText(Path.Combine(runPath!, "TestData", "release-announcement-v0.1.jsonc"));
var reader = new PostListReader();
var list = reader.ReadJson(json);
var message = list!.ToSocialMessages().Single();

var formatter = PostFormatVariantFactory.For(network);
var result = formatter.FormatText(message);

Assert.Equal(expectedMessages, result.Count());

var expectedMinusHashTags = expectedText.Select(m => m.RemoveHashTags().Trim());
var resultMinusHashTags = result.Select(m => m.RemoveHashTags().Trim());

Assert.Equal(expectedMinusHashTags, resultMinusHashTags);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public async Task ResolvesRelativePathFileUrls()
[Fact]
public async Task ResolvesHttpsUrls()
{
await TestSocialImageUri("https://github.com/instantiator/consensus-chess-engine/blob/main/docs/images/cog-icon.png?raw=true", false, "cog-icon.png");
await TestSocialImageUri("https://instantiator.dev/hammer/hammer.png", false, "hammer.png");
}

private async Task TestSocialImageUri(string testUri, bool isFilePath, string testFilename)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"posts": [
{
"parts": [
{ "part": "Text", "content": { "any": "🎉 Announcing release 0.1 of Open Social Distributor - a command line tool that can create posts and threads across a variety of social networks. $$ 🆓 It's open source and free - and you're welcome to contribute to it if you fancy getting involved. $$ 🧵 This post (and thread if you're looking at it on Mastodon!) is an early test for the Open Social Distributor... 🤞"} },
{ "part": "Link", "content": { "any": "https://instantiator.dev/open-social-distributor/" } },
{ "part": "Tag", "content": { "any": "SocialNetworks" } },
{ "part": "Tag", "content": { "any": "SocialMedia" } },
{ "part": "Tag", "content": { "any": "OpenSocialDistributor" } },
{ "part": "Tag", "content": { "any": "OSD" } },
{ "part": "Tag", "content": { "any": "Tool" } },
{ "part": "Tag", "content": { "any": "CLI" } },
{ "part": "Tag", "content": { "any": "OpenSource" } },
{ "part": "Tag", "content": { "any": "OSS" } }
], // list of parts
"images": [
{
"uri": "file://sample-messages/social-distributor-icon.png",
"description": "The icon for Open Social Distributor - clear arrows in the 4 ordinal directions overlay a cloud of colours."
},
{
"uri": "file://sample-messages/2023-10-08-capabilities.png",
"description": "A table showing the supported social networks: Mastodon, Discord, Facebook and LinkedIn all appear. All support threads, links, tags, and images - although Facebook and LinkedIn have not yet had their threading capability fully tested."
}
] // list of images
} // individual message
] // list of posts
}

0 comments on commit b6695bd

Please sign in to comment.