-
Notifications
You must be signed in to change notification settings - Fork 32
/
beancount-tests.el
524 lines (463 loc) · 16.1 KB
/
beancount-tests.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
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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
;;; beancount-test.el --- ERT for beancount-mode -*- lexical-binding: t -*-
;; Copyright 2019 Daniele Nicolodi <[email protected]>
;; 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.
(require 'ert)
(require 'eieio)
(require 'beancount)
(require 'imenu)
(ert-deftest beancount/smoke-001 ()
:tags '(regress)
(with-temp-buffer
(beancount-mode)
(font-lock-ensure)))
(ert-deftest beancount/number-regexp-001 ()
:tags '(regexp regress)
(let ((r (concat "\\`" beancount-number-regexp "\\'")))
(should (string-match r "1"))
(should (string-match r "1."))
(should (string-match r "1.0"))
(should (string-match r "10000.00"))
(should (string-match r "1,234.56"))
(should (string-match r "10,000.00"))
(should (string-match r "1,000,000.00"))
(should-not (string-match r "1.00.00"))
(should-not (string-match r "1,00.00"))
(should-not (string-match r ",000.00"))
(should-not (string-match r ".00"))
(should-not (string-match r "."))
(should-not (string-match r ","))))
(defun beancount-test-fontify-string (string)
"Fontify STRING in beancount-mode."
(with-temp-buffer
(insert string)
(beancount-mode)
(font-lock-ensure)
(buffer-string)))
(defun beancount-test-face-groups (fontified)
"Group a fontified string by face.
Return a list of substrings each followed by its face."
(cl-loop for start = 0 then end
while start
for end = (next-single-property-change start 'face fontified)
for prop = (get-text-property start 'face fontified)
for text = (substring-no-properties fontified start end)
if prop
append (list text prop)))
(defun beancount-test-group-str-by-face (str)
"Fontify `str' in beancount-mode and group it by face.
Return a list of substrings each followed by its face."
(beancount-test-face-groups (beancount-test-fontify-string str)))
(defun beancount-test-font-lock (source face-groups)
"Test that `source' fontifies to the expected `face-groups'."
(should (equal (beancount-test-group-str-by-face source) face-groups)))
(ert-deftest beancount/fontify-001 ()
:tags '(font regress)
(beancount-test-font-lock "
2019-01-01 * \"Example\"
Expenses:Example 1.00 USD
Assets:Checking
"
'("2019-01-01" beancount-date
"*" beancount-narrative-cleared
"\"Example\"" beancount-narrative-cleared
"Expenses:Example" beancount-account
"1.00 USD" beancount-amount
"Assets:Checking" beancount-account)))
(ert-deftest beancount/fontify-002 ()
:tags '(font regress)
(beancount-test-font-lock "
2019-01-01 ! \"Example\"
Expenses:Example 1.00 USD
Assets:Checking
"
'("2019-01-01" beancount-date
"!" beancount-narrative-pending
"\"Example\"" beancount-narrative-pending
"Expenses:Example" beancount-account
"1.00 USD" beancount-amount
"Assets:Checking" beancount-account)))
(ert-deftest beancount/fontify-003 ()
:tags '(font regress)
(beancount-test-font-lock "
2019-01-01 A \"Example\"
Expenses:Example 1.00 USD
Assets:Checking
"
'("2019-01-01" beancount-date
"A" beancount-narrative
"\"Example\"" beancount-narrative
"Expenses:Example" beancount-account
"1.00 USD" beancount-amount
"Assets:Checking" beancount-account)))
(ert-deftest beancount/fontify-004 ()
:tags '(font regress)
(beancount-test-font-lock "
2019-01-01 * \"Example\"
#foo
^bar
Expenses:Example 1.00 USD
Assets:Checking
"
'("2019-01-01" beancount-date
"*" beancount-narrative-cleared
"\"Example\"" beancount-narrative-cleared
"#foo" beancount-tag
"^bar" beancount-link
"Expenses:Example" beancount-account
"1.00 USD" beancount-amount
"Assets:Checking" beancount-account)))
(ert-deftest beancount/fontify-005 ()
:tags '(font regress)
(beancount-test-font-lock "
2019-01-01 open Assets:TD:TDB900 TDB900
"
'("2019-01-01" beancount-date
"open" beancount-directive
"Assets:TD:TDB900" beancount-account)))
(ert-deftest beancount/indent-001 ()
:tags '(indent regress)
(with-temp-buffer
(insert "
2019-01-01 * \"Example\"
#foo
^bar
Expenses:Example 1.00 USD
Assets:Checking 1.00 USD
")
(beancount-mode)
(forward-line -1)
(beancount-indent-transaction)
(should (equal (buffer-string) "
2019-01-01 * \"Example\"
#foo
^bar
Expenses:Example 1.00 USD
Assets:Checking 1.00 USD
"))))
(ert-deftest beancount/options-001 ()
"Verify that beancount-mode recognises all options implemented
in beancount. Use the output of bean-doctor to get a list of
known option nmaes."
:tags '(options)
(let (options)
(with-temp-buffer
(shell-command "bean-doctor list_options" t)
(goto-char (point-min))
(while (re-search-forward "^option\\s-+\"\\([a-z_]*\\)\"" nil t)
(setq options (cons (match-string-no-properties 1) options))))
(should (equal (sort options #'string<) beancount-option-names))))
(ert-deftest beancount/completion-001 ()
:tags '(completion regress)
(with-temp-buffer
(insert "
2019-01-01 * \"Example\"
Expenses:Test 1.00 USD
Assets:Checking
2019-01-01 * \"Example\"
Expenses:T
")
(beancount-mode)
(forward-line -1)
(move-end-of-line 1)
(completion-at-point)
(should (equal (buffer-string) "
2019-01-01 * \"Example\"
Expenses:Test 1.00 USD
Assets:Checking
2019-01-01 * \"Example\"
Expenses:Test
"))
(should (equal beancount-accounts '("Assets:Checking" "Expenses:Test")))))
(ert-deftest beancount/outline-001 ()
:tags '(outline)
(with-temp-buffer
(insert "
* A
** B
*** C
")
(beancount-mode)
(outline-minor-mode)
(forward-line -1)
(should (looking-at beancount-outline-regexp))
(should (equal (beancount-outline-level) 3))))
(ert-deftest beancount/outline-002 ()
:tags '(outline)
(with-temp-buffer
(insert "
;;; A
;;;; B
;;;;; C
")
(beancount-mode)
(outline-minor-mode)
(forward-line -1)
(should (looking-at beancount-outline-regexp))
(should (equal (beancount-outline-level) 3))))
(ert-deftest beancount/outline-fontify-001 ()
:tags '(outline)
(let ((fontified
(with-temp-buffer
(insert "
* A
** B
*** C
")
(beancount-mode)
(outline-minor-mode)
(font-lock-ensure)
(buffer-string))))
(should (equal (beancount-test-face-groups fontified)
'("* A" beancount-outline-1
"** B" beancount-outline-2
"*** C" beancount-outline-3)))))
(ert-deftest beancount/account-currency-001 ()
:tags '(regress)
(with-temp-buffer
(insert "
2019-12-22 open Assets:Test:One USD
2019-12-22 open Assets:Test:Two USD,EUR
2019-12-22 open Assets:Test:Three USD \"STRICT\"
2019-12-22 open Assets:Test:Four USD,EUR \"STRICT\"
2019-12-22 open Assets:Test:Five \"STRICT\"
")
(should (equal (beancount--account-currency "Assets:Test:One") "USD"))
(should (equal (beancount--account-currency "Assets:Test:Two") nil))
(should (equal (beancount--account-currency "Assets:Test:Three") "USD"))
(should (equal (beancount--account-currency "Assets:Test:Four") nil))
(should (equal (beancount--account-currency "Assets:Test:Five") nil))))
(ert-deftest beancount/imenu-001 ()
:tags '(regress imenu)
(with-temp-buffer
(insert "
;;; 2019
;;;; 2019 January
;;;; 2019 February
;;; 2020
;;;; 2020 January
2020-01-01 * \"Example\"
Expenses:Test 1.00 USD
Assets:Checking
;;;; 2020 February
")
(beancount-mode)
(outline-minor-mode)
(let* ((imenu-use-markers nil) ; makes testing easier
(index (funcall imenu-create-index-function)))
(should (equal index '(("2019" . 2)
("2019 January" . 11)
("2019 February" . 29)
("2020" . 48)
("2020 January" . 57)
("2020 February" . 146)))))))
(ert-deftest beancount/imenu-002 ()
:tags '(regress imenu)
(with-temp-buffer
(insert "
* 2019
** 2019 January
2019-01-01 * \"Example\"
Expenses:Test 1.00 USD
Assets:Checking
** 2019 February
* 2020
** 2020 January
** 2020 February
")
(beancount-mode)
(outline-minor-mode)
(let* ((imenu-use-markers nil) ; makes testing easier
(index (funcall imenu-create-index-function)))
(should (equal index '(("2019" . 2)
("2019 January" . 9)
("2019 February" . 96)
("2020" . 113)
("2020 January" . 120)
("2020 February" . 136)))))))
(ert-deftest beancount/link-at-point-001 ()
:tags '(regress thing-at-point)
(with-temp-buffer
(insert "^link")
(should (equal (thing-at-point 'beancount-link) "^link"))))
(ert-deftest beancount/link-at-point-002 ()
:tags '(regress thing-at-point)
(with-temp-buffer
(insert "not-a-link")
(should (equal (thing-at-point 'beancount-link) nil))))
(ert-deftest beancount/link-at-point-003 ()
:tags '(regress thing-at-point)
(with-temp-buffer
(insert "foo ^link baz")
(goto-char 7)
(should (equal (thing-at-point 'beancount-link) "^link"))))
;;; Date shifting
(ert-deftest beancount/date-shift-up-day ()
:tags '(date-shift)
(with-temp-buffer
(insert "2024-05-11\n")
(goto-char 0)
(beancount-date-up-day)
(should (equal (thing-at-point 'line) "2024-05-12\n"))))
(ert-deftest beancount/date-shift-down-day ()
:tags '(date-shift)
(with-temp-buffer
(insert "2024-05-11\n")
(goto-char 0)
(beancount-date-down-day)
(should (equal (thing-at-point 'line) "2024-05-10\n"))))
;;; Xref backend
(defun beancount-test-xref-definition-pos (identifier position)
"Check if IDENTIFIER's position is the same is the same as
POSITION provided by Beancount's xref-backend-definitions lookup."
(let ((defs (xref-backend-definitions 'beancount identifier)))
(should (equal (length defs) 1))
(let* ((def (car (xref-backend-definitions 'beancount identifier)))
(loc (xref-item-location def))
;; Pre Emacs-28.1, defclass was used for
;; xref-buffer-location.
(pos (if (< emacs-major-version 28)
(oref loc position)
(xref-buffer-location-position loc))))
(should (equal pos position)))))
(ert-deftest beancount/xref-backend-definitions-accounts ()
:tags '(xref)
(with-temp-buffer
(insert "
2019-01-01 open Assets:Account1 TDB900
2019-01-01 open Assets:Account2 TDB900
2019-01-01 open Assets:Account3 TDB900
2019-01-10 * \"Opening Balances\"
Equity:Opening-Balances
Assets:Account1 1.00 TDB900
")
(beancount-test-xref-definition-pos "Assets:Account1" 2)
(beancount-test-xref-definition-pos "Assets:Account2" 41)
(beancount-test-xref-definition-pos "Assets:Account3" 80)))
(ert-deftest beancount/xref-backend-definitions-tags ()
:tags '(xref)
(with-temp-buffer
(insert "
2019-01-10 * \"Opening Balances\" #tag1
Equity:Opening-Balances
Assets:Account1 1.00 TDB900
2019-01-10 * \"Opening Balances\" #tag2
Equity:Opening-Balances
Assets:Account1 1.00 TDB900
")
(beancount-test-xref-definition-pos "#tag1" 35)
(beancount-test-xref-definition-pos "#tag2" 138)))
(ert-deftest beancount/xref-backend-definitions-links ()
:tags '(xref)
(with-temp-buffer
(insert "
2019-01-10 * \"Opening Balances\" #link1
Equity:Opening-Balances
Assets:Account1 1.00 TDB900
2019-01-10 * \"Opening Balances\" #link2
Equity:Opening-Balances
Assets:Account1 1.00 TDB900
")
(beancount-test-xref-definition-pos "#link1" 35)
(beancount-test-xref-definition-pos "#link2" 139)))
(defmacro beancount-with-temp-file (&rest body)
"Generate a temporary file and open it as a current buffer.
Run BODY forms in the buffer's context. Remove both the buffer
and a backing file having completed the test."
(declare (indent 1))
`(let ((file (make-temp-file "beancount-test-"))
buf)
(unwind-protect
(progn (setq buf (find-file-literally file))
,@body)
(ignore-errors (delete-file file))
(ignore-errors
(with-current-buffer buf
(set-buffer-modified-p nil))
(kill-buffer buf)))))
(ert-deftest beancount/xref-backend-references-accounts ()
:tags '(xref)
;; Creating Xref file locations assumes a buffer backed by a file.
(beancount-with-temp-file
(insert "
2019-01-01 open Assets:Account1 TDB900
2019-01-01 open Assets:Account2 TDB900
2019-01-01 open Assets:Account3 TDB900
2019-01-10 * \"Opening Balances\"
Equity:Opening-Balances
Assets:Account1 1.00 TDB900
Assets:Account2 2.00 TDB900
2019-01-10 * \"More Balances\"
Equity:Opening-Balances
Assets:Account1 1.00 TDB900
")
(should (equal (length (xref-backend-references 'beancount "Assets:Account1")) 3))
(should (equal (length (xref-backend-references 'beancount "Assets:Account2")) 2))
(should (equal (length (xref-backend-references 'beancount "Assets:Account3")) 1))))
(ert-deftest beancount/xref-backend-references-tags ()
:tags '(xref)
;; Creating Xref file locations assumes a buffer backed by a file.
(beancount-with-temp-file
(insert "
2019-01-10 * \"More Balances\" #tag1
Equity:Opening-Balances
Assets:Account1 1.00 TDB900
2019-01-10 * \"Opening Balances\" #tag2
Assets:Account1 1.00 TDB900
Assets:Account2 2.00 TDB900
2019-01-10 * \"More Balances\" #tag2
Equity:Opening-Balances
Assets:Account1 1.00 TDB900
")
(should (equal (length (xref-backend-references 'beancount "#tag1")) 1))
(should (equal (length (xref-backend-references 'beancount "#tag2")) 2))))
(ert-deftest beancount/xref-backend-references-links ()
:tags '(xref)
;; Creating Xref file locations assumes a buffer backed by a file.
(beancount-with-temp-file
(insert "
2019-01-10 * \"More Balances\" ^link1
Equity:Opening-Balances
Assets:Account1 1.00 TDB900
2019-01-10 * \"Opening Balances\" ^link1
Assets:Account1 1.00 TDB900
Assets:Account2 2.00 TDB900
2019-01-10 * \"More Balances\" ^link2
Equity:Opening-Balances
Assets:Account1 1.00 TDB900
")
(should (equal (length (xref-backend-references 'beancount "^link1")) 2))
(should (equal (length (xref-backend-references 'beancount "^link2")) 1))))
(ert-deftest beancount/xref-backend-apropos ()
:tags '(xref)
;; Creating Xref file locations assumes a buffer backed by a file.
(beancount-with-temp-file
(insert "
2019-01-01 open Assets:Account1 TDB900
2019-01-01 open Assets:Account2 TDB900
2019-01-01 open Assets:Account3 TDB900
2019-01-10 * \"Opening Balances\" #tag ^link1
Equity:Opening-Balances
Assets:Account1 1.00 TDB900
Assets:Account2 2.00 TDB900
2019-01-10 * \"More Balances\" #tag ^link2
Equity:Opening-Balances
Assets:Account1 1.00 TDB900
")
(should (equal (length (xref-backend-apropos 'beancount "Assets")) 6))
(should (equal (length (xref-backend-apropos 'beancount "Assets Account")) 6))
(should (equal (length (xref-backend-apropos 'beancount "Assets Account1")) 3))
(should (equal (length (xref-backend-apropos 'beancount "Equity")) 2))
(should (equal (length (xref-backend-apropos 'beancount "Opening")) 2))
(should (equal (length (xref-backend-apropos 'beancount "Opening Assets")) 0))
(should (equal (length (xref-backend-apropos 'beancount "tag")) 2))
(should (equal (length (xref-backend-apropos 'beancount "link1")) 1))
(should (equal (length (xref-backend-apropos 'beancount "link2")) 1))))