Skip to content

Instantly share code, notes, and snippets.

@technomancy
Created February 17, 2009 19:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save technomancy/65906 to your computer and use it in GitHub Desktop.
Save technomancy/65906 to your computer and use it in GitHub Desktop.
;;; clojure-test-mode --- Minor mode for Clojure tests
;; Copyright (C) 2009 Phil Hagelberg
;; Author: Phil Hagelberg <technomancy@gmail.com>
;; URL: http://emacswiki.org/cgi-bin/wiki/ClojureTestMode
;; Version: 0.1
;; Keywords: languages, lisp
;; This file is not part of GNU Emacs.
;;; Commentary:
;; This file provides support for running Clojure tests (using the
;; test-is framework) via SLIME and seeing feedback in the test buffer
;; about which tests passed and which failed or errored.
;;; Installation:
;; (0) Add this file to your load-path, usually the ~/.emacs.d directory.
;; (1) Either:
;; Add these lines to your .emacs:
;; (autoload 'clojure-test-mode "clojure-test-mode" "Clojure test mode" t)
;; (add-hook 'clojure-mode-hook
;; (lambda () (save-excursion
;; (goto-char (point-min))
;; (if (search-forward "(deftest" nil t)
;; (clojure-test-mode)))))
;; Or generate autoloads with the `update-directory-autoloads' function.
;;; Code:
(require 'clojure-mode)
(require 'slime)
(defvar clojure-test-results nil
"An alist containing the results of the last test run for each test.")
;; Support Functions
(defun clojure-test-focused-test ()
(save-excursion
(search-backward-regexp "(deftest \\(.*\\)")
(match-string 1)))
(defun clojure-test-report ()
;; TODO: write
;; This needs to somehow wait until the tests have actually
;; finished. Maybe it needs to be run in a hook instead of called
;; directly?
)
(defun clojure-test-load-test-is-hook ()
"Redefine the test-is report function to store results in metadata."
(slime-repl-eval-string
"(use 'clojure.contrib.test-is)
(ns clojure.contrib.test-is)
(defonce old-report report)
(defn report [event msg expected actual]
(if-let [current-test (last *testing-vars*)]
(alter-meta! current-test
assoc :status [event msg expected actual]))
(old-report event msg expected actual))"))
;; Commands
(defun clojure-test-run-tests ()
"Run all the tests in the current namespace."
(interactive)
(slime-load-file (buffer-file-name))
(slime-repl-eval-string
(format "(run-tests '%s)" (slime-current-package)))
(clojure-test-report))
(defun clojure-test-run-focused-test ()
"Run the test under point."
(interactive)
(slime-load-file (buffer-file-name))
(slime-repl-eval-string "(test-var #'%s)" (clojure-test-focused-test))
(clojure-test-report))
(defun clojure-test-show-result ()
"Show the result of the test under point."
(interactive)
;; TODO: write
)
;;;###autoload
(define-minor-mode clojure-test-mode
"A minor mode for running Clojure tests."
nil "Test"
'(((kbd "C-c t") . clojure-test-run-tests
(kbd "C-c C-tt") . clojure-test-run-focused-test
(kbd "C-c d") . clojure-test-show-result))
(clojure-test-load-test-is-hook)
(slime-mode t))
;;;###autoload
(add-hook 'clojure-mode-hook
(lambda () (save-excursion
(goto-char (point-min))
(if (search-forward "(deftest" nil t)
(clojure-test-mode)))))
(provide 'clojure-test-mode)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment