From 75ca370de8e7167e77c36ea9cd92f99a3659b86d Mon Sep 17 00:00:00 2001 From: Eric Schulte Date: Mon, 11 May 2009 19:41:56 -0700 Subject: [PATCH] added tests, and fixed uncovered bugs tests currently cover the following all of which are passing - basic execution - referencing tables - referencing other source blocks --- litorgy/litorgy-ref.el | 7 ++- litorgy/litorgy-script.el | 14 +++-- litorgy/litorgy-table.el | 23 ++++---- litorgy/litorgy.el | 25 ++++---- rorg.org | 116 ++++++++++++++++++++++++++++++++++++++ 5 files changed, 156 insertions(+), 29 deletions(-) diff --git a/litorgy/litorgy-ref.el b/litorgy/litorgy-ref.el index 984676510..110eac9c4 100644 --- a/litorgy/litorgy-ref.el +++ b/litorgy/litorgy-ref.el @@ -92,10 +92,11 @@ return nil." (let ((case-fold-search t) type args new-ref) ;; case search? ;; assign any arguments to pass to source block - (when (string-match "\\(.+\\)\(\\(.+\\)\)" ref) + (when (string-match "\\(.+\\)\(\\(.*\\)\)" ref) (save-match-data - (setq args (mapcar (lambda (ref) (cons :var ref)) - (split-string (match-string 2 ref) ",[ \f\t\n\r\v]*")))) + (if (> (length (match-string 2)) 0) + (setq args (mapcar (lambda (ref) (cons :var ref)) + (split-string (match-string 2 ref) ",[ \f\t\n\r\v]*"))))) (setq ref (match-string 1 ref))) (when (string-match "\\(.+\\):\\(.+\\)" ref) (find-file (match-string 1 ref)) diff --git a/litorgy/litorgy-script.el b/litorgy/litorgy-script.el index 11dd80025..ed7629d85 100644 --- a/litorgy/litorgy-script.el +++ b/litorgy/litorgy-script.el @@ -48,7 +48,8 @@ automatically generated wrapper for `litorgy-script-execute'.") def main %s end -puts main().inspect +results = main() +puts (results.class == String) ? results : results.inspect ") (defvar litorgy-script-python-wrapper-method @@ -108,12 +109,17 @@ Emacs-lisp table, otherwise return the results as a string." (if (string-match "^\\[.+\\]$" results) ;; somewhat hacky, but thanks to similarities between languages ;; it seems to work - (replace-regexp-in-string + (litorgy-read + (replace-regexp-in-string "\\[" "(" (replace-regexp-in-string "\\]" ")" (replace-regexp-in-string ", " " " (replace-regexp-in-string - "'" "\"" results)))) - results))) + "'" "\"" results))))) + ;; strip trailing endline + (progn + (while (string= "\n" (substring results -1)) + (setq results (substring results 0 -1))) + results)))) (provide 'litorgy-script) ;;; litorgy-script.el ends here diff --git a/litorgy/litorgy-table.el b/litorgy/litorgy-table.el index b6e7cb1b1..0b269fabc 100644 --- a/litorgy/litorgy-table.el +++ b/litorgy/litorgy-table.el @@ -68,16 +68,19 @@ source code block. #+begin_src emacs-lisp :var results=source-block(n=2, m=3) :results silent results #+end_src" - (let ((params (eval `(litorgy-parse-header-arguments - (concat ":var results=" - (symbol-name ,source-block) - "(" - (mapconcat (lambda (var-spec) - (format "%S=%s" (first var-spec) (second var-spec))) - ',variables ", ") - ")"))))) - (litorgy-execute-src-block - nil (list "emacs-lisp" "results" (org-combine-plists params '((:results . "silent"))))))) + (unless (stringp source-block) (setq source-block (symbol-name source-block))) + (if (and source-block (> (length source-block) 0)) + (let ((params (eval `(litorgy-parse-header-arguments + (concat ":var results=" + ,source-block + "(" + (mapconcat (lambda (var-spec) + (format "%S=%s" (first var-spec) (second var-spec))) + ',variables ", ") + ")"))))) + (litorgy-execute-src-block + nil (list "emacs-lisp" "results" (org-combine-plists params '((:results . "silent")))))) + "")) (provide 'litorgy-table) ;;; litorgy-table.el ends here diff --git a/litorgy/litorgy.el b/litorgy/litorgy.el index b7b1dc9ab..7dd777792 100644 --- a/litorgy/litorgy.el +++ b/litorgy/litorgy.el @@ -121,8 +121,9 @@ the header arguments specified at the source code block." (error "Language is not in `litorgy-interpreters': %s" lang)) (setq result (funcall cmd body params)) (if arg - (progn (message (format "%S" result)) result) - (litorgy-insert-result result (cdr (assoc :results params)))))) + (message (if (stringp result) result (format "%S" result))) + (litorgy-insert-result result (cdr (assoc :results params)))) + result)) (defun litorgy-eval-buffer (&optional arg) "Replace EVAL snippets in the entire buffer." @@ -264,17 +265,17 @@ string. This is taken almost directly from `org-read-prop'." (if (and (stringp cell) (not (equal cell ""))) - (let ((out (string-to-number cell))) - (if (equal out 0) - (if (or (equal "(" (substring cell 0 1)) - (equal "'" (substring cell 0 1))) - (read cell) - (if (string-match "^\\(+0\\|-0\\|0\\)$" cell) - 0 - (progn (set-text-properties 0 (length cell) nil cell) - cell))) - out)) + (if (litorgy-number-p cell) + (string-to-number cell) + (if (or (equal "(" (substring cell 0 1)) + (equal "'" (substring cell 0 1))) + (read cell) + (progn (set-text-properties 0 (length cell) nil cell) cell))) cell)) +(defun litorgy-number-p (string) + "Return t if STRING represents a number" + (string-match "^[[:digit:]]*\\.?[[:digit:]]*$" string)) + (provide 'litorgy) ;;; litorgy.el ends here diff --git a/rorg.org b/rorg.org index e18f57e23..3e06fbd7e 100644 --- a/rorg.org +++ b/rorg.org @@ -355,6 +355,122 @@ recognition of ruby arrays as such. | 1 | 2 | 3 | 4 | +* Tests + +Evaluate all the cells in this table for a comprehensive test of the +litorgy functionality. + +#+TBLNAME: litorgy-tests +| functionality | block | expected | results | pass | +|-------------------------+------------------+-------------+-------------+------| +| basic evaluation | | | | pass | +|-------------------------+------------------+-------------+-------------+------| +| emacs lisp | basic-elisp | 5 | 5 | pass | +| shell | basic-shell | 6 | 6 | pass | +| ruby | basic-ruby | litorgy | litorgy | pass | +| python | basic-python | hello world | hello world | pass | +| R | basic-R | 13 | 13 | pass | +|-------------------------+------------------+-------------+-------------+------| +| tables | | | | pass | +|-------------------------+------------------+-------------+-------------+------| +| emacs lisp | table-elisp | 3 | 3 | pass | +| ruby | table-ruby | 1-2-3 | 1-2-3 | pass | +| python | table-python | 5 | 5 | pass | +| R | table-R | 3.5 | 3.5 | pass | +|-------------------------+------------------+-------------+-------------+------| +| source block references | | | | pass | +|-------------------------+------------------+-------------+-------------+------| +| all languages | chained-ref-last | Array | Array | pass | +#+TBLFM: $4='(sbe $2)::$5='(if (string= $3 $4) "pass" (format "expected %S but was %S" $3 $4)) + +** basic tests + +#+srcname: basic-elisp +#+begin_src emacs-lisp :results silent +(+ 1 4) +#+end_src + +#+srcname: basic-shell +#+begin_src sh :results silent +expr 1 + 5 +#+end_src + + +#+srcname: basic-ruby +#+begin_src ruby :results silent +"litorgy" +#+end_src + +#+srcname: basic-python +#+begin_src python :results silent +'hello world' +#+end_src + +#+srcname: basic-R +#+begin_src R :results silent +b <- 9 +b + 4 +#+end_src + +** read tables + +#+tblname: test-table +| 1 | 2 | 3 | +| 4 | 5 | 6 | + +#+srcname: table-elisp +#+begin_src emacs-lisp :results silent :var table=test-table +(length (car table)) +#+end_src + +#+srcname: table-ruby +#+begin_src ruby :results silent :var table=test-table +table.first.join("-") +#+end_src + +#+srcname: table-python +#+begin_src python :var table=test-table +table[1][1] +#+end_src + +#+srcname: table-R +#+begin_src R :var table=test-table +mean(mean(table)) +#+end_src + +** references + +Lets pass a references through all of our languages... + +Lets start by reversing the table from the previous examples + +#+srcname: chained-ref-first +#+begin_src python :var table = test-table +table.reverse +#+end_src + +Take the first part of the list + +#+srcname: chained-ref-second +#+begin_src R :var table = chained-ref-first +table[1] +#+end_src + +Turn the numbers into string + +#+srcname: chained-ref-third +#+begin_src emacs-lisp :var table = chained-ref-second +(mapcar (lambda (el) (format "%S" el)) table) +#+end_src + +and Check that it is still a list + +#+srcname: chained-ref-last +#+begin_src ruby :var table=chained-ref-third +table.class.name +#+end_src + + * Sandbox :PROPERTIES: :CUSTOM_ID: sandbox