test-ob-python.el 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. ;;; test-ob-python.el --- tests for ob-python.el
  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. (unless (featurep 'ob-python)
  18. (signal 'missing-test-dependency "Support for Python code blocks"))
  19. (ert-deftest test-ob-python/colnames-yes-header-argument ()
  20. (should
  21. (equal '(("col") hline ("a") ("b"))
  22. (org-test-with-temp-text "#+name: eg
  23. | col |
  24. |-----|
  25. | a |
  26. | b |
  27. #+header: :colnames yes
  28. #+header: :var x = eg
  29. <point>#+begin_src python
  30. return x
  31. #+end_src"
  32. (org-babel-execute-src-block)))))
  33. (ert-deftest test-ob-python/colnames-yes-header-argument-again ()
  34. (should
  35. (equal '(("a") hline ("b*") ("c*"))
  36. (org-test-with-temp-text "#+name: less-cols
  37. | a |
  38. |---|
  39. | b |
  40. | c |
  41. #+header: :colnames yes
  42. <point>#+begin_src python :var tab=less-cols
  43. return [[val + '*' for val in row] for row in tab]
  44. #+end_src"
  45. (org-babel-execute-src-block)))))
  46. (ert-deftest test-ob-python/colnames-nil-header-argument ()
  47. (should
  48. (equal '(("col") hline ("a") ("b"))
  49. (org-test-with-temp-text "#+name: eg
  50. | col |
  51. |-----|
  52. | a |
  53. | b |
  54. #+header: :colnames nil
  55. #+header: :var x = eg
  56. <point>#+begin_src python
  57. return x
  58. #+end_src"
  59. (org-babel-execute-src-block)))))
  60. (ert-deftest test-ob-python/colnames-no-header-argument-again ()
  61. (should
  62. (equal '(("a*") ("b*") ("c*"))
  63. (org-test-with-temp-text "#+name: less-cols
  64. | a |
  65. |---|
  66. | b |
  67. | c |
  68. #+header: :colnames no
  69. <point>#+begin_src python :var tab=less-cols
  70. return [[val + '*' for val in row] for row in tab]
  71. #+end_src"
  72. (org-babel-execute-src-block)))))
  73. (ert-deftest test-ob-python/colnames-no-header-argument ()
  74. (should
  75. (equal '(("col") ("a") ("b"))
  76. (org-test-with-temp-text "#+name: eg
  77. | col |
  78. |-----|
  79. | a |
  80. | b |
  81. #+header: :colnames no
  82. #+header: :var x = eg
  83. <point>#+begin_src python
  84. return x
  85. #+end_src"
  86. (org-babel-execute-src-block)))))
  87. (ert-deftest test-ob-python/session-multiline ()
  88. (should
  89. (equal "20"
  90. (org-test-with-temp-text "#+begin_src python :session :results output
  91. foo = 0
  92. for _ in range(10):
  93. foo += 1
  94. foo += 1
  95. print(foo)
  96. #+end_src"
  97. (org-babel-execute-src-block)))))
  98. (ert-deftest test-ob-python/insert-necessary-blank-line-when-sending-code-to-interpreter ()
  99. (should
  100. (equal 2 (org-test-with-temp-text "#+begin_src python :session :results value
  101. if True:
  102. 1
  103. 2
  104. #+end_src"
  105. ;; Previously, while adding `:session' to a normal code
  106. ;; block, also need to add extra blank lines to end
  107. ;; indent block or indicate logical sections. Now, the
  108. ;; `org-babel-python-evaluate-session' can do it
  109. ;; automatically:
  110. ;;
  111. ;; >>> if True:
  112. ;; >>> 1
  113. ;; >>> <insert_blank_line_here>
  114. ;; >>> 2
  115. (org-babel-execute-maybe)
  116. (org-babel-execute-src-block)))))
  117. (ert-deftest test-ob-python/if-else-block ()
  118. (should
  119. (equal "success" (org-test-with-temp-text "#+begin_src python :session :results value
  120. value = 'failure'
  121. if False:
  122. pass
  123. else:
  124. value = 'success'
  125. value
  126. #+end_src"
  127. (org-babel-execute-src-block)))))
  128. (ert-deftest test-ob-python/indent-block-with-blank-lines ()
  129. (should
  130. (equal 20
  131. (org-test-with-temp-text "#+begin_src python :session :results value
  132. foo = 0
  133. for i in range(10):
  134. foo += 1
  135. foo += 1
  136. foo
  137. #+end_src"
  138. (org-babel-execute-src-block)))))
  139. (ert-deftest test-ob-python/assign-underscore ()
  140. (should
  141. (equal "success"
  142. (org-test-with-temp-text "#+begin_src python :session :results value
  143. _ = 'failure'
  144. 'success'
  145. #+end_src"
  146. (org-babel-execute-src-block)))))
  147. (ert-deftest test-ob-python/multiline-var ()
  148. (should
  149. (equal "a\nb\nc"
  150. (org-test-with-temp-text "#+begin_src python :var text=\"a\\nb\\nc\"
  151. return text
  152. #+end_src"
  153. (org-babel-execute-src-block)))))
  154. (ert-deftest test-ob-python/multiline-str ()
  155. (should
  156. (equal "a\nb\nc"
  157. (org-test-with-temp-text "#+begin_src python
  158. text=\"a\\nb\\nc\"
  159. return text
  160. #+end_src"
  161. (org-babel-execute-src-block)))))
  162. (ert-deftest test-ob-python/header-var-assignment ()
  163. (should
  164. (equal "success"
  165. (org-test-with-temp-text "#+begin_src python :var text=\"failure\"
  166. text
  167. text=\"success\"
  168. return text
  169. #+end_src"
  170. (org-babel-execute-src-block)))))
  171. (ert-deftest test-ob-python/session-value-sleep ()
  172. (should
  173. (equal "success"
  174. (org-test-with-temp-text "#+begin_src python :session :results value
  175. import time
  176. time.sleep(.1)
  177. 'success'
  178. #+end_src"
  179. (org-babel-execute-src-block)))))
  180. (provide 'test-ob-python)
  181. ;;; test-ob-python.el ends here