Fixing unit tests for Channel command

This commit is contained in:
Griatch 2021-05-05 22:14:28 +02:00
parent 2da679cdd1
commit bbfb77022e
4 changed files with 64 additions and 69 deletions

View file

@ -68,7 +68,6 @@ class AccountCmdSet(CmdSet):
self.add(comms.CmdChannelCreate())
self.add(comms.CmdClock())
self.add(comms.CmdCBoot())
self.add(comms.CmdCemit())
self.add(comms.CmdCWho())
self.add(comms.CmdCdesc())
self.add(comms.CmdPage())

View file

@ -181,7 +181,7 @@ class CmdChannel(COMMAND_DEFAULT_CLASS):
"log_file", default=channel.log_to_file.format(channel_key=channel.key))
def send_msg(lines):
return caller.msg(
return self.msg(
"".join(line.split("[-]", 1)[1] if "[-]" in line else line for line in lines)
)
# asynchronously tail the log file
@ -699,6 +699,8 @@ class CmdChannel(COMMAND_DEFAULT_CLASS):
switches = self.switches
channel_names = [name for name in self.lhslist if name]
#from evennia import set_trace;set_trace()
if not channel_names:
if 'all' in switches:
# show all available channels
@ -719,7 +721,7 @@ class CmdChannel(COMMAND_DEFAULT_CLASS):
return
if not self.switches and not self.args:
caller.msg("Usage[/switches]: channel [= message]")
self.msg("Usage[/switches]: channel [= message]")
return
if 'create' in switches:
@ -801,8 +803,10 @@ class CmdChannel(COMMAND_DEFAULT_CLASS):
return
if 'sub' in switches:
# subscribe to a channel aliases = set(alias.strip().lower() for
# alias in self.rhs.split(";"))
# subscribe to a channel
aliases = []
if self.rhs:
aliases = set(alias.strip().lower() for alias in self.rhs.split(";"))
success, err = self.sub_to_channel(channel)
if success:
for alias in aliases:
@ -864,7 +868,7 @@ class CmdChannel(COMMAND_DEFAULT_CLASS):
def _perform_delete(caller, *args, **kwargs):
self.destroy_channel(channel, message=reason)
caller.msg(f"Channel {channel.key} was successfully deleted.")
self.msg(f"Channel {channel.key} was successfully deleted.")
ask_yes_no(
caller,
@ -904,9 +908,9 @@ class CmdChannel(COMMAND_DEFAULT_CLASS):
success, err = self.set_lock(channel, self.rhs)
if success:
caller.msg("Added/updated lock on channel.")
self.msg("Added/updated lock on channel.")
else:
caller.msg(f"Could not add/update lock: {err}")
self.msg(f"Could not add/update lock: {err}")
return
if 'unlock' in switches:
@ -923,16 +927,16 @@ class CmdChannel(COMMAND_DEFAULT_CLASS):
success, err = self.unset_lock(channel, self.rhs)
if success:
caller.msg("Removed lock from channel.")
self.msg("Removed lock from channel.")
else:
caller.msg(f"Could not remove lock: {err}")
self.msg(f"Could not remove lock: {err}")
return
if 'boot' in switches:
# boot a user from channel(s)
if not self.rhs:
caller.msg("Usage: channel/boot channel[,channel,...] = username [:reason]")
self.msg("Usage: channel/boot channel[,channel,...] = username [:reason]")
return
target_str, *reason = self.rhs.rsplit(":", 1)
@ -947,16 +951,16 @@ class CmdChannel(COMMAND_DEFAULT_CLASS):
# the target must be a member of all given channels
target = caller.search(target_str, candidates=chan.subscriptions.all())
if not target:
caller.msg(f"Cannot boot '{target_str}' - not in channel {chan.key}.")
self.msg(f"Cannot boot '{target_str}' - not in channel {chan.key}.")
return
def _boot_user(caller, *args, **kwargs):
for chan in channels:
success, err = self.boot_user(chan, target, quiet=False, reason=reason)
if success:
caller.msg(f"Booted {target.key} from channel {chan.key}.")
self.msg(f"Booted {target.key} from channel {chan.key}.")
else:
caller.msg(f"Cannot boot {target.key} from channel {chan.key}: {err}")
self.msg(f"Cannot boot {target.key} from channel {chan.key}: {err}")
channames = ", ".join(chan.key for chan in channels)
reasonwarn = (". Also note that your reason will be echoed to the channel"
@ -994,13 +998,13 @@ class CmdChannel(COMMAND_DEFAULT_CLASS):
for chan in channels:
# the target must be a member of all given channels
if not chan.access(caller, "control"):
caller.msg(f"You don't have access to ban users on channel {chan.key}")
self.msg(f"You don't have access to ban users on channel {chan.key}")
return
target = caller.search(target_str, candidates=chan.subscriptions.all())
if not target:
caller.msg(f"Cannot ban '{target_str}' - not in channel {chan.key}.")
self.msg(f"Cannot ban '{target_str}' - not in channel {chan.key}.")
return
def _ban_user(caller, *args, **kwargs):
@ -1036,7 +1040,7 @@ class CmdChannel(COMMAND_DEFAULT_CLASS):
for chan in channels:
# the target must be a member of all given channels
if not chan.access(caller, "control"):
caller.msg(f"You don't have access to unban users on channel {chan.key}")
self.msg(f"You don't have access to unban users on channel {chan.key}")
return
banlists.extend(chan.banlist)
@ -1058,10 +1062,15 @@ class CmdChannel(COMMAND_DEFAULT_CLASS):
who_list = [f"Subscribed to {channel.key}:"]
who_list.extend(self.channel_list_who(channel))
caller.msg("\n".join(who_list))
self.msg("\n".join(who_list))
return
# a channel-command parent for use with Characters/Objects.
class CmdObjectChannel(CmdChannel):
account_caller = False
class CmdAddCom(CmdChannel):
"""
Add a channel alias and/or subscribe to a channel
@ -1117,15 +1126,14 @@ class CmdAddCom(CmdChannel):
return
if channel.unmute(caller):
string += "You unmute channel %s." % channel.key
self.msg(f"You unmute channel {channel.key}.")
else:
string += "You are already connected to channel %s." % channel.key
self.msg(f"You are already connected to channel {channel.key}.")
if alias:
# create a nick and add it to the caller.
self.add_alias(channel, alias)
self.msg(f" You can now refer to the channel {channel} with the alias '{alias}'.")
self.msg(string % (channel.key, alias))
else:
string += " No alias added."
self.msg(string)
@ -1219,8 +1227,11 @@ class CmdAllCom(CmdChannel):
caller = self.caller
args = self.args
if not args:
self.execute_cmd("channels")
self.msg("(Usage: allcom on | off | who | destroy)")
subscribed, available = self.list_channels()
table = self.display_all_channels(subscribed, available)
self.msg(
"\n|wAvailable channels:\n{table}")
return
return
if args == "on":

View file

@ -35,7 +35,6 @@ from evennia.commands.default import (
unloggedin,
syscommands,
)
from evennia.commands.cmdparser import build_matches
from evennia.commands.default.muxcommand import MuxCommand
from evennia.commands.command import Command, InterruptCommand
from evennia.commands import cmdparser
@ -1770,21 +1769,13 @@ class TestComms(CommandTest):
self.call(
comms.CmdAddCom(),
"tc = testchan",
"You are already connected to channel testchan. You can now",
"You are already connected to channel testchan.| You can now",
receiver=self.account,
)
self.call(
comms.CmdDelCom(),
"tc",
"Your alias 'tc' for channel testchan was cleared.",
receiver=self.account,
)
def test_channels(self):
self.call(
comms.CmdChannels(),
"",
"Available channels (use comlist,addcom and delcom to manage",
"Any alias 'tc' for channel testchan was cleared.",
receiver=self.account,
)
@ -1792,7 +1783,7 @@ class TestComms(CommandTest):
self.call(
comms.CmdAllCom(),
"",
"Available channels (use comlist,addcom and delcom to manage",
"Available channels:",
receiver=self.account,
)
@ -1812,14 +1803,6 @@ class TestComms(CommandTest):
receiver=self.account,
)
def test_cemit(self):
self.call(
comms.CmdCemit(),
"testchan = Test Message",
"[testchan] Test Message|Sent to channel testchan: Test Message",
receiver=self.account,
)
def test_cwho(self):
self.call(
comms.CmdCWho(),
@ -1869,6 +1852,8 @@ class TestCommsChannel(CommandTest):
key="testchannel",
desc="A test channel")
self.channel.connect(self.char1)
self.cmdchannel = comms.CmdChannel
self.cmdchannel.account_caller = False
def tearDown(self):
if self.channel.pk:
@ -1877,7 +1862,7 @@ class TestCommsChannel(CommandTest):
# test channel command
def test_channel__noarg(self):
self.call(
comms.CmdChannel(),
self.cmdchannel(),
"",
"Channel subscriptions"
)
@ -1885,7 +1870,7 @@ class TestCommsChannel(CommandTest):
def test_channel__msg(self):
self.channel.msg = Mock()
self.call(
comms.CmdChannel(),
self.cmdchannel(),
"testchannel = Test message",
""
)
@ -1893,14 +1878,14 @@ class TestCommsChannel(CommandTest):
def test_channel__list(self):
self.call(
comms.CmdChannel(),
self.cmdchannel(),
"/list",
"Channel subscriptions"
)
def test_channel__all(self):
self.call(
comms.CmdChannel(),
self.cmdchannel(),
"/all",
"Available channels"
)
@ -1908,7 +1893,7 @@ class TestCommsChannel(CommandTest):
def test_channel__history(self):
with patch("evennia.commands.default.comms.tail_log_file") as mock_tail:
self.call(
comms.CmdChannel(),
self.cmdchannel(),
"/history testchannel",
""
)
@ -1918,18 +1903,16 @@ class TestCommsChannel(CommandTest):
self.channel.disconnect(self.char1)
self.call(
comms.CmdChannel(),
self.cmdchannel(),
"/sub testchannel",
"You are now subscribed"
)
self.assertTrue(self.char1 in self.channel.subscriptions.all())
alias_msg = comms.CmdChannel.channel_msg_nick_alias.format(alias='testchannel')
self.assertEqual(self.char1.nicks.get(alias_msg, category="channel"),
"channel testchannel = $1")
self.assertEqual(self.char1.nicks.nickreplace("testchannel Hello"), "channel testchannel = Hello")
def test_channel__unsub(self):
self.call(
comms.CmdChannel(),
self.cmdchannel(),
"/unsub testchannel",
"You un-subscribed"
)
@ -1939,24 +1922,24 @@ class TestCommsChannel(CommandTest):
"""Add and then remove a channel alias"""
# add alias
self.call(
comms.CmdChannel(),
self.cmdchannel(),
"/alias testchannel = foo",
"Added/updated your alias 'foo' for channel testchannel."
)
self.assertEqual(
self.char1.nicks.get('foo $1', category="channel"), "channel testchannel = $1")
self.char1.nicks.nickreplace('foo Hello'), "channel testchannel = Hello")
# use alias
self.channel.msg = Mock()
self.call(
comms.CmdChannel(),
self.cmdchannel(),
"foo = test message",
"")
self.channel.msg.assert_called_with("test message", senders=self.char1)
# remove alias
self.call(
comms.CmdChannel(),
self.cmdchannel(),
"/unalias testchannel = foo",
"Removed your channel alias 'foo'"
)
@ -1964,7 +1947,7 @@ class TestCommsChannel(CommandTest):
def test_channel__mute(self):
self.call(
comms.CmdChannel(),
self.cmdchannel(),
"/mute testchannel",
"Muted channel testchannel"
)
@ -1974,7 +1957,7 @@ class TestCommsChannel(CommandTest):
self.channel.mute(self.char1)
self.call(
comms.CmdChannel(),
self.cmdchannel(),
"/unmute testchannel = Char1",
"Un-muted channel testchannel"
)
@ -1982,7 +1965,7 @@ class TestCommsChannel(CommandTest):
def test_channel__create(self):
self.call(
comms.CmdChannel(),
self.cmdchannel(),
"/create testchannel2",
"Created (and joined) new channel"
)
@ -1990,7 +1973,7 @@ class TestCommsChannel(CommandTest):
def test_channel__destroy(self):
self.channel.msg = Mock()
self.call(
comms.CmdChannel(),
self.cmdchannel(),
"/destroy testchannel = delete reason",
"Are you sure you want to delete channel ",
inputs=['Yes']
@ -2000,14 +1983,14 @@ class TestCommsChannel(CommandTest):
def test_channel__desc(self):
self.call(
comms.CmdChannel(),
self.cmdchannel(),
"/desc testchannel = Another description",
"Updated channel description."
)
def test_channel__lock(self):
self.call(
comms.CmdChannel(),
self.cmdchannel(),
"/lock testchannel = foo:false()",
"Added/updated lock on channel"
)
@ -2016,7 +1999,7 @@ class TestCommsChannel(CommandTest):
def test_channel__unlock(self):
self.channel.locks.add("foo:true()")
self.call(
comms.CmdChannel(),
self.cmdchannel(),
"/unlock testchannel = foo",
"Removed lock from channel"
)
@ -2029,7 +2012,7 @@ class TestCommsChannel(CommandTest):
self.char2.msg = Mock()
self.call(
comms.CmdChannel(),
self.cmdchannel(),
"/boot testchannel = Char2 : Booting from channel!",
"Are you sure ",
inputs=["Yes"]
@ -2049,7 +2032,7 @@ class TestCommsChannel(CommandTest):
self.char2.msg = Mock()
self.call(
comms.CmdChannel(),
self.cmdchannel(),
"/ban testchannel = Char2 : Banning from channel!",
"Are you sure ",
inputs=["Yes"]
@ -2063,7 +2046,7 @@ class TestCommsChannel(CommandTest):
# unban
self.call(
comms.CmdChannel(),
self.cmdchannel(),
"/unban testchannel = Char2",
"Un-banned Char2 from channel testchannel"
)
@ -2071,7 +2054,7 @@ class TestCommsChannel(CommandTest):
def test_channel__who(self):
self.call(
comms.CmdChannel(),
self.cmdchannel(),
"/who testchannel",
"Subscribed to testchannel:\nChar"
)

View file

@ -1853,10 +1853,12 @@ def format_grid(elements, width=78, sep=" ", verbatim_elements=None):
decorations in the grid, such as horizontal bars.
Returns:
gridstr: The grid as a list of ready-formatted rows. We return it
list: The grid as a list of ready-formatted rows. We return it
like this to make it easier to insert decorations between rows, such
as horizontal bars.
"""
if not elements:
return []
if not verbatim_elements:
verbatim_elements = []