|
| 1 | +;;; clojure-mode-cycling-test.el --- Clojure Mode: cycling things tests -*- lexical-binding: t; -*- |
| 2 | + |
| 3 | +;; Copyright (C) 2016-2021 Benedek Fazekas <[email protected]> |
| 4 | + |
| 5 | +;; This file is not part of GNU Emacs. |
| 6 | + |
| 7 | +;; This program is free software; you can redistribute it and/or modify |
| 8 | +;; it under the terms of the GNU General Public License as published by |
| 9 | +;; the Free Software Foundation, either version 3 of the License, or |
| 10 | +;; (at your option) any later version. |
| 11 | + |
| 12 | +;; This program is distributed in the hope that it will be useful, |
| 13 | +;; but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | +;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | +;; GNU General Public License for more details. |
| 16 | + |
| 17 | +;; You should have received a copy of the GNU General Public License |
| 18 | +;; along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 19 | + |
| 20 | +;;; Commentary: |
| 21 | + |
| 22 | +;; The cycling privacy and if/if-not code is ported from |
| 23 | +;; clj-refactor.el and the work of the clj-reafctor.el team. |
| 24 | + |
| 25 | +;;; Code: |
| 26 | + |
| 27 | +(require 'clojure-mode) |
| 28 | +(require 'buttercup) |
| 29 | + |
| 30 | +(describe "clojure-cycle-privacy" |
| 31 | + |
| 32 | + (when-refactoring-it "should turn a public defn into a private defn" |
| 33 | + "(defn add [a b] |
| 34 | + (+ a b))" |
| 35 | + |
| 36 | + "(defn- add [a b] |
| 37 | + (+ a b))" |
| 38 | + |
| 39 | + (clojure-cycle-privacy)) |
| 40 | + |
| 41 | + (when-refactoring-it "should also work from the beginning of a sexp" |
| 42 | + "(defn- add [a b] |
| 43 | + (+ a b))" |
| 44 | + |
| 45 | + "(defn add [a b] |
| 46 | + (+ a b))" |
| 47 | + |
| 48 | + (backward-sexp) |
| 49 | + (clojure-cycle-privacy)) |
| 50 | + |
| 51 | + (when-refactoring-it "should use metadata when clojure-use-metadata-for-privacy is set to true" |
| 52 | + "(defn add [a b] |
| 53 | + (+ a b))" |
| 54 | + |
| 55 | + "(defn ^:private add [a b] |
| 56 | + (+ a b))" |
| 57 | + |
| 58 | + (let ((clojure-use-metadata-for-privacy t)) |
| 59 | + (clojure-cycle-privacy))) |
| 60 | + |
| 61 | + (when-refactoring-it "should turn a private defn into a public defn" |
| 62 | + "(defn- add [a b] |
| 63 | + (+ a b))" |
| 64 | + |
| 65 | + "(defn add [a b] |
| 66 | + (+ a b))" |
| 67 | + |
| 68 | + (clojure-cycle-privacy)) |
| 69 | + |
| 70 | + (when-refactoring-it "should turn a private defn with metadata into a public defn" |
| 71 | + "(defn ^:private add [a b] |
| 72 | + (+ a b))" |
| 73 | + |
| 74 | + "(defn add [a b] |
| 75 | + (+ a b))" |
| 76 | + |
| 77 | + (let ((clojure-use-metadata-for-privacy t)) |
| 78 | + (clojure-cycle-privacy))) |
| 79 | + |
| 80 | + (when-refactoring-it "should also work with pre-existing metadata" |
| 81 | + "(def ^:dynamic config |
| 82 | + \"docs\" |
| 83 | + {:env \"staging\"})" |
| 84 | + |
| 85 | + "(def ^:private ^:dynamic config |
| 86 | + \"docs\" |
| 87 | + {:env \"staging\"})" |
| 88 | + |
| 89 | + (clojure-cycle-privacy)) |
| 90 | + |
| 91 | + (when-refactoring-it "should turn a private def with metadata into a public def" |
| 92 | + "(def ^:private config |
| 93 | + \"docs\" |
| 94 | + {:env \"staging\"})" |
| 95 | + |
| 96 | + "(def config |
| 97 | + \"docs\" |
| 98 | + {:env \"staging\"})" |
| 99 | + |
| 100 | + (clojure-cycle-privacy))) |
| 101 | + |
| 102 | +(describe "clojure-cycle-if" |
| 103 | + |
| 104 | + (when-refactoring-it "should cycle inner if" |
| 105 | + "(if this |
| 106 | + (if that |
| 107 | + (then AAA) |
| 108 | + (else BBB)) |
| 109 | + (otherwise CCC))" |
| 110 | + |
| 111 | + "(if this |
| 112 | + (if-not that |
| 113 | + (else BBB) |
| 114 | + (then AAA)) |
| 115 | + (otherwise CCC))" |
| 116 | + |
| 117 | + (beginning-of-buffer) |
| 118 | + (search-forward "BBB)") |
| 119 | + (clojure-cycle-if)) |
| 120 | + |
| 121 | + (when-refactoring-it "should cycle outer if" |
| 122 | + "(if-not this |
| 123 | + (if that |
| 124 | + (then AAA) |
| 125 | + (else BBB)) |
| 126 | + (otherwise CCC))" |
| 127 | + |
| 128 | + "(if this |
| 129 | + (otherwise CCC) |
| 130 | + (if that |
| 131 | + (then AAA) |
| 132 | + (else BBB)))" |
| 133 | + |
| 134 | + (beginning-of-buffer) |
| 135 | + (search-forward "BBB))") |
| 136 | + (clojure-cycle-if))) |
| 137 | + |
| 138 | +(describe "clojure-cycle-when" |
| 139 | + |
| 140 | + (when-refactoring-it "should cycle inner when" |
| 141 | + "(when this |
| 142 | + (when that |
| 143 | + (aaa) |
| 144 | + (bbb)) |
| 145 | + (ccc))" |
| 146 | + |
| 147 | + "(when this |
| 148 | + (when-not that |
| 149 | + (aaa) |
| 150 | + (bbb)) |
| 151 | + (ccc))" |
| 152 | + |
| 153 | + (beginning-of-buffer) |
| 154 | + (search-forward "bbb)") |
| 155 | + (clojure-cycle-when)) |
| 156 | + |
| 157 | + (when-refactoring-it "should cycle outer when" |
| 158 | + "(when-not this |
| 159 | + (when that |
| 160 | + (aaa) |
| 161 | + (bbb)) |
| 162 | + (ccc))" |
| 163 | + |
| 164 | + "(when this |
| 165 | + (when that |
| 166 | + (aaa) |
| 167 | + (bbb)) |
| 168 | + (ccc))" |
| 169 | + |
| 170 | + (beginning-of-buffer) |
| 171 | + (search-forward "bbb))") |
| 172 | + (clojure-cycle-when))) |
| 173 | + |
| 174 | +(describe "clojure-cycle-not" |
| 175 | + |
| 176 | + (when-refactoring-it "should add a not when missing" |
| 177 | + "(ala bala portokala)" |
| 178 | + "(not (ala bala portokala))" |
| 179 | + |
| 180 | + (beginning-of-buffer) |
| 181 | + (search-forward "bala") |
| 182 | + (clojure-cycle-not)) |
| 183 | + |
| 184 | + (when-refactoring-it "should remove a not when present" |
| 185 | + "(not (ala bala portokala))" |
| 186 | + "(ala bala portokala)" |
| 187 | + |
| 188 | + (beginning-of-buffer) |
| 189 | + (search-forward "bala") |
| 190 | + (clojure-cycle-not))) |
| 191 | + |
| 192 | +(provide 'clojure-mode-cycling-test) |
| 193 | + |
| 194 | +;;; clojure-mode-cycling-test.el ends here |
0 commit comments