diff options
| -rw-r--r-- | tests/test-gloss-core--find-buffer-position.el | 10 | ||||
| -rw-r--r-- | tests/test-gloss-core--first-call-creates-file.el | 27 | ||||
| -rw-r--r-- | tests/test-gloss-core--list.el | 10 | ||||
| -rw-r--r-- | tests/test-gloss-core--lookup.el | 12 | ||||
| -rw-r--r-- | tests/testutil-gloss.el | 18 |
5 files changed, 34 insertions, 43 deletions
diff --git a/tests/test-gloss-core--find-buffer-position.el b/tests/test-gloss-core--find-buffer-position.el index 1e63309..f159608 100644 --- a/tests/test-gloss-core--find-buffer-position.el +++ b/tests/test-gloss-core--find-buffer-position.el @@ -36,14 +36,8 @@ (ert-deftest test-gloss-core-find-buffer-position-missing-file-returns-nil () "Error: returns nil when the file does not exist." - (let ((gloss-file (concat temporary-file-directory "gloss-pos-nonexistent-" - (number-to-string (random 100000)) ".org"))) - (unwind-protect - (progn - (gloss-core--cache-reset) - (should-not (gloss-core-find-buffer-position "any"))) - (gloss-core--cache-reset) - (when (file-exists-p gloss-file) (delete-file gloss-file))))) + (gloss-test--with-missing-glossary + (should-not (gloss-core-find-buffer-position "any")))) (provide 'test-gloss-core--find-buffer-position) ;;; test-gloss-core--find-buffer-position.el ends here diff --git a/tests/test-gloss-core--first-call-creates-file.el b/tests/test-gloss-core--first-call-creates-file.el index e168435..22d313e 100644 --- a/tests/test-gloss-core--first-call-creates-file.el +++ b/tests/test-gloss-core--first-call-creates-file.el @@ -14,24 +14,15 @@ (ert-deftest test-gloss-core-save-creates-file-when-missing () "Normal: first save creates the file with a TITLE header." - (let ((gloss-file (concat temporary-file-directory "gloss-create-" - (number-to-string (random 100000)) ".org"))) - (unwind-protect - (progn - (gloss-core--cache-reset) - (should-not (file-exists-p gloss-file)) - (gloss-core-save "anaphora" "Reference earlier." 'manual) - (should (file-exists-p gloss-file)) - (let ((content (with-temp-buffer - (insert-file-contents gloss-file) - (buffer-string)))) - (should (string-match-p "#\\+TITLE:" content)) - (should (string-match-p "^\\* anaphora" content)))) - (gloss-core--cache-reset) - (when-let ((buf (find-buffer-visiting gloss-file))) - (with-current-buffer buf (set-buffer-modified-p nil)) - (kill-buffer buf)) - (when (file-exists-p gloss-file) (delete-file gloss-file))))) + (gloss-test--with-missing-glossary + (should-not (file-exists-p gloss-file)) + (gloss-core-save "anaphora" "Reference earlier." 'manual) + (should (file-exists-p gloss-file)) + (let ((content (with-temp-buffer + (insert-file-contents gloss-file) + (buffer-string)))) + (should (string-match-p "#\\+TITLE:" content)) + (should (string-match-p "^\\* anaphora" content))))) (ert-deftest test-gloss-core-save-creates-parent-directory () "Boundary: first save creates missing parent directory." diff --git a/tests/test-gloss-core--list.el b/tests/test-gloss-core--list.el index 22c988e..2e5a04d 100644 --- a/tests/test-gloss-core--list.el +++ b/tests/test-gloss-core--list.el @@ -23,14 +23,8 @@ (ert-deftest test-gloss-core-list-missing-file-returns-nil () "Error: list before any save returns nil (file does not exist)." - (let ((gloss-file (concat temporary-file-directory "gloss-list-nonexistent-" - (number-to-string (random 100000)) ".org"))) - (unwind-protect - (progn - (gloss-core--cache-reset) - (should-not (gloss-core-list))) - (gloss-core--cache-reset) - (when (file-exists-p gloss-file) (delete-file gloss-file))))) + (gloss-test--with-missing-glossary + (should-not (gloss-core-list)))) (provide 'test-gloss-core--list) ;;; test-gloss-core--list.el ends here diff --git a/tests/test-gloss-core--lookup.el b/tests/test-gloss-core--lookup.el index a1986a7..0e044ca 100644 --- a/tests/test-gloss-core--lookup.el +++ b/tests/test-gloss-core--lookup.el @@ -59,15 +59,9 @@ (ert-deftest test-gloss-core-lookup-missing-file-returns-nil () "Error: lookup before any save returns nil (file does not exist)." - (let ((gloss-file (concat temporary-file-directory "gloss-nonexistent-" - (number-to-string (random 100000)) ".org"))) - (unwind-protect - (progn - (gloss-core--cache-reset) - (should-not (file-exists-p gloss-file)) - (should-not (gloss-core-lookup "anything"))) - (gloss-core--cache-reset) - (when (file-exists-p gloss-file) (delete-file gloss-file))))) + (gloss-test--with-missing-glossary + (should-not (file-exists-p gloss-file)) + (should-not (gloss-core-lookup "anything")))) (provide 'test-gloss-core--lookup) ;;; test-gloss-core--lookup.el ends here diff --git a/tests/testutil-gloss.el b/tests/testutil-gloss.el index b70b0ea..7510765 100644 --- a/tests/testutil-gloss.el +++ b/tests/testutil-gloss.el @@ -58,5 +58,23 @@ visiting buffer." (when (file-exists-p gloss-file) (delete-file gloss-file))))) +(defmacro gloss-test--with-missing-glossary (&rest body) + "Bind `gloss-file' to a path that does not yet exist; run BODY. +Reset the in-memory cache before BODY and after. Clean up the file and +any visiting buffer if BODY created them." + (declare (indent 0) (debug t)) + `(let ((gloss-file (concat temporary-file-directory "gloss-missing-" + (number-to-string (random 100000)) ".org"))) + (unwind-protect + (progn + (gloss-core--cache-reset) + ,@body) + (gloss-core--cache-reset) + (when-let ((buf (find-buffer-visiting gloss-file))) + (with-current-buffer buf (set-buffer-modified-p nil)) + (kill-buffer buf)) + (when (file-exists-p gloss-file) + (delete-file gloss-file))))) + (provide 'testutil-gloss) ;;; testutil-gloss.el ends here |
