test-ob-julia.el 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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. (ert-deftest test-ob-julia/colnames-yes-header-argument ()
  22. (should
  23. (equal '(("col") hline ("a") ("b"))
  24. (org-test-with-temp-text "#+name: eg
  25. | col |
  26. |-----|
  27. | a |
  28. | b |
  29. #+header: :colnames yes
  30. #+header: :var x = eg
  31. <point>#+begin_src julia
  32. return x
  33. #+end_src"
  34. (org-babel-execute-src-block)))))
  35. (ert-deftest test-ob-julia/colnames-yes-header-argument-again ()
  36. (should
  37. (equal '(("a") hline ("b*") ("c*"))
  38. (org-test-with-temp-text "#+name: less-cols
  39. | a |
  40. |---|
  41. | b |
  42. | c |
  43. #+header: :colnames yes
  44. <point>#+begin_src julia :var tab=less-cols
  45. return [[val + '*' for val in row] for row in tab]
  46. #+end_src"
  47. (org-babel-execute-src-block)))))
  48. (ert-deftest test-ob-julia/colnames-nil-header-argument ()
  49. (should
  50. (equal '(("col") hline ("a") ("b"))
  51. (org-test-with-temp-text "#+name: eg
  52. | col |
  53. |-----|
  54. | a |
  55. | b |
  56. #+header: :colnames nil
  57. #+header: :var x = eg
  58. <point>#+begin_src julia
  59. return x
  60. #+end_src"
  61. (org-babel-execute-src-block)))))
  62. (ert-deftest test-ob-julia/colnames-no-header-argument-again ()
  63. (should
  64. (equal '(("a*") ("b*") ("c*"))
  65. (org-test-with-temp-text "#+name: less-cols
  66. | a |
  67. |---|
  68. | b |
  69. | c |
  70. #+header: :colnames no
  71. <point>#+begin_src julia :var tab=less-cols
  72. return [[val + '*' for val in row] for row in tab]
  73. #+end_src"
  74. (org-babel-execute-src-block)))))
  75. (ert-deftest test-ob-julia/colnames-no-header-argument ()
  76. (should
  77. (equal '(("col") ("a") ("b"))
  78. (org-test-with-temp-text "#+name: eg
  79. | col |
  80. |-----|
  81. | a |
  82. | b |
  83. #+header: :colnames no
  84. #+header: :var x = eg
  85. <point>#+begin_src julia
  86. return x
  87. #+end_src"
  88. (org-babel-execute-src-block)))))
  89. (ert-deftest test-ob-julia/session-multiline ()
  90. (should
  91. (equal "20"
  92. (org-test-with-temp-text "#+begin_src julia :session :results output
  93. foo = 0
  94. for _ in range(10):
  95. foo += 1
  96. foo += 1
  97. print(foo)
  98. #+end_src"
  99. (org-babel-execute-src-block)))))
  100. (ert-deftest test-ob-julia/insert-necessary-blank-line-when-sending-code-to-interpreter ()
  101. (should
  102. (equal 2 (org-test-with-temp-text "#+begin_src julia :session :results value
  103. if True:
  104. 1
  105. 2
  106. #+end_src"
  107. ;; Previously, while adding `:session' to a normal code
  108. ;; block, also need to add extra blank lines to end
  109. ;; indent block or indicate logical sections. Now, the
  110. ;; `org-babel-julia-evaluate-session' can do it
  111. ;; automatically:
  112. ;;
  113. ;; >>> if True:
  114. ;; >>> 1
  115. ;; >>> <insert_blank_line_here>
  116. ;; >>> 2
  117. (org-babel-execute-maybe)
  118. (org-babel-execute-src-block)))))
  119. (ert-deftest test-ob-julia/if-else-block ()
  120. (should
  121. (equal "success" (org-test-with-temp-text "#+begin_src julia :session :results value
  122. value = 'failure'
  123. if False:
  124. pass
  125. else:
  126. value = 'success'
  127. value
  128. #+end_src"
  129. (org-babel-execute-src-block)))))
  130. (ert-deftest test-ob-julia/indent-block-with-blank-lines ()
  131. (should
  132. (equal 20
  133. (org-test-with-temp-text "#+begin_src julia :session :results value
  134. foo = 0
  135. for i in range(10):
  136. foo += 1
  137. foo += 1
  138. foo
  139. #+end_src"
  140. (org-babel-execute-src-block)))))
  141. (ert-deftest test-ob-julia/assign-underscore ()
  142. (should
  143. (equal "success"
  144. (org-test-with-temp-text "#+begin_src julia :session :results value
  145. _ = 'failure'
  146. 'success'
  147. #+end_src"
  148. (org-babel-execute-src-block)))))
  149. (ert-deftest test-ob-julia/multiline-var ()
  150. (should
  151. (equal "a\nb\nc"
  152. (org-test-with-temp-text "#+begin_src julia :var text=\"a\\nb\\nc\"
  153. return text
  154. #+end_src"
  155. (org-babel-execute-src-block)))))
  156. (ert-deftest test-ob-julia/multiline-str ()
  157. (should
  158. (equal "a\nb\nc"
  159. (org-test-with-temp-text "#+begin_src julia
  160. text=\"a\\nb\\nc\"
  161. return text
  162. #+end_src"
  163. (org-babel-execute-src-block)))))
  164. (ert-deftest test-ob-julia/header-var-assignment ()
  165. (should
  166. (equal "success"
  167. (org-test-with-temp-text "#+begin_src julia :var text=\"failure\"
  168. text
  169. text=\"success\"
  170. return text
  171. #+end_src"
  172. (org-babel-execute-src-block)))))
  173. (ert-deftest test-ob-julia/session-value-sleep ()
  174. (should
  175. (equal "success"
  176. (org-test-with-temp-text "#+begin_src julia :session :results value
  177. import time
  178. time.sleep(.1)
  179. 'success'
  180. #+end_src"
  181. (org-babel-execute-src-block)))))
  182. (ert-deftest test-ob-julia/async-simple-session-output ()
  183. (let ((org-babel-temporary-directory temporary-file-directory)
  184. (org-confirm-babel-evaluate nil))
  185. (org-test-with-temp-text
  186. "#+begin_src julia :session :async yes :results output
  187. import time
  188. time.sleep(.1)
  189. print('Yep!')
  190. #+end_src\n"
  191. (should (let ((expected "Yep!"))
  192. (and (not (string= expected (org-babel-execute-src-block)))
  193. (string= expected
  194. (progn
  195. (sleep-for 0 200)
  196. (goto-char (org-babel-where-is-src-block-result))
  197. (org-babel-read-result)))))))))
  198. (ert-deftest test-ob-julia/async-named-output ()
  199. (let (org-confirm-babel-evaluate
  200. (org-babel-temporary-directory temporary-file-directory)
  201. (src-block "#+begin_src julia :async :session :results output
  202. print(\"Yep!\")
  203. #+end_src")
  204. (results-before "
  205. #+NAME: foobar
  206. #+RESULTS:
  207. : Nope!")
  208. (results-after "
  209. #+NAME: foobar
  210. #+RESULTS:
  211. : Yep!
  212. "))
  213. (org-test-with-temp-text
  214. (concat src-block results-before)
  215. (should (progn (org-babel-execute-src-block)
  216. (sleep-for 0 200)
  217. (string= (concat src-block results-after)
  218. (buffer-string)))))))
  219. (ert-deftest test-ob-julia/async-output-drawer ()
  220. (let (org-confirm-babel-evaluate
  221. (org-babel-temporary-directory temporary-file-directory)
  222. (src-block "#+begin_src julia :async :session :results output drawer
  223. print(list(range(3)))
  224. #+end_src")
  225. (result "
  226. #+RESULTS:
  227. :results:
  228. [0, 1, 2]
  229. :end:
  230. "))
  231. (org-test-with-temp-text
  232. src-block
  233. (should (progn (org-babel-execute-src-block)
  234. (sleep-for 0 200)
  235. (string= (concat src-block result)
  236. (buffer-string)))))))
  237. (provide 'test-ob-julia)
  238. ;;; test-ob-julia.el ends here