|
| 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