From 238651c028b2d456a90fd5d2ed81588fabeee4c6 Mon Sep 17 00:00:00 2001 From: Bjarte Johansen Date: Sun, 31 May 2015 15:58:54 +0200 Subject: [PATCH] Test by name and better session tests --- test/test-ob-prolog.el | 70 +++++++++++++++++++++++++++-------------- test/test-ob-prolog.org | 10 ++++-- 2 files changed, 54 insertions(+), 26 deletions(-) diff --git a/test/test-ob-prolog.el b/test/test-ob-prolog.el index 4f062fc..eb6bfee 100644 --- a/test/test-ob-prolog.el +++ b/test/test-ob-prolog.el @@ -27,53 +27,77 @@ (unless (featurep 'ob-prolog) (signal 'missing-test-dependency "Support for Prolog code blocks")) -(defmacro test-ob-prolog/test-src-block (number &rest body) +(defmacro test-ob-prolog/test-src-block (name &rest body) (declare (indent 1)) (let ((buf (make-symbol "buf")) (visited-p (make-symbol "visited-p")) (file-pos (make-symbol "file-pos")) + (active-session (make-symbol "active-session")) (file "test-ob-prolog.org")) - `(let ((,visited-p (get-file-buffer ,file)) - (,buf (find-file-noselect ,file))) - (unwind-protect - (with-current-buffer ,buf - (goto-char (point-min)) - (condition-case nil - (progn - (org-show-subtree) - (org-show-block-all)) - (error nil)) - (dotimes (,(make-symbol "_") ,number) - (org-babel-next-src-block)) - (save-restriction ,@body)) - (unless ,visited-p - (kill-buffer ,buf)))))) + `(let* ((,visited-p (get-file-buffer ,file)) + (,buf (or ,visited-p + (find-file-noselect ,file))) + ,active-session) + (with-current-buffer ,buf + (unwind-protect + (save-match-data + (save-excursion + (goto-char (point-min)) + (condition-case nil + (progn + (org-show-subtree) + (org-show-block-all)) + (error nil)) + (org-babel-goto-named-src-block ,(symbol-name name)) + (save-excursion + (ignore-errors + (org-babel-previous-src-block) + (end-of-line) + (let* ((info (nth 2 (org-babel-get-src-block-info))) + (session (cdr (assq :session info))) + (bound (point))) + (setq ,active-session + (unless (string= "none" session) + session)) + (goto-char (point-min)) + (while (search-forward (concat ":session " session) bound t) + (org-babel-execute-src-block))))) + (save-restriction ,@body))) + (unless ,visited-p + (kill-buffer ,buf)) + (when ,active-session + (kill-buffer ,active-session))))))) (def-edebug-spec test-ob-prolog/test-src-block (form body)) (ert-deftest test-ob-prolog/simple-execution () "Test simple execution of prolog source block." - (test-ob-prolog/test-src-block 1 + (test-ob-prolog/test-src-block basic-test (should (string= "Hello, org_mode." (org-babel-execute-src-block))))) (ert-deftest test-ob-prolog/goal-execution () "Test execution of goal argument to prolog source block." - (test-ob-prolog/test-src-block 2 + (test-ob-prolog/test-src-block goal-test (should (string= "Hello, world!" (org-babel-execute-src-block))))) (ert-deftest test-ob-prolog/simple-running-session () - (test-ob-prolog/test-src-block 3 + "Test running a session." + (test-ob-prolog/test-src-block session-test (should (string= "A = 41." (org-babel-execute-src-block))))) -(ert-deftest test-ob-prolog/simple-running-session () - (test-ob-prolog/test-src-block 4 +(ert-deftest test-ob-prolog/call-predicate-in-session () + "Test calling a predicate in the session that is defined in +another block." + (test-ob-prolog/test-src-block other-predicate-test (should (string= "A = 42." (org-babel-execute-src-block))))) -(ert-deftest test-ob-prolog/simple-running-session () - (test-ob-prolog/test-src-block 6 +(ert-deftest test-ob-prolog/interacting-with-other-block () + "Test interacting with source block that is not a prolog +block." + (test-ob-prolog/test-src-block interaction-test (should (string= "A = [0, 1, 2, 3]." (org-babel-execute-src-block))))) diff --git a/test/test-ob-prolog.org b/test/test-ob-prolog.org index 88b4575..f0deab3 100644 --- a/test/test-ob-prolog.org +++ b/test/test-ob-prolog.org @@ -1,19 +1,21 @@ #+PROPERTY: results silent scalar #+TITLE: Tests for ob-prolog - * Basic prolog source block + #+NAME: basic-test #+BEGIN_SRC prolog :- format('Hello, ~a.', org_mode). #+END_SRC * Source block with goal + #+NAME: goal-test #+BEGIN_SRC prolog :goal main main :- write('Hello, world!'). #+END_SRC * Simple running session + #+NAME: session-test #+HEADER: :session *prolog-1* #+HEADER: :goal fourtyone(A) #+BEGIN_SRC prolog @@ -21,8 +23,9 @@ #+END_SRC * Calling predicate from session - #+HEADER :session *prolog-1* - #+HEADER :goal answer(A) + #+NAME: other-predicate-test + #+HEADER: :goal answer(A) + #+HEADER: :session *prolog-1* #+BEGIN_SRC prolog answer(C) :- fourtyone(B), @@ -36,6 +39,7 @@ '(1 2 3) #+END_SRC + #+NAME: interaction-test #+HEADER: :var a=f() #+HEADER: :var b=0 #+HEADER: :goal main(A)