unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
blob cf1b3ba78f918f27c8a49cb8ec713f76c226db2f 6134 bytes (raw)
name: test/lisp/erc/erc-fill-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
 
;;; erc-fill-tests.el --- ERC message filling -*- lexical-binding: t -*-

;; Copyright (C) 2020-2021 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/>.

;;; Code:

(require 'ert-x)
(require 'erc-fill)

(defun erc-fill-tests--insert (&rest strings)
  (let ((inhibit-read-only t))
    (erc-parse-server-response erc-server-process (apply #'concat strings))))

(defun erc-fill-tests--setup-server-buffer ()
  (with-current-buffer (get-buffer-create "foonet")
    (erc-mode)
    (setq erc-server-process (start-process "true" (current-buffer) "true")
          erc-server-current-nick "tester"
          erc-server-users (make-hash-table :test #'equal))
    (set-process-query-on-exit-flag erc-server-process nil)))

(defun erc-fill-tests--setup-channel-buffer ()
  (with-current-buffer (get-buffer-create "#chan")
    (erc-mode)
    (insert "\n\n")
    (setq erc-input-marker (make-marker)
          ;; Kludge to get around saving display prop
          erc-timestamp-use-align-to nil
          ;; Kludge to make whitespace compare equal without expanding
          indent-tabs-mode nil
          erc-insert-marker (make-marker)
          erc-default-recipients '("#chan")
          erc-channel-users (make-hash-table :test #'equal)
          erc-server-process (with-current-buffer "foonet"
                               erc-server-process))
    (set-marker erc-insert-marker (point-max))
    (erc-display-prompt)))

(defun erc-fill-tests--populate ()
  (let* ((ts (+ (* 2 60 60 24) (* 60 60 24 365 10)))
         (ct (time-convert ts)))

    (cl-letf (((symbol-function 'current-time) (lambda () ct)))
      (with-current-buffer "foonet"
        (erc-fill-tests--insert ":irc.foonet.org 324 tester #chan +nt")
        (erc-fill-tests--insert ":irc.foonet.org 329 tester #chan 1620104779")

        (setq ct (time-convert (cl-incf ts 60)))
        (erc-fill-tests--insert
         ":bob!~u@rz2v467q4rwhy.irc PRIVMSG #chan :lorem ipsum"
         " This buffer is for text that is not saved, and for Lisp evaluation.")

        (setq ct (time-convert (cl-incf ts 120)))
        (erc-fill-tests--insert
         ":alice!~u@rz2v467q4rwhy.irc PRIVMSG #chan :tester, welcome!"
         " Your name may or may not be highlighted depending on whether"
         " button's been loaded by an earlier test. ERC needs help!")

        (setq ct (time-convert (cl-incf ts (* 60 60 24))))
        (erc-fill-tests--insert
         ":bob!~u@rz2v467q4rwhy.irc PRIVMSG #chan :tester, welcome!"
         " To create a file, visit it with ? and enter text in its buffer.")))))

(defun erc-fill-tests--teardown ()
  ;; XXX when inspecting manually, must reactivate fill and stamp modes.
  ;; Otherwise `erc-fill-buffer' won't work.
  (kill-buffer "variable-60.buffer")
  (kill-buffer "variable-72.buffer")
  (kill-buffer "static-60.buffer")
  (kill-buffer "static-72.buffer")
  (advice-remove 'format-time-string 'ts)
  (let (erc-kill-server-hook
        erc-kill-channel-hook)
    (kill-buffer "#chan")
    (kill-buffer "foonet"))
  (should (= erc-fill-column 78)))

(defun erc-fill-tests--compare (name)
  ;; Git didn't allow committing with a trailing space after the
  ;; prompt, hence this:
  (equal (substring-no-properties (buffer-string) 0 -1)
         (with-current-buffer (find-file-literally (ert-resource-file name))
           (buffer-string))))

(defun erc-fill-tests--await-fill ()
  (call-interactively #'erc-fill-buffer)
  ;; This timeout silliness seemed a little more realistic than just:
  ;;
  ;;   (thread-join erc-fill--refill-thread)
  ;;
  ;; Probably dumb, right?.
  (with-timeout (3 (error "Failed"))
    (while (thread-live-p erc-fill--refill-thread)
      (sleep-for 0.01))))

(ert-deftest erc-fill-buffer ()
  (advice-add 'format-time-string :filter-args
              (lambda (args) (list (car args) (cadr args) 0)) '((name . ts)))
  (let* (erc-insert-pre-hook
         erc-insert-modify-hook
         erc-send-modify-hook
         erc-mode-hook
         erc-stamp-mode
         erc-fill--refill-thread)

    (erc-stamp-mode +1)

    (erc-fill-tests--setup-server-buffer)
    (erc-fill-tests--setup-channel-buffer)
    (erc-fill-tests--populate)

    (with-current-buffer "#chan"
      ;; These would get clobbered by the new thread if we let-bound
      ;; them, and we can't set them globally, so best just fake it:
      (setq-local erc-fill-mode t
                  erc-stamp-mode t
                  erc-fill-column 60)
      (erc-fill-tests--await-fill)
      (ert-info ("Baseline")
        (should (erc-fill-tests--compare "variable-60.buffer")))

      (ert-info ("Wider")
        (setq erc-fill-column 72)
        (erc-fill-tests--await-fill)
        (should (erc-fill-tests--compare "variable-72.buffer")))

      (ert-info ("Fancy")
        (setq erc-fill-function #'erc-fill-static)
        (erc-fill-tests--await-fill)
        (should (erc-fill-tests--compare "static-72.buffer")))

      (ert-info ("Fancy normal")
        (setq erc-fill-column 60)
        (erc-fill-tests--await-fill)
        (should (erc-fill-tests--compare "static-60.buffer")))

      (ert-info ("Again!")
        (erc-fill-tests--await-fill)
        (should (erc-fill-tests--compare "static-60.buffer")))

      (ert-info ("Back home")
        (setq erc-fill-function #'erc-fill-variable)
        (erc-fill-tests--await-fill)
        (should (erc-fill-tests--compare "variable-60.buffer")))))

  (when noninteractive
    (erc-fill-tests--teardown)))

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

debug log:

solving cf1b3ba78f ...
found cf1b3ba78f in https://yhetil.org/emacs-bugs/87v90pzfgn.fsf__41738.4281916142$1637247311$gmane$org@neverwas.me/

applying [1/1] https://yhetil.org/emacs-bugs/87v90pzfgn.fsf__41738.4281916142$1637247311$gmane$org@neverwas.me/
diff --git a/test/lisp/erc/erc-fill-tests.el b/test/lisp/erc/erc-fill-tests.el
new file mode 100644
index 0000000000..cf1b3ba78f

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

index at:
100644 cf1b3ba78f918f27c8a49cb8ec713f76c226db2f	test/lisp/erc/erc-fill-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).