|
| 1 | +;;; clojure-mode-refactor-add-arity.el --- Clojure Mode: refactor add arity -*- 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 | + |
| 19 | +;;; Commentary: |
| 20 | + |
| 21 | +;; Tests for clojure-add-arity |
| 22 | + |
| 23 | +;;; Code: |
| 24 | + |
| 25 | +(require 'clojure-mode) |
| 26 | +(require 'buttercup) |
| 27 | + |
| 28 | +(defmacro when-refactoring-with-point-it (description before after &rest body) |
| 29 | + "Return a buttercup spec. |
| 30 | +
|
| 31 | +Like when-refactor-it but also checks whether point is moved to the expected |
| 32 | +position. |
| 33 | +
|
| 34 | +BEFORE is the buffer string before refactoring, where a pipe (|) represents |
| 35 | +point. |
| 36 | +
|
| 37 | +AFTER is the expected buffer string after refactoring, where a pipe (|) |
| 38 | +represents the expected position of point. |
| 39 | +
|
| 40 | +DESCRIPTION is a string with the description of the spec." |
| 41 | + `(it ,description |
| 42 | + (let* ((after ,after) |
| 43 | + (expected-cursor-pos (1+ (s-index-of "|" after))) |
| 44 | + (expected-state (delete ?| after))) |
| 45 | + (with-clojure-buffer ,before |
| 46 | + (goto-char (point-min)) |
| 47 | + (search-forward "|") |
| 48 | + (delete-char -1) |
| 49 | + ,@body |
| 50 | + (expect (buffer-string) :to-equal expected-state) |
| 51 | + (expect (point) :to-equal expected-cursor-pos))))) |
| 52 | + |
| 53 | +(describe "clojure-add-arity" |
| 54 | + |
| 55 | + (when-refactoring-with-point-it "should add an arity to a single-arity defn with args on same line" |
| 56 | + "(defn foo [arg] |
| 57 | + body|)" |
| 58 | + |
| 59 | + "(defn foo |
| 60 | + ([|]) |
| 61 | + ([arg] |
| 62 | + body))" |
| 63 | + |
| 64 | + (clojure-add-arity)) |
| 65 | + |
| 66 | + (when-refactoring-with-point-it "should add an arity to a single-arity defn with args on next line" |
| 67 | + "(defn foo |
| 68 | + [arg] |
| 69 | + bo|dy)" |
| 70 | + |
| 71 | + "(defn foo |
| 72 | + ([|]) |
| 73 | + ([arg] |
| 74 | + body))" |
| 75 | + |
| 76 | + (clojure-add-arity)) |
| 77 | + |
| 78 | + (when-refactoring-with-point-it "should handle a single-arity defn with a docstring" |
| 79 | + "(defn foo |
| 80 | + \"some docst|ring\" |
| 81 | + [arg] |
| 82 | + body)" |
| 83 | + |
| 84 | + "(defn foo |
| 85 | + \"some docstring\" |
| 86 | + ([|]) |
| 87 | + ([arg] |
| 88 | + body))" |
| 89 | + |
| 90 | + (clojure-add-arity)) |
| 91 | + |
| 92 | + (when-refactoring-with-point-it "should handle a single-arity defn with metadata" |
| 93 | + "(defn fo|o |
| 94 | + ^{:bla \"meta\"} |
| 95 | + [arg] |
| 96 | + body)" |
| 97 | + |
| 98 | + "(defn foo |
| 99 | + ^{:bla \"meta\"} |
| 100 | + ([|]) |
| 101 | + ([arg] |
| 102 | + body))" |
| 103 | + |
| 104 | + (clojure-add-arity)) |
| 105 | + |
| 106 | + (when-refactoring-with-point-it "should add an arity to a multi-arity defn" |
| 107 | + "(defn foo |
| 108 | + ([arg1]) |
| 109 | + ([ar|g1 arg2] |
| 110 | + body))" |
| 111 | + |
| 112 | + "(defn foo |
| 113 | + ([|]) |
| 114 | + ([arg1]) |
| 115 | + ([arg1 arg2] |
| 116 | + body))" |
| 117 | + |
| 118 | + (clojure-add-arity)) |
| 119 | + |
| 120 | + (when-refactoring-with-point-it "should handle a multi-arity defn with a docstring" |
| 121 | + "(defn foo |
| 122 | + \"some docstring\" |
| 123 | + ([]) |
| 124 | + ([arg|] |
| 125 | + body))" |
| 126 | + |
| 127 | + "(defn foo |
| 128 | + \"some docstring\" |
| 129 | + ([|]) |
| 130 | + ([]) |
| 131 | + ([arg] |
| 132 | + body))" |
| 133 | + |
| 134 | + (clojure-add-arity)) |
| 135 | + |
| 136 | + (when-refactoring-with-point-it "should handle a multi-arity defn with metadata" |
| 137 | + "(defn foo |
| 138 | + \"some docstring\" |
| 139 | + ^{:bla \"meta\"} |
| 140 | + ([]) |
| 141 | + |([arg] |
| 142 | + body))" |
| 143 | + |
| 144 | + "(defn foo |
| 145 | + \"some docstring\" |
| 146 | + ^{:bla \"meta\"} |
| 147 | + ([|]) |
| 148 | + ([]) |
| 149 | + ([arg] |
| 150 | + body))" |
| 151 | + |
| 152 | + (clojure-add-arity))) |
| 153 | + |
| 154 | +(provide 'clojure-mode-refactor-add-arity-test) |
| 155 | + |
| 156 | +;;; clojure-mode-refactor-add-arity-test.el ends here |
0 commit comments