summaryrefslogtreecommitdiff
path: root/bog-tests.el
blob: 7cb191167e2fd9525c90f18f50bf18c759ce7a70 (plain)
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
(require 'ert)
(require 'org)
(require 'dash)
(require 'bog)

;; Modified from magit-tests.el.
(defmacro bog-tests--with-temp-dir (&rest body)
  (declare (indent 0) (debug t))
  (let ((dir (gensym)))
    `(let ((,dir (file-name-as-directory (make-temp-file "dir" t))))
       (unwind-protect
           (let ((default-directory ,dir)) ,@body)
         (delete-directory ,dir t)))))


;;; Citekey functions

;; `bog-citekey-p'

(ert-deftest bog-citekey-p ()
  (should (bog-citekey-p "name2010word"))
  (should (bog-citekey-p "name1900word"))
  (should-not (bog-citekey-p "name201word")))

(ert-deftest bog-citekey-p-with-hyphen-in-name ()
  (should (bog-citekey-p "hyphen-ok2010word")))

(ert-deftest bog-citekey-p-with-other-text ()
  (should (bog-citekey-p "name2010word more text")))

(ert-deftest bog-citekey-only-p ()
  (should (bog-citekey-only-p "name2010word"))
  (should-not (bog-citekey-only-p "name201word"))
  (should (bog-citekey-only-p "hyphen-ok2010word"))
  (should-not (bog-citekey-only-p "name2010word more text")))

;; `bog-citekey-groups-with-delim'

(ert-deftest bog-citekey-groups-with-delim ()
  (let ((citekey "name2010word"))
    (should (equal (bog-citekey-groups-with-delim citekey)
                   "name 2010 word"))
    (should (equal (bog-citekey-groups-with-delim citekey ",")
                   "name,2010,word"))
    (should (equal (bog-citekey-groups-with-delim citekey nil '(1 3))
                   "name word"))))

;; `bog-citekey-at-point'

(ert-deftest bog-citekey-at-point ()
  (let ((citekey "name2010word"))
    (with-temp-buffer
      (insert citekey)
      (goto-char (point-min))
      (should (equal (bog-citekey-at-point) citekey)))))

;; `bog-citekey-from-heading-title'

(ert-deftest bog-citekey-from-heading-title-current-level ()
  (let ((citekey "name2010word"))
    (with-temp-buffer
      (insert (format "\n* top level\n\n** %s\n\nsome text\n"
                      citekey))
      (org-mode)
      (show-all)
      (should (equal (bog-citekey-from-heading-title) citekey)))))

(ert-deftest bog-citekey-from-heading-title-in-parent ()
  (let ((citekey "name2010word"))
    (with-temp-buffer
      (insert (format "\n* top level\n\n** %s\n\n*** subheading\n\nsome text\n"
                      citekey))
      (org-mode)
      (show-all)
      (should (equal (bog-citekey-from-heading-title) citekey)))))

(ert-deftest bog-citekey-from-heading-title-on-heading ()
  (let ((citekey "name2010word"))
    (with-temp-buffer
      (insert (format "\n* top level\n\n** %s\n\nsome text\n"
                      citekey))
      (org-mode)
      (show-all)
      (re-search-backward bog-citekey-format)
      (should (equal (bog-citekey-from-heading-title) citekey)))))

;; `bog-citekey-from-property'

(ert-deftest bog-citekey-from-property-current-level ()
  (let ((citekey "name2010word"))
    (with-temp-buffer
      (insert "\n* top level\n\n** subhead\n"
              (format  ":PROPERTIES:\n:CUSTOM_ID: %s\n" citekey)
              ":END:\nsome text\n")
      (org-mode)
      (show-all)
      (should (equal (bog-citekey-from-property) citekey)))))

(ert-deftest bog-citekey-from-property-in-parent ()
  (let ((citekey "name2010word"))
    (with-temp-buffer
      (insert "\n* top level\n"
              (format  ":PROPERTIES:\n:CUSTOM_ID: %s\n" citekey)
              ":END:\nsome text\n"
              "** subhead\n\n")
      (org-mode)
      (show-all)
      (should (equal (bog-citekey-from-property) citekey)))))

(ert-deftest bog-citekey-from-property-on-heading ()
  (let ((citekey "name2010word"))
    (with-temp-buffer
      (insert "\n* top level\n\n** subhead\n"
              (format  ":PROPERTIES:\n:CUSTOM_ID: %s\n" citekey)
              ":END:\nsome text\n")
      (org-mode)
      (show-all)
      (org-back-to-heading)
      (should (equal (bog-citekey-from-property) citekey)))))

;; `bog-citekey-from-notes'

(ert-deftest bog-citekey-from-notes-on-heading ()
  (let ((citekey "name2010word"))
    (with-temp-buffer
      (insert (format "\n* top level\n\n** %s\n\nsome text\n"
                      citekey))
      (org-mode)
      (show-all)
      (re-search-backward bog-citekey-format)
      (should (equal (bog-citekey-from-notes) citekey)))))

(ert-deftest bog-citekey-from-notes-on-in-text-citekey ()
  (let ((citekey "name2010word"))
    (with-temp-buffer
      (insert (format "\n* top level\n\n** other2000key\n\nsome text and %s\n"
                      citekey))
      (org-mode)
      (show-all)
      (re-search-backward bog-citekey-format)
      (should (equal (bog-citekey-from-notes) citekey)))))

(ert-deftest bog-citekey-from-notes-no-citekey ()
  (with-temp-buffer
    (insert  "\n* top level\n\n** second\n\n")
    (org-mode)
    (show-all)
    (should-error (bog-citekey-from-notes))))


;;; PDF functions

(ert-deftest bog-rename-staged-pdf-to-citekey-one-pdf ()
  (bog-tests--with-temp-dir
   (let ((bog-stage-directory (expand-file-name "stage"))
         (bog-pdf-directory (expand-file-name "pdfs"))
         (citekey "name2010word"))
     (make-directory bog-stage-directory)
     (make-directory bog-pdf-directory)
     (write-region "" nil (expand-file-name "one.pdf" bog-stage-directory))
     (with-temp-buffer
       (insert (format "\n* top level\n\n** %s\n\nsome text\n"
                       citekey))
       (org-mode)
       (show-all)
       (re-search-backward bog-citekey-format)
       (bog-rename-staged-pdf-to-citekey))
     (should (file-exists-p (expand-file-name
                             (concat citekey ".pdf") bog-pdf-directory)))
     (should-not (file-exists-p (expand-file-name
                                 (concat "one.pdf") bog-stage-directory))))))

(ert-deftest bog-pdf-citekeys-multiple-variants ()
  (bog-tests--with-temp-dir
   (let* ((bog-pdf-directory (expand-file-name "pdfs"))
          (citekey "name2010word")
          (variants (--map (concat citekey it ".pdf")
                           '("" "_0" "-supplement")))
          found-files)
     (make-directory bog-pdf-directory)
     (--each variants
       (write-region "" nil (expand-file-name it bog-pdf-directory)))
     (setq files-found (bog-citekey-pdfs citekey))
     (should (= (length files-found) 3)))))


;;; BibTeX functions

;; `bog-prepare-bib-file'

(ert-deftest bog-prepare-bib-file ()
  (let ((temp-file (make-temp-file "bog-testing-" nil ".bib"))
        (citekey "name2010word"))
    (with-current-buffer (find-file-noselect temp-file)
      (insert (format "\n@article{%s,\n" citekey)
              "title = {A title},\n"
              "author = {Last, First},\n"
              "journal = {Some journal},\n"
              "year = 2009,\n"
              "\n}")
        (save-buffer))
    (kill-buffer (get-file-buffer temp-file))
    (bog-prepare-bib-file temp-file)
    (should-not (file-exists-p temp-file))
    (let* ((new-file (expand-file-name (concat citekey ".bib") "/tmp"))
           (new-buffer (get-file-buffer new-file)))
      (should-not new-buffer)
      (delete-file new-file))))

(ert-deftest bog-prepare-bib-file-was-open ()
  (let ((temp-file (make-temp-file "bog-testing-" nil ".bib"))
        (citekey "name2010word"))
    (with-current-buffer (find-file-noselect temp-file)
      (insert (format "\n@article{%s,\n" citekey)
              "title = {A title},\n"
              "author = {Last, First},\n"
              "journal = {Some journal},\n"
              "year = 2009,\n"
              "\n}")
        (save-buffer))
    (bog-prepare-bib-file temp-file)
    (should-not (file-exists-p temp-file))
    (let* ((new-file (expand-file-name (concat citekey ".bib") "/tmp"))
           (new-buffer (get-file-buffer new-file)))
      (should new-buffer)
      (kill-buffer new-buffer)
      (delete-file new-file))))

;; `bog-collect-references'

(ert-deftest bog-collect-references ()
  (with-temp-buffer
    (insert  "abc1900word\nhij2000word\nefg1800word\n")
    (should (equal (bog-collect-references)
                   '("abc1900word" "efg1800word" "hij2000word")))))

(ert-deftest bog-collect-references-no-sort ()
  (with-temp-buffer
    (insert  "abc1900word\nhij2000word\nefg1800word\n")
    (should (equal (bog-collect-references t)
                   '("efg1800word" "hij2000word" "abc1900word")))))

;; `bog-sort-topic-headings-in-buffer'

(ert-deftest bog-sort-topic-headings-in-buffer ()
  (with-temp-buffer
    (let ((bog-topic-heading-level 1))
      (insert  "\n* topic heading\n\n"
               "** zoo2000key\n\nsome text\n\n"
               "** apple2000key\n\nsome text\n"
               "* another topic heading\n\n"
               "** orange2000key\n\nsome text\n\n"
               "** banana2000key\n\nsome text\n"
               "** yogurt2000key\n\nsome text\n")
      (org-mode)
      (show-all)
      (bog-sort-topic-headings-in-buffer)
      (goto-char 0)
      (outline-next-visible-heading 2)
      (should (equal (org-no-properties (org-get-heading t t))
                     "apple2000key"))
      (outline-next-visible-heading 3)
      (should (equal (org-no-properties (org-get-heading t t))
                     "banana2000key")))))

(ert-deftest bog-sort-topic-headings-in-buffer-ignore-citekey-heading ()
  (with-temp-buffer
    (let ((bog-topic-heading-level 1))
      (insert  "\n* topic heading\n\n"
               "** zoo2000key\n\nsome text\n\n"
               "** apple2000key\n\nsome text\n"
               "* citekey2000heading\n\n"
               "** orange2000key\n\nsome text\n\n"
               "** banana2000key\n\nsome text\n"
               "** yogurt2000key\n\nsome text\n")
      (org-mode)
      (show-all)
      (bog-sort-topic-headings-in-buffer)
      (goto-char 0)
      (outline-next-visible-heading 2)
      (should (equal (org-no-properties (org-get-heading t t))
                     "apple2000key"))
      (outline-next-visible-heading 3)
      (should (equal (org-no-properties (org-get-heading t t))
                     "orange2000key")))))

(ert-deftest bog-sort-topic-headings-in-buffer-ignore-citekey-property ()
  (with-temp-buffer
    (let ((bog-topic-heading-level 1))
      (insert  "\n* topic heading\n\n"
               "** zoo2000key\n\nsome text\n\n"
               "** apple2000key\n\nsome text\n"
               "* non-topic heading\n"
               " :PROPERTIES:\n"
               (format " :%s: citekey2000prop\n" bog-citekey-property)
               " :END:\n"
               "** orange2000key\n\nsome text\n\n"
               "** banana2000key\n\nsome text\n"
               "** yogurt2000key\n\nsome text\n")
      (org-mode)
      (show-all)
      (bog-sort-topic-headings-in-buffer)
      (goto-char 0)
      (outline-next-visible-heading 2)
      (should (equal (org-no-properties (org-get-heading t t))
                     "apple2000key"))
      (outline-next-visible-heading 3)
      (should (equal (org-no-properties (org-get-heading t t))
                     "orange2000key")))))

(ert-deftest bog-sort-topic-headings-in-buffer-passed-sorting-type ()
  (with-temp-buffer
    (let ((bog-topic-heading-level 1))
      (insert  "\n* topic heading\n\n"
               "** zoo2000key\n\nsome text\n\n"
               "** apple2000key\n\nsome text\n"
               "* another topic heading\n\n"
               "** orange2000key\n\nsome text\n\n"
               "** banana2000key\n\nsome text\n"
               "** yogurt2000key\n\nsome text\n")
      (org-mode)
      (show-all)
      (bog-sort-topic-headings-in-buffer ?n)
      (goto-char 0)
      (outline-next-visible-heading 2)
      (should (equal (org-no-properties (org-get-heading t t))
                     "zoo2000key"))
      (outline-next-visible-heading 3)
      (should (equal (org-no-properties (org-get-heading t t))
                     "orange2000key")))))