Skip to content

Commit

Permalink
test: Add tests for org-hugo-get-md5
Browse files Browse the repository at this point in the history
  • Loading branch information
kaushalmodi committed Mar 15, 2022
1 parent 5e7cc6f commit 7b51ed9
Show file tree
Hide file tree
Showing 4 changed files with 123 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
7 changes: 7 additions & 0 deletions test/ert/all_tests.el
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,11 @@

;;; Code:

(setq load-prefer-newer t)

(require 'org-test-lib)

(require 'ox-hugo)

(require 't1)
(require 'tanchor)
67 changes: 67 additions & 0 deletions test/ert/org-test-lib.el
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
;;; org-test-lib.el --- Library of functions and macros needed for running the tests -*- lexical-binding: t; -*-

;; Authors: Kaushal Modi <[email protected]>
;; 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 <https://www.gnu.org/licenses/>.

;;; 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 \"<point>\" 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 "<point>" 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)
48 changes: 48 additions & 0 deletions test/ert/tanchor.el
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
;;; tanchor.el --- Tests related to anchor string derivation -*- lexical-binding: t; -*-

;; Authors: Kaushal Modi <[email protected]>
;; 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 <https://www.gnu.org/licenses/>.

;;; 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<point>"
(let ((el (org-element-at-point)))
(org-hugo-get-md5 el info)))))

;; Blank heading
(should
(string= "d41d8c"
(org-test-with-parsed-data "* <point>"
(let ((el (org-element-at-point)))
(org-hugo-get-md5 el info)))))

;; No heading
(should
(string= "d41d8c"
(org-test-with-parsed-data "<point>"
(let ((el (org-element-at-point)))
(org-hugo-get-md5 el info)))))
)


(provide 'tanchor)

0 comments on commit 7b51ed9

Please sign in to comment.