From 8aada5b29a7609c52818538a258c88522f58d7c7 Mon Sep 17 00:00:00 2001 From: Maciej Szlosarczyk Date: Sun, 18 Aug 2024 19:02:36 +0300 Subject: [PATCH] Use with-simulated-input for tests --- Eask | 1 + counsel-projectile.el | 2 +- tests/test-counsel-projectile.el | 40 ++++++++++++++++++++++++++++---- 3 files changed, 37 insertions(+), 6 deletions(-) diff --git a/Eask b/Eask index f2ee0d1..f507150 100644 --- a/Eask +++ b/Eask @@ -15,4 +15,5 @@ (depends-on "counsel") (development + (depends-on "with-simulated-input") (depends-on "buttercup")) diff --git a/counsel-projectile.el b/counsel-projectile.el index 8cc42b8..a13d618 100644 --- a/counsel-projectile.el +++ b/counsel-projectile.el @@ -58,7 +58,7 @@ with default value ACTION, to be used as `:action' parameter for COMMAND's `ivy-read' call. -This variable holds either a single action function, or an action +;; This variable holds either a single action function, or an action list whose first element is the index of the default action in the list and the remaining elements are the actions (a key, a function, and a name for each action)." diff --git a/tests/test-counsel-projectile.el b/tests/test-counsel-projectile.el index 0d23712..8013965 100644 --- a/tests/test-counsel-projectile.el +++ b/tests/test-counsel-projectile.el @@ -1,13 +1,43 @@ ;;; test-counsel-projectile.el --- Buttercup tests for counsel-projectile -*- lexical-binding: t; -*- ;;; Commentary: ;;; Code: - -(require 'buttercup) +(require 'with-simulated-input) (require 'counsel-projectile) ;; Example test! -(describe "A suite" - (it "contains a spec with an expectation" - (expect counsel-projectile--buffers :to-be nil))) +(describe "counsel-projectile-switch-project with a mock" + (before-each + (spy-on 'ivy-read) + (spy-on 'projectile-project-root :and-return-value "/usr/fake-project") + (setq + projectile-known-projects '("/usr/fake-project" "~/some-project") + counsel-projectile-switch-project-action 'counsel-projectile-switch-project-action)) + (it "spies on ivy-read arguments" + (counsel-projectile-switch-project) + (expect 'projectile-project-root :to-have-been-called) + + (expect 'ivy-read :to-have-been-called-with + "[fake-project] Switch to project: " + '("/usr/fake-project" "~/some-project") + :preselect "/usr/fake-project" + :action counsel-projectile-switch-project-action + :require-match t + :sort nil + :caller 'counsel-projectile-switch-project))) + +(describe "counsel-projectile-switch-project with the real ivy-read" + (before-each + (spy-on 'projectile-project-root :and-return-value "/usr/fake-project") + (setq + counsel-projectile-switch-project-action 'counsel-projectile-switch-project-action)) + + (it "signals error when project is unknown" + (expect + (with-simulated-input "/usr/fake RET" + (condition-case err + (counsel-projectile-switch-project) + (file-missing + (error-message-string err)))) + :to-equal "Setting current directory: No such file or directory, /usr/fake-project"))) ;;; test-counsel-projectile.el ends here