unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
blob 0b90867b32d67afedd3ca24e2b17c452db032c1b 33749 bytes (raw)
name: test/lisp/erc/erc-match-tests.el 	 # note: path name is non-authoritative(*)

  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
 
;;; erc-match-tests.el --- Tests for erc-match.  -*- lexical-binding:t -*-

;; Copyright (C) 2022-2024 Free Software Foundation, Inc.

;; This file is part of GNU Emacs.

;; GNU Emacs is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.

;; GNU Emacs is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs.  If not, see <https://www.gnu.org/licenses/>.

;;; Commentary:
;;; Code:

(require 'ert-x)
(require 'erc-match)
(eval-and-compile
  (let ((load-path (cons (ert-resource-directory) load-path)))
    (require 'erc-tests-common)))


(ert-deftest erc-add-entry-to-list ()
  (let ((erc-pals '("z"))
        (erc-match-quote-when-adding 'ask))

    (ert-info ("Default (ask)")
      (ert-simulate-keys "\t\ry\r"
        (erc-add-entry-to-list 'erc-pals "?" '((".")) nil)
        (should (equal (pop erc-pals) "\\.")))

      (ert-info ("Inverted")
        (ert-simulate-keys "\t\ry\r"
          (erc-add-entry-to-list 'erc-pals "?" '((".")) nil)
          (should (equal (pop erc-pals) "\\."))))

      (ert-info ("Skipped")
        (ert-simulate-keys "\t\r"
          (erc-add-entry-to-list 'erc-pals "?" '(("x")) nil)
          (should (equal (pop erc-pals) "x")))))

    (ert-info ("Verbatim")
      (setq erc-match-quote-when-adding nil)
      (ert-simulate-keys "\t\r"
        (erc-add-entry-to-list 'erc-pals "?" '((".")) nil)
        (should (equal (pop erc-pals) ".")))

      (ert-info ("Inverted")
        (ert-simulate-keys "\t\r"
          (erc-add-entry-to-list 'erc-pals "?" '((".")) t)
          (should (equal (pop erc-pals) "\\.")))))

    (ert-info ("Quoted")
      (setq erc-match-quote-when-adding t)
      (ert-simulate-keys "\t\r"
        (erc-add-entry-to-list 'erc-pals "?" '((".")) nil)
        (should (equal (pop erc-pals) "\\.")))

      (ert-info ("Inverted")
        (ert-simulate-keys "\t\r"
          (erc-add-entry-to-list 'erc-pals "?" '((".")) t)
          (should (equal (pop erc-pals) ".")))))

    (should (equal erc-pals '("z")))))

(ert-deftest erc-pals ()
  (with-temp-buffer
    (setq erc-server-process (start-process "true" (current-buffer) "true")
          erc-server-users (make-hash-table :test #'equal))
    (set-process-query-on-exit-flag erc-server-process nil)
    (erc-add-server-user "FOO[m]" (make-erc-server-user :nickname "foo[m]"))
    (erc-add-server-user "tester" (make-erc-server-user :nickname "tester"))

    (let ((erc-match-quote-when-adding t)
          erc-pals calls rvs)
      (cl-letf (((symbol-function 'completing-read)
                 (lambda (&rest r) (push r calls) (pop rvs))))

        (ert-info ("`erc-add-pal'")
          (push "foo[m]" rvs)
          (ert-simulate-command '(erc-add-pal))
          (should (equal (cadr (pop calls)) '(("tester") ("foo[m]"))))
          (should (equal erc-pals '("foo\\[m]"))))

        (ert-info ("`erc-match-pal-p'")
          (should (erc-match-pal-p "FOO[m]!~u@example.net" nil)))

        (ert-info ("`erc-delete-pal'")
          (push "foo\\[m]" rvs)
          (ert-simulate-command '(erc-delete-pal))
          (should (equal (cadr (pop calls)) '(("foo\\[m]"))))
          (should-not erc-pals))

        (ert-info ("`erc-add-pal' verbatim")
          (push "foo[m]" rvs)
          (ert-simulate-command '(erc-add-pal (4)))
          (should (equal (cadr (pop calls)) '(("tester") ("foo[m]"))))
          (should (equal erc-pals '("foo[m]"))))))))

(ert-deftest erc-fools ()
  (with-temp-buffer
    (setq erc-server-process (start-process "true" (current-buffer) "true")
          erc-server-users (make-hash-table :test #'equal))
    (set-process-query-on-exit-flag erc-server-process nil)
    (erc-add-server-user "FOO[m]" (make-erc-server-user :nickname "foo[m]"))
    (erc-add-server-user "tester" (make-erc-server-user :nickname "tester"))

    (let ((erc-match-quote-when-adding t)
          erc-fools calls rvs)
      (cl-letf (((symbol-function 'completing-read)
                 (lambda (&rest r) (push r calls) (pop rvs))))

        (ert-info ("`erc-add-fool'")
          (push "foo[m]" rvs)
          (ert-simulate-command '(erc-add-fool))
          (should (equal (cadr (pop calls)) '(("tester") ("foo[m]"))))
          (should (equal erc-fools '("foo\\[m]"))))

        (ert-info ("`erc-match-fool-p'")
          (should (erc-match-fool-p "FOO[m]!~u@example.net" ""))
          (should (erc-match-fool-p "tester!~u@example.net" "FOO[m]: die")))

        (ert-info ("`erc-delete-fool'")
          (push "foo\\[m]" rvs)
          (ert-simulate-command '(erc-delete-fool))
          (should (equal (cadr (pop calls)) '(("foo\\[m]"))))
          (should-not erc-fools))

        (ert-info ("`erc-add-fool' verbatim")
          (push "foo[m]" rvs)
          (ert-simulate-command '(erc-add-fool (4)))
          (should (equal (cadr (pop calls)) '(("tester") ("foo[m]"))))
          (should (equal erc-fools '("foo[m]"))))))))

(ert-deftest erc-keywords ()
  (let ((erc-match-quote-when-adding t)
        erc-keywords calls rvs)
    (cl-letf (((symbol-function 'completing-read)
               (lambda (&rest r) (push r calls) (pop rvs))))

      (ert-info ("`erc-add-keyword'")
        (push "[cit. needed]" rvs)
        (ert-simulate-command '(erc-add-keyword))
        (should (equal (cadr (pop calls)) nil))
        (should (equal erc-keywords '("\\[cit\\. needed]"))))

      (ert-info ("`erc-match-keyword-p'")
        (should (erc-match-keyword-p nil "is pretty [cit. needed]")))

      (ert-info ("`erc-delete-keyword'")
        (push "\\[cit\\. needed]" rvs)
        (ert-simulate-command '(erc-delete-keyword))
        (should (equal (cadr (pop calls)) '(("\\[cit\\. needed]"))))
        (should-not erc-keywords))

      (ert-info ("`erc-add-keyword' verbatim")
        (push "[...]" rvs)
        (ert-simulate-command '(erc-add-keyword (4)))
        (should (equal (cadr (pop calls)) nil))
        (should (equal erc-keywords '("[...]")))))))

(ert-deftest erc-dangerous-hosts ()
  (let ((erc-match-quote-when-adding t)
        erc-dangerous-hosts calls rvs)
    (cl-letf (((symbol-function 'completing-read)
               (lambda (&rest r) (push r calls) (pop rvs))))

      (ert-info ("`erc-add-dangerous-host'")
        (push "example.net" rvs)
        (ert-simulate-command '(erc-add-dangerous-host))
        (should (equal (cadr (pop calls)) nil))
        (should (equal erc-dangerous-hosts '("example\\.net"))))

      (ert-info ("`erc-match-dangerous-host-p'")
        (should (erc-match-dangerous-host-p "FOO[m]!~u@example.net" nil)))

      (ert-info ("`erc-delete-dangerous-host'")
        (push "example\\.net" rvs)
        (ert-simulate-command '(erc-delete-dangerous-host))
        (should (equal (cadr (pop calls)) '(("example\\.net"))))
        (should-not erc-dangerous-hosts))

      (ert-info ("`erc-add-dangerous-host' verbatim")
        (push "example.net" rvs)
        (ert-simulate-command '(erc-add-dangerous-host (4)))
        (should (equal (cadr (pop calls)) nil))
        (should (equal erc-dangerous-hosts '("example.net")))))))

(defun erc-match-tests--assert-face-absent (face end)
  "Ensure FACE is absent from point until pos or substring END."
  (when (stringp end)
    (save-excursion
      (search-forward end)
      (setq end (1- (match-beginning 0)))))
  (ert-info ((format "Face %S absent throughout: %S" face
                     (buffer-substring-no-properties (point) end)))
    (while (<= (point) end)
      (ert-info ((format "Looking at: (%d %c)" (char-after) (char-after)))
        (let ((val (ensure-list (get-text-property (point) 'font-lock-face))))
          (should-not (memq face val))))
      (forward-char))))

(defun erc-match-tests--assert-face-present (face end)
  "Ensure FACE is present from point until pos or substring END."
  (when (stringp end)
    (save-excursion
      (search-forward end)
      (setq end (1- (match-beginning 0)))))
  (ert-info ((format "Face %S appears throughout: %S" face
                     (buffer-substring-no-properties (point) end)))
    (while (<= (point) end)
      (ert-info ((format "Looking at: (%d %c)" (char-after) (char-after)))
        (let ((val (ensure-list (get-text-property (point) 'font-lock-face))))
          (should (eq face (car val)))))
      (forward-char))))

(defun erc-match-tests--assert-speaker-highlighted (nick face)
  (search-forward (concat "<" nick ">"))
  (goto-char (pos-bol))
  (should (= (char-after) ?<))
  (should (equal (get-text-property (point) 'font-lock-face)
                 'erc-default-face))

  (ert-info ((format "Nick in <%s> highlighted" nick))
    (forward-char)
    (erc-match-tests--assert-face-present face "> "))

  (should (= (char-after) ?>)))

(defun erc-match-tests--assert-speaker-only-highlighted (nick face)
  (erc-match-tests--assert-speaker-highlighted nick face)
  (ert-info ("Remaining text in line not highlighted")
    (erc-match-tests--assert-face-absent face (pos-eol))))

(defun erc-match-tests--perform (test)
  (erc-tests-common-make-server-buf)
  (setq erc-server-current-nick "tester")
  (let (erc-match--opt-pat-cache)
    (with-current-buffer (erc--open-target "#chan")
      (funcall test)))
  (when noninteractive
    (erc-tests-common-kill-buffers)))

;; The `nick' highlight type only covers a matching sender's speaker
;; tag.  It does not do any highlighting for pal/fool/dangerous-host
;; mentions.  While `current-nick' and `keyword' categories match
;; against a message's content, the speaker's nick is still highlighted
;; (in the corresponding face) when a match occurs.
(defun erc-match-tests--hl-type-nick (face &optional test)
  (should (eq erc-current-nick-highlight-type 'keyword))
  (should (eq erc-keyword-highlight-type 'keyword))

  (erc-match-tests--perform
   (lambda ()
     (erc-tests-common-add-cmem "bob")
     (erc-tests-common-add-cmem "alice")
     ;; Change highlight type for match categories `keyword' and
     ;; `current-nick' to `nick'.
     (let ((erc-current-nick-highlight-type 'nick)
           (erc-keyword-highlight-type 'nick)
           (erc-keywords '("thing")))
       (erc-tests-common-simulate-privmsg "bob" "hi alice")
       (erc-tests-common-simulate-privmsg "alice" "hi bob")
       (erc-tests-common-simulate-privmsg "bob" "hi tester")
       (erc-tests-common-simulate-privmsg "bob" "something blue"))
     (goto-char (point-min))

     ;; A sender's nick appears in `erc-{pals,fools,dangerous-hosts}',
     ;; so the nick portion of their speaker tag alone is highlighted.
     (erc-match-tests--assert-speaker-only-highlighted "bob" face)

     ;; A non-matching sender mentions a would-be match (if message
     ;; bodies were considered), and the nick portion of their speaker
     ;; tag is *not* highlighted.
     (search-forward "<alice>")
     (goto-char (pos-bol))
     (erc-match-tests--assert-face-absent face (pos-eol))

     ;; A matching sender mentions our own nick ("tester"), and their
     ;; speaker's nick is highlighted in `erc-current-nick-face' instead
     ;; of the normal category face (e.g., `erc-pal-face').  This
     ;; happens because the implementation applies highlighting for
     ;; non-NUH-based categories (`keyword' and `current-nick') after
     ;; sender-based ones.
     (should (looking-at (rx "<bob>")))
     (erc-match-tests--assert-speaker-only-highlighted
      "bob" 'erc-current-nick-face)

     ;; A matching sender mentions keyword "tester", and their speaker's
     ;; nick is highlighted in `erc-keyword-face' instead of the normal
     ;; category face for the same reason mentioned above.
     (should (looking-at (rx "<bob>")))
     (erc-match-tests--assert-speaker-only-highlighted
      "bob" 'erc-keyword-face)

     (when test
       (funcall test)))))

(defun erc-match-tests--hl-type-nick/mention (face)
  (erc-match-tests--hl-type-nick
   face
   (lambda ()
     (erc-tests-common-simulate-privmsg "alice" "bob: one")
     (erc-tests-common-simulate-privmsg "alice" "bob, two")
     (erc-tests-common-simulate-privmsg "alice" "three, bob.")

     (search-forward "<alice> bob: one")
     (goto-char (pos-bol))
     (erc-match-tests--assert-speaker-only-highlighted "alice" face)

     (search-forward "<alice> bob, two")
     (goto-char (pos-bol))
     (erc-match-tests--assert-speaker-only-highlighted "alice" face)

     (search-forward "<alice> three, bob.")
     (goto-char (pos-bol))
     (erc-match-tests--assert-speaker-only-highlighted "alice" face))))

(ert-deftest erc-match-message/pal/nick ()
  (should (eq erc-pal-highlight-type 'nick))
  (let ((erc-pals (list "bob")))
    (erc-match-tests--hl-type-nick 'erc-pal-face)))

(ert-deftest erc-match-message/fool/nick ()
  (should (eq erc-fool-highlight-type 'nick))
  (let ((erc-fools (list "bob")))
    (erc-match-tests--hl-type-nick/mention 'erc-fool-face)))

(ert-deftest erc-match-message/dangerous-host/nick ()
  (should (eq erc-dangerous-host-highlight-type 'nick))
  (let ((erc-dangerous-hosts (list "bob")))
    (erc-match-tests--hl-type-nick 'erc-dangerous-host-face)))

(ert-deftest erc-match-message/pal/nick/legacy ()
  (should (eq erc-pal-highlight-type 'nick))
  (with-suppressed-warnings ((erc-match-use-legacy-logic-p obsolete))
    (let ((erc-match-use-legacy-logic-p t)
          (erc-pals (list "bob")))
      (erc-match-tests--hl-type-nick 'erc-pal-face))))

(ert-deftest erc-match-message/fool/nick/legacy ()
  (should (eq erc-fool-highlight-type 'nick))
  (with-suppressed-warnings ((erc-match-use-legacy-logic-p obsolete))
    (let ((erc-match-use-legacy-logic-p t)
          (erc-fools (list "bob")))
      (erc-match-tests--hl-type-nick/mention 'erc-fool-face))))

(ert-deftest erc-match-message/dangerous-host/nick/legacy ()
  (should (eq erc-dangerous-host-highlight-type 'nick))
  (with-suppressed-warnings ((erc-match-use-legacy-logic-p obsolete))
    (let ((erc-match-use-legacy-logic-p t)
          (erc-dangerous-hosts (list "bob")))
      (erc-match-tests--hl-type-nick 'erc-dangerous-host-face))))

;; Mentions are treated as keywords, even in the speaker portion.
;; Contrast this with `erc-match-tests--hl-type-nick/mention', where the
;; speakers are highlighted despite "mention" matches occurring in the
;; message body.
(defun erc-match-tests--hl-type-nick-or-mention (face)
  (erc-match-tests--hl-type-nick
   face
   (lambda ()
     (erc-tests-common-simulate-privmsg "alice" "bob: one bob ONE")
     (erc-tests-common-simulate-privmsg "alice" "bob, two")
     (erc-tests-common-simulate-privmsg "alice" "three, bob.")

     (search-forward "<alice> bob: one")
     (goto-char (pos-bol))
     (erc-match-tests--assert-face-absent face "bob: one")
     (erc-match-tests--assert-face-present face ": one ")
     (erc-match-tests--assert-face-absent face "bob ONE")
     (erc-match-tests--assert-face-present face " ONE")
     (erc-match-tests--assert-face-absent face (pos-eol))

     (search-forward "<alice> bob, two")
     (goto-char (pos-bol))
     (erc-match-tests--assert-face-absent face "bob, two")
     (erc-match-tests--assert-face-present face ", two")
     (erc-match-tests--assert-face-absent face (pos-eol))

     (search-forward "<alice> three, bob.")
     (goto-char (pos-bol))
     (erc-match-tests--assert-face-absent face "bob.")
     (erc-match-tests--assert-face-present face ".")
     (erc-match-tests--assert-face-absent face (pos-eol)))))

(ert-deftest erc-match-message/pal/nick-or-mention ()
  (should (eq erc-pal-highlight-type 'nick))
  (let ((erc-pal-highlight-type 'nick-or-mention)
        (erc-pals (list "bob")))
    (erc-match-tests--hl-type-nick-or-mention 'erc-pal-face)))

(ert-deftest erc-match-message/fool/nick-or-mention ()
  (should (eq erc-fool-highlight-type 'nick))
  (let ((erc-fool-highlight-type 'nick-or-mention)
        (erc-fools (list "bob")))
    (erc-match-tests--hl-type-nick-or-mention 'erc-fool-face)))

(ert-deftest erc-match-message/dangerous-host/nick-or-mention ()
  (should (eq erc-dangerous-host-highlight-type 'nick))
  (let ((erc-dangerous-host-highlight-type 'nick-or-mention)
        (erc-dangerous-hosts (list "bob")))
    (erc-match-tests--hl-type-nick-or-mention 'erc-dangerous-host-face)))

(defun erc-match-tests--hl-type-message (face)
  (should (eq erc-current-nick-highlight-type 'keyword))
  (should (eq erc-keyword-highlight-type 'keyword))

  (erc-match-tests--perform
   (lambda ()
     (erc-tests-common-add-cmem "bob")
     (erc-tests-common-add-cmem "alice")
     ;; Change highlight type for categories `keyword' and
     ;; `current-nick' to `message'.
     (let ((erc-current-nick-highlight-type 'message)
           (erc-keyword-highlight-type 'message)
           (erc-keywords '("thing")))
       (erc-tests-common-simulate-privmsg "bob" "hi alice")
       (erc-tests-common-simulate-privmsg "alice" "hi bob")
       (erc-tests-common-simulate-privmsg "bob" "hi tester")
       (erc-tests-common-simulate-privmsg "bob" "something blue"))
     (goto-char (point-min))

     ;; Message body portion appears in `erc-{pals,fools,dangerous-hosts}'.
     ;; But the speaker portion is not highlighted by `match'.
     (erc-match-tests--assert-face-absent face "hi alice")
     (erc-match-tests--assert-face-present face
                                           (+ (point) (length "hi alice") -1))

     ;; A non-matching sender mentions a would-be match (if message
     ;; bodies were considered), but nothing is highlighted.
     (search-forward "<alice>")
     (goto-char (pos-bol))
     (erc-match-tests--assert-face-absent face (pos-eol))

     ;; A matching sender mentions our own nick ("tester"), and the
     ;; message body is highlighted in `erc-current-nick-face' instead
     ;; of the normal category face (e.g., `erc-pal-face').
     (should (looking-at (rx "<bob>")))
     (save-excursion (erc-match-tests--assert-face-absent face "hi tester"))
     (erc-match-tests--assert-face-absent 'erc-current-nick-face "hi tester")
     (erc-match-tests--assert-face-present 'erc-current-nick-face (pos-eol))

     ;; A matching sender mentions keyword "thing", and the message body
     ;; is highlighted in `erc-keyword-face' instead of the normal
     ;; category face.
     (should (looking-at (rx "<bob>")))
     (save-excursion (erc-match-tests--assert-face-absent face "something"))
     (erc-match-tests--assert-face-absent 'erc-keyword-face "something")
     (erc-match-tests--assert-face-present 'erc-keyword-face (pos-eol)))))

(ert-deftest erc-match-message/pal/message ()
  (should (eq erc-pal-highlight-type 'nick))
  (let ((erc-pals (list "bob"))
        (erc-pal-highlight-type 'message))
    (erc-match-tests--hl-type-message 'erc-pal-face)))

(ert-deftest erc-match-message/fool/message ()
  (should (eq erc-fool-highlight-type 'nick))
  (let ((erc-fools (list "bob"))
        (erc-fool-highlight-type 'message))
    (erc-match-tests--hl-type-message 'erc-fool-face)))

(ert-deftest erc-match-message/dangerous-host/message ()
  (should (eq erc-dangerous-host-highlight-type 'nick))
  (let ((erc-dangerous-hosts (list "bob"))
        (erc-dangerous-host-highlight-type 'message))
    (erc-match-tests--hl-type-message 'erc-dangerous-host-face)))

(ert-deftest erc-match-message/pal/message/legacy ()
  (should (eq erc-pal-highlight-type 'nick))
  (with-suppressed-warnings ((erc-match-use-legacy-logic-p obsolete))
    (let ((erc-match-use-legacy-logic-p t)
          (erc-pals (list "bob"))
          (erc-pal-highlight-type 'message))
      (erc-match-tests--hl-type-message 'erc-pal-face))))

(ert-deftest erc-match-message/fool/message/legacy ()
  (should (eq erc-fool-highlight-type 'nick))
  (with-suppressed-warnings ((erc-match-use-legacy-logic-p obsolete))
    (let ((erc-match-use-legacy-logic-p t)
          (erc-fools (list "bob"))
          (erc-fool-highlight-type 'message))
      (erc-match-tests--hl-type-message 'erc-fool-face))))

(ert-deftest erc-match-message/dangerous-host/message/legacy ()
  (should (eq erc-dangerous-host-highlight-type 'nick))
  (with-suppressed-warnings ((erc-match-use-legacy-logic-p obsolete))
    (let ((erc-match-use-legacy-logic-p t)
          (erc-dangerous-hosts (list "bob"))
          (erc-dangerous-host-highlight-type 'message))
      (erc-match-tests--hl-type-message 'erc-dangerous-host-face))))

(defun erc-match-tests--hl-type-all (face)
  (should (eq erc-current-nick-highlight-type 'keyword))
  (should (eq erc-keyword-highlight-type 'keyword))

  (erc-match-tests--perform
   (lambda ()
     (erc-tests-common-add-cmem "bob")
     (erc-tests-common-add-cmem "alice")
     ;; Change highlight type for categories `current-nick' and
     ;; `keyword' to `all'.
     (let ((erc-current-nick-highlight-type 'all)
           (erc-keyword-highlight-type 'all)
           (erc-keywords '("thing")))
       (erc-tests-common-simulate-privmsg "bob" "hi alice")
       (erc-tests-common-simulate-privmsg "alice" "hi bob")
       (erc-tests-common-simulate-privmsg "bob" "hi tester")
       (erc-tests-common-simulate-privmsg "bob" "something blue"))
     (goto-char (point-min))

     ;; Entire message, including speaker appears in a speaker-based
     ;; face `erc-{pals,fools,dangerous-hosts}'.
     (search-forward "<bob>")
     (goto-char (pos-bol))
     (erc-match-tests--assert-face-present
      face (+ (point) (length "<bob> hi alice") -1))

     ;; A non-matching sender mentions a would-be match (if message
     ;; bodies were considered), but nothing is highlighted.
     (search-forward "<alice>")
     (goto-char (pos-bol))
     (erc-match-tests--assert-face-absent face (pos-eol))

     ;; A matching sender mentions our own nick ("tester"), and the
     ;; entire message, including the speaker portion, is highlighted in
     ;; `erc-current-nick-face' instead of the normal category face
     ;; (e.g., `erc-pal-face').
     (should (looking-at (rx "<bob>")))
     (save-excursion (erc-match-tests--assert-face-absent face (pos-eol)))
     (erc-match-tests--assert-face-present 'erc-current-nick-face (pos-eol))

     ;; A matching sender mentions keyword "thing", and the entire
     ;; message is highlighted in `erc-keyword-face' instead of the
     ;; normal category face.
     (should (looking-at (rx "<bob>")))
     (save-excursion (erc-match-tests--assert-face-absent face (pos-eol)))
     (erc-match-tests--assert-face-present 'erc-keyword-face (pos-eol)))))

(ert-deftest erc-match-message/pal/all ()
  (should (eq erc-pal-highlight-type 'nick))
  (let ((erc-pals (list "bob"))
        (erc-pal-highlight-type 'all))
    (erc-match-tests--hl-type-all 'erc-pal-face)))

(ert-deftest erc-match-message/fool/all ()
  (should (eq erc-fool-highlight-type 'nick))
  (let ((erc-fools (list "bob"))
        (erc-fool-highlight-type 'all))
    (erc-match-tests--hl-type-all 'erc-fool-face)))

(ert-deftest erc-match-message/dangerous-host/all ()
  (should (eq erc-dangerous-host-highlight-type 'nick))
  (let ((erc-dangerous-hosts (list "bob"))
        (erc-dangerous-host-highlight-type 'all))
    (erc-match-tests--hl-type-all 'erc-dangerous-host-face)))

(ert-deftest erc-match-message/pal/all/legacy ()
  (should (eq erc-pal-highlight-type 'nick))
  (with-suppressed-warnings ((erc-match-use-legacy-logic-p obsolete))
    (let ((erc-match-use-legacy-logic-p t)
          (erc-pals (list "bob"))
          (erc-pal-highlight-type 'all))
      (erc-match-tests--hl-type-all 'erc-pal-face))))

(ert-deftest erc-match-message/fool/all/legacy ()
  (should (eq erc-fool-highlight-type 'nick))
  (with-suppressed-warnings ((erc-match-use-legacy-logic-p obsolete))
    (let ((erc-match-use-legacy-logic-p t)
          (erc-fools (list "bob"))
          (erc-fool-highlight-type 'all))
      (erc-match-tests--hl-type-all 'erc-fool-face))))

(ert-deftest erc-match-message/dangerous-host/all/legacy ()
  (should (eq erc-dangerous-host-highlight-type 'nick))
  (with-suppressed-warnings ((erc-match-use-legacy-logic-p obsolete))
    (let ((erc-match-use-legacy-logic-p t)
          (erc-dangerous-hosts (list "bob"))
          (erc-dangerous-host-highlight-type 'all))
      (erc-match-tests--hl-type-all 'erc-dangerous-host-face))))

(defun erc-match-tests--hl-type-nick-or-keyword ()
  (should (eq erc-current-nick-highlight-type 'keyword))

  (erc-match-tests--perform
   (lambda ()
     (erc-tests-common-add-cmem "bob")
     (erc-tests-common-add-cmem "alice")
     ;; Change highlight type for category `current-nick' from the
     ;; default to `nick-or-keyword'.
     (let ((erc-current-nick-highlight-type 'nick-or-keyword))
       (erc-tests-common-simulate-line
        ":irc.foonet.org 353 tester = #chan :bob tester alice")
       (erc-tests-common-simulate-line
        ":irc.foonet.org 366 tester #chan :End of NAMES list")
       (erc-tests-common-simulate-privmsg "bob" "hi tester"))
     (goto-char (point-min))

     ;; An initial NAMES burst arrives.  Its sender is "irc.foonet.org",
     ;; so `match' skips the "nick" half of `nick-or-keyword' and
     ;; considers the input non-NUH-based (because a host name alone
     ;; can't be a real user).  IOW, it pretends the option's value is
     ;; `keyword', and highlights all occurrences in the message body.
     (search-forward "*** Users on #chan: bob tester")
     (goto-char (pos-bol))
     (erc-match-tests--assert-face-absent 'erc-current-nick-face "tester")
     (erc-match-tests--assert-face-present 'erc-current-nick-face
                                           (+ (point) (length "tester") -1))
     (erc-match-tests--assert-face-absent 'erc-current-nick-face (pos-eol))

     ;; Someone mentions our nick ("tester"), and only their speaker
     ;; tag's nick is highlighted in `erc-current-nick-face' because
     ;; that speaker is a real server user.
     (search-forward "<bob>")
     (goto-char (pos-bol))
     (should-not (get-text-property (point) 'erc-current-nick-face))
     (forward-char)
     (erc-match-tests--assert-face-present 'erc-current-nick-face
                                           "> hi tester")
     (erc-match-tests--assert-face-absent 'erc-current-nick-face
                                          (+ (point) (length "hi tester"))))))

(ert-deftest erc-match-message/current-nick/nick-or-keyword ()
  (erc-match-tests--hl-type-nick-or-keyword))

(ert-deftest erc-match-message/current-nick/nick-or-keyword/legacy ()
  (with-suppressed-warnings ((erc-match-use-legacy-logic-p obsolete))
    (let ((erc-match-use-legacy-logic-p t))
      (erc-match-tests--hl-type-nick-or-keyword))))

(defun erc-match-tests--hl-type-keyword ()
  (should (eq erc-keyword-highlight-type 'keyword))

  (erc-match-tests--perform
   (lambda ()
     (erc-tests-common-add-cmem "bob")
     (erc-tests-common-add-cmem "imamodel")
     (erc-tests-common-add-cmem "ModerNerd")

     (let ((erc-keywords '("mode")))
       (erc-tests-common-simulate-line
        ":irc.foonet.org 353 tester = #chan :bob imamodel ModerNerd tester")
       (erc-tests-common-simulate-line
        ":irc.foonet.org 366 tester #chan :End of NAMES list")
       (erc-tests-common-simulate-line
        ":irc.foonet.org 324 tester #chan +Cnt")
       (erc-tests-common-simulate-line
        ":irc.foonet.org 329 tester #chan 1703579802")
       (erc-tests-common-simulate-privmsg "bob" "imamodel: spam a la mode!")
       (erc-tests-common-simulate-privmsg "imamodel" "hi bob"))

     (goto-char (point-min))

     ;; All occurrences highlighted in a non-user-based message.
     (search-forward "*** Users on #chan:")
     (goto-char (pos-bol))
     (erc-match-tests--assert-face-absent 'erc-keyword-face "model ")
     (erc-match-tests--assert-face-present 'erc-keyword-face "l ")
     (erc-match-tests--assert-face-absent 'erc-keyword-face "Mode")
     (erc-match-tests--assert-face-present 'erc-keyword-face "rNerd")
     (erc-match-tests--assert-face-absent 'erc-keyword-face (pos-eol))

     ;; Formatted text matched against rather than original message.
     (search-forward "*** #chan modes:")
     (goto-char (pos-bol))
     (erc-match-tests--assert-face-absent 'erc-keyword-face "modes:")
     (erc-match-tests--assert-face-present 'erc-keyword-face "s: +Cnt")
     (erc-match-tests--assert-face-absent 'erc-keyword-face (pos-eol))

     ;; All occurrences highlighted in a user-based message.
     (search-forward "<bob>")
     (goto-char (pos-bol))
     (erc-match-tests--assert-face-absent 'erc-keyword-face "model")
     (erc-match-tests--assert-face-present 'erc-keyword-face "l: spam")
     (erc-match-tests--assert-face-absent 'erc-keyword-face "mode!")
     (erc-match-tests--assert-face-present 'erc-keyword-face "!")
     (erc-match-tests--assert-face-absent 'erc-keyword-face (pos-eol))

     ;; Matching speaker ignored.
     (search-forward "<imamodel>")
     (goto-char (pos-bol))
     (erc-match-tests--assert-face-absent 'erc-keyword-face (pos-eol)))))

(ert-deftest erc-match-message/keyword/keyword ()
  (erc-match-tests--hl-type-keyword))

(ert-deftest erc-match-message/keyword/keyword/legacy ()
  (with-suppressed-warnings ((erc-match-use-legacy-logic-p obsolete))
    (let ((erc-match-use-legacy-logic-p t))
      (erc-match-tests--hl-type-keyword))))

(defun erc-match-tests--log-matches ()
  (let ((erc-log-matches-flag t)
        (erc-timestamp-format "[@@TS@@]")
        (inhibit-message noninteractive))
    (erc-match-tests--hl-type-keyword)
    (with-current-buffer "*scratch*"
      (ert-simulate-keys "\t\r"
        (erc-go-to-log-matches-buffer))
      (should (equal (buffer-name) "ERC Keywords"))
      (goto-char (point-min))
      (should (equal (buffer-string) "\
 == Type \"q\" to dismiss messages ==
[@@TS@@]<Server:353:#chan> *** Users on #chan: bob imamodel ModerNerd tester
[@@TS@@]<Server:324:#chan> *** #chan modes: +Cnt
[@@TS@@]<bob:#chan> imamodel: spam a la mode!
"))
      (when noninteractive
        (kill-buffer)))))

(ert-deftest erc-log-matches ()
  (erc-match-tests--log-matches))

(ert-deftest erc-log-matches/legacy ()
  (with-suppressed-warnings ((erc-match-use-legacy-logic-p obsolete))
    (let ((erc-match-use-legacy-logic-p t))
      (erc-match-tests--log-matches))))

;; This demos bare-bones usage of the `erc-match' API that implicitly
;; opts out of the traditional options and "parts"-based mechanism.  The
;; user does not have to provide a `:part' keyword because they've
;; overridden the `:handler', meaning `erc-match-highlight-by-part'
;; never runs.  This is somewhat analogous but ultimately orthogonal to
;; `erc-text-matched-hook' not running because that happens on account
;; of the user not specifying a `:category' field.
(ert-deftest erc-match-functions/api/non-parts-based ()
  (let* ((results ())
         (erc-text-matched-hook (lambda (&rest r) (push r results)))
         (erc-match-functions
          (list
           (lambda (&rest plist)
             ;; Doing everything in `:pred' would also work if
             ;; specifying `ignore' for `:handler'.  And you wouldn't
             ;; even need to return non-nil on matches.
             (apply #'erc-match
                    :predicate (lambda (_) (search-forward "alice" nil t))
                    :handler (lambda (_) (push (match-string 0) results))
                    plist)))))

    (erc-match-tests--perform
     (lambda ()
       (erc-tests-common-add-cmem "bob")
       (erc-tests-common-add-cmem "Alice")
       (erc-tests-common-simulate-line
        ":irc.foonet.org 353 tester = #chan :bob tester Alice")
       (erc-tests-common-simulate-line
        ":irc.foonet.org 366 tester #chan :End of NAMES list")
       (erc-tests-common-simulate-privmsg "bob" "hi ALICE")
       (goto-char (point-min))

       (should (equal results '("ALICE" "Alice")))))))

;; This one piggybacks on infrastructure supporting the traditional
;; `match' interface.
(ert-deftest erc-match-functions/api/parts-based ()
  (let* ((results ())
         (erc-text-matched-hook (lambda (&rest r) (push r results)))
         (erc-match-functions ()))

    (erc-match-tests--perform
     (lambda ()

       ;; Use local setter for no particular reason.
       (add-hook 'erc-match-functions
                 (lambda (&rest plist)
                   (apply #'erc-match-traditional
                          :category 'keyword
                          :part 'keyword
                          :data '("alice")
                          :face 'error
                          :predicate (lambda (_)
                                       (search-forward "alice" nil t))
                          plist))
                 0 t)

       (erc-tests-common-add-cmem "bob")
       (erc-tests-common-add-cmem "Alice")
       (erc-tests-common-simulate-line
        ":irc.foonet.org 353 tester = #chan :Alice bob tester")
       (erc-tests-common-simulate-line
        ":irc.foonet.org 366 tester #chan :End of NAMES list")
       (erc-tests-common-simulate-privmsg "bob" "hi ALICE")
       (goto-char (point-min))

       (search-forward "*** Users on #chan:")
       (erc-match-tests--assert-face-absent 'error "Alice")
       (erc-match-tests--assert-face-present 'error " bob")
       (erc-match-tests--assert-face-absent 'error (pos-eol))

       (should (equal results
                      '(( keyword "bob!~bob@fsf.org" "hi ALICE\n")
                        ( keyword "Server:353"
                          "*** Users on #chan: Alice bob tester\n"))))))))

;;; erc-match-tests.el ends here

debug log:

solving 0b90867b32d ...
found 0b90867b32d in https://yhetil.org/emacs-bugs/87h68rrs3o.fsf__9524.09048784233$1730468446$gmane$org@neverwas.me/
found e8726ca148e in https://yhetil.org/emacs-bugs/877c9v4u97.fsf__36819.6135548473$1729900311$gmane$org@neverwas.me/
found fb92a153c95 in https://git.savannah.gnu.org/cgit/emacs.git
preparing index
index prepared:
100644 fb92a153c9528941453f24e3922f9599ff82922b	test/lisp/erc/erc-match-tests.el

applying [1/2] https://yhetil.org/emacs-bugs/877c9v4u97.fsf__36819.6135548473$1729900311$gmane$org@neverwas.me/
diff --git a/test/lisp/erc/erc-match-tests.el b/test/lisp/erc/erc-match-tests.el
index fb92a153c95..e8726ca148e 100644


applying [2/2] https://yhetil.org/emacs-bugs/87h68rrs3o.fsf__9524.09048784233$1730468446$gmane$org@neverwas.me/
diff --git a/test/lisp/erc/erc-match-tests.el b/test/lisp/erc/erc-match-tests.el
index e8726ca148e..0b90867b32d 100644

Checking patch test/lisp/erc/erc-match-tests.el...
Applied patch test/lisp/erc/erc-match-tests.el cleanly.
Checking patch test/lisp/erc/erc-match-tests.el...
Applied patch test/lisp/erc/erc-match-tests.el cleanly.

skipping https://yhetil.org/emacs-bugs/87h68rrs3o.fsf__9524.09048784233$1730468446$gmane$org@neverwas.me/ for 0b90867b32d
index at:
100644 0b90867b32d67afedd3ca24e2b17c452db032c1b	test/lisp/erc/erc-match-tests.el

(*) Git path names are given by the tree(s) the blob belongs to.
    Blobs themselves have no identifier aside from the hash of its contents.^

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).