emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
blob 7524d73af2dbc6ba48f65026e3e822628792b228 24619 bytes (raw)
name: testing/lisp/test-ol.el 	 # note: path name is non-authoritative(*)

  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
 
;;; test-ol.el --- Tests for Org Links library       -*- lexical-binding: t; -*-

;; Copyright (C) 2019  Nicolas Goaziou

;; Author: Nicolas Goaziou <mail@nicolasgoaziou.fr>

;; 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 <https://www.gnu.org/licenses/>.

;;; Code:

\f
;;; Decode and Encode Links

(ert-deftest test-ol/encode ()
  "Test `org-link-encode' specifications."
  ;; Regural test.
  (should (string= "Foo%3A%42ar" (org-link-encode "Foo:Bar" '(?\: ?\B))))
  ;; Encode an ASCII character.
  (should (string= "%5B" (org-link-encode "[" '(?\[))))
  ;; Encode an ASCII control character.
  (should (string= "%09" (org-link-encode "\t" '(9))))
  ;; Encode a Unicode multibyte character.
  (should (string= "%E2%82%AC" (org-link-encode "€" '(?\€)))))

(ert-deftest test-ol/decode ()
  "Test `org-link-decode' specifications."
  ;; Decode an ASCII character.
  (should (string= "[" (org-link-decode "%5B")))
  ;; Decode an ASCII control character.
  (should (string= "\n" (org-link-decode "%0A")))
  ;; Decode a Unicode multibyte character.
  (should (string= "€" (org-link-decode "%E2%82%AC"))))

(ert-deftest test-ol/encode-url-with-escaped-char ()
  "Encode and decode a URL that includes an encoded char."
  (should
   (string= "http://some.host.com/form?&id=blah%2Bblah25"
	    (org-link-decode
	     (org-link-encode "http://some.host.com/form?&id=blah%2Bblah25"
			      '(?\s ?\[ ?\] ?%))))))

(ert-deftest test-ol/org-toggle-link-display ()
  "Make sure that `org-toggle-link-display' is working.
See https://github.com/yantar92/org/issues/4."
  (dolist (org-link-descriptive '(nil t))
    (org-test-with-temp-text "* Org link test
[[https://example.com][A link to a site]]"
      (dotimes (_ 2)
        (goto-char 1)
        (re-search-forward "\\[")
        (should-not (org-xor org-link-descriptive (org-invisible-p)))
        (re-search-forward "example")
        (should-not (org-xor org-link-descriptive (org-invisible-p)))
        (re-search-forward "com")
        (should-not (org-xor org-link-descriptive (org-invisible-p)))
        (re-search-forward "]")
        (should-not (org-xor org-link-descriptive (org-invisible-p)))
        (re-search-forward "\\[")
        (should-not (org-invisible-p))
        (re-search-forward "link")
        (should-not (org-invisible-p))
        (re-search-forward "]")
        (should-not (org-xor org-link-descriptive (org-invisible-p)))
        (org-toggle-link-display)))))

\f
;;; Escape and Unescape Links

(ert-deftest test-ol/escape ()
  "Test `org-link-escape' specifications."
  ;; No-op when there is no backslash or square bracket.
  (should (string= "foo" (org-link-escape "foo")))
  ;; Escape square brackets at boundaries of the link.
  (should (string= "\\[foo\\]" (org-link-escape "[foo]")))
  ;; Escape square brackets followed by another square bracket.
  (should (string= "foo\\]\\[bar" (org-link-escape "foo][bar")))
  (should (string= "foo\\]\\]bar" (org-link-escape "foo]]bar")))
  (should (string= "foo\\[\\[bar" (org-link-escape "foo[[bar")))
  (should (string= "foo\\[\\]bar" (org-link-escape "foo[]bar")))
  ;; Escape backslashes at the end of the link.
  (should (string= "foo\\\\" (org-link-escape "foo\\")))
  ;; Escape backslashes that could be confused with escaping
  ;; characters.
  (should (string= "foo\\\\\\]" (org-link-escape "foo\\]")))
  (should (string= "foo\\\\\\]\\[" (org-link-escape "foo\\][")))
  (should (string= "foo\\\\\\]\\]bar" (org-link-escape "foo\\]]bar")))
  ;; Do not escape backslash characters when unnecessary.
  (should (string= "foo\\bar" (org-link-escape "foo\\bar")))
  ;; Pathological cases: consecutive closing square brackets.
  (should (string= "\\[\\[\\[foo\\]\\]\\]" (org-link-escape "[[[foo]]]")))
  (should (string= "\\[\\[foo\\]\\] bar" (org-link-escape "[[foo]] bar"))))

(ert-deftest test-ol/unescape ()
  "Test `org-link-unescape' specifications."
  ;; No-op if there is no backslash.
  (should (string= "foo" (org-link-unescape "foo")))
  ;; No-op if backslashes are not escaping backslashes.
  (should (string= "foo\\bar" (org-link-unescape "foo\\bar")))
  ;; Unescape backslashes before square brackets.
  (should (string= "foo]bar" (org-link-unescape "foo\\]bar")))
  (should (string= "foo\\]" (org-link-unescape "foo\\\\\\]")))
  (should (string= "foo\\][" (org-link-unescape "foo\\\\\\][")))
  (should (string= "foo\\]]bar" (org-link-unescape "foo\\\\\\]\\]bar")))
  (should (string= "foo\\[[bar" (org-link-unescape "foo\\\\\\[\\[bar")))
  (should (string= "foo\\[]bar" (org-link-unescape "foo\\\\\\[\\]bar")))
  ;; Unescape backslashes at the end of the link.
  (should (string= "foo\\" (org-link-unescape "foo\\\\")))
  ;; Unescape closing square bracket at boundaries of the link.
  (should (string= "[foo]" (org-link-unescape "\\[foo\\]")))
  ;; Pathological cases: consecutive closing square brackets.
  (should (string= "[[[foo]]]" (org-link-unescape "\\[\\[\\[foo\\]\\]\\]")))
  (should (string= "[[foo]] bar" (org-link-unescape "\\[\\[foo\\]\\] bar"))))

(ert-deftest test-ol/make-string ()
  "Test `org-link-make-string' specifications."
  ;; Throw an error on empty URI.
  (should-error (org-link-make-string ""))
  ;; Empty description returns a [[URI]] construct.
  (should (string= "[[uri]]"(org-link-make-string "uri")))
  ;; Non-empty description returns a [[URI][DESCRIPTION]] construct.
  (should
   (string= "[[uri][description]]"
	    (org-link-make-string "uri" "description")))
  ;; Escape "]]" strings in the description with zero-width spaces.
  (should
   (let ((zws (string ?\x200B)))
     (string= (format "[[uri][foo]%s]bar]]" zws)
	      (org-link-make-string "uri" "foo]]bar"))))
  ;; Prevent description from ending with a closing square bracket
  ;; with a zero-width space.
  (should
   (let ((zws (string ?\x200B)))
     (string= (format "[[uri][foo]%s]]" zws)
	      (org-link-make-string "uri" "foo]")))))

\f
;;; Store links

(ert-deftest test-ol/store-link ()
  "Test `org-store-link' specifications."
  ;; On a headline, link to that headline.  Use heading as the
  ;; description of the link.
  (should
   (let (org-store-link-props org-stored-links)
     (org-test-with-temp-text-in-file "* H1"
       (let ((file (buffer-file-name)))
	 (equal (format "[[file:%s::*H1][H1]]" file)
		(org-store-link nil))))))
  ;; On a headline, remove TODO and COMMENT keywords, priority cookie,
  ;; and tags.
  (should
   (let (org-store-link-props org-stored-links)
     (org-test-with-temp-text-in-file "* TODO H1"
       (let ((file (buffer-file-name)))
	 (equal (format "[[file:%s::*H1][H1]]" file)
		(org-store-link nil))))))
  (should
   (let (org-store-link-props org-stored-links)
     (org-test-with-temp-text-in-file "* COMMENT H1"
       (let ((file (buffer-file-name)))
	 (equal (format "[[file:%s::*H1][H1]]" file)
		(org-store-link nil))))))
  (should
   (let (org-store-link-props org-stored-links)
     (org-test-with-temp-text-in-file "* [#A] H1"
       (let ((file (buffer-file-name)))
	 (equal (format "[[file:%s::*H1][H1]]" file)
		(org-store-link nil))))))
  (should
   (let (org-store-link-props org-stored-links)
     (org-test-with-temp-text-in-file "* H1 :tag:"
       (let ((file (buffer-file-name)))
	 (equal (format "[[file:%s::*H1][H1]]" file)
		(org-store-link nil))))))
  ;; On a headline, remove any link from description.
  (should
   (let (org-store-link-props org-stored-links)
     (org-test-with-temp-text-in-file "* [[#l][d]]"
       (let ((file (buffer-file-name)))
	 (equal (format "[[file:%s::*%s][d]]"
			file
			(org-link-escape "[[#l][d]]"))
		(org-store-link nil))))))
  (should
   (let (org-store-link-props org-stored-links)
     (org-test-with-temp-text-in-file "* [[l]]"
       (let ((file (buffer-file-name)))
	 (equal (format "[[file:%s::*%s][l]]" file (org-link-escape "[[l]]"))
		(org-store-link nil))))))
  (should
   (let (org-store-link-props org-stored-links)
     (org-test-with-temp-text-in-file "* [[l1][d1]] [[l2][d2]]"
       (let ((file (buffer-file-name)))
	 (equal (format "[[file:%s::*%s][d1 d2]]"
			file
			(org-link-escape "[[l1][d1]] [[l2][d2]]"))
		(org-store-link nil))))))
  ;; On a named element, link to that element.
  (should
   (let (org-store-link-props org-stored-links)
     (org-test-with-temp-text-in-file "#+NAME: foo\nParagraph"
       (let ((file (buffer-file-name)))
	 (equal (format "[[file:%s::foo][foo]]" file)
		(org-store-link nil))))))
  ;; Store link to Org buffer, with context.
  (should
   (let ((org-stored-links nil)
	 (org-id-link-to-org-use-id nil)
	 (org-context-in-file-links t))
     (org-test-with-temp-text-in-file "* h1"
       (let ((file (buffer-file-name)))
	 (equal (format "[[file:%s::*h1][h1]]" file)
		(org-store-link nil))))))
  ;; Store link to Org buffer, without context.
  (should
   (let ((org-stored-links nil)
	 (org-id-link-to-org-use-id nil)
	 (org-context-in-file-links nil))
     (org-test-with-temp-text-in-file "* h1"
       (let ((file (buffer-file-name)))
	 (equal (format "[[file:%s][file:%s]]" file file)
		(org-store-link nil))))))
  ;; C-u prefix reverses `org-context-in-file-links' in Org buffer.
  (should
   (let ((org-stored-links nil)
	 (org-id-link-to-org-use-id nil)
	 (org-context-in-file-links nil))
     (org-test-with-temp-text-in-file "* h1"
       (let ((file (buffer-file-name)))
	 (equal (format "[[file:%s::*h1][h1]]" file)
		(org-store-link '(4)))))))
  ;; A C-u C-u does *not* reverse `org-context-in-file-links' in Org
  ;; buffer.
  (should
   (let ((org-stored-links nil)
	 (org-id-link-to-org-use-id nil)
	 (org-context-in-file-links nil))
     (org-test-with-temp-text-in-file "* h1"
       (let ((file (buffer-file-name)))
	 (equal (format "[[file:%s][file:%s]]" file file)
		(org-store-link '(16)))))))
  ;; Store file link to non-Org buffer, with context.
  (should
   (let ((org-stored-links nil)
	 (org-link-context-for-files t))
     (org-test-with-temp-text-in-file "one\n<point>two"
       (fundamental-mode)
       (let ((file (buffer-file-name)))
	 (equal (format "[[file:%s::two]]" file)
		(org-store-link nil))))))
  ;; Store file link to non-Org buffer, without context.
  (should
   (let ((org-stored-links nil)
	 (org-context-in-file-links nil))
     (org-test-with-temp-text-in-file "one\n<point>two"
       (fundamental-mode)
       (let ((file (buffer-file-name)))
	 (equal (format "[[file:%s][file:%s]]" file file)
		(org-store-link nil))))))
  ;; C-u prefix reverses `org-context-in-file-links' in non-Org
  ;; buffer.
  (should
   (let ((org-stored-links nil)
	 (org-link-context-for-files nil))
     (org-test-with-temp-text-in-file "one\n<point>two"
       (fundamental-mode)
       (let ((file (buffer-file-name)))
	 (equal (format "[[file:%s::two]]" file)
		(org-store-link '(4)))))))
  ;; A C-u C-u does *not* reverse `org-context-in-file-links' in
  ;; non-Org buffer.
  (should
   (let ((org-stored-links nil)
	 (org-context-in-file-links nil))
     (org-test-with-temp-text-in-file "one\n<point>two"
       (fundamental-mode)
       (let ((file (buffer-file-name)))
	 (equal (format "[[file:%s][file:%s]]" file file)
		(org-store-link '(16)))))))
  ;; Context does not include special search syntax.
  (should
   (let ((org-stored-links nil)
	 (org-context-in-file-links t))
     (org-test-with-temp-text-in-file "(two)"
       (fundamental-mode)
       (let ((file (buffer-file-name)))
	 (equal (format "[[file:%s::two]]" file file)
		(org-store-link nil))))))
  (should
   (let ((org-stored-links nil)
	 (org-context-in-file-links t))
     (org-test-with-temp-text-in-file "# two"
       (fundamental-mode)
       (let ((file (buffer-file-name)))
	 (equal (format "[[file:%s::two]]" file file)
		(org-store-link nil))))))
  (should
   (let ((org-stored-links nil)
	 (org-context-in-file-links t))
     (org-test-with-temp-text-in-file "*two"
       (fundamental-mode)
       (let ((file (buffer-file-name)))
	 (equal (format "[[file:%s::two]]" file file)
		(org-store-link nil))))))
  (should
   (let ((org-stored-links nil)
	 (org-context-in-file-links t))
     (org-test-with-temp-text-in-file "( two )"
       (fundamental-mode)
       (let ((file (buffer-file-name)))
	 (equal (format "[[file:%s::two]]" file file)
		(org-store-link nil))))))
  (should
   (let ((org-stored-links nil)
	 (org-context-in-file-links t))
     (org-test-with-temp-text-in-file "# two"
       (fundamental-mode)
       (let ((file (buffer-file-name)))
	 (equal (format "[[file:%s::two]]" file file)
		(org-store-link nil))))))
  (should
   (let ((org-stored-links nil)
	 (org-context-in-file-links t))
     (org-test-with-temp-text-in-file "#( two )"
       (fundamental-mode)
       (let ((file (buffer-file-name)))
	 (equal (format "[[file:%s::two]]" file file)
		(org-store-link nil))))))
  (should
   (let ((org-stored-links nil)
	 (org-context-in-file-links t))
     (org-test-with-temp-text-in-file "#** ((## two) )"
       (fundamental-mode)
       (let ((file (buffer-file-name)))
	 (equal (format "[[file:%s::two]]" file file)
		(org-store-link nil))))))
  (should-not
   (let ((org-stored-links nil)
	 (org-context-in-file-links t))
     (org-test-with-temp-text-in-file "(two"
       (fundamental-mode)
       (let ((file (buffer-file-name)))
	 (equal (format "[[file:%s::two]]" file file)
		(org-store-link nil))))))
  ;; Context also ignore statistics cookies and special headlines
  ;; data.
  (should
   (let ((org-stored-links nil)
	 (org-context-in-file-links t))
     (org-test-with-temp-text-in-file "* TODO [#A] COMMENT foo :bar:"
       (let ((file (buffer-file-name)))
	 (equal (format "[[file:%s::*foo][foo]]" file file)
		(org-store-link nil))))))
  (should
   (let ((org-stored-links nil)
	 (org-context-in-file-links t))
     (org-test-with-temp-text-in-file "* foo[33%]bar"
       (let ((file (buffer-file-name)))
	 (equal (format "[[file:%s::*foo bar][foo bar]]" file file)
		(org-store-link nil))))))
  (should
   (let ((org-stored-links nil)
	 (org-context-in-file-links t))
     (org-test-with-temp-text-in-file "* [%][/]  foo [35%] bar[3/5]"
       (let ((file (buffer-file-name)))
	 (equal (format "[[file:%s::*foo bar][foo bar]]" file file)
		(org-store-link nil)))))))

\f
;;; Radio Targets

(ert-deftest test-ol/update-radio-target-regexp ()
  "Test `org-update-radio-target-regexp' specifications."
  ;; Properly update cache with no previous radio target regexp.
  (should
   (eq 'link
       (org-test-with-temp-text "radio\n\nParagraph\n\nradio"
	 (save-excursion (goto-char (point-max)) (org-element-context))
	 (insert "<<<")
	 (search-forward "o")
	 (insert ">>>")
	 (org-update-radio-target-regexp)
	 (goto-char (point-max))
	 (org-element-type (org-element-context)))))
  ;; Properly update cache with previous radio target regexp.
  (should
   (eq 'link
       (org-test-with-temp-text "radio\n\nParagraph\n\nradio"
	 (save-excursion (goto-char (point-max)) (org-element-context))
	 (insert "<<<")
	 (search-forward "o")
	 (insert ">>>")
	 (org-update-radio-target-regexp)
	 (search-backward "r")
	 (delete-char 5)
	 (insert "new")
	 (org-update-radio-target-regexp)
	 (goto-char (point-max))
	 (delete-region (line-beginning-position) (point))
	 (insert "new")
	 (org-element-type (org-element-context))))))

\f
;;; Navigation

(ert-deftest test-ol/next-link ()
  "Test `org-next-link' specifications."
  ;; Move to any type of link.
  (should
   (equal "[[link]]"
	  (org-test-with-temp-text "foo [[link]]"
	    (org-next-link)
	    (buffer-substring (point) (line-end-position)))))
  (should
   (equal "http://link"
	  (org-test-with-temp-text "foo http://link"
	    (org-next-link)
	    (buffer-substring (point) (line-end-position)))))
  (should
   (equal "<http://link>"
	  (org-test-with-temp-text "foo <http://link>"
	    (org-next-link)
	    (buffer-substring (point) (line-end-position)))))
  ;; Ignore link at point.
  (should
   (equal "[[link2]]"
	  (org-test-with-temp-text "[[link1]] [[link2]]"
	    (org-next-link)
	    (buffer-substring (point) (line-end-position)))))
  ;; Ignore fake links.
  (should
   (equal "[[truelink]]"
	  (org-test-with-temp-text "foo\n: [[link]]\n[[truelink]]"
	    (org-next-link)
	    (buffer-substring (point) (line-end-position)))))
  ;; Do not move point when there is no link.
  (should
   (org-test-with-temp-text "foo bar"
     (org-next-link)
     (bobp)))
  ;; Wrap around after a failed search.
  (should
   (equal "[[link]]"
	  (org-test-with-temp-text "[[link]]\n<point>foo"
	    (org-next-link)
	    (let* ((this-command 'org-next-link)
		   (last-command this-command))
	      (org-next-link))
	    (buffer-substring (point) (line-end-position)))))
  ;; Find links with item tags.
  (should
   (equal "[[link1]]"
	  (org-test-with-temp-text "- tag [[link1]] :: description"
	    (org-next-link)
	    (buffer-substring (point) (search-forward "]]" nil t))))))

(ert-deftest test-ol/previous-link ()
  "Test `org-previous-link' specifications."
  ;; Move to any type of link.
  (should
   (equal "[[link]]"
	  (org-test-with-temp-text "[[link]]\nfoo<point>"
	    (org-previous-link)
	    (buffer-substring (point) (line-end-position)))))
  (should
   (equal "http://link"
	  (org-test-with-temp-text "http://link\nfoo<point>"
	    (org-previous-link)
	    (buffer-substring (point) (line-end-position)))))
  (should
   (equal "<http://link>"
	  (org-test-with-temp-text "<http://link>\nfoo<point>"
	    (org-previous-link)
	    (buffer-substring (point) (line-end-position)))))
  ;; Ignore link at point.
  (should
   (equal "[[link1]]"
	  (org-test-with-temp-text "[[link1]]\n[[link2<point>]]"
	    (org-previous-link)
	    (buffer-substring (point) (line-end-position)))))
  (should
   (equal "[[link1]]"
	  (org-test-with-temp-text "line\n[[link1]]\n[[link2<point>]]"
	    (org-previous-link)
	    (buffer-substring (point) (line-end-position)))))
  ;; Ignore fake links.
  (should
   (equal "[[truelink]]"
	  (org-test-with-temp-text "[[truelink]]\n: [[link]]\n<point>"
	    (org-previous-link)
	    (buffer-substring (point) (line-end-position)))))
  ;; Do not move point when there is no link.
  (should
   (org-test-with-temp-text "foo bar<point>"
     (org-previous-link)
     (eobp)))
  ;; Wrap around after a failed search.
  (should
   (equal "[[link]]"
	  (org-test-with-temp-text "foo\n[[link]]"
	    (org-previous-link)
	    (let* ((this-command 'org-previous-link)
		   (last-command this-command))
	      (org-previous-link))
	    (buffer-substring (point) (line-end-position))))))

\f
;;; Link regexps


(defmacro test-ol-parse-link-in-text (text)
  "Return list of :type and :path of link parsed in TEXT.
\"<point>\" string must be at the beginning of the link to be parsed."
  (declare (indent 1))
  `(org-test-with-temp-text ,text
     (list (org-element-property :type (org-element-link-parser))
           (org-element-property :path (org-element-link-parser)))))

(ert-deftest test-ol/plain-link-re ()
  "Test `org-link-plain-re'."
  (should
   (equal
    '("https" "//example.com")
    (test-ol-parse-link-in-text
        "(<point>https://example.com)")))
  (should
   (equal
    '("https" "//example.com/qwe()")
    (test-ol-parse-link-in-text
        "(Some text <point>https://example.com/qwe())")))
  (should
   (equal
    '("https" "//doi.org/10.1016/0160-791x(79)90023-x")
    (test-ol-parse-link-in-text
        "<point>https://doi.org/10.1016/0160-791x(79)90023-x")))
  (should
   (equal
    '("file" "aa")
    (test-ol-parse-link-in-text
        "The <point>file:aa link")))
  (should
   (equal
    '("file" "a(b)c")
    (test-ol-parse-link-in-text
        "The <point>file:a(b)c link")))
  (should
   (equal
    '("file" "a()")
    (test-ol-parse-link-in-text
        "The <point>file:a() link")))
  (should
   (equal
    '("file" "aa((a))")
    (test-ol-parse-link-in-text
        "The <point>file:aa((a)) link")))
  (should
   (equal
    '("file" "aa(())")
    (test-ol-parse-link-in-text
        "The <point>file:aa(()) link")))
  (should
   (equal
    '("file" "/a")
    (test-ol-parse-link-in-text
        "The <point>file:/a link")))
  (should
   (equal
    '("file" "/a/")
    (test-ol-parse-link-in-text
        "The <point>file:/a/ link")))
  (should
   (equal
    '("http" "//")
    (test-ol-parse-link-in-text
        "The <point>http:// link")))
  (should
   (equal
    '("file" "ab")
    (test-ol-parse-link-in-text
        "The (some <point>file:ab) link")))
  (should
   (equal
    '("file" "aa")
    (test-ol-parse-link-in-text
        "The <point>file:aa) link")))
  (should
   (equal
    '("file" "aa")
    (test-ol-parse-link-in-text
        "The <point>file:aa( link")))
  (should
   (equal
    '("http" "//foo.com/more_(than)_one_(parens)")
    (test-ol-parse-link-in-text
        "The <point>http://foo.com/more_(than)_one_(parens) link")))
  (should
   (equal
    '("http" "//foo.com/blah_(wikipedia)#cite-1")
    (test-ol-parse-link-in-text
        "The <point>http://foo.com/blah_(wikipedia)#cite-1 link")))
  (should
   (equal
    '("http" "//foo.com/blah_(wikipedia)_blah#cite-1")
    (test-ol-parse-link-in-text
        "The <point>http://foo.com/blah_(wikipedia)_blah#cite-1 link")))
  (should
   (equal
    '("http" "//foo.com/unicode_(✪)_in_parens")
    (test-ol-parse-link-in-text
        "The <point>http://foo.com/unicode_(✪)_in_parens link")))
  (should
   (equal
    '("http" "//foo.com/(something)?after=parens")
    (test-ol-parse-link-in-text
        "The <point>http://foo.com/(something)?after=parens link"))))

;;; Insert Links

(defmacro test-ol-with-link-parameters-as (type parameters &rest body)
  "Pass TYPE/PARAMETERS to `org-link-parameters' and execute BODY.

Save the original value of `org-link-parameters', execute
`org-link-set-parameters' with the relevant args, execute BODY
and restore `org-link-parameters'.

TYPE is as in `org-link-set-parameters'.  PARAMETERS is a plist to
be passed to `org-link-set-parameters'."
  `(progn
     (setq orig-parameters org-link-parameters)
     (org-link-set-parameters ,type ,@parameters)
     (let ((rtn (progn ,@body)))
       (setq org-link-parameters orig-parameters)
       rtn)))

(defun test-ol-insert-link-get-desc (&optional link-location description)
  "Insert link in temp buffer, return description.

LINK-LOCATION and DESCRIPTION are passed to
`org-insert-link' (COMPLETE-FILE is always nil)."
  (org-test-with-temp-text ""
    (org-insert-link nil link-location description)
    (save-match-data
      (when (and
             (org-in-regexp org-link-bracket-re 1)
             (match-end 2))
        (match-string-no-properties 2)))))

(defun test-ol/return-foobar (_link-test _desc)
  "Return string \"foobar\".

Take (and ignore) arguments conforming to `:default-description'
API in `org-link-parameters'.  Used in test
`test-ol/insert-link-default-description', for the case where
`:default-description' is a function symbol."
  "foobar")

(ert-deftest test-ol/insert-link-default-description ()
  "Test `:default-description' parameter handling."
  ;; String case
  (should
   (string=
    "foobar"
    (test-ol-with-link-parameters-as
     "id" (:default-description "foobar")
     (test-ol-insert-link-get-desc "id:foo-bar"))))
  ;; Lambda case
  (should
   (string=
    "foobar"
    (test-ol-with-link-parameters-as
     "id" (:default-description (lambda (_link-test _desc) "foobar"))
     (test-ol-insert-link-get-desc "id:foo-bar"))))
  ;; Function symbol case
  (should
   (string=
    "foobar"
    (test-ol-with-link-parameters-as
     "id" (:default-description #'test-ol/return-foobar)
     (test-ol-insert-link-get-desc "id:foo-bar"))))
  ;; `:default-description' parameter is defined, but doesn't return a
  ;; string
  (should-error
   (test-ol-with-link-parameters-as
    "id" (:default-description #'ignore)
    (test-ol-insert-link-get-desc "id:foo-bar")))
  ;; Description argument should override `:default-description'
  (should
   (string=
    "notfoobar"
    (test-ol-with-link-parameters-as
     "id" (:default-description "foobar")
     (test-ol-insert-link-get-desc "id:foo-bar" "notfoobar")))))

(provide 'test-ol)
;;; test-ol.el ends here

debug log:

solving 7524d73af ...
found 7524d73af in https://list.orgmode.org/orgmode/87zgi6fckr.fsf@heagren.com/
found 429bb52ee in https://git.savannah.gnu.org/cgit/emacs/org-mode.git
preparing index
index prepared:
100644 429bb52ee95f5da85ce2202c85184480b8d1d51c	testing/lisp/test-ol.el

applying [1/1] https://list.orgmode.org/orgmode/87zgi6fckr.fsf@heagren.com/
diff --git a/testing/lisp/test-ol.el b/testing/lisp/test-ol.el
index 429bb52ee..7524d73af 100644

Checking patch testing/lisp/test-ol.el...
Applied patch testing/lisp/test-ol.el cleanly.

index at:
100644 7524d73af2dbc6ba48f65026e3e822628792b228	testing/lisp/test-ol.el

(*) Git path names are given by the tree(s) the blob belongs to.
    Blobs themselves have no identifier aside from the hash of its contents.^

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).