summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndreas Politz <politza@hochschule-trier.de>2015-02-02 09:03:03 +0100
committerAndreas Politz <politza@hochschule-trier.de>2015-02-02 09:03:03 +0100
commitc6d2150a0e7c0695aa6290259402b4c15b7a403d (patch)
treec3dafc4ba0a9d5bb38a36d1b8653d4939c1d2bb1
parent855078c284d71041cafdf770b612fa108db82fe6 (diff)
Added more tests.
* test/pdf-info.ert (pdf-info):
-rw-r--r--test/pdf-info.ert252
1 files changed, 243 insertions, 9 deletions
diff --git a/test/pdf-info.ert b/test/pdf-info.ert
index 3f907cb..8aac181 100644
--- a/test/pdf-info.ert
+++ b/test/pdf-info.ert
@@ -1,12 +1,246 @@
+(require 'pdf-info)
-;; This is a very basic test, which merely checks the basic
-;; functionality of the epdfinfo program.
+(defmacro pdf-test-with-test-pdf (&rest body)
+ (declare (indent 0) (debug t))
+ (let ((buffer (make-symbol "buffer")))
+ `(let ((,buffer (find-file-noselect
+ (expand-file-name "test.pdf"))))
+ (pdf-info-process-assert-running t)
+ (unwind-protect
+ (with-current-buffer ,buffer ,@body)
+ (when (buffer-live-p ,buffer)
+ (set-buffer-modified-p nil)
+ (kill-buffer))
+ (pdf-info-quit)))))
-(require 'pdf-info)
+(defun pdf-test-relative-edges-p (edges)
+ (and (consp edges)
+ (cl-every (lambda (x)
+ (and (numberp x)
+ (<= x 1)
+ (>= x 0)))
+ edges)))
+
+(ert-deftest pdf-info-open/close ()
+ (pdf-test-with-test-pdf
+ (should-not (pdf-info-open))
+ (should (pdf-info-close))))
+
+(ert-deftest pdf-info-metadata ()
+ (pdf-test-with-test-pdf
+ (should (cl-every (lambda (elt)
+ (and (consp elt)
+ (symbolp (car elt))))
+ (pdf-info-metadata)))))
+
+(ert-deftest pdf-info-search-string ()
+ (pdf-test-with-test-pdf
+ (let (matches)
+ (should (setq matches (pdf-info-search-string "PDF Tools")))
+ (should (= 1 (length matches)))
+ (should (= 2 (length (cdar matches))))
+ (should (cl-every 'stringp
+ (mapcar 'car (cdar matches))))
+ (should (cl-every 'pdf-test-relative-edges-p
+ (mapcar 'cadr (cdar matches)))))))
+
+
+(ert-deftest pdf-info-search-regexp ()
+ (pdf-test-with-test-pdf
+ (let (case-fold-search matches)
+ (should (setq matches (pdf-info-search-regexp "PDF Tools")))
+ (should (= 1 (length matches)))
+ (should (= 2 (length (cdar matches))))
+ (should (cl-every 'stringp
+ (mapcar 'car (cdar matches))))
+ (should (cl-every 'pdf-test-relative-edges-p
+ (mapcar 'cadr (cdar matches)))))))
+
+(ert-deftest pdf-info-pagelinks ()
+ (pdf-test-with-test-pdf
+ (let (links)
+ (should (= 2 (length (setq links (pdf-info-pagelinks 3)))))
+ (should (cl-every 'pdf-test-relative-edges-p
+ (mapcar 'car links)))
+ (setq links (mapcar 'cdr links))
+ (should (equal (mapcar 'car links)
+ '(goto-dest uri)))
+ (should (equal (mapcar 'caddr links)
+ '(1 "http://www.gnu.org"))))))
+
+(ert-deftest pdf-info-number-of-pages ()
+ (pdf-test-with-test-pdf
+ (should (= 5 (pdf-info-number-of-pages)))))
+
+(ert-deftest pdf-info-outline ()
+ (pdf-test-with-test-pdf
+ (let (outline)
+ (should (= 7 (length (setq outline (pdf-info-outline)))))
+ (should (equal (mapcar 'car outline)
+ '(1 1 1 1 1 2 3)))
+ (should (cl-every (lambda (type)
+ (equal type 'goto-dest))
+ (mapcar 'cadr outline))))))
+
+(ert-deftest pdf-info-gettext ()
+ (pdf-test-with-test-pdf
+ (should (string-match "PDF Tools\\(?:.\\|\n\\)*in memory"
+ (pdf-info-gettext 1 '(0 0 1 1))))))
+
+(ert-deftest pdf-info-getselection ()
+ (pdf-test-with-test-pdf
+ (should (consp (pdf-info-getselection 1 '(0 0 1 1))))
+ (should (cl-every 'pdf-test-relative-edges-p
+ (pdf-info-getselection 1 '(0 0 1 1))))))
+
+(ert-deftest pdf-info-textlayout ()
+ (pdf-test-with-test-pdf
+ (should (consp (pdf-info-textlayout 1)))))
+
+(ert-deftest pdf-info-pagesize ()
+ (pdf-test-with-test-pdf
+ (should (cl-every (lambda (size)
+ (and (consp size)
+ (natnump (car size))
+ (natnump (cdr size))))
+ (list (pdf-info-pagesize 1))))))
+
+(ert-deftest pdf-info-quit ()
+ (pdf-test-with-test-pdf
+ (pdf-info-quit)
+ (let (pdf-info-restart-process-p)
+ (should-error (pdf-info-open)))
+ (let ((pdf-info-restart-process-p t))
+ (pdf-info-open)
+ (should (pdf-info-close)))))
+
+(ert-deftest pdf-info-getannots ()
+ (pdf-test-with-test-pdf
+ (when (memq 'markup-annotations
+ (pdf-info-features))
+ (cl-labels ((alists-get (alists key)
+ (mapcar (lambda (alist)
+ (cdr (assoc key alist)))
+ alists)))
+ (let (annots)
+ (should (= 5 (length (setq annots (cl-remove-if
+ (lambda (elt)
+ (memq (cdr (assq 'type elt))
+ '(file link)))
+ (pdf-info-getannots))))))
+ (should (equal (alists-get annots 'page)
+ '(2 2 2 2 2)))
+ (should (equal (sort (copy-sequence (alists-get annots 'type)) 'string-lessp)
+ (sort (copy-sequence
+ '(text strike-out highlight underline squiggly))
+ 'string-lessp)))
+ (should (equal (alists-get annots 'color)
+ '("#ff0000" "#ff0000" "#ff0000"
+ "#ff0000" "#ff0000")))
+ (should (cl-every 'pdf-test-relative-edges-p
+ (alists-get annots 'edges))))))))
+
+(ert-deftest pdf-info-getannot ()
+ (pdf-test-with-test-pdf
+ (let* ((text-annot (car
+ (cl-remove-if-not (lambda (elt)
+ (eq (cdr (assq 'type elt)) 'text))
+ (pdf-info-getannots))))
+ (key (cdr (assq 'id text-annot))))
+ (should (consp text-annot))
+ (should (symbolp key))
+ (should key)
+ (should (equal (cl-sort text-annot 'string-lessp :key 'car)
+ (cl-sort (pdf-info-getannot key)
+ 'string-lessp :key 'car))))))
+
+(ert-deftest pdf-info-addannot ()
+ (pdf-test-with-test-pdf
+ (let (annot)
+ (should (consp (setq annot
+ (pdf-info-addannot 1 '(0 0 1 1) 'text))))
+ (should (eq 1 (cdr (assq 'page annot))))
+ (should (eq 'text (cdr (assq 'type annot))))
+ (should (equal "" (cdr (assq 'contents annot)))))))
+
+(ert-deftest pdf-info-delannot ()
+ (pdf-test-with-test-pdf
+ (let ((nannots (length (pdf-info-getannots)))
+ annots)
+ (push (pdf-info-addannot 1 '(0 0 1 1) 'text)
+ annots)
+ (when (memq 'markup-annotations
+ (pdf-info-features))
+ (push (pdf-info-addannot 1 '(0 0 1 1) 'squiggly '(0 0 1 1)) annots)
+ (push (pdf-info-addannot 1 '(0 0 1 1) 'highlight '(0 0 1 1)) annots)
+ (push (pdf-info-addannot 1 '(0 0 1 1) 'underline '(0 0 1 1)) annots)
+ (push (pdf-info-addannot 1 '(0 0 1 1) 'strike-out '(0 0 1 1)) annots))
+ (dolist (a annots)
+ (pdf-info-delannot (cdr (assq 'id a))))
+ (should (= nannots (length (pdf-info-getannots)))))))
+
+(ert-deftest pdf-info-mvannot ()
+ (pdf-test-with-test-pdf
+ (let ((edges '(0.25 0.25 1.0 1.0))
+ (id (cdr (assq 'id (car (pdf-info-getannots))))))
+ (pdf-info-mvannot id edges)
+ (should (equal (cdr (assq 'edges (pdf-info-getannot id)))
+ edges)))))
+
+(ert-deftest pdf-info-editannot ()
+ (pdf-test-with-test-pdf
+ (let ((color "#ffa500")
+ (id (cdr (assq 'id (car (pdf-info-getannots))))))
+ (should (and id (symbolp id)))
+ (pdf-info-editannot id `((color . ,color)))
+ (should (equal (cdr (assq 'color (pdf-info-getannot id)))
+ color)))))
+
+(ert-deftest pdf-info-save ()
+ (pdf-test-with-test-pdf
+ (dolist (id (mapcar (lambda (a)
+ (cdr (assq 'id a)))
+ (pdf-info-getannots)))
+ (pdf-info-delannot id))
+ (let (tempfile)
+ (unwind-protect
+ (progn
+ (setq tempfile (pdf-info-save))
+ (should (file-exists-p tempfile))
+ (should (= 0 (length (pdf-info-getannots nil tempfile)))))
+ (when (file-exists-p tempfile)
+ (delete-file tempfile))))))
+
+;; FIXME: Write me.
+;; (ert-deftest pdf-info-getattachment-from-annot ()
+;; (pdf-test-with-test-pdf
+;; ))
+
+;; (ert-deftest pdf-info-getattachments ()
+;; (pdf-test-with-test-pdf
+;; ))
+
+;; (ert-deftest pdf-info-synctex-forward-search ()
+;; (pdf-test-with-test-pdf
+;; ))
+
+;; (ert-deftest pdf-info-synctex-backward-search ()
+;; (pdf-test-with-test-pdf
+;; ))
+
+;; (ert-deftest pdf-info-renderpage ()
+;; (pdf-test-with-test-pdf
+;; ))
+
+;; (ert-deftest pdf-info-renderpage-text-regions ()
+;; (pdf-test-with-test-pdf
+;; ))
+
+;; (ert-deftest pdf-info-renderpage-highlight ()
+;; (pdf-test-with-test-pdf
+;; ))
+
+;; (ert-deftest pdf-info-boundingbox ()
+;; (pdf-test-with-test-pdf
+;; ))
-(ert-deftest pdf-info ()
- (let* ((pdf (expand-file-name "test.pdf")))
- (should-not (pdf-info-open pdf))
- (should (= (pdf-info-number-of-pages pdf) 3))
- (should (= (length (pdf-info-search-string "Hello" nil pdf)) 3))
- (should (pdf-info-close pdf))))