Skip to content

Commit c870999

Browse files
committed
Map tests
1 parent cc39dbd commit c870999

File tree

2 files changed

+53
-11
lines changed

2 files changed

+53
-11
lines changed

src/basilisp/pprint.lpy

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,12 @@
5353
*print-right-margin*
5454
72)
5555

56+
(def ^{:doc "If bound to ``true``, associative collections will be printed in sorted
57+
order by their keys. Default is ``false``."
58+
:dynamic true}
59+
*print-sort-keys*
60+
false)
61+
5662
(def ^{:doc "If ``true``, suppress printing symbol namespaces. This may be useful when
5763
printing macroexpansions.
5864

@@ -573,17 +579,25 @@
573579
(defn ^:private print-map
574580
"Print an associative collection."
575581
[prefix suffix obj]
576-
(pprint-logical-block :prefix prefix :suffix suffix
577-
(print-length-loop [m obj]
578-
(when (seq m)
579-
(let [[k v] (first m)]
580-
(write-out k)
581-
(.write *out* " ")
582-
(write-out v)
583-
(pprint-newline :linear)
584-
(when-let [more (seq (rest m))]
585-
(.write *out* " ")
586-
(recur more)))))))
582+
(let [coll (if *print-sort-keys*
583+
(sort-by key obj)
584+
obj)]
585+
(pprint-logical-block :prefix prefix :suffix suffix
586+
(print-length-loop [m coll]
587+
(when (seq m)
588+
(let [[k v] (first m)]
589+
(pprint-logical-block
590+
(write-out k)
591+
(.write *out* " ")
592+
(pprint-newline :linear)
593+
;; set the current length such that we won't print
594+
;; only a key without it's corresponding value
595+
(binding [*current-length* (dec *current-length*)]
596+
(write-out v)))
597+
(when-let [more (seq (rest m))]
598+
(.write *out* " ")
599+
(pprint-newline :linear)
600+
(recur more))))))))
587601

588602
(defn ^:private print-meta
589603
"Print the metadata associated with an object if it has any metadata and if

tests/basilisp/test_pprint.lpy

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,34 @@
154154
"^{:a 1} #{:a}" ^{:a 1} #{:a}
155155
"^{:a true} #{:a}" ^:a #{:a})))
156156

157+
(deftest pprint-print-associative-test
158+
(let [long-map (into {} (map #(vector (keyword (python/chr %1)) %2)
159+
(range (python/ord "a") (python/ord "z"))
160+
(range)))]
161+
(are [res len expr] (= res (str/rtrim
162+
(binding [pprint/*print-sort-keys* true
163+
*print-length* len]
164+
(with-out-str
165+
(pprint/pprint expr)))))
166+
"{...}" 0 long-map
167+
"{:a 0 ...}" 1 long-map
168+
"{:a 0
169+
:b 1
170+
:c 2
171+
:d 3
172+
:e 4
173+
:f 5
174+
:g 6
175+
:h 7
176+
:i 8
177+
:j 9
178+
:k 10
179+
:l 11
180+
:m 12
181+
:n 13
182+
:o 14
183+
...}" 15 long-map)))
184+
157185
(deftest pprint-base-and-radix-test
158186
(are [res base expr] (= res (str/rtrim
159187
(binding [pprint/*print-radix* true

0 commit comments

Comments
 (0)