1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
|
;;; clojure-mode-promote-fn-literal-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 <https://www.gnu.org/licenses/>.
;;; Commentary:
;; Tests for clojure-promote-fn-literal
;;; Code:
(require 'clojure-mode)
(require 'buttercup)
(require 'test-helper "test/utils/test-helper")
(describe "clojure-promote-fn-literal"
: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-promote-fn-literal))
(when-refactoring-it "should convert 1-arg fns"
"#(= % 1)"
"(fn [x] (= x 1))"
(setq names '("x"))
(clojure-promote-fn-literal))
(when-refactoring-it "should convert 2-arg fns"
"#(conj (pop %) (assoc (peek %1) %2 (* %2 %2)))"
"(fn [acc x] (conj (pop acc) (assoc (peek acc) x (* x x))))"
(setq names '("acc" "x"))
(clojure-promote-fn-literal))
(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-promote-fn-literal))
(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-promote-fn-literal)))
(provide 'clojure-mode-convert-fn-test)
;;; clojure-mode-promote-fn-literal-test.el ends here
|