-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: verify that events are raised correctly
- Loading branch information
Showing
4 changed files
with
67 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,79 @@ | ||
using System.Text; | ||
using FluentAssertions.Events; | ||
using Microsoft.Extensions.Logging; | ||
using Microsoft.Extensions.Options; | ||
using NSubstitute; | ||
using Streamlabs.SocketClient.Events; | ||
using Streamlabs.SocketClient.InternalExtensions; | ||
|
||
namespace Streamlabs.SocketClient.Tests; | ||
|
||
public class MessageTypeTests | ||
{ | ||
public sealed record JsonFile(string FileName, Type ExpectedType, string? EventName = null) | ||
{ | ||
public string GetJson() => File.ReadAllText(Path.Combine("./MessageJson", FileName), Encoding.UTF8); | ||
}; | ||
|
||
public static IReadOnlyCollection<JsonFile> All { get; } = | ||
new List<JsonFile> | ||
{ | ||
new("donation.json", typeof(DonationEvent)), | ||
new("bits.json", typeof(BitsEvent)), | ||
new("donationDelete.json", typeof(DonationDeleteEvent)), | ||
new("alertPlaying_subscription.json", typeof(AlertPlayingEvent), "SubscriptionAlertPlaying"), | ||
new("alertPlaying_bits.json", typeof(AlertPlayingEvent), "BitsAlertPlaying"), | ||
new("follow.json", typeof(FollowEvent)), | ||
new("raid.json", typeof(RaidEvent)), | ||
new("rollEndCredits.json", typeof(RollEndCreditsEvent)), | ||
new("streamlabels.json", typeof(StreamlabelsEvent)), | ||
new("streamlabelsUnderlying.json", typeof(StreamlabelsUnderlyingEvent)), | ||
}; | ||
|
||
public static TheoryData<JsonFile> GetTheoryData() | ||
{ | ||
var data = new TheoryData<JsonFile>(); | ||
foreach (var jsonFile in All) | ||
{ | ||
data.Add(jsonFile); | ||
} | ||
|
||
return data; | ||
} | ||
|
||
[Theory] | ||
[InlineData("donation.json", typeof(DonationEvent))] | ||
[InlineData("bits.json", typeof(BitsEvent))] | ||
[InlineData("donationDelete.json", typeof(DonationDeleteEvent))] | ||
[InlineData("alertPlaying_subscription.json", typeof(AlertPlayingEvent))] | ||
[InlineData("alertPlaying_bits.json", typeof(AlertPlayingEvent))] | ||
[InlineData("follow.json", typeof(FollowEvent))] | ||
[InlineData("raid.json", typeof(RaidEvent))] | ||
[InlineData("rollEndCredits.json", typeof(RollEndCreditsEvent))] | ||
[InlineData("streamlabels.json", typeof(StreamlabelsEvent))] | ||
[InlineData("streamlabelsUnderlying.json", typeof(StreamlabelsUnderlyingEvent))] | ||
public void MessageTypes_CanBeDeserialized(string fileName, Type expectedType) | ||
[MemberData(nameof(GetTheoryData))] | ||
public void MessageTypes_CanBeDeserialized(JsonFile file) | ||
{ | ||
// Arrange | ||
string json = File.ReadAllText(Path.Combine("./MessageJson", fileName), Encoding.UTF8); | ||
string json = file.GetJson(); | ||
|
||
// Act | ||
var messages = json.Deserialize(); | ||
|
||
// Assert | ||
messages.Should().HaveCount(1); | ||
messages.Should().AllBeOfType(expectedType); | ||
messages.Should().AllBeOfType(file.ExpectedType); | ||
} | ||
|
||
[Theory] | ||
[MemberData(nameof(GetTheoryData))] | ||
public void Events_AreRaised_WhenMessageIsDispatched(JsonFile file) | ||
{ | ||
// Arrange | ||
var logger = Substitute.For<ILogger<StreamlabsClient>>(); | ||
var options = new StreamlabsOptions(); | ||
var client = new StreamlabsClient(logger, new OptionsWrapper<StreamlabsOptions>(options)); | ||
using IMonitor<StreamlabsClient> sut = client.Monitor(); | ||
string json = file.GetJson(); | ||
|
||
// Act | ||
client.Dispatch(json); | ||
|
||
// Assert | ||
sut.Should().Raise(nameof(client.OnEventRaw)); | ||
sut.Should().Raise(nameof(client.OnEvent)); | ||
string eventName = file.EventName ?? file.ExpectedType.Name.Replace("Event", string.Empty); | ||
sut.Should().Raise($"On{eventName}"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters