Skip to content

Commit eebb353

Browse files
yuhan0yuhan
authored and
yuhan
committed
Add tests for shorthand fn
1 parent 94ae83f commit eebb353

File tree

2 files changed

+73
-1
lines changed

2 files changed

+73
-1
lines changed

clojure-mode.el

+1-1
Original file line numberDiff line numberDiff line change
@@ -2805,7 +2805,7 @@ END marks the end of the fn expression"
28052805
(when-let (beg (clojure-string-start))
28062806
(goto-char beg))
28072807
(if (or (looking-at-p "#(")
2808-
(forward-char 1)
2808+
(ignore-errors (forward-char 1))
28092809
(re-search-backward "#(" (save-excursion (beginning-of-defun) (point)) 'noerror))
28102810
(let* ((end (save-excursion (clojure-forward-logical-sexp) (point-marker)))
28112811
(argspec (clojure--gather-shorthand-args))

test/clojure-mode-convert-fn-test.el

+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
;;; clojure-mode-convert-fn-test.el --- Clojure Mode: convert fn syntax -*- lexical-binding: t; -*-
2+
3+
;; This file is not part of GNU Emacs.
4+
5+
;; This program is free software; you can redistribute it and/or modify
6+
;; it under the terms of the GNU General Public License as published by
7+
;; the Free Software Foundation, either version 3 of the License, or
8+
;; (at your option) any later version.
9+
10+
;; This program is distributed in the hope that it will be useful,
11+
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
;; GNU General Public License for more details.
14+
15+
;; You should have received a copy of the GNU General Public License
16+
;; along with this program. If not, see <http://www.gnu.org/licenses/>.
17+
18+
;;; Commentary:
19+
20+
;; Tests for clojure-convert-shorthand-fn
21+
22+
;;; Code:
23+
24+
(require 'clojure-mode)
25+
(require 'buttercup)
26+
27+
(describe "clojure-convert-shorthand-fn"
28+
:var (names)
29+
30+
(before-each
31+
(spy-on 'read-string
32+
:and-call-fake (lambda (_) (or (pop names) (error "")))))
33+
34+
(when-refactoring-it "should convert 0-arg fns"
35+
"#(rand)"
36+
"(fn [] (rand))"
37+
(clojure-convert-shorthand-fn))
38+
39+
(when-refactoring-it "should convert 1-arg fns"
40+
"#(= % 1)"
41+
"(fn [x] (= x 1))"
42+
(setq names '("x"))
43+
(clojure-convert-shorthand-fn))
44+
45+
(when-refactoring-it "should convert 2-arg fns"
46+
"#(conj (pop %1) (assoc (peek %1) %2 (* %2 %2)))"
47+
"(fn [acc x] (conj (pop acc) (assoc (peek acc) x (* x x))))"
48+
(setq names '("acc" "x"))
49+
(clojure-convert-shorthand-fn))
50+
51+
(when-refactoring-it "should convert variadic fns"
52+
;; from https://hypirion.com/musings/swearjure
53+
"#(* (`[~@%&] (+))
54+
((% (+)) % (- (`[~@%&] (+)) (*))))"
55+
"(fn [v & vs] (* (`[~@vs] (+))
56+
((v (+)) v (- (`[~@vs] (+)) (*)))))"
57+
(setq names '("v" "vs"))
58+
(clojure-convert-shorthand-fn))
59+
60+
(when-refactoring-it "should ignore strings and comments"
61+
"#(format \"%2\" ;; FIXME: %2 is an illegal specifier
62+
%7) "
63+
"(fn [_ _ _ _ _ _ id] (format \"%2\" ;; FIXME: %2 is an illegal specifier
64+
id)) "
65+
(setq names '("_" "_" "_" "_" "_" "_" "id"))
66+
(clojure-convert-shorthand-fn)))
67+
68+
69+
(provide 'clojure-mode-convert-fn-test)
70+
71+
72+
;;; clojure-mode-convert-fn-test.el ends here

0 commit comments

Comments
 (0)