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 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465
|
;;; clojure-mode-refactor-threading-test.el --- Clojure Mode: refactor threading tests -*- 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 threading refactoring code is ported from clj-refactor.el
;; and mainly the work of Magnar Sveen, Alex Baranosky and
;; the rest of the clj-reafctor.el team.
;;; Code:
(require 'clojure-mode)
(require 'buttercup)
(describe "clojure-thread"
(when-refactoring-it "should work with -> when performed once"
"(-> (dissoc (assoc {} :key \"value\") :lock))"
"(-> (assoc {} :key \"value\")
(dissoc :lock))"
(clojure-thread))
(when-refactoring-it "should work with -> when performed twice"
"(-> (dissoc (assoc {} :key \"value\") :lock))"
"(-> {}
(assoc :key \"value\")
(dissoc :lock))"
(clojure-thread)
(clojure-thread))
(when-refactoring-it "should not thread maps"
"(-> (dissoc (assoc {} :key \"value\") :lock))"
"(-> {}
(assoc :key \"value\")
(dissoc :lock))"
(clojure-thread)
(clojure-thread)
(clojure-thread))
(when-refactoring-it "should not thread last sexp"
"(-> (dissoc (assoc (get-a-map) :key \"value\") :lock))"
"(-> (get-a-map)
(assoc :key \"value\")
(dissoc :lock))"
(clojure-thread)
(clojure-thread)
(clojure-thread))
(when-refactoring-it "should thread-first-easy-on-whitespace"
"(->
(dissoc (assoc {} :key \"value\") :lock))"
"(->
(assoc {} :key \"value\")
(dissoc :lock))"
(clojure-thread))
(when-refactoring-it "should remove superfluous parens"
"(-> (square (sum [1 2 3 4 5])))"
"(-> [1 2 3 4 5]
sum
square)"
(clojure-thread)
(clojure-thread))
(when-refactoring-it "should work with cursor before ->"
"(-> (not (s-acc/mobile? session)))"
"(-> (s-acc/mobile? session)
not)"
(beginning-of-buffer)
(clojure-thread))
(when-refactoring-it "should work with one step with ->>"
"(->> (map square (filter even? [1 2 3 4 5])))"
"(->> (filter even? [1 2 3 4 5])
(map square))"
(clojure-thread))
(when-refactoring-it "should work with two steps with ->>"
"(->> (map square (filter even? [1 2 3 4 5])))"
"(->> [1 2 3 4 5]
(filter even?)
(map square))"
(clojure-thread)
(clojure-thread))
(when-refactoring-it "should not thread vectors with ->>"
"(->> (map square (filter even? [1 2 3 4 5])))"
"(->> [1 2 3 4 5]
(filter even?)
(map square))"
(clojure-thread)
(clojure-thread)
(clojure-thread))
(when-refactoring-it "should not thread last sexp with ->>"
"(->> (map square (filter even? (get-a-list))))"
"(->> (get-a-list)
(filter even?)
(map square))"
(clojure-thread)
(clojure-thread)
(clojure-thread))
(when-refactoring-it "should work with some->"
"(some-> (+ (val (find {:a 1} :b)) 5))"
"(some-> {:a 1}
(find :b)
val
(+ 5))"
(clojure-thread)
(clojure-thread)
(clojure-thread))
(when-refactoring-it "should work with some->>"
"(some->> (+ 5 (val (find {:a 1} :b))))"
"(some->> :b
(find {:a 1})
val
(+ 5))"
(clojure-thread)
(clojure-thread)
(clojure-thread)))
(describe "clojure-unwind"
(when-refactoring-it "should unwind -> one step"
"(-> {}
(assoc :key \"value\")
(dissoc :lock))"
"(-> (assoc {} :key \"value\")
(dissoc :lock))"
(clojure-unwind))
(when-refactoring-it "should unwind -> two steps"
"(-> {}
(assoc :key \"value\")
(dissoc :lock))"
"(-> (dissoc (assoc {} :key \"value\") :lock))"
(clojure-unwind)
(clojure-unwind))
(when-refactoring-it "should unwind -> completely"
"(-> {}
(assoc :key \"value\")
(dissoc :lock))"
"(dissoc (assoc {} :key \"value\") :lock)"
(clojure-unwind)
(clojure-unwind)
(clojure-unwind))
(when-refactoring-it "should unwind ->> one step"
"(->> [1 2 3 4 5]
(filter even?)
(map square))"
"(->> (filter even? [1 2 3 4 5])
(map square))"
(clojure-unwind))
(when-refactoring-it "should unwind ->> two steps"
"(->> [1 2 3 4 5]
(filter even?)
(map square))"
"(->> (map square (filter even? [1 2 3 4 5])))"
(clojure-unwind)
(clojure-unwind))
(when-refactoring-it "should unwind ->> completely"
"(->> [1 2 3 4 5]
(filter even?)
(map square))"
"(map square (filter even? [1 2 3 4 5]))"
(clojure-unwind)
(clojure-unwind)
(clojure-unwind))
(when-refactoring-it "should unwind N steps with numeric prefix arg"
"(->> [1 2 3 4 5]
(filter even?)
(map square)
sum)"
"(->> (sum (map square (filter even? [1 2 3 4 5]))))"
(clojure-unwind 3))
(when-refactoring-it "should unwind completely with universal prefix arg"
"(->> [1 2 3 4 5]
(filter even?)
(map square)
sum)"
"(sum (map square (filter even? [1 2 3 4 5])))"
(clojure-unwind '(4)))
(when-refactoring-it "should unwind correctly when multiple ->> are present on same line"
"(->> 1 inc) (->> [1 2 3 4 5]
(filter even?)
(map square))"
"(->> 1 inc) (->> (map square (filter even? [1 2 3 4 5])))"
(clojure-unwind)
(clojure-unwind))
(when-refactoring-it "should unwind with function name"
"(->> [1 2 3 4 5]
sum
square)"
"(->> (sum [1 2 3 4 5])
square)"
(clojure-unwind))
(when-refactoring-it "should unwind with function name twice"
"(-> [1 2 3 4 5]
sum
square)"
"(-> (square (sum [1 2 3 4 5])))"
(clojure-unwind)
(clojure-unwind))
(when-refactoring-it "should thread-issue-6-1"
"(defn plus [a b]
(-> a (+ b)))"
"(defn plus [a b]
(-> (+ a b)))"
(clojure-unwind))
(when-refactoring-it "should thread-issue-6-2"
"(defn plus [a b]
(->> a (+ b)))"
"(defn plus [a b]
(->> (+ b a)))"
(clojure-unwind))
(when-refactoring-it "should unwind some->"
"(some-> {:a 1}
(find :b)
val
(+ 5))"
"(some-> (+ (val (find {:a 1} :b)) 5))"
(clojure-unwind)
(clojure-unwind)
(clojure-unwind))
(when-refactoring-it "should unwind some->>"
"(some->> :b
(find {:a 1}) val
(+ 5))"
"(some->> (+ 5 (val (find {:a 1} :b))))"
(clojure-unwind)
(clojure-unwind)
(clojure-unwind)))
(describe "clojure-thread-first-all"
(when-refactoring-it "should thread first all sexps"
"(->map (assoc {} :key \"value\") :lock)"
"(-> {}
(assoc :key \"value\")
(->map :lock))"
(beginning-of-buffer)
(clojure-thread-first-all nil))
(when-refactoring-it "should thread a form except the last expression"
"(->map (assoc {} :key \"value\") :lock)"
"(-> (assoc {} :key \"value\")
(->map :lock))"
(beginning-of-buffer)
(clojure-thread-first-all t)))
(describe "clojure-thread-last-all"
(when-refactoring-it "should fully thread a form"
"(map square (filter even? (make-things)))"
"(->> (make-things)
(filter even?)
(map square))"
(beginning-of-buffer)
(clojure-thread-last-all nil))
(when-refactoring-it "should thread a form except the last expression"
"(map square (filter even? (make-things)))"
"(->> (filter even? (make-things))
(map square))"
(beginning-of-buffer)
(clojure-thread-last-all t))
(when-refactoring-it "should handle dangling parens 1"
"(map inc
(range))"
"(->> (range)
(map inc))"
(beginning-of-buffer)
(clojure-thread-last-all nil))
(when-refactoring-it "should handle dangling parens 2"
"(deftask dev []
(comp (serve)
(cljs)))"
"(->> (cljs)
(comp (serve))
(deftask dev []))"
(beginning-of-buffer)
(clojure-thread-last-all nil)))
(describe "clojure-unwind-all"
(when-refactoring-it "should unwind all in ->"
"(-> {}
(assoc :key \"value\")
(dissoc :lock))"
"(dissoc (assoc {} :key \"value\") :lock)"
(beginning-of-buffer)
(clojure-unwind-all))
(when-refactoring-it "should unwind all in ->>"
"(->> (make-things)
(filter even?)
(map square))"
"(map square (filter even? (make-things)))"
(beginning-of-buffer)
(clojure-unwind-all))
;; fix for clojure-emacs/clj-refactor.el#259
(when-refactoring-it "should leave multiline sexp alone"
"(->> [a b]
(some (fn [x]
(when x
10))))"
"(some (fn [x]
(when x
10))
[a b])"
(clojure-unwind-all))
(when-refactoring-it "should thread-last-maybe-unjoin-lines"
"(deftask dev []
(comp (serve)
(cljs (lala)
10)))"
"(deftask dev []
(comp (serve)
(cljs (lala)
10)))"
(goto-char (point-min))
(clojure-thread-last-all nil)
(clojure-unwind-all)))
(describe "clojure-thread-first-all"
(when-refactoring-it "should thread with an empty first line"
"(map
inc
[1 2])"
"(-> inc
(map
[1 2]))"
(goto-char (point-min))
(clojure-thread-first-all nil))
(when-refactoring-it "should thread-first-maybe-unjoin-lines"
"(map
inc
[1 2])"
"(map
inc
[1 2])"
(goto-char (point-min))
(clojure-thread-first-all nil)
(clojure-unwind-all)))
(provide 'clojure-mode-refactor-threading-test)
;;; clojure-mode-refactor-threading-test.el ends here
|