From 4c63a2103635fb7ab8b19dbcbbc8d9df167542d5 Mon Sep 17 00:00:00 2001 From: Trygve Aaberge Date: Tue, 26 Sep 2017 22:19:18 +0200 Subject: refactor: Split topic_command_cb and add tests Split out the parts of topic_command_cb that's most interesting and easiest to test into parse_topic_command and add tests for that function. --- _pytest/test_topic_command.py | 44 +++++++++++++++++++++++++++++++++++++++++++ wee_slack.py | 41 ++++++++++++++++++++++++---------------- 2 files changed, 69 insertions(+), 16 deletions(-) create mode 100644 _pytest/test_topic_command.py diff --git a/_pytest/test_topic_command.py b/_pytest/test_topic_command.py new file mode 100644 index 0000000..2a4f069 --- /dev/null +++ b/_pytest/test_topic_command.py @@ -0,0 +1,44 @@ +from wee_slack import parse_topic_command + + +def test_parse_topic_without_arguments(): + channel_name, topic = parse_topic_command('/topic') + + assert channel_name is None + assert topic is None + + +def test_parse_topic_with_text(): + channel_name, topic = parse_topic_command('/topic some topic text') + + assert channel_name is None + assert topic == 'some topic text' + + +def test_parse_topic_with_delete(): + channel_name, topic = parse_topic_command('/topic -delete') + + assert channel_name is None + assert topic == '' + + +def test_parse_topic_with_channel(): + channel_name, topic = parse_topic_command('/topic #general') + + assert channel_name == 'general' + assert topic is None + + +def test_parse_topic_with_channel_and_text(): + channel_name, topic = parse_topic_command( + '/topic #general some topic text') + + assert channel_name == 'general' + assert topic == 'some topic text' + + +def test_parse_topic_with_channel_and_delete(): + channel_name, topic = parse_topic_command('/topic #general -delete') + + assert channel_name == 'general' + assert topic == '' diff --git a/wee_slack.py b/wee_slack.py index 37db0ec..46ad278 100644 --- a/wee_slack.py +++ b/wee_slack.py @@ -2825,6 +2825,28 @@ def part_command_cb(data, current_buffer, args): return w.WEECHAT_RC_OK_EAT +def parse_topic_command(command): + args = command.split()[1:] + channel_name = None + topic = None + + if args: + if args[0].startswith('#'): + channel_name = args[0][1:] + topic = args[1:] + else: + topic = args + + if topic == []: + topic = None + if topic: + topic = ' '.join(topic) + if topic == '-delete': + topic = '' + + return channel_name, topic + + @slack_buffer_or_ignore def topic_command_cb(data, current_buffer, command): """ @@ -2833,18 +2855,10 @@ def topic_command_cb(data, current_buffer, command): """ data = decode_from_utf8(data) command = decode_from_utf8(command) - team = EVENTROUTER.weechat_controller.buffers[current_buffer].team - args = command.split()[1:] - channel_name = None - topic = [] - if args: - if args[0].startswith('#'): - channel_name = args[0][1:] - topic = args[1:] - else: - topic = args + channel_name, topic = parse_topic_command(command) + team = EVENTROUTER.weechat_controller.buffers[current_buffer].team if channel_name: channel = team.channels.get(team.get_channel_map().get(channel_name)) else: @@ -2854,12 +2868,7 @@ def topic_command_cb(data, current_buffer, command): w.prnt(team.channel_buffer, "#{}: No such channel".format(channel_name)) return w.WEECHAT_RC_OK_EAT - if topic: - topic = ' '.join(topic) - if topic == '-delete': - topic = '' - - if topic == []: + if topic is None: w.prnt(channel.channel_buffer, 'Topic for {} is "{}"'.format(channel.name, channel.slack_topic['value'])) else: s = SlackRequest(team.token, "channels.setTopic", {"channel": channel.identifier, "topic": topic}, team_hash=team.team_hash) -- cgit