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 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259
|
;;; clojure-mode-refactor-let-test.el --- Clojure Mode: refactor let -*- lexical-binding: t; -*-
;; Copyright (C) 2016-2021 Benedek Fazekas <benedek.fazekas@gmail.com>
;; 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:
;; The refactor-let code originally was implemented in clj-refactor.el
;; and is the work of the clj-reafctor.el team.
;;; Code:
(require 'clojure-mode)
(require 'buttercup)
(describe "clojure--introduce-let-internal"
(when-refactoring-it "should introduce a let form"
"{:status 200
:body (find-body abc)}"
"{:status 200
:body (let [body (find-body abc)]
body)}"
(search-backward "(find-body")
(clojure--introduce-let-internal "body"))
(when-refactoring-it "should introduce an expanded let form"
"(defn handle-request []
{:status 200
:length (count (find-body abc))
:body (find-body abc)})"
"(defn handle-request []
(let [body (find-body abc)]
{:status 200
:length (count body)
:body body}))"
(search-backward "(find-body")
(clojure--introduce-let-internal "body" 1))
(when-refactoring-it "should replace bindings whitespace"
"(defn handle-request []
{:status 200
:length (count
(find-body
abc))
:body (find-body abc)})"
"(defn handle-request []
(let [body (find-body abc)]
{:status 200
:length (count
body)
:body body}))"
(search-backward "(find-body")
(clojure--introduce-let-internal "body" 1)))
(describe "clojure-let-forward-slurp-sexp"
(when-refactoring-it "should slurp the next 2 sexps after the let into the let form"
"(defn handle-request []
(let [body (find-body abc)]
{:status 200
:length (count body)
:body body})
(println (find-body abc))
(println \"foobar\"))"
"(defn handle-request []
(let [body (find-body abc)]
{:status 200
:length (count body)
:body body}
(println body)
(println \"foobar\")))"
(search-backward "(count body")
(clojure-let-forward-slurp-sexp 2)))
(describe "clojure-let-backward-slurp-sexp"
(when-refactoring-it "should slurp the previous 2 sexps before the let into the let form"
"(defn handle-request []
(println (find-body abc))
(println \"foobar\")
(let [body (find-body abc)]
{:status 200
:length (count body)
:body body}))"
"(defn handle-request []
(let [body (find-body abc)]
(println body)
(println \"foobar\")
{:status 200
:length (count body)
:body body}))"
(search-backward "(count body")
(clojure-let-backward-slurp-sexp 2)))
(describe "clojure--move-to-let-internal"
(when-refactoring-it "should move sexp to let"
"(defn handle-request
(let [body (find-body abc)]
{:status (or status 500)
:body body}))"
"(defn handle-request
(let [body (find-body abc)
status (or status 500)]
{:status status
:body body}))"
(search-backward "(or ")
(clojure--move-to-let-internal "status"))
(when-refactoring-it "should move constant to when let"
"(defn handle-request
(when-let [body (find-body abc)]
{:status 42
:body body}))"
"(defn handle-request
(when-let [body (find-body abc)
status 42]
{:status status
:body body}))"
(search-backward "42")
(clojure--move-to-let-internal "status"))
(when-refactoring-it "should move sexp to empty let"
"(defn handle-request
(if-let []
{:status (or status 500)
:body body}))"
"(defn handle-request
(if-let [status (or status 500)]
{:status status
:body body}))"
(search-backward "(or ")
(clojure--move-to-let-internal "status"))
(when-refactoring-it "should introduce let if missing"
"(defn handle-request
{:status (or status 500)
:body body})"
"(defn handle-request
{:status (let [status (or status 500)]
status)
:body body})"
(search-backward "(or ")
(clojure--move-to-let-internal "status"))
(when-refactoring-it "should move multiple occurrences of a sexp"
"(defn handle-request
(let []
(println \"body: \" body \", params: \" \", status: \" (or status 500))
{:status (or status 500)
:body body}))"
"(defn handle-request
(let [status (or status 500)]
(println \"body: \" body \", params: \" \", status: \" status)
{:status status
:body body}))"
(search-backward "(or ")
(clojure--move-to-let-internal "status"))
(when-refactoring-it "should handle a name that is longer than the expression"
"(defn handle-request
(let []
(println \"body: \" body \", params: \" \", status: \" 5)
{:body body
:status 5}))"
"(defn handle-request
(let [status 5]
(println \"body: \" body \", params: \" \", status: \" status)
{:body body
:status status}))"
(search-backward "5")
(search-backward "5")
(clojure--move-to-let-internal "status"))
;; clojure-emacs/clj-refactor.el#41
(when-refactoring-it "should not move to nested let"
"(defn foo []
(let [x (range 10)]
(doseq [x (range 10)]
(let [x2 (* x x)]))
(+ 1 1)))"
"(defn foo []
(let [x (range 10)
something (+ 1 1)]
(doseq [x x]
(let [x2 (* x x)]))
something))"
(search-backward "(+ 1 1")
(clojure--move-to-let-internal "something"))
;; clojure-emacs/clj-refactor.el#30
(when-refactoring-it "should move before current form when already inside let binding-1"
"(deftest retrieve-order-body-test
(let [item (get-in (retrieve-order-body order-item-response-str))]))"
"(deftest retrieve-order-body-test
(let [something (retrieve-order-body order-item-response-str)
item (get-in something)]))"
(search-backward "(retrieve")
(clojure--move-to-let-internal "something"))
;; clojure-emacs/clj-refactor.el#30
(when-refactoring-it "should move before current form when already inside let binding-2"
"(let [parent (.getParent (io/file root adrf))
builder (string-builder)
normalize-path (comp (partial path/relative-to root)
path/->normalized
foobar)]
(do-something-spectacular parent builder))"
"(let [parent (.getParent (io/file root adrf))
builder (string-builder)
something (partial path/relative-to root)
normalize-path (comp something
path/->normalized
foobar)]
(do-something-spectacular parent builder))"
(search-backward "(partial")
(clojure--move-to-let-internal "something")))
(provide 'clojure-mode-refactor-let-test)
;;; clojure-mode-refactor-let-test.el ends here
|