emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
blob f34cc7b772984bff44b8d74e3bca22e04950b597 22755 bytes (raw)

  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
 
;;; test-org-export.el --- Tests for org-export.el

;; Copyright (C) 2012  Nicolas Goaziou

;; Author: Nicolas Goaziou <n.goaziou at gmail dot com>

;; Released under the GNU General Public License version 3
;; see: http://www.gnu.org/licenses/gpl-3.0.html

;;;; Comments


\f
;;; Code:

(unless (featurep 'org-export)
  (signal 'missing-test-dependency "org-export"))


\f
;;; Tests

(defmacro org-test-with-backend (backend &rest body)
  "Execute body with an export back-end defined.

BACKEND is the name, as a string, of the back-end.  BODY is the
body to execute.  The defined back-end simply returns parsed data
as Org syntax."
  (declare (debug (form body)) (indent 1))
  `(flet ,(let (transcoders)
	    (dolist (type (append org-element-all-elements
				  org-element-all-objects)
			  transcoders)
	      (push `(,(intern (format "org-%s-%s" backend type))
		      (obj contents info)
		      (,(intern (format "org-element-%s-interpreter" type))
		       obj contents))
		    transcoders)))
     ,@body))

(ert-deftest test-org-export/parse-option-keyword ()
  "Test reading all standard #+OPTIONS: items."
  (should
   (equal
    (org-export-parse-option-keyword
     "H:1 num:t \\n:t timestamp:t arch:t author:t creator:t d:t email:t
 *:t e:t ::t f:t pri:t -:t ^:t toc:t |:t tags:t tasks:t <:t todo:t")
    '(:headline-levels
      1 :preserve-breaks t :section-numbers t :time-stamp-file t
      :with-archived-trees t :with-author t :with-creator t :with-drawers t
      :with-email t :with-emphasize t :with-entities t :with-fixed-width t
      :with-footnotes t :with-priority t :with-special-strings t
      :with-sub-superscript t :with-toc t :with-tables t :with-tags t
      :with-tasks t :with-timestamps t :with-todo-keywords t)))
  ;; Test some special values.
  (should
   (equal
    (org-export-parse-option-keyword
     "arch:headline creator:comment d:(\"TEST\")
 ^:{} toc:1 tags:not-in-toc tasks:todo num:2")
    '( :section-numbers
       2
       :with-archived-trees headline :with-creator comment
       :with-drawers ("TEST") :with-sub-superscript {} :with-toc 1
       :with-tags not-in-toc :with-tasks todo))))

(ert-deftest test-org-export/get-inbuffer-options ()
  "Test reading all standard export keywords."
  (should
   (equal
    (org-test-with-temp-text "#+AUTHOR: Me, Myself and I
#+CREATOR: Idem
#+DATE: Today
#+DESCRIPTION: Testing
#+DESCRIPTION: with two lines
#+EMAIL: some@email.org
#+EXPORT_EXCLUDE_TAGS: noexport invisible
#+KEYWORDS: test
#+LANGUAGE: en
#+EXPORT_SELECT_TAGS: export
#+TITLE: Some title
#+TITLE: with spaces"
      (org-export-get-inbuffer-options))
    '(:author
      "Me, Myself and I" :creator "Idem" :date "Today"
      :description "Testing\nwith two lines" :email "some@email.org"
      :exclude-tags ("noexport" "invisible") :keywords "test" :language "en"
      :select-tags ("export") :title "Some title with spaces"))))

