Skip to content

Commit

Permalink
Merge pull request #284 from seanirby/add-atom-print-method
Browse files Browse the repository at this point in the history
Add print method and tests for atoms
  • Loading branch information
bbatsov committed Jan 9, 2016
2 parents e324c2b + 92a67b4 commit 8238274
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/cider/nrepl/print_method.clj
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
(ns cider.nrepl.print-method
(:require [clojure.string :as s])
(:import [clojure.lang AFunction MultiFn Namespace]
(:import [clojure.lang AFunction Atom MultiFn Namespace]
java.io.Writer))

;; Extending `print-method` defined in clojure.core, to provide
Expand All @@ -25,6 +25,13 @@
(do ~@(map #(list '.write 'w %) strings))
(#'clojure.core/print-object ~arg ~'w))))

;;; Atoms
;; Ex: #atom[{:foo :bar} 0x54274a2b]
(def-print-method Atom c
"#atom["
(print-str @c)
(format " 0x%x]" (System/identityHashCode c)))

;;; Function objects
;; Ex: #function[cider.nrepl.print-method/multifn-name]
(def-print-method AFunction c
Expand Down
6 changes: 6 additions & 0 deletions test/clj/cider/nrepl/print_method_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@

(defn dummy-fn [o])

(deftest print-atoms
(is (re-find #"#atom\[ 0x[a-z0-9]+\]" (pr-str (atom ""))))
(is (re-find #"#atom\[nil 0x[a-z0-9]+\]" (pr-str (atom nil))))
(is (re-find #"#atom\[\{:foo :bar\} 0x[a-z0-9]+\]" (pr-str (atom {:foo :bar}))))
(is (re-find #"#atom\[#function\[clojure.core/\+\] 0x[a-z0-9]+\]" (pr-str (atom +)))))

(deftest print-functions
(are [f s] (= (pr-str f) s)
print-functions "#function[cider.nrepl.print-method-test/print-functions]"
Expand Down

0 comments on commit 8238274

Please sign in to comment.