diff --git a/Makefile b/Makefile index 50263496..33453ffc 100644 --- a/Makefile +++ b/Makefile @@ -191,7 +191,7 @@ diff: @git diff ert: - $(EMACS) --batch -l ert -L $(OX_HUGO_TEST_DIR)/ert/ -l all_tests.el -f ert-run-tests-batch-and-exit + $(EMACS) --batch -l ert -L . -L $(OX_HUGO_TEST_DIR)/ert/ -l all_tests.el -f ert-run-tests-batch-and-exit test: vcheck_emacs vcheck_pandoc ert testmkgold do_test diff --git a/test/ert/all_tests.el b/test/ert/all_tests.el index e7494ec3..c7a3e789 100644 --- a/test/ert/all_tests.el +++ b/test/ert/all_tests.el @@ -20,4 +20,11 @@ ;;; Code: +(setq load-prefer-newer t) + +(require 'org-test-lib) + +(require 'ox-hugo) + (require 't1) +(require 'tanchor) diff --git a/test/ert/org-test-lib.el b/test/ert/org-test-lib.el new file mode 100644 index 00000000..2753647b --- /dev/null +++ b/test/ert/org-test-lib.el @@ -0,0 +1,67 @@ +;;; org-test-lib.el --- Library of functions and macros needed for running the tests -*- lexical-binding: t; -*- + +;; Authors: Kaushal Modi +;; URL: https://ox-hugo.scripter.co + +;; This file is not part of GNU Emacs. + +;; This program is free software; you can redistribute it and/or modify +;; it under the terms of the GNU General Public License as published by +;; the Free Software Foundation, either version 3 of the License, or +;; (at your option) any later version. + +;; This program is distributed in the hope that it will be useful, +;; but WITHOUT ANY WARRANTY; without even the implied warranty of +;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +;; GNU General Public License for more details. + +;; You should have received a copy of the GNU General Public License +;; along with this program. If not, see . + +;;; Code: + +(require 'org) + +;; Source: https://git.savannah.gnu.org/cgit/emacs/org-mode.git/tree/testing/org-test.el +(defmacro org-test-with-temp-text (text &rest body) + "Run body in a temporary buffer with Org mode as the active +mode holding TEXT. If the string \"\" appears in TEXT +then remove it and place the point there before running BODY, +otherwise place the point at the beginning of the inserted text." + (declare (indent 1)) + `(let ((inside-text (if (stringp ,text) ,text (eval ,text))) + (org-mode-hook nil)) + (with-temp-buffer + (org-mode) + (let ((point (string-match "" inside-text))) + (if point + (progn + (insert (replace-match "" nil nil inside-text)) + (goto-char (1+ (match-beginning 0)))) + (insert inside-text) + (goto-char (point-min)))) + (font-lock-ensure (point-min) (point-max)) + ,@body))) +(def-edebug-spec org-test-with-temp-text (form body)) + +;; Source: https://git.savannah.gnu.org/cgit/emacs/org-mode.git/tree/testing/lisp/test-ox.el +(defmacro org-test-with-parsed-data (data &rest body) + "Execute body with parsed data available. +DATA is a string containing the data to be parsed. BODY is the +body to execute. Parse tree is available under the `tree' +variable, and communication channel under `info'." + (declare (debug (form body)) (indent 1)) + `(org-test-with-temp-text ,data + (org-export--delete-comment-trees) + (let* ((tree (org-element-parse-buffer)) + (info (org-combine-plists + (org-export--get-export-attributes) + (org-export-get-environment)))) + (org-export--prune-tree tree info) + (org-export--remove-uninterpreted-data tree info) + (let ((info (org-combine-plists + info (org-export--collect-tree-properties tree info)))) + ,@body)))) + + +(provide 'org-test-lib) diff --git a/test/ert/tanchor.el b/test/ert/tanchor.el new file mode 100644 index 00000000..e19a95d7 --- /dev/null +++ b/test/ert/tanchor.el @@ -0,0 +1,48 @@ +;;; tanchor.el --- Tests related to anchor string derivation -*- lexical-binding: t; -*- + +;; Authors: Kaushal Modi +;; URL: https://ox-hugo.scripter.co + +;; This file is not part of GNU Emacs. + +;; This program is free software; you can redistribute it and/or modify +;; it under the terms of the GNU General Public License as published by +;; the Free Software Foundation, either version 3 of the License, or +;; (at your option) any later version. + +;; This program is distributed in the hope that it will be useful, +;; but WITHOUT ANY WARRANTY; without even the implied warranty of +;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +;; GNU General Public License for more details. + +;; You should have received a copy of the GNU General Public License +;; along with this program. If not, see . + +;;; Code: + +(ert-deftest test-anchor/org-hugo-get-md5 () + "Test md5 based anchor string generation." + ;; md5 of "abc" heading + (should + (string= "900150" + (org-test-with-parsed-data "* abc" + (let ((el (org-element-at-point))) + (org-hugo-get-md5 el info))))) + + ;; Blank heading + (should + (string= "d41d8c" + (org-test-with-parsed-data "* " + (let ((el (org-element-at-point))) + (org-hugo-get-md5 el info))))) + + ;; No heading + (should + (string= "d41d8c" + (org-test-with-parsed-data "" + (let ((el (org-element-at-point))) + (org-hugo-get-md5 el info))))) + ) + + +(provide 'tanchor)