forked from ahyatt/ekg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ekg-test.el
162 lines (144 loc) · 6.52 KB
/
ekg-test.el
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
;;; ekg-test.el --- Tests for ekg -*- lexical-binding: t; -*-
;; Copyright (c) 2022-2023 Andrew Hyatt <[email protected]>
;; 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 2 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 GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
;;; Commentary:
;; These tests should be run and pass before every commit.
;;; Code:
(require 'ekg)
(require 'ert)
(require 'ert-x)
(defmacro ekg-deftest (name _ &rest body)
"A test that will set up an empty `ekg-db' for use."
(declare (debug t) (indent defun))
`(ert-deftest ,name ()
(let ((ekg-db-file (make-temp-file "ekg-test"))
(ekg-db nil)
(orig-buffers (buffer-list))
;; Remove hooks
(ekg-add-schema-hook nil)
(ekg-note-pre-save-hook nil)
(ekg-note-save-hook nil)
(ekg-note-pre-delete-hook nil)
(ekg-note-delete-hook nil)
(ekg-note-add-tag-hook nil))
(ekg--connect)
(save-excursion
(unwind-protect
(progn ,@body)
;; Kill all opened bufferes
(mapc #'kill-buffer (seq-difference (buffer-list) orig-buffers)))))))
(ekg-deftest ekg-test-note-lifecycle ()
(let ((note (ekg-note-create "Test text" 'text-mode '("tag1" "tag2"))))
(ekg-save-note note)
;; We should have an ID now.
(should (ekg-note-id note))
(should (= 1 (length (ekg-get-notes-with-tags '("tag1")))))
;; Just one note, even if it has both of the tags.
(should (= 1 (length (ekg-get-notes-with-tags '("tag1" "tag2")))))
;; WHen we get notes with tags, it's an AND, so it shouldn't get anything.
(should (= 0 (length (ekg-get-notes-with-tags '("tag1" "tag2" "nonexistent")))))
(should (equal note (car (ekg-get-notes-with-tags '("tag1")))))
(should (ekg-has-live-tags-p (ekg-note-id note)))
(ekg-note-trash note)
(should-not (ekg-has-live-tags-p (ekg-note-id note)))
(should (= 0 (length (ekg-get-notes-with-tags '("tag1" "tag2")))))))
(ekg-deftest ekg-test-tags ()
(should-not (ekg-tags))
;; Make sure we trim and lowercase all tags.
(ekg-save-note (ekg-note-create "" 'text-mode '(" a" " B ")))
(should (equal (sort (ekg-tags) #'string<) '("a" "b")))
(should (equal (ekg-tags-including "b") '("b")))
(should (string= (ekg-tags-display '("a" "b")) "a b")))
(ekg-deftest ekg-test-org-link-to-id ()
(require 'ol)
(let* ((note (ekg-note-create "" 'text-mode '("a" "b")))
(note-buf (ekg-edit note)))
(unwind-protect
(progn
;; Can we store a link?
(with-current-buffer note-buf
(org-store-link nil 1)
(should (car org-stored-links)))
(with-temp-buffer
;; Does the link look correct?
(org-mode)
(org-insert-last-stored-link nil)
(should (string= (buffer-string) (format "[[ekg-note:%d][EKG note: %d]]\n" (ekg-note-id note) (ekg-note-id note))))
;; Does the link work?
(goto-char 1)
(org-open-at-point nil)
(should (eq note-buf (current-buffer)))))
(kill-buffer note-buf))))
(ekg-deftest ekg-test-org-link-to-tags ()
(require 'ol)
(ekg-save-note (ekg-note-create "" 'text-mode '("a" "b")))
(ekg-show-notes-with-any-tags '("a" "b"))
(let* ((tag-buf (get-buffer "*ekg tags (any): a b*")))
(unwind-protect
(progn
;; Can we store a link?
(with-current-buffer tag-buf
(org-store-link nil 1)
(should (car org-stored-links)))
(with-temp-buffer
;; Does the link look correct?
(org-mode)
(org-insert-last-stored-link nil)
(should (string= (buffer-string) "[[ekg-tags-any:(\"a\" \"b\")][EKG page for any of the tags: a, b]]\n"))
;; Does the link work?
(goto-char 1)
(org-open-at-point nil)
(should (eq tag-buf (current-buffer)))))
(kill-buffer tag-buf))))
(ekg-deftest ekg-test-url-handling ()
(ekg-capture-url "http://testurl" "A URL used for testing")
(insert "Text added to the URL")
(ert-simulate-command '(ekg-capture-finalize))
(should (equal (ekg-document-titles) (list (cons "http://testurl" "A URL used for testing"))))
(should (member "doc/a url used for testing" (ekg-tags)))
;; Re-capture, should edit the existing note.
(ekg-capture-url "http://testurl" "A URL used for testing")
(cl-letf (((symbol-function 'y-or-n-p) (lambda (_) t)))
(when (search-backward "http://testurl")
(replace-match "http://testurl/v2"))
(ert-simulate-command '(ekg-capture-finalize)))
(should (equal (ekg-document-titles) (list (cons "http://testurl/v2" "A URL used for testing")))))
(ekg-deftest ekg-test-sort-nondestructive ()
(mapc #'ekg-save-note
(list (ekg-note-create "a" ekg-capture-default-mode '("tag/a"))
(ekg-note-create "b" ekg-capture-default-mode '("tag/b"))))
(ekg-show-notes-with-any-tags '("tag/b" "tag/a"))
(should (string= (car (ewoc-get-hf ekg-notes-ewoc)) "tags (any): tag/a tag/b")))
(ekg-deftest ekg-test-note-roundtrip ()
(let ((text "foo\n\tbar \"baz\" ☃"))
(ekg-save-note (ekg-note-create text #'text-mode '("test")))
(let ((note (car (ekg-get-notes-with-tag "test"))))
(should (ekg-note-id note))
(should (equal text (ekg-note-text note)))
(should (equal 'text-mode (ekg-note-mode note))))))
(ekg-deftest ekg-test-templating ()
(ekg-save-note (ekg-note-create "ABC" #'text-mode '("test" "template")))
(ekg-save-note (ekg-note-create "DEF" #'text-mode '("test" "template")))
(let ((ekg-note-add-tag-hook '(ekg-on-add-tag-insert-template)))
(ekg-capture '("test"))
(let ((text (substring-no-properties (buffer-string))))
(should (string-match (rx (literal "ABC")) text))
(should (string-match (rx (literal "DEF")) text)))))
(ekg-deftest ekg-test-get-notes-with-tags ()
(ekg-save-note (ekg-note-create "ABC" #'text-mode '("foo" "bar")))
(should-not (ekg-get-notes-with-tags '("foo" "none")))
(should-not (ekg-get-notes-with-tags '("none" "foo")))
(should (= (length (ekg-get-notes-with-tags '("bar" "foo"))) 1)))
(provide 'ekg-test)
;;; ekg-test.el ends here