-
Notifications
You must be signed in to change notification settings - Fork 19
/
notify_send_tests.py
1164 lines (841 loc) · 42.4 KB
/
notify_send_tests.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# -*- coding: utf-8 -*-
#
# Project: weechat-notify-send
# Homepage: https://github.com/s3rvac/weechat-notify-send
# Description: Unit tests for the project.
# License: MIT (see below)
#
# Copyright (c) 2015 by Petr Zemek <s3rvac@gmail.com> and contributors
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
#
import os
import sys
import unittest
try:
from unittest import mock # Python 3
except ImportError:
import mock # Python 2
# We need to mock the 'weechat' import because the tests do not run under
# WeeChat, so the import would fail.
weechat = mock.Mock()
sys.modules['weechat'] = weechat
from notify_send import Notification
from notify_send import add_default_value_to
from notify_send import default_value_of
from notify_send import escape_html
from notify_send import escape_slashes
from notify_send import ignore_notifications_from_buffer
from notify_send import ignore_notifications_from_messages_tagged_with
from notify_send import ignore_notifications_from_nick
from notify_send import is_below_min_notification_delay
from notify_send import message_printed_callback
from notify_send import names_for_buffer
from notify_send import nick_separator
from notify_send import nick_that_sent_message
from notify_send import notification_should_be_sent
from notify_send import notify_on_all_messages_in_buffer
from notify_send import notify_on_messages_that_match
from notify_send import prepare_notification
from notify_send import send_notification
from notify_send import shorten_message
def new_notification(source='source', message='message', icon='icon.png',
desktop_entry='weechat', timeout=5000, transient=True,
urgency='normal'):
return Notification(source, message, icon, desktop_entry, timeout, transient, urgency)
def set_config_option(option, value):
"""Sets the given configuration option to the given value."""
orig_config_get_plugin = weechat.config_get_plugin.side_effect
def config_get_plugin(opt):
if opt == option:
return value
if orig_config_get_plugin is not None:
return orig_config_get_plugin(opt)
# Assume that options are off by default.
return 'off'
weechat.config_get_plugin.side_effect = config_get_plugin
def set_buffer_string(buffer, string, value):
"""Sets the given buffer string to the given value."""
orig_buffer_get_string = weechat.buffer_get_string.side_effect
def buffer_get_string(b, s):
if b == buffer and s == string:
return value
if orig_buffer_get_string is not None:
return orig_buffer_get_string(b, s)
return ''
weechat.buffer_get_string.side_effect = buffer_get_string
class TestsBase(unittest.TestCase):
"""A base class for all tests."""
def setUp(self):
# Mock weechat again before running any tests. We need to do this
# because tests may change the global mock (return value, side effects,
# etc.) and thus affect other tests.
patcher = mock.patch('notify_send.weechat')
global weechat
weechat = patcher.start()
self.addCleanup(patcher.stop)
# Mock time.time().
patcher = mock.patch('notify_send.time.time')
self.time = patcher.start()
self.addCleanup(patcher.stop)
self.time.return_value = 0.0
# Default values for config options.
set_config_option('notify_on_highlights', 'on')
set_config_option('notify_on_privmsgs', 'on')
set_config_option('notify_on_filtered_messages', 'off')
set_config_option('notify_when_away', 'on')
set_config_option('notify_for_current_buffer', 'on')
set_config_option('notify_on_all_messages_in_current_buffer', 'on')
set_config_option('notify_on_all_messages_in_buffers', '')
set_config_option('notify_on_all_messages_in_buffers_that_match', '')
set_config_option('notify_on_messages_that_match', '')
set_config_option('min_notification_delay', '0')
set_config_option('ignore_messages_tagged_with', '')
set_config_option('ignore_buffers', '')
set_config_option('ignore_buffers_starting_with', '')
set_config_option('ignore_nicks', '')
set_config_option('ignore_nicks_starting_with', '')
set_config_option('hide_messages_in_buffers_that_match', '')
set_config_option('nick_separator', '')
set_config_option('escape_html', 'off')
set_config_option('max_length', '0')
set_config_option('ellipsis', '')
set_config_option('icon', '')
set_config_option('desktop_entry', '')
set_config_option('timeout', '0')
set_config_option('transient', 'on')
set_config_option('urgency', '')
# Mimic the behavior of weechat.buffer_get_string() by returning the
# empty string by default.
weechat.buffer_get_string.side_effect = lambda buffer, string: ''
class DefaultValueOfTests(TestsBase):
"""Tests for default_value_of()."""
def test_returns_correct_value(self):
self.assertEqual(default_value_of('nick_separator'), ': ')
class AddDefaultValueToTests(TestsBase):
"""Tests for add_default_value_to()."""
def test_adds_correct_default_value_when_default_value_is_nonempty(self):
description = add_default_value_to('Option description.', 'on')
self.assertEqual(description, 'Option description. Default: on.')
def test_adds_correct_default_value_when_default_value_is_empty(self):
description = add_default_value_to('Option description.', '')
self.assertEqual(description, 'Option description. Default: "".')
class NickThatSentMessageTests(TestsBase):
"""Tests for nick_that_sent_message()."""
def test_returns_prefix_when_there_are_no_tags_and_prefix_has_no_modes(self):
self.assertEqual(nick_that_sent_message([], 'john'), 'john')
def test_returns_prefix_without_mode_when_there_are_no_tags_and_prefix_has_mode(self):
self.assertEqual(nick_that_sent_message([], '~john'), 'john')
self.assertEqual(nick_that_sent_message([], '&john'), 'john')
self.assertEqual(nick_that_sent_message([], '@john'), 'john')
self.assertEqual(nick_that_sent_message([], '%john'), 'john')
self.assertEqual(nick_that_sent_message([], '+john'), 'john')
self.assertEqual(nick_that_sent_message([], '-john'), 'john')
def test_removes_also_space_before_nick_when_obtained_from_prefix(self):
self.assertEqual(nick_that_sent_message([], ' john'), 'john')
def test_only_single_character_is_removed_as_mode_from_prefix(self):
# Some protocols (e.g. Matrix) may start prefixes with a space.
# However, any subsequent characters should be considered to be part of
# the nick (e.g. from ' @john', we want '@john', including the '@').
self.assertEqual(nick_that_sent_message([], ' @john'), '@john')
def test_returns_nick_from_tags_when_tags_only_contains_nick(self):
self.assertEqual(nick_that_sent_message(['nick_john'], '--'), 'john')
def test_returns_nick_from_tags_when_tags_contain_also_other_info(self):
tags = ['prefix_nick_lightcyan', 'nick_john', 'host_~user@domain.com']
self.assertEqual(nick_that_sent_message(tags, '--'), 'john')
def test_returns_empty_string_when_both_tags_and_prefix_are_empty(self):
self.assertEqual(nick_that_sent_message([], ''), '')
class MessagePrintedCallbackTests(TestsBase):
"""Tests for message_printed_callback()."""
def setUp(self):
super(MessagePrintedCallbackTests, self).setUp()
# Mock notification_should_be_sent().
patcher = mock.patch('notify_send.notification_should_be_sent')
self.notification_should_be_sent = patcher.start()
self.addCleanup(patcher.stop)
# Mock send_notification().
patcher = mock.patch('notify_send.send_notification')
self.send_notification = patcher.start()
self.addCleanup(patcher.stop)
def message_printed_callback(self, data=None, buffer='buffer', date=None,
tags='', is_displayed='1', is_highlight='0',
prefix='prefix', message='message'):
return message_printed_callback(data, buffer, date, tags, is_displayed,
is_highlight, prefix, message)
def test_sends_notification_when_it_should_be_sent(self):
self.notification_should_be_sent.return_value = True
rc = self.message_printed_callback()
self.assertTrue(self.notification_should_be_sent.called)
self.assertTrue(self.send_notification.called)
self.assertEqual(rc, weechat.WEECHAT_RC_OK)
def test_does_not_send_notification_when_it_should_not_be_sent(self):
self.notification_should_be_sent.return_value = False
rc = self.message_printed_callback()
self.assertTrue(self.notification_should_be_sent.called)
self.assertFalse(self.send_notification.called)
self.assertEqual(rc, weechat.WEECHAT_RC_OK)
class NotificationShouldBeSentTests(TestsBase):
"""Tests for notification_should_be_sent()."""
def notification_should_be_sent(self, buffer='buffer', tags=(), nick='nick',
is_displayed=True, is_highlight=False, message=''):
return notification_should_be_sent(buffer, tags, nick,
is_displayed, is_highlight, message)
def test_returns_false_for_message_from_self(self):
BUFFER = 'buffer'
NICK = 'nick'
set_buffer_string(BUFFER, 'localvar_nick', NICK)
should_be_sent = self.notification_should_be_sent(
buffer=BUFFER,
nick=NICK
)
self.assertFalse(should_be_sent)
def test_returns_false_when_message_is_filtered_and_option_is_off(self):
set_config_option('notify_on_filtered_messages', 'off')
should_be_sent = self.notification_should_be_sent(
is_highlight=True,
is_displayed=False # WeeChat marks filtered messages as not displayed.
)
self.assertFalse(should_be_sent)
def test_returns_true_when_message_is_not_filtered_and_option_is_on(self):
set_config_option('notify_on_filtered_messages', 'on')
should_be_sent = self.notification_should_be_sent(
is_highlight=True,
is_displayed=False # WeeChat marks filtered messages as not displayed.
)
self.assertTrue(should_be_sent)
def test_returns_true_when_message_matches(self):
set_config_option('notify_on_messages_that_match', 'foo')
should_be_sent = self.notification_should_be_sent(message='foobar')
self.assertTrue(should_be_sent)
def test_returns_false_when_away_and_option_is_off(self):
BUFFER = 'buffer'
set_buffer_string(BUFFER, 'localvar_away', 'away')
set_config_option('notify_when_away', 'off')
should_be_sent = self.notification_should_be_sent(buffer=BUFFER)
self.assertFalse(should_be_sent)
def test_returns_false_when_highlight_in_current_buffer_and_option_is_off(self):
set_config_option('notify_for_current_buffer', 'off')
BUFFER = 'buffer'
weechat.current_buffer.return_value = BUFFER
should_be_sent = self.notification_should_be_sent(buffer=BUFFER)
self.assertFalse(should_be_sent)
def test_returns_true_when_in_curr_buf_and_notify_on_all_msgs_in_curr_buf_is_on(self):
set_config_option('notify_on_all_messages_in_current_buffer', 'on')
BUFFER = 'buffer'
weechat.current_buffer.return_value = BUFFER
should_be_sent = self.notification_should_be_sent(buffer=BUFFER)
self.assertTrue(should_be_sent)
def test_returns_false_when_in_curr_buf_and_notify_on_all_msgs_in_curr_buf_is_off(self):
set_config_option('notify_on_all_messages_in_current_buffer', 'off')
BUFFER = 'buffer'
weechat.current_buffer.return_value = BUFFER
should_be_sent = self.notification_should_be_sent(buffer=BUFFER)
self.assertFalse(should_be_sent)
def test_returns_false_when_ordinary_message_in_buffer_not_in_list(self):
BUFFER = 'buffer'
set_buffer_string(BUFFER, 'short_name', '#buffer')
set_config_option('notify_on_all_messages_in_buffers', '')
should_be_sent = self.notification_should_be_sent(buffer=BUFFER)
self.assertFalse(should_be_sent)
def test_returns_true_when_ordinary_message_in_buffer_in_list(self):
BUFFER = 'buffer'
set_buffer_string(BUFFER, 'short_name', '#buffer')
set_config_option('notify_on_all_messages_in_buffers', '#buffer')
should_be_sent = self.notification_should_be_sent(buffer=BUFFER)
self.assertTrue(should_be_sent)
def test_returns_false_when_neither_private_message_or_highlight(self):
BUFFER = 'buffer'
set_buffer_string(BUFFER, 'localvar_type', '')
should_be_sent = self.notification_should_be_sent(buffer=BUFFER)
self.assertFalse(should_be_sent)
def test_returns_false_when_nick_is_missing(self):
set_config_option('notify_on_highlights', 'on')
should_be_sent = self.notification_should_be_sent(nick='')
self.assertFalse(should_be_sent)
def test_returns_false_when_message_is_between_ignored_tags(self):
BUFFER = 'buffer'
set_buffer_string(BUFFER, 'localvar_type', 'private')
set_config_option('ignore_messages_tagged_with', 'tag1,tag2')
should_be_sent = self.notification_should_be_sent(
buffer=BUFFER,
tags=['tag2', 'tag3']
)
self.assertFalse(should_be_sent)
def test_returns_false_when_buffer_is_ignored(self):
BUFFER = 'buffer'
set_buffer_string(BUFFER, 'short_name', '#buffer')
set_config_option('ignore_buffers', '#buffer')
should_be_sent = self.notification_should_be_sent(buffer=BUFFER)
self.assertFalse(should_be_sent)
def test_returns_false_when_nick_is_ignored(self):
BUFFER = 'buffer'
set_buffer_string(BUFFER, 'localvar_type', 'private')
set_config_option('ignore_nicks', 'nick')
should_be_sent = self.notification_should_be_sent(
nick='nick'
)
self.assertFalse(should_be_sent)
def test_returns_false_when_is_below_min_notification_delay(self):
BUFFER = 'buffer'
set_buffer_string(
BUFFER,
'localvar_notify_send_last_notification_time',
'0.7'
)
set_config_option('min_notification_delay', 500)
self.time.return_value = 1.0
should_be_sent = self.notification_should_be_sent()
self.assertFalse(should_be_sent)
def test_returns_true_on_private_message_when_notify_on_privmsgs_is_on(self):
set_config_option('notify_on_privmsgs', 'on')
BUFFER = 'buffer'
set_buffer_string(BUFFER, 'localvar_type', 'private')
should_be_sent = self.notification_should_be_sent(buffer=BUFFER)
self.assertTrue(should_be_sent)
def test_returns_false_on_private_message_when_notify_on_privmsgs_is_off(self):
set_config_option('notify_on_privmsgs', 'off')
BUFFER = 'buffer'
set_buffer_string(BUFFER, 'localvar_type', 'private')
should_be_sent = self.notification_should_be_sent(buffer=BUFFER)
self.assertFalse(should_be_sent)
def test_returns_true_on_highlight_when_notify_on_highlights_is_on(self):
set_config_option('notify_on_highlights', 'on')
should_be_sent = self.notification_should_be_sent(
is_highlight=True
)
self.assertTrue(should_be_sent)
def test_returns_false_on_highlight_when_notify_on_highlights_is_off(self):
set_config_option('notify_on_highlights', 'off')
should_be_sent = self.notification_should_be_sent(
is_highlight=True
)
self.assertFalse(should_be_sent)
class IsBelowMinNotificationDelayTests(TestsBase):
"""Tests for is_below_min_notification_delay()."""
def test_returns_false_when_min_notification_delay_is_zero(self):
BUFFER = 'buffer'
set_buffer_string(
BUFFER,
'localvar_notify_send_last_notification_time',
'1.0'
)
self.time.return_value = 1.0
set_config_option('min_notification_delay', 0)
self.assertFalse(is_below_min_notification_delay(BUFFER))
def test_returns_false_when_last_time_is_not_below_min_notification_delay(self):
BUFFER = 'buffer'
set_buffer_string(
BUFFER,
'localvar_notify_send_last_notification_time',
'0.4'
)
self.time.return_value = 1.0
set_config_option('min_notification_delay', 500)
self.assertFalse(is_below_min_notification_delay(BUFFER))
def test_returns_true_when_last_time_is_below_min_notification_delay(self):
BUFFER = 'buffer'
set_buffer_string(
BUFFER,
'localvar_notify_send_last_notification_time',
'1.4'
)
self.time.return_value = 1.0
set_config_option('min_notification_delay', 500)
self.assertTrue(is_below_min_notification_delay(BUFFER))
def test_updates_last_notification_time(self):
CURRENT_TIME = 1.0
self.time.return_value = CURRENT_TIME
BUFFER = 'buffer'
is_below_min_notification_delay(BUFFER)
weechat.buffer_set.assert_called_once_with(
BUFFER,
'localvar_set_notify_send_last_notification_time',
str(CURRENT_TIME)
)
class NamesForBufferTests(TestsBase):
"""Tests for names_for_buffer()."""
def test_returns_correct_list_when_buffer_has_both_names(self):
BUFFER = 'buffer'
set_buffer_string(BUFFER, 'name', 'network.#buffer')
set_buffer_string(BUFFER, 'short_name', '#buffer')
self.assertEqual(
names_for_buffer(BUFFER),
['network.#buffer', '#buffer']
)
def test_includes_short_name_with_hash_when_short_name_starts_with_gt(self):
# This may happen with the wee_slack script, see the comment in
# names_for_buffer().
BUFFER = 'buffer'
set_buffer_string(BUFFER, 'name', 'network.#buffer')
set_buffer_string(BUFFER, 'short_name', '>buffer')
self.assertEqual(
names_for_buffer(BUFFER),
['network.#buffer', '>buffer', '#buffer']
)
def test_returns_empty_list_when_buffer_has_no_name(self):
BUFFER = 'buffer'
set_buffer_string(BUFFER, 'name', '')
set_buffer_string(BUFFER, 'short_name', '')
self.assertEqual(names_for_buffer(BUFFER), [])
class IgnoreNotificationsFromMessagesTaggedWith(TestsBase):
"""Tests for ignore_notifications_from_messages_tagged_with()."""
def test_returns_false_when_no_tags_are_ignored(self):
set_config_option('ignore_messages_tagged_with', '')
self.assertFalse(
ignore_notifications_from_messages_tagged_with(['tag1', 'tag2'])
)
def test_returns_false_when_no_tag_is_between_ignored(self):
set_config_option('ignore_messages_tagged_with', 'tagA,tagB')
self.assertFalse(
ignore_notifications_from_messages_tagged_with(['tag1', 'tag2'])
)
def test_returns_true_when_tag_is_between_ignored(self):
set_config_option('ignore_messages_tagged_with', 'tag3,tag4,tag5')
self.assertTrue(
ignore_notifications_from_messages_tagged_with(['tag0', 'tag2', 'tag3'])
)
class IgnoreNotificationsFromBufferTests(TestsBase):
"""Tests for ignore_notifications_from_buffer()."""
def test_returns_false_when_nothing_is_ignored(self):
BUFFER = 'buffer'
set_buffer_string(BUFFER, 'name', 'network.#buffer')
set_buffer_string(BUFFER, 'short_name', '#buffer')
set_config_option('ignore_buffers', '')
set_config_option('ignore_buffers_starting_with', '')
self.assertFalse(ignore_notifications_from_buffer(BUFFER))
def test_returns_false_when_buffer_has_no_name(self):
BUFFER = 'buffer'
set_buffer_string(BUFFER, 'name', '')
set_buffer_string(BUFFER, 'short_name', '')
set_config_option('ignore_buffers', '')
set_config_option('ignore_buffers_starting_with', '')
self.assertFalse(ignore_notifications_from_buffer(BUFFER))
def test_returns_false_when_buffer_is_not_between_ignored(self):
BUFFER = 'buffer'
set_buffer_string(BUFFER, 'name', 'network.#buffer')
set_buffer_string(BUFFER, 'short_name', '#buffer')
set_config_option('ignore_buffers', '#buffer1,#buffer2,#buffer3')
self.assertFalse(ignore_notifications_from_buffer(BUFFER))
def test_returns_true_when_buffer_is_ignored_by_short_name(self):
BUFFER = 'buffer'
set_buffer_string(BUFFER, 'name', 'network.#buffer')
set_buffer_string(BUFFER, 'short_name', '#buffer')
set_config_option('ignore_buffers', '#buffer')
self.assertTrue(ignore_notifications_from_buffer(BUFFER))
def test_returns_true_when_buffer_is_ignored_by_full_name(self):
BUFFER = 'buffer'
set_buffer_string(BUFFER, 'name', 'network.#buffer')
set_buffer_string(BUFFER, 'short_name', '#buffer')
set_config_option('ignore_buffers', 'network.#buffer')
self.assertTrue(ignore_notifications_from_buffer(BUFFER))
def test_returns_true_when_buffer_is_between_ignored_by_short_name(self):
BUFFER = 'buffer'
set_buffer_string(BUFFER, 'name', 'network.#buffer')
set_buffer_string(BUFFER, 'short_name', '#buffer')
set_config_option('ignore_buffers', '#aaa,#buffer,#bbb')
self.assertTrue(ignore_notifications_from_buffer(BUFFER))
def test_returns_true_when_buffer_is_between_ignored_by_full_name(self):
BUFFER = 'buffer'
set_buffer_string(BUFFER, 'name', 'network.#buffer')
set_buffer_string(BUFFER, 'short_name', '#buffer')
set_config_option('ignore_buffers', '#aaa,network.#buffer,#bbb')
self.assertTrue(ignore_notifications_from_buffer(BUFFER))
def test_strips_beginning_and_trailing_whitespace_from_ignored_buffers(self):
BUFFER = 'buffer'
set_buffer_string(BUFFER, 'name', 'network.#buffer')
set_buffer_string(BUFFER, 'short_name', '#buffer')
set_config_option('ignore_buffers', ' #buffer ')
self.assertTrue(ignore_notifications_from_buffer(BUFFER))
def test_returns_false_when_buffer_is_not_prefixed_with_ignored_prefix(self):
BUFFER = 'buffer'
set_buffer_string(BUFFER, 'name', 'network.#buffer')
set_buffer_string(BUFFER, 'short_name', '#buffer')
set_config_option('ignore_buffers_starting_with', 'some_prefix')
self.assertFalse(ignore_notifications_from_buffer(BUFFER))
def test_returns_true_when_buffer_full_name_is_prefixed_with_ignored_prefix(self):
BUFFER = 'buffer'
set_buffer_string(BUFFER, 'name', 'network.#buffer')
set_buffer_string(BUFFER, 'short_name', '#buffer')
set_config_option('ignore_buffers_starting_with', '#aaa,network.,#bbb')
self.assertTrue(ignore_notifications_from_buffer(BUFFER))
def test_returns_true_when_buffer_short_name_is_prefixed_with_ignored_prefix(self):
BUFFER = 'buffer'
set_buffer_string(BUFFER, 'name', 'network.#buffer')
set_buffer_string(BUFFER, 'short_name', '#buffer')
set_config_option('ignore_buffers_starting_with', '#aaa,#buf,#bbb')
self.assertTrue(ignore_notifications_from_buffer(BUFFER))
def test_strips_beginning_and_trailing_whitespace_from_ignored_prefixes(self):
BUFFER = 'buffer'
set_buffer_string(BUFFER, 'name', 'network.#buffer')
set_buffer_string(BUFFER, 'short_name', '#buffer')
set_config_option('ignore_buffers_starting_with', ' network. ')
self.assertTrue(ignore_notifications_from_buffer(BUFFER))
class IgnoreNotificationsFromNickTests(TestsBase):
"""Tests for ignore_notifications_from_nick()."""
def test_returns_false_when_nothing_is_ignored(self):
set_config_option('ignore_nicks', '')
set_config_option('ignore_nicks_starting_with', '')
self.assertFalse(ignore_notifications_from_nick('nick'))
def test_returns_false_when_nick_is_not_between_ignored(self):
set_config_option('ignore_nicks', 'nick1,nick2,nick3')
self.assertFalse(ignore_notifications_from_nick('wizard'))
def test_returns_true_when_nick_is_ignored(self):
set_config_option('ignore_nicks', 'nick')
self.assertTrue(ignore_notifications_from_nick('nick'))
def test_returns_true_when_nick_is_between_ignored(self):
set_config_option('ignore_nicks', 'nick1,nick2,nick3')
self.assertTrue(ignore_notifications_from_nick('nick2'))
def test_strips_beginning_and_trailing_whitespace_from_ignored_nicks(self):
set_config_option('ignore_nicks', ' nick ')
self.assertTrue(ignore_notifications_from_nick('nick'))
def test_returns_false_when_nick_is_not_prefixed_with_ignored_prefix(self):
set_config_option('ignore_nicks_starting_with', 'pre_')
self.assertFalse(ignore_notifications_from_nick('nick'))
def test_returns_true_when_nick_is_prefixed_with_ignored_prefix(self):
set_config_option('ignore_nicks_starting_with', 'pre_')
self.assertTrue(ignore_notifications_from_nick('pre_nick'))
def test_returns_true_when_nick_is_prefixed_with_prefix_between_ignored_prefixes(self):
set_config_option('ignore_nicks_starting_with', 'pre1_,pre2_,pre3_')
self.assertTrue(ignore_notifications_from_nick('pre2_nick'))
def test_strips_beginning_and_trailing_whitespace_from_ignored_prefixes(self):
set_config_option('ignore_nicks_starting_with', ' pre_ ')
self.assertTrue(ignore_notifications_from_nick('pre_nick'))
class NotifyOnMessagesThatMatchTests(TestsBase):
"""Tests for notify_on_messages_that_match()."""
def test_returns_false_when_list_has_no_patterns(self):
set_config_option('notify_on_messages_that_match', '')
self.assertFalse(notify_on_messages_that_match('foobar'))
def test_returns_false_when_no_pattern_matches(self):
set_config_option('notify_on_messages_that_match', 'xyz')
self.assertFalse(notify_on_messages_that_match('foobar'))
def test_returns_true_when_message_matches(self):
set_config_option('notify_on_messages_that_match', 'foo')
self.assertTrue(notify_on_messages_that_match('foobar'))
def test_re_search_is_used_for_matching(self):
set_config_option('notify_on_messages_that_match', 'bar')
self.assertTrue(notify_on_messages_that_match('foobar'))
class NotifyOnAllMessagesInBufferTests(TestsBase):
"""Tests for notify_on_all_messages_in_buffer()."""
def test_returns_false_when_options_do_not_contain_any_buffer(self):
BUFFER = 'buffer'
set_buffer_string(BUFFER, 'name', 'network.#buffer')
set_buffer_string(BUFFER, 'short_name', '#buffer')
set_config_option('notify_on_all_messages_in_buffers', '')
set_config_option('notify_on_all_messages_in_buffers_that_match', '')
self.assertFalse(notify_on_all_messages_in_buffer(BUFFER))
def test_returns_false_when_buffer_has_no_name(self):
BUFFER = 'buffer'
set_buffer_string(BUFFER, 'name', '')
set_buffer_string(BUFFER, 'short_name', '')
set_config_option('notify_on_all_messages_in_buffers', '')
set_config_option('notify_on_all_messages_in_buffers_that_match', '')
self.assertFalse(notify_on_all_messages_in_buffer(BUFFER))
def test_returns_false_when_buffer_is_not_in_buffer_list(self):
BUFFER = 'buffer'
set_buffer_string(BUFFER, 'name', 'network.#buffer')
set_buffer_string(BUFFER, 'short_name', '#buffer')
set_config_option('notify_on_all_messages_in_buffers', '#buffer1,#buffer2')
self.assertFalse(notify_on_all_messages_in_buffer(BUFFER))
def test_returns_false_when_buffer_does_not_match_any_pattern_in_buffer_pattern_list(self):
BUFFER = 'buffer'
set_buffer_string(BUFFER, 'name', 'network.#buffer')
set_buffer_string(BUFFER, 'short_name', '#buffer')
set_config_option('notify_on_all_messages_in_buffers_that_match', r'#a.*,#c.*')
self.assertFalse(notify_on_all_messages_in_buffer(BUFFER))
def test_returns_true_when_buffer_short_name_is_in_buffer_list(self):
BUFFER = 'buffer'
set_buffer_string(BUFFER, 'name', 'network.#buffer')
set_buffer_string(BUFFER, 'short_name', '#buffer')
set_config_option('notify_on_all_messages_in_buffers', '#aaa,#buffer,#bbb')
self.assertTrue(notify_on_all_messages_in_buffer(BUFFER))
def test_returns_true_when_buffer_short_name_matches_one_in_buffer_pattern_list(self):
BUFFER = 'buffer'
set_buffer_string(BUFFER, 'name', 'network.#buffer')
set_buffer_string(BUFFER, 'short_name', '#buffer')
set_config_option('notify_on_all_messages_in_buffers_that_match', r'#a.*,#buf.*')
self.assertTrue(notify_on_all_messages_in_buffer(BUFFER))
def test_returns_true_when_buffer_full_name_is_in_buffer_list(self):
BUFFER = 'buffer'
set_buffer_string(BUFFER, 'name', 'network.#buffer')
set_buffer_string(BUFFER, 'short_name', '#buffer')
set_config_option('notify_on_all_messages_in_buffers', '#aaa,network.#buffer,#bbb')
self.assertTrue(notify_on_all_messages_in_buffer(BUFFER))
def test_returns_true_when_buffer_full_name_matches_one_in_buffer_pattern_list(self):
BUFFER = 'buffer'
set_buffer_string(BUFFER, 'name', 'network.#buffer')
set_buffer_string(BUFFER, 'short_name', '#buffer')
set_config_option('notify_on_all_messages_in_buffers_that_match', r'#a.*,network\.#buf.*')
self.assertTrue(notify_on_all_messages_in_buffer(BUFFER))
def test_strips_beginning_and_trailing_whitespace_from_buffers_in_buffer_list(self):
BUFFER = 'buffer'
set_buffer_string(BUFFER, 'name', 'network.#buffer')
set_buffer_string(BUFFER, 'short_name', '#buffer')
set_config_option('notify_on_all_messages_in_buffers', ' #buffer ')
self.assertTrue(notify_on_all_messages_in_buffer(BUFFER))
def test_strips_beginning_and_trailing_whitespace_from_patterns_in_buffer_pattern_list(self):
BUFFER = 'buffer'
set_buffer_string(BUFFER, 'name', 'network.#buffer')
set_buffer_string(BUFFER, 'short_name', '#buffer')
set_config_option('notify_on_all_messages_in_buffers_that_match', ' #buf.* ')
self.assertTrue(notify_on_all_messages_in_buffer(BUFFER))
class EscapeHtmlTests(TestsBase):
"""Tests for escape_html()."""
def test_properly_escapes_needed_html_characters(self):
self.assertEqual(
escape_html('< > &'),
'< > &'
)
class EscapeSlashesTests(TestsBase):
"""Tests for escape_slashes()."""
def test_properly_escapes_slashes(self):
self.assertEqual(
escape_slashes(r'a\tb\nc'),
r'a\\tb\\nc'
)
class PrepareNotificationTests(TestsBase):
"""Tests for prepare_notification()."""
def prepare_notification(self, buffer='buffer', nick='nick', message='message',
is_private_message=True):
if is_private_message:
set_buffer_string(buffer, 'localvar_type', 'private')
return prepare_notification(buffer, nick, message)
def test_notification_has_correct_source_and_message_when_private_message(self):
NICK = 'nick'
MESSAGE = 'message'
notification = self.prepare_notification(
nick=NICK,
message=MESSAGE,
is_private_message=True
)
self.assertEqual(notification.source, NICK)
self.assertEqual(notification.message, MESSAGE)
def test_notification_has_correct_source_and_message_when_ordinary_message(self):
BUFFER = 'buffer'
set_buffer_string(BUFFER, 'short_name', '#buffer')
set_config_option('nick_separator', ': ')
notification = self.prepare_notification(
buffer=BUFFER,
nick='nick',
message='message',
is_private_message=False
)
self.assertEqual(notification.source, '#buffer')
self.assertEqual(notification.message, 'nick: message')
def test_notification_has_correct_icon(self):
ICON = '/path/to/icon.png'
set_config_option('icon', ICON)
notification = self.prepare_notification()
self.assertEqual(notification.icon, ICON)
def test_notification_has_correct_desktop_entry(self):
DESKTOP_ENTRY = 'weechat'
set_config_option('desktop_entry', DESKTOP_ENTRY)
notification = self.prepare_notification()
self.assertEqual(notification.desktop_entry, DESKTOP_ENTRY)
def test_notification_has_correct_timeout(self):
TIMEOUT = 1000
set_config_option('timeout', TIMEOUT)
notification = self.prepare_notification()
self.assertEqual(notification.timeout, TIMEOUT)
def test_notification_has_correct_transient_when_on(self):
set_config_option('transient', 'on')
notification = self.prepare_notification()
self.assertTrue(notification.transient)
def test_notification_has_correct_transient_when_off(self):
set_config_option('transient', 'off')
notification = self.prepare_notification()
self.assertFalse(notification.transient)
def test_notification_has_correct_urgency(self):
URGENCY = 'critical'
set_config_option('urgency', URGENCY)
notification = self.prepare_notification()
self.assertEqual(notification.urgency, URGENCY)
def test_shortens_message_when_max_length_is_non_zero_and_message_is_long(self):
set_config_option('max_length', 10)
set_config_option('ellipsis', '[..]')
notification = self.prepare_notification(message='123456789abcd')
self.assertEqual(notification.message, '123456[..]')
def test_does_not_shorten_message_when_max_length_zero(self):
set_config_option('max_length', 0)
MESSAGE = '123456789'
notification = self.prepare_notification(message=MESSAGE)
self.assertEqual(notification.message, MESSAGE)
def test_does_not_shorten_message_when_message_is_short_enough(self):
set_config_option('max_length', 10)
MESSAGE = 10 * 'a'
notification = self.prepare_notification(message=MESSAGE)
self.assertEqual(notification.message, MESSAGE)
def test_escapes_html_when_escape_html_is_on(self):
set_config_option('escape_html', 'on')
notification = self.prepare_notification(message='<>')
self.assertEqual(notification.message, '<>')
def test_does_not_escape_html_when_escape_html_is_off(self):
set_config_option('escape_html', 'off')
notification = self.prepare_notification(message='<>')
self.assertEqual(notification.message, '<>')
def test_hides_message_when_buffer_name_matches_regex_in_option(self):
BUFFER = 'defghijk'
set_buffer_string(BUFFER, 'short_name', '#' + BUFFER)
set_config_option('hide_messages_in_buffers_that_match', 'abc,def.*ijk,xyz')
notification = self.prepare_notification(BUFFER, message='hello')
self.assertEqual(notification.message, '')
def test_does_not_hide_message_when_buffer_name_does_not_match_regex_in_option(self):
BUFFER = 'foo'
set_buffer_string(BUFFER, 'short_name', '#' + BUFFER)
set_config_option('hide_messages_in_buffers_that_match', 'abc,def.*ijk,xyz')
notification = self.prepare_notification(BUFFER, message='hello')
self.assertEqual(notification.message, 'hello')
def test_does_not_hide_message_when_option_is_empty(self):
BUFFER = 'foo'
set_buffer_string(BUFFER, 'short_name', '#' + BUFFER)
set_config_option('hide_messages_in_buffers_that_match', '')
notification = self.prepare_notification(BUFFER, message='hello')
self.assertEqual(notification.message, 'hello')
class NickSeparatorTests(TestsBase):
"""Tests for nick_separator()."""
def test_returns_value_from_config_when_set(self):
NICK_SEPARATOR = ' --> '
set_config_option('nick_separator', NICK_SEPARATOR)
self.assertEqual(nick_separator(), NICK_SEPARATOR)
def test_returns_default_value_when_config_value_is_not_set(self):
set_config_option('nick_separator', '')