Skip to content

Commit

Permalink
Add some MSum() tests for irc
Browse files Browse the repository at this point in the history
  • Loading branch information
occluder committed Jan 5, 2024
1 parent a634b88 commit a899ecd
Show file tree
Hide file tree
Showing 3 changed files with 131 additions and 2 deletions.
129 changes: 129 additions & 0 deletions MiniTwitch.Irc.Test/SpanSumTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
using System.Text;
using MiniTwitch.Irc.Internal.Enums;
using Xunit;
using Xunit.Sdk;

namespace MiniTwitch.Irc.Test;
public class SpanSumTests
{
// <expected, string>
static readonly Dictionary<int, string> _values = new()
{
{ (int)Tags.Id, "id" },
{ (int)Tags.Mod, "mod" },
{ (int)Tags.Vip, "vip" },
{ (int)Tags.R9K, "r9k" },
{ (int)Tags.Bits, "bits" },
{ (int)Tags.Flags, "flags" },
{ (int)Tags.Color, "color" },
{ (int)Tags.Slow, "slow" },
{ (int)Tags.Turbo, "turbo" },
{ (int)Tags.Badges, "badges" },
{ (int)Tags.Emotes, "emotes" },
{ (int)Tags.UserId, "user-id" },
{ (int)Tags.FirstMsg, "first-msg" },
{ (int)Tags.MsgId, "msg-id" },
{ (int)Tags.Login, "login" },
{ (int)Tags.RoomId, "room-id" },
{ (int)Tags.SubsOnly, "subs-only" },
{ (int)Tags.EmoteOnly, "emote-only" },
{ (int)Tags.TmiSentTs, "tmi-sent-ts" },
{ (int)Tags.BanDuration, "ban-duration" },
{ (int)Tags.TargetMsgId, "target-msg-id" },
{ (int)Tags.TargetUserId, "target-user-id" },
{ (int)Tags.FollowersOnly, "followers-only" },
{ (int)Tags.UserType, "user-type" },
{ (int)Tags.BadgeInfo, "badge-info" },
{ (int)Tags.Subscriber, "subscriber" },
{ (int)Tags.ClientNonce, "client-nonce" },
{ (int)Tags.DisplayName, "display-name" },
{ (int)Tags.ReturningChatter, "returning-chatter" },
{ (int)Tags.ReplyParentMsgId, "reply-parent-msg-id" },
{ (int)Tags.ReplyParentUserId, "reply-parent-user-id" },
{ (int)Tags.ReplyParentMsgBody, "reply-parent-msg-body" },
{ (int)Tags.ReplyParentUserLogin, "reply-parent-user-login" },
{ (int)Tags.ReplyParentDisplayName, "reply-parent-display-name" },
{ (int)Tags.ReplyThreadParentMsgId, "reply-thread-parent-msg-id" },
{ (int)Tags.ReplyThreadParentUserLogin, "reply-thread-parent-user-login" },
{ (int)Tags.SystemMsg, "system-msg" },
{ (int)Tags.MsgParamColor, "msg-param-color" },
{ (int)Tags.MsgParamMonths, "msg-param-months" },
{ (int)Tags.MsgParamSubPlan, "msg-param-sub-plan" },
{ (int)Tags.MsgParamSenderName, "msg-param-sender-name" },
{ (int)Tags.MsgParamGiftMonths, "msg-param-gift-months" },
{ (int)Tags.MsgParamViewerCount, "msg-param-viewerCount" },
{ (int)Tags.MsgParamRecipientId, "msg-param-recipient-id" },
{ (int)Tags.MsgParamSenderLogin, "msg-param-sender-login" },
{ (int)Tags.MsgParamSenderCount, "msg-param-sender-count" },
{ (int)Tags.MsgParamSubPlanName, "msg-param-sub-plan-name" },
{ (int)Tags.MsgParamStreakMonths, "msg-param-streak-months" },
{ (int)Tags.MsgParamMassGiftCount, "msg-param-mass-gift-count" },
{ (int)Tags.MsgParamCumulativeMonths, "msg-param-cumulative-months" },
{ (int)Tags.MsgParamRecipientUserName, "msg-param-recipient-user-name" },
{ (int)Tags.MsgParamShouldShareStreak, "msg-param-should-share-streak" },
{ (int)Tags.MsgParamRecipientDisplayName, "msg-param-recipient-display-name" },
{ (int)Tags.MsgParamCharityName, "msg-param-charity-name" },
{ (int)Tags.MsgParamDonationAmount, "msg-param-donation-amount" },
{ (int)Tags.MsgParamExponent, "msg-param-exponent" },
{ (int)Tags.MsgParamDonationCurrency, "msg-param-donation-currency" },
{ (int)Tags.EmoteSets, "emote-sets" },
{ (int)Tags.MessageId, "message-id" },
{ (int)Tags.ThreadId, "thread-id" },
};

[Fact]
public void Test_Includes_All_Tags()
{
var tags = Enum.GetNames(typeof(Tags));
if (tags.Length != _values.Count)
{
var notIncluded = tags.Where(x => !_values.ContainsKey((int)Enum.Parse<Tags>(x)));
Assert.Fail($"Test does not account for all tags, expected {tags.Length}, got {_values.Count}." +
$"\nMissing tags: \n{string.Join("\n", notIncluded)}");
}

foreach (var tag in tags)
{
var enumValue = Enum.Parse<Tags>(tag);
try
{
Assert.True(_values.ContainsKey((int)enumValue));
}
catch (TrueException)
{
Assert.Fail($"Enum value {enumValue} not present.");
}
}
}

[Fact]
public void All_Tag_Values_Match_Sum()
{
foreach (var kvp in _values)
{
if (kvp.Key != MSum(kvp.Value))
{
Assert.Fail($"Tag \"{kvp.Value}\" does not match expected sum {kvp.Key}. Got {MSum(kvp.Value)} instead.");
}
}
}


private static int MSum(string s)
{
var source = Encoding.UTF8.GetBytes(s);
if (source.Length == 0)
{
return 0;
}

int m = source[0];
int sum = 0;
foreach (byte b in source)
{
sum += b;
}

return m * sum;
}
}
2 changes: 1 addition & 1 deletion MiniTwitch.Irc/Internal/Enums/Tags.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ internal enum Tags
MsgParamCumulativeMonths = 298987,
MsgParamRecipientUserName = 312067,
MsgParamShouldShareStreak = 313048,
MsgparamRecipientDisplayName = 345966,
MsgParamRecipientDisplayName = 345966,
MsgParamCharityName = 235876,
MsgParamDonationAmount = 273699,
MsgParamExponent = 199143,
Expand Down
2 changes: 1 addition & 1 deletion MiniTwitch.Irc/Models/Usernotice.cs
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ internal Usernotice(ref IrcMessage message)
break;

//msg-param-recipient-display-name
case (int)Tags.MsgparamRecipientDisplayName:
case (int)Tags.MsgParamRecipientDisplayName:
recipientDisplayName = TagHelper.GetString(tagValue);
break;

Expand Down

0 comments on commit a899ecd

Please sign in to comment.