Skip to content

Commit

Permalink
Add tests for shorthand fn
Browse files Browse the repository at this point in the history
  • Loading branch information
yuhan0 authored and yuhan committed Sep 17, 2021
1 parent 94ae83f commit eebb353
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 1 deletion.
2 changes: 1 addition & 1 deletion clojure-mode.el
Original file line number Diff line number Diff line change
Expand Up @@ -2805,7 +2805,7 @@ END marks the end of the fn expression"
(when-let (beg (clojure-string-start))
(goto-char beg))
(if (or (looking-at-p "#(")
(forward-char 1)
(ignore-errors (forward-char 1))
(re-search-backward "#(" (save-excursion (beginning-of-defun) (point)) 'noerror))
(let* ((end (save-excursion (clojure-forward-logical-sexp) (point-marker)))
(argspec (clojure--gather-shorthand-args))
Expand Down
72 changes: 72 additions & 0 deletions test/clojure-mode-convert-fn-test.el
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
;;; clojure-mode-convert-fn-test.el --- Clojure Mode: convert fn syntax -*- lexical-binding: t; -*-

;; This file is not part of GNU Emacs.

;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.

;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <http://www.gnu.org/licenses/>.

;;; Commentary:

;; Tests for clojure-convert-shorthand-fn

;;; Code:

(require 'clojure-mode)
(require 'buttercup)

(describe "clojure-convert-shorthand-fn"
:var (names)

(before-each
(spy-on 'read-string
:and-call-fake (lambda (_) (or (pop names) (error "")))))

(when-refactoring-it "should convert 0-arg fns"
"#(rand)"
"(fn [] (rand))"
(clojure-convert-shorthand-fn))

(when-refactoring-it "should convert 1-arg fns"
"#(= % 1)"
"(fn [x] (= x 1))"
(setq names '("x"))
(clojure-convert-shorthand-fn))

(when-refactoring-it "should convert 2-arg fns"
"#(conj (pop %1) (assoc (peek %1) %2 (* %2 %2)))"
"(fn [acc x] (conj (pop acc) (assoc (peek acc) x (* x x))))"
(setq names '("acc" "x"))
(clojure-convert-shorthand-fn))

(when-refactoring-it "should convert variadic fns"
;; from https://hypirion.com/musings/swearjure
"#(* (`[~@%&] (+))
((% (+)) % (- (`[~@%&] (+)) (*))))"
"(fn [v & vs] (* (`[~@vs] (+))
((v (+)) v (- (`[~@vs] (+)) (*)))))"
(setq names '("v" "vs"))
(clojure-convert-shorthand-fn))

(when-refactoring-it "should ignore strings and comments"
"#(format \"%2\" ;; FIXME: %2 is an illegal specifier
%7) "
"(fn [_ _ _ _ _ _ id] (format \"%2\" ;; FIXME: %2 is an illegal specifier
id)) "
(setq names '("_" "_" "_" "_" "_" "_" "id"))
(clojure-convert-shorthand-fn)))


(provide 'clojure-mode-convert-fn-test)


;;; clojure-mode-convert-fn-test.el ends here

0 comments on commit eebb353

Please sign in to comment.