-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathorgzly-formatter-test.el
More file actions
721 lines (580 loc) · 16.9 KB
/
Copy pathorgzly-formatter-test.el
File metadata and controls
721 lines (580 loc) · 16.9 KB
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
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
;;; orgzly-formatter-test.el --- ERT tests for orgzly-formatter -*- lexical-binding: t -*-
;; Copyright (C) 2026 Stefan Lendl
;; SPDX-License-Identifier: GPL-3.0-or-later
;; Run from the command line:
;; emacs -batch -l orgzly-formatter.el -l orgzly-formatter-test.el \
;; -f ert-run-tests-batch-and-exit
;;
;; Or via the Makefile:
;; make test
;;; Code:
(require 'ert)
(require 'org)
(require 'orgzly-formatter)
;;;; ── Test helper ─────────────────────────────────────────────────────────────
(defmacro ozfmt-deftest (name doc input expected)
"Define an ERT test NAME (doc-string DOC) that runs the full formatter.
Inserts INPUT into a fresh org-mode buffer, calls `orgzly-formatter-buffer',
and asserts the result equals EXPECTED.
TODO/NEXT/DONE keywords are always configured so state-only heading tests
work regardless of the caller's `org-todo-keywords'."
(declare (indent 3) (doc-string 2))
`(ert-deftest ,name ()
,doc
(let ((result
(let ((org-todo-keywords '((sequence "TODO" "NEXT" "|" "DONE"))))
(with-temp-buffer
;; delay-mode-hooks prevents our own save-hook from firing
;; inside the test and interfering with assertions.
(delay-mode-hooks (org-mode))
(insert ,input)
(orgzly-formatter-buffer)
(buffer-string)))))
(should (equal result ,expected)))))
;;;; ── WS: trailing whitespace ─────────────────────────────────────────────────
;; These tests use \n-escaped strings intentionally: Emacs strips trailing
;; spaces on save, which would silently corrupt the test inputs/expectations.
(ozfmt-deftest ozfmt/ws/strips-body-line
"Trailing spaces on a body line are removed."
"* H\nbody text \n"
"* H\nbody text\n\n")
(ozfmt-deftest ozfmt/ws/strips-heading-trailing-spaces
"Trailing spaces on a plain heading (no keyword) are removed."
"* Heading \n"
"* Heading\n")
(ozfmt-deftest ozfmt/ws/strips-titled-todo-heading
"A TODO heading with a title loses trailing spaces — only keyword-only gets the exemption."
"* TODO My task \n"
"* TODO My task\n")
(ozfmt-deftest ozfmt/ws/preserves-todo-state-only-space
"* TODO (single trailing space) is preserved — load-bearing for org parser."
"* TODO \n"
"* TODO \n")
(ozfmt-deftest ozfmt/ws/preserves-next-state-only-space
"* NEXT (single trailing space) is preserved."
"* NEXT \n"
"* NEXT \n")
(ozfmt-deftest ozfmt/ws/strips-multiple-trailing-spaces-on-state-heading
"Three trailing spaces on a state-only heading are all stripped (pattern requires exactly one)."
"* NEXT \n"
"* NEXT\n")
;;;; ── Empty entries (heading only, no content) ───────────────────────────────
(ozfmt-deftest ozfmt/empty/single-heading
"A file with a single empty heading produces no trailing blank line."
"* Heading
"
"* Heading
")
(ozfmt-deftest ozfmt/empty/two-adjacent-headings
"Two adjacent empty headings: no blank line injected."
"* A
* B
"
"* A
* B
")
(ozfmt-deftest ozfmt/empty/blank-between-empty-headings-stripped
"A stray blank line between two empty headings is removed by R1 case A."
"* A
* B
"
"* A
* B
")
(ozfmt-deftest ozfmt/empty/multiple-blanks-between-empty-headings-stripped
"Multiple blank lines between two empty headings collapse to zero."
"* A
* B
"
"* A
* B
")
(ozfmt-deftest ozfmt/empty/heading-only-last-entry-no-eof-blank
"The last heading-only entry in the file gets no trailing blank line."
"* First
some content
* Last
"
"* First
some content
* Last
")
;;;; ── Content entries: R3 trailing blank ─────────────────────────────────────
(ozfmt-deftest ozfmt/content/body-gets-trailing-blank
"An entry with plain body text gets exactly one trailing blank (diff 1 regression)."
"* H
body text
* Next
"
"* H
body text
* Next
")
(ozfmt-deftest ozfmt/content/drawer-only-gets-trailing-blank
"An entry with only a properties drawer gets a trailing blank (diff 2 regression)."
"* H
:PROPERTIES:
:ID: abc123
:END:
* Next
"
"* H
:PROPERTIES:
:ID: abc123
:END:
* Next
")
(ozfmt-deftest ozfmt/content/planning-only-gets-trailing-blank
"An entry with only a SCHEDULED line gets a trailing blank."
"* H
SCHEDULED: <2026-03-16 Mo>
* Next
"
"* H
SCHEDULED: <2026-03-16 Mo>
* Next
")
(ozfmt-deftest ozfmt/content/planning-plus-drawer-gets-trailing-blank
"Planning + properties, no body — still gets a trailing blank."
"* H
SCHEDULED: <2026-03-16 Mo>
:PROPERTIES:
:ID: x
:END:
* Next
"
"* H
SCHEDULED: <2026-03-16 Mo>
:PROPERTIES:
:ID: x
:END:
* Next
")
(ozfmt-deftest ozfmt/content/body-trailing-blank-already-present
"When the trailing blank already exists the buffer is unchanged (idempotent)."
"* H
body text
* Next
"
"* H
body text
* Next
")
(ozfmt-deftest ozfmt/content/body-ends-with-bold-text
"When the last line of the body ends with Org markup (e.g. bold), the trailing blank is still added after it, not stripped as part of the markup."
"* H
body text
*bold test*
* Next
"
"* H
body text
*bold test*
* Next
")
(ozfmt-deftest ozfmt/content/last-entry-with-body-gets-trailing-blank
"The last entry in the file gets a trailing blank even with no next heading."
"* A
body
"
"* A
body
")
(ozfmt-deftest ozfmt/content/multiline-body-gets-single-trailing-blank
"Multiple body lines get exactly one trailing blank, not one per line."
"* H
line one
line two
line three
* Next
"
"* H
line one
line two
line three
* Next
")
;;;; ── R1: blank before headings ──────────────────────────────────────────────
(ozfmt-deftest ozfmt/r1/no-blank-before-first-heading
"The very first heading must not gain a leading blank line (bobp guard)."
"* First
body
"
"* First
body
")
(ozfmt-deftest ozfmt/r1/one-blank-already-correct
"Exactly one blank before a heading: nothing changes."
"* A
body
* B
"
"* A
body
* B
")
(ozfmt-deftest ozfmt/r1/two-blanks-trimmed-to-one
"Two blank lines between content headings are trimmed to one."
"* A
body
* B
"
"* A
body
* B
")
(ozfmt-deftest ozfmt/r1/three-blanks-trimmed-to-one
"Three blank lines between content headings are trimmed to one."
"* A
body
* B
"
"* A
body
* B
")
(ozfmt-deftest ozfmt/r1/blank-inserted-when-missing-between-content-headings
"When content A is followed immediately by B (no blank), R3 for A fires and
satisfies R1 for B in the same pass."
"* A
body
* B
body
"
"* A
body
* B
body
")
;;;; ── EOF rule ────────────────────────────────────────────────────────────────
(ozfmt-deftest ozfmt/eof/one-trailing-blank-unchanged
"A file already ending with exactly one blank line is not modified."
"* H
body
"
"* H
body
")
(ozfmt-deftest ozfmt/eof/two-trailing-blanks-trimmed
"Two trailing blank lines (diff 3 regression) are trimmed to one."
"* H
body
"
"* H
body
")
(ozfmt-deftest ozfmt/eof/three-trailing-blanks-trimmed
"Three trailing blank lines are trimmed to one."
"* H
body
"
"* H
body
")
(ozfmt-deftest ozfmt/eof/empty-heading-at-eof-no-trailing-blank
"An empty heading at EOF does not get a trailing blank."
"* Content
body
* EmptyAtEnd
"
"* Content
body
* EmptyAtEnd
")
;;;; ── R2: blank between drawer and body ──────────────────────────────────────
(ozfmt-deftest ozfmt/r2/blank-inserted-after-drawer
"R2: a blank line is inserted between :END: and immediately-following body text."
"* H
:PROPERTIES:
:ID: x
:END:
body text
* Next
"
"* H
:PROPERTIES:
:ID: x
:END:
body text
* Next
")
(ozfmt-deftest ozfmt/r2/already-present-unchanged
"R2: when a blank already follows :END:, nothing changes (idempotent)."
"* H
:PROPERTIES:
:ID: x
:END:
body text
* Next
"
"* H
:PROPERTIES:
:ID: x
:END:
body text
* Next
")
(ozfmt-deftest ozfmt/r2/drawer-only-no-blank
"R2 does not fire when :END: is followed by a heading; R3 supplies the blank."
"* H
:PROPERTIES:
:ID: x
:END:
* Next
"
"* H
:PROPERTIES:
:ID: x
:END:
* Next
")
(ozfmt-deftest ozfmt/r2/planning-drawer-body
"R2: planning line + drawer + body text — blank inserted after :END: before body."
"* H
SCHEDULED: <2026-03-16 Mo>
:PROPERTIES:
:ID: x
:END:
body text
* Next
"
"* H
SCHEDULED: <2026-03-16 Mo>
:PROPERTIES:
:ID: x
:END:
body text
* Next
")
(ozfmt-deftest ozfmt/r2/multiple-drawers-blank-only-after-last
"R2: two consecutive drawers — blank inserted only after the last :END:, not between them."
"* H
:PROPERTIES:
:ID: x
:END:
:LOGBOOK:
- Note
:END:
body text
* Next
"
"* H
:PROPERTIES:
:ID: x
:END:
:LOGBOOK:
- Note
:END:
body text
* Next
")
(ozfmt-deftest ozfmt/r2/drawer-at-eof-no-spurious-blank
"R2: a drawer-only heading at end-of-file gets just the R3+EOF trailing blank — no extra blank after :END:."
"* H
:PROPERTIES:
:ID: x
:END:
"
"* H
:PROPERTIES:
:ID: x
:END:
")
(ozfmt-deftest ozfmt/r2/indented-drawer-followed-by-body
"R2: an indented drawer (org-mode allows leading whitespace before :END:) is recognized via org-element."
"* H
:PROPERTIES:
:ID: x
:END:
body text
* Next
"
"* H
:PROPERTIES:
:ID: x
:END:
body text
* Next
")
;;;; ── R4: planning keyword order ─────────────────────────────────────────────
(ozfmt-deftest ozfmt/r4/reorder-scheduled-deadline
"R4: SCHEDULED before DEADLINE is reordered to DEADLINE then SCHEDULED."
"* NEXT USt Voranmeldung
SCHEDULED: <2026-05-08 Fr +3m> DEADLINE: <2026-05-15 Fr +3m>
:PROPERTIES:
:ID: x
:END:
"
"* NEXT USt Voranmeldung
DEADLINE: <2026-05-15 Fr +3m> SCHEDULED: <2026-05-08 Fr +3m>
:PROPERTIES:
:ID: x
:END:
")
(ozfmt-deftest ozfmt/r4/already-canonical-unchanged
"R4: a planning line already in canonical order is unchanged (idempotent)."
"* H
DEADLINE: <2026-05-15 Fr> SCHEDULED: <2026-05-08 Fr>
body
"
"* H
DEADLINE: <2026-05-15 Fr> SCHEDULED: <2026-05-08 Fr>
body
")
(ozfmt-deftest ozfmt/r4/all-three-keywords-reordered
"R4: SCHEDULED+DEADLINE+CLOSED in arbitrary order → CLOSED DEADLINE SCHEDULED."
"* DONE H
SCHEDULED: <2026-02-26 Do> DEADLINE: <2026-03-01 Su> CLOSED: [2026-03-02 Mo 09:48]
body
"
"* DONE H
CLOSED: [2026-03-02 Mo 09:48] DEADLINE: <2026-03-01 Su> SCHEDULED: <2026-02-26 Do>
body
")
(ozfmt-deftest ozfmt/r4/multi-line-planning-left-alone
"R4: org-mode's grammar allows only a single planning line, so a stray
second `KEYWORD:' line is treated as body text and not merged. Neither
org-mode nor Orgzly generates multi-line planning, so this case does not
arise in practice."
"* H
SCHEDULED: <2026-05-08 Fr>
DEADLINE: <2026-05-15 Fr>
body
"
"* H
SCHEDULED: <2026-05-08 Fr>
DEADLINE: <2026-05-15 Fr>
body
")
(ozfmt-deftest ozfmt/r4/normalize-extra-spaces
"R4: extra whitespace between tokens and after KEYWORD: is collapsed to single spaces."
"* H
SCHEDULED: <2026-05-08 Fr> DEADLINE: <2026-05-15 Fr>
"
"* H
DEADLINE: <2026-05-15 Fr> SCHEDULED: <2026-05-08 Fr>
")
(ozfmt-deftest ozfmt/r4/preserves-indentation
"R4: leading indentation of the planning line is preserved."
"* H
SCHEDULED: <2026-05-08 Fr> DEADLINE: <2026-05-15 Fr>
"
"* H
DEADLINE: <2026-05-15 Fr> SCHEDULED: <2026-05-08 Fr>
")
(ozfmt-deftest ozfmt/r4/timestamp-range-preserved
"R4: a SCHEDULED date range (--) round-trips intact when reordered."
"* H
SCHEDULED: <2026-05-08 Fr>--<2026-05-10 Su> DEADLINE: <2026-05-15 Fr>
"
"* H
DEADLINE: <2026-05-15 Fr> SCHEDULED: <2026-05-08 Fr>--<2026-05-10 Su>
")
(ozfmt-deftest ozfmt/r4/single-keyword-unchanged
"R4: a single SCHEDULED line is left alone (already canonical)."
"* H
SCHEDULED: <2026-05-08 Fr +3m>
body
"
"* H
SCHEDULED: <2026-05-08 Fr +3m>
body
")
(ozfmt-deftest ozfmt/r4/no-planning-info-unchanged
"R4: entries without planning info are not touched."
"* H
body
* Next
"
"* H
body
* Next
")
;;;; ── Idempotency ─────────────────────────────────────────────────────────────
(ert-deftest ozfmt/idempotent/already-formatted-buffer ()
"Running the formatter on a correctly-formatted buffer produces no changes."
(let ((input "* First
SCHEDULED: <2026-03-16 Mo>
:PROPERTIES:
:ID: abc-123
:END:
* Empty
* Last
body text
"))
(let ((result
(let ((org-todo-keywords '((sequence "TODO" "NEXT" "|" "DONE"))))
(with-temp-buffer
(delay-mode-hooks (org-mode))
(insert input)
(orgzly-formatter-buffer)
(buffer-string)))))
(should (equal result input)))))
(ert-deftest ozfmt/idempotent/two-runs-produce-same-output ()
"Two successive formatter runs yield identical output."
(let ((input "* A
content A
* B empty
* C
:PROPERTIES:
:ID: xyz
:END:
body C
"))
(let* ((first-pass
(let ((org-todo-keywords '((sequence "TODO" "NEXT" "|" "DONE"))))
(with-temp-buffer
(delay-mode-hooks (org-mode))
(insert input)
(orgzly-formatter-buffer)
(buffer-string))))
(second-pass
(let ((org-todo-keywords '((sequence "TODO" "NEXT" "|" "DONE"))))
(with-temp-buffer
(delay-mode-hooks (org-mode))
(insert first-pass)
(orgzly-formatter-buffer)
(buffer-string)))))
(should (equal second-pass first-pass)))))
;;;; ── Regression: exact diffs from the bug report ────────────────────────────
(ozfmt-deftest ozfmt/regression/diff1-blank-after-content-section
"Diff 1 regression: Orgzly added \\n after a body content entry."
"* DONE Write skill doc
CLOSED: [2026-03-02 Mo 09:48] SCHEDULED: <2026-02-26 Do>
continue and finish writing down the skill with
claude --resume be0987e1-c562-4aad-a373-a06cb0628d7f
* Next heading
"
"* DONE Write skill doc
CLOSED: [2026-03-02 Mo 09:48] SCHEDULED: <2026-02-26 Do>
continue and finish writing down the skill with
claude --resume be0987e1-c562-4aad-a373-a06cb0628d7f
* Next heading
")
(ozfmt-deftest ozfmt/regression/diff2-blank-after-properties-only
"Diff 2 regression: Orgzly added \\n after :END: in a properties-only entry."
"* TODO Some task
SCHEDULED: <2026-03-16 Mo>
:CREATED: [2026-03-16 Mo 08:30]
:ID: a335e133-26c3-4b3d-a6b5-05dc52790b50
:END:
* Next heading
"
"* TODO Some task
SCHEDULED: <2026-03-16 Mo>
:CREATED: [2026-03-16 Mo 08:30]
:ID: a335e133-26c3-4b3d-a6b5-05dc52790b50
:END:
* Next heading
")
(ozfmt-deftest ozfmt/regression/diff3-extra-trailing-newlines-removed
"Diff 3 regression: Orgzly removed extra trailing newlines."
"* Last heading
Speaker 2
body text here
"
"* Last heading
Speaker 2
body text here
")
;;; orgzly-formatter-test.el ends here