|
| 1 | +;;; inf-elixir-test.el --- Inf-Elixir's test suite -*- lexical-binding: t -*- |
| 2 | + |
| 3 | +;;; Commentary: |
| 4 | +;; Inf-Elixir uses ERT for testing. |
| 5 | +;; Run tests with: |
| 6 | +;; $ emacs -batch -l ert -l inf-elixir.el -l tests/inf-elixir-test.el -f ert-run-tests-batch-and-exit |
| 7 | + |
| 8 | +;;; Code: |
| 9 | + |
| 10 | +(require 'inf-elixir) |
| 11 | + |
| 12 | +(defmacro inf-elixir-with-project (&rest body) |
| 13 | + "Create an Elixir project directory and eval BODY there." |
| 14 | + (let ((temp-dir (concat temporary-file-directory (file-name-as-directory "inf-elixir-test")))) |
| 15 | + `(unwind-protect |
| 16 | + (progn |
| 17 | + (make-directory ,temp-dir) |
| 18 | + (let ((default-directory ,temp-dir)) |
| 19 | + (with-temp-buffer |
| 20 | + (insert "defmodule Foobar.MixProject do |
| 21 | + use Mix.Project |
| 22 | +
|
| 23 | + def project do |
| 24 | + [ |
| 25 | + app: :foobar, |
| 26 | + version: \"0.1.0\" |
| 27 | + ] |
| 28 | + end |
| 29 | +end") |
| 30 | + (write-file "mix.exs")) |
| 31 | + ,@body)) |
| 32 | + (when (file-directory-p ,temp-dir) |
| 33 | + (delete-directory ,temp-dir t))))) |
| 34 | + |
| 35 | +(defmacro inf-elixir-with-cleanup (&rest body) |
| 36 | + "Evaulate BODY and cleanup inf-elixir side-effects afterwards." |
| 37 | + `(unwind-protect |
| 38 | + (progn |
| 39 | + ,@body) |
| 40 | + (let ((kill-buffer-query-functions nil)) |
| 41 | + (when-let ((inf-elixir-buf (get-buffer "*Inf-Elixir*"))) |
| 42 | + (kill-buffer inf-elixir-buf)) |
| 43 | + |
| 44 | + (maphash |
| 45 | + (lambda (proj buffer) (kill-buffer buffer)) |
| 46 | + inf-elixir-project-buffers) |
| 47 | + |
| 48 | + (clrhash inf-elixir-project-buffers)))) |
| 49 | + |
| 50 | +(ert-deftest inf-elixir-run-cmd () |
| 51 | + "`inf-elixir-run-cmd' opens new buffer running cmd." |
| 52 | + (inf-elixir-with-cleanup |
| 53 | + (inf-elixir-run-cmd nil "iex") |
| 54 | + (should (process-live-p (get-buffer-process (current-buffer)))) |
| 55 | + (should (equal (buffer-name (current-buffer)) "*Inf-Elixir*")) |
| 56 | + (should (equal |
| 57 | + (process-command (get-buffer-process (current-buffer))) |
| 58 | + '("iex"))))) |
| 59 | + |
| 60 | +(ert-deftest inf-elixir-has-no-project () |
| 61 | + "The `inf-elixir' command does not associate REPL with a project." |
| 62 | + (inf-elixir-with-cleanup |
| 63 | + (inf-elixir) |
| 64 | + (should (process-live-p (get-buffer-process (current-buffer)))) |
| 65 | + (should (equal (buffer-name (current-buffer)) "*Inf-Elixir*")) |
| 66 | + (maphash |
| 67 | + (lambda (proj buffer) (should-not (eq buffer (current-buffer)))) |
| 68 | + inf-elixir-project-buffers))) |
| 69 | + |
| 70 | +(ert-deftest inf-elixir-project-has-project () |
| 71 | + "The `inf-elixir-project' command associates a REPL with a project." |
| 72 | + (inf-elixir-with-project |
| 73 | + (inf-elixir-with-cleanup |
| 74 | + (inf-elixir-project) |
| 75 | + (should (process-live-p (get-buffer-process (current-buffer)))) |
| 76 | + (should (equal (buffer-name (current-buffer)) "*Inf-Elixir - inf-elixir-test*")) |
| 77 | + (should (gethash default-directory inf-elixir-project-buffers))))) |
| 78 | + |
| 79 | +(ert-deftest inf-elixir-send-within-project () |
| 80 | + "`inf-elixir-send-line' sends text to a REPL within the project." |
| 81 | + (inf-elixir-with-project |
| 82 | + (inf-elixir-with-cleanup |
| 83 | + (inf-elixir-project) |
| 84 | + (while (not (string-match-p "iex(1)>" (buffer-string))) (sleep-for 0 10)) |
| 85 | + (with-temp-buffer |
| 86 | + (insert "1 + 1") |
| 87 | + (inf-elixir-send-line)) |
| 88 | + (sleep-for 0 500) |
| 89 | + (should (string-match-p "iex(1)> 2" (buffer-string)))))) |
| 90 | + |
| 91 | +(provide 'inf-elixir-test) |
| 92 | +;;; inf-elixir-test.el ends here |
0 commit comments