Skip to content

Commit

Permalink
New tests and bug-fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
ljos committed May 30, 2015
1 parent fd497eb commit 31d663a
Show file tree
Hide file tree
Showing 4 changed files with 129 additions and 55 deletions.
9 changes: 4 additions & 5 deletions ob-prolog.el
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@

(defun org-babel-prolog--variable-assignment (pair)
(format "recorda('%s', %s)"
(replace-regexp-in-string "'" "\\'" (car pair))
(car pair)
(org-babel-prolog--elisp-to-pl (cdr pair))))

(defun org-babel-variable-assignments:prolog (params)
Expand Down Expand Up @@ -149,10 +149,9 @@ resulting in running just the body through the Prolog process."
tmp-file
(replace-regexp-in-string
"\"" "\\\"" (or goal "halt")))))
(write-region (org-babel-chomp body) nil tmp-file nil 'no-message)
(with-temp-buffer
(call-process-shell-command command nil t)
(buffer-string))))
(with-temp-file tmp-file
(insert (org-babel-chomp body)))
(org-babel-eval command "")))

(defun org-babel-prolog-evaluate-session (session goal body)
"Evaluates the GOAL in the BODY of the prolog block in the
Expand Down
50 changes: 0 additions & 50 deletions ob-prolog.org

This file was deleted.

80 changes: 80 additions & 0 deletions test/test-ob-prolog.el
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
;;; test-ob-prolog.el --- tests for ob-prolog.el

;; Copyright (c) 2015 Bjarte Johansen
;; Authors: Bjarte Johansen

;; This file is not part of GNU Emacs.

;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.

;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <http://www.gnu.org/licenses/>.

;;; Code:

(require 'ert)
(require 'org-id)


(unless (featurep 'ob-prolog)
(signal 'missing-test-dependency "Support for Prolog code blocks"))

(defmacro test-ob-prolog/test-src-block (number &rest body)
(declare (indent 1))
(let ((buf (make-symbol "buf"))
(visited-p (make-symbol "visited-p"))
(file-pos (make-symbol "file-pos"))
(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))))))
(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
(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
(should (string= "Hello, world!"
(org-babel-execute-src-block)))))

(ert-deftest test-ob-prolog/simple-running-session ()
(test-ob-prolog/test-src-block 3
(should (string= "A = 41."
(org-babel-execute-src-block)))))

(ert-deftest test-ob-prolog/simple-running-session ()
(test-ob-prolog/test-src-block 4
(should (string= "A = 42."
(org-babel-execute-src-block)))))

(ert-deftest test-ob-prolog/simple-running-session ()
(test-ob-prolog/test-src-block 6
(should (string= "A = [0, 1, 2, 3]."
(org-babel-execute-src-block)))))

;;; test-ob-prolog ends here
45 changes: 45 additions & 0 deletions test/test-ob-prolog.org
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#+PROPERTY: results silent scalar
#+TITLE: Tests for ob-prolog


* Basic prolog source block
#+BEGIN_SRC prolog
:- format('Hello, ~a.', org_mode).
#+END_SRC

* Source block with goal
#+BEGIN_SRC prolog :goal main
main :-
write('Hello, world!').
#+END_SRC

* Simple running session
#+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)
#+BEGIN_SRC prolog
answer(C) :-
fourtyone(B),
C is B+1.
#+END_SRC


* Test interaction with other blocks
#+NAME: f
#+BEGIN_SRC elisp :results vector
'(1 2 3)
#+END_SRC

#+HEADER: :var a=f()
#+HEADER: :var b=0
#+HEADER: :goal main(A)
#+HEADER: :session *prolog-1*
#+BEGIN_SRC prolog
main(B) :- recorded(a, A), recorded(b, C), append([C], A, B).
#+END_SRC

0 comments on commit 31d663a

Please sign in to comment.