diff options
| author | Craig Jennings <c@cjennings.net> | 2026-04-30 00:28:29 -0500 |
|---|---|---|
| committer | Craig Jennings <c@cjennings.net> | 2026-04-30 00:28:29 -0500 |
| commit | e7938e9193ba1a39aab0e614bb3bf682508685b2 (patch) | |
| tree | b1b64f14c5d5de5defb1a8f418ed6fe08b57ecd9 /tests | |
| parent | 29c21851bddf76c5fd659d4225bb426fc1396750 (diff) | |
| download | gloss-e7938e9193ba1a39aab0e614bb3bf682508685b2.tar.gz gloss-e7938e9193ba1a39aab0e614bb3bf682508685b2.zip | |
test: add gloss-drill test suite (red phase)
Four test files plus a small testutil for toggling the `org-drill'
feature flag. All 10 tests fail at this commit because the implementation
is still a stub.
The suite covers Normal (untagged entries get the tag and property),
Boundary (empty file, idempotency, untag never-tagged), and Error
(org-drill not installed). The error path also asserts the file is left
untouched. Untag-all is tested under both feature states because the
user might want to remove tags after uninstalling org-drill.
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/test-gloss-drill--export-all-no-orgdrill-installed.el | 38 | ||||
| -rw-r--r-- | tests/test-gloss-drill--export-all-skips-already-tagged.el | 60 | ||||
| -rw-r--r-- | tests/test-gloss-drill--export-all-tags-untagged.el | 66 | ||||
| -rw-r--r-- | tests/test-gloss-drill--untag-all.el | 56 | ||||
| -rw-r--r-- | tests/testutil-gloss-drill.el | 40 |
5 files changed, 260 insertions, 0 deletions
diff --git a/tests/test-gloss-drill--export-all-no-orgdrill-installed.el b/tests/test-gloss-drill--export-all-no-orgdrill-installed.el new file mode 100644 index 0000000..f8f4967 --- /dev/null +++ b/tests/test-gloss-drill--export-all-no-orgdrill-installed.el @@ -0,0 +1,38 @@ +;;; test-gloss-drill--export-all-no-orgdrill-installed.el --- Error tests for missing org-drill -*- lexical-binding: t -*- + +;; SPDX-License-Identifier: GPL-3.0-or-later + +;;; Commentary: +;; Tests for `gloss-drill-export-all' covering the Error case where +;; `org-drill' is not installed. The function must raise `user-error' +;; with an install hint and must not touch `gloss-file'. + +;;; Code: + +(require 'ert) +(require 'gloss-drill) +(require 'testutil-gloss) +(require 'testutil-gloss-drill) + +(ert-deftest test-gloss-drill-export-all-without-org-drill-raises-user-error () + "Error: missing `org-drill' raises `user-error' with an install hint." + (gloss-test--with-temp-glossary gloss-test--sample-content + (gloss-test--without-org-drill-feature + (let ((err (should-error (gloss-drill-export-all) :type 'user-error))) + (should (string-match-p "org-drill" (error-message-string err))))))) + +(ert-deftest test-gloss-drill-export-all-without-org-drill-leaves-file-untouched () + "Error: missing `org-drill' must not modify the glossary file." + (gloss-test--with-temp-glossary gloss-test--sample-content + (gloss-test--without-org-drill-feature + (let ((before (with-temp-buffer + (insert-file-contents gloss-file) + (buffer-string)))) + (ignore-errors (gloss-drill-export-all)) + (let ((after (with-temp-buffer + (insert-file-contents gloss-file) + (buffer-string)))) + (should (equal before after))))))) + +(provide 'test-gloss-drill--export-all-no-orgdrill-installed) +;;; test-gloss-drill--export-all-no-orgdrill-installed.el ends here diff --git a/tests/test-gloss-drill--export-all-skips-already-tagged.el b/tests/test-gloss-drill--export-all-skips-already-tagged.el new file mode 100644 index 0000000..52978c6 --- /dev/null +++ b/tests/test-gloss-drill--export-all-skips-already-tagged.el @@ -0,0 +1,60 @@ +;;; test-gloss-drill--export-all-skips-already-tagged.el --- Idempotency tests for gloss-drill-export-all -*- lexical-binding: t -*- + +;; SPDX-License-Identifier: GPL-3.0-or-later + +;;; Commentary: +;; Tests for `gloss-drill-export-all' covering the Boundary case of +;; running export twice in a row. Running it on an already-tagged +;; entry must not duplicate the :drill: tag, and the +;; :DRILL_CARD_TYPE: twosided property must remain a single property +;; with the same value. + +;;; Code: + +(require 'ert) +(require 'gloss-drill) +(require 'testutil-gloss) +(require 'testutil-gloss-drill) + +(defun gloss-test--drill-tag-count-on-first-entry () + "Return how many times \"drill\" appears in the first entry's tag list. +Reads the file fresh from disk." + (with-current-buffer (find-file-noselect gloss-file) + (revert-buffer t t t) + (let ((count 0)) + (org-map-entries + (lambda () + (when (= 1 (org-current-level)) + (setq count (length (cl-remove-if-not + (lambda (tag) (equal tag "drill")) + (org-get-tags nil t)))) + (throw 'done nil)))) + count))) + +(ert-deftest test-gloss-drill-export-all-idempotent-tag-not-duplicated () + "Boundary: running export-all twice does not duplicate the :drill: tag." + (gloss-test--with-temp-glossary gloss-test--sample-content + (gloss-test--with-org-drill-feature + (gloss-drill-export-all) + (gloss-drill-export-all) + (catch 'done + (should (= (gloss-test--drill-tag-count-on-first-entry) 1)))))) + +(ert-deftest test-gloss-drill-export-all-idempotent-property-unchanged () + "Boundary: running export-all twice keeps :DRILL_CARD_TYPE: twosided." + (gloss-test--with-temp-glossary gloss-test--sample-content + (gloss-test--with-org-drill-feature + (gloss-drill-export-all) + (gloss-drill-export-all) + (with-current-buffer (find-file-noselect gloss-file) + (revert-buffer t t t) + (catch 'done + (org-map-entries + (lambda () + (when (= 1 (org-current-level)) + (should (equal (org-entry-get nil "DRILL_CARD_TYPE") + "twosided")) + (throw 'done nil))))))))) + +(provide 'test-gloss-drill--export-all-skips-already-tagged) +;;; test-gloss-drill--export-all-skips-already-tagged.el ends here diff --git a/tests/test-gloss-drill--export-all-tags-untagged.el b/tests/test-gloss-drill--export-all-tags-untagged.el new file mode 100644 index 0000000..bf8cf64 --- /dev/null +++ b/tests/test-gloss-drill--export-all-tags-untagged.el @@ -0,0 +1,66 @@ +;;; test-gloss-drill--export-all-tags-untagged.el --- Tests for gloss-drill-export-all on untagged entries -*- lexical-binding: t -*- + +;; SPDX-License-Identifier: GPL-3.0-or-later + +;;; Commentary: +;; Tests for `gloss-drill-export-all' covering the Normal happy-path +;; (untagged entries gain the tag and property) and the Boundary case of +;; an empty glossary file. The entry walking uses real `org-element' / +;; `org-map-entries'; only the `org-drill' feature flag is mocked. + +;;; Code: + +(require 'ert) +(require 'gloss-drill) +(require 'testutil-gloss) +(require 'testutil-gloss-drill) + +(defun gloss-test--drill-tagged-count () + "Return the number of top-level entries in `gloss-file' tagged :drill:. +Reads the file fresh from disk." + (with-current-buffer (find-file-noselect gloss-file) + (revert-buffer t t t) + (let ((count 0)) + (org-map-entries + (lambda () + (when (and (= 1 (org-current-level)) + (member "drill" (org-get-tags nil t))) + (setq count (1+ count))))) + count))) + +(defun gloss-test--drill-card-type-count () + "Return the number of top-level entries with :DRILL_CARD_TYPE: twosided'. +Reads the file fresh from disk." + (with-current-buffer (find-file-noselect gloss-file) + (revert-buffer t t t) + (let ((count 0)) + (org-map-entries + (lambda () + (when (and (= 1 (org-current-level)) + (equal (org-entry-get nil "DRILL_CARD_TYPE") "twosided")) + (setq count (1+ count))))) + count))) + +(ert-deftest test-gloss-drill-export-all-adds-drill-tag-to-every-entry () + "Normal: every untagged entry gains :drill: after export-all." + (gloss-test--with-temp-glossary gloss-test--sample-content + (gloss-test--with-org-drill-feature + (gloss-drill-export-all) + (should (= (gloss-test--drill-tagged-count) 2))))) + +(ert-deftest test-gloss-drill-export-all-sets-drill-card-type-property () + "Normal: every entry gains :DRILL_CARD_TYPE: twosided after export-all." + (gloss-test--with-temp-glossary gloss-test--sample-content + (gloss-test--with-org-drill-feature + (gloss-drill-export-all) + (should (= (gloss-test--drill-card-type-count) 2))))) + +(ert-deftest test-gloss-drill-export-all-empty-file-no-op () + "Boundary: export-all on an empty glossary file is a no-op, no error." + (gloss-test--with-temp-glossary "#+TITLE: Glossary\n#+STARTUP: showall\n" + (gloss-test--with-org-drill-feature + (gloss-drill-export-all) + (should (= (gloss-test--drill-tagged-count) 0))))) + +(provide 'test-gloss-drill--export-all-tags-untagged) +;;; test-gloss-drill--export-all-tags-untagged.el ends here diff --git a/tests/test-gloss-drill--untag-all.el b/tests/test-gloss-drill--untag-all.el new file mode 100644 index 0000000..7fe9986 --- /dev/null +++ b/tests/test-gloss-drill--untag-all.el @@ -0,0 +1,56 @@ +;;; test-gloss-drill--untag-all.el --- Tests for gloss-drill-untag-all -*- lexical-binding: t -*- + +;; SPDX-License-Identifier: GPL-3.0-or-later + +;;; Commentary: +;; Tests for `gloss-drill-untag-all' covering Normal (export then untag), +;; Boundary (untag when nothing was tagged), and the no-org-drill-needed +;; case (untag must work without `org-drill' installed, since the user +;; might want to remove tags after uninstalling). + +;;; Code: + +(require 'ert) +(require 'gloss-drill) +(require 'testutil-gloss) +(require 'testutil-gloss-drill) + +(defun gloss-test--no-drill-tags-or-properties-p () + "Return non-nil when no top-level entry carries :drill: or DRILL_CARD_TYPE. +Reads the file fresh from disk." + (with-current-buffer (find-file-noselect gloss-file) + (revert-buffer t t t) + (let ((dirty nil)) + (org-map-entries + (lambda () + (when (= 1 (org-current-level)) + (when (or (member "drill" (org-get-tags nil t)) + (org-entry-get nil "DRILL_CARD_TYPE")) + (setq dirty t))))) + (not dirty)))) + +(ert-deftest test-gloss-drill-untag-all-removes-tag-and-property () + "Normal: untag-all removes :drill: and :DRILL_CARD_TYPE: from every entry." + (gloss-test--with-temp-glossary gloss-test--sample-content + (gloss-test--with-org-drill-feature + (gloss-drill-export-all) + (gloss-drill-untag-all) + (should (gloss-test--no-drill-tags-or-properties-p))))) + +(ert-deftest test-gloss-drill-untag-all-no-op-when-nothing-tagged () + "Boundary: untag-all on never-tagged entries is a no-op, no error." + (gloss-test--with-temp-glossary gloss-test--sample-content + (gloss-drill-untag-all) + (should (gloss-test--no-drill-tags-or-properties-p)))) + +(ert-deftest test-gloss-drill-untag-all-works-without-org-drill-installed () + "Boundary: untag-all does NOT require org-drill to be installed." + (gloss-test--with-temp-glossary gloss-test--sample-content + (gloss-test--with-org-drill-feature + (gloss-drill-export-all)) + (gloss-test--without-org-drill-feature + (gloss-drill-untag-all) + (should (gloss-test--no-drill-tags-or-properties-p))))) + +(provide 'test-gloss-drill--untag-all) +;;; test-gloss-drill--untag-all.el ends here diff --git a/tests/testutil-gloss-drill.el b/tests/testutil-gloss-drill.el new file mode 100644 index 0000000..721922c --- /dev/null +++ b/tests/testutil-gloss-drill.el @@ -0,0 +1,40 @@ +;;; testutil-gloss-drill.el --- Shared test fixtures for gloss-drill -*- lexical-binding: t -*- + +;; SPDX-License-Identifier: GPL-3.0-or-later + +;;; Commentary: + +;; Fixtures used across `gloss-drill' test files. Provides: +;; - `gloss-test--with-org-drill-feature' macro to register `org-drill' +;; as a feature for the duration of BODY (without actually loading it). + +;;; Code: + +(defmacro gloss-test--with-org-drill-feature (&rest body) + "Run BODY with `org-drill' provided as a feature. +Restores the prior feature state on exit so tests can both assert +\"org-drill present\" and \"org-drill absent\" behaviour without leaking +state across the suite." + (declare (indent 0) (debug t)) + `(let ((had-org-drill (featurep 'org-drill))) + (unwind-protect + (progn + (provide 'org-drill) + ,@body) + (unless had-org-drill + (setq features (delq 'org-drill features)))))) + +(defmacro gloss-test--without-org-drill-feature (&rest body) + "Run BODY with `org-drill' un-registered as a feature. +Restores the prior feature state on exit." + (declare (indent 0) (debug t)) + `(let ((had-org-drill (featurep 'org-drill))) + (unwind-protect + (progn + (setq features (delq 'org-drill features)) + ,@body) + (when had-org-drill + (provide 'org-drill))))) + +(provide 'testutil-gloss-drill) +;;; testutil-gloss-drill.el ends here |
