Browse Source

Babel: use case of reading entry properties as an ERT

* testing/examples/babel.org: Add test entry for ERT.
* testing/lisp/test-ob-exp.el(ob-exp/use-case-of-reading-entry-properties):
Add expected source block results for ERT.
Michael Brand 4 years ago
parent
commit
b01871f23e
2 changed files with 119 additions and 0 deletions
  1. 77 0
      testing/examples/babel.org
  2. 42 0
      testing/lisp/test-ob-exp.el

+ 77 - 0
testing/examples/babel.org

@@ -359,3 +359,80 @@ Here is a call line with more than just the results exported.
   <<strip-export-1>>
   echo "1$i"
 #+END_SRC
+
+* use case of reading entry properties
+  :PROPERTIES:
+  :ID:       cc5fbc20-bca5-437a-a7b8-2b4d7a03f820
+  :END:
+
+If overriden by caller then use :var from header else use entry property.
+
+** section
+   :PROPERTIES:
+   :a:        1
+   :c:        3
+   :END:
+
+Note: Just export of a property can be done with a macro: {{{property(a)}}}.
+
+#+CALL: src_block_location_shell(dummy_name="sect call")
+#+CALL: src_block_location_elisp[:session sect call]()
+- sect inline call_src_block_location_shell(dummy_name="sect inline")
+- sect inline call_src_block_location_elisp[:session sect inline]()
+
+*** subsection
+    :PROPERTIES:
+    :b:        2
+    :c:        4
+    :END:
+
+#+CALL: src_block_location_shell(dummy_name="sub0 call")
+#+CALL: src_block_location_elisp[:session sub0 call]()
+- sub0 inline call_src_block_location_shell(dummy_name="sub0 inline")
+- sub0 inline call_src_block_location_elisp[:session sub0 inline]()
+
+#+CALL: src_block_location_shell(dummy_name="sub1 call", c=5, e=6)
+#+CALL: src_block_location_elisp[:session sub1 call](c=5, e=6)
+- sub1 inline call_src_block_location_shell(dummy_name="sub1 inline", c=5, e=6)
+- sub1 inline call_src_block_location_elisp[:session sub1 inline](c=5, e=6)
+
+**** function definition
+
+#+NAME: src_block_location_shell
+#+HEADER: :var dummy_name="workaround to get different result blocks"
+#+HEADER: :var a=(or (org-entry-get org-babel-current-src-block-location "a" t) "0")
+#+HEADER: :var b=(or (org-entry-get org-babel-current-src-block-location "b" t) "0")
+#+HEADER: :var c=(or (org-entry-get org-babel-current-src-block-location "c" t) "0")
+#+HEADER: :var d=(or (org-entry-get org-babel-current-src-block-location "d" t) "0")
+#+HEADER: :var e=(or (org-entry-get org-babel-current-src-block-location "e" t) "0")
+#+BEGIN_SRC sh :shebang #!/bin/sh :exports results :results verbatim
+  printf "shell a:$a, b:$b, c:$c, d:$d, e:$e"
+#+END_SRC
+
+#+RESULTS: src_block_location_shell
+
+#+NAME: src_block_location_elisp
+#+HEADER: :var a='nil
+#+HEADER: :var b='nil
+#+HEADER: :var c='nil
+#+HEADER: :var d='nil
+#+HEADER: :var e='nil
+#+BEGIN_SRC emacs-lisp :exports results
+  (setq
+   a (or a (string-to-number
+            (or (org-entry-get org-babel-current-src-block-location "a" t)
+                "0")))
+   b (or b (string-to-number
+            (or (org-entry-get org-babel-current-src-block-location "b" t)
+                "0")))
+   c (or c (string-to-number
+            (or (org-entry-get org-babel-current-src-block-location "c" t)
+                "0")))
+   d (or d (string-to-number
+            (or (org-entry-get org-babel-current-src-block-location "e" t)
+                "0")))
+   e (or e (string-to-number
+            (or (org-entry-get org-babel-current-src-block-location "d" t)
+                "0"))))
+  (format "elisp a:%d, b:%d, c:%d, d:%d, e:%d" a b c d e)
+#+END_SRC

+ 42 - 0
testing/lisp/test-ob-exp.el

@@ -216,6 +216,48 @@ Here is one at the end of a line. =2=
       (should-not (string-match (regexp-quote "<<strip-export-1>>") result))
       (should-not (string-match (regexp-quote "i=\"10\"") result)))))
 
+(ert-deftest ob-exp/use-case-of-reading-entry-properties ()
+  (org-test-at-id "cc5fbc20-bca5-437a-a7b8-2b4d7a03f820"
+    (org-narrow-to-subtree)
+    (let* ((case-fold-search nil)
+	   (result (org-test-with-expanded-babel-code (buffer-string)))
+	   (sect "a:1, b:0, c:3, d:0, e:0")
+	   (sub0 "a:1, b:2, c:4, d:0, e:0")
+	   (sub1 "a:1, b:2, c:5, d:0, e:6")
+	   (func sub0))
+      ;; entry "section"
+      (should (string-match (concat "\"sect call\".*)\n: shell " sect "\n")
+			    result))
+      (should (string-match (concat "sect call\\](.*)\n: elisp " sect "\n")
+			    result))
+      (should (string-match (concat "\n- sect inline =shell " sect "=\n")
+			    result))
+      (should (string-match (concat "\n- sect inline =elisp " sect "=\n")
+			    result))
+      ;; entry "subsection", call without arguments
+      (should (string-match (concat "\"sub0 call\".*)\n: shell " sub0 "\n")
+			    result))
+      (should (string-match (concat "sub0 call\\](.*)\n: elisp " sub0 "\n")
+			    result))
+      (should (string-match (concat "\n- sub0 inline =shell " sub0 "=\n")
+			    result))
+      (should (string-match (concat "\n- sub0 inline =elisp " sub0 "=\n")
+			    result))
+      ;; entry "subsection", call with arguments
+      (should (string-match (concat "\"sub1 call\".*)\n: shell " sub1 "\n")
+			    result))
+      (should (string-match (concat "sub1 call\\](.*)\n: elisp " sub1 "\n")
+			    result))
+      (should (string-match (concat "\n- sub1 inline =shell " sub1 "=\n")
+			    result))
+      (should (string-match (concat "\n- sub1 inline =elisp " sub1 "=\n")
+			    result))
+      ;; entry "function definition"
+      (should (string-match (concat "_location_shell\n: shell " func "\n")
+			    result))
+      (should (string-match (concat "_location_elisp\n: elisp " func "\n")
+			    result)))))
+
 (ert-deftest ob-exp/export-from-a-temp-buffer ()
   :expected-result :failed
   (org-test-with-temp-text