refactor: don't use a class for GroupAddRemoveResponse; fix tests

This commit is contained in:
spiral 2021-08-27 19:18:59 -04:00
parent ac2671452d
commit 2ddef25177
No known key found for this signature in database
GPG Key ID: A6059F0CA0E1BD31
2 changed files with 36 additions and 67 deletions

View File

@ -7,103 +7,72 @@ namespace PluralKit.Bot
public static class GroupAddRemoveResponseService public static class GroupAddRemoveResponseService
{ {
public static string GenerateResponse(Groups.AddRemoveOperation action, int memberCount, int groupCount, int actionedOn, int notActionedOn) public static string GenerateResponse(Groups.AddRemoveOperation action, int memberCount, int groupCount, int actionedOn, int notActionedOn)
=> new Response(action, memberCount, groupCount, actionedOn, notActionedOn).ToString();
private class Response
{ {
private readonly Groups.AddRemoveOperation _op; var op = action;
private readonly string _actionStr; var actionStr = action == Groups.AddRemoveOperation.Add ? "added to" : "removed from";
private readonly string _containStr; var containStr = action == Groups.AddRemoveOperation.Add ? "in" : "not in";
private readonly string _emojiStr; var emojiStr = actionedOn > 0 ? Emojis.Success : Emojis.Error;
private readonly bool _memberPlural; var memberPlural = memberCount > 1;
private readonly bool _groupPlural; var groupPlural = groupCount > 1;
private readonly int _actionedOn;
private readonly int _notActionedOn;
public Response(Groups.AddRemoveOperation action, int memberCount, int groupCount, int actionedOn,
int notActionedOn)
{
_op = action;
_actionStr = action == Groups.AddRemoveOperation.Add ? "added to" : "removed from";
_containStr = action == Groups.AddRemoveOperation.Add ? "in" : "not in";
_emojiStr = actionedOn > 0 ? Emojis.Success : Emojis.Error;
_memberPlural = memberCount > 1;
_groupPlural = groupCount > 1;
// sanity checking: we can't add multiple groups to multiple members (at least for now) // sanity checking: we can't add multiple groups to multiple members (at least for now)
if (_memberPlural && _groupPlural) if (memberPlural && groupPlural)
throw new ArgumentOutOfRangeException(); throw new ArgumentOutOfRangeException();
// sanity checking: we can't act/not act on a different number of entities than we have // sanity checking: we can't act/not act on a different number of entities than we have
if (_memberPlural && (actionedOn + notActionedOn) != memberCount) if (memberPlural && (actionedOn + notActionedOn) != memberCount)
throw new ArgumentOutOfRangeException(); throw new ArgumentOutOfRangeException();
if (_groupPlural && (actionedOn + notActionedOn) != groupCount) if (groupPlural && (actionedOn + notActionedOn) != groupCount)
throw new ArgumentOutOfRangeException(); throw new ArgumentOutOfRangeException();
_actionedOn = actionedOn;
_notActionedOn = notActionedOn;
}
// name generators // name generators
private string MemberString(bool capitalize = false) string MemberString(int count, bool capitalize = false)
=> capitalize
? (_memberPlural ? "Members" : "Member")
: (_memberPlural ? "members" : "member");
private string MemberString(int count, bool capitalize = false)
=> capitalize => capitalize
? (count == 1 ? "Member" : "Members") ? (count == 1 ? "Member" : "Members")
: (count == 1 ? "member" : "members"); : (count == 1 ? "member" : "members");
private string GroupString() => _groupPlural ? "groups" : "group"; string GroupString(int count)
private string GroupString(int count)
=> count == 1 ? "group" : "groups"; => count == 1 ? "group" : "groups";
// string generators // string generators
private string ResponseString() string ResponseString()
{ {
if (_actionedOn > 0 && _notActionedOn > 0 && _memberPlural) if (actionedOn > 0 && notActionedOn > 0 && memberPlural)
return $"{_actionedOn} {MemberString(_actionedOn)} {_actionStr} {GroupString()}"; return $"{actionedOn} {MemberString(actionedOn)} {actionStr} {GroupString(groupCount)}";
if (_actionedOn > 0 && _notActionedOn > 0 && _groupPlural) if (actionedOn > 0 && notActionedOn > 0 && groupPlural)
return $"{MemberString(capitalize: true)} {_actionStr} {_actionedOn} {GroupString(_actionedOn)}"; return $"{MemberString(memberCount, capitalize: true)} {actionStr} {actionedOn} {GroupString(actionedOn)}";
if (_notActionedOn == 0) if (notActionedOn == 0)
return $"{MemberString(capitalize: true)} {_actionStr} {GroupString()}"; return $"{MemberString(memberCount, capitalize: true)} {actionStr} {GroupString(groupCount)}";
if (_actionedOn == 0) if (actionedOn == 0)
return $"{MemberString(capitalize: true)} not {_actionStr} {GroupString()}"; return $"{MemberString(memberCount, capitalize: true)} not {actionStr} {GroupString(groupCount)}";
throw new ArgumentOutOfRangeException(); throw new ArgumentOutOfRangeException();
} }
private string InfoMessage() string InfoMessage()
{ {
if (_notActionedOn == 0) return $""; if (notActionedOn == 0) return $"";
var msg = ""; var msg = "";
if (_actionedOn > 0 && _memberPlural) if (actionedOn > 0 && memberPlural)
msg += $"{_notActionedOn} {MemberString(_notActionedOn)}"; msg += $"{notActionedOn} {MemberString(notActionedOn)}";
else else
msg += $"{MemberString()}"; msg += $"{MemberString(memberCount)}";
msg += $" already {_containStr}"; msg += $" already {containStr}";
if (_actionedOn > 0 && _groupPlural) if (actionedOn > 0 && groupPlural)
msg += $" {_notActionedOn} {GroupString(_notActionedOn)}"; msg += $" {notActionedOn} {GroupString(notActionedOn)}";
else else
msg += $" {GroupString()}"; msg += $" {GroupString(groupCount)}";
return $" ({msg})"; return $" ({msg})";
} }
public string ToString() => $"{_emojiStr} {ResponseString()}{InfoMessage()}."; return $"{emojiStr} {ResponseString()}{InfoMessage()}.";
// |
} }
} }
} }

View File

@ -158,7 +158,7 @@ namespace PluralKit.Tests
func(removeOp, 2, 1, 2, 0) func(removeOp, 2, 1, 2, 0)
); );
[Fact]
public void PartialSuccess1() public void PartialSuccess1()
=> Assert.Equal( => Assert.Equal(
$"{success} 1 member removed from group (2 members already not in group).", $"{success} 1 member removed from group (2 members already not in group).",