Skip to content

Commit

Permalink
Test by name and better session tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ljos committed May 31, 2015
1 parent 31d663a commit 238651c
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 26 deletions.
70 changes: 47 additions & 23 deletions test/test-ob-prolog.el
Original file line number Diff line number Diff line change
Expand Up @@ -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)))))

Expand Down
10 changes: 7 additions & 3 deletions test/test-ob-prolog.org
Original file line number Diff line number Diff line change
@@ -1,28 +1,31 @@
#+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
fourtyone(A) :- A is 41.
#+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),
Expand All @@ -36,6 +39,7 @@
'(1 2 3)
#+END_SRC

#+NAME: interaction-test
#+HEADER: :var a=f()
#+HEADER: :var b=0
#+HEADER: :goal main(A)
Expand Down

0 comments on commit 238651c

Please sign in to comment.