(ert-deftest test-org-export/define-macro ()
  "Try defining various Org macro using in-buffer #+MACRO: keyword."
  ;; Parsed macro.
  (should (equal (org-test-with-temp-text "#+MACRO: one 1"
		   (org-export-get-inbuffer-options))
		 '(:macro-one ("1"))))
  ;; Evaled macro.
  (should (equal (org-test-with-temp-text "#+MACRO: two (eval (+ 1 1))"
		   (org-export-get-inbuffer-options))
		 '(:macro-two "(eval (+ 1 1))")))
  ;; Incomplete macro.
  (should-not (org-test-with-temp-text "#+MACRO: three"
		(org-export-get-inbuffer-options)))
  ;; Macro with newline character.
  (should (equal (org-test-with-temp-text "#+MACRO: four a\\nb"
		   (org-export-get-inbuffer-options))
		 '(:macro-four ("a\nb"))))
  ;; Macro with protected newline character.
  (should (equal (org-test-with-temp-text "#+MACRO: five a\\\\nb"
		   (org-export-get-inbuffer-options))
		 '(:macro-five ("a\\nb"))))
  ;; Recursive macro.
  (org-test-with-temp-text "#+MACRO: six 6\n#+MACRO: seven 1 + {{{six}}}"
    (should
     (equal
      (org-export-get-inbuffer-options)
      '(:macro-six
	("6")
	:macro-seven
	("1 + " (macro (:key "six" :value "{{{six}}}" :args nil :begin 5 :end 14
			     :post-blank 0))))))))

(ert-deftest test-org-export/handle-options ()
  "Test if export options have an impact on output."
  ;; Test exclude tags.
  (org-test-with-temp-text "* Head1 :noexport:"
    (org-test-with-backend "test"
      (should
       (equal (org-export-as 'test nil nil nil '(:exclude-tags ("noexport")))
	      ""))))
  ;; Test include tags.
  (org-test-with-temp-text "
* Head1
** Sub-Head1.1 :export:
*** Sub-Head1.1.1
* Head2"
    (org-test-with-backend "test"
      (should
       (string-match
	"\\* Head1\n\\*\\* Sub-Head1.1[ \t]+:export:\n\\*\\*\\* Sub-Head1.1.1\n"
	(org-export-as 'test nil nil nil '(:select-tags ("export")))))))
  ;; Test mixing include tags and exclude tags.
  (org-test-with-temp-text "
* Head1 :export:
** Sub-Head1 :noexport:
** Sub-Head2
* Head2 :noexport:
** Sub-Head1 :export:"
    (org-test-with-backend "test"
      (should
       (string-match
	"\\* Head1[ \t]+:export:\n\\*\\* Sub-Head2\n"
	(org-export-as
	 'test nil nil nil
	 '(:select-tags ("export") :exclude-tags ("noexport")))))))
  ;; Ignore tasks.
  (let ((org-todo-keywords '((sequence "TODO" "DONE"))))
    (org-test-with-temp-text "* TODO Head1"
      (org-test-with-backend "test"
	(should (equal (org-export-as 'test nil nil nil '(:with-tasks nil))
		       "")))))
  (let ((org-todo-keywords '((sequence "TODO" "DONE"))))
    (org-test-with-temp-text "* TODO Head1"
      (org-test-with-backend "test"
	(should (equal (org-export-as 'test nil nil nil '(:with-tasks t))
		       "* TODO Head1\n")))))
  ;; Archived tree.
  (org-test-with-temp-text "* Head1 :archive:"
    (let ((org-archive-tag "archive"))
      (org-test-with-backend "test"
	(should
	 (equal (org-export-as 'test nil nil nil '(:with-archived-trees nil))
		"")))))
  (org-test-with-temp-text "* Head1 :archive:\nbody\n** Sub-head 2"
    (let ((org-archive-tag "archive"))
      (org-test-with-backend "test"
	(should
	 (string-match
	  "\\* Head1[ \t]+:archive:"
	  (org-export-as 'test nil nil nil
			 '(:with-archived-trees headline)))))))
  (org-test-with-temp-text "* Head1 :archive:"
    (let ((org-archive-tag "archive"))
      (org-test-with-backend "test"
	(should
	 (string-match
	  "\\`\\* Head1[ \t]+:archive:\n\\'"
	  (org-export-as 'test nil nil nil '(:with-archived-trees t)))))))
  ;; Drawers.
  (let ((org-drawers '("TEST")))
    (org-test-with-temp-text ":TEST:\ncontents\n:END:"
      (org-test-with-backend "test"
	(should (equal (org-export-as 'test nil nil nil '(:with-drawers nil))
		       "")))))
  (let ((org-drawers '("TEST")))
    (org-test-with-temp-text ":TEST:\ncontents\n:END:"
      (org-test-with-backend "test"
	(should (equal (org-export-as 'test nil nil nil '(:with-drawers t))
		       ":TEST:\ncontents\n:END:\n"))))))

(ert-deftest test-org-export/comment-tree ()
  "Test if export process ignores commented trees."
  (let ((org-comment-string "COMMENT"))
    (org-test-with-temp-text "* COMMENT Head1"
      (org-test-with-backend "test"
	(should (equal (org-export-as 'test) ""))))))

(ert-deftest test-org-export/export-scope ()
  "Test all export scopes."
  (org-test-with-temp-text "
* Head1
** Head2
text
*** Head3"
    (org-test-with-backend "test"
      ;; Subtree.
      (forward-line 3)
      (should (equal (org-export-as 'test 'subtree) "text\n*** Head3\n"))
      ;; Visible.
      (goto-char (point-min))
      (forward-line)
      (org-cycle)
      (should (equal (org-export-as 'test nil 'visible) "* Head1\n"))
      ;; Body only.
      (flet ((org-test-template (body info) (format "BEGIN\n%sEND" body)))
	(should (equal (org-export-as 'test nil nil 'body-only)
		       "* Head1\n** Head2\ntext\n*** Head3\n"))
	(should (equal (org-export-as 'test)
		       "BEGIN\n* Head1\n** Head2\ntext\n*** Head3\nEND")))
      ;; Region.
      (goto-char (point-min))
      (forward-line 3)
      (transient-mark-mode 1)
      (push-mark (point) t t)
      (goto-char (point-at-eol))
      (should (equal (org-export-as 'test) "text\n")))))

(ert-deftest test-org-export/export-snippet ()
  "Test export snippets transcoding."
  (org-test-with-temp-text "@test{A}@t{B}"
    (org-test-with-backend "test"
      (flet ((org-test-export-snippet
	      (snippet contents info)
	      (when (eq (org-export-snippet-backend snippet) 'test)
		(org-element-property :value snippet))))
	(let ((org-export-snippet-translation-alist nil))
	  (should (equal (org-export-as 'test) "A\n")))
	(let ((org-export-snippet-translation-alist '(("t" . "test"))))
	  (should (equal (org-export-as 'test) "AB\n")))))))

(ert-deftest test-org-export/expand-include ()
  "Test file inclusion in an Org buffer."
  ;; Full insertion with recursive inclusion.
  (org-test-with-temp-text
      (format "#+INCLUDE: \"%s/examples/include.org\"" org-test-dir)
    (org-export-expand-include-keyword)
    (should (equal (buffer-string)
		   "Small Org file with an include keyword.

#+BEGIN_SRC emacs-lisp :exports results\n(+ 2 1)\n#+END_SRC

Success!

* Heading
body\n")))
  ;; Localized insertion.
  (org-test-with-temp-text
      (format "#+INCLUDE: \"%s/examples/include.org\" :lines \"1-2\""
	      org-test-dir)
    (org-export-expand-include-keyword)
    (should (equal (buffer-string)
		   "Small Org file with an include keyword.\n")))
  ;; Insertion with constraints on headlines level.
  (org-test-with-temp-text
      (format
       "* Top heading\n#+INCLUDE: \"%s/examples/include.org\" :lines \"9-\""
       org-test-dir)
    (org-export-expand-include-keyword)
    (should (equal (buffer-string) "* Top heading\n** Heading\nbody\n")))
  ;; Inclusion within an example block.
  (org-test-with-temp-text
      (format "#+INCLUDE: \"%s/examples/include.org\" :lines \"1-2\" example"
	      org-test-dir)
    (org-export-expand-include-keyword)
    (should
     (equal
      (buffer-string)
      "#+BEGIN_EXAMPLE\nSmall Org file with an include keyword.\n#+END_EXAMPLE\n")))
  ;; Inclusion within a src-block.
  (org-test-with-temp-text
      (format
       "#+INCLUDE: \"%s/examples/include.org\" :lines \"4-5\" src emacs-lisp"
       org-test-dir)
    (org-export-expand-include-keyword)
    (should (equal (buffer-string)
		   "#+BEGIN_SRC emacs-lisp\n(+ 2 1)\n#+END_SRC\n"))))

(ert-deftest test-org-export/user-ignore-list ()
  "Test if `:ignore-list' accepts user input."
  (org-test-with-backend "test"
    (flet ((skip-note-head
	    (data backend info)
	    ;; Ignore headlines with the word "note" in their title.
	    (org-element-map
	     data 'headline
	     (lambda (headline)
	       (when (string-match "\\<note\\>"
				   (org-element-property :raw-value headline))
		 (org-export-ignore-element headline info)))
	     info)
	    data))
      ;; Install function in parse tree filters.
      (let ((org-export-filter-parse-tree-functions '(skip-note-head)))
	(org-test-with-temp-text "* Head1\n* Head2 (note)\n"
	  (should (equal (org-export-as 'test) "* Head1\n")))))))


\f
;; Footnotes

(ert-deftest test-org-export/footnotes ()
  "Test footnotes specifications."
  (let ((org-footnote-section nil))
    ;; 1. Read every type of footnote.
    (org-test-with-temp-text
	"Text[fn:1] [1] [fn:label:C] [fn::D]\n\n[fn:1] A\n\n[1] B"
      (let* ((tree (org-element-parse-buffer))
	     (info (org-combine-plists
		    (org-export-initial-options) '(:with-footnotes t))))
	(setq info (org-combine-plists
		    info (org-export-collect-tree-properties tree info 'test)))
	(should
	 (equal
	  '((1 . "A") (2 . "B") (3 . "C") (4 . "D"))
	  (org-element-map
	   tree 'footnote-reference
	   (lambda (ref)
	     (let ((def (org-export-get-footnote-definition ref info)))
	       (cons (org-export-get-footnote-number ref info)
		     (if (eq (org-element-property :type ref) 'inline) (car def)
		       (car (org-element-contents
			     (car (org-element-contents def))))))))
	   info)))))
    ;; 2. Test nested footnotes order.
    (org-test-with-temp-text
	"Text[fn:1:A[fn:2]] [fn:3].\n\n[fn:2] B [fn:3] [fn::D].\n\n[fn:3] C."
      (let* ((tree (org-element-parse-buffer))
	     (info (org-combine-plists
		    (org-export-initial-options) '(:with-footnotes t))))
	(setq info (org-combine-plists
		    info (org-export-collect-tree-properties tree info 'test)))
	(should
	 (equal
	  '((1 . "fn:1") (2 . "fn:2") (3 . "fn:3") (4))
	  (org-element-map
	   tree 'footnote-reference
	   (lambda (ref)
	     (when (org-export-footnote-first-reference-p ref info)
	       (cons (org-export-get-footnote-number ref info)
		     (org-element-property :label ref))))
	   info)))))
    ;; 3. Test nested footnote in invisible definitions.
    (org-test-with-temp-text "Text[1]\n\n[1] B [2]\n\n[2] C."
      ;; Hide definitions.
      (narrow-to-region (point) (point-at-eol))
      (let* ((tree (org-element-parse-buffer))
	     (info (org-combine-plists
		    (org-export-initial-options) '(:with-footnotes t))))
	(setq info (org-combine-plists
		    info (org-export-collect-tree-properties tree info 'test)))
	;; Both footnotes should be seen.
	(should
	 (= (length (org-export-collect-footnote-definitions tree info)) 2))))
    ;; 4. Test footnotes definitions collection.
    (org-test-with-temp-text "Text[fn:1:A[fn:2]] [fn:3].

\[fn:2] B [fn:3] [fn::D].

\[fn:3] C."
      (let ((tree (org-element-parse-buffer))
	    (info (org-combine-plists
		   (org-export-initial-options) '(:with-footnotes t))))
	(setq info (org-combine-plists
		    info (org-export-collect-tree-properties tree info 'test)))
	(should (= (length (org-export-collect-footnote-definitions tree info))
		   4))))))


\f
;;; Links

(ert-deftest test-org-export/fuzzy-links ()
  "Test fuzz link export specifications."
  ;; 1. Links to invisible (keyword) targets should be ignored.
  (org-test-with-temp-text
      "Paragraph.\n#+TARGET: Test\n[[Test]]"
    (let* ((tree (org-element-parse-buffer))
	   (info (org-combine-plists (org-export-initial-options))))
      (setq info (org-combine-plists
		  info (org-export-collect-tree-properties tree info 'test)))
      (should-not
       (org-element-map
	tree 'link
	(lambda (link)
	  (org-export-get-ordinal
	   (org-export-resolve-fuzzy-link link info) info)) info))))
  ;; 2. Link to an headline should return headline's number.
  (org-test-with-temp-text
      "Paragraph.\n* Head1\n* Head2\n* Head3\n[[Head2]]"
    (let* ((tree (org-element-parse-buffer))
	   (info (org-combine-plists (org-export-initial-options))))
      (setq info (org-combine-plists
		  info (org-export-collect-tree-properties tree info 'test)))
      (should
       ;; Note: Headline's number is in fact a list of numbers.
       (equal '(2)
	      (org-element-map
	       tree 'link
	       (lambda (link)
		 (org-export-get-ordinal
		  (org-export-resolve-fuzzy-link link info) info)) info t)))))
  ;; 3. Link to a target in an item should return item's number.
  (org-test-with-temp-text
      "- Item1\n  - Item11\n  - <<test>>Item12\n- Item2\n\n\n[[test]]"
    (let* ((tree (org-element-parse-buffer))
	   (info (org-combine-plists (org-export-initial-options))))
      (setq info (org-combine-plists
		  info (org-export-collect-tree-properties tree info 'test)))
      (should
       ;; Note: Item's number is in fact a list of numbers.
       (equal '(1 2)
	      (org-element-map
	       tree 'link
	       (lambda (link)
		 (org-export-get-ordinal
		  (org-export-resolve-fuzzy-link link info) info)) info t)))))
  ;; 4. Link to a target in a footnote should return footnote's
  ;;    number.
  (org-test-with-temp-text
      "Paragraph[1][2][fn:lbl3:C<<target>>][[test]][[target]]\n[1] A\n\n[2] <<test>>B"
    (let* ((tree (org-element-parse-buffer))
	   (info (org-combine-plists (org-export-initial-options))))
      (setq info (org-combine-plists
		  info (org-export-collect-tree-properties tree info 'test)))
      (should
       (equal '(2 3)
	      (org-element-map
	       tree 'link
	       (lambda (link)
		 (org-export-get-ordinal
		  (org-export-resolve-fuzzy-link link info) info)) info)))))
  ;; 5. Link to a named element should return sequence number of that
  ;;    element.
  (org-test-with-temp-text
      "#+NAME: tbl1\n|1|2|\n#+NAME: tbl2\n|3|4|\n#+NAME: tbl3\n|5|6|\n[[tbl2]]"
    (let* ((tree (org-element-parse-buffer))
	   (info (org-combine-plists (org-export-initial-options))))
      (setq info (org-combine-plists
		  info (org-export-collect-tree-properties tree info 'test)))
      (should
       (= 2
	  (org-element-map
	   tree 'link
	   (lambda (link)
	     (org-export-get-ordinal
	      (org-export-resolve-fuzzy-link link info) info)) info t)))))
  ;; 6. Link to a target not within an item, a table, a footnote
  ;;    reference or definition should return section number.
  (org-test-with-temp-text
      "* Head1\n* Head2\nParagraph<<target>>\n* Head3\n[[target]]"
    (let* ((tree (org-element-parse-buffer))
	   (info (org-combine-plists (org-export-initial-options))))
      (setq info (org-combine-plists
		  info (org-export-collect-tree-properties tree info 'test)))
      (should
       (equal '(2)
	      (org-element-map
	       tree 'link
	       (lambda (link)
		 (org-export-get-ordinal
		  (org-export-resolve-fuzzy-link link info) info)) info t))))))

(defun test-org-export/resolve-coderef ()
  "Test `org-export-resolve-coderef' specifications."
  (let ((org-coderef-label-format "(ref:%s)"))
    ;; 1. A link to a "-n -k -r" block returns line number.
    (org-test-with-temp-text
	"#+BEGIN_EXAMPLE -n -k -r\nText (ref:coderef)\n#+END_EXAMPLE"
      (let ((tree (org-element-parse-buffer)))
	(should
	 (= (org-export-resolve-coderef "coderef" `(:parse-tree ,tree)) 1))))
    (org-test-with-temp-text
	"#+BEGIN_SRC emacs-lisp -n -k -r\n(+ 1 1) (ref:coderef)\n#+END_SRC"
      (let ((tree (org-element-parse-buffer)))
	(should
	 (= (org-export-resolve-coderef "coderef" `(:parse-tree ,tree)) 1))))
    ;; 2. A link to a "-n -r" block returns line number.
    (org-test-with-temp-text
	"#+BEGIN_EXAMPLE -n -r\nText (ref:coderef)\n#+END_EXAMPLE"
      (let ((tree (org-element-parse-buffer)))
	(should
	 (= (org-export-resolve-coderef "coderef" `(:parse-tree ,tree)) 1))))
    (org-test-with-temp-text
	"#+BEGIN_SRC emacs-lisp -n -r\n(+ 1 1) (ref:coderef)\n#+END_SRC"
      (let ((tree (org-element-parse-buffer)))
	(should
	 (= (org-export-resolve-coderef "coderef" `(:parse-tree ,tree)) 1))))
    ;; 3. A link to a "-n" block returns coderef.
    (org-test-with-temp-text
	"#+BEGIN_SRC emacs-lisp -n\n(+ 1 1) (ref:coderef)\n#+END_SRC"
      (let ((tree (org-element-parse-buffer)))
	(should
	 (equal (org-export-resolve-coderef "coderef" `(:parse-tree ,tree))
		"coderef"))))
    (org-test-with-temp-text
	"#+BEGIN_EXAMPLE -n\nText (ref:coderef)\n#+END_EXAMPLE"
      (let ((tree (org-element-parse-buffer)))
	(should
	 (equal (org-export-resolve-coderef "coderef" `(:parse-tree ,tree))
		"coderef"))))
    ;; 4. A link to a "-r" block returns line number.
    (org-test-with-temp-text
	"#+BEGIN_SRC emacs-lisp -r\n(+ 1 1) (ref:coderef)\n#+END_SRC"
      (let ((tree (org-element-parse-buffer)))
	(should
	 (= (org-export-resolve-coderef "coderef" `(:parse-tree ,tree)) 1))))
    (org-test-with-temp-text
	"#+BEGIN_EXAMPLE -r\nText (ref:coderef)\n#+END_EXAMPLE"
      (let ((tree (org-element-parse-buffer)))
	(should
	 (= (org-export-resolve-coderef "coderef" `(:parse-tree ,tree)) 1))))
    ;; 5. A link to a block without a switch returns coderef.
    (org-test-with-temp-text
	"#+BEGIN_SRC emacs-lisp\n(+ 1 1) (ref:coderef)\n#+END_SRC"
      (let ((tree (org-element-parse-buffer)))
	(should
	 (equal (org-export-resolve-coderef "coderef" `(:parse-tree ,tree))
		"coderef"))))
    (org-test-with-temp-text
	"#+BEGIN_EXAMPLE\nText (ref:coderef)\n#+END_EXAMPLE"
      (let ((tree (org-element-parse-buffer)))
	(should
	 (equal (org-export-resolve-coderef "coderef" `(:parse-tree ,tree))
		"coderef"))))
    ;; 6. Correctly handle continued line numbers.  A "+n" switch
    ;;    should resume numbering from previous block with numbered
    ;;    lines, ignoring blocks not numbering lines in the process.
    ;;    A "-n" switch resets count.
    (org-test-with-temp-text "
#+BEGIN_EXAMPLE -n
Text.
#+END_EXAMPLE

#+BEGIN_SRC emacs-lisp
\(- 1 1)
#+END_SRC

#+BEGIN_SRC emacs-lisp +n -r
\(+ 1 1) (ref:addition)
#+END_SRC

#+BEGIN_EXAMPLE -n -r
Another text. (ref:text)
#+END_EXAMPLE"
      (let* ((tree (org-element-parse-buffer))
	     (info `(:parse-tree ,tree)))
	(should (= (org-export-resolve-coderef "addition" info) 2))
	(should (= (org-export-resolve-coderef "text" info) 1))))
    ;; 7. Recognize coderef with user-specified syntax.
    (org-test-with-temp-text
	"#+BEGIN_EXAMPLE -l \"[ref:%s]\"\nText. [ref:text]\n#+END_EXAMPLE"
      (let ((tree (org-element-parse-buffer)))
	(should (equal (org-export-resolve-coderef "text" `(:parse-tree ,tree))
		       "text"))))))


\f
;;; Src-block and example-block

(ert-deftest test-org-export/unravel-code ()
  "Test `org-export-unravel-code' function."
  (let ((org-coderef-label-format "(ref:%s)"))
    ;; 1. Code without reference.
    (org-test-with-temp-text "#+BEGIN_EXAMPLE\n(+ 1 1)\n#+END_EXAMPLE"
      (should (equal (org-export-unravel-code (org-element-current-element))
		     '("(+ 1 1)\n"))))
    ;; 2. Code with reference.
    (org-test-with-temp-text
	"#+BEGIN_EXAMPLE\n(+ 1 1) (ref:test)\n#+END_EXAMPLE"
      (should (equal (org-export-unravel-code (org-element-current-element))
		     '("(+ 1 1)\n" (1 . "test")))))
    ;; 3. Code with user-defined reference.
    (org-test-with-temp-text
	"#+BEGIN_EXAMPLE -l \"[ref:%s]\"\n(+ 1 1) [ref:test]\n#+END_EXAMPLE"
      (should (equal (org-export-unravel-code (org-element-current-element))
		     '("(+ 1 1)\n" (1 . "test")))))
    ;; 4. Code references keys are relative to the current block.
    (org-test-with-temp-text "
#+BEGIN_EXAMPLE -n
\(+ 1 1)
#+END_EXAMPLE
#+BEGIN_EXAMPLE +n
\(+ 2 2)
\(+ 3 3) (ref:one)
#+END_EXAMPLE"
      (goto-line 5)
      (should (equal (org-export-unravel-code (org-element-current-element))
		     '("(+ 2 2)\n(+ 3 3)\n" (2 . "one")))))
    ;; 5. Free up comma-protected lines.
    ;;
    ;; 5.1. In an Org source block, every line is protected.
    (org-test-with-temp-text
	"#+BEGIN_SRC org\n,* Test\n,# comment\n,Text\n#+END_SRC"
      (should (equal (org-export-unravel-code (org-element-current-element))
		     '("* Test\n# comment\nText\n"))))
    ;; 5.2. In other blocks, only headlines, comments and keywords are
    ;;      protected.
    (org-test-with-temp-text
	"#+BEGIN_EXAMPLE\n,* Headline\n, * Not headline\n,Keep\n#+END_EXAMPLE"
      (should (equal (org-export-unravel-code (org-element-current-element))
		     '("* Headline\n, * Not headline\n,Keep\n"))))))



(provide 'test-org-export)
;;; test-org-export.el end here

debug log:

solving f34cc7b ...
found f34cc7b in https://git.savannah.gnu.org/cgit/emacs/org-mode.git

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).