Skip to content

Commit 953feb0

Browse files
arichiardibbatsov
authored andcommitted
Improve command sanitation code
This patch improves the parsing and sanitation of command and make sure, with tests, that we do things correctly.
1 parent 6c719c6 commit 953feb0

File tree

3 files changed

+26
-4
lines changed

3 files changed

+26
-4
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
## master (unreleased)
44

5+
* [#135](https://github.com/clojure-emacs/inf-clojure/pull/135): Improve command sanitation code.
6+
57
## 2.1.0 (2018-01-02)
68

79
### New Features

inf-clojure.el

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -307,10 +307,10 @@ It requires a REPL PROC for inspecting the correct type."
307307

308308
(defun inf-clojure--single-linify (string)
309309
"Convert a multi-line STRING in a single-line STRING.
310-
It also reduces/adds redundant whitespace for readability. Note
311-
that this function will transform the empty string in \" \" (it
312-
adds an empty space)."
313-
(replace-regexp-in-string "[ \\|\n]+" " " string))
310+
It also reduces redundant whitespace for readability."
311+
(thread-last string
312+
(replace-regexp-in-string "[ \\|\n]+" " ")
313+
(replace-regexp-in-string " $" "")))
314314

315315
(defun inf-clojure--trim-newline-right (string)
316316
"Trim newlines (only) in STRING."

test/inf-clojure-tests.el

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,4 +111,24 @@
111111
(expect (ict-bounds-string (inf-clojure-completion-bounds-of-expr-at-point))
112112
:to-equal "deref")))))
113113

114+
(describe "inf-clojure--single-linify"
115+
(it "replaces newlines with whitespace"
116+
(expect (inf-clojure--single-linify "(do\n(println \"hello world\")\n)") :to-equal "(do (println \"hello world\") )"))
117+
118+
(it "does not leave whitespace at the end"
119+
(expect (inf-clojure--single-linify "(do\n(println \"hello world\")\n)\n\n") :to-equal "(do (println \"hello world\") )"))
120+
121+
(it "returns empty string in case of only newline"
122+
(expect (inf-clojure--single-linify "\n\n\n\n") :to-equal "")))
123+
124+
(describe "inf-clojure--sanitize-command"
125+
(it "sanitizes the command correctly"
126+
(expect (inf-clojure--sanitize-command "(doc println)") :to-equal "(doc println)\n"))
127+
128+
(it "trims newline at the right of a command"
129+
(expect (inf-clojure--sanitize-command "(doc println)\n\n\n\n") :to-equal "(doc println)\n"))
130+
131+
(it "returns empty string when the command is empty"
132+
(expect (inf-clojure--sanitize-command " ") :to-equal "")))
133+
114134
;;; inf-clojure-tests.el ends here

0 commit comments

Comments
 (0)