pdf-info-test.el (9383B)
1 ;; -*- lexical-binding: t -*- 2 3 (ert-deftest pdf-info-open/close () 4 (pdf-test-with-test-pdf 5 (should-not (pdf-info-open)) 6 (should (pdf-info-close))) 7 (pdf-test-with-encrypted-pdf 8 (should-error (pdf-info-open nil "Invalid Password")) 9 (should-not (pdf-info-open nil "pdftool")))) 10 11 (ert-deftest pdf-info-metadata () 12 (pdf-test-with-test-pdf 13 (should (cl-every (lambda (elt) 14 (and (consp elt) 15 (symbolp (car elt)))) 16 (pdf-info-metadata))))) 17 18 (ert-deftest pdf-info-search-string () 19 (pdf-test-with-test-pdf 20 (let (matches) 21 (should (setq matches (pdf-info-search-string "PDF Tools"))) 22 (should (= 2 (length matches))) 23 (should (cl-every (lambda (m) 24 (let-alist m 25 (and (stringp .text) 26 (cl-every 'pdf-test-relative-edges-p .edges) 27 (= 1 .page)))) 28 matches))))) 29 30 31 (ert-deftest pdf-info-search-regexp () 32 (pdf-test-with-test-pdf 33 (let (case-fold-search matches) 34 (should (setq matches (pdf-info-search-regexp "PDF Tools"))) 35 (should (= 2 (length matches))) 36 (should (cl-every (lambda (m) 37 (let-alist m 38 (and (stringp .text) 39 (cl-every 'pdf-test-relative-edges-p .edges) 40 (= 1 .page)))) 41 matches))))) 42 43 (ert-deftest pdf-info-pagelinks () 44 (pdf-test-with-test-pdf 45 (let ((links (pdf-info-pagelinks 3))) 46 (should (= 2 (length links))) 47 (should (cl-every 'pdf-test-relative-edges-p 48 (mapcar (apply-partially 'alist-get 'edges) 49 links))) 50 (should (equal (mapcar (apply-partially 'alist-get 'type) links) 51 '(goto-dest uri))) 52 (should (equal (mapcar (apply-partially 'alist-get 'uri) 53 links) 54 '(nil "http://www.gnu.org"))) 55 (should (equal (mapcar (apply-partially 'alist-get 'page) 56 links) 57 '(1 nil)))))) 58 59 (ert-deftest pdf-info-number-of-pages () 60 (pdf-test-with-test-pdf 61 (should (= 6 (pdf-info-number-of-pages))))) 62 63 (ert-deftest pdf-info-outline () 64 (pdf-test-with-test-pdf 65 (let ((outline (pdf-info-outline))) 66 (should (= 7 (length outline))) 67 (should (equal (mapcar (apply-partially 'alist-get 'depth) outline) 68 '(1 1 1 1 1 2 3))) 69 (should (cl-every (lambda (elt) 70 (eq (alist-get 'type elt) 71 'goto-dest)) 72 outline))))) 73 74 (ert-deftest pdf-info-gettext () 75 (pdf-test-with-test-pdf 76 (should (string-match "PDF Tools\\(?:.\\|\n\\)*in memory" 77 (pdf-info-gettext 1 '(0 0 1 1)))))) 78 79 (ert-deftest pdf-info-getselection () 80 (pdf-test-with-test-pdf 81 (should (consp (pdf-info-getselection 1 '(0 0 1 1)))) 82 (should (cl-every 'pdf-test-relative-edges-p 83 (pdf-info-getselection 1 '(0 0 1 1)))))) 84 85 (ert-deftest pdf-info-textregions () 86 (pdf-test-with-test-pdf 87 (should (consp (pdf-info-textregions 1))))) 88 89 (ert-deftest pdf-info-pagesize () 90 (pdf-test-with-test-pdf 91 (should (cl-every (lambda (size) 92 (and (consp size) 93 (natnump (car size)) 94 (natnump (cdr size)))) 95 (list (pdf-info-pagesize 1)))))) 96 97 (ert-deftest pdf-info-quit () 98 (pdf-test-with-test-pdf 99 (pdf-info-quit) 100 (let (pdf-info-restart-process-p) 101 (should-error (pdf-info-open))) 102 (let ((pdf-info-restart-process-p t)) 103 (pdf-info-open) 104 (should (pdf-info-close))))) 105 106 (ert-deftest pdf-info-getannots () 107 (skip-unless (pdf-info-writable-annotations-p)) 108 (pdf-test-with-test-pdf 109 (when (memq 'markup-annotations 110 (pdf-info-features)) 111 (cl-labels ((alists-get (alists key) 112 (mapcar (lambda (alist) 113 (cdr (assoc key alist))) 114 alists))) 115 (let (annots) 116 (should (= 5 (length (setq annots (cl-remove-if 117 (lambda (elt) 118 (memq (cdr (assq 'type elt)) 119 '(file link))) 120 (pdf-info-getannots)))))) 121 (should (equal (alists-get annots 'page) 122 '(2 2 2 2 2))) 123 (should (equal (sort (copy-sequence (alists-get annots 'type)) 'string-lessp) 124 (sort (copy-sequence 125 '(text strike-out highlight underline squiggly)) 126 'string-lessp))) 127 (should (equal (alists-get annots 'color) 128 '("#ff0000" "#ff0000" "#ff0000" 129 "#ff0000" "#ff0000"))) 130 (should (cl-every 'pdf-test-relative-edges-p 131 (alists-get annots 'edges)))))))) 132 133 (ert-deftest pdf-info-getannot () 134 (skip-unless (pdf-info-writable-annotations-p)) 135 (pdf-test-with-test-pdf 136 (let* ((text-annot (car 137 (cl-remove-if-not (lambda (elt) 138 (eq (cdr (assq 'type elt)) 'text)) 139 (pdf-info-getannots)))) 140 (key (cdr (assq 'id text-annot)))) 141 (should (consp text-annot)) 142 (should (symbolp key)) 143 (should key) 144 (should (equal (cl-sort text-annot 'string-lessp :key 'car) 145 (cl-sort (pdf-info-getannot key) 146 'string-lessp :key 'car)))))) 147 148 (ert-deftest pdf-info-addannot () 149 (skip-unless (pdf-info-writable-annotations-p)) 150 (pdf-test-with-test-pdf 151 (let (annot) 152 (should (consp (setq annot 153 (pdf-info-addannot 1 '(0 0 1 1) 'text)))) 154 (should (eq 1 (cdr (assq 'page annot)))) 155 (should (eq 'text (cdr (assq 'type annot)))) 156 (should (equal "" (cdr (assq 'contents annot))))))) 157 158 (ert-deftest pdf-info-delannot () 159 (skip-unless (pdf-info-writable-annotations-p)) 160 (pdf-test-with-test-pdf 161 (let ((nannots (length (pdf-info-getannots))) 162 annots) 163 (push (pdf-info-addannot 1 '(0 0 1 1) 'text) 164 annots) 165 (when (memq 'markup-annotations 166 (pdf-info-features)) 167 168 (push (pdf-info-addannot 1 '(0 0 1 1) 'squiggly nil nil '(0 0 1 1)) annots) 169 (push (pdf-info-addannot 1 '(0 0 1 1) 'highlight 'word nil '(0 0 1 1)) annots) 170 (push (pdf-info-addannot 1 '(0 0 1 1) 'underline 'line nil '(0 0 1 1)) annots) 171 (push (pdf-info-addannot 1 '(0 0 1 1) 'strike-out 'glyph nil '(0 0 1 1)) annots)) 172 (dolist (a annots) 173 (pdf-info-delannot (cdr (assq 'id a)))) 174 (should (= nannots (length (pdf-info-getannots))))))) 175 176 (ert-deftest pdf-info-mvannot () 177 (skip-unless (pdf-info-writable-annotations-p)) 178 (pdf-test-with-test-pdf 179 (let ((edges '(0.25 0.25 1.0 1.0)) 180 (id (cdr (assq 'id (car (pdf-info-getannots)))))) 181 (pdf-info-mvannot id edges) 182 (should (equal (cdr (assq 'edges (pdf-info-getannot id))) 183 edges))))) 184 185 (ert-deftest pdf-info-editannot () 186 (skip-unless (pdf-info-writable-annotations-p)) 187 (pdf-test-with-test-pdf 188 (let ((color "#ffa500") 189 (id (cdr (assq 'id (car (pdf-info-getannots)))))) 190 (should (and id (symbolp id))) 191 (pdf-info-editannot id `((color . ,color))) 192 (should (equal (cdr (assq 'color (pdf-info-getannot id))) 193 color))))) 194 195 (ert-deftest pdf-info-save () 196 (skip-unless (pdf-info-writable-annotations-p)) 197 (pdf-test-with-test-pdf 198 (dolist (id (mapcar (lambda (a) 199 (cdr (assq 'id a))) 200 (pdf-info-getannots))) 201 (pdf-info-delannot id)) 202 (let (tempfile) 203 (unwind-protect 204 (progn 205 (setq tempfile (pdf-info-save)) 206 (should (file-exists-p tempfile)) 207 (should (= 0 (length (pdf-info-getannots nil tempfile))))) 208 (when (file-exists-p tempfile) 209 (delete-file tempfile)))))) 210 211 (ert-deftest pdf-info-check-epdfinfo () 212 (should (progn (pdf-info-check-epdfinfo) t)) 213 (should-error 214 (let (pdf-info-epdfinfo-program) 215 (pdf-info-check-epdfinfo))) 216 (should-error 217 (let ((pdf-info-epdfinfo-program 42)) 218 (pdf-info-check-epdfinfo))) 219 (should-error 220 (let ((pdf-info-epdfinfo-program 221 (make-temp-name "pdf-info"))) 222 (pdf-info-check-epdfinfo)))) 223 224 ;; FIXME: Write me. 225 ;; (ert-deftest pdf-info-getattachment-from-annot () 226 ;; (pdf-test-with-test-pdf 227 ;; )) 228 229 ;; (ert-deftest pdf-info-getattachments () 230 ;; (pdf-test-with-test-pdf 231 ;; )) 232 233 ;; (ert-deftest pdf-info-synctex-forward-search () 234 ;; (pdf-test-with-test-pdf 235 ;; )) 236 237 ;; (ert-deftest pdf-info-synctex-backward-search () 238 ;; (pdf-test-with-test-pdf 239 ;; )) 240 241 ;; (ert-deftest pdf-info-renderpage () 242 ;; (pdf-test-with-test-pdf 243 ;; )) 244 245 ;; (ert-deftest pdf-info-renderpage-text-regions () 246 ;; (pdf-test-with-test-pdf 247 ;; )) 248 249 ;; (ert-deftest pdf-info-renderpage-highlight () 250 ;; (pdf-test-with-test-pdf 251 ;; )) 252 253 ;; (ert-deftest pdf-info-boundingbox () 254 ;; (pdf-test-with-test-pdf 255 ;; ))