test-ob-julia.el 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. ;;; test-ob-python.el --- tests for ob-python.el -*- lexical-binding: t; -*-
  2. ;; Copyright (c) 2011-2014, 2019, 2021 Eric Schulte
  3. ;; Authors: Pedro Bruel, based on test-ob-python.el by Eric Schulte
  4. ;; Maintainer: Pedro Bruel <pedro.bruel@gmail.com>
  5. ;; This file is not part of GNU Emacs.
  6. ;; This program is free software; you can redistribute it and/or modify
  7. ;; it under the terms of the GNU General Public License as published by
  8. ;; the Free Software Foundation, either version 3 of the License, or
  9. ;; (at your option) any later version.
  10. ;; This program is distributed in the hope that it will be useful,
  11. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ;; GNU General Public License for more details.
  14. ;; You should have received a copy of the GNU General Public License
  15. ;; along with this program. If not, see <https://www.gnu.org/licenses/>.
  16. ;;; Code:
  17. (org-test-for-executable "julia")
  18. (require 'ob-core)
  19. (unless (featurep 'ob-julia)
  20. (signal 'missing-test-dependency "Support for julia code blocks"))
  21. (unless (featurep 'ess)
  22. (signal 'missing-test-dependency "ESS"))
  23. (ert-deftest test-ob-julia/colnames-yes-header-argument ()
  24. (should
  25. (equal '(("col") hline ("a") ("b"))
  26. (org-test-with-temp-text "#+name: eg
  27. | col |
  28. |-----|
  29. | a |
  30. | b |
  31. #+header: :colnames yes
  32. #+header: :var x = eg
  33. <point>#+begin_src julia
  34. return x
  35. #+end_src"
  36. (org-babel-execute-src-block)))))
  37. (ert-deftest test-ob-julia/colnames-yes-header-argument-again ()
  38. (should
  39. (equal '(("a") hline ("b*") ("c*"))
  40. (org-test-with-temp-text "#+name: less-cols
  41. | a |
  42. |---|
  43. | b |
  44. | c |
  45. #+header: :colnames yes
  46. <point>#+begin_src julia :var tab=less-cols
  47. return [[val + '*' for val in row] for row in tab]
  48. #+end_src"
  49. (org-babel-execute-src-block)))))
  50. (ert-deftest test-ob-julia/colnames-nil-header-argument ()
  51. (should
  52. (equal '(("col") hline ("a") ("b"))
  53. (org-test-with-temp-text "#+name: eg
  54. | col |
  55. |-----|
  56. | a |
  57. | b |
  58. #+header: :colnames nil
  59. #+header: :var x = eg
  60. <point>#+begin_src julia
  61. return x
  62. #+end_src"
  63. (org-babel-execute-src-block)))))
  64. (ert-deftest test-ob-julia/colnames-no-header-argument-again ()
  65. (should
  66. (equal '(("a*") ("b*") ("c*"))
  67. (org-test-with-temp-text "#+name: less-cols
  68. | a |
  69. |---|
  70. | b |
  71. | c |
  72. #+header: :colnames no
  73. <point>#+begin_src julia :var tab=less-cols
  74. return [[val + '*' for val in row] for row in tab]
  75. #+end_src"
  76. (org-babel-execute-src-block)))))
  77. (ert-deftest test-ob-julia/colnames-no-header-argument ()
  78. (should
  79. (equal '(("col") ("a") ("b"))
  80. (org-test-with-temp-text "#+name: eg
  81. | col |
  82. |-----|
  83. | a |
  84. | b |
  85. #+header: :colnames no
  86. #+header: :var x = eg
  87. <point>#+begin_src julia
  88. return x
  89. #+end_src"
  90. (org-babel-execute-src-block)))))
  91. (ert-deftest test-ob-julia/session-multiline ()
  92. (should
  93. (equal "20"
  94. (org-test-with-temp-text "#+begin_src julia :session :results output
  95. foo = 0
  96. for _ in range(10):
  97. foo += 1
  98. foo += 1
  99. print(foo)
  100. #+end_src"
  101. (org-babel-execute-src-block)))))
  102. (ert-deftest test-ob-julia/insert-necessary-blank-line-when-sending-code-to-interpreter ()
  103. (should
  104. (equal 2 (org-test-with-temp-text "#+begin_src julia :session :results value
  105. if True:
  106. 1
  107. 2
  108. #+end_src"
  109. ;; Previously, while adding `:session' to a normal code
  110. ;; block, also need to add extra blank lines to end
  111. ;; indent block or indicate logical sections. Now, the
  112. ;; `org-babel-julia-evaluate-session' can do it
  113. ;; automatically:
  114. ;;
  115. ;; >>> if True:
  116. ;; >>> 1
  117. ;; >>> <insert_blank_line_here>
  118. ;; >>> 2
  119. (org-babel-execute-maybe)
  120. (org-babel-execute-src-block)))))
  121. (ert-deftest test-ob-julia/if-else-block ()
  122. (should
  123. (equal "success" (org-test-with-temp-text "#+begin_src julia :session :results value
  124. value = 'failure'
  125. if False:
  126. pass
  127. else:
  128. value = 'success'
  129. value
  130. #+end_src"
  131. (org-babel-execute-src-block)))))
  132. (ert-deftest test-ob-julia/indent-block-with-blank-lines ()
  133. (should
  134. (equal 20
  135. (org-test-with-temp-text "#+begin_src julia :session :results value
  136. foo = 0
  137. for i in range(10):
  138. foo += 1
  139. foo += 1
  140. foo
  141. #+end_src"
  142. (org-babel-execute-src-block)))))
  143. (ert-deftest test-ob-julia/assign-underscore ()
  144. (should
  145. (equal "success"
  146. (org-test-with-temp-text "#+begin_src julia :session :results value
  147. _ = 'failure'
  148. 'success'
  149. #+end_src"
  150. (org-babel-execute-src-block)))))
  151. (ert-deftest test-ob-julia/multiline-var ()
  152. (should
  153. (equal "a\nb\nc"
  154. (org-test-with-temp-text "#+begin_src julia :var text=\"a\\nb\\nc\"
  155. return text
  156. #+end_src"
  157. (org-babel-execute-src-block)))))
  158. (ert-deftest test-ob-julia/multiline-str ()
  159. (should
  160. (equal "a\nb\nc"
  161. (org-test-with-temp-text "#+begin_src julia
  162. text=\"a\\nb\\nc\"
  163. return text
  164. #+end_src"
  165. (org-babel-execute-src-block)))))
  166. (ert-deftest test-ob-julia/header-var-assignment ()
  167. (should
  168. (equal "success"
  169. (org-test-with-temp-text "#+begin_src julia :var text=\"failure\"
  170. text
  171. text=\"success\"
  172. return text
  173. #+end_src"
  174. (org-babel-execute-src-block)))))
  175. (ert-deftest test-ob-julia/session-value-sleep ()
  176. (should
  177. (equal "success"
  178. (org-test-with-temp-text "#+begin_src julia :session :results value
  179. import time
  180. time.sleep(.1)
  181. 'success'
  182. #+end_src"
  183. (org-babel-execute-src-block)))))
  184. (ert-deftest test-ob-julia/async-simple-session-output ()
  185. (let ((org-babel-temporary-directory temporary-file-directory)
  186. (org-confirm-babel-evaluate nil))
  187. (org-test-with-temp-text
  188. "#+begin_src julia :session :async yes :results output
  189. import time
  190. time.sleep(.1)
  191. print('Yep!')
  192. #+end_src\n"
  193. (should (let ((expected "Yep!"))
  194. (and (not (string= expected (org-babel-execute-src-block)))
  195. (string= expected
  196. (progn
  197. (sleep-for 0 200)
  198. (goto-char (org-babel-where-is-src-block-result))
  199. (org-babel-read-result)))))))))
  200. (ert-deftest test-ob-julia/async-named-output ()
  201. (let (org-confirm-babel-evaluate
  202. (org-babel-temporary-directory temporary-file-directory)
  203. (src-block "#+begin_src julia :async :session :results output
  204. print(\"Yep!\")
  205. #+end_src")
  206. (results-before "
  207. #+NAME: foobar
  208. #+RESULTS:
  209. : Nope!")
  210. (results-after "
  211. #+NAME: foobar
  212. #+RESULTS:
  213. : Yep!
  214. "))
  215. (org-test-with-temp-text
  216. (concat src-block results-before)
  217. (should (progn (org-babel-execute-src-block)
  218. (sleep-for 0 200)
  219. (string= (concat src-block results-after)
  220. (buffer-string)))))))
  221. (ert-deftest test-ob-julia/async-output-drawer ()
  222. (let (org-confirm-babel-evaluate
  223. (org-babel-temporary-directory temporary-file-directory)
  224. (src-block "#+begin_src julia :async :session :results output drawer
  225. print(list(range(3)))
  226. #+end_src")
  227. (result "
  228. #+RESULTS:
  229. :results:
  230. [0, 1, 2]
  231. :end:
  232. "))
  233. (org-test-with-temp-text
  234. src-block
  235. (should (progn (org-babel-execute-src-block)
  236. (sleep-for 0 200)
  237. (string= (concat src-block result)
  238. (buffer-string)))))))
  239. (provide 'test-ob-julia)
  240. ;;; test-ob-julia.el ends here