diff --git a/clojure-mode.el b/clojure-mode.el index 06a8967c..e3460e19 100644 --- a/clojure-mode.el +++ b/clojure-mode.el @@ -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)) diff --git a/test/clojure-mode-convert-fn-test.el b/test/clojure-mode-convert-fn-test.el new file mode 100644 index 00000000..401a7be8 --- /dev/null +++ b/test/clojure-mode-convert-fn-test.el @@ -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 . + +;;; 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