test-org-protocol.el 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. ;;; test-org-protocol.el --- tests for org-protocol.el -*- lexical-binding: t; -*-
  2. ;; Copyright (c) Sacha Chua
  3. ;; Authors: Sacha Chua
  4. ;; This file is not part of GNU Emacs.
  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. ;;; Code:
  16. (require 'cl-lib)
  17. (unless (featurep 'org-protocol)
  18. (signal 'missing-test-dependency "Support for org-protocol"))
  19. (ert-deftest test-org-protocol/org-protocol-parse-parameters ()
  20. "Test `org-protocol-parse-parameters' specifications."
  21. ;; Ignore lists
  22. (let ((data (org-protocol-parse-parameters '(:url "abc" :title "def") nil)))
  23. (should (string= (plist-get data :url) "abc"))
  24. (should (string= (plist-get data :title) "def")))
  25. ;; Parse new-style links
  26. (let ((data (org-protocol-parse-parameters "url=abc&title=def" t)))
  27. (should (string= (plist-get data :url) "abc"))
  28. (should (string= (plist-get data :title) "def")))
  29. ;; Parse old-style links
  30. (let ((data (org-protocol-parse-parameters "abc/def" nil '(:url :title))))
  31. (should (string= (plist-get data :url) "abc"))
  32. (should (string= (plist-get data :title) "def")))
  33. ;; Parse old-style links even without keys
  34. (let ((data (org-protocol-parse-parameters "b/abc/def" nil)))
  35. (should (equal data '("b" "abc" "def"))))
  36. ;; Parse old-style links with key/val pairs
  37. (let ((data (org-protocol-parse-parameters "b/abc/extrakey/extraval" nil '(:param1 :param2))))
  38. (should (string= (plist-get data :param1) "b"))
  39. (should (string= (plist-get data :param2) "abc"))
  40. (should (string= (plist-get data :extrakey) "extraval"))))
  41. (ert-deftest test-org-protocol/org-protocol-store-link ()
  42. "Test `org-protocol-store-link' specifications."
  43. ;; Old link style
  44. (let ((uri "/some/directory/org-protocol:/store-link:/URL/TITLE"))
  45. (should (null (org-protocol-check-filename-for-protocol uri (list uri) nil)))
  46. (should (equal (car org-stored-links) '("URL" "TITLE"))))
  47. ;; URL encoded
  48. (let ((uri (format "/some/directory/org-protocol:/store-link:/%s/TITLE"
  49. (url-hexify-string "http://example.com"))))
  50. (should (null (org-protocol-check-filename-for-protocol uri (list uri) nil)))
  51. (should (equal (car org-stored-links) '("http://example.com" "TITLE"))))
  52. ;; Handle multiple slashes, old link style
  53. (let ((uri "/some/directory/org-protocol://store-link://URL2//TITLE2"))
  54. (should (null (org-protocol-check-filename-for-protocol uri (list uri) nil)))
  55. (should (equal (car org-stored-links) '("URL2" "TITLE2"))))
  56. ;; New link style
  57. (let ((uri "/some/directory/org-protocol://store-link?url=URL3&title=TITLE3"))
  58. (should (null (org-protocol-check-filename-for-protocol uri (list uri) nil)))
  59. (should (equal (car org-stored-links) '("URL3" "TITLE3")))))
  60. (ert-deftest test-org-protocol/org-protocol-capture ()
  61. "Test `org-protocol-capture' specifications."
  62. (let* ((org-protocol-default-template-key "t")
  63. (temp-file-name (make-temp-file "org-protocol-test"))
  64. (org-capture-templates
  65. `(("t" "Test" entry (file ,temp-file-name) "** TODO\n\n%i\n\n%a\n" :kill-buffer t)
  66. ("x" "With params" entry (file ,temp-file-name) "** SOMEDAY\n\n%i\n\n%a\n" :kill-buffer t)
  67. ("X" "Just the template" entry (file ,temp-file-name) "** Hello World\n\n%i\n\nGoodbye World\n" :kill-buffer t)))
  68. (test-urls
  69. '(
  70. ;; Old style:
  71. ;; - multiple slashes
  72. ("/some/directory/org-protocol:/capture:/URL/TITLE"
  73. . "** TODO\n\n\n\n[[URL][TITLE]]\n")
  74. ;; - body specification
  75. ("/some/directory/org-protocol:/capture:/URL/TITLE/BODY"
  76. . "** TODO\n\nBODY\n\n[[URL][TITLE]]\n")
  77. ;; - template
  78. ("/some/directory/org-protocol:/capture:/x/URL/TITLE/BODY"
  79. . "** SOMEDAY\n\nBODY\n\n[[URL][TITLE]]\n")
  80. ;; - query parameters, not sure how to include them in template
  81. ("/some/directory/org-protocol:/capture:/x/URL/TITLE/BODY/from/example"
  82. . "** SOMEDAY\n\nBODY\n\n[[URL][TITLE]]\n")
  83. ;; New style:
  84. ;; - multiple slashes
  85. ("/some/directory/org-protocol:/capture?url=NEWURL&title=TITLE"
  86. . "** TODO\n\n\n\n[[NEWURL][TITLE]]\n")
  87. ;; - body specification
  88. ("/some/directory/org-protocol:/capture?url=NEWURL&title=TITLE&body=BODY"
  89. . "** TODO\n\nBODY\n\n[[NEWURL][TITLE]]\n")
  90. ;; - template
  91. ("/some/directory/org-protocol:/capture?template=x&url=NEWURL&title=TITLE&body=BODY"
  92. . "** SOMEDAY\n\nBODY\n\n[[NEWURL][TITLE]]\n")
  93. ;; - no url specified
  94. ("/some/directory/org-protocol:/capture?template=x&title=TITLE&body=BODY"
  95. . "** SOMEDAY\n\nBODY\n\nTITLE\n")
  96. ;; - no title specified
  97. ("/some/directory/org-protocol:/capture?template=x&url=NEWURL&body=BODY"
  98. . "** SOMEDAY\n\nBODY\n\n[[NEWURL][NEWURL]]\n")
  99. ;; - just the template
  100. ("/some/directory/org-protocol:/capture?template=X"
  101. . "** Hello World\n\n\n\nGoodbye World\n")
  102. ;; - query parameters, not sure how to include them in template
  103. ("/some/directory/org-protocol:/capture?template=x&url=URL&title=TITLE&body=BODY&from=example"
  104. . "** SOMEDAY\n\nBODY\n\n[[URL][TITLE]]\n")
  105. )))
  106. ;; Old link style
  107. (mapc
  108. (lambda (test-case)
  109. (let ((uri (car test-case)))
  110. (org-protocol-check-filename-for-protocol uri (list uri) nil)
  111. (should (string= (buffer-string) (cdr test-case)))
  112. (org-capture-kill)))
  113. test-urls)
  114. (delete-file temp-file-name)))
  115. (ert-deftest test-org-protocol/org-protocol-open-source ()
  116. "Test org-protocol://open-source links."
  117. (let* ((temp-file-name1 (make-temp-file "org-protocol-test1"))
  118. (temp-file-name2 (make-temp-file "org-protocol-test2"))
  119. (org-protocol-project-alist
  120. `((test1
  121. :base-url "http://example.com/"
  122. :online-suffix ".html"
  123. :working-directory ,(file-name-directory temp-file-name1))
  124. (test2
  125. :base-url "http://another.example.com/"
  126. :online-suffix ".js"
  127. :working-directory ,(file-name-directory temp-file-name2))
  128. (test3
  129. :base-url "https://blog-example.com/"
  130. :working-directory ,(file-name-directory temp-file-name2)
  131. :online-suffix ".html"
  132. :working-suffix ".md"
  133. :rewrites (("\\(https://blog-example.com/[0-9]+/[0-9]+/[0-9]+/\\)" . ".md")))))
  134. (test-cases
  135. (list
  136. ;; Old-style URLs
  137. (cons
  138. (concat "/some/directory/org-protocol:/open-source:/"
  139. (url-hexify-string
  140. (concat "http://example.com/" (file-name-nondirectory temp-file-name1) ".html")))
  141. temp-file-name1)
  142. (cons
  143. (concat "/some/directory/org-protocol:/open-source:/"
  144. (url-hexify-string
  145. (concat "http://another.example.com/" (file-name-nondirectory temp-file-name2) ".js")))
  146. temp-file-name2)
  147. ;; New-style URLs
  148. (cons
  149. (concat "/some/directory/org-protocol:/open-source?url="
  150. (url-hexify-string
  151. (concat "http://example.com/" (file-name-nondirectory temp-file-name1) ".html")))
  152. temp-file-name1)
  153. (cons
  154. (concat "/some/directory/org-protocol:/open-source?url="
  155. (url-hexify-string
  156. (concat "http://another.example.com/" (file-name-nondirectory temp-file-name2) ".js")))
  157. temp-file-name2))))
  158. (mapc (lambda (test-case)
  159. (should (string=
  160. (org-protocol-check-filename-for-protocol
  161. (car test-case)
  162. (list (car test-case)) nil)
  163. (cdr test-case))))
  164. test-cases)
  165. (delete-file temp-file-name1)
  166. (delete-file temp-file-name2)))
  167. (defun test-org-protocol/org-protocol-greedy-handler (fname)
  168. ;; fname should be a list of parsed items
  169. (should (listp fname))
  170. nil)
  171. (ert-deftest test-org-protocol/org-protocol-with-greedy-handler ()
  172. "Check that greedy handlers are called with all the filenames."
  173. (let ((org-protocol-protocol-alist
  174. '(("protocol-a" :protocol "greedy" :function test-org-protocol/org-protocol-greedy-handler :kill-client t :greedy t))))
  175. ;; Neither of these should signal errors
  176. (let ((uri "/some/dir/org-protocol://greedy?a=b&c=d")
  177. (uri2 "/some/dir/org-protocol://greedy?e=f&g=h"))
  178. (org-protocol-check-filename-for-protocol uri (list uri uri2) nil))))
  179. ;; TODO: Verify greedy protocol handling
  180. ;;; test-org-protocol.el ends here