Skip to content

Commit

Permalink
[#18] pprint pt. 2
Browse files Browse the repository at this point in the history
* pprint support: Fix querying middleware

The pprint support erroneously used the value of the middleware as the
map to look into. Also, it seems that the type of the middleware is a
string, not a symbol.

This fixes sthis by swapping the argument order and changing the pretty
printer lookup up to use string keys.

* pprint support: wrap pprint to not use *out*

* pprint: Add test

* Test different pprint middleware settings
  • Loading branch information
Grazfather authored Feb 4, 2021
1 parent ba40b84 commit ecdccd4
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
9 changes: 5 additions & 4 deletions src/babashka/nrepl/impl/server.clj
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@
(set! *warn-on-reflection* true)

(def pretty-print-fns-map
{'clojure.core/prn prn
'clojure.pprint/pprint pprint})
{"clojure.core/prn" prn
"clojure.pprint/pprint" pprint
"cider.nrepl.pprint/pprint" pprint})

(defn eval-msg [ctx o msg {:keys [debug] :as opts}]
(try
Expand Down Expand Up @@ -51,8 +52,8 @@
(utils/send o (utils/response-for msg
{"ns" (vars/current-ns-name)
"value" (if nrepl-pprint
(if-let [pprint-fn (get nrepl-pprint pretty-print-fns-map)]
(pprint-fn value)
(if-let [pprint-fn (get pretty-print-fns-map nrepl-pprint)]
(with-out-str (pprint-fn value))
(do
(when debug
(println "Pretty-Printing is only supported for clojure.core/prn and clojure.pprint/pprint."))
Expand Down
13 changes: 11 additions & 2 deletions test/babashka/nrepl/server_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,16 @@
"session" session
"id" (new-id!)})
(let [reply (read-reply in session @id)]
(is (= "\"/tmp/foo.clj\"" (:value reply))))))
(is (= "\"/tmp/foo.clj\"" (:value reply)))))
(testing "supports print middleware for pprint"
(doseq [print-fn ["clojure.pprint/pprint" "cider.nrepl.pprint/pprint"]]
(bencode/write-bencode os {"op" "eval"
"code" "{:a {:a 0} :b {:a 0} :c {:a 0 :b 1} :d {:a 0 :b 1} :e {:a 0 :b 1}}"
"nrepl.middleware.print/print" print-fn,
"session" session
"id" (new-id!)})
(let [reply (read-reply in session @id)]
(is (= "{:e {:b 1, :a 0},\n :c {:b 1, :a 0},\n :b {:a 0},\n :d {:b 1, :a 0},\n :a {:a 0}}\n" (:value reply))))))
(testing "load-file"
(bencode/write-bencode os {"op" "load-file" "file" "(ns foo) (defn foo [] :foo)" "session" session "id" (new-id!)})
(read-reply in session @id)
Expand Down Expand Up @@ -321,7 +330,7 @@
(testing "dynamic var can be set! if provided in :dynamic-vars option"
(bencode/write-bencode os {"op" "eval" "code" "(set! *warn-on-reflection* true)"
"session" session "id" (new-id!)})
(is (= "true" (:value (read-reply in session @id))))))))
(is (= "true" (:value (read-reply in session @id)))))))))

(deftest nrepl-server-test
(let [service (atom nil)]
Expand Down

0 comments on commit ecdccd4

Please sign in to comment.