diff --git a/ob-prolog.el b/ob-prolog.el index 5a3c508..59d93b9 100644 --- a/ob-prolog.el +++ b/ob-prolog.el @@ -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) @@ -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 diff --git a/ob-prolog.org b/ob-prolog.org deleted file mode 100644 index 4f840fc..0000000 --- a/ob-prolog.org +++ /dev/null @@ -1,50 +0,0 @@ -#+TITLE: Tests for ob-prolog - - -* Simple tests - - #+NAME: f - #+BEGIN_SRC elisp - '(1 2 3) - #+END_SRC - - #+RESULTS: f - | 1 | 2 | 3 | - - #+HEADER: :session *prolog-1* - #+HEADER: :system swipl - #+HEADER: :var a=f() - #+HEADER: :var b="asdf" - #+HEADER: :goal main(A, D) - #+BEGIN_SRC prolog - main(A, B) :- recorded(a, A), recorded(b, C), append([C], A, B). - #+END_SRC - - #+RESULTS: - : A = [1, 2, 3], - : D = [asdf, 1, 2, 3]. - - #+HEADER: :session *prolog-1* - #+HEADER: :goal other(A) - #+BEGIN_SRC prolog - other(A) :- A is 3. - #+END_SRC - - #+RESULTS: - : A = 3. - - - #+begin_src prolog - :- format('Hello, ~a.', org_mode). - #+end_src - - #+RESULTS: - : Hello, org_mode. - - #+begin_src prolog :session *prolog-1* - :- format('Hello, ~a.', org_session). - #+end_src - - #+RESULTS: - : Hello, org_session. - : true. diff --git a/test/test-ob-prolog.el b/test/test-ob-prolog.el new file mode 100644 index 0000000..4f062fc --- /dev/null +++ b/test/test-ob-prolog.el @@ -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 . + +;;; 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 diff --git a/test/test-ob-prolog.org b/test/test-ob-prolog.org new file mode 100644 index 0000000..88b4575 --- /dev/null +++ b/test/test-ob-prolog.org @@ -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