Skip to content

Instantly share code, notes, and snippets.

@josephwilk
Created May 3, 2013 12:42
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 josephwilk/5508883 to your computer and use it in GitHub Desktop.
Save josephwilk/5508883 to your computer and use it in GitHub Desktop.
(defun clojure-in-tests-p ()
(or (string-match-p "test\." (clojure-find-ns))
(string-match-p "/test" (buffer-file-name))))
(defun midje-test-for (namespace)
(let* ((namespace (clojure-underscores-for-hyphens namespace))
(segments (split-string namespace "\\."))
(main-namespace (first segments))
(filename (last segments))
(test-segments (append (list "test" main-namespace "unit") (rest (butlast segments)))))
(concat (mapconcat 'identity test-segments "/") "/t_" (first filename))))
(defun midje-jump-to-test ()
"Jump from implementation file to test."
(interactive)
(find-file (format "%s/%s.clj"
(file-name-as-directory
(locate-dominating-file buffer-file-name "src/"))
(midje-test-for (clojure-find-ns)))))
(defun midje-implementation-for (namespace)
(let* ((namespace (clojure-underscores-for-hyphens namespace))
(segments (split-string (replace-regexp-in-string "t_" "" namespace) "\\.")))
(replace-regexp-in-string "unit" "" (mapconcat 'identity segments "/"))))
(defun midje-jump-to-implementation ()
"Jump from midje test file to implementation."
(interactive)
(find-file (format "%s/src/%s.clj"
(locate-dominating-file buffer-file-name "src/")
(midje-implementation-for (clojure-find-package)))))
(defun midje-jump-between-tests-and-code ()
(interactive)
(if (clojure-in-tests-p)
(midje-jump-to-implementation)
(midje-jump-to-test)))
(define-key clojure-mode-map (kbd "C-c t") 'midje-jump-between-tests-and-code)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment