Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
- [#3354](https://github.com/clojure-emacs/cider/issues/3354): Add new customization variable `cider-reuse-dead-repls` to control how dead REPL buffers are reused on new connections.
- [#3364](https://github.com/clojure-emacs/cider/pull/3364): Update enrich-classpath, adding Clojure CLI compatibility, and reworking its integration into CIDER.
* It will be progressively refined and documented, please consider this alpha software.
- [#2958](https://github.com/clojure-emacs/cider/issues/2958), [#3279](https://github.com/clojure-emacs/cider/issues/3279): `cider-test-run-test`: support arbitrary deftest-like forms, defns with :test metadata, and search for a `-test` counterpart for a given defn (following `cider-test-infer-test-ns` logic).
- This also makes obsolete the `cider-test-defining-forms` customization variable.

### Bugs fixed

Expand Down
71 changes: 43 additions & 28 deletions cider-test.el
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,7 @@
:type 'boolean
:package-version '(cider . "0.9.0"))

(defcustom cider-test-defining-forms '("deftest" "defspec")
"Forms that define individual tests.
CIDER considers the \"top-level\" form around point to define a test if
the form starts with one of these forms.
Add to this list to have CIDER recognize additional test defining macros."
:type '(repeat string)
:package-version '(cider . "0.15.0"))
(make-obsolete 'cider-test-defining-forms nil "1.8.0")

(defvar cider-test-last-summary nil
"The summary of the last run test.")
Expand Down Expand Up @@ -838,31 +832,52 @@ See `cider-test-rerun-test'."
(setq cider-test-last-test-ns ns
cider-test-last-test-var var))

(defun cider--test-var-p (ns var)
"Determines if the VAR in NS is a test."
(if (cider-nrepl-op-supported-p "cider/get-state")
(cider-resolve--get-in ns "interns" var "test")
(equal "true"
(nrepl-dict-get (cider-sync-tooling-eval
(format "(clojure.core/-> %s var clojure.core/meta (clojure.core/contains? :test))"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will do for now, but it'd be nice if we made something more portable at some point. I was also thinking it'd be handy to have a command that just dumps a var metadata in an overlay or a buffer.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will do for now, but it'd be nice if we made something more portable at some point.

Speaking of, cljs functionality for our test-related functionality doesn't seem as hard as it might have been in the past.

Inspecting the cljs env, and evaling cljs code seem pretty vanilla things to do nowadays. We could look into it soon enough.

var)
ns)
"value"))))

(defun cider-test-run-test ()
"Run the test at point.
The test ns/var exist as text properties on report items and on highlighted
failed/erred test definitions. When not found, a test definition at point
is searched."
failed/erred test definitions.

When not found, a test definition at point
or in a corresponding test namespace is searched."
(interactive)
(let ((ns (get-text-property (point) 'ns))
(var (get-text-property (point) 'var)))
(if (and ns var)
;; we're in a `cider-test-report-mode' buffer
;; or on a highlighted failed/erred test definition
(progn
(cider-test-update-last-test ns var)
(cider-test-execute ns (list var)))
;; we're in a `clojure-mode' buffer
(or (when-let* ((ns (cider-get-ns-name))
(def (clojure-find-def)) ; it's a list of the form (deftest something)
(deftype (car def))
(var (cadr def)))
(if (and ns (member deftype cider-test-defining-forms))
(progn
(cider-test-update-last-test ns (list var))
(cider-test-execute ns (list var)))
(message "No test at point")))
(message "No test at point")))))
(let* ((ns-from-text-property (get-text-property (point) 'ns))
(var-from-text-property (when ns-from-text-property
;; we're in a `cider-test-report-mode' buffer
;; or on a highlighted failed/erred test definition
(get-text-property (point) 'var)))
(found (or (when (and var-from-text-property
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably this can be extracted to a private function.

;; Slightly redundant check. However querying `cider-resolve--get-in` is cheap:
(cider--test-var-p ns-from-text-property var-from-text-property))
(list ns-from-text-property var-from-text-property))
(when-let* ((n (cider-get-ns-name))
(v (cadr (clojure-find-def))))
(or (when (cider--test-var-p n v)
(list n v))
(let ((derived-ns (funcall cider-test-infer-test-ns n))
(derived-var (concat v "-test")))
;; deftest foo-test:
(or (when (cider--test-var-p derived-ns derived-var)
(list derived-ns derived-var))
;; deftest foo (less usual, but quite frequent):
(when (cider--test-var-p derived-ns v)
(list derived-ns v))))))))
(found-ns (car found))
(found-var (cadr found)))
(if (not found-var)
(message "No test found at point")
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe it could be a (user-error instead?

(cider-test-update-last-test found-ns (list found-var))
(cider-test-execute found-ns (list found-var)))))

(defun cider-test-rerun-test ()
"Re-run the test that was previously ran."
Expand Down
10 changes: 2 additions & 8 deletions doc/modules/ROOT/pages/testing/running_tests.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,8 @@ command with a prefix (kbd:[C-u C-c C-t C-s]) you can suppress
the namespace inference logic as for kbd:[C-u C-c C-t C-n]

Finally, you can execute the specific test at the point using
kbd:[C-c C-t t] or kbd:[C-c C-t C-t].
kbd:[C-c C-t t] or kbd:[C-c C-t C-t]. It will also work for implementation functions,
by searching for a matching test namespace with a matching deftest name.

== Configuration

Expand Down Expand Up @@ -141,13 +142,6 @@ selectors so that tests tagged as "integration" or "flakey" don't run.

TIP: You'll generally want to place default selectors in xref:config/project_config.adoc[your project configuration], as opposed to your global configuration.

=== Macros Used to Define Tests

If your individual tests are not defined by `deftest` or `defspec`, CIDER will
not recognize them when searching for a test at point in `cider-test-run-test`.
You can customize the variable `cider-test-defining-forms` to add additional
forms for CIDER to recognize as individual test definitions.

=== Display Test Report on Success

By default the test report is displayed only when there are test failures or
Expand Down
2 changes: 1 addition & 1 deletion doc/modules/ROOT/pages/usage/cider_mode.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ kbd:[C-c C-d C-e]
| `cider-test-run-test`
| kbd:[C-c C-t t] +
kbd:[C-c C-t C-t]
| Run test at point.
| Run test at point. If the form under the point is a function, try to search and run a corresponding test.

| `cider-test-rerun-test`
| kbd:[C-c C-t a] +
Expand Down