pdf-virtual-test.el (8976B)
1 ;; -*- lexical-binding: t -*- 2 3 (require 'pdf-virtual) 4 (require 'ert) 5 6 (defvar pdf-virtual-test-document 7 '(("test.pdf" 8 ;; Annotations 3,4,5 9 (2 . (0.1805 0.2462 0.4046 0.3392)) 10 ;; Should match first paragraph. 11 (1 . (0.2163 0.1879 0.7848 0.22)) 12 4 3 5 6))) 13 14 (defmacro with-pdf-virtual-test-document (var &rest body) 15 (declare (indent 1) (debug t)) 16 `(let ((,var (pdf-virtual-document-create 17 pdf-virtual-test-document))) 18 ,@body)) 19 20 (defmacro with-pdf-virtual-test-buffer (&rest body) 21 (declare (indent 0) (debug t)) 22 `(let ((doc pdf-virtual-test-document)) 23 (pdf-info-process-assert-running t) 24 (with-temp-buffer 25 (insert ";; %VPDF 1.0\n\n") 26 (let (print-length) 27 (pp doc (current-buffer))) 28 (pdf-virtual-view-mode) 29 (progn ,@body)))) 30 31 (ert-deftest pdf-virtual-document-create () 32 (let ((doc (pdf-virtual-document-create 33 pdf-virtual-test-document))) 34 (should (pdf-virtual-document-p doc)) 35 (should (= (length (pdf-virtual-document-page-array doc)) 36 6)) 37 (should (equal (pdf-virtual-document-file-map doc) 38 '(("test.pdf" 1)))))) 39 40 (ert-deftest pdf-virtual-document-filenames () 41 (with-pdf-virtual-test-document doc 42 (should (equal (pdf-virtual-document-filenames doc) 43 '("test.pdf"))))) 44 45 (ert-deftest pdf-virtual-document-pages () 46 (with-pdf-virtual-test-document doc 47 (should (equal '(("test.pdf" (4 . 4) nil) 48 ("test.pdf" (3 . 3) nil) 49 ("test.pdf" (5 . 6) nil)) 50 (pdf-virtual-document-pages '(3 . 6) doc))))) 51 52 (ert-deftest pdf-virtual-document-page () 53 (with-pdf-virtual-test-document doc 54 (should (equal '("test.pdf" 6 nil) 55 (pdf-virtual-document-page 6 doc))))) 56 57 (ert-deftest pdf-virtual-document-page-of () 58 (with-pdf-virtual-test-document doc 59 (let ((pages '(2 1 4 3 5 6))) 60 (dotimes (i (length pages)) 61 (should (equal (1+ i) 62 (pdf-virtual-document-page-of 63 "test.pdf" (nth i pages) nil doc))))))) 64 65 (ert-deftest pdf-virtual-open () 66 (with-pdf-virtual-test-buffer 67 (should (progn (pdf-info-open) t)))) 68 69 (ert-deftest pdf-virtual-close () 70 (with-pdf-virtual-test-buffer 71 (should (progn (pdf-info-close) t)))) 72 73 (ert-deftest pdf-virtual-metadata () 74 (with-pdf-virtual-test-buffer 75 (should (consp (pdf-info-metadata))))) 76 77 (ert-deftest pdf-virtual-search () 78 (with-pdf-virtual-test-buffer 79 (dolist (m (list (pdf-info-search-string "PDF" 2) 80 (pdf-info-search-regexp "PDF" 2))) 81 (should (= 2 (length m))) 82 (should (equal (mapcar (apply-partially 'alist-get 'page) 83 m) 84 '(2 2))) 85 (should (cl-every (lambda (elt) 86 (cl-every 'pdf-test-relative-edges-p elt)) 87 (mapcar (apply-partially 'alist-get 'edges) 88 m)))))) 89 90 (ert-deftest pdf-virtual-pagelinks () 91 (with-pdf-virtual-test-buffer 92 (let ((links (pdf-info-pagelinks 4))) 93 (should (cl-every 'pdf-test-relative-edges-p 94 (mapcar (apply-partially 'alist-get 'edges) 95 links))) 96 (should (equal (mapcar (apply-partially 'alist-get 'type) 97 links) 98 '(goto-dest uri))) 99 (should (equal (mapcar (apply-partially 'alist-get 'uri) 100 links) 101 '(nil "http://www.gnu.org")))))) 102 103 (ert-deftest pdf-virtual-number-of-pages () 104 (with-pdf-virtual-test-buffer 105 (should (= 6 (pdf-info-number-of-pages))))) 106 107 (ert-deftest pdf-virtual-outline () 108 (with-pdf-virtual-test-buffer 109 (let ((outline (pdf-info-outline))) 110 (should (= 8 (length outline))) 111 (should (equal (mapcar (apply-partially 'alist-get 'depth) 112 outline) 113 '(1 2 2 2 2 2 3 4))) 114 (should (cl-every (lambda (type) 115 (equal type 'goto-dest)) 116 (mapcar (apply-partially 'alist-get 'type) 117 (cdr outline))))))) 118 119 (ert-deftest pdf-virtual-gettext () 120 (with-pdf-virtual-test-buffer 121 (let ((text (pdf-info-gettext 2 '(0 0 1 1)))) 122 (should 123 (= 2 (with-temp-buffer 124 (insert text) 125 (count-matches "PDF" 1 (point)))))))) 126 127 (ert-deftest pdf-virtual-getselection () 128 (with-pdf-virtual-test-buffer 129 (should (consp (pdf-info-getselection 1 '(0 0 1 1)))) 130 (should (cl-every 'pdf-test-relative-edges-p 131 (pdf-info-getselection 1 '(0 0 1 1)))))) 132 133 (ert-deftest pdf-virtual-charlayout () 134 (with-pdf-virtual-test-buffer 135 (let ((cl (pdf-info-charlayout 1))) 136 (should (eq ?3 (car (car cl)))) 137 (should (eq ?y (car (car (last cl))))) 138 (should (cl-every 'characterp (mapcar 'car cl))) 139 (should (cl-every 140 (apply-partially 141 'cl-every 'pdf-test-relative-edges-p) 142 (mapcar 'cdr cl)))))) 143 144 (ert-deftest pdf-virtual-pagesize () 145 (with-pdf-virtual-test-buffer 146 (let* ((os '(612 . 792)) 147 (s (pdf-info-pagesize 1)) 148 (ds (cons (* (- 0.4046 0.1879) (car os)) 149 (* (- 0.3392 0.2462) (cdr os))))) 150 (should (< (abs (- (car s) (car ds))) 10)) 151 (should (< (abs (- (cdr s) (cdr ds))) 10))))) 152 153 (ert-deftest pdf-virtual-getannots () 154 (with-pdf-virtual-test-buffer 155 (let ((a (pdf-info-getannots 1))) 156 (should (= 3 (length a))) 157 (should (equal (sort (copy-sequence '(highlight underline squiggly)) 158 'string<) 159 (sort (mapcar (lambda (elt) 160 (cdr (assq 'type elt))) 161 a) 162 'string<)))))) 163 164 (ert-deftest pdf-virtual-getannot () 165 (with-pdf-virtual-test-buffer 166 (let* ((a1 (car (pdf-info-getannots 1))) 167 (a2 (pdf-info-getannot (cdr (assq 'id a1))))) 168 (should (equal a1 a2))))) 169 170 (ert-deftest pdf-virtual-addannot () 171 (with-pdf-virtual-test-buffer 172 (should-error (pdf-info-addannot 1 '(0 0 1 1) 'text)))) 173 174 (ert-deftest pdf-virtual-delannot () 175 (skip-unless (pdf-info-writable-annotations-p)) 176 (with-pdf-virtual-test-buffer 177 (should-error (pdf-info-delannot 178 (cdr (assq 'id (car (pdf-info-getannots 1)))))))) 179 180 (ert-deftest pdf-virtual-mvannot () 181 (skip-unless (pdf-info-writable-annotations-p)) 182 (with-pdf-virtual-test-buffer 183 (should-error (pdf-info-mvannot 184 (cdr (assq 'id (car (pdf-info-getannots 1)))) 185 '(0 0 0 0))))) 186 187 (ert-deftest pdf-virtual-editannot () 188 (skip-unless (pdf-info-writable-annotations-p)) 189 (with-pdf-virtual-test-buffer 190 (should-error (pdf-info-editannot 191 (cdr (assq 'id (car (pdf-info-getannots 1)))) 192 '((color . "blue")))))) 193 194 (ert-deftest pdf-virtual-save () 195 (skip-unless (pdf-info-writable-annotations-p)) 196 (with-pdf-virtual-test-buffer 197 (should-error (pdf-info-save)))) 198 199 (ert-deftest pdf-virtual-adapter-argument-handling () 200 (let ((enabled-p pdf-virtual-global-minor-mode)) 201 (unwind-protect 202 (progn 203 (pdf-virtual-global-minor-mode 1) 204 (with-pdf-virtual-test-buffer 205 (should (stringp (pdf-info-renderpage 1 100 :alpha 0.1))) 206 (should (stringp (pdf-info-renderpage 207 1 100 (current-buffer) :alpha 0.2)))) 208 (pdf-test-with-test-pdf 209 (should (plist-get (pdf-info-setoptions 210 :render/printed t) 211 :render/printed)) 212 (should-not (plist-get (pdf-info-setoptions 213 (current-buffer) 214 :render/printed nil) 215 :render/printed)) 216 (should (plist-get (pdf-info-setoptions 217 (buffer-file-name) 218 :render/printed t) 219 :render/printed)))) 220 (unless enabled-p 221 (pdf-virtual-global-minor-mode -1))))) 222 223 ;; (ert-deftest pdf-virtual-getattachment-from-annot () 224 ;; ) 225 226 ;; (ert-deftest pdf-virtual-getattachments () 227 ;; ) 228 229 ;; (ert-deftest pdf-virtual-synctex-forward-search () 230 ;; ) 231 232 ;; (ert-deftest pdf-virtual-synctex-backward-search () 233 ;; ) 234 235 ;; (ert-deftest pdf-virtual-renderpage () 236 ;; ) 237 238 ;; (ert-deftest pdf-virtual-boundingbox () 239 ;; ) 240 241 ;; (ert-deftest pdf-virtual-pagelabels () 242 ;; ) 243 244 ;; (ert-deftest pdf-virtual-setoptions () 245 ;; ) 246 247 ;; (ert-deftest pdf-virtual-getoptions () 248 ;; )