test.el 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. (require 'cl)
  2. (ert-deftest ert-test-emacspy-py-import ()
  3. (should-error (py-import "test" "NON_EXISTING_MOD")
  4. :type 'python-exception) )
  5. (ert-deftest ert-test-emacspy-py-run-string ()
  6. (should (py-run-string "test" "ospath.realpath('/')"))
  7. (should-error (py-run-string "test" "some bullshit")
  8. :type 'python-exception)
  9. (should (py-run-string "test" "ospath.realpath('/')" "run_string_test_var"))
  10. (should (string= "/" (py-get-global-variable "test" "run_string_test_var")))
  11. (should (py-run-string "test" "1==1"))
  12. (should-not (py-run-string "test" "1==2")) )
  13. (ert-deftest ert-test-emacspy-data-bool ()
  14. (should (eq 't (py-run-string "test" "True")))
  15. (should (eq nil (py-run-string "test" "False")))
  16. (should (eq 't (py-get-global-variable
  17. "test" (py-set-global "test" 't "test_bool"))))
  18. (should (eq nil (py-get-global-variable
  19. "test" (py-set-global "test" () "test_bool")))) )
  20. (ert-deftest ert-test-emacspy-py-call-method ()
  21. (should (string= "/" (emacspy--call "test" "ospath" "realpath" nil '("/") nil)))
  22. (should (emacspy--call "test" "ospath" "realpath" "call_method_test_var" '("/") nil))
  23. (should (string= "/" (py-get-global-variable "test" "call_method_test_var" )))
  24. (should-error (emacspy--call "test" "ospath" "DUMMY_METHOD" nil (emacspy-alist2hash nil))
  25. :type 'python-exception)
  26. (should-error (emacspy--call "test" "NON_EXISTING_OBJECT" "DUMMY_METHOD" nil (emacspy-alist2hash nil))
  27. :type 'python-exception)
  28. (should-error (emacspy--call "test" "string" "digits" nil nil nil)
  29. :type 'python-exception) )
  30. (ert-deftest ert-test-emacspy-py-get-global-variable ()
  31. (should (string= "__main__" (py-get-global-variable "test" "__name__")))
  32. (should-error (py-get-global-variable "test" "NON_EXISTING_VARIABLE")) )
  33. (ert-deftest ert-test-emacspy-emacspy--call-function ()
  34. (should (eq 3 (emacspy--call "test" "len" nil nil '("123") nil)))
  35. (should (emacspy--call "test" "len" nil "call_function_test_var" '("123") nil))
  36. (should (eq 3 (py-get-global-variable "test" "call_function_test_var")))
  37. (should-error (emacspy--call "test" "NON-EXISTING-FUNCTION" nil nil '("123") (emacspy-alist2hash nil))
  38. :type 'python-exception)
  39. (should (emacspy--call "test" "dict" nil "call_function_kvargs_test_var" nil
  40. (emacspy-alist2hash '(("some_test" . 1) ("test" . "also_test"))) ))
  41. (let ((ret (py-get-global-variable "test" "call_function_kvargs_test_var")))
  42. (should (hash-table-p ret))
  43. (should (eq 1 (gethash "some_test" ret )))
  44. (should (string= "also_test" (gethash "test" ret))) ) )
  45. (ert-deftest ert-test-emacspy-py-get-object-attr ()
  46. (should (string= "0123456789" (py-get-object-attr "test" "string" "digits")))
  47. (should (py-get-object-attr "test" "string" "digits" "test_digs"))
  48. (should (string= "0123456789" (py-get-global-variable "test" "test_digs")))
  49. (should-error (py-get-object-attr "test" "NON_EXISTING_OBJECT" "digits" "test_digs")
  50. :type 'python-exception)
  51. (should-error (py-get-object-attr "test" "string" "NON_EXISTING_ATTR")
  52. :type 'python-exception) )
  53. (ert-deftest ert-test-emacspy-py-set-global ()
  54. (should (string= "test_str" (py-set-global "test" "test_value" "test_str")))
  55. (should (string= "test_value" (py-get-global-variable "test" "test_str"))) )
  56. (ert-deftest ert-test-emacspy-import-custom-module ()
  57. (should (py-import "test" "sys"))
  58. (should (py-get-object-attr "test" "sys" "path" "syspath"))
  59. (should-not (emacspy--call "test" "syspath" "append" nil '("./tests/") nil))
  60. (should (py-import "test" "emacspy_test"))
  61. (should (py-get-object-attr "test" "emacspy_test" "test_obj" "test_obj"))
  62. (should (string= "test" (emacspy--call "test" "test_obj" "get_string" nil nil nil))) )
  63. (ert-deftest ert-test-emacspy-non-existing-interpreter ()
  64. (should-error (py-run-string "NON_EXISTING" "True")))
  65. (ert-deftest ert-test-emacspy-duplicate-load ()
  66. (should-error (progn (load "emacspy-module")
  67. (load "emacspy-module"))))
  68. (ert-deftest ert-test-emacspy-create-destroy-subinterpreter ()
  69. (let ((sub "test_subinterpreter"))
  70. (should (py-make-interpreter sub))
  71. (should (py-make-interpreter sub))
  72. (should (py-destroy-interpreter sub))
  73. (should-error (py-get-global-variable sub "__name__")) ))
  74. (ert-deftest ert-test-emacspy-subinterpreter-isolation () ;; Test if we really switch
  75. (let ((sub "test2"))
  76. (should (py-make-interpreter sub))
  77. (should (py-set-global sub 't "testvarisolation"))
  78. (should-error (py-get-global-variable "test" "testvarisolation"))
  79. (should (py-destroy-interpreter sub)) ))
  80. (ert-deftest ert-test-emacspy-list-subiterpreters ()
  81. (let ((ret (py-list-interpreters)))
  82. (should (listp ret))
  83. (should (eq 1 (length ret)))
  84. (should (string= "test" (nth 0 ret))) ))
  85. (ert-deftest ert-test-emacspy-data-int ()
  86. (should (eq 1 (py-get-global-variable
  87. "test" (py-set-global "test" 1 "test_int"))))
  88. (should (eq -1 (py-get-global-variable
  89. "test" (py-set-global "test" -1 "test_int"))))
  90. (should (eq 0 (py-get-global-variable
  91. "test" (py-set-global "test" 0 "test_int")))) )
  92. (ert-deftest ert-test-emacspy-data-float ()
  93. (should (= 0.5 (py-get-global-variable
  94. "test" (py-set-global "test" 0.5 "test_int"))))
  95. (should (= -0.5 (py-get-global-variable
  96. "test" (py-set-global "test" -0.5 "test_int"))))
  97. (should (= 0.0 (py-get-global-variable
  98. "test" (py-set-global "test" 0.0 "test_int")))) )
  99. (ert-deftest ert-test-emacspy-data-str ()
  100. (should (string= "" (py-run-string "test" "''")))
  101. (should (string= "str" (py-run-string "test" "'str'")))
  102. (should (string= "субтитри" (py-run-string "test" "'субтитри'"))) )
  103. (ert-deftest ert-test-emacspy-data-list ()
  104. (let ((lst (py-run-string "test" "[1, True, 2, 'test']")))
  105. (should (eq 4 (length lst)))
  106. (should (eq 1 (nth 0 lst)))
  107. (should (eq 't (nth 1 lst)))
  108. (should (eq 2 (nth 2 lst)))
  109. (should (string= "test" (nth 3 lst))) )
  110. (let ((lst (py-run-string "test" "(False,)")))
  111. (should (eq 1 (length lst)))
  112. (should (eq nil (nth 0 lst))) )
  113. (let ((lst (py-run-string "test" "([1, 2, 3], 'test', False)")))
  114. (should (eq 3 (length lst)))
  115. (let ((nested-lst (nth 0 lst)))
  116. (should (listp nested-lst))
  117. (should (eq 3 (length nested-lst)))
  118. (should (eq 3 (nth 2 nested-lst))) )
  119. (should (string= "test" (nth 1 lst)))
  120. (should (eq nil (nth 2 lst))) )
  121. (let ((lst (py-get-global-variable
  122. "test"
  123. (py-set-global "test" '(t nil 3 "test") "test_list"))))
  124. (should (eq 't (nth 0 lst)))
  125. (should (eq nil (nth 1 lst)))
  126. (should (eq 3 (nth 2 lst)))
  127. (should (string= "test" (nth 3 lst)))
  128. (should (eq 4 (length lst))) )
  129. (let ((lst (py-get-global-variable
  130. "test"
  131. (py-set-global "test" '(("test") (1 2 3)) "test_list"))))
  132. (should (eq 2 (length lst)))
  133. (let ((nested-lst (nth 0 lst)))
  134. (should (listp nested-lst))
  135. (should (eq 1 (length nested-lst)))
  136. (should (string= "test" (car nested-lst))) )
  137. (let ((nested-lst (nth 1 lst)))
  138. (should (listp nested-lst))
  139. (should (eq 3 (length nested-lst)))
  140. (should (eq 3 (nth 2 nested-lst))) ) ))
  141. (ert-deftest ert-test-emacspy-data-hash ()
  142. (should (functionp 'emacspy--hash-table-to-lists))
  143. (let ((hash (make-hash-table))
  144. (nhash (make-hash-table))
  145. (empty-hash (make-hash-table)))
  146. (puthash 1 "test" hash)
  147. (puthash 2 nil hash)
  148. (puthash "list" '(1) hash)
  149. (puthash "key" -1.5 nhash)
  150. (puthash "hash" nhash hash)
  151. (should (py-set-global "test" empty-hash "test_empty_hash"))
  152. (should (py-set-global "test" hash "test_hash"))
  153. (should (py-run-string "test" "test_hash[1]=='test'"))
  154. (should (py-run-string "test" "test_hash[2]==False"))
  155. (should (py-run-string "test" "test_hash['list'][0]==1"))
  156. (should (py-run-string "test" "test_hash['hash']['key']==-1.5"))
  157. (let ((py-hash (py-get-global-variable "test" "test_hash")))
  158. (should (hash-table-p py-hash))
  159. (should (string= "test" (gethash 1 py-hash)))
  160. (should (eq nil (gethash 2 py-hash)))
  161. (should (listp (gethash "list" py-hash)))
  162. (should (eq 1 (nth 0 (gethash "list" py-hash)) ))
  163. (should (= -1.5 (gethash "key" (gethash "hash" py-hash)))) ) ))