├── .gitignore ├── .travis.yml ├── Makefile ├── README.org ├── ob-hy.el ├── test-ob-hy.el └── test-ob-hy.org /.gitignore: -------------------------------------------------------------------------------- 1 | # Compiled 2 | *.elc 3 | 4 | # Packaging 5 | .cask 6 | 7 | # Backup files 8 | *~ 9 | 10 | # Undo-tree save-files 11 | *.~undo-tree 12 | 13 | .test-org-id-locations -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: generic 2 | 3 | env: 4 | matrix: 5 | - EMACS=emacs24 6 | - EMACS=emacs-snapshot 7 | 8 | matrix: 9 | allow_failures: 10 | - env: EMACS=emacs-snapshot 11 | 12 | before_install: 13 | - if [ "$EMACS"= "emacs24" ]; then 14 | sudo apt-get -qq update && 15 | sudo apt-get -qq -f install && 16 | sudo apt-get -qq install emacs24 emacs24-el curl org-mode; 17 | fi 18 | fi 19 | - if [ "$EMACS" = "emacs-snapshot" ]; then 20 | sudo add-apt-repository -y ppa:ubuntu-elisp/ppa && 21 | sudo apt-get -qq update && 22 | sudo apt-get -qq -f install && 23 | sudo apt-get -qq install emacs-snapshot emacs-snapshot-el; 24 | fi 25 | 26 | script: make test 27 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | .PHONY: test 2 | 3 | test: 4 | @rm -f .test-org-id-locations 5 | emacs -Q --batch -q \ 6 | -L . \ 7 | -l ob-hy.el \ 8 | -l test-ob-hy.el \ 9 | --eval "(progn \ 10 | (setq org-confirm-babel-evaluate nil) \ 11 | (org-babel-do-load-languages \ 12 | 'org-babel-load-languages '((emacs-lisp . t) \ 13 | (sh . t) \ 14 | (org . t) \ 15 | (hy . t))))" \ 16 | -f ob-hy-test-runall 17 | -------------------------------------------------------------------------------- /README.org: -------------------------------------------------------------------------------- 1 | #+TITLE: ob-hy 2 | [[https://travis-ci.org/brantou/ob-hy][file:https://travis-ci.org/brantou/ob-hy.svg?branch=master]] 3 | [[https://melpa.org/#/ob-hy][file:https://melpa.org/packages/ob-hy-badge.svg]][[https://stable.melpa.org/#/ob-hy][file:https://stable.melpa.org/packages/ob-hy-badge.svg]] 4 | * Introduction 5 | :PROPERTIES: 6 | :ID: 614a110c-77b2-41f8-9714-ce311ff1acb0 7 | :END: 8 | 9 | =ob-hy= enables [[http://orgmode.org/worg/org-contrib/babel/intro.html][Org-Babel]] support for evaluating [[http://hylang.org/][Hy]] code. 10 | It was created based on the usage of [[./ob-template.el][ob-template]]. 11 | 12 | #+BEGIN_SRC hy :results output 13 | (print "hello world") 14 | #+END_SRC 15 | 16 | #+RESULTS: 17 | : hello world 18 | 19 | * Examples 20 | :PROPERTIES: 21 | :ID: 98dc532f-d354-4d5e-a4ba-2489ad02730b 22 | :END: 23 | ** variables 24 | :PROPERTIES: 25 | :ID: e4f5eca1-cbd3-4a46-a8f3-ba92a2b869f6 26 | :END: 27 | : #+BEGIN_SRC hy :var a=3 b=5 28 | : (+ a b) 29 | : #+END_SRC 30 | 31 | : #+RESULTS: 32 | : : 8 33 | ** table and list 34 | :PROPERTIES: 35 | :ID: 7beff92a-3dc6-47ad-9898-0dbbad5b090f 36 | :END: 37 | : #+NAME: tel-note 38 | : | name | tel | 39 | : |-------+--------| 40 | : | brant | 170... | 41 | : | ou | 138... | 42 | 43 | : #+BEGIN_SRC hy :var tb=tel-note :results output table 44 | : (print tb) 45 | : #+END_SRC 46 | 47 | : #+RESULTS: 48 | : : ((u'brant' u'170...') (u'ou' u'138...')) 49 | 50 | : #+BEGIN_SRC hy :var lst='("1" "2" "3") :results output 51 | : (print lst) 52 | : #+END_SRC 53 | 54 | : #+RESULTS: 55 | : : (u'1' u'2' u'3') 56 | 57 | ** literate programming 58 | :PROPERTIES: 59 | :ID: 92a873f1-0fd5-46de-8e3c-104bc2c91c01 60 | :END: 61 | : #+NAME: square 62 | : #+BEGIN_SRC hy 63 | : (defn square [x] 64 | : (* x x)) 65 | : #+END_SRC 66 | 67 | : #+NAME: calc-square 68 | : #+BEGIN_SRC hy :var x=0 :noweb strip-export :results output 69 | : <> 70 | : (print (square x)) 71 | : #+END_SRC 72 | 73 | : #+RESULTS: calc-square 74 | : : 0 75 | 76 | : #+CALL: calc-square(x=5) 77 | 78 | : #+RESULTS: 79 | : : 25 80 | 81 | 82 | ** session 83 | :PROPERTIES: 84 | :ID: ea79d97b-4b6c-48f6-8154-6de10ee5e40c 85 | :END: 86 | : #+BEGIN_SRC hy :session hylang :results output 87 | : (print "hello world") 88 | : (defn square [x] 89 | : (* x x)) 90 | : (print (square 5)) 91 | : #+END_SRC 92 | 93 | : #+RESULTS: 94 | : : hello world 95 | : : ... 25 96 | 97 | * Running tests 98 | :PROPERTIES: 99 | :ID: 82cd12e6-b401-439a-9da5-03f0cf6e8e89 100 | :END: 101 | 102 | Tests can be executed by /make test/ or invoking emacs directly with 103 | the command-line below: 104 | 105 | #+BEGIN_SRC shell 106 | emacs -Q --batch -q \ 107 | -L . \ 108 | -l ob-hy.el \ 109 | -l test-ob-hy.el \ 110 | --eval "(progn \ 111 | (setq org-confirm-babel-evaluate nil) \ 112 | (org-babel-do-load-languages \ 113 | 'org-babel-load-languages '((emacs-lisp . t) \ 114 | (sh . t) \ 115 | (org . t) \ 116 | (hy . t))))" \ 117 | -f ob-hy-test-runall 118 | #+END_SRC 119 | -------------------------------------------------------------------------------- /ob-hy.el: -------------------------------------------------------------------------------- 1 | ;;; ob-hy.el --- org-babel functions for Hy-lang evaluation 2 | 3 | ;; Copyright (C) 2017 Brantou 4 | 5 | ;; Author: Brantou 6 | ;; URL: https://github.com/brantou/ob-hy 7 | ;; Keywords: hy, literate programming, reproducible research 8 | ;; Homepage: http://orgmode.org 9 | ;; Version: 1.0.1 10 | ;; Package-Requires: ((emacs "24.4")) 11 | 12 | ;;; License: 13 | 14 | ;; This program is free software; you can redistribute it and/or modify 15 | ;; it under the terms of the GNU General Public License as published by 16 | ;; the Free Software Foundation; either version 3, or (at your option) 17 | ;; any later version. 18 | ;; 19 | ;; This program is distributed in the hope that it will be useful, 20 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of 21 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 22 | ;; GNU General Public License for more details. 23 | ;; 24 | ;; You should have received a copy of the GNU General Public License 25 | ;; along with GNU Emacs; see the file COPYING. If not, write to the 26 | ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 27 | ;; Boston, MA 02110-1301, USA. 28 | 29 | ;; This file is not part of GNU Emacs. 30 | 31 | ;;; Commentary: 32 | ;; 33 | ;; Org-Babel support for evaluating hy-lang code. 34 | ;; 35 | ;; It was created based on the usage of ob-template. 36 | ;; 37 | 38 | ;;; Requirements: 39 | ;; 40 | ;; - hy :: https://hy-lang.org/ 41 | ;; 42 | ;; - hy-mode :: Can be installed through from ELPA, or from 43 | ;; https://raw.githubusercontent.com/hylang/hy-mode/master/hy-mode.el 44 | ;; 45 | 46 | ;;; TODO 47 | ;; 48 | ;; - Provide better error feedback. 49 | ;; 50 | 51 | ;;; Code: 52 | (require 'ob) 53 | (require 'ob-eval) 54 | (require 'ob-tangle) 55 | 56 | (defvar org-babel-tangle-lang-exts) 57 | (add-to-list 'org-babel-tangle-lang-exts '("hy" . "hy")) 58 | 59 | (defvar org-babel-default-header-args:hy '() 60 | "Default header arguments for hy code blocks.") 61 | 62 | (defcustom org-babel-hy-command "hy" 63 | "Name of command used to evaluate hy blocks." 64 | :group 'org-babel 65 | :version "24.4" 66 | :package-version '(Org . "8.0") 67 | :type 'string) 68 | 69 | (defcustom org-babel-hy-nil-to 'hline 70 | "Replace nil in hy tables with this before returning." 71 | :group 'org-babel 72 | :version "24.4" 73 | :package-version '(Org . "8.0") 74 | :type 'symbol) 75 | 76 | (defvar org-babel-hy-eoe-indicator ":org_babel_hy_eoe" 77 | "A string to indicate that evaluation has completed.") 78 | 79 | (defconst org-babel-hy-wrapper-method 80 | " 81 | (defn main [] 82 | %s) 83 | 84 | (with [f (open \"%s\" \"w\")] (.write f (str (main))))") 85 | 86 | (defconst org-babel-hy-pp-wrapper-method 87 | " 88 | (import pprint) 89 | (defn main [] 90 | %s) 91 | 92 | (with [f (open \"%s\" \"w\")] (.write f (.pformat pprint (main))))") 93 | 94 | (defun org-babel-expand-body:hy (body params) 95 | "Expand BODY according to PARAMS, return the expanded body." 96 | (let* ((vars (org-babel-hy-get-vars params)) 97 | (body (if (null vars) (org-trim body) 98 | (concat 99 | (mapconcat 100 | (lambda (var) 101 | (format "(setv %S (quote %S))" (car var) (cdr var))) 102 | vars "\n") 103 | "\n" body)))) 104 | body)) 105 | 106 | (defun org-babel-execute:hy (body params) 107 | "Execute a block of Hy code with org-babel. 108 | This function is called by `org-babel-execute-src-block'" 109 | (message "executing Hy source code block") 110 | (let* ((org-babel-hy-command 111 | (or (cdr (assq :hy params)) 112 | org-babel-hy-command)) 113 | (session (org-babel-hy-initiate-session 114 | (cdr (assq :session params)))) 115 | (result-params (cdr (assq :result-params params))) 116 | (result-type (cdr (assq :result-type params))) 117 | (full-body (org-babel-expand-body:hy body params)) 118 | (result (org-babel-hy-evaluate 119 | session full-body result-type result-params))) 120 | (org-babel-reassemble-table 121 | result 122 | (org-babel-pick-name (cdr (assq :colname-names params)) 123 | (cdr (assq :colnames params))) 124 | (org-babel-pick-name (cdr (assq :rowname-names params)) 125 | (cdr (assq :rownames params)))))) 126 | 127 | (defun org-babel-hy-evaluate 128 | (session body &optional result-type result-params) 129 | "Evaluate BODY as Hy code." 130 | (if session 131 | (org-babel-hy-evaluate-session 132 | session body result-type result-params) 133 | (org-babel-hy-evaluate-external-process 134 | body result-type result-params))) 135 | 136 | (defun org-babel-hy-evaluate-external-process 137 | (body &optional result-type result-params) 138 | "Evaluate BODY in external hy process. 139 | If RESULT-TYPE equals `output' then return standard output as a 140 | string. If RESULT-TYPE equals `value' then return the value of the 141 | last statement in BODY, as elisp." 142 | (let ((result 143 | (pcase result-type 144 | (`output (org-babel-eval 145 | (format "%s -c '%s'" org-babel-hy-command body) "")) 146 | (`value (let ((tmp-file (org-babel-temp-file "hy-"))) 147 | (org-babel-eval 148 | (format 149 | "%s -c '%s'" 150 | org-babel-hy-command 151 | (format 152 | (if (member "pp" result-params) 153 | org-babel-hy-pp-wrapper-method 154 | org-babel-hy-wrapper-method) 155 | body 156 | (org-babel-process-file-name tmp-file 'noquote))) "") 157 | (org-babel-eval-read-file tmp-file)))))) 158 | (org-babel-result-cond result-params 159 | result 160 | (org-babel-hy-table-or-string result)))) 161 | 162 | (defun org-babel-hy-evaluate-session 163 | (session body &optional result-type result-params) 164 | "Pass BODY to the Hy process in SESSION. 165 | If RESULT-TYPE equals `output' then return standard output as a 166 | string. If RESULT-TYPE equals `value' then return the value of the 167 | last statement in BODY, as elisp." 168 | (let* ((send-wait (lambda () (comint-send-input nil t))) 169 | (dump-last-value 170 | (lambda 171 | (tmp-file pp) 172 | (mapc 173 | (lambda 174 | (statement) 175 | (insert (org-babel-chomp statement)) (funcall send-wait)) 176 | (if pp 177 | (list 178 | "(import pprint)" 179 | (format "(with [f (open \"%s\" \"w\")] (.write f (.pformat pprint _)))" 180 | (org-babel-process-file-name tmp-file 'noquote))) 181 | (list (format "(with [f (open \"%s\" \"w\")] (.write f (str _)))" 182 | (org-babel-process-file-name tmp-file 'noquote))))))) 183 | (input-body (lambda (body) 184 | (mapc 185 | (lambda (line) 186 | (insert (org-babel-chomp line)) (funcall send-wait)) 187 | (list body)))) 188 | (results 189 | (pcase result-type 190 | (`output 191 | (replace-regexp-in-string "=> " "" 192 | (mapconcat 193 | #'org-trim 194 | (butlast 195 | (org-babel-comint-with-output 196 | (session org-babel-hy-eoe-indicator t body) 197 | (funcall input-body body) 198 | (insert (format "(import builtins)\n(builtins.print \"%s\")" 199 | org-babel-hy-eoe-indicator)) 200 | (funcall send-wait)) 201 | 2) "\n"))) 202 | (`value 203 | (let ((tmp-file (org-babel-temp-file "hy-"))) 204 | (org-babel-comint-with-output 205 | (session org-babel-hy-eoe-indicator t body) 206 | (let ((comint-process-echoes nil)) 207 | (funcall send-wait) 208 | (funcall input-body body) 209 | (funcall dump-last-value tmp-file 210 | (member "pp" result-params)) 211 | (insert (format "(import builtins)\n(builtins.print \"%s\")" 212 | org-babel-hy-eoe-indicator)) 213 | (funcall send-wait) (funcall send-wait))) 214 | (org-babel-eval-read-file tmp-file)))))) 215 | (unless (string= (substring org-babel-hy-eoe-indicator 1 -1) results) 216 | (org-babel-result-cond result-params 217 | (org-trim results) 218 | (org-babel-hy-table-or-string (org-trim results)))))) 219 | 220 | (defun org-babel-prep-session:hy (session params) 221 | "Prepare SESSION according to the header arguments in PARAMS. 222 | VARS contains resolved variable references" 223 | (let* ((session (org-babel-hy-initiate-session session)) 224 | (var-lines 225 | (org-babel-variable-assignments:hy params))) 226 | (org-babel-comint-in-buffer session 227 | (mapc (lambda (var) 228 | (end-of-line 1) (insert var) (comint-send-input) 229 | (org-babel-comint-wait-for-output session)) var-lines)) 230 | session)) 231 | 232 | (defun org-babel-load-session:hy (session body params) 233 | "Load BODY into SESSION." 234 | (save-window-excursion 235 | (let ((buffer (org-babel-prep-session:hy session params))) 236 | (with-current-buffer buffer 237 | (goto-char (process-mark (get-buffer-process (current-buffer)))) 238 | (insert (org-babel-chomp body))) 239 | buffer))) 240 | 241 | ;; helper functions 242 | 243 | (defun org-babel-hy-get-vars (params) 244 | "org-babel-get-header was removed in org version 8.3.3" 245 | (if (fboundp 'org-babel-get-header) 246 | (mapcar #'cdr (org-babel-get-header params :var)) 247 | (org-babel--get-vars params))) 248 | 249 | (defun org-babel-variable-assignments:hy (params) 250 | "Return list of hy statements assigning the block's variables." 251 | (mapcar 252 | (lambda (pair) 253 | (format "%s=%s" 254 | (car pair) 255 | (org-babel-hy-var-to-hy (cdr pair)))) 256 | (org-babel-hy-get-vars params))) 257 | 258 | (defun org-babel-hy-var-to-hy (var) 259 | "Convert VAR into a hy variable. 260 | Convert an elisp value into a string of hy source code 261 | specifying a variable of the same value." 262 | (if (listp var) 263 | (concat "[" (mapconcat #'org-babel-hy-var-to-hy var ", ") "]") 264 | (if (eq var 'hline) 265 | org-babel-hy-hline-to 266 | (format "%S" var)))) 267 | 268 | (defun org-babel-hy-table-or-string (results) 269 | "Convert RESULTS into an appropriate elisp value. 270 | If RESULTS look like a table, then convert them into an 271 | Emacs-lisp table, otherwise return the results as a string." 272 | (let ((res (org-babel-script-escape results))) 273 | (if (listp res) 274 | (mapcar (lambda (el) (if (not el) 275 | org-babel-hy-nil-to el)) 276 | res) 277 | res))) 278 | 279 | (defun org-babel-hy-read (results) 280 | "Convert RESULTS into an appropriate elisp value. 281 | If RESULTS look like a table, then convert them into an 282 | Emacs-lisp table, otherwise return the results as a string." 283 | (org-babel-read 284 | (if (and (stringp results) 285 | (string-prefix-p "[" results) 286 | (string-suffix-p "]" results)) 287 | (org-babel-read 288 | (concat "'" 289 | (replace-regexp-in-string 290 | "\\[" "(" (replace-regexp-in-string 291 | "\\]" ")" (replace-regexp-in-string 292 | ",[[:space:]]" " " 293 | (replace-regexp-in-string 294 | "'" "\"" results)))))) 295 | results))) 296 | 297 | ;; session helper functions 298 | 299 | (defvar org-babel-hy-buffers '((:default . "*Hy*"))) 300 | 301 | (defun org-babel-hy-session-buffer (session) 302 | "Return the buffer associated with SESSION." 303 | (cdr (assoc session org-babel-hy-buffers))) 304 | 305 | (defun org-babel-hy-with-earmuffs (session) 306 | (let ((name (if (stringp session) session (format "%s" session)))) 307 | (if (and (string= "*" (substring name 0 1)) 308 | (string= "*" (substring name (- (length name) 1)))) 309 | name 310 | (format "*%s*" name)))) 311 | 312 | (defun org-babel-hy-without-earmuffs (session) 313 | (let ((name (if (stringp session) session (format "%s" session)))) 314 | (if (and (string= "*" (substring name 0 1)) 315 | (string= "*" (substring name (- (length name) 1)))) 316 | (substring name 1 (- (length name) 1)) 317 | name))) 318 | 319 | (defvar hy-default-interpreter) 320 | (defvar hy-which-bufname) 321 | (defvar hy-shell-buffer-name) 322 | (defun org-babel-hy-initiate-session-by-key (&optional session) 323 | "Initiate a hy session. 324 | If there is not a current inferior-process-buffer in SESSION 325 | then create. Return the initialized session." 326 | (if (and (featurep 'hy-mode) (fboundp 'run-python)) 327 | (require 'hy-mode) 328 | (error "No function available for running an inferior Hy")) 329 | (save-window-excursion 330 | (let* ((session (if session (intern session) :default)) 331 | (hy-buffer (org-babel-hy-session-buffer session)) 332 | (cmd org-babel-hy-command)) 333 | (unless hy-buffer 334 | (setq hy-buffer (org-babel-hy-with-earmuffs session))) 335 | (let ((hy-shell-buffer-name 336 | (org-babel-hy-without-earmuffs hy-buffer))) 337 | (run-hy cmd)) 338 | (setq org-babel-hy-buffers 339 | (cons (cons session hy-buffer) 340 | (assq-delete-all session org-babel-hy-buffers))) 341 | session))) 342 | 343 | (defun org-babel-hy-initiate-session (&optional session _params) 344 | "Create a session named SESSION according to PARAMS." 345 | (unless (string= session "none") 346 | (org-babel-hy-session-buffer 347 | (org-babel-hy-initiate-session-by-key session)))) 348 | 349 | (provide 'ob-hy) 350 | ;;; ob-hy.el ends here 351 | -------------------------------------------------------------------------------- /test-ob-hy.el: -------------------------------------------------------------------------------- 1 | ;;; test-ob-hy.el --- tests for ob-hy.el 2 | 3 | ;; This file is not part of GNU Emacs. 4 | 5 | ;; This program is free software; you can redistribute it and/or modify 6 | ;; it under the terms of the GNU General Public License as published by 7 | ;; the Free Software Foundation, either version 3 of the License, or 8 | ;; (at your option) any later version. 9 | 10 | ;; This program is distributed in the hope that it will be useful, 11 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | ;; GNU General Public License for more details. 14 | 15 | ;; You should have received a copy of the GNU General Public License 16 | ;; along with this program. If not, see . 17 | 18 | ;;; Code: 19 | (require 'ert) 20 | (require 'org-id) 21 | 22 | (defconst ob-hy-test-dir 23 | (expand-file-name (file-name-directory (or load-file-name buffer-file-name)))) 24 | 25 | (defconst org-id-locations-file 26 | (expand-file-name ".test-org-id-locations" ob-hy-test-dir)) 27 | 28 | (defun ob-hy-test-update-id-locations () 29 | (let ((files (directory-files 30 | ob-hy-test-dir 'full 31 | "^\\([^.]\\|\\.\\([^.]\\|\\..\\)\\).*\\.org$"))) 32 | (org-id-update-id-locations files))) 33 | 34 | (defmacro org-test-at-id (id &rest body) 35 | "Run body after placing the point in the headline identified by ID." 36 | (declare (indent 1)) 37 | `(let* ((id-location (org-id-find ,id)) 38 | (id-file (car id-location)) 39 | (visited-p (get-file-buffer id-file)) 40 | to-be-removed) 41 | (unwind-protect 42 | (save-window-excursion 43 | (save-match-data 44 | (org-id-goto ,id) 45 | (setq to-be-removed (current-buffer)) 46 | (condition-case nil 47 | (progn 48 | (org-show-subtree) 49 | (org-show-block-all)) 50 | (error nil)) 51 | (save-restriction ,@body))) 52 | (unless (or visited-p (not to-be-removed)) 53 | (kill-buffer to-be-removed))))) 54 | 55 | (def-edebug-spec org-test-at-id (form body)) 56 | 57 | (unless (featurep 'ob-hy) 58 | (signal 'missing-test-dependency "Support for Hy code blocks")) 59 | 60 | (ert-deftest ob-hy/hy-executable () 61 | (should (executable-find org-babel-hy-command))) 62 | 63 | (ert-deftest ob-hy/ns-rt-value () 64 | "Test no session return-type: value." 65 | (if (executable-find org-babel-hy-command) 66 | (org-test-at-id "977ba858-a4aa-4108-8e61-43dd880d5b08" 67 | (org-babel-next-src-block 1) 68 | (should 69 | (string-equal 70 | "ob-hy" (org-babel-execute-src-block)))))) 71 | 72 | (ert-deftest ob-hy/ns-rt-output () 73 | "Test no session return-type: output." 74 | (if (executable-find org-babel-hy-command) 75 | (org-test-at-id "977ba858-a4aa-4108-8e61-43dd880d5b08" 76 | (org-babel-next-src-block 2) 77 | (should 78 | (string-equal 79 | "ob-hy\n" (org-babel-execute-src-block)))))) 80 | 81 | (ert-deftest ob-hy/ns-variable-int () 82 | (if (executable-find org-babel-hy-command) 83 | (org-test-at-id "1f5d82ee-93a4-4821-85fb-c855188beb65" 84 | (org-babel-next-src-block 1) 85 | (should 86 | (equal 87 | 5 (org-babel-execute-src-block)))))) 88 | 89 | (ert-deftest ob-hy/ns-variable-str () 90 | (if (executable-find org-babel-hy-command) 91 | (org-test-at-id "1f5d82ee-93a4-4821-85fb-c855188beb65" 92 | (org-babel-next-src-block 2) 93 | (should 94 | (string-equal "ob-hy" (org-babel-execute-src-block)))))) 95 | 96 | (ert-deftest ob-hy/ns-variable-list () 97 | (if (executable-find org-babel-hy-command) 98 | (org-test-at-id "1f5d82ee-93a4-4821-85fb-c855188beb65" 99 | (org-babel-next-src-block 3) 100 | (should 101 | (string-equal "(\"a\" \"b\" \"c\")" (org-babel-execute-src-block)))))) 102 | 103 | (ert-deftest ob-hy/ns-variable-tb () 104 | (if (executable-find org-babel-hy-command) 105 | (org-test-at-id "1f5d82ee-93a4-4821-85fb-c855188beb65" 106 | (org-babel-next-src-block 4) 107 | (should 108 | (string-equal "((1 2) (3 4))" (org-babel-execute-src-block)))))) 109 | 110 | (ert-deftest ob-hy/ns-multi-variables () 111 | (if (executable-find org-babel-hy-command) 112 | (org-test-at-id "1f5d82ee-93a4-4821-85fb-c855188beb65" 113 | (org-babel-next-src-block 5) 114 | (should 115 | (equal 12 (org-babel-execute-src-block)))))) 116 | 117 | (defun ob-hy-test-runall () 118 | (progn 119 | (ob-hy-test-update-id-locations) 120 | (ert t))) 121 | 122 | (provide 'ob-hy-test) 123 | -------------------------------------------------------------------------------- /test-ob-hy.org: -------------------------------------------------------------------------------- 1 | # -*- org-confirm-babel-evaluate: nil -*- 2 | #+OPTIONS: ^:nil 3 | 4 | * No session 5 | :PROPERTIES: 6 | :ID: 37bab689-027c-4189-9abc-9a993da1c099 7 | :END: 8 | ** return type 9 | :PROPERTIES: 10 | :ID: 977ba858-a4aa-4108-8e61-43dd880d5b08 11 | :END: 12 | #+NAME: ns-rt-value 13 | #+BEGIN_SRC hy :results value 14 | "ob-hy" 15 | #+END_SRC 16 | 17 | #+RESULTS: ns-rt-value 18 | : ob-hy 19 | 20 | #+NAME: ns-rt-output 21 | #+BEGIN_SRC hy :results output 22 | (print "ob-hy") 23 | #+END_SRC 24 | 25 | #+RESULTS: ns-rt-output 26 | : ob-hy 27 | 28 | ** variable 29 | :PROPERTIES: 30 | :ID: 1f5d82ee-93a4-4821-85fb-c855188beb65 31 | :END: 32 | 33 | #+NAME: ns-variable-int 34 | #+BEGIN_SRC hy :results value :var x=5 35 | x 36 | #+END_SRC 37 | 38 | #+RESULTS: ns-variable-int 39 | : 5 40 | 41 | #+NAME: ns-variable-str 42 | #+BEGIN_SRC hy :results value :var str="ob-hy" 43 | str 44 | #+END_SRC 45 | 46 | #+RESULTS: ns-variable-str 47 | : ob-hy 48 | 49 | #+NAME: ns-variable-list 50 | #+BEGIN_SRC hy :results value raw :var list='("a" "b" "c") 51 | list 52 | #+END_SRC 53 | 54 | #+RESULTS: ns-variable-list 55 | (u'a' u'b' u'c') 56 | 57 | #+NAME: num-tb 58 | | 1 | 2 | 59 | |---+---| 60 | | 3 | 4 | 61 | 62 | #+NAME: ns-variable-tb 63 | #+BEGIN_SRC hy :results value raw :var tb=num-tb :colnames no 64 | tb 65 | #+END_SRC 66 | 67 | #+RESULTS: ns-variable-tb 68 | ((1L 2L) (3L 4L)) 69 | 70 | #+NAME: ns-multi-variables 71 | #+BEGIN_SRC hy :results value :var x=3 y=4 z=5 72 | (+ x y z) 73 | #+END_SRC 74 | 75 | #+RESULTS: ns-multi-variables 76 | : 12 77 | 78 | * Session 79 | :PROPERTIES: 80 | :ID: fe04c2ad-1df7-4708-aca7-3f139e854f88 81 | :END: 82 | 83 | ** return type 84 | :PROPERTIES: 85 | :ID: 9b2000b6-97e1-4015-9db9-f97e39d82bf2 86 | :END: 87 | #+NAME: s-rt-value 88 | #+BEGIN_SRC hy :results value :session 89 | "ob-hy" 90 | #+END_SRC 91 | 92 | #+RESULTS: s-rt-value 93 | : ob-hy 94 | 95 | #+NAME: s-rt-output 96 | #+BEGIN_SRC hy :results output :session 97 | (print "ob-hy") 98 | #+END_SRC 99 | 100 | #+RESULTS: s-rt-output 101 | : ob-hy 102 | 103 | #+BEGIN_SRC elisp :results value 104 | (replace-regexp-in-string "=> " "" "=> => => ob-hy") 105 | #+END_SRC 106 | 107 | #+RESULTS: 108 | : ob-hy 109 | 110 | ** variable 111 | :PROPERTIES: 112 | :ID: 4f83532c-0db3-4b39-8c91-b83bc106ee64 113 | :END: 114 | 115 | #+NAME: s-variable-int 116 | #+BEGIN_SRC hy :results value :var x=5 :session 117 | x 118 | #+END_SRC 119 | 120 | #+RESULTS: s-variable-int 121 | : 5 122 | 123 | #+NAME: s-variable-str 124 | #+BEGIN_SRC hy :results value :var strVar="ob-hy" :session 125 | strVar 126 | #+END_SRC 127 | 128 | #+RESULTS: s-variable-str 129 | : ob-hy 130 | 131 | #+NAME: s-variable-list 132 | #+BEGIN_SRC hy :results value raw :var list='("a" "b" "c") :session 133 | list 134 | #+END_SRC 135 | 136 | #+RESULTS: s-variable-list 137 | (u'a' u'b' u'c') 138 | 139 | #+NAME: s-variable-tb 140 | #+BEGIN_SRC hy :results value raw :var tb=num-tb :colnames no :session 141 | tb 142 | #+END_SRC 143 | 144 | #+RESULTS: s-variable-tb 145 | ((1L 2L) (3L 4L)) 146 | 147 | #+NAME: s-multi-variables 148 | #+BEGIN_SRC hy :results value :var x=3 y=4 z=5 :session 149 | (+ x y z) 150 | #+END_SRC 151 | 152 | #+RESULTS: s-multi-variables 153 | : 12 154 | 155 | * Test 156 | :PROPERTIES: 157 | :ID: 0f17afd6-27b4-4186-aeb5-e0dd2b99a8fc 158 | :END: 159 | 160 | #+NAME: ob-hy-tests 161 | | functionality | block | arg | expected | results | pass | 162 | |-----------------------------------+--------------------+-----+---------------+---------------------+--------------------------------------------------------------------| 163 | | return-type : no session value | ns-rt-value | | ob-hy | ob-hy | pass | 164 | | return-type : no session output | ns-rt-output | | ob-hy | ob-hy | pass | 165 | | variable-int : no session | ns-variable-int | | 5 | 5 | pass | 166 | | variable-str : no session | ns-variable-str | | ob-hy | ob-hy | pass | 167 | | variable-list : no session | ns-variable-list | | ("a" "b" "c") | (u "a" u "b" u "c") | expected "(\"a\" \"b\" \"c\")" but was "(u \"a\" u \"b\" u \"c\")" | 168 | | variable-tb : no session | ns-variable-tb | | ((1 2) (3 4)) | ((1L 2L) (3L 4L)) | expected "((1 2) (3 4))" but was "((1L 2L) (3L 4L))" | 169 | | variable-multi: no session | ns-multi-variables | | 12 | 12 | pass | 170 | #+TBLFM: $5='(org-sbe $2) :: $6='(if (string= $4 $5) "pass" (format "expected %S but was %S" $4 $5)) 171 | --------------------------------------------------------------------------------