test-ob-python.el 6.9 KB

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