test-org-attach.el 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. ;;; test-org-attach.el --- tests for org-attach.el -*- lexical-binding: t; -*-
  2. ;; Copyright (C) 2017
  3. ;; Author: Marco Wahl
  4. ;; Keywords: internal
  5. ;; This program is free software; you can redistribute it and/or modify
  6. ;; it under the terms of the GNU General Public License as published by
  7. ;; the Free Software Foundation, either version 3 of the License, or
  8. ;; (at your option) any later version.
  9. ;; This program is distributed in the hope that it will be useful,
  10. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. ;; GNU General Public License for more details.
  13. ;; You should have received a copy of the GNU General Public License
  14. ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. ;;; Commentary:
  16. ;;
  17. ;;; Code:
  18. (require 'org-attach)
  19. (defun touch (filename)
  20. "Make sure FILENAME exists."
  21. (find-file filename)
  22. (save-buffer)
  23. (kill-buffer))
  24. (ert-deftest test-org-attach/dired-attach-to-next-best-subtree/1 ()
  25. "Attach file at point in dired to subtree."
  26. ;; prepare
  27. (let* ((tmpdir (make-temp-file "test-org-attach_" t "/"))
  28. (orgfilename (concat tmpdir "attach.org"))
  29. (a-filename (concat tmpdir "a")))
  30. (touch a-filename)
  31. (dired tmpdir)
  32. (delete-other-windows)
  33. (find-file-other-window orgfilename)
  34. (erase-buffer)
  35. (org-mode)
  36. (insert "* foo :foo:")
  37. (other-window 1)
  38. (assert (eq 'dired-mode major-mode))
  39. (dired-goto-file a-filename)
  40. ;;action
  41. (call-interactively #'org-attach-dired-attach-to-next-best-subtree)
  42. (find-file-other-window orgfilename)
  43. (beginning-of-buffer)
  44. (search-forward "* foo")
  45. ;; expectation. tag ATTACH has been appended.
  46. (should
  47. (reduce (lambda (x y) (or x y))
  48. (mapcar (lambda (x) (string-equal "ATTACH" x))
  49. (plist-get
  50. (plist-get
  51. (org-element-at-point) 'headline) :tags))))
  52. ;; cleanup
  53. (delete-directory tmpdir 'recursive)))
  54. ;; Use a test core several times.
  55. (defmacro standard-core-test-org-attach/dired-attach-function-for-method (fun)
  56. "Create test core for FUN. Attach two marked files."
  57. `(let* ((tmpdir (make-temp-file "test-org-attach_" t "/"))
  58. (orgfilename (concat tmpdir "attach.org"))
  59. (a-filename (concat tmpdir "a"))
  60. (b-filename (concat tmpdir "b")))
  61. (touch a-filename)
  62. (touch b-filename)
  63. (dired tmpdir)
  64. (delete-other-windows)
  65. (find-file-other-window orgfilename)
  66. (org-mode)
  67. (insert "* foo :foo:")
  68. (other-window 1)
  69. (assert (eq 'dired-mode major-mode))
  70. (dired-goto-file a-filename)
  71. (dired-mark 1)
  72. (dired-goto-file b-filename)
  73. (dired-mark 1)
  74. ;; action
  75. (call-interactively #',fun)
  76. (find-file-other-window orgfilename)
  77. (beginning-of-buffer)
  78. (search-forward "* foo")
  79. ;; check
  80. (should
  81. (and (file-exists-p (concat (org-attach-dir) "/" "a"))
  82. (file-exists-p (concat (org-attach-dir) "/" "b"))))
  83. ;; cleanup
  84. (delete-directory tmpdir 'recursive)))
  85. (ert-deftest test-org-attach/dired-attach-to-next-best-subtree/2 ()
  86. "Attach two marked."
  87. (standard-core-test-org-attach/dired-attach-function-for-method
  88. org-attach-dired-attach-to-next-best-subtree))
  89. (ert-deftest test-org-attach/dired-attach-to-next-best-subtree-cp ()
  90. (standard-core-test-org-attach/dired-attach-function-for-method
  91. org-attach-dired-attach-to-next-best-subtree-cp))
  92. (ert-deftest test-org-attach/dired-attach-to-next-best-subtree-mv ()
  93. (standard-core-test-org-attach/dired-attach-function-for-method
  94. org-attach-dired-attach-to-next-best-subtree-mv))
  95. (ert-deftest test-org-attach/dired-attach-to-next-best-subtree-ln ()
  96. (standard-core-test-org-attach/dired-attach-function-for-method
  97. org-attach-dired-attach-to-next-best-subtree-mv))
  98. (ert-deftest test-org-attach/dired-attach-to-next-best-subtree-lns ()
  99. (standard-core-test-org-attach/dired-attach-function-for-method
  100. org-attach-dired-attach-to-next-best-subtree-lns))
  101. (provide 'test-org-attach)
  102. ;;; test-org-attach.el ends here