testing/*: Inline result tests modified to follow current schema
[org-mode/org-kjn.git] / testing / examples / babel.org
blob80a43c4c3e215affbecad25ed72673a5789bbbc3
1 #+Title: a collection of examples for Babel tests
2 #+OPTIONS: ^:nil
4 * =:noweb= header argument expansion
5   :PROPERTIES:
6   :ID:       eb1f6498-5bd9-45e0-9c56-50717053e7b7
7   :END:
9 #+name: noweb-example
10 #+begin_src emacs-lisp :results silent :exports code
11   (message "expanded1")
12 #+end_src
14 #+name: noweb-example2
15 #+begin_src emacs-lisp :results silent
16   (message "expanded2")
17 #+end_src
19 #+begin_src emacs-lisp :noweb yes :results silent
20 ;; noweb-1-yes-start
21   <<noweb-example>>
22 #+end_src
24 #+begin_src emacs-lisp :noweb no  :results silent
25 ;; noweb-no-start
26   <<noweb-example1>>
27 #+end_src
29 #+begin_src emacs-lisp :noweb yes :results silent
30 ;; noweb-2-yes-start
31   <<noweb-example2>>
32 #+end_src
34 #+begin_src emacs-lisp :noweb tangle :results silent
35   ;; noweb-tangle-start
36   <<noweb-example1>>
37   <<noweb-example2>>
38 #+end_src
40 * =:noweb= header argument expansion using :exports results
41   :PROPERTIES:
42   :ID:       8701beb4-13d9-468c-997a-8e63e8b66f8d
43   :END:
45 #+name: noweb-example
46 #+begin_src emacs-lisp :exports results
47   (message "expanded1")
48 #+end_src
50 #+name: noweb-example2
51 #+begin_src emacs-lisp :exports results
52   (message "expanded2")
53 #+end_src
55 #+begin_src emacs-lisp :noweb yes :exports results
56 ;; noweb-1-yes-start
57   <<noweb-example>>
58 #+end_src
60 #+begin_src emacs-lisp :noweb no :exports code
61 ;; noweb-no-start
62   <<noweb-example1>>
63 #+end_src
65 #+begin_src emacs-lisp :noweb yes :exports results
66 ;; noweb-2-yes-start
67   <<noweb-example2>>
68 #+end_src
70 #+begin_src emacs-lisp :noweb tangle :exports code
71   <<noweb-example1>>
72   <<noweb-example2>>
73 #+end_src
75 * excessive id links on tangling
76   :PROPERTIES:
77   :ID:       ef06fd7f-012b-4fde-87a2-2ae91504ea7e
78   :END:
80 ** no, don't give me an ID
81 #+begin_src emacs-lisp :tangle no
82   (message "not to be tangled")
83 #+end_src
85 ** yes, I'd love an ID
86    :PROPERTIES:
87    :ID:       ae7b55ca-9ef2-4d30-bd48-da30e35fd0f3
88    :END:
89 #+begin_src emacs-lisp :tangle no
90   (message "for tangling")
91 #+end_src
92 * simple named code block
93   :PROPERTIES:
94   :ID:       0d82b52d-1bb9-4916-816b-2c67c8108dbb
95   :END:
97 #+name: i-have-a-name
98 #+begin_src emacs-lisp
99   42
100 #+end_src
102 #+name:
103 : 42
105 #+name: i-have-a-name
106 : 42
108 * Pascal's Triangle -- exports both test
109   :PROPERTIES:
110   :ID:       92518f2a-a46a-4205-a3ab-bcce1008a4bb
111   :END:
113 #+name: pascals-triangle
114 #+begin_src emacs-lisp :var n=5 :exports both
115   (require 'cl)
116   (defalias 'my-map (if (org-version-check "24.2.50" "cl" :predicate)
117                         'cl-map
118                       'map))
119   (defun pascals-triangle (n)
120     (if (= n 0)
121         (list (list 1))
122       (let* ((prev-triangle (pascals-triangle (- n 1)))
123              (prev-row (car (reverse prev-triangle))))
124         (append prev-triangle
125                 (list (my-map 'list #'+
126                               (append prev-row '(0))
127                               (append '(0) prev-row)))))))
128   
129   (pascals-triangle n)
130 #+end_src
132 * calling code blocks from inside table
133   :PROPERTIES:
134   :ID:       6d2ff4ce-4489-4e2a-9c65-e3f71f77d975
135   :END:
137 #+name: take-sqrt
138 #+begin_src emacs-lisp :var n=9
139   (sqrt n)
140 #+end_src
142 * executing an lob call line
143   :PROPERTIES:
144   :results:  silent
145   :ID:       fab7e291-fde6-45fc-bf6e-a485b8bca2f0
146   :END:
148 #+call: echo(input="testing")
149 #+call: echo(input="testing") :results vector
150 #+call: echo[:var input="testing"]()
151 #+call: echo[:var input="testing"]() :results vector
152 #+call: echo("testing")
153 #+call: echo("testing") :results vector
154 This is an inline call call_echo(input="testing") embedded in prose.
155 This is an inline call call_echo(input="testing")[:results vector] embedded in prose.
156 #+call: lob-minus(8, 4)
157 call_echo("testing")
158 call_concat(1,2,3)
160 #+name: concat
161 #+begin_src emacs-lisp :var a=0 :var b=0 :var c=0
162   (format "%S%S%S" a b c)
163 #+end_src
165 * exporting an lob call line
166   :PROPERTIES:
167   :ID:       72ddeed3-2d17-4c7f-8192-a575d535d3fc
168   :END:
170 #+name: double
171 #+begin_src emacs-lisp :var it=0
172   (* 2 it)
173 #+end_src
175 The following exports as a normal call line
176 #+call: double(it=0)
178 Now here is an inline call call_double(it=1) stuck in the middle of
179 some prose.
181 This one should not be exported =call_double(it=2)= because it is
182 quoted.
184 Finally this next one should export, even though it starts a line
185 call_double(it=3) because sometimes inline blocks fold with a
186 paragraph.
188 And, a call with raw results call_double(4)[:results raw] should not
189 have quoted results.
191 The following 2*5=call_double(5) should export even when prefixed by
192 an = sign.
194 * inline source block
195   :PROPERTIES:
196   :results:  silent
197   :ID:       54cb8dc3-298c-4883-a933-029b3c9d4b18
198   :END:
199 Here is one in the middle src_sh{echo 1} of a line.
200 Here is one at the end of a line. src_sh{echo 2}
201 src_sh{echo 3} Here is one at the beginning of a line.
203 * exported inline source block
204 :PROPERTIES:
205 :ID:       cd54fc88-1b6b-45b6-8511-4d8fa7fc8076
206 :results:  silent
207 :exports:  code
208 :END:
209 Here is one in the middle src_sh{echo 1} of a line.
210 Here is one at the end of a line. src_sh{echo 2}
211 src_sh{echo 3} Here is one at the beginning of a line.
212 Here is one that is also evaluated: src_sh[:exports both]{echo 4}
214 * mixed blocks with exports both
215   :PROPERTIES:
216   :ID:       5daa4d03-e3ea-46b7-b093-62c1b7632df3
217   :END:
219 #+name: a-list
220 - a
221 - b
222 - c
224 #+begin_src emacs-lisp :exports both
225     "code block results"
226 #+end_src
228 #+begin_src emacs-lisp :var lst=a-list :results list :exports both
229   (reverse lst)
230 #+end_src
232 * using the =:noweb-ref= header argument
233   :PROPERTIES:
234   :ID:       54d68d4b-1544-4745-85ab-4f03b3cbd8a0
235   :noweb-sep: ""
236   :END:
238 #+begin_src sh :tangle yes :noweb yes :shebang "#!/bin/sh"
239   <<fullest-disk>>
240 #+end_src
242 ** query all mounted disks
243 #+begin_src sh :noweb-ref fullest-disk
244   df
245 #+end_src
247 ** strip the header row
248 #+begin_src sh :noweb-ref fullest-disk
249   |sed '1d'
250 #+end_src
252 ** sort by the percent full
253 #+begin_src sh :noweb-ref fullest-disk
254   |awk '{print $5 " " $6}'|sort -n |tail -1
255 #+end_src
257 ** extract the mount point
258 #+begin_src sh :noweb-ref fullest-disk
259   |awk '{print $2}'
260 #+end_src
261 * resolving sub-trees as references
262   :PROPERTIES:
263   :ID:       2409e8ba-7b5f-4678-8888-e48aa02d8cb4
264   :results:  silent
265   :END:
267 #+begin_src emacs-lisp :var text=d4faa7b3-072b-4dcf-813c-dd7141c633f3
268   (length text)
269 #+end_src
271 #+begin_src org :noweb yes
272   <<simple-subtree>>
273   <<d4faa7b3-072b-4dcf-813c-dd7141c633f3>>
274 #+end_src
276 ** simple subtree with custom ID
277    :PROPERTIES:
278    :CUSTOM_ID: simple-subtree
279    :END:
280 this is simple
282 ** simple subtree with global ID
283    :PROPERTIES:
284    :ID:       d4faa7b3-072b-4dcf-813c-dd7141c633f3
285    :END:
286 has length 14
288 * org-babel-get-inline-src-block-matches
289   :PROPERTIES:
290   :results:  silent
291   :ID:       0D0983D4-DE33-400A-8A05-A225A567BC74
292   :END:
293 src_sh{echo "One"} block at start of line
294  One spaced block in  src_sh{ echo "middle" } of line
295 src_sh{echo 2} blocks on the src_emacs-lisp{"same"} line
296  Inline block with src_sh[:results silent]{ echo "parameters" }.
298 * org-babel-get-inline-src-block-matches (with empty args)
299   :PROPERTIES:
300   :results:  silent
301   :ID:       d55dada7-de0e-4340-8061-787cccbedee5
302   :END:
303 src_sh[]{echo "One"} block at start of line
304  One spaced block in  src_sh[]{ echo "middle" } of line
305 src_sh[]{echo 2} blocks on the src_emacs-lisp[]{"same"} line
306  Inline block with src_sh[:results silent]{ echo "parameters" }.
308 * exporting a code block with a name
309   :PROPERTIES:
310   :ID:       b02ddd8a-eeb8-42ab-8664-8a759e6f43d9
311   :END:
313 exporting a code block with a name
314 #+name: qux
315 #+begin_src sh :foo "baz"
316   echo bar
317 #+end_src
318 * noweb no-export and exports both
319   :PROPERTIES:
320   :ID:       8a820f6c-7980-43db-8a24-0710d33729c9
321   :END:
322 Weird interaction.
324 here is one block
326 #+name: noweb-no-export-and-exports-both-1
327 #+BEGIN_SRC sh :exports none
328   echo 1
329 #+END_SRC
331 and another
333 #+BEGIN_SRC sh :noweb no-export :exports both
334   # I am inside the code block
335   <<noweb-no-export-and-exports-both-1>>
336 #+END_SRC
338 * in order evaluation on export
339   :PROPERTIES:
340   :exports: results
341   :ID:       96cc7073-97ec-4556-87cf-1f9bffafd317
342   :END:
344 First.
345 #+name: foo-for-order-of-evaluation
346 #+begin_src emacs-lisp :var it=1
347   (push it *evaluation-collector*)
348 #+end_src
350 Second
351 #+begin_src emacs-lisp
352   (push 2 *evaluation-collector*)
353 #+end_src
355 Third src_emacs-lisp{(car (push 3 *evaluation-collector*))}
357 Fourth
358 #+call: foo-for-order-of-evaluation(4)
360 Fifth
361 #+begin_src emacs-lisp
362   (push 5 *evaluation-collector*)
363 #+end_src
364 * exporting more than just results from a call line
365   :PROPERTIES:
366   :ID:       bec63a04-491e-4caa-97f5-108f3020365c
367   :END:
368 Here is a call line with more than just the results exported.
369 #+call: double(8)
370 * strip noweb references on export
371   :PROPERTIES:
372   :ID:       8e7bd234-99b2-4b14-8cd6-53945e409775
373   :END:
375 #+name: strip-export-1
376 #+BEGIN_SRC sh :exports none
377   i="10"
378 #+END_SRC
380 #+BEGIN_SRC sh :noweb strip-export :exports code :results silent
381   <<strip-export-1>>
382   echo "1$i"
383 #+END_SRC
385 * use case of reading entry properties
386   :PROPERTIES:
387   :ID:       cc5fbc20-bca5-437a-a7b8-2b4d7a03f820
388   :END:
390 Use case checked and documented with this test: During their
391 evaluation the source blocks read values from properties from the
392 entry where the call has been made unless the value is overridden with
393 the optional argument of the caller.
395 ** section
396    :PROPERTIES:
397    :a:        1
398    :c:        3
399    :END:
401 Note: Just export of a property can be done with a macro: {{{property(a)}}}.
403 #+NAME: src_block_location_shell-sect-call
404 #+CALL: src_block_location_shell()
406 #+NAME: src_block_location_elisp-sect-call
407 #+CALL: src_block_location_elisp()
409 - sect inline call_src_block_location_shell()[:results raw]
410 - sect inline call_src_block_location_elisp()[:results raw]
412 *** subsection
413     :PROPERTIES:
414     :b:        2
415     :c:        4
416     :END:
418 #+NAME: src_block_location_shell-sub0-call
419 #+CALL: src_block_location_shell()
421 #+NAME: src_block_location_elisp-sub0-call
422 #+CALL: src_block_location_elisp()
424 - sub0 inline call_src_block_location_shell()[:results raw]
425 - sub0 inline call_src_block_location_elisp()[:results raw]
427 #+NAME: src_block_location_shell-sub1-call
428 #+CALL: src_block_location_shell(c=5, e=6)
430 #+NAME: src_block_location_elisp-sub1-call
431 #+CALL: src_block_location_elisp(c=5, e=6)
433 - sub1 inline call_src_block_location_shell(c=5, e=6)[:results raw]
434 - sub1 inline call_src_block_location_elisp(c=5, e=6)[:results raw]
436 **** function definition
438 comments for ":var":
439 - The "or" is to deal with a property not present.
440 - The t is to get property inheritance.
441 #+NAME: src_block_location_shell
442 #+HEADER: :var a=(or (org-entry-get org-babel-current-src-block-location "a" t) "0")
443 #+HEADER: :var b=(or (org-entry-get org-babel-current-src-block-location "b" t) "0")
444 #+HEADER: :var c=(or (org-entry-get org-babel-current-src-block-location "c" t) "0")
445 #+HEADER: :var d=(or (org-entry-get org-babel-current-src-block-location "d" t) "0")
446 #+HEADER: :var e=(or (org-entry-get org-babel-current-src-block-location "e" t) "0")
447 #+BEGIN_SRC sh :shebang #!/bin/sh :exports results :results verbatim
448   printf "shell a:$a, b:$b, c:$c, d:$d, e:$e"
449 #+END_SRC
451 #+RESULTS: src_block_location_shell
453 #+NAME: src_block_location_elisp
454 #+HEADER: :var a='nil
455 #+HEADER: :var b='nil
456 #+HEADER: :var c='nil
457 #+HEADER: :var d='nil
458 #+HEADER: :var e='nil
459 #+BEGIN_SRC emacs-lisp :exports results
460   (setq
461    ;; - The first `or' together with ":var <var>='nil" is to check for
462    ;;   a value bound from an optional call argument, in the examples
463    ;;   here: c=5, e=6
464    ;; - The second `or' is to deal with a property not present
465    ;; - The t is to get property inheritance
466    a (or a (string-to-number
467             (or (org-entry-get org-babel-current-src-block-location "a" t)
468                 "0")))
469    b (or b (string-to-number
470             (or (org-entry-get org-babel-current-src-block-location "b" t)
471                 "0")))
472    c (or c (string-to-number
473             (or (org-entry-get org-babel-current-src-block-location "c" t)
474                 "0")))
475    d (or d (string-to-number
476             (or (org-entry-get org-babel-current-src-block-location "e" t)
477                 "0")))
478    e (or e (string-to-number
479             (or (org-entry-get org-babel-current-src-block-location "d" t)
480                 "0"))))
481   (format "elisp a:%d, b:%d, c:%d, d:%d, e:%d" a b c d e)
482 #+END_SRC
484 * =:file-ext= and =:output-dir= header args
485   :PROPERTIES:
486   :ID:       93573e1d-6486-442e-b6d0-3fedbdc37c9b
487   :END:
488 #+name: file-ext-basic
489 #+BEGIN_SRC emacs-lisp :file-ext txt
491 #+END_SRC
493 #+name: file-ext-dir-relative
494 #+BEGIN_SRC emacs-lisp :file-ext txt :output-dir foo
496 #+END_SRC
498 #+name: file-ext-dir-relative-slash
499 #+BEGIN_SRC emacs-lisp :file-ext txt :output-dir foo/
501 #+END_SRC
503 #+name: file-ext-dir-absolute
504 #+BEGIN_SRC emacs-lisp :file-ext txt :output-dir /tmp
506 #+END_SRC
508 #+name: file-ext-file-wins
509 #+BEGIN_SRC emacs-lisp :file-ext txt :file foo.bar
511 #+END_SRC
513 #+name: output-dir-and-file
514 #+BEGIN_SRC emacs-lisp :output-dir xxx :file foo.bar
516 #+END_SRC