test-org-protocol.el 9.0 KB

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