test-ob-R.el 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. ;;; test-ob-R.el --- tests for ob-R.el -*- lexical-binding: t; -*-
  2. ;; Copyright (c) 2011-2014, 2019 Eric Schulte
  3. ;; Authors: Eric Schulte
  4. ;; This file is not part of GNU Emacs.
  5. ;; This program is free software; you can redistribute it and/or modify
  6. ;; it under the terms of the GNU General Public License as published by
  7. ;; the Free Software Foundation, either version 3 of the License, or
  8. ;; (at your option) any later version.
  9. ;; This program is distributed in the hope that it will be useful,
  10. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. ;; GNU General Public License for more details.
  13. ;; You should have received a copy of the GNU General Public License
  14. ;; along with this program. If not, see <https://www.gnu.org/licenses/>.
  15. ;;; Code:
  16. (org-test-for-executable "R")
  17. (require 'ob-core)
  18. (unless (featurep 'ess)
  19. (signal 'missing-test-dependency "ESS"))
  20. (defvar ess-ask-for-ess-directory)
  21. (defvar ess-history-file)
  22. (unless (featurep 'ob-R)
  23. (signal 'missing-test-dependency "Support for R code blocks"))
  24. (ert-deftest test-ob-R/simple-session ()
  25. (let (ess-ask-for-ess-directory ess-history-file)
  26. (org-test-with-temp-text
  27. "#+begin_src R :session R\n paste(\"Yep!\")\n#+end_src\n"
  28. (should (string= "Yep!" (org-babel-execute-src-block))))))
  29. (ert-deftest test-ob-R/colnames-yes-header-argument ()
  30. (org-test-with-temp-text "#+name: eg
  31. | col |
  32. |-----|
  33. | a |
  34. | b |
  35. #+header: :colnames yes
  36. #+header: :var x = eg
  37. #+begin_src R
  38. x
  39. #+end_src"
  40. (org-babel-next-src-block)
  41. (should (equal '(("col") hline ("a") ("b"))
  42. (org-babel-execute-src-block)))))
  43. (ert-deftest test-ob-R/colnames-nil-header-argument ()
  44. (org-test-with-temp-text "#+name: eg
  45. | col |
  46. |-----|
  47. | a |
  48. | b |
  49. #+header: :colnames nil
  50. #+header: :var x = eg
  51. #+begin_src R
  52. x
  53. #+end_src"
  54. (org-babel-next-src-block)
  55. (should (equal '(("col") hline ("a") ("b"))
  56. (org-babel-execute-src-block)))))
  57. (ert-deftest test-ob-R/colnames-no-header-argument ()
  58. (org-test-with-temp-text "#+name: eg
  59. | col |
  60. |-----|
  61. | a |
  62. | b |
  63. #+header: :colnames no
  64. #+header: :var x = eg
  65. #+begin_src R
  66. x
  67. #+end_src"
  68. (org-babel-next-src-block)
  69. (should (equal '(("col") ("a") ("b"))
  70. (org-babel-execute-src-block)))))
  71. (ert-deftest test-ob-R/results-file ()
  72. (let (ess-ask-for-ess-directory ess-history-file)
  73. (org-test-with-temp-text
  74. "#+NAME: TESTSRC
  75. #+BEGIN_SRC R :results file
  76. a <- file.path(\"junk\", \"test.org\")
  77. a
  78. #+END_SRC"
  79. (goto-char (point-min)) (org-babel-execute-maybe)
  80. (org-babel-goto-named-result "TESTSRC") (forward-line 1)
  81. (should (string= "[[file:junk/test.org]]"
  82. (buffer-substring-no-properties (point-at-bol) (point-at-eol))))
  83. (goto-char (point-min)) (forward-line 1)
  84. (insert "#+header: :session\n")
  85. (goto-char (point-min)) (org-babel-execute-maybe)
  86. (org-babel-goto-named-result "TESTSRC") (forward-line 1)
  87. (should (string= "[[file:junk/test.org]]"
  88. (buffer-substring-no-properties (point-at-bol) (point-at-eol)))))))
  89. (ert-deftest test-ob-r/output-with-<> ()
  90. "make sure angle brackets are well formatted"
  91. (let (ess-ask-for-ess-directory ess-history-file)
  92. (should (string="[1] \"<X> <Y> <!>\"
  93. [1] \"one <two> three\"
  94. [1] \"end35\"
  95. "
  96. (org-test-with-temp-text "#+begin_src R :results output
  97. print(\"<X> <Y> <!>\")
  98. print(\"one <two> three\")
  99. print(\"end35\")
  100. #+end_src
  101. "
  102. (org-babel-execute-src-block))
  103. ))))
  104. ;; (ert-deftest test-ob-r/output-with-error ()
  105. ;; "make sure angle brackets are well formatted"
  106. ;; (let (ess-ask-for-ess-directory ess-history-file)
  107. ;; (should (string="Error in print(1/a) : object 'a' not found"
  108. ;; (org-test-with-temp-text "#+begin_src R :results output
  109. ;; print(1/a)
  110. ;; #+end_src
  111. ;; "
  112. ;; (org-babel-execute-src-block))
  113. ;; ))))
  114. (ert-deftest test-ob-R/output-nonprinted ()
  115. (let (ess-ask-for-ess-directory ess-history-file)
  116. (org-test-with-temp-text
  117. "#+begin_src R :results output
  118. 4.0 * 3.5
  119. log(10)
  120. log10(10)
  121. \(3 + 1) * 5
  122. 3^-1
  123. 1/0
  124. #+end_src"
  125. (should (string= "[1] 14\n[1] 2.302585\n[1] 1\n[1] 20\n[1] 0.3333333\n[1] Inf\n" (org-babel-execute-src-block))))))
  126. (ert-deftest test-ob-r/NA-blank ()
  127. "For :results value, NAs should be empty"
  128. (let (ess-ask-for-ess-directory ess-history-file)
  129. (should (equal '(("A" "B") hline ("" 1) (1 2) (1 "") (1 4) (1 4))
  130. (org-test-with-temp-text "#+BEGIN_SRC R :results value :colnames yes
  131. data.frame(A=c(NA,1,1,1,1),B=c(1,2,NA,4,4))
  132. #+end_src"
  133. (org-babel-execute-src-block))))))
  134. (ert-deftest ob-session-async-R-simple-session-async-value ()
  135. (let (ess-ask-for-ess-directory
  136. ess-history-file
  137. (org-babel-temporary-directory "/tmp")
  138. (org-confirm-babel-evaluate nil))
  139. (org-test-with-temp-text
  140. "#+begin_src R :session R :async yes\n Sys.sleep(.1)\n paste(\"Yep!\")\n#+end_src\n"
  141. (should (let ((expected "Yep!"))
  142. (and (not (string= expected (org-babel-execute-src-block)))
  143. (string= expected
  144. (progn
  145. (sleep-for 0 200)
  146. (goto-char (org-babel-where-is-src-block-result))
  147. (org-babel-read-result)))))))))
  148. (ert-deftest ob-session-async-R-simple-session-async-output ()
  149. (let (ess-ask-for-ess-directory
  150. ess-history-file
  151. (org-babel-temporary-directory "/tmp")
  152. (org-confirm-babel-evaluate nil))
  153. (org-test-with-temp-text
  154. "#+begin_src R :session R :results output :async yes\n Sys.sleep(.1)\n 1:5\n#+end_src\n"
  155. (should (let ((expected "[1] 1 2 3 4 5"))
  156. (and (not (string= expected (org-babel-execute-src-block)))
  157. (string= expected
  158. (progn
  159. (sleep-for 0 200)
  160. (goto-char (org-babel-where-is-src-block-result))
  161. (org-babel-read-result)))))))))
  162. (ert-deftest ob-session-async-R-named-output ()
  163. (let (ess-ask-for-ess-directory
  164. ess-history-file
  165. (org-babel-temporary-directory "/tmp")
  166. org-confirm-babel-evaluate
  167. (src-block "#+begin_src R :async :session R :results output\n 1:5\n#+end_src")
  168. (results-before "\n\n#+NAME: foobar\n#+RESULTS:\n: [1] 1")
  169. (results-after "\n\n#+NAME: foobar\n#+RESULTS:\n: [1] 1 2 3 4 5\n"))
  170. (org-test-with-temp-text
  171. (concat src-block results-before)
  172. (should (progn (org-babel-execute-src-block)
  173. (sleep-for 0 200)
  174. (string= (concat src-block results-after)
  175. (buffer-string)))))))
  176. (ert-deftest ob-session-async-R-named-value ()
  177. (let (ess-ask-for-ess-directory
  178. ess-history-file
  179. org-confirm-babel-evaluate
  180. (org-babel-temporary-directory "/tmp")
  181. (src-block "#+begin_src R :async :session R :results value\n paste(\"Yep!\")\n#+end_src")
  182. (results-before "\n\n#+NAME: foobar\n#+RESULTS:\n: [1] 1")
  183. (results-after "\n\n#+NAME: foobar\n#+RESULTS:\n: Yep!\n"))
  184. (org-test-with-temp-text
  185. (concat src-block results-before)
  186. (should (progn (org-babel-execute-src-block)
  187. (sleep-for 0 200)
  188. (string= (concat src-block results-after)
  189. (buffer-string)))))))
  190. (ert-deftest ob-session-async-R-output-drawer ()
  191. (let (ess-ask-for-ess-directory
  192. ess-history-file
  193. org-confirm-babel-evaluate
  194. (org-babel-temporary-directory "/tmp")
  195. (src-block "#+begin_src R :async :session R :results output drawer\n 1:5\n#+end_src")
  196. (result "\n\n#+RESULTS:\n:results:\n[1] 1 2 3 4 5\n:end:\n"))
  197. (org-test-with-temp-text
  198. src-block
  199. (should (progn (org-babel-execute-src-block)
  200. (sleep-for 0 200)
  201. (string= (concat src-block result)
  202. (buffer-string)))))))
  203. (ert-deftest ob-session-async-R-value-drawer ()
  204. (let (ess-ask-for-ess-directory
  205. ess-history-file
  206. org-confirm-babel-evaluate
  207. (org-babel-temporary-directory "/tmp")
  208. (src-block "#+begin_src R :async :session R :results value drawer\n 1:3\n#+end_src")
  209. (result "\n\n#+RESULTS:\n:results:\n1\n2\n3\n:end:\n"))
  210. (org-test-with-temp-text
  211. src-block
  212. (should (progn (org-babel-execute-src-block)
  213. (sleep-for 0 200)
  214. (string= (concat src-block result)
  215. (buffer-string)))))))
  216. (provide 'test-ob-R)
  217. ;;; test-ob-R.el ends here