1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
| | ;;; test-org-protocol.el --- tests for org-protocol.el -*- lexical-binding: t; -*-
;; Copyright (c) Sacha Chua
;; Authors: Sacha Chua
;; 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 <http://www.gnu.org/licenses/>.
;;; Code:
(require 'cl-lib)
(unless (featurep 'org-protocol)
(signal 'missing-test-dependency "Support for org-protocol"))
(ert-deftest test-org-protocol/org-protocol-parse-parameters ()
"Test `org-protocol-parse-parameters' specifications."
(let ((data (org-protocol-parse-parameters "url=abc&title=def" t)))
(should (string= (plist-get data :url) "abc"))
(should (string= (plist-get data :title) "def")))
(let ((data (org-protocol-parse-parameters "abc/def" nil '(:url :title))))
(should (string= (plist-get data :url) "abc"))
(should (string= (plist-get data :title) "def")))
(let ((data (org-protocol-parse-parameters "b/abc/def" nil)))
(should (equal data '("b" "abc" "def"))))
(let ((data (org-protocol-parse-parameters "b/abc/extrakey/extraval" nil '(:param1 :param2))))
(should (string= (plist-get data :param1) "b"))
(should (string= (plist-get data :param2) "abc"))
(should (string= (plist-get data :extrakey) "extraval"))))
(ert-deftest test-org-protocol/org-protocol-store-link ()
"Test `org-protocol-store-link' specifications."
;; Old link style
(let ((uri "/some/directory/org-protocol:/store-link:/URL/TITLE"))
(should (null (org-protocol-check-filename-for-protocol uri (list uri) nil)))
(should (equal (car org-stored-links) '("URL" "TITLE"))))
;; URL encoded
(let ((uri (format "/some/directory/org-protocol:/store-link:/%s/TITLE"
(url-hexify-string "http://example.com"))))
(should (null (org-protocol-check-filename-for-protocol uri (list uri) nil)))
(should (equal (car org-stored-links) '("http://example.com" "TITLE"))))
;; Handle multiple slashes, old link style
(let ((uri "/some/directory/org-protocol://store-link://URL2//TITLE2"))
(should (null (org-protocol-check-filename-for-protocol uri (list uri) nil)))
(should (equal (car org-stored-links) '("URL2" "TITLE2"))))
;; New link style
(let ((uri "/some/directory/org-protocol://store-link?url=URL3&title=TITLE3"))
(should (null (org-protocol-check-filename-for-protocol uri (list uri) nil)))
(should (equal (car org-stored-links) '("URL3" "TITLE3")))))
(defun test-org-protocol/one-arg-fn (info) nil)
(defun test-org-protocol/two-arg-fn (info2) nil)
(ert-deftest test-org-protocol/org-protocol-check-filename-for-protocol ()
"Make sure existing functions will work with one or two args."
(let ((org-protocol-protocol-alist
'(("protocol-a" :protocol "only-one-arg" :function test-org-protocol/one-arg-fn :kill-client t)
("protocol-b" :protocol "two-args" :function test-org-protocol/two-arg-fn :kill-client t))
))
;; Neither of these should signal errors
(let ((uri "/some/dir/org-protocol://only-one-arg?a=b"))
(org-protocol-check-filename-for-protocol uri (list uri) nil))
(let ((uri "/some/dir/org-protocol://two-args?a=b"))
(should (null (org-protocol-check-filename-for-protocol uri (list uri) nil))))))
(ert-deftest test-org-protocol/org-protocol-capture ()
"Test `org-protocol-capture' specifications."
(let* ((org-protocol-default-template-key "t")
(temp-file-name (make-temp-file "org-protocol-test"))
(org-capture-templates
`(("t" "Test" entry (file ,temp-file-name) "** TODO\n\n%i\n\n%a\n" :kill-buffer t)
("x" "With params" entry (file ,temp-file-name) "** SOMEDAY\n\n%i\n\n%a\n" :kill-buffer t)))
(test-urls
'(
;; Old style:
;; - multiple slashes
("/some/directory/org-protocol:/capture:/URL/TITLE"
. "** TODO\n\n\n\n[[URL][TITLE]]\n")
;; - body specification
("/some/directory/org-protocol:/capture:/URL/TITLE/BODY"
. "** TODO\n\nBODY\n\n[[URL][TITLE]]\n")
;; - template
("/some/directory/org-protocol:/capture:/x/URL/TITLE/BODY"
. "** SOMEDAY\n\nBODY\n\n[[URL][TITLE]]\n")
;; - query parameters, not sure how to include them in template
("/some/directory/org-protocol:/capture:/x/URL/TITLE/BODY/from/example"
. "** SOMEDAY\n\nBODY\n\n[[URL][TITLE]]\n")
;; New style:
;; - multiple slashes
("/some/directory/org-protocol:/capture?url=NEWURL&title=TITLE"
. "** TODO\n\n\n\n[[NEWURL][TITLE]]\n")
;; - body specification
("/some/directory/org-protocol:/capture?url=NEWURL&title=TITLE&body=BODY"
. "** TODO\n\nBODY\n\n[[NEWURL][TITLE]]\n")
;; - template
("/some/directory/org-protocol:/capture?template=x&url=NEWURL&title=TITLE&body=BODY"
. "** SOMEDAY\n\nBODY\n\n[[NEWURL][TITLE]]\n")
;; - query parameters, not sure how to include them in template
("/some/directory/org-protocol:/capture?template=x&url=URL&title=TITLE&body=BODY&from=example"
. "** SOMEDAY\n\nBODY\n\n[[URL][TITLE]]\n")
)))
;; Old link style
(mapc
(lambda (test-case)
(let ((uri (car test-case)))
(org-protocol-check-filename-for-protocol uri (list uri) nil)
(should (string= (buffer-string) (cdr test-case)))
(org-capture-kill)))
test-urls)
(delete-file temp-file-name)))
(ert-deftest test-org-protocol/org-protocol-open-source ()
"Test org-protocol://open-source links."
(let* ((temp-file-name1 (make-temp-file "org-protocol-test1"))
(temp-file-name2 (make-temp-file "org-protocol-test2"))
(org-protocol-project-alist
`((test1
:base-url "http://example.com/"
:online-suffix ".html"
:working-directory ,(file-name-directory temp-file-name1))
(test2
:base-url "http://another.example.com/"
:online-suffix ".js"
:working-directory ,(file-name-directory temp-file-name2))
))
(test-cases
(list
;; Old-style URLs
(cons
(concat "/some/directory/org-protocol:/open-source:/"
(url-hexify-string
(concat "http://example.com/" (file-name-nondirectory temp-file-name1) ".html")))
temp-file-name1)
(cons
(concat "/some/directory/org-protocol:/open-source:/"
(url-hexify-string
(concat "http://another.example.com/" (file-name-nondirectory temp-file-name2) ".js")))
temp-file-name2)
;; New-style URLs
(cons
(concat "/some/directory/org-protocol:/open-source?url="
(url-hexify-string
(concat "http://example.com/" (file-name-nondirectory temp-file-name1) ".html")))
temp-file-name1)
(cons
(concat "/some/directory/org-protocol:/open-source?url="
(url-hexify-string
(concat "http://another.example.com/" (file-name-nondirectory temp-file-name2) ".js")))
temp-file-name2))))
(mapc (lambda (test-case)
(should (string=
(org-protocol-check-filename-for-protocol
(car test-case)
(list (car test-case)) nil)
(cdr test-case))))
test-cases)
(delete-file temp-file-name1)
(delete-file temp-file-name2)))
;;; test-org-protocol.el ends here
|