test-ob-python.el 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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 <http://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. ;; FIXME workaround to prevent starting prompt leaking into output
  89. (run-python)
  90. (sleep-for 0 10)
  91. (should
  92. (equal "20"
  93. (org-test-with-temp-text "#+begin_src python :session :results output
  94. foo = 0
  95. for _ in range(10):
  96. foo += 1
  97. foo += 1
  98. print(foo)
  99. #+end_src"
  100. (org-babel-execute-src-block)))))
  101. (ert-deftest test-ob-python/insert-necessary-blank-line-when-sending-code-to-interpreter ()
  102. (should
  103. (equal 2 (org-test-with-temp-text "#+begin_src python :session :results value
  104. if True:
  105. 1
  106. 2
  107. #+end_src"
  108. ;; Previously, while adding `:session' to a normal code
  109. ;; block, also need to add extra blank lines to end
  110. ;; indent block or indicate logical sections. Now, the
  111. ;; `org-babel-python-evaluate-session' can do it
  112. ;; automatically:
  113. ;;
  114. ;; >>> if True:
  115. ;; >>> 1
  116. ;; >>> <insert_blank_line_here>
  117. ;; >>> 2
  118. (org-babel-execute-maybe)
  119. (org-babel-execute-src-block)))))
  120. (provide 'test-ob-python)
  121. ;;; test-ob-python.el ends here