all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
blob b2ece3049a1eed3435476c6df9af6a81541ca9ba 7662 bytes (raw)
name: test/lisp/net/shr-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
 
;;; shr-tests.el --- tests for shr.el  -*- lexical-binding: t; -*-

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

;; Author: Lars Ingebrigtsen <larsi@gnus.org>

;; 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)
(require 'ert-x)
(require 'shr)

(declare-function libxml-parse-html-region "xml.c")

(defvar shr-test--max-wait-time 5
  "The maximum amount of time to wait for a condition to resolve, in seconds.
See `shr-test-wait-for'.")

(defun shr-test-wait-for (predicate &optional message)
  "Wait until PREDICATE returns non-nil.
If this takes longer than `shr-test--max-wait-time', raise an error.
MESSAGE is an optional message to use if this times out."
  (let ((start (current-time))
        (message (or message "timed out waiting for condition")))
    (while (not (funcall predicate))
      (when (> (float-time (time-since start))
               shr-test--max-wait-time)
        (error message))
      (sit-for 0.1))))

(defun shr-test--rendering-check (name &optional context)
  "Render NAME.html and compare it to NAME.txt.
Raise a test failure if the rendered buffer does not match NAME.txt.
Append CONTEXT to the failure data, if non-nil."
  (let ((text-file (file-name-concat (ert-resource-directory) (concat name ".txt")))
        (html-file (file-name-concat (ert-resource-directory) (concat name ".html")))
        (description (if context (format "%s (%s)" name context) name)))
    (with-temp-buffer
      (insert-file-contents html-file)
      (let ((dom (libxml-parse-html-region (point-min) (point-max)))
            (shr-width 80)
            (shr-use-fonts nil))
        (erase-buffer)
        (shr-insert-document dom)
        (let ((result (buffer-substring-no-properties (point-min) (point-max)))
              (expected
               (with-temp-buffer
                 (insert-file-contents text-file)
                 (while (re-search-forward "%\\([0-9A-F][0-9A-F]\\)" nil t)
                   (replace-match (string (string-to-number (match-string 1) 16))
                                  t t))
                 (buffer-string))))
          (unless (equal result expected)
            (ert-fail (list description result expected))))))))

(defconst shr-test--rendering-extra-configs
  '(("blockquote"
     ;; Make sure blockquotes remain indented even when filling is
     ;; disabled (bug#69555).
     . ((shr-fill-text . nil))))
  "Extra customizations which can impact rendering.
This is a list of (NAME . SETTINGS) pairs.  NAME is the basename of a
set of txt/html files under shr-resources/, as passed to `shr-test'.
SETTINGS is a list of (OPTION . VALUE) pairs that are interesting to
validate for the NAME testcase.

The `rendering' testcase will test NAME once without altering any
settings, then once more for each (OPTION . VALUE) pair.")

;;; Tests:

(ert-deftest rendering ()
  (skip-unless (fboundp 'libxml-parse-html-region))
  (dolist (file (directory-files (ert-resource-directory) nil "\\.html\\'"))
    (let* ((name (string-remove-suffix ".html" file))
           (extra-options (alist-get name shr-test--rendering-extra-configs
                                     nil nil 'string=)))
      ;; Test once with default settings.
      (shr-test--rendering-check name)
      ;; Test once more for every extra option for this specific NAME.
      (pcase-dolist (`(,option-sym ,option-val)
                     extra-options)
        (let ((option-old (symbol-value option-sym)))
          (set option-sym option-val)
          (unwind-protect
              (shr-test--rendering-check
               name (format "with %s %s" option-sym option-val))
            (set option-sym option-old)))))))

(ert-deftest use-cookies ()
  (let ((shr-cookie-policy 'same-origin))
    (should
     (shr--use-cookies-p "http://images.fsf.org" '("http://www.fsf.org")))
    (should
     (shr--use-cookies-p "http://www.fsf.org" '("https://www.fsf.org")))
    (should
     (shr--use-cookies-p "http://www.fsf.org" '("https://www.fsf.org")))
    (should
     (shr--use-cookies-p "http://www.fsf.org" '("http://fsf.org")))
    (should-not
     (shr--use-cookies-p "http://www.gnu.org" '("http://www.fsf.org")))))

(ert-deftest shr-srcset ()
  (should (equal (shr--parse-srcset "") nil))

  (should (equal (shr--parse-srcset "a 10w, b 20w")
                 '(("b" 20) ("a" 10))))

  (should (equal (shr--parse-srcset "a 10w b 20w")
                 '(("a" 10))))

  (should (equal (shr--parse-srcset "https://example.org/1\n\n 10w , https://example.org/2 20w      ")
	         '(("https://example.org/2" 20) ("https://example.org/1" 10))))

  (should (equal (shr--parse-srcset "https://example.org/1,2\n\n 10w , https://example.org/2 20w      ")
	         '(("https://example.org/2" 20) ("https://example.org/1,2" 10)))))

(ert-deftest shr-test/zoom-image ()
  "Test that `shr-zoom-image' properly replaces the original image."
  (let ((image (expand-file-name "data/image/blank-100x200.png"
                                 (getenv "EMACS_TEST_DIRECTORY"))))
    (dolist (alt '(nil "" "nothing to see here"))
      (with-temp-buffer
        (ert-info ((format "image with alt=%S" alt))
          (let ((attrs (if alt (format " alt=\"%s\"" alt) "")))
            (insert (format "<img src=\"file://%s\" %s" image attrs)))
          (cl-letf* (;; Pretend we're a graphical display.
                     ((symbol-function 'display-graphic-p) #'always)
                     ((symbol-function 'url-queue-retrieve)
                      (lambda (&rest args)
                        (apply #'run-at-time 0 nil #'url-retrieve args)))
                     (put-image-calls 0)
                     (shr-put-image-function
                      (lambda (&rest args)
                        (cl-incf put-image-calls)
                        (apply #'shr-put-image args)))
                     (shr-width 80)
                     (shr-use-fonts nil)
                     (shr-image-animate nil)
                     (dom (libxml-parse-html-region (point-min) (point-max))))
            ;; Render the document.
            (erase-buffer)
            (shr-insert-document dom)
            (shr-test-wait-for (lambda () (= put-image-calls 1)))
            ;; Now zoom the image.
            (goto-char (point-min))
            (shr-zoom-image)
            (shr-test-wait-for (lambda () (= put-image-calls 2)))
            ;; Check that we got a sliced image.
            (let ((slice-count 0))
              (goto-char (point-min))
              (while-let ((pos (next-single-property-change (point) 'display)))
                (when-let ((display (get-text-property pos 'display)))
                  ;; If this is nil, we found a non-sliced image, but we
                  ;; should have replaced that!
                  (should (assq 'slice display))
                  (cl-incf slice-count))
                (goto-char pos))
              ;; Make sure we actually saw a slice.
              (should (> slice-count 1)))))))))

(require 'shr)

;;; shr-tests.el ends here

debug log:

solving b2ece3049a1 ...
found b2ece3049a1 in https://yhetil.org/emacs/7ee8d7ac-93d5-4ce3-488a-2ece09f659b7@gmail.com/
found 17138053450 in https://git.savannah.gnu.org/cgit/emacs.git
preparing index
index prepared:
100644 171380534504e972b9664e05b7ec8364583c39c3	test/lisp/net/shr-tests.el

applying [1/1] https://yhetil.org/emacs/7ee8d7ac-93d5-4ce3-488a-2ece09f659b7@gmail.com/
diff --git a/test/lisp/net/shr-tests.el b/test/lisp/net/shr-tests.el
index 17138053450..b2ece3049a1 100644

Checking patch test/lisp/net/shr-tests.el...
Applied patch test/lisp/net/shr-tests.el cleanly.

index at:
100644 b2ece3049a1eed3435476c6df9af6a81541ca9ba	test/lisp/net/shr-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 external index

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

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.