summaryrefslogtreecommitdiff
path: root/rust-cargo-tests.el
diff options
context:
space:
mode:
authorSvilen Ivanov <isvilen@applicata.bg>2024-05-13 13:03:40 +0300
committerSvilen Ivanov <isvilen@applicata.bg>2024-05-14 14:54:25 +0300
commit947ebf993818f7135e12d4b0da6f5b05ba790c14 (patch)
treeb0f81270f68977ac27a2deb07413be7adc071df2 /rust-cargo-tests.el
parente54bbae8c4c2af580b5721ad5ac151f2ad19293e (diff)
downloadrust-mode-947ebf993818f7135e12d4b0da6f5b05ba790c14.tar.gz
Support running interactive programs
Diffstat (limited to 'rust-cargo-tests.el')
-rw-r--r--rust-cargo-tests.el53
1 files changed, 53 insertions, 0 deletions
diff --git a/rust-cargo-tests.el b/rust-cargo-tests.el
new file mode 100644
index 0000000..f49cba0
--- /dev/null
+++ b/rust-cargo-tests.el
@@ -0,0 +1,53 @@
+;;; rust-cargo-tests.el --- ERT tests for rust-cargo.el -*- lexical-binding: t; -*-
+(require 'rust-cargo)
+(require 'ert)
+
+(defun rust-test--wait-process-exit ()
+ "Wait for comint process for current buffer to exit."
+ (let ((proc (get-buffer-process (current-buffer))))
+ (while (not (eq (process-status proc) 'exit))
+ (sit-for 0.2))))
+
+(defun rust-test--send-process-string (string)
+ "Send STRING to comint process for current buffer."
+ (let ((proc (get-buffer-process (current-buffer))))
+ (comint-send-string proc string)))
+
+(defmacro rust-test--with-main-file-buffer (expr)
+ `(let* ((test-dir (expand-file-name "test-project/" default-directory))
+ (main-file (expand-file-name "src/main.rs" test-dir)))
+ (save-current-buffer
+ (find-file main-file)
+ ,expr)))
+
+(defun rust-test--find-string (string)
+ "Find STRING in current buffer."
+ (goto-char (point-min))
+ (not (null (search-forward string nil t))))
+
+
+(ert-deftest rust-test-compile ()
+ (skip-unless (executable-find rust-cargo-bin))
+ (setq rust-cargo-default-arguments "")
+ (rust-test--with-main-file-buffer
+ (with-current-buffer (rust-compile)
+ (rust-test--wait-process-exit)
+ (should (rust-test--find-string "Compilation finished")))))
+
+(ert-deftest rust-test-run ()
+ (skip-unless (executable-find rust-cargo-bin))
+ (setq rust-cargo-default-arguments "")
+ (rust-test--with-main-file-buffer
+ (with-current-buffer (rust-run)
+ (rust-test--wait-process-exit)
+ (should (rust-test--find-string "***run not interactive")))))
+
+(ert-deftest rust-test-run-interactive ()
+ (skip-unless (executable-find rust-cargo-bin))
+ (setq rust-cargo-default-arguments "interactive")
+ (rust-test--with-main-file-buffer
+ ;; '(4) is default value when called interactively with universal argument
+ (with-current-buffer (rust-run '(4))
+ (rust-test--send-process-string "1234\n")
+ (rust-test--wait-process-exit)
+ (should (rust-test--find-string "***run interactive: 1234")))